Search in sources :

Example 1 with MetricsFile

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

the class FilterByOrientationBias method onTraversalStart.

@Override
public void onTraversalStart() {
    // Gets around issue 2274 in gatk public
    if (transitions.size() == 0) {
        transitions.add(DEFAULT_ARTIFACT_MODE);
    }
    // Sort the input artifacts argument
    transitions.sort(null);
    final MetricsFile<SequencingArtifactMetrics.PreAdapterDetailMetrics, Comparable<?>> mf = new MetricsFile<>();
    try {
        mf.read(new FileReader(preAdapterMetricsFile));
    } catch (final FileNotFoundException fnfe) {
        throw new UserException("Could not find file: " + preAdapterMetricsFile.getAbsolutePath());
    }
    // Collect all of the transitions that were specified in the parameters.
    relevantTransitions.addAll(transitions.stream().map(s -> convertParameterToTransition(s)).collect(Collectors.toSet()));
    // Get the PreAdapterQ score from the picard metrics tool, which gives an indication of how badly infested the bam file is.
    transitionToPreAdapterScoreMap = PreAdapterOrientationScorer.scoreOrientationBiasMetricsOverContext(mf.getMetrics());
    logger.info("preAdapter scores:");
    transitionToPreAdapterScoreMap.keySet().stream().forEach(k -> logger.info(k + ": " + transitionToPreAdapterScoreMap.get(k)));
    setupVCFWriter();
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 2 with MetricsFile

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

the class CollectRnaSeqMetricsTest method testMultiLevel.

@Test
public void testMultiLevel() throws Exception {
    final String sequence = "chr1";
    final String ignoredSequence = "chrM";
    // Create some alignments that hit the ribosomal sequence, various parts of the gene, and intergenic.
    final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.coordinate, false);
    // Set seed so that strandedness is consistent among runs.
    builder.setRandomSeed(0);
    final int sequenceIndex = builder.getHeader().getSequenceIndex(sequence);
    final SAMReadGroupRecord rg1 = new SAMReadGroupRecord("2");
    rg1.setSample("Sample");
    rg1.setLibrary("foo");
    builder.setReadGroup(rg1);
    builder.addPair("pair1", sequenceIndex, 45, 475);
    builder.addPair("pair2", sequenceIndex, 90, 225);
    builder.addFrag("frag1", sequenceIndex, 150, true);
    builder.addFrag("frag2", sequenceIndex, 450, true);
    final SAMReadGroupRecord rg2 = new SAMReadGroupRecord("3");
    rg2.setSample("Sample");
    rg2.setLibrary("bar");
    builder.setReadGroup(rg2);
    builder.addPair("pair3", sequenceIndex, 120, 600);
    builder.addFrag("frag3", sequenceIndex, 225, false);
    builder.addPair("rrnaPair", sequenceIndex, 400, 500);
    builder.addFrag("ignoredFrag", builder.getHeader().getSequenceIndex(ignoredSequence), 1, false);
    final File samFile = BaseTest.createTempFile("tmp.collectRnaSeqMetrics.", ".sam");
    try (final SAMFileWriter samWriter = new SAMFileWriterFactory().makeSAMWriter(builder.getHeader(), false, samFile)) {
        for (final SAMRecord rec : builder.getRecords()) samWriter.addAlignment(rec);
    }
    // Create an interval list with one ribosomal interval.
    final Interval rRnaInterval = new Interval(sequence, 300, 520, true, "rRNA");
    final IntervalList rRnaIntervalList = new IntervalList(builder.getHeader());
    rRnaIntervalList.add(rRnaInterval);
    final File rRnaIntervalsFile = BaseTest.createTempFile("tmp.rRna.", ".interval_list");
    rRnaIntervalList.write(rRnaIntervalsFile);
    // Generate the metrics.
    final File metricsFile = BaseTest.createTempFile("tmp.", ".rna_metrics");
    final String[] args = new String[] { "--input", samFile.getAbsolutePath(), "--output", metricsFile.getAbsolutePath(), "--REF_FLAT", getRefFlatFile(sequence).getAbsolutePath(), "--RIBOSOMAL_INTERVALS", rRnaIntervalsFile.getAbsolutePath(), "--STRAND_SPECIFICITY", "SECOND_READ_TRANSCRIPTION_STRAND", "--IGNORE_SEQUENCE", ignoredSequence, "--LEVEL", "SAMPLE", "--LEVEL", "LIBRARY" };
    runCommandLine(args);
    final MetricsFile<RnaSeqMetrics, Comparable<?>> output = new MetricsFile<>();
    output.read(new FileReader(metricsFile));
    for (final RnaSeqMetrics metrics : output.getMetrics()) {
        if (metrics.LIBRARY == null) {
            Assert.assertEquals(metrics.PF_ALIGNED_BASES, 396);
            Assert.assertEquals(metrics.PF_BASES, 432);
            Assert.assertEquals(metrics.RIBOSOMAL_BASES.longValue(), 108L);
            Assert.assertEquals(metrics.CODING_BASES, 136);
            Assert.assertEquals(metrics.UTR_BASES, 51);
            Assert.assertEquals(metrics.INTRONIC_BASES, 50);
            Assert.assertEquals(metrics.INTERGENIC_BASES, 51);
            Assert.assertEquals(metrics.CORRECT_STRAND_READS, 3);
            Assert.assertEquals(metrics.INCORRECT_STRAND_READS, 4);
            Assert.assertEquals(metrics.IGNORED_READS, 1);
        } else if (metrics.LIBRARY.equals("foo")) {
            Assert.assertEquals(metrics.PF_ALIGNED_BASES, 216);
            Assert.assertEquals(metrics.PF_BASES, 216);
            Assert.assertEquals(metrics.RIBOSOMAL_BASES.longValue(), 36L);
            Assert.assertEquals(metrics.CODING_BASES, 89);
            Assert.assertEquals(metrics.UTR_BASES, 51);
            Assert.assertEquals(metrics.INTRONIC_BASES, 25);
            Assert.assertEquals(metrics.INTERGENIC_BASES, 15);
            Assert.assertEquals(metrics.CORRECT_STRAND_READS, 3);
            Assert.assertEquals(metrics.INCORRECT_STRAND_READS, 2);
            Assert.assertEquals(metrics.IGNORED_READS, 0);
        } else if (metrics.LIBRARY.equals("bar")) {
            Assert.assertEquals(metrics.PF_ALIGNED_BASES, 180);
            Assert.assertEquals(metrics.PF_BASES, 216);
            Assert.assertEquals(metrics.RIBOSOMAL_BASES.longValue(), 72L);
            Assert.assertEquals(metrics.CODING_BASES, 47);
            Assert.assertEquals(metrics.UTR_BASES, 0);
            Assert.assertEquals(metrics.INTRONIC_BASES, 25);
            Assert.assertEquals(metrics.INTERGENIC_BASES, 36);
            Assert.assertEquals(metrics.CORRECT_STRAND_READS, 0);
            Assert.assertEquals(metrics.INCORRECT_STRAND_READS, 2);
            Assert.assertEquals(metrics.IGNORED_READS, 1);
        }
    }
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMRecordSetBuilder(htsjdk.samtools.SAMRecordSetBuilder) IntervalList(htsjdk.samtools.util.IntervalList) SAMRecord(htsjdk.samtools.SAMRecord) FileReader(java.io.FileReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File) Interval(htsjdk.samtools.util.Interval) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 3 with MetricsFile

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

the class CollectAlignmentSummaryMetricsTest method testBisulfite.

@Test
public void testBisulfite() throws IOException {
    final File input = new File(TEST_DATA_DIR, "summary_alignment_bisulfite_test.sam");
    final File reference = new File(TEST_DATA_DIR, "summary_alignment_stats_test.fasta");
    final File outfile = BaseTest.createTempFile("alignmentMetrics", ".txt");
    final String[] args = new String[] { "--input", input.getAbsolutePath(), "--output", outfile.getAbsolutePath(), "--reference", reference.getAbsolutePath(), "--IS_BISULFITE_SEQUENCED", "true" };
    runCommandLine(args);
    final NumberFormat format = NumberFormat.getInstance();
    format.setMaximumFractionDigits(4);
    final MetricsFile<AlignmentSummaryMetrics, Comparable<?>> output = new MetricsFile<>();
    output.read(new FileReader(outfile));
    for (final AlignmentSummaryMetrics metrics : output.getMetrics()) {
        Assert.assertEquals(metrics.MEAN_READ_LENGTH, 101.0);
        switch(metrics.CATEGORY) {
            case FIRST_OF_PAIR:
                // 19 no-calls, one potentially methylated base, one mismatch at a potentially methylated base
                Assert.assertEquals(metrics.TOTAL_READS, 1);
                Assert.assertEquals(metrics.PF_READS, 1);
                Assert.assertEquals(metrics.PF_HQ_ALIGNED_BASES, 101);
                Assert.assertEquals(metrics.PF_HQ_MEDIAN_MISMATCHES, 20.0);
                Assert.assertEquals(metrics.PF_ALIGNED_BASES, 101);
                Assert.assertEquals(metrics.PF_MISMATCH_RATE, 20D / 100D);
                Assert.assertEquals(metrics.BAD_CYCLES, 20);
                Assert.assertEquals(format.format(metrics.PF_HQ_ERROR_RATE), format.format(20 / (double) 100));
                break;
            case SECOND_OF_PAIR:
                // Three no-calls, two potentially methylated bases
                Assert.assertEquals(metrics.TOTAL_READS, 1);
                Assert.assertEquals(metrics.PF_READS, 1);
                Assert.assertEquals(metrics.PF_HQ_ALIGNED_BASES, 101);
                Assert.assertEquals(metrics.PF_HQ_MEDIAN_MISMATCHES, 3.0);
                Assert.assertEquals(metrics.PF_ALIGNED_BASES, 101);
                Assert.assertEquals(metrics.PF_MISMATCH_RATE, /*3D/99D*/
                0.030303);
                Assert.assertEquals(metrics.BAD_CYCLES, 3);
                Assert.assertEquals(format.format(metrics.PF_HQ_ERROR_RATE), format.format(3 / (double) 99));
                break;
            case PAIR:
                Assert.assertEquals(metrics.TOTAL_READS, 2);
                Assert.assertEquals(metrics.PF_READS, 2);
                Assert.assertEquals(metrics.PF_HQ_ALIGNED_BASES, 202);
                Assert.assertEquals(metrics.PF_HQ_MEDIAN_MISMATCHES, 11.5);
                Assert.assertEquals(metrics.PF_ALIGNED_BASES, 202);
                Assert.assertEquals(metrics.PF_MISMATCH_RATE, /*23D/199D*/
                0.115578);
                Assert.assertEquals(metrics.BAD_CYCLES, 23);
                Assert.assertEquals(format.format(metrics.PF_HQ_ERROR_RATE), format.format(23 / (double) 199));
                break;
            case UNPAIRED:
            default:
                Assert.fail("Data does not contain this category: " + metrics.CATEGORY);
        }
    }
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) FileReader(java.io.FileReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File) NumberFormat(java.text.NumberFormat) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 4 with MetricsFile

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

the class MeanQualityByCycleIntegrationTest method test.

@Test(dataProvider = "filenames", groups = { "R" })
public void test(final String inputFile, final String referenceName) throws IOException {
    final File input = new File(TEST_DATA_DIR, inputFile);
    final File expectedFile = new File(TEST_DATA_DIR, "meanqualbycycle.txt");
    final File outfile = BaseTest.createTempFile("testMeanQualityByCycle", ".metrics");
    final File pdf = BaseTest.createTempFile("testMeanQualityByCycle", ".pdf");
    outfile.deleteOnExit();
    pdf.deleteOnExit();
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add("--input");
    args.add(input.getAbsolutePath());
    args.add("--output");
    args.add(outfile.getAbsolutePath());
    args.add("--CHART");
    args.add(pdf.getAbsolutePath());
    args.add("--PRODUCE_PLOT");
    args.add("true");
    if (null != referenceName) {
        final File REF = new File(referenceName);
        args.add("-R");
        args.add(REF.getAbsolutePath());
    }
    runCommandLine(args.getArgsArray());
    try (final FileReader actualReader = new FileReader(outfile)) {
        final MetricsFile<?, Integer> output = new MetricsFile<>();
        output.read(actualReader);
        Assert.assertEquals(output.getAllHistograms().size(), 1);
        Assert.assertEquals(output.getHistogram().size(), 202);
    }
    Assert.assertTrue(pdf.exists(), "exists");
    Assert.assertTrue(pdf.length() > 0, "length");
    IntegrationTestSpec.assertEqualTextFiles(outfile, expectedFile, "#");
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) ArgumentsBuilder(org.broadinstitute.hellbender.utils.test.ArgumentsBuilder) FileReader(java.io.FileReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 5 with MetricsFile

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

the class CollectAlignmentSummaryMetricsTest method testZeroLengthReads.

@Test
public void testZeroLengthReads() throws IOException {
    final File input = new File(TEST_DATA_DIR, "summary_alignment_stats_test2.sam");
    final File outfile = BaseTest.createTempFile("alignmentMetrics", ".txt");
    final String[] args = new String[] { "--input", input.getAbsolutePath(), "--output", outfile.getAbsolutePath() };
    runCommandLine(args);
    final MetricsFile<AlignmentSummaryMetrics, Comparable<?>> output = new MetricsFile<>();
    output.read(new FileReader(outfile));
    for (final AlignmentSummaryMetrics metrics : output.getMetrics()) {
    // test that it doesn't blow up
    }
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) FileReader(java.io.FileReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

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