Search in sources :

Example 1 with Bin

use of htsjdk.samtools.Bin 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)

Aggregations

BAMFileSpan (htsjdk.samtools.BAMFileSpan)1 BAMIndexMetaData (htsjdk.samtools.BAMIndexMetaData)1 Bin (htsjdk.samtools.Bin)1 BinList (htsjdk.samtools.BinList)1 BrowseableBAMIndex (htsjdk.samtools.BrowseableBAMIndex)1 Chunk (htsjdk.samtools.Chunk)1 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)1 SamReader (htsjdk.samtools.SamReader)1 SamReaderFactory (htsjdk.samtools.SamReaderFactory)1 OutputStream (java.io.OutputStream)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1