Search in sources :

Example 1 with SamReaderFactory

use of htsjdk.samtools.SamReaderFactory in project gatk by broadinstitute.

the class UnmarkDuplicatesIntegrationTest method getDuplicateCountForBam.

private static int getDuplicateCountForBam(final File bam, final File referenceFile) throws IOException {
    int duplicateCount = 0;
    final SamReaderFactory factory = SamReaderFactory.makeDefault();
    if (referenceFile != null) {
        factory.referenceSequence(referenceFile);
    }
    try (final SamReader reader = factory.open(bam)) {
        for (SAMRecord read : reader) {
            if (read.getDuplicateReadFlag()) {
                ++duplicateCount;
            }
        }
    }
    return duplicateCount;
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMRecord(htsjdk.samtools.SAMRecord)

Example 2 with SamReaderFactory

use of htsjdk.samtools.SamReaderFactory in project gatk by broadinstitute.

the class CompareBaseQualities method doWork.

@Override
protected Object doWork() {
    if (roundDown && (staticQuantizationQuals == null || staticQuantizationQuals.isEmpty())) {
        throw new CommandLineException.BadArgumentValue("round_down_quantized", "true", "This option can only be used if static_quantized_quals is also used");
    }
    staticQuantizedMapping = constructStaticQuantizedMapping(staticQuantizationQuals, roundDown);
    IOUtil.assertFileIsReadable(samFiles.get(0));
    IOUtil.assertFileIsReadable(samFiles.get(1));
    final SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(VALIDATION_STRINGENCY);
    final SamReader reader1 = factory.referenceSequence(REFERENCE_SEQUENCE).open(samFiles.get(0));
    final SamReader reader2 = factory.referenceSequence(REFERENCE_SEQUENCE).open(samFiles.get(1));
    final SecondaryOrSupplementarySkippingIterator it1 = new SecondaryOrSupplementarySkippingIterator(reader1.iterator());
    final SecondaryOrSupplementarySkippingIterator it2 = new SecondaryOrSupplementarySkippingIterator(reader2.iterator());
    final CompareMatrix finalMatrix = new CompareMatrix(staticQuantizedMapping);
    final ProgressMeter progressMeter = new ProgressMeter(1.0);
    progressMeter.start();
    while (it1.hasCurrent() && it2.hasCurrent()) {
        final SAMRecord read1 = it1.getCurrent();
        final SAMRecord read2 = it2.getCurrent();
        progressMeter.update(read1);
        if (!read1.getReadName().equals(read2.getReadName())) {
            throw new UserException.BadInput("files do not have the same exact order of reads:" + read1.getReadName() + " vs " + read2.getReadName());
        }
        finalMatrix.add(read1.getBaseQualities(), read2.getBaseQualities());
        it1.advance();
        it2.advance();
    }
    progressMeter.stop();
    if (it1.hasCurrent() || it2.hasCurrent()) {
        throw new UserException.BadInput("files do not have the same exact number of reads");
    }
    CloserUtil.close(reader1);
    CloserUtil.close(reader2);
    finalMatrix.printOutResults(outputFilename);
    if (throwOnDiff && finalMatrix.hasNonDiagonalElements()) {
        throw new UserException("Quality scores from the two BAMs do not match");
    }
    return finalMatrix.hasNonDiagonalElements() ? 1 : 0;
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) ProgressMeter(org.broadinstitute.hellbender.engine.ProgressMeter) SAMRecord(htsjdk.samtools.SAMRecord) SecondaryOrSupplementarySkippingIterator(htsjdk.samtools.SecondaryOrSupplementarySkippingIterator) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 3 with SamReaderFactory

use of htsjdk.samtools.SamReaderFactory in project gatk by broadinstitute.

the class PrintReadsIntegrationTest method testReadFilters.

@Test(dataProvider = "readFilterTestData")
public void testReadFilters(final String input, final String reference, final String extOut, final List<String> inputArgs, final int expectedCount) throws IOException {
    final File outFile = createTempFile("testReadFilter", extOut);
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add("-I");
    args.add(new File(TEST_DATA_DIR, input).getAbsolutePath());
    args.add("-O");
    args.add(outFile.getAbsolutePath());
    if (reference != null) {
        args.add("-R");
        args.add(new File(TEST_DATA_DIR, reference).getAbsolutePath());
    }
    for (final String filter : inputArgs) {
        args.add(filter);
    }
    runCommandLine(args);
    SamReaderFactory factory = SamReaderFactory.makeDefault();
    if (reference != null) {
        factory = factory.referenceSequence(new File(TEST_DATA_DIR, reference));
    }
    int count = 0;
    try (final SamReader reader = factory.open(outFile)) {
        Iterator<SAMRecord> it = reader.iterator();
        while (it.hasNext()) {
            SAMRecord rec = it.next();
            count++;
        }
    }
    Assert.assertEquals(count, expectedCount);
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMRecord(htsjdk.samtools.SAMRecord) ArgumentsBuilder(org.broadinstitute.hellbender.utils.test.ArgumentsBuilder) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 4 with SamReaderFactory

use of htsjdk.samtools.SamReaderFactory in project gatk by broadinstitute.

the class GATKTool method initializeReads.

/**
     * Initialize our source of reads data (or set it to null if no reads argument(s) were provided).
     *
     * Package-private so that engine classes can access it, but concrete tool child classes cannot.
     * May be overridden by traversals that require custom initialization of the reads data source.
     */
void initializeReads() {
    if (!readArguments.getReadFiles().isEmpty()) {
        SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(readArguments.getReadValidationStringency());
        if (hasReference()) {
            // pass in reference if available, because CRAM files need it
            factory = factory.referenceSequence(referenceArguments.getReferenceFile());
        } else if (hasCramInput()) {
            throw new UserException.MissingReference("A reference file is required when using CRAM files.");
        }
        if (bamIndexCachingShouldBeEnabled()) {
            factory = factory.enable(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES);
        }
        reads = new ReadsDataSource(readArguments.getReadPaths(), readArguments.getReadIndexPaths(), factory, cloudPrefetchBuffer, (cloudIndexPrefetchBuffer < 0 ? cloudPrefetchBuffer : cloudIndexPrefetchBuffer));
    } else {
        reads = null;
    }
}
Also used : SamReaderFactory(htsjdk.samtools.SamReaderFactory) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 5 with SamReaderFactory

use of htsjdk.samtools.SamReaderFactory in project gatk by broadinstitute.

the class SamComparisonTest method testHelper.

private void testHelper(final String f1, final String f2, final int expectedMatch, final int expectedDiffer, final int expectedUnmappedBoth, final int expectedUnmappedLeft, final int expectedUnmappedRight, final int expectedMissingLeft, final int expectedMissingRight, final boolean areEqual) throws IOException {
    SamReaderFactory factory = SamReaderFactory.makeDefault();
    File sam1 = new File(TEST_FILES_DIR, f1);
    File sam2 = new File(TEST_FILES_DIR, f2);
    try (final SamReader reader1 = factory.open(sam1);
        final SamReader reader2 = factory.open(sam2)) {
        final SamComparison comparison = new SamComparison(reader1, reader2);
        Assert.assertEquals(areEqual, comparison.areEqual());
        Assert.assertEquals(expectedMatch, comparison.getMappingsMatch());
        Assert.assertEquals(expectedDiffer, comparison.getMappingsDiffer());
        Assert.assertEquals(expectedUnmappedBoth, comparison.getUnmappedBoth());
        Assert.assertEquals(expectedUnmappedLeft, comparison.getUnmappedLeft());
        Assert.assertEquals(expectedUnmappedRight, comparison.getUnmappedRight());
        Assert.assertEquals(expectedMissingLeft, comparison.getMissingLeft());
        Assert.assertEquals(expectedMissingRight, comparison.getMissingRight());
    }
    // now reverse the comparison
    try (final SamReader reader1 = factory.open(sam1);
        final SamReader reader2 = factory.open(sam2)) {
        final SamComparison comparison = new SamComparison(reader2, reader1);
        Assert.assertEquals(areEqual, comparison.areEqual());
        Assert.assertEquals(expectedMatch, comparison.getMappingsMatch());
        Assert.assertEquals(expectedDiffer, comparison.getMappingsDiffer());
        Assert.assertEquals(expectedUnmappedBoth, comparison.getUnmappedBoth());
        Assert.assertEquals(expectedUnmappedRight, comparison.getUnmappedLeft());
        Assert.assertEquals(expectedUnmappedLeft, comparison.getUnmappedRight());
        Assert.assertEquals(expectedMissingRight, comparison.getMissingLeft());
        Assert.assertEquals(expectedMissingLeft, comparison.getMissingRight());
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) File(java.io.File)

Aggregations

SamReaderFactory (htsjdk.samtools.SamReaderFactory)57 SamReader (htsjdk.samtools.SamReader)51 File (java.io.File)43 SAMRecord (htsjdk.samtools.SAMRecord)27 IOException (java.io.IOException)26 SAMFileHeader (htsjdk.samtools.SAMFileHeader)18 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)17 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)17 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)14 ArrayList (java.util.ArrayList)13 List (java.util.List)11 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)10 HashSet (java.util.HashSet)10 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)9 SAMFileWriter (htsjdk.samtools.SAMFileWriter)8 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)8 URL (java.net.URL)8 HashMap (java.util.HashMap)8 BufferedReader (java.io.BufferedReader)7 PrintWriter (java.io.PrintWriter)7