Search in sources :

Example 11 with MetricsFile

use of htsjdk.samtools.metrics.MetricsFile in project gridss by PapenfussLab.

the class IdsvSamFileMetricsCollectorTest method should_calc_max_fragment_size.

@Test
public void should_calc_max_fragment_size() {
    IdsvSamFileMetricsCollector c = new IdsvSamFileMetricsCollector(null);
    c.acceptRecord(Read(0, 1, "100M"), null);
    c.acceptRecord(RP(0, 1, 2, 1)[0], null);
    c.acceptRecord(RP(0, 1, 7, 5)[0], null);
    // should ignore this as it's not a proper pair
    SAMRecord r = RP(0, 1, 100, 5)[0];
    r.setProperPairFlag(false);
    c.acceptRecord(r, null);
    // 12345678901234567890
    // ----> <----
    c.acceptRecord(Read(0, 1, "100M"), null);
    MetricsFile<IdsvMetrics, Integer> idsv = new MetricsFile<IdsvMetrics, Integer>();
    MetricsFile<InsertSizeMetrics, Integer> is = new MetricsFile<InsertSizeMetrics, Integer>();
    MetricsFile<CigarDetailMetrics, Integer> sc = new MetricsFile<CigarDetailMetrics, Integer>();
    MetricsFile<MapqMetrics, Integer> mq = new MetricsFile<MapqMetrics, Integer>();
    c.finish(is, idsv, mq, sc);
    assertEquals(11, (int) ((IdsvMetrics) idsv.getMetrics().get(0)).MAX_PROPER_PAIR_FRAGMENT_LENGTH);
    c = new IdsvSamFileMetricsCollector(null);
    // mate before
    r = RP(0, 1, 100, 5)[1];
    c.acceptRecord(r, null);
    c.finish(is, idsv, mq, sc);
    assertEquals(11, (int) ((IdsvMetrics) idsv.getMetrics().get(0)).MAX_PROPER_PAIR_FRAGMENT_LENGTH);
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) IdsvMetrics(gridss.analysis.IdsvMetrics) SAMRecord(htsjdk.samtools.SAMRecord) InsertSizeMetrics(picard.analysis.InsertSizeMetrics) MapqMetrics(gridss.analysis.MapqMetrics) CigarDetailMetrics(gridss.analysis.CigarDetailMetrics) Test(org.junit.Test)

Example 12 with MetricsFile

use of htsjdk.samtools.metrics.MetricsFile in project gatk by broadinstitute.

the class InsertSizeMetricsCollector method finish.

/**
     * Finish the metrics collection by saving any results to a metrics file.
     * @param metricsFile a metricsFile where the collected metrics should be stored. May not be null.
     * @param inputName the name of the input, for optional inclusion in the metrics file. May not be null.
     * @param authHolder Authentication info for this context.
     */
public void finish(final MetricsFile<InsertSizeMetrics, Integer> metricsFile, final String inputName, final AuthHolder authHolder) {
    Utils.nonNull(metricsFile);
    finish();
    addAllLevelsToFile(metricsFile);
    if (metricsFile.getNumHistograms() == 0) {
        // can happen if user sets MINIMUM_PCT = 0.5, etc.
        log.warn("All data categories were discarded because they contained < " + inputArgs.minimumPct + " of the total aligned paired data.");
        final PerUnitInsertSizeMetricsCollector allReadsCollector = getAllReadsCollector();
        log.warn("Total mapped pairs in all categories: " + (allReadsCollector == null ? allReadsCollector : allReadsCollector.getTotalInserts()));
    } else {
        if (null != authHolder) {
            MetricsUtils.saveMetrics(metricsFile, inputArgs.output, authHolder);
        } else {
            metricsFile.write(new File(inputArgs.output));
        }
        if (inputArgs.producePlot) {
            writeHistogramPDF(inputName);
        }
    }
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File)

Example 13 with MetricsFile

use of htsjdk.samtools.metrics.MetricsFile in project gatk-protected by broadinstitute.

the class PreAdapterOrientationScorerUnitTest method testBasicScoring.

/**
     * Note that (due to raw data), this test includes collapsing over libraries (not just the contexts).
     * @throws IOException
     */
@Test
public void testBasicScoring() throws IOException {
    final MetricsFile<SequencingArtifactMetrics.PreAdapterDetailMetrics, Comparable<?>> mf = new MetricsFile<>();
    mf.read(new FileReader(testPreAdapterDetailsMetrics));
    final Map<Transition, Double> scoreMap = PreAdapterOrientationScorer.scoreOrientationBiasMetricsOverContext(mf.getMetrics());
    Assert.assertNotNull(scoreMap);
    Assert.assertEquals(scoreMap.keySet().size(), 12);
    // Ground truth values painstakingly derived manually
    Assert.assertEquals(scoreMap.get(Transition.transitionOf('A', 'C')), 100.0, 1e-6);
    Assert.assertEquals(scoreMap.get(Transition.transitionOf('A', 'G')), 50.5788416297570, 1e-6);
    Assert.assertEquals(scoreMap.get(Transition.transitionOf('A', 'T')), 100.0, 1e-6);
    Assert.assertEquals(scoreMap.get(Transition.transitionOf('C', 'A')), 100.0, 1e-6);
    Assert.assertEquals(scoreMap.get(Transition.transitionOf('C', 'G')), 100.0, 1e-6);
    Assert.assertEquals(scoreMap.get(Transition.transitionOf('C', 'T')), 58.0641821538479, 1e-6);
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) Transition(org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition) FileReader(java.io.FileReader) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 14 with MetricsFile

use of htsjdk.samtools.metrics.MetricsFile in project gatk-protected by broadinstitute.

the class PreAdapterOrientationScorerUnitTest method testBasicCounting.

/**
     * Note that (due to raw data), this test includes collapsing over libraries (not just the contexts).
     * @throws IOException
     */
@Test
public void testBasicCounting() throws IOException {
    final MetricsFile<SequencingArtifactMetrics.PreAdapterDetailMetrics, Comparable<?>> mf = new MetricsFile<>();
    mf.read(new FileReader(testPreAdapterDetailsMetrics));
    final Map<Transition, RealMatrix> countMap = PreAdapterOrientationScorer.countOrientationBiasMetricsOverContext(mf.getMetrics());
    Assert.assertNotNull(countMap);
    Assert.assertEquals(countMap.keySet().size(), 12);
    // Ground truth values painstakingly derived manually from both libraries (hence four values in the expression instead of two)
    // L1 norm can be used since all values are always positive.
    Assert.assertEquals(countMap.get(Transition.transitionOf('A', 'C')).getRowVector(0).getL1Norm(), 2836660 + 2631203 + 240 + 246, 1e-1);
    Assert.assertEquals(countMap.get(Transition.transitionOf('A', 'C')).getRowVector(1).getL1Norm(), 2852491 + 333 + 2646654 + 297, 1e-1);
    Assert.assertEquals(countMap.get(Transition.transitionOf('A', 'G')).getRowVector(0).getL1Norm(), 2631203 + 416 + 2836660 + 481, 1e-1);
    Assert.assertEquals(countMap.get(Transition.transitionOf('A', 'G')).getRowVector(1).getL1Norm(), 2852491 + 404 + 2646654 + 397, 1e-1);
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Transition(org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition) FileReader(java.io.FileReader) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 15 with MetricsFile

use of htsjdk.samtools.metrics.MetricsFile in project gatk by broadinstitute.

the class CollectRrbsMetrics method doWork.

@Override
protected Object doWork() {
    if (!METRICS_FILE_PREFIX.endsWith(".")) {
        METRICS_FILE_PREFIX = METRICS_FILE_PREFIX + ".";
    }
    final File SUMMARY_OUT = new File(METRICS_FILE_PREFIX + SUMMARY_FILE_EXTENSION);
    final File DETAILS_OUT = new File(METRICS_FILE_PREFIX + DETAIL_FILE_EXTENSION);
    final File PLOTS_OUT = new File(METRICS_FILE_PREFIX + PDF_FILE_EXTENSION);
    assertIoFiles(SUMMARY_OUT, DETAILS_OUT, PLOTS_OUT);
    final SamReader samReader = SamReaderFactory.makeDefault().open(INPUT);
    if (!ASSUME_SORTED && samReader.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.coordinate) {
        throw new UserException("The input file " + INPUT.getAbsolutePath() + " does not appear to be coordinate sorted");
    }
    final ReferenceSequenceFileWalker refWalker = new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE);
    final ProgressLogger progressLogger = new ProgressLogger(logger);
    final RrbsMetricsCollector metricsCollector = new RrbsMetricsCollector(METRIC_ACCUMULATION_LEVEL, samReader.getFileHeader().getReadGroups(), C_QUALITY_THRESHOLD, NEXT_BASE_QUALITY_THRESHOLD, MINIMUM_READ_LENGTH, MAX_MISMATCH_RATE);
    for (final SAMRecord samRecord : samReader) {
        progressLogger.record(samRecord);
        if (!samRecord.getReadUnmappedFlag() && !isSequenceFiltered(samRecord.getReferenceName())) {
            final ReferenceSequence referenceSequence = refWalker.get(samRecord.getReferenceIndex());
            metricsCollector.acceptRecord(samRecord, referenceSequence);
        }
    }
    metricsCollector.finish();
    final MetricsFile<RrbsMetrics, Long> rrbsMetrics = getMetricsFile();
    metricsCollector.addAllLevelsToFile(rrbsMetrics);
    // Using RrbsMetrics as a way to get both of the metrics objects through the MultiLevelCollector. Once
    // we get it out split it apart to the two separate MetricsFiles and write them to file
    final MetricsFile<RrbsSummaryMetrics, ?> summaryFile = getMetricsFile();
    final MetricsFile<RrbsCpgDetailMetrics, ?> detailsFile = getMetricsFile();
    for (final RrbsMetrics rrbsMetric : rrbsMetrics.getMetrics()) {
        summaryFile.addMetric(rrbsMetric.getSummaryMetrics());
        for (final RrbsCpgDetailMetrics detailMetric : rrbsMetric.getDetailMetrics()) {
            detailsFile.addMetric(detailMetric);
        }
    }
    summaryFile.write(SUMMARY_OUT);
    detailsFile.write(DETAILS_OUT);
    if (PRODUCE_PLOT) {
        final RScriptExecutor executor = new RScriptExecutor();
        executor.addScript(new Resource(R_SCRIPT, CollectRrbsMetrics.class));
        executor.addArgs(DETAILS_OUT.getAbsolutePath(), SUMMARY_OUT.getAbsolutePath(), PLOTS_OUT.getAbsolutePath());
        executor.exec();
    }
    CloserUtil.close(samReader);
    return null;
}
Also used : Resource(org.broadinstitute.hellbender.utils.io.Resource) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) ReferenceSequence(htsjdk.samtools.reference.ReferenceSequence) SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) RScriptExecutor(org.broadinstitute.hellbender.utils.R.RScriptExecutor) UserException(org.broadinstitute.hellbender.exceptions.UserException) ReferenceSequenceFileWalker(htsjdk.samtools.reference.ReferenceSequenceFileWalker) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File)

Aggregations

MetricsFile (htsjdk.samtools.metrics.MetricsFile)32 FileReader (java.io.FileReader)21 File (java.io.File)19 Test (org.testng.annotations.Test)15 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)14 SAMRecord (htsjdk.samtools.SAMRecord)10 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)9 SamReader (htsjdk.samtools.SamReader)6 FileNotFoundException (java.io.FileNotFoundException)5 UserException (org.broadinstitute.hellbender.exceptions.UserException)5 IntervalList (htsjdk.samtools.util.IntervalList)4 Transition (org.broadinstitute.hellbender.tools.picard.analysis.artifacts.Transition)4 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)4 InsertSizeMetrics (picard.analysis.InsertSizeMetrics)3 CigarDetailMetrics (gridss.analysis.CigarDetailMetrics)2 IdsvMetrics (gridss.analysis.IdsvMetrics)2 MapqMetrics (gridss.analysis.MapqMetrics)2 SAMFileWriter (htsjdk.samtools.SAMFileWriter)2 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)2 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)2