Search in sources :

Example 61 with SamReader

use of htsjdk.samtools.SamReader in project jvarkit by lindenb.

the class BamStats02 method doWork.

@Override
public int doWork(List<String> args) {
    SamReader samFileReader = null;
    PrintWriter out = null;
    try {
        if (bedFile != null) {
            LOG.info("Reading BED file " + bedFile);
            this.intervals = super.readBedFileAsBooleanIntervalTreeMap(bedFile);
        }
        out = super.openFileOrStdoutAsPrintWriter(outputFile);
        boolean first = true;
        out.print("#");
        for (final STRING_PROPS p : STRING_PROPS.values()) {
            if (!first)
                out.print("\t");
            first = false;
            out.print(p.name());
        }
        for (final INT_PROPS p : INT_PROPS.values()) {
            out.print("\t");
            out.print(p.name());
        }
        for (final SAMFlag flg : SAMFlag.values()) {
            out.print("\t");
            out.print(flg.name());
        }
        out.print("\t");
        out.print("count");
        out.println();
        final SamReaderFactory srf = super.createSamReaderFactory();
        if (args.isEmpty()) {
            LOG.info("Reading from stdin");
            samFileReader = srf.open(SamInputResource.of(stdin()));
            run("stdin", samFileReader, out);
            samFileReader.close();
            samFileReader = null;
        } else {
            for (final String filename : IOUtils.unrollFiles(args)) {
                LOG.info("Reading from " + filename);
                samFileReader = srf.open(new File(filename));
                run(filename, samFileReader, out);
                samFileReader.close();
                samFileReader = null;
            }
        }
        out.flush();
        return RETURN_OK;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(samFileReader);
        CloserUtil.close(out);
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMFlag(htsjdk.samtools.SAMFlag) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 62 with SamReader

use of htsjdk.samtools.SamReader in project jvarkit by lindenb.

the class Biostar172515 method doWork.

@Override
public int doWork(final List<String> inputFiles) {
    final SamReaderFactory samReaderFactory = SamReaderFactory.makeDefault().setOption(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES, Boolean.TRUE).validationStringency(ValidationStringency.LENIENT);
    OutputStream stream = null;
    SamReader samReader = null;
    Set<String> args = IOUtils.unrollFiles(inputFiles);
    try {
        stream = super.openFileOrStdoutAsStream(this.outputFile);
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        this.w = xof.createXMLStreamWriter(stream);
        this.w.writeStartDocument("UTF-8", "1.0");
        this.w.writeStartElement("bai-list");
        for (final String filename : args) {
            this.w.writeStartElement("bam");
            this.w.writeAttribute("bam", filename);
            samReader = samReaderFactory.open(SamInputResource.of(filename));
            this.w.writeAttribute("has-index", String.valueOf(samReader.hasIndex()));
            if (!samReader.hasIndex()) {
                this.w.writeEndElement();
                samReader.close();
                continue;
            }
            final SamReader.Indexing indexing = samReader.indexing();
            if (!indexing.hasBrowseableIndex()) {
                this.w.writeComment("no browseable index");
                this.w.writeEndElement();
                samReader.close();
                continue;
            }
            final SAMSequenceDictionary dict = samReader.getFileHeader().getSequenceDictionary();
            this.w.writeAttribute("n_ref", String.valueOf(dict.size()));
            final BrowseableBAMIndex baiFile;
            try {
                baiFile = indexing.getBrowseableIndex();
            } catch (Exception err) {
                this.w.writeComment("no browseable index");
                this.w.writeEndElement();
                samReader.close();
                continue;
            }
            for (int tid = 0; tid < dict.size(); ++tid) {
                final SAMSequenceRecord ssr = dict.getSequence(tid);
                final BAMIndexMetaData baiMetaData = baiFile.getMetaData(tid);
                this.w.writeStartElement("reference");
                this.w.writeAttribute("ref-id", String.valueOf(tid));
                this.w.writeAttribute("ref-name", ssr.getSequenceName());
                this.w.writeAttribute("ref-length", String.valueOf(ssr.getSequenceLength()));
                this.w.writeAttribute("n_aligned", String.valueOf(baiMetaData.getAlignedRecordCount()));
                BinList binList = baiFile.getBinsOverlapping(tid, 1, ssr.getSequenceLength());
                int n_bin = 0;
                for (@SuppressWarnings("unused") final Bin binItem : binList) n_bin++;
                this.w.writeAttribute("n_bin", String.valueOf(n_bin));
                this.w.writeAttribute("n_no_coor", String.valueOf(baiMetaData.getUnalignedRecordCount()));
                for (final Bin binItem : binList) {
                    this.w.writeStartElement("bin");
                    this.w.writeAttribute("first-locus", String.valueOf(baiFile.getFirstLocusInBin(binItem)));
                    this.w.writeAttribute("last-locus", String.valueOf(baiFile.getLastLocusInBin(binItem)));
                    this.w.writeAttribute("level", String.valueOf(baiFile.getLevelForBin(binItem)));
                    final BAMFileSpan span = baiFile.getSpanOverlapping(binItem);
                    this.w.writeAttribute("first-offset", String.valueOf(span.getFirstOffset()));
                    final List<Chunk> chunks = span.getChunks();
                    this.w.writeAttribute("n_chunk", String.valueOf(chunks.size()));
                    for (final Chunk chunk : chunks) {
                        this.w.writeEmptyElement("chunk");
                        this.w.writeAttribute("chunk_beg", String.valueOf(chunk.getChunkStart()));
                        this.w.writeAttribute("chunk_end", String.valueOf(chunk.getChunkEnd()));
                    }
                    this.w.writeEndElement();
                }
                this.w.writeEndElement();
            }
            this.w.writeEndElement();
            samReader.close();
        }
        this.w.writeEndElement();
        this.w.flush();
        this.w.close();
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(this.w);
        CloserUtil.close(stream);
        CloserUtil.close(samReader);
        this.w = null;
    }
}
Also used : BrowseableBAMIndex(htsjdk.samtools.BrowseableBAMIndex) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) SamReaderFactory(htsjdk.samtools.SamReaderFactory) BAMIndexMetaData(htsjdk.samtools.BAMIndexMetaData) Bin(htsjdk.samtools.Bin) OutputStream(java.io.OutputStream) BAMFileSpan(htsjdk.samtools.BAMFileSpan) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) Chunk(htsjdk.samtools.Chunk) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) SamReader(htsjdk.samtools.SamReader) BinList(htsjdk.samtools.BinList)

Example 63 with SamReader

use of htsjdk.samtools.SamReader in project jvarkit by lindenb.

the class BioAlcidae method execute_bam.

private int execute_bam(String source) throws IOException {
    SamReader in = null;
    SAMRecordIterator iter = null;
    try {
        SamReaderFactory srf = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT);
        if (source == null) {
            in = srf.open(SamInputResource.of(stdin()));
        } else {
            in = srf.open(SamInputResource.of(source));
        }
        iter = in.iterator();
        bindings.put("header", in.getFileHeader());
        bindings.put("iter", iter);
        bindings.put("format", "sam");
        this.script.eval(bindings);
        this.writer.flush();
        return RETURN_OK;
    } catch (Exception e) {
        LOG.error(e);
        return -1;
    } finally {
        CloserUtil.close(in);
        CloserUtil.close(iter);
        bindings.remove("header");
        bindings.remove("iter");
        bindings.remove("format");
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SamReaderFactory(htsjdk.samtools.SamReaderFactory) RuntimeIOException(htsjdk.samtools.util.RuntimeIOException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 64 with SamReader

use of htsjdk.samtools.SamReader in project jvarkit by lindenb.

the class TestUtils method sortBamOnQueryName.

protected File sortBamOnQueryName(final Path bamFile, final Predicate<SAMRecord> pred) throws IOException {
    File sortedBam = this.createTmpFile(".bam");
    SamReader sr = SamReaderFactory.makeDefault().open(bamFile);
    SAMFileHeader outHeader = sr.getFileHeader().clone();
    outHeader.setSortOrder(SortOrder.queryname);
    SAMFileWriter w = new SAMFileWriterFactory().makeBAMWriter(outHeader, false, sortedBam);
    sr.iterator().stream().filter(R -> pred == null ? true : pred.test(R)).forEach(R -> w.addAlignment(R));
    w.close();
    sr.close();
    return sortedBam;
}
Also used : Arrays(java.util.Arrays) AfterGroups(org.testng.annotations.AfterGroups) IOUtil(htsjdk.samtools.util.IOUtil) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) SamFiles(htsjdk.samtools.SamFiles) Random(java.util.Random) Test(org.testng.annotations.Test) JFXPanel(javafx.embed.swing.JFXPanel) SAMFileHeader(htsjdk.samtools.SAMFileHeader) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) Application(javafx.application.Application) DefaultSAMRecordFactory(htsjdk.samtools.DefaultSAMRecordFactory) Vector(java.util.Vector) BeforeGroups(org.testng.annotations.BeforeGroups) ImageIO(javax.imageio.ImageIO) SAXParser(javax.xml.parsers.SAXParser) FastaSequenceIndexCreator(htsjdk.samtools.reference.FastaSequenceIndexCreator) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) JfxLauncher(com.github.lindenb.jvarkit.util.jcommander.JfxLauncher) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) BeforeClass(org.testng.annotations.BeforeClass) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) AfterTest(org.testng.annotations.AfterTest) SAMSequenceDictionaryCodec(htsjdk.samtools.SAMSequenceDictionaryCodec) Platform(javafx.application.Platform) CountDownLatch(java.util.concurrent.CountDownLatch) ReferenceSequenceFileFactory(htsjdk.samtools.reference.ReferenceSequenceFileFactory) List(java.util.List) Stream(java.util.stream.Stream) VariantContext(htsjdk.variant.variantcontext.VariantContext) Pattern(java.util.regex.Pattern) SamReaderFactory(htsjdk.samtools.SamReaderFactory) BeforeSuite(org.testng.annotations.BeforeSuite) FilenameFilter(java.io.FilenameFilter) CloseableIterator(htsjdk.samtools.util.CloseableIterator) ZipInputStream(java.util.zip.ZipInputStream) DataProvider(org.testng.annotations.DataProvider) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Function(java.util.function.Function) ValidationStringency(htsjdk.samtools.ValidationStringency) ArrayList(java.util.ArrayList) NcbiApiKey(com.github.lindenb.jvarkit.util.ncbi.NcbiApiKey) Interval(htsjdk.samtools.util.Interval) BeforeTest(org.testng.annotations.BeforeTest) Assert(org.testng.Assert) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) SAMRecordFactory(htsjdk.samtools.SAMRecordFactory) SAMSequenceDictionaryExtractor(htsjdk.variant.utils.SAMSequenceDictionaryExtractor) AfterClass(org.testng.annotations.AfterClass) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SamReader(htsjdk.samtools.SamReader) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Stage(javafx.stage.Stage) Paths(java.nio.file.Paths) BAMIndex(htsjdk.samtools.BAMIndex) BufferedReader(java.io.BufferedReader) AfterSuite(org.testng.annotations.AfterSuite) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) SamReader(htsjdk.samtools.SamReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Example 65 with SamReader

use of htsjdk.samtools.SamReader in project jvarkit by lindenb.

the class TestUtils method addClippingToBam.

protected File addClippingToBam(final File bamFile) throws IOException {
    final String bases = "ATGC";
    File clippedBam = this.createTmpFile(".bam");
    SamReader sr = SamReaderFactory.makeDefault().open(bamFile);
    SAMFileHeader inHeader = sr.getFileHeader();
    boolean createIndex = sr.hasIndex() && inHeader.getSortOrder().equals(SortOrder.coordinate);
    if (createIndex) {
        this.deleteFilesAtExit.add(new File(bamFile.getParentFile(), IOUtil.basename(clippedBam) + BAMIndex.BAMIndexSuffix));
    }
    final SAMFileWriter w = new SAMFileWriterFactory().setCreateIndex(createIndex).makeBAMWriter(inHeader, true, clippedBam);
    sr.iterator().stream().map(R -> {
        if (R.getReadUnmappedFlag() || R.getCigar() == null)
            return R;
        if (R.getCigar().isClipped())
            return R;
        if (R.getBaseQualities().equals(SAMRecord.NULL_QUALS))
            return R;
        if (R.getBaseQualityString().equals(SAMRecord.NULL_QUALS_STRING))
            return R;
        for (int side = 0; side < 2; side++) {
            final String cigar;
            boolean hard = this.random.nextBoolean();
            final int clipLen = 1 + this.random.nextInt(100);
            final StringBuilder seq = new StringBuilder();
            final StringBuilder qual = new StringBuilder();
            if (hard) {
                cigar = String.valueOf(clipLen) + "H";
            } else {
                cigar = String.valueOf(clipLen) + "S";
                for (int x = 0; x < clipLen; ++x) {
                    seq.append(bases.charAt(this.random.nextInt(bases.length())));
                    qual.append("#");
                }
            }
            if (side == 0) {
                R.setReadString(seq.toString() + R.getReadString());
                R.setBaseQualityString(qual.toString() + R.getBaseQualityString());
                R.setCigarString(cigar + R.getCigarString());
            } else {
                R.setCigarString(R.getCigarString() + cigar);
                R.setReadString(R.getReadString() + seq.toString());
                R.setBaseQualityString(R.getBaseQualityString() + qual.toString());
            }
        }
        return R;
    }).forEach(R -> w.addAlignment(R));
    w.close();
    sr.close();
    assertIsValidBam(bamFile);
    return bamFile;
}
Also used : Arrays(java.util.Arrays) AfterGroups(org.testng.annotations.AfterGroups) IOUtil(htsjdk.samtools.util.IOUtil) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) SamFiles(htsjdk.samtools.SamFiles) Random(java.util.Random) Test(org.testng.annotations.Test) JFXPanel(javafx.embed.swing.JFXPanel) SAMFileHeader(htsjdk.samtools.SAMFileHeader) SortOrder(htsjdk.samtools.SAMFileHeader.SortOrder) Application(javafx.application.Application) DefaultSAMRecordFactory(htsjdk.samtools.DefaultSAMRecordFactory) Vector(java.util.Vector) BeforeGroups(org.testng.annotations.BeforeGroups) ImageIO(javax.imageio.ImageIO) SAXParser(javax.xml.parsers.SAXParser) FastaSequenceIndexCreator(htsjdk.samtools.reference.FastaSequenceIndexCreator) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) CloserUtil(htsjdk.samtools.util.CloserUtil) PrintWriter(java.io.PrintWriter) Predicate(java.util.function.Predicate) JfxLauncher(com.github.lindenb.jvarkit.util.jcommander.JfxLauncher) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) BeforeClass(org.testng.annotations.BeforeClass) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) AfterTest(org.testng.annotations.AfterTest) SAMSequenceDictionaryCodec(htsjdk.samtools.SAMSequenceDictionaryCodec) Platform(javafx.application.Platform) CountDownLatch(java.util.concurrent.CountDownLatch) ReferenceSequenceFileFactory(htsjdk.samtools.reference.ReferenceSequenceFileFactory) List(java.util.List) Stream(java.util.stream.Stream) VariantContext(htsjdk.variant.variantcontext.VariantContext) Pattern(java.util.regex.Pattern) SamReaderFactory(htsjdk.samtools.SamReaderFactory) BeforeSuite(org.testng.annotations.BeforeSuite) FilenameFilter(java.io.FilenameFilter) CloseableIterator(htsjdk.samtools.util.CloseableIterator) ZipInputStream(java.util.zip.ZipInputStream) DataProvider(org.testng.annotations.DataProvider) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Function(java.util.function.Function) ValidationStringency(htsjdk.samtools.ValidationStringency) ArrayList(java.util.ArrayList) NcbiApiKey(com.github.lindenb.jvarkit.util.ncbi.NcbiApiKey) Interval(htsjdk.samtools.util.Interval) BeforeTest(org.testng.annotations.BeforeTest) Assert(org.testng.Assert) IOUtils(com.github.lindenb.jvarkit.io.IOUtils) SAMRecordFactory(htsjdk.samtools.SAMRecordFactory) SAMSequenceDictionaryExtractor(htsjdk.variant.utils.SAMSequenceDictionaryExtractor) AfterClass(org.testng.annotations.AfterClass) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SamReader(htsjdk.samtools.SamReader) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Stage(javafx.stage.Stage) Paths(java.nio.file.Paths) BAMIndex(htsjdk.samtools.BAMIndex) BufferedReader(java.io.BufferedReader) AfterSuite(org.testng.annotations.AfterSuite) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) SamReader(htsjdk.samtools.SamReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

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