Search in sources :

Example 76 with GATKException

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

the class ReadEndsForMarkDuplicatesCodec method decode.

@Override
public ReadEndsForMarkDuplicates decode() {
    final ReadEndsForMarkDuplicates read = new ReadEndsForMarkDuplicates();
    try {
        // If the first read results in an EOF we've exhausted the stream
        try {
            read.score = this.in.readShort();
        } catch (final EOFException eof) {
            return null;
        }
        read.libraryId = this.in.readShort();
        read.orientation = this.in.readByte();
        read.read1ReferenceIndex = this.in.readInt();
        read.read1Coordinate = this.in.readInt();
        read.read1IndexInFile = this.in.readLong();
        read.read2ReferenceIndex = this.in.readInt();
        if (read.orientation > ReadEnds.R) {
            read.read2Coordinate = this.in.readInt();
            read.read2IndexInFile = this.in.readLong();
        }
        read.readGroup = this.in.readShort();
        read.tile = this.in.readShort();
        read.x = this.in.readShort();
        read.y = this.in.readShort();
        read.orientationForOpticalDuplicates = this.in.readByte();
        return read;
    } catch (final IOException ioe) {
        throw new GATKException("Exception writing ReadEnds to file.", ioe);
    }
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 77 with GATKException

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

the class SWPairwiseAlignment method printAlignment.

public void printAlignment(final byte[] ref, final byte[] read, final int width) {
    final StringBuilder bread = new StringBuilder();
    final StringBuilder bref = new StringBuilder();
    final StringBuilder match = new StringBuilder();
    int i = 0;
    int j = 0;
    final int offset = getAlignmentStart2wrt1();
    Cigar cigar = getCigar();
    if (overhangStrategy != OverhangStrategy.SOFTCLIP) {
        // otherwise offset is never negative
        if (offset < 0) {
            for (; j < (-offset); j++) {
                bread.append((char) read[j]);
                bref.append(' ');
                match.append(' ');
            }
            // at negative offsets, our cigar's first element carries overhanging bases
            // that we have just printed above. Tweak the first element to
            // exclude those bases. Here we create a new list of cigar elements, so the original
            // list/original cigar are unchanged (they are unmodifiable anyway!)
            final List<CigarElement> tweaked = new ArrayList<>();
            tweaked.addAll(cigar.getCigarElements());
            tweaked.set(0, new CigarElement(cigar.getCigarElement(0).getLength() + offset, cigar.getCigarElement(0).getOperator()));
            cigar = new Cigar(tweaked);
        }
    }
    if (offset > 0) {
        // note: the way this implementation works, cigar will ever start from S *only* if read starts before the ref, i.e. offset = 0
        for (; i < getAlignmentStart2wrt1(); i++) {
            bref.append((char) ref[i]);
            bread.append(' ');
            match.append(' ');
        }
    }
    for (final CigarElement e : cigar.getCigarElements()) {
        switch(e.getOperator()) {
            case M:
                for (int z = 0; z < e.getLength(); z++, i++, j++) {
                    bref.append((i < ref.length) ? (char) ref[i] : ' ');
                    bread.append((j < read.length) ? (char) read[j] : ' ');
                    match.append((i < ref.length && j < read.length) ? (ref[i] == read[j] ? '.' : '*') : ' ');
                }
                break;
            case I:
                for (int z = 0; z < e.getLength(); z++, j++) {
                    bref.append('-');
                    bread.append((char) read[j]);
                    match.append('I');
                }
                break;
            case S:
                for (int z = 0; z < e.getLength(); z++, j++) {
                    bref.append(' ');
                    bread.append((char) read[j]);
                    match.append('S');
                }
                break;
            case D:
                for (int z = 0; z < e.getLength(); z++, i++) {
                    bref.append((char) ref[i]);
                    bread.append('-');
                    match.append('D');
                }
                break;
            default:
                throw new GATKException("Unexpected Cigar element:" + e.getOperator());
        }
    }
    for (; i < ref.length; i++) bref.append((char) ref[i]);
    for (; j < read.length; j++) bread.append((char) read[j]);
    int pos = 0;
    final int maxlength = Math.max(match.length(), Math.max(bread.length(), bref.length()));
    while (pos < maxlength) {
        print_cautiously(match, pos, width);
        print_cautiously(bread, pos, width);
        print_cautiously(bref, pos, width);
        System.out.println();
        pos += width;
    }
}
Also used : Cigar(htsjdk.samtools.Cigar) ArrayList(java.util.ArrayList) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) CigarElement(htsjdk.samtools.CigarElement)

Example 78 with GATKException

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

the class AlignmentUtils method readToAlignmentByteArray.

/**
     * Generate an array of bases for just those that are aligned to the reference (i.e. no clips or insertions)
     *
     * @param cigar            the read's CIGAR -- cannot be null
     * @param read             the read's base array
     * @return a non-null array of bases (bytes)
     */
@SuppressWarnings("fallthrough")
public static byte[] readToAlignmentByteArray(final Cigar cigar, final byte[] read) {
    Utils.nonNull(cigar);
    Utils.nonNull(read);
    final int alignmentLength = cigar.getReferenceLength();
    final byte[] alignment = new byte[alignmentLength];
    int alignPos = 0;
    int readPos = 0;
    for (int iii = 0; iii < cigar.numCigarElements(); iii++) {
        final CigarElement ce = cigar.getCigarElement(iii);
        final int elementLength = ce.getLength();
        switch(ce.getOperator()) {
            case I:
                if (alignPos > 0) {
                    final int prevPos = alignPos - 1;
                    if (alignment[prevPos] == BaseUtils.Base.A.base) {
                        alignment[prevPos] = PileupElement.A_FOLLOWED_BY_INSERTION_BASE;
                    } else if (alignment[prevPos] == BaseUtils.Base.C.base) {
                        alignment[prevPos] = PileupElement.C_FOLLOWED_BY_INSERTION_BASE;
                    } else if (alignment[prevPos] == BaseUtils.Base.T.base) {
                        alignment[prevPos] = PileupElement.T_FOLLOWED_BY_INSERTION_BASE;
                    } else if (alignment[prevPos] == BaseUtils.Base.G.base) {
                        alignment[prevPos] = PileupElement.G_FOLLOWED_BY_INSERTION_BASE;
                    }
                }
            case S:
                readPos += elementLength;
                break;
            case D:
            case N:
                for (int jjj = 0; jjj < elementLength; jjj++) {
                    alignment[alignPos++] = PileupElement.DELETION_BASE;
                }
                break;
            case M:
            case EQ:
            case X:
                for (int jjj = 0; jjj < elementLength; jjj++) {
                    alignment[alignPos++] = read[readPos++];
                }
                break;
            case H:
            case P:
                break;
            default:
                throw new GATKException("Unsupported cigar operator: " + ce.getOperator());
        }
    }
    return alignment;
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException) CigarElement(htsjdk.samtools.CigarElement)

Example 79 with GATKException

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

the class ArtificialReadQueryIterator method initialize.

/**
     * initialize the query iterator
     *
     * @param contig the contig
     * @param start  the start position
     * @param stop   the stop postition
     */
private void initialize(String contig, int start, int stop) {
    // throw away data from the previous invocation, if one exists.
    ensureUntouched();
    reset();
    finalPos = stop;
    startPos = start;
    if (finalPos < 0) {
        finalPos = Integer.MAX_VALUE;
    }
    // sanity check that we have the contig
    contigIndex = -1;
    List<SAMSequenceRecord> list = header.getSequenceDictionary().getSequences();
    for (SAMSequenceRecord rec : list) {
        if (rec.getSequenceName().equals(contig)) {
            contigIndex = rec.getSequenceIndex();
        }
    }
    if (contigIndex < 0) {
        throw new IllegalArgumentException("ArtificialContig" + contig + " doesn't exist");
    }
    while (super.hasNext() && ReadUtils.getReferenceIndex(this.peek(), header) < contigIndex) {
        super.next();
    }
    if (!super.hasNext()) {
        throw new GATKException("Unable to find the target chromosome");
    }
    while (super.hasNext() && this.peek().getStart() < start) {
        super.next();
    }
    // sanity check that we have an actual matching read next
    GATKRead rec = this.peek();
    if (!matches(rec)) {
        throw new GATKException("The next read doesn't match");
    }
    // set the seeked variable to true
    seeked = true;
}
Also used : SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 80 with GATKException

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

the class CigarConversionUtils method convertSAMCigarElementToCigarUnit.

/**
     * Converts an individual SAM CigarElement to an equivalent GA4GH-style CigarUnit
     *
     * @param cigarElement SAM CigarElement to convert to CigarUnit
     * @return equivalent CigarUnit
     */
public static CigarUnit convertSAMCigarElementToCigarUnit(final CigarElement cigarElement) {
    if (cigarElement == null) {
        throw new IllegalArgumentException("Cannot convert a null CigarElement");
    }
    final String convertedOperation = samToGA4GHOperatorTable.get(cigarElement.getOperator());
    if (convertedOperation == null) {
        throw new GATKException("Unable to convert CigarElement to CigarUnit: unknown operator in CigarElement: " + cigarElement.getOperator());
    }
    if (cigarElement.getLength() < 0) {
        throw new GATKException("Unable to convert CigarElement to CigarUnit: negative operation length in CigarElement: " + cigarElement.getLength());
    }
    final CigarUnit cigarUnit = new CigarUnit();
    cigarUnit.setOperationLength((long) cigarElement.getLength());
    cigarUnit.setOperation(convertedOperation);
    return cigarUnit;
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException) CigarUnit(com.google.api.services.genomics.model.CigarUnit)

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