Search in sources :

Example 86 with GATKException

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

the class GATKReport method addRow.

/**
     * This method provides an efficient way to populate a simplified GATK report. This method will only work on reports
     * that qualify as simplified GATK reports. See the newSimpleReport() constructor for more information.
     *
     * @param values     the row of data to be added to the table.
     *               Note: the number of arguments must match the columns in the table.
     */
public void addRow(final Object... values) {
    // Must be a simple report
    if (tables.size() != 1)
        throw new GATKException("Cannot write a row to a complex GATK Report");
    GATKReportTable table = tables.firstEntry().getValue();
    if (table.getNumColumns() != values.length)
        throw new GATKException("The number of arguments in writeRow (" + values.length + ") must match the number of columns in the table (" + table.getNumColumns() + ")");
    final int rowIndex = table.getNumRows();
    for (int i = 0; i < values.length; i++) table.set(rowIndex, i, values[i]);
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 87 with GATKException

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

the class GATKReportTable method write.

/**
     * Write the table to the PrintStream, formatted nicely to be human-readable, AWK-able, and R-friendly.
     *
     * @param out the PrintStream to which the table should be written
     * @param sortingWay how to sort the table
     */
void write(final PrintStream out, Sorting sortingWay) {
    /*
          * Table header:
          * #:GATKTable:nColumns:nRows:(DataType for each column):;
          * #:GATKTable:TableName:Description :;
          * key   colA  colB
          * row1  xxxx  xxxxx
         */
    // write the table definition
    out.printf(GATKTABLE_HEADER_PREFIX + ":%d:%d", getNumColumns(), getNumRows());
    // write the formats for all the columns
    for (final GATKReportColumn column : columnInfo) out.print(SEPARATOR + column.getFormat());
    out.println(ENDLINE);
    // write the table name & description
    out.printf(GATKTABLE_HEADER_PREFIX + ":%s:%s\n", tableName, tableDescription);
    // write the column names
    boolean needsPadding = false;
    for (final GATKReportColumn column : columnInfo) {
        if (needsPadding)
            out.printf("  ");
        needsPadding = true;
        out.printf(column.getColumnFormat().getNameFormat(), column.getColumnName());
    }
    out.println();
    // write the table body
    switch(sortingWay) {
        case SORT_BY_COLUMN:
            Collections.sort(underlyingData, ROW_COMPARATOR);
            for (final Object[] row : underlyingData) {
                writeRow(out, row);
            }
            break;
        case SORT_BY_ROW:
            // make sure that there are exactly the correct number of ID mappings
            if (rowIdToIndex.size() != underlyingData.size())
                throw new GATKException("There isn't a 1-to-1 mapping from row ID to index; this can happen when rows are not created consistently");
            final TreeMap<Object, Integer> sortedMap;
            try {
                sortedMap = new TreeMap<>(rowIdToIndex);
            } catch (ClassCastException e) {
                throw new GATKException("Unable to sort the rows based on the row IDs because the ID Objects are of different types");
            }
            for (final Map.Entry<Object, Integer> rowKey : sortedMap.entrySet()) writeRow(out, underlyingData.get(rowKey.getValue()));
            break;
        case DO_NOT_SORT:
            for (final Object[] row : underlyingData) writeRow(out, row);
    }
    out.println();
}
Also used : BigInteger(java.math.BigInteger) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 88 with GATKException

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

the class GATKReportTable method increment.

/**
    * Increment the value for a given position in the table.
    * Throws an exception if the value in the cell is not an integer.
    *
    * @param rowID        the row ID
    * @param columnName   the name of the column
    */
public void increment(final Object rowID, final String columnName) {
    int prevValue;
    if (!rowIdToIndex.containsKey(rowID)) {
        rowIdToIndex.put(rowID, underlyingData.size());
        underlyingData.add(new Object[getNumColumns()]);
        prevValue = 0;
    } else {
        Object obj = get(rowID, columnName);
        if (!(obj instanceof Integer))
            throw new GATKException("Attempting to increment a value in a cell that is not an integer");
        prevValue = (Integer) obj;
    }
    set(rowIdToIndex.get(rowID), columnNameToIndex.get(columnName), prevValue + 1);
}
Also used : BigInteger(java.math.BigInteger) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 89 with GATKException

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

the class BaseRecalibrationEngine method calculateIsSNPOrIndel.

/**
     * Locates all SNP and indel events, storing them in the provided snp, isIns, and isDel arrays, and returns
     * the total number of SNP/indel events.
     *
     * @param read read to inspect
     * @param ref source of reference bses
     * @param snp storage for snp events (must be of length read.getBases().length and initialized to all 0's)
     * @param isIns storage for insertion events (must be of length read.getBases().length and initialized to all 0's)
     * @param isDel storage for deletion events (must be of length read.getBases().length and initialized to all 0's)
     * @return the total number of SNP and indel events
     */
protected static int calculateIsSNPOrIndel(final GATKRead read, final ReferenceDataSource ref, int[] snp, int[] isIns, int[] isDel) {
    final byte[] refBases = ref.queryAndPrefetch(read.getContig(), read.getStart(), read.getEnd()).getBases();
    int readPos = 0;
    int refPos = 0;
    int nEvents = 0;
    for (final CigarElement ce : read.getCigarElements()) {
        final int elementLength = ce.getLength();
        switch(ce.getOperator()) {
            case M:
            case EQ:
            case X:
                for (int i = 0; i < elementLength; i++) {
                    int snpInt = (BaseUtils.basesAreEqual(read.getBase(readPos), refBases[refPos]) ? 0 : 1);
                    snp[readPos] = snpInt;
                    nEvents += snpInt;
                    readPos++;
                    refPos++;
                }
                break;
            case D:
                {
                    final int index = (read.isReverseStrand() ? readPos : readPos - 1);
                    updateIndel(isDel, index);
                    refPos += elementLength;
                    break;
                }
            case N:
                refPos += elementLength;
                break;
            case I:
                {
                    final boolean forwardStrandRead = !read.isReverseStrand();
                    if (forwardStrandRead) {
                        updateIndel(isIns, readPos - 1);
                    }
                    readPos += elementLength;
                    if (!forwardStrandRead) {
                        updateIndel(isIns, readPos);
                    }
                    break;
                }
            case // ReferenceContext doesn't have the soft clipped bases!
            S:
                readPos += elementLength;
                break;
            case H:
            case P:
                break;
            default:
                throw new GATKException("Unsupported cigar operator: " + ce.getOperator());
        }
    }
    // we don't sum those as we go because they might set the same place to 1 twice
    nEvents += MathUtils.sum(isDel) + MathUtils.sum(isIns);
    return nEvents;
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException) CigarElement(htsjdk.samtools.CigarElement)

Example 90 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)

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