Search in sources :

Example 71 with GATKException

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

the class RecalibrationReport method gatherReports.

/**
     * Gathers a set of files containing {@link RecalibrationReport}s into a single {@link GATKReport}.
     *
     * @param inputs a list of files containing {@link RecalibrationReport}s
     * @return gathered recalibration GATK report
     */
public static GATKReport gatherReports(final List<File> inputs) {
    Utils.nonNull(inputs);
    Utils.nonEmpty(inputs, "Cannot gather an empty list of inputs");
    final SortedSet<String> allReadGroups = new TreeSet<>();
    final Map<File, Set<String>> inputReadGroups = new LinkedHashMap<>();
    // Get the read groups from each input report
    for (final File input : inputs) {
        final GATKReport report = new GATKReport(input);
        final Set<String> readGroups = report.getReadGroups();
        inputReadGroups.put(input, readGroups);
        allReadGroups.addAll(readGroups);
    }
    logTablesWithMissingReadGroups(allReadGroups, inputReadGroups);
    final RecalibrationReport result = inputs.stream().map(i -> new RecalibrationReport(new GATKReport(i), allReadGroups)).reduce(RecalibrationReport::combine).filter(r -> !r.isEmpty()).orElseThrow(() -> new GATKException("there is no usable data in any input file"));
    result.quantizationInfo = new QuantizationInfo(result.recalibrationTables, result.RAC.QUANTIZING_LEVELS);
    return result.createGATKReport();
}
Also used : GATKReport(org.broadinstitute.hellbender.utils.report.GATKReport) Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Logger(org.apache.log4j.Logger) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) GATKReportTable(org.broadinstitute.hellbender.utils.report.GATKReportTable) PrintStream(java.io.PrintStream) GATKReport(org.broadinstitute.hellbender.utils.report.GATKReport) Set(java.util.Set) QualityUtils(org.broadinstitute.hellbender.utils.QualityUtils) Covariate(org.broadinstitute.hellbender.utils.recalibration.covariates.Covariate) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCovariateList(org.broadinstitute.hellbender.utils.recalibration.covariates.StandardCovariateList) NestedIntegerArray(org.broadinstitute.hellbender.utils.collections.NestedIntegerArray) List(java.util.List) UserException(org.broadinstitute.hellbender.exceptions.UserException) Utils(org.broadinstitute.hellbender.utils.Utils) LogManager(org.apache.log4j.LogManager) Collections(java.util.Collections) InputStream(java.io.InputStream) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) Set(java.util.Set) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) File(java.io.File)

Example 72 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 73 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 74 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 75 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)

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