Search in sources :

Example 61 with UserException

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;
}
Also used : FastqRecord(htsjdk.samtools.fastq.FastqRecord) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 62 with UserException

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;
}
Also used : UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 63 with UserException

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;
}
Also used : AlignedFilter(htsjdk.samtools.filter.AlignedFilter) FilteringSamIterator(htsjdk.samtools.filter.FilteringSamIterator) ReadNameFilter(htsjdk.samtools.filter.ReadNameFilter) IOException(java.io.IOException) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 64 with UserException

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;
}
Also used : HashMap(java.util.HashMap) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 65 with UserException

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);
    }
}
Also used : Arrays(java.util.Arrays) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) FileSystem(org.apache.hadoop.fs.FileSystem) StorageOptions(com.google.cloud.storage.StorageOptions) FileStatus(org.apache.hadoop.fs.FileStatus) TabixUtils(htsjdk.tribble.util.TabixUtils) GoogleCredentials(shaded.cloud_nio.com.google.auth.oauth2.GoogleCredentials) AbstractFeatureReader(htsjdk.tribble.AbstractFeatureReader) HttpTransportOptions(com.google.cloud.HttpTransportOptions) CloudStorageConfiguration(com.google.cloud.storage.contrib.nio.CloudStorageConfiguration) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) RetrySettings(shaded.cloud_nio.com.google.api.gax.core.RetrySettings) Configuration(org.apache.hadoop.conf.Configuration) Path(org.apache.hadoop.fs.Path) Credentials(shaded.cloud_nio.com.google.auth.Credentials) Files(java.nio.file.Files) IOUtils(org.broadinstitute.hellbender.utils.io.IOUtils) UUID(java.util.UUID) Logger(org.apache.logging.log4j.Logger) UserException(org.broadinstitute.hellbender.exceptions.UserException) java.io(java.io) ByteStreams(com.google.common.io.ByteStreams) Duration(shaded.cloud_nio.org.joda.time.Duration) Utils(org.broadinstitute.hellbender.utils.Utils) Tribble(htsjdk.tribble.Tribble) LogManager(org.apache.logging.log4j.LogManager) Path(org.apache.hadoop.fs.Path) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) FileStatus(org.apache.hadoop.fs.FileStatus) CloudStorageConfiguration(com.google.cloud.storage.contrib.nio.CloudStorageConfiguration) Configuration(org.apache.hadoop.conf.Configuration) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) CloudStorageFileSystem(com.google.cloud.storage.contrib.nio.CloudStorageFileSystem) FileSystem(org.apache.hadoop.fs.FileSystem) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Aggregations

UserException (org.broadinstitute.hellbender.exceptions.UserException)100 File (java.io.File)30 IOException (java.io.IOException)30 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)14 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)11 SamReader (htsjdk.samtools.SamReader)10 VariantContext (htsjdk.variant.variantcontext.VariantContext)10 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)10 SAMRecord (htsjdk.samtools.SAMRecord)9 IntervalList (htsjdk.samtools.util.IntervalList)9 List (java.util.List)9 SAMFileHeader (htsjdk.samtools.SAMFileHeader)8 ReferenceSequenceFileWalker (htsjdk.samtools.reference.ReferenceSequenceFileWalker)8 SamLocusIterator (htsjdk.samtools.util.SamLocusIterator)8 LogManager (org.apache.logging.log4j.LogManager)8 Logger (org.apache.logging.log4j.Logger)8 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)7 MetricsFile (htsjdk.samtools.metrics.MetricsFile)6 SamRecordFilter (htsjdk.samtools.filter.SamRecordFilter)5 FileNotFoundException (java.io.FileNotFoundException)5