use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class RnaSeqMetricsCollector method makeOverlapDetector.
public static OverlapDetector<Interval> makeOverlapDetector(final File samFile, final SAMFileHeader header, final File ribosomalIntervalsFile) {
OverlapDetector<Interval> ribosomalSequenceOverlapDetector = new OverlapDetector<>(0, 0);
if (ribosomalIntervalsFile != null) {
final IntervalList ribosomalIntervals = IntervalList.fromFile(ribosomalIntervalsFile);
try {
SequenceUtil.assertSequenceDictionariesEqual(header.getSequenceDictionary(), ribosomalIntervals.getHeader().getSequenceDictionary());
} catch (SequenceUtil.SequenceListsDifferException e) {
throw new UserException("Sequence dictionaries differ in " + samFile.getAbsolutePath() + " and " + ribosomalIntervalsFile.getAbsolutePath(), e);
}
final IntervalList uniquedRibosomalIntervals = ribosomalIntervals.uniqued();
final List<Interval> intervals = uniquedRibosomalIntervals.getIntervals();
ribosomalSequenceOverlapDetector.addAll(intervals, intervals);
}
return ribosomalSequenceOverlapDetector;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class SamToFastq method doWork.
@Override
protected Object doWork() {
IOUtil.assertFileIsReadable(INPUT);
final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
final Map<String, SAMRecord> firstSeenMates = new HashMap<>();
final FastqWriterFactory factory = new FastqWriterFactory();
factory.setCreateMd5(CREATE_MD5_FILE);
final Map<SAMReadGroupRecord, FastqWriters> writers = generateWriters(reader.getFileHeader().getReadGroups(), factory);
final ProgressLogger progress = new ProgressLogger(logger);
for (final SAMRecord currentRecord : reader) {
if (currentRecord.isSecondaryOrSupplementary() && !INCLUDE_NON_PRIMARY_ALIGNMENTS)
continue;
// Skip non-PF reads as necessary
if (currentRecord.getReadFailsVendorQualityCheckFlag() && !INCLUDE_NON_PF_READS)
continue;
final FastqWriters fq = writers.get(currentRecord.getReadGroup());
if (currentRecord.getReadPairedFlag()) {
final String currentReadName = currentRecord.getReadName();
final SAMRecord firstRecord = firstSeenMates.remove(currentReadName);
if (firstRecord == null) {
firstSeenMates.put(currentReadName, currentRecord);
} else {
assertPairedMates(firstRecord, currentRecord);
final SAMRecord read1 = currentRecord.getFirstOfPairFlag() ? currentRecord : firstRecord;
final SAMRecord read2 = currentRecord.getFirstOfPairFlag() ? firstRecord : currentRecord;
writeRecord(read1, 1, fq.getFirstOfPair(), READ1_TRIM, READ1_MAX_BASES_TO_WRITE);
final FastqWriter secondOfPairWriter = fq.getSecondOfPair();
if (secondOfPairWriter == null) {
throw new UserException("Input contains paired reads but no SECOND_END_FASTQ specified.");
}
writeRecord(read2, 2, secondOfPairWriter, READ2_TRIM, READ2_MAX_BASES_TO_WRITE);
}
} else {
writeRecord(currentRecord, null, fq.getUnpaired(), READ1_TRIM, READ1_MAX_BASES_TO_WRITE);
}
progress.record(currentRecord);
}
CloserUtil.close(reader);
// Close all the fastq writers being careful to close each one only once!
for (final FastqWriters writerMapping : new HashSet<>(writers.values())) {
writerMapping.closeAll();
}
if (!firstSeenMates.isEmpty()) {
SAMUtils.processValidationError(new SAMValidationError(SAMValidationError.Type.MATE_NOT_FOUND, "Found " + firstSeenMates.size() + " unpaired mates", null), VALIDATION_STRINGENCY);
}
return null;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class SamToFastq method makeReadGroupFile.
private File makeReadGroupFile(final SAMReadGroupRecord readGroup, final String preExtSuffix) {
String fileName = null;
if (RG_TAG.equalsIgnoreCase("PU")) {
fileName = readGroup.getPlatformUnit();
} else if (RG_TAG.equalsIgnoreCase("ID")) {
fileName = readGroup.getReadGroupId();
}
if (fileName == null) {
throw new UserException("The selected RG_TAG: " + RG_TAG + " is not present in the bam header.");
}
fileName = IOUtil.makeFileNameSafe(fileName);
if (preExtSuffix != null)
fileName += preExtSuffix;
fileName += ".fastq";
final File result = (OUTPUT_DIR != null) ? new File(OUTPUT_DIR, fileName) : new File(fileName);
IOUtil.assertFileIsWritable(result);
return result;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class CalculateReadGroupChecksum method doWork.
@Override
protected Object doWork() {
final File output = (OUTPUT == null) ? new File(INPUT.getParentFile(), getOutputFileName(INPUT)) : OUTPUT;
IOUtil.assertFileIsWritable(output);
final String hashText = SAMUtils.calculateReadGroupRecordChecksum(INPUT, REFERENCE_SEQUENCE);
try (final FileWriter outputWriter = new FileWriter(output)) {
outputWriter.write(hashText);
} catch (final IOException ioe) {
throw new UserException("Could not write the computed hash (" + hashText + ") to the output file: " + ioe.getMessage(), ioe);
}
return null;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class FastqToSam method getBaseName.
/** Returns read baseName and asserts correct pair read name format:
* <ul>
* <li> Paired reads must either have the exact same read names or they must contain at least one "/"
* <li> and the First pair read name must end with "/1" and second pair read name ends with "/2"
* <li> The baseName (read name part before the /) must be the same for both read names
* <li> If the read names are exactly the same but end in "/2" or "/1" then an exception will be thrown
* </ul>
*/
String getBaseName(final String readName1, final String readName2, final FastqReader freader1, final FastqReader freader2) {
String[] toks = getReadNameTokens(readName1, 1, freader1);
final String baseName1 = toks[0];
final String num1 = toks[1];
toks = getReadNameTokens(readName2, 2, freader2);
final String baseName2 = toks[0];
final String num2 = toks[1];
if (!baseName1.equals(baseName2)) {
throw new UserException(String.format("In paired mode, read name 1 (%s) does not match read name 2 (%s)", baseName1, baseName2));
}
final boolean num1Blank = StringUtil.isBlank(num1);
final boolean num2Blank = StringUtil.isBlank(num2);
if (num1Blank || num2Blank) {
if (//num1 != blank and num2 == blank
!num1Blank)
//num1 != blank and num2 == blank
throw new UserException(error(freader1, "Pair 1 number is missing (" + readName1 + "). Both pair numbers must be present or neither."));
else //num1 == blank and num =2 != blank
if (!num2Blank)
throw new UserException(error(freader2, "Pair 2 number is missing (" + readName2 + "). Both pair numbers must be present or neither."));
} else {
if (!num1.equals("1"))
throw new UserException(error(freader1, "Pair 1 number must be 1 (" + readName1 + ")"));
if (!num2.equals("2"))
throw new UserException(error(freader2, "Pair 2 number must be 2 (" + readName2 + ")"));
}
return baseName1;
}
Aggregations