Java Code Examples for htsjdk.samtools.metrics.MetricsFile#readBeans()

The following examples show how to use htsjdk.samtools.metrics.MetricsFile#readBeans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterBamByTagTest.java    From Drop-seq with MIT License 6 votes vote down vote up
@Test
public void testDoWorkPaired () throws IOException {
	final FilterBamByTag f = runClp(true, null);
	//samtools view -c paired_reads_tagged_filtered.bam
	// 12
	// samtools view -c paired_reads_tagged.bam
	// 20

	List<FilteredReadsMetric> metrics = MetricsFile.readBeans(f.SUMMARY);
	Assert.assertEquals(PAIRED_READS_ACCEPTED, metrics.get(0).READS_ACCEPTED);
	Assert.assertEquals(PAIRED_READS_REJECTED, metrics.get(0).READS_REJECTED);
							
	CompareBAMTagValues cbtv = new CompareBAMTagValues();
	cbtv.INPUT_1=PAIRED_INPUT_FILE_FILTERED;
	cbtv.INPUT_2=f.OUTPUT;
	List<String> tags = new ArrayList<>();
	tags.add("XC");
	cbtv.TAGS=tags;
	int r = cbtv.doWork();
	Assert.assertEquals(r, 0);
	
}
 
Example 2
Source File: FilterBamTest.java    From Drop-seq with MIT License 6 votes vote down vote up
@Test
public void testOrganismFiltering () throws IOException {
	FilterBam f = new FilterBam();
	f.INPUT=ORGANISM_INPUT_FILE;
	f.SUMMARY=File.createTempFile("paired_input", ".summary.txt");
	f.OUTPUT=File.createTempFile("paired_input", ".bam");
	f.OUTPUT.deleteOnExit();
	f.SUMMARY.deleteOnExit();
	f.REF_SOFT_MATCHED_RETAINED=Arrays.asList("HUMAN");
	// need to set this to an empty list, as that's what the constructor for FilterBam would do.
	f.STRIP_REF_PREFIX=new ArrayList<>();
	f.doWork();
	
	List<FilteredReadsMetric> metrics = MetricsFile.readBeans(f.SUMMARY);
	Assert.assertEquals(NUM_HUMAN_READS, metrics.get(0).READS_ACCEPTED);
	Assert.assertEquals(TOTAL_READS - NUM_HUMAN_READS, metrics.get(0).READS_REJECTED);
	
	f.REF_SOFT_MATCHED_RETAINED=Arrays.asList("MOUSE");
	Assert.assertEquals(f.doWork(), 0);
	metrics = MetricsFile.readBeans(f.SUMMARY);
	Assert.assertEquals(NUM_MOUSE_READS, metrics.get(0).READS_ACCEPTED);
	Assert.assertEquals(TOTAL_READS - NUM_MOUSE_READS, metrics.get(0).READS_REJECTED);
	
	
}
 
Example 3
Source File: CollectSamErrorMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "OneCovariateErrorMetricsDataProvider")
public void testOneCovariateErrorMetrics(final String errorSubscript, final File samFile, final int priorQ, BaseErrorMetric expectedMetric) {

    ErrorMetric.setPriorError(QualityUtil.getErrorProbabilityFromPhredScore(priorQ));
    expectedMetric.calculateDerivedFields();

    // Note that soft clipped bases are not counted
    List<BaseErrorMetric> metrics = MetricsFile.readBeans(new File(errorMetrics.get(samFile).getAbsolutePath() + errorSubscript));

    BaseErrorMetric metric = metrics
            .stream()
            .filter(m -> m.COVARIATE.equals(expectedMetric.COVARIATE))
            .findAny()
            .orElseThrow(() -> new AssertionError("didn't find metric with COVARIATE==" + expectedMetric.COVARIATE));

    Assert.assertEquals(metric, expectedMetric);
}
 
Example 4
Source File: CollectSamErrorMetricsTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "oneCovariateIndelErrorMetricsDataProvider")
public void testOneCovariateIndelErrorMetrics(final String errorSubscript, final File samFile, final int priorQ, BaseErrorMetric expectedMetric) {

    final File errorByAll = new File(errorMetrics.get(samFile).getAbsolutePath() + errorSubscript);
    errorByAll.deleteOnExit();

    ErrorMetric.setPriorError(QualityUtil.getErrorProbabilityFromPhredScore(priorQ));
    expectedMetric.calculateDerivedFields();

    // Note that soft clipped bases are not counted
    List<BaseErrorMetric> metrics = MetricsFile.readBeans(errorByAll);

    BaseErrorMetric metric = metrics
            .stream()
            .filter(m -> m.COVARIATE.equals(expectedMetric.COVARIATE))
            .findAny()
            .orElseThrow(() -> new AssertionError("didn't find metric with COVARIATE==" + expectedMetric.COVARIATE));

    Assert.assertEquals(metric, expectedMetric);
}
 
Example 5
Source File: SpermSeqMarkDuplicatesTest.java    From Drop-seq with MIT License 5 votes vote down vote up
@Test
// tests which reads are marked as duplicates by the read position strategy.
public void testDetectDuplicatesByReadPositionStrategy() throws IOException {
	String [] duplicateReadNames={"READ1:2", "READ2:3"};
	Set<String> dupes = new HashSet<String>(Arrays.asList(duplicateReadNames));

	SpermSeqMarkDuplicates d = new SpermSeqMarkDuplicates();
	d.INPUT=Arrays.asList(INPUT);
	d.OUTPUT=File.createTempFile("testDetectDuplicatesByReadPositionStrategy.", ".bam");
	d.OUTPUT.deleteOnExit();
	d.OUTPUT_STATS=File.createTempFile("testDetectDuplicatesByReadPositionStrategy.", ".pcr_duplicate_metrics");
	d.OUTPUT_STATS.deleteOnExit();
	Assert.assertEquals(0, d.doWork());

	SamReader inputSam = SamReaderFactory.makeDefault().open(d.OUTPUT);
	for (SAMRecord r: inputSam) {
		boolean duplicateReadFlag = r.getDuplicateReadFlag();
		String readName = r.getReadName();
		if (dupes.contains(readName))
			Assert.assertTrue(duplicateReadFlag);
		else
			Assert.assertFalse(duplicateReadFlag);
	}
	final List<SpermSeqMarkDuplicates.PCRDuplicateMetrics> beans = MetricsFile.readBeans(d.OUTPUT_STATS);
	Assert.assertEquals(1, beans.size());
	Assert.assertEquals(dupes.size(), beans.get(0).NUM_DUPLICATES);
}
 
Example 6
Source File: EstimateLibraryComplexityTest.java    From picard with MIT License 5 votes vote down vote up
private void examineMetricsFile(final File output, final int numDuplicates, final int numReadPairsExamined) {
    final List<DuplicationMetrics> metricsList = MetricsFile.readBeans(output);
    Assert.assertEquals(metricsList.size(), 1);
    final DuplicationMetrics metrics = metricsList.get(0);
    Assert.assertEquals(metrics.READ_PAIR_DUPLICATES * 2 + metrics.UNPAIRED_READ_DUPLICATES, numDuplicates);
    Assert.assertEquals(metrics.READ_PAIRS_EXAMINED, numReadPairsExamined);
}
 
Example 7
Source File: CalculateFingerprintMetricsTest.java    From picard with MIT License 5 votes vote down vote up
private void runIt(final File inputFile, final File outputFile) {
    final List<String> args = new ArrayList<>(Arrays.asList(
            "INPUT=" + inputFile.getAbsolutePath(),
            "OUTPUT=" + outputFile.getAbsolutePath(),
            "H=" + HAPLOTYPE_MAP.getAbsolutePath()));
    assertEquals(runPicardCommandLine(args),0);

    final List<FingerprintMetrics> metrics = MetricsFile.readBeans(outputFile);

    for (FingerprintMetrics metric : metrics) {
        Assert.assertTrue(metric.CHI_SQUARED_PVALUE > 0.05);
        Assert.assertTrue(metric.LOG10_CHI_SQUARED_PVALUE > -2);
        Assert.assertTrue(metric.LOD_SELF_CHECK > 0);
        Assert.assertTrue(metric.CROSS_ENTROPY_LOD < 1);
        Assert.assertTrue(metric.DEFINITE_GENOTYPES >= 0);
        Assert.assertTrue(metric.HAPLOTYPES > 4);
        Assert.assertTrue(metric.HAPLOTYPES_WITH_EVIDENCE > 0);
        Assert.assertTrue(metric.HET_CHI_SQUARED_PVALUE > 0.05);
        Assert.assertTrue(metric.LOG10_HET_CHI_SQUARED_PVALUE > -2);
        Assert.assertTrue(metric.HET_CROSS_ENTROPY_LOD < 1);
        Assert.assertTrue(metric.HOM_CHI_SQUARED_PVALUE > 0.05);
        Assert.assertTrue(metric.LOG10_HOM_CHI_SQUARED_PVALUE > -2);
        Assert.assertTrue(metric.HOM_CROSS_ENTROPY_LOD < 1);
        Assert.assertTrue(metric.NUM_HET >= 0);
        Assert.assertTrue(metric.NUM_HOM_ALLELE1 >= 0);
        Assert.assertTrue(metric.NUM_HOM_ALLELE2 >= 0);
        Assert.assertTrue(metric.EXPECTED_HET >= 0);
        Assert.assertTrue(metric.EXPECTED_HOM_ALLELE1>= 0);
        Assert.assertTrue(metric.EXPECTED_HOM_ALLELE2 >= 0);
        Assert.assertTrue(metric.DISCRIMINATORY_POWER > 0);
    }
}
 
Example 8
Source File: FilterBamByTagTest.java    From Drop-seq with MIT License 4 votes vote down vote up
@Test
public void testDoWorkUnPaired () throws IOException {
	FilterBamByTag f = runClp(false, null);

	// samtools view -c unpaired_reads_tagged_filtered.bam
	// 6
	// samtools view -c unpaired_reads_tagged.bam 
	// 10
			
	List<FilteredReadsMetric> metrics = MetricsFile.readBeans(f.SUMMARY);
	Assert.assertEquals(UNPAIRED_READS_ACCEPTED, metrics.get(0).READS_ACCEPTED);
	Assert.assertEquals(UNPAIRED_READS_REJECTED, metrics.get(0).READS_REJECTED);

	CompareBAMTagValues cbtv = new CompareBAMTagValues();
	cbtv.INPUT_1=UNPAIRED_INPUT_FILE_FILTERED;
	cbtv.INPUT_2=f.OUTPUT;
	List<String> tags = new ArrayList<>();
	tags.add("XC");
	cbtv.TAGS=tags;
	int r = cbtv.doWork();
	Assert.assertEquals(r, 0);

	// test alternate path without tag values file.
	f.INPUT=UNPAIRED_INPUT_FILE;
	f.OUTPUT=File.createTempFile("unpaired_input_single_cell", ".bam");
	f.TAG="XC";
	f.TAG_VALUE=Arrays.asList("AAAGTAGAGTGG");
	f.TAG_VALUES_FILE=null;
	f.PAIRED_MODE=false;
	f.OUTPUT.deleteOnExit();
	Assert.assertEquals(f.doWork(), 0);


	cbtv.INPUT_1=UNPAIRED_INPUT_FILE_FILTERED_AAAGTAGAGTGG;
	cbtv.INPUT_2=f.OUTPUT;
	cbtv.TAGS=tags;
	r = cbtv.doWork();
	Assert.assertEquals(r, 0);
	

}
 
Example 9
Source File: CollectSamErrorMetricsTest.java    From picard with MIT License 4 votes vote down vote up
@Test(dataProvider = "provideForTestIndelErrors")
public void testIndelErrors(final String[] readCigars, final String errorSubscript, IndelErrorMetric expectedMetric) throws IOException {

    final File temp = File.createTempFile("Indels", ".bam");
    temp.deleteOnExit();

    final int priorQ = 30;

    try (final ReferenceSequenceFileWalker referenceSequenceFileWalker =
                    new ReferenceSequenceFileWalker(CommandLineProgramTest.CHR_M_REFERENCE)) {

        final SAMRecordSetBuilder builder = new SAMRecordSetBuilder();
        builder.getHeader().setSequenceDictionary(referenceSequenceFileWalker.getSequenceDictionary());

        for (int i = 0; i < readCigars.length; i++) {
            // 10M2I3M4D10M5I
            builder.addFrag("Read" + i, 0, 100, false, false, readCigars[i], null, 30);
        }

        try (final SAMFileWriter writer = new SAMFileWriterFactory()
                .setCompressionLevel(2)
                .makeBAMWriter(builder.getHeader(), false, temp)) {
            builder.forEach(writer::addAlignment);
        }
    }

    final File referenceFile = CHR_M_REFERENCE;
    final File vcf = new File(TEST_DIR, "NIST.selected.vcf");

    final File outputBaseFileName = new File(OUTPUT_DATA_PATH, "test");
    final File errorByAll = new File(outputBaseFileName.getAbsolutePath() + errorSubscript);
    errorByAll.deleteOnExit();
    outputBaseFileName.deleteOnExit();

    final String[] args = {
            "INPUT=" + temp,
            "OUTPUT=" + outputBaseFileName,
            "REFERENCE_SEQUENCE=" + referenceFile.getAbsolutePath(),
            "ERROR_METRICS=" + "INDEL_ERROR",
            "ERROR_METRICS=" + "INDEL_ERROR:INDEL_LENGTH",
            "VCF=" + vcf.getAbsolutePath()
    };

    CollectSamErrorMetrics collectSamErrorMetrics = new CollectSamErrorMetrics();
    collectSamErrorMetrics.ERROR_METRICS.clear();

    Assert.assertEquals(collectSamErrorMetrics.instanceMain(args), 0);

    ErrorMetric.setPriorError(QualityUtil.getErrorProbabilityFromPhredScore(priorQ));
    expectedMetric.calculateDerivedFields();

    // Note that soft clipped bases are not counted
    List<IndelErrorMetric> metrics = MetricsFile.readBeans(errorByAll);

    IndelErrorMetric metric = metrics
            .stream()
            .filter(m -> m.COVARIATE.equals(expectedMetric.COVARIATE))
            .findAny()
            .orElseThrow(() -> new AssertionError("didn't find metric with COVARIATE==" + expectedMetric.COVARIATE));

    Assert.assertEquals(metric, expectedMetric);
}