Search in sources :

Example 61 with GATKException

use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.

the class ReadClipper method clipByReferenceCoordinates.

/**
     * Generic functionality to  clip a read, used internally by hardClipByReferenceCoordinatesLeftTail
     * and hardClipByReferenceCoordinatesRightTail. Should not be used directly.
     *
     * Note, it REQUIRES you to give the directionality of your hard clip (i.e. whether you're clipping the
     * left of right tail) by specifying either refStart < 0 or refStop < 0.
     *
     * @param refStart  first base to clip (inclusive)
     * @param refStop last base to clip (inclusive)
     * @param clippingOp clipping operation to be performed
     * @return a new read, without the clipped bases
     */
protected GATKRead clipByReferenceCoordinates(final int refStart, final int refStop, ClippingRepresentation clippingOp, boolean runAsserts) {
    if (read.isEmpty()) {
        return read;
    }
    if ((clippingOp == ClippingRepresentation.SOFTCLIP_BASES) && read.isUnmapped()) {
        throw new GATKException("Cannot softclip read " + read.commonToString() + " by reference coordinates because it is unmapped");
    }
    final int start;
    final int stop;
    // Determine the read coordinate to start and stop hard clipping
    if (refStart < 0) {
        if (refStop < 0) {
            throw new GATKException("Only one of refStart or refStop must be < 0, not both (" + refStart + ", " + refStop + ")");
        }
        start = 0;
        stop = ReadUtils.getReadCoordinateForReferenceCoordinate(read, refStop, ReadUtils.ClippingTail.LEFT_TAIL);
    } else {
        if (refStop >= 0) {
            throw new GATKException("Either refStart or refStop must be < 0 (" + refStart + ", " + refStop + ")");
        }
        start = ReadUtils.getReadCoordinateForReferenceCoordinate(read, refStart, ReadUtils.ClippingTail.RIGHT_TAIL);
        stop = read.getLength() - 1;
    }
    if (start < 0 || stop > read.getLength() - 1) {
        throw new GATKException("Trying to clip before the start or after the end of a read");
    }
    if (start > stop) {
        throw new GATKException(String.format("START (%d) > (%d) STOP -- this should never happen, please check read: %s (CIGAR: %s)", start, stop, read, read.getCigar().toString()));
    }
    if (start > 0 && stop < read.getLength() - 1) {
        throw new GATKException(String.format("Trying to clip the middle of the read: start %d, stop %d, cigar: %s", start, stop, read.getCigar().toString()));
    }
    this.addOp(new ClippingOp(start, stop));
    final GATKRead clippedRead = clipRead(clippingOp, runAsserts);
    this.ops = null;
    return clippedRead;
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 62 with GATKException

use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk-protected by broadinstitute.

the class ConvertGSVariantsToSegments method calculateLog10CallQuality.

private double calculateLog10CallQuality(final double[] log10Probs, final CopyNumberTriState call) {
    final IntRange callCopyNumberRange;
    switch(call) {
        case NEUTRAL:
            callCopyNumberRange = new IntRange(neutralCopyNumber, neutralCopyNumber);
            break;
        case DELETION:
            callCopyNumberRange = new IntRange(0, neutralCopyNumber - 1);
            break;
        case DUPLICATION:
            callCopyNumberRange = new IntRange(neutralCopyNumber + 1, log10Probs.length - 1);
            break;
        default:
            throw new GATKException("unexpected call");
    }
    // We aggregate the probs of any copy number that that does not correspond
    final double log10OneMinusProbCall = MathUtils.approximateLog10SumLog10(MathUtils.log10SumLog10(log10Probs, 0, Math.min(callCopyNumberRange.getMinimumInteger(), log10Probs.length)), MathUtils.log10SumLog10(log10Probs, callCopyNumberRange.getMaximumInteger() + 1, log10Probs.length));
    final double log10ProbTotal = MathUtils.log10SumLog10(log10Probs);
    return log10OneMinusProbCall - log10ProbTotal;
}
Also used : IntRange(org.apache.commons.lang.math.IntRange) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 63 with GATKException

use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.

the class BwaAndMarkDuplicatesPipelineSpark method runTool.

@Override
protected void runTool(final JavaSparkContext ctx) {
    try (final BwaSparkEngine engine = new BwaSparkEngine(ctx, indexImageFile, getHeaderForReads(), getReferenceSequenceDictionary())) {
        final JavaRDD<GATKRead> alignedReads = engine.align(getReads());
        final JavaRDD<GATKRead> markedReadsWithOD = MarkDuplicatesSpark.mark(alignedReads, engine.getHeader(), duplicatesScoringStrategy, new OpticalDuplicateFinder(), getRecommendedNumReducers());
        final JavaRDD<GATKRead> markedReads = MarkDuplicatesSpark.cleanupTemporaryAttributes(markedReadsWithOD);
        try {
            ReadsSparkSink.writeReads(ctx, output, referenceArguments.getReferenceFile().getAbsolutePath(), markedReads, engine.getHeader(), shardedOutput ? ReadsWriteFormat.SHARDED : ReadsWriteFormat.SINGLE, getRecommendedNumReducers());
        } catch (IOException e) {
            throw new GATKException("unable to write bam: " + e);
        }
    }
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) OpticalDuplicateFinder(org.broadinstitute.hellbender.utils.read.markduplicates.OpticalDuplicateFinder) BwaSparkEngine(org.broadinstitute.hellbender.tools.spark.bwa.BwaSparkEngine) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 64 with GATKException

use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.

the class AlignedAssemblyOrExcuse method writeSAMFile.

/**
     * write a SAM file containing records for each aligned contig
     */
static void writeSAMFile(final String samFile, final SAMFileHeader header, final List<AlignedAssemblyOrExcuse> alignedAssemblyOrExcuseList) {
    try (final OutputStream os = BucketUtils.createFile(samFile)) {
        final SAMTextWriter writer = new SAMTextWriter(os);
        writer.setSortOrder(SAMFileHeader.SortOrder.queryname, true);
        writer.setHeader(header);
        final List<String> refNames = getRefNames(header);
        alignedAssemblyOrExcuseList.stream().filter(AlignedAssemblyOrExcuse::isNotFailure).flatMap(aa -> aa.toSAMStreamForAlignmentsOfThisAssembly(header, refNames)).forEach(writer::addAlignment);
        writer.finish();
    } catch (final IOException ioe) {
        throw new GATKException("Can't write SAM file of aligned contigs.", ioe);
    }
}
Also used : IntStream(java.util.stream.IntStream) Output(com.esotericsoftware.kryo.io.Output) java.util(java.util) SAMFileHeader(htsjdk.samtools.SAMFileHeader) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) BwaMemAlignment(org.broadinstitute.hellbender.utils.bwa.BwaMemAlignment) Kryo(com.esotericsoftware.kryo.Kryo) BufferedOutputStream(java.io.BufferedOutputStream) FermiLiteAssembly(org.broadinstitute.hellbender.utils.fermi.FermiLiteAssembly) BucketUtils(org.broadinstitute.hellbender.utils.gcs.BucketUtils) Input(com.esotericsoftware.kryo.io.Input) SAMTextWriter(htsjdk.samtools.SAMTextWriter) OutputStreamWriter(java.io.OutputStreamWriter) OutputStream(java.io.OutputStream) DefaultSerializer(com.esotericsoftware.kryo.DefaultSerializer) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SAMRecord(htsjdk.samtools.SAMRecord) Stream(java.util.stream.Stream) Contig(org.broadinstitute.hellbender.utils.fermi.FermiLiteAssembly.Contig) PipelineOptions(com.google.cloud.dataflow.sdk.options.PipelineOptions) Utils(org.broadinstitute.hellbender.utils.Utils) BwaMemAlignmentUtils(org.broadinstitute.hellbender.utils.bwa.BwaMemAlignmentUtils) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) Connection(org.broadinstitute.hellbender.utils.fermi.FermiLiteAssembly.Connection) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) SAMTextWriter(htsjdk.samtools.SAMTextWriter) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 65 with GATKException

use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.

the class AlignedAssemblyOrExcuse method writeIntervalFile.

/**
     * write a file describing each interval
     */
public static void writeIntervalFile(final String intervalFile, final SAMFileHeader header, final List<SVInterval> intervals, final List<AlignedAssemblyOrExcuse> intervalDispositions) {
    final Map<Integer, AlignedAssemblyOrExcuse> resultsMap = new HashMap<>();
    intervalDispositions.forEach(alignedAssemblyOrExcuse -> resultsMap.put(alignedAssemblyOrExcuse.getAssemblyId(), alignedAssemblyOrExcuse));
    try (final OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(BucketUtils.createFile(intervalFile)))) {
        final List<SAMSequenceRecord> contigs = header.getSequenceDictionary().getSequences();
        final int nIntervals = intervals.size();
        for (int intervalId = 0; intervalId != nIntervals; ++intervalId) {
            final SVInterval interval = intervals.get(intervalId);
            final String seqName = contigs.get(interval.getContig()).getSequenceName();
            final AlignedAssemblyOrExcuse alignedAssemblyOrExcuse = resultsMap.get(intervalId);
            final String disposition;
            if (alignedAssemblyOrExcuse == null) {
                disposition = "unknown";
            } else if (alignedAssemblyOrExcuse.getErrorMessage() != null) {
                disposition = alignedAssemblyOrExcuse.getErrorMessage();
            } else {
                disposition = "produced " + alignedAssemblyOrExcuse.getAssembly().getNContigs() + " contigs";
            }
            writer.write(intervalId + "\t" + seqName + ":" + interval.getStart() + "-" + interval.getEnd() + "\t" + disposition + "\n");
        }
    } catch (final IOException ioe) {
        throw new GATKException("Can't write intervals file " + intervalFile, ioe);
    }
}
Also used : SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

GATKException (org.broadinstitute.hellbender.exceptions.GATKException)96 IOException (java.io.IOException)19 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)13 CigarElement (htsjdk.samtools.CigarElement)12 ArrayList (java.util.ArrayList)10 UserException (org.broadinstitute.hellbender.exceptions.UserException)10 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)8 Cigar (htsjdk.samtools.Cigar)7 File (java.io.File)6 SAMFileHeader (htsjdk.samtools.SAMFileHeader)5 OutputStream (java.io.OutputStream)5 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Utils (org.broadinstitute.hellbender.utils.Utils)4 Tuple2 (scala.Tuple2)4 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)3 BufferedOutputStream (java.io.BufferedOutputStream)3 InputStream (java.io.InputStream)3 BigInteger (java.math.BigInteger)3 java.util (java.util)3