use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class FastqToSam method doPaired.
/** More complicated method that takes two fastq files and builds pairing information in the SAM. */
protected int doPaired(final FastqReader freader1, final FastqReader freader2, final SAMFileWriter writer) {
int readCount = 0;
final ProgressLogger progress = new ProgressLogger(LOG);
for (; freader1.hasNext() && freader2.hasNext(); readCount++) {
final FastqRecord frec1 = freader1.next();
final FastqRecord frec2 = freader2.next();
final String frec1Name = getReadName(frec1.getReadName(), true);
final String frec2Name = getReadName(frec2.getReadName(), true);
final String baseName = getBaseName(frec1Name, frec2Name, freader1, freader2);
final SAMRecord srec1 = createSamRecord(writer.getFileHeader(), baseName, frec1, true);
srec1.setFirstOfPairFlag(true);
srec1.setSecondOfPairFlag(false);
writer.addAlignment(srec1);
progress.record(srec1);
final SAMRecord srec2 = createSamRecord(writer.getFileHeader(), baseName, frec2, true);
srec2.setFirstOfPairFlag(false);
srec2.setSecondOfPairFlag(true);
writer.addAlignment(srec2);
progress.record(srec2);
}
writer.close();
if (freader1.hasNext() || freader2.hasNext()) {
throw new UserException("Input paired fastq files must be the same length");
}
return readCount;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class FastqToSam method getReadNameTokens.
/** Breaks up read name into baseName and number separated by the last / */
private String[] getReadNameTokens(final String readName, final int pairNum, final FastqReader freader) {
if (readName.equals(""))
throw new UserException(error(freader, "Pair read name " + pairNum + " cannot be empty: " + readName));
final int idx = readName.lastIndexOf('/');
final String[] result = new String[2];
if (idx == -1) {
result[0] = readName;
result[1] = null;
} else {
// should be a 1 or 2
result[1] = readName.substring(idx + 1, readName.length());
if (!result[1].equals("1") && !result[1].equals("2")) {
//if not a 1 or 2 then names must be identical
result[0] = readName;
result[1] = null;
} else {
// baseName
result[0] = readName.substring(0, idx);
}
}
return result;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class FilterReads method doWork.
@Override
protected Object doWork() {
try {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertFileIsWritable(OUTPUT);
if (WRITE_READS_FILES)
writeReadsFile(INPUT);
switch(FILTER) {
case includeAligned:
filterReads(new FilteringSamIterator(SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT).iterator(), new AlignedFilter(true), true));
break;
case excludeAligned:
filterReads(new FilteringSamIterator(SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT).iterator(), new AlignedFilter(false), true));
break;
case includeReadList:
filterReads(new FilteringSamIterator(SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT).iterator(), new ReadNameFilter(READ_LIST_FILE, true)));
break;
case excludeReadList:
filterReads(new FilteringSamIterator(SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT).iterator(), new ReadNameFilter(READ_LIST_FILE, false)));
break;
default:
throw new UnsupportedOperationException(FILTER.name() + " has not been implemented!");
}
IOUtil.assertFileIsReadable(OUTPUT);
if (WRITE_READS_FILES)
writeReadsFile(OUTPUT);
} catch (IOException e) {
if (OUTPUT.exists() && !OUTPUT.delete()) {
throw new UserException("Failed to delete existing output: " + OUTPUT.getAbsolutePath());
} else {
throw new UserException("Failed to filter " + INPUT.getName());
}
}
return null;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class ReorderSam method buildSequenceDictionaryMap.
/**
* Constructs a mapping from read sequence records index -> new sequence dictionary index for use in
* reordering the reference index and mate reference index in each read. -1 means unmapped.
*/
private Map<Integer, Integer> buildSequenceDictionaryMap(final SAMSequenceDictionary refDict, final SAMSequenceDictionary readsDict) {
Map<Integer, Integer> newOrder = new HashMap<>();
logger.info("Reordering SAM/BAM file:");
for (final SAMSequenceRecord refRec : refDict.getSequences()) {
final SAMSequenceRecord readsRec = readsDict.getSequence(refRec.getSequenceName());
if (readsRec != null) {
if (refRec.getSequenceLength() != readsRec.getSequenceLength()) {
String msg = String.format("Discordant contig lengths: read %s LN=%d, ref %s LN=%d", readsRec.getSequenceName(), readsRec.getSequenceLength(), refRec.getSequenceName(), refRec.getSequenceLength());
if (ALLOW_CONTIG_LENGTH_DISCORDANCE) {
logger.warn(msg);
} else {
throw new UserException(msg);
}
}
logger.info(String.format(" Reordering read contig %s [index=%d] to => ref contig %s [index=%d]%n", readsRec.getSequenceName(), readsRec.getSequenceIndex(), refRec.getSequenceName(), refRec.getSequenceIndex()));
newOrder.put(readsRec.getSequenceIndex(), refRec.getSequenceIndex());
}
}
for (SAMSequenceRecord readsRec : readsDict.getSequences()) {
if (!newOrder.containsKey(readsRec.getSequenceIndex())) {
if (ALLOW_INCOMPLETE_DICT_CONCORDANCE)
newOrder.put(readsRec.getSequenceIndex(), -1);
else
throw new UserException("New reference sequence does not contain a matching contig for " + readsRec.getSequenceName());
}
}
return newOrder;
}
use of org.broadinstitute.hellbender.exceptions.UserException in project gatk by broadinstitute.
the class BucketUtils method dirSize.
/**
* Returns the total file size of all files in a directory, or the file size if the path specifies a file.
* Note that sub-directories are ignored - they are not recursed into.
* Only supports HDFS and local paths.
*
* @param path The URL to the file or directory whose size to return
* @return the total size of all files in bytes
*/
public static long dirSize(String path) {
try {
// GCS case (would work with local too)
if (isCloudStorageUrl(path)) {
java.nio.file.Path p = getPathOnGcs(path);
return Files.list(p).mapToLong(q -> {
try {
return (Files.isRegularFile(q) ? Files.size(q) : 0);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}).sum();
}
// local file or HDFS case
Path hadoopPath = new Path(path);
FileSystem fs = new Path(path).getFileSystem(new Configuration());
FileStatus status = fs.getFileStatus(hadoopPath);
if (status == null) {
throw new UserException.CouldNotReadInputFile(path, "File not found.");
}
long size = 0;
if (status.isDirectory()) {
for (FileStatus st : fs.listStatus(status.getPath())) {
if (st.isFile()) {
size += st.getLen();
}
}
} else {
size += status.getLen();
}
return size;
} catch (RuntimeIOException | IOException e) {
throw new UserException("Failed to determine total input size of " + path + "\n Caused by:" + e.getMessage(), e);
}
}
Aggregations