Search in sources :

Example 1 with SamReader

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

the class WalkerOutputIntegrationTest method testPGOnByDefault.

@Test
public void testPGOnByDefault() throws IOException {
    final File inFile = new File(TEST_DATA_DIR, "testPGOnByDefault.bam");
    final File outFile = BaseTest.createTempFile("testNoConflictRG", ".bam");
    final String[] args = new String[] { //NOTE: no addOutputSAMProgramRecord argument
    "--input", inFile.getAbsolutePath(), "--output", outFile.getAbsolutePath() };
    runCommandLine(args);
    try (final SamReader reader1 = SamReaderFactory.makeDefault().open(inFile)) {
        Assert.assertNotNull(reader1.getFileHeader().getProgramRecord("GATK PrintReads"));
    }
    try (final SamReader reader2 = SamReaderFactory.makeDefault().open(inFile)) {
        Assert.assertNull(reader2.getFileHeader().getProgramRecord("GATK PrintReads.1"));
    }
    //output has both GATK PrintReads and GATK PrintReads.1 in headers
    try (final SamReader reader3 = SamReaderFactory.makeDefault().open(outFile)) {
        Assert.assertNotNull(reader3.getFileHeader().getProgramRecord("GATK PrintReads"));
    }
    try (final SamReader reader4 = SamReaderFactory.makeDefault().open(outFile)) {
        Assert.assertNotNull(reader4.getFileHeader().getProgramRecord("GATK PrintReads.1"));
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Example 2 with SamReader

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

the class FilterReadsIntegrationTest method validateSortOrder.

private boolean validateSortOrder(final String resultFileName, final String referenceFileName, final String sortOrderName) throws IOException {
    final File path = new File(resultFileName);
    IOUtil.assertFileIsReadable(path);
    final File refFile = null == referenceFileName ? null : new File(TEST_DATA_DIR, referenceFileName);
    try (final SamReader in = SamReaderFactory.makeDefault().referenceSequence(refFile).open(path)) {
        final SAMFileHeader header = in.getFileHeader();
        final SAMFileHeader.SortOrder hdrOrder = header.getSortOrder();
        switch(sortOrderName) {
            case "coordinate":
                {
                    return hdrOrder == SAMFileHeader.SortOrder.coordinate;
                }
            case "queryname":
                {
                    return hdrOrder == SAMFileHeader.SortOrder.queryname;
                }
            case "unsorted":
            default:
                // don't care what it is
                return true;
        }
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Example 3 with SamReader

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

the class NioBam method getReads.

/** Parses the BAM file into SAMRecords. Will be distributed onto at least 'numPartitions' partitions. **/
public JavaRDD<SAMRecord> getReads(JavaSparkContext ctx, int numPartitions) {
    try {
        Path bamPath = IOUtils.getPath(bam);
        ChannelAsSeekableStream bamOverNIO = new ChannelAsSeekableStream(Files.newByteChannel(bamPath), bamPath.toString());
        final byte[] index = getIndex();
        SeekableStream indexInMemory = new ByteArraySeekableStream(index);
        SamReader bam3 = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.LENIENT).enable(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES).open(SamInputResource.of(bamOverNIO).index(indexInMemory));
        List<QueryInterval> chunks = getAllChunksBalanced(bam3, numPartitions);
        // Ideally we'd get exactly the number of chunks the user is asking for, but until then...
        logger.debug("We got: " + chunks.size() + " chunks.");
        return ctx.parallelize(chunks, chunks.size()).flatMap(qi -> new ReadsIterable(bam, index, qi).iterator());
    } catch (IOException e) {
        throw new GATKException("I/O error loading reads", e);
    }
}
Also used : Path(java.nio.file.Path) SamReader(htsjdk.samtools.SamReader) ByteArraySeekableStream(htsjdk.samtools.seekablestream.ByteArraySeekableStream) ByteArraySeekableStream(htsjdk.samtools.seekablestream.ByteArraySeekableStream) SeekableStream(htsjdk.samtools.seekablestream.SeekableStream) QueryInterval(htsjdk.samtools.QueryInterval) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 4 with SamReader

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

the class GetBayesianHetCoverageIntegrationTest method initHeaders.

@BeforeClass
public void initHeaders() throws IOException {
    try (final SamReader normalBamReader = SamReaderFactory.makeDefault().open(NORMAL_BAM_FILE);
        final SamReader tumorBamReader = SamReaderFactory.makeDefault().open(TUMOR_BAM_FILE)) {
        normalHeader = normalBamReader.getFileHeader();
        tumorHeader = tumorBamReader.getFileHeader();
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) BeforeClass(org.testng.annotations.BeforeClass)

Example 5 with SamReader

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

the class RevertBaseQualityScoresIntegrationTest method hasOriginalQualScores.

private static boolean hasOriginalQualScores(final File bam) throws IOException {
    try (final SamReader reader = SamReaderFactory.makeDefault().open(bam)) {
        for (SAMRecord read : reader) {
            final byte[] originalBaseQualities = read.getOriginalBaseQualities();
            Assert.assertNotNull(originalBaseQualities);
            final byte[] baseQualities = read.getBaseQualities();
            Assert.assertEquals(originalBaseQualities.length, baseQualities.length);
            for (int i = 0; i < originalBaseQualities.length; ++i) {
                if (originalBaseQualities[i] != baseQualities[i]) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord)

Aggregations

SamReader (htsjdk.samtools.SamReader)211 SAMRecord (htsjdk.samtools.SAMRecord)137 File (java.io.File)111 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)89 SAMFileHeader (htsjdk.samtools.SAMFileHeader)83 IOException (java.io.IOException)71 SamReaderFactory (htsjdk.samtools.SamReaderFactory)65 ArrayList (java.util.ArrayList)63 SAMFileWriter (htsjdk.samtools.SAMFileWriter)58 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)44 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)42 List (java.util.List)39 CigarElement (htsjdk.samtools.CigarElement)32 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)32 HashMap (java.util.HashMap)31 Cigar (htsjdk.samtools.Cigar)30 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)30 PrintWriter (java.io.PrintWriter)27 Interval (htsjdk.samtools.util.Interval)26 HashSet (java.util.HashSet)26