Search in sources :

Example 1 with SAMFileWriterFactory

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

the class SplitReads method createWriters.

/**
     * Creates SAMFileWriter instances for the reader splitters based on the input file.
     * @param splitters Reader splitters.
     * @return A map of file name keys to SAMFileWriter.
     */
private Map<String, SAMFileGATKReadWriter> createWriters(final List<ReaderSplitter<?>> splitters) {
    final Map<String, SAMFileGATKReadWriter> outs = new LinkedHashMap<>();
    final SAMFileWriterFactory samFileWriterFactory = new SAMFileWriterFactory();
    final SAMFileHeader samFileHeaderIn = getHeaderForReads();
    // Build up a list of key options at each level.
    final List<List<?>> splitKeys = splitters.stream().map(splitter -> splitter.getSplitsBy(samFileHeaderIn)).collect(Collectors.toList());
    // For every combination of keys, add a SAMFileWriter.
    addKey(splitKeys, 0, "", key -> {
        outs.put(key, prepareSAMFileWriter(samFileWriterFactory, samFileHeaderIn, key));
    });
    return outs;
}
Also used : DocumentedFeature(org.broadinstitute.barclay.help.DocumentedFeature) LibraryNameSplitter(org.broadinstitute.hellbender.tools.readersplitters.LibraryNameSplitter) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) IOUtil(htsjdk.samtools.util.IOUtil) Argument(org.broadinstitute.barclay.argparser.Argument) StandardArgumentDefinitions(org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions) SampleNameSplitter(org.broadinstitute.hellbender.tools.readersplitters.SampleNameSplitter) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SAMFileHeader(htsjdk.samtools.SAMFileHeader) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ReadWalker(org.broadinstitute.hellbender.engine.ReadWalker) ReadProgramGroup(org.broadinstitute.hellbender.cmdline.programgroups.ReadProgramGroup) ReferenceContext(org.broadinstitute.hellbender.engine.ReferenceContext) FeatureContext(org.broadinstitute.hellbender.engine.FeatureContext) ReadGroupIdSplitter(org.broadinstitute.hellbender.tools.readersplitters.ReadGroupIdSplitter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) ReaderSplitter(org.broadinstitute.hellbender.tools.readersplitters.ReaderSplitter) Collectors(java.util.stream.Collectors) File(java.io.File) Consumer(java.util.function.Consumer) List(java.util.List) UserException(org.broadinstitute.hellbender.exceptions.UserException) SAMFileGATKReadWriter(org.broadinstitute.hellbender.utils.read.SAMFileGATKReadWriter) FilenameUtils(org.apache.commons.io.FilenameUtils) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileGATKReadWriter(org.broadinstitute.hellbender.utils.read.SAMFileGATKReadWriter) ArrayList(java.util.ArrayList) List(java.util.List) SAMFileHeader(htsjdk.samtools.SAMFileHeader) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with SAMFileWriterFactory

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

the class SplitReads method createUnknownOutOnDemand.

// Create an output stream on demand for holding any reads that do not have a value for one or more of the
// attributes we're grouping by
private SAMFileGATKReadWriter createUnknownOutOnDemand(String attributeValue) {
    if (!attributeValue.equals("." + UNKNOWN_OUT_PREFIX)) {
        // attribute for which a given read/group has no value; anything else indicates a coding error
        throw new GATKException.ShouldNeverReachHereException("Unrecognized attribute value found: " + attributeValue);
    }
    final SAMFileWriterFactory samFileWriterFactory = new SAMFileWriterFactory();
    final SAMFileHeader samFileHeaderIn = getHeaderForReads();
    return prepareSAMFileWriter(samFileWriterFactory, samFileHeaderIn, attributeValue);
}
Also used : SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 3 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory 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 4 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project hmftools by hartwigmedical.

the class Analysis method queryNameSortedBAM.

private static File queryNameSortedBAM(final SamReader reader, final QueryInterval[] intervals, final String name) throws IOException {
    final SAMFileHeader header = reader.getFileHeader().clone();
    header.setSortOrder(SAMFileHeader.SortOrder.queryname);
    final File file = File.createTempFile(name, ".bam");
    final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, false, file);
    final SAMRecordIterator iterator = reader.queryOverlapping(intervals);
    while (iterator.hasNext()) {
        writer.addAlignment(iterator.next());
    }
    iterator.close();
    writer.close();
    return file;
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Example 5 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project hmftools by hartwigmedical.

the class BreakPointInspectorApplication method writeToSlice.

private static void writeToSlice(final String path, final SamReader reader, final QueryInterval[] intervals) {
    final File outputBAM = new File(path);
    final SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(), true, outputBAM);
    final SAMRecordIterator iterator = reader.queryOverlapping(intervals);
    while (iterator.hasNext()) {
        writer.addAlignment(iterator.next());
    }
    iterator.close();
    writer.close();
}
Also used : SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) File(java.io.File)

Aggregations

SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)33 SAMFileWriter (htsjdk.samtools.SAMFileWriter)26 File (java.io.File)26 SAMRecord (htsjdk.samtools.SAMRecord)22 SAMFileHeader (htsjdk.samtools.SAMFileHeader)20 SamReader (htsjdk.samtools.SamReader)17 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)14 IOException (java.io.IOException)14 SamReaderFactory (htsjdk.samtools.SamReaderFactory)12 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)8 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)7 ArrayList (java.util.ArrayList)7 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)5 Test (org.testng.annotations.Test)5 Interval (htsjdk.samtools.util.Interval)4 BufferedReader (java.io.BufferedReader)4 List (java.util.List)4 BAMIndex (htsjdk.samtools.BAMIndex)3 DefaultSAMRecordFactory (htsjdk.samtools.DefaultSAMRecordFactory)3 SAMProgramRecord (htsjdk.samtools.SAMProgramRecord)3