|
39 | 39 | import java.util.ArrayList; |
40 | 40 | import java.util.Arrays; |
41 | 41 | import java.util.List; |
| 42 | +import java.util.Set; |
| 43 | +import java.util.stream.Collectors; |
42 | 44 |
|
43 | 45 | public class UpdateReadsetFilesHandler extends AbstractParameterizedOutputHandler<SequenceOutputHandler.SequenceOutputProcessor> |
44 | 46 | { |
@@ -119,17 +121,25 @@ private SAMFileHeader getAndValidateHeaderForBam(SequenceOutputFile so, String n |
119 | 121 | try (SamReader reader = samReaderFactory.open(so.getFile())) |
120 | 122 | { |
121 | 123 | SAMFileHeader header = reader.getFileHeader().clone(); |
122 | | - int nSamples = reader.getFileHeader().getReadGroups().size(); |
123 | | - if (nSamples != 1) |
| 124 | + List<SAMReadGroupRecord> rgs = header.getReadGroups(); |
| 125 | + Set<String> distinctLibraries = rgs.stream().map(SAMReadGroupRecord::getLibrary).collect(Collectors.toSet()); |
| 126 | + if (distinctLibraries.size() > 1) |
124 | 127 | { |
125 | | - throw new PipelineJobException("File has more than one read group, found: " + nSamples); |
| 128 | + throw new PipelineJobException("File has more than one library in read group(s), found: " + distinctLibraries.stream().collect(Collectors.joining(", "))); |
126 | 129 | } |
127 | 130 |
|
128 | | - List<SAMReadGroupRecord> rgs = header.getReadGroups(); |
129 | | - String existingSample = rgs.get(0).getSample(); |
130 | | - if (existingSample.equals(newRsName)) |
| 131 | + Set<String> distinctSamples = rgs.stream().map(SAMReadGroupRecord::getSample).collect(Collectors.toSet()); |
| 132 | + if (distinctSamples.size() > 1) |
131 | 133 | { |
132 | | - throw new PipelineJobException("Sample names match, aborting"); |
| 134 | + throw new PipelineJobException("File has more than one sample in read group(s), found: " + distinctSamples.stream().collect(Collectors.joining(", "))); |
| 135 | + } |
| 136 | + |
| 137 | + if ( |
| 138 | + distinctLibraries.stream().filter(x -> !x.equals(newRsName)).count() == 0L && |
| 139 | + distinctSamples.stream().filter(x -> !x.equals(newRsName)).count() == 0L |
| 140 | + ) |
| 141 | + { |
| 142 | + throw new PipelineJobException("Sample and library names match in read group(s), aborting"); |
133 | 143 | } |
134 | 144 |
|
135 | 145 | return header; |
@@ -252,13 +262,23 @@ private void reheaderBamOrCram(SequenceOutputFile so, JobContext ctx, String new |
252 | 262 |
|
253 | 263 | List<SAMReadGroupRecord> rgs = header.getReadGroups(); |
254 | 264 | String existingSample = rgs.get(0).getSample(); |
255 | | - rgs.get(0).setSample(newRsName); |
| 265 | + String existingLibrary = rgs.get(0).getLibrary(); |
| 266 | + rgs.forEach(rg -> { |
| 267 | + rg.setSample(newRsName); |
| 268 | + rg.setLibrary(newRsName); |
| 269 | + }); |
256 | 270 |
|
257 | 271 | File headerBam = new File(ctx.getWorkingDirectory(), "header.bam"); |
258 | 272 | try (SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(header, false, headerBam)) |
259 | 273 | { |
260 | 274 |
|
261 | 275 | } |
| 276 | + |
| 277 | + if (!headerBam.exists()) |
| 278 | + { |
| 279 | + throw new PipelineJobException("Expected header was not created: " + headerBam.getPath()); |
| 280 | + } |
| 281 | + |
262 | 282 | ctx.getFileManager().addIntermediateFile(headerBam); |
263 | 283 | ctx.getFileManager().addIntermediateFile(SequencePipelineService.get().getExpectedIndex(headerBam)); |
264 | 284 |
|
|
0 commit comments