/** * Write the meta data represented by the chunkLists associated with bin MAX_BINS 37450 * * @param metaData information describing numAligned records, numUnAligned, etc */ private void writeChunkMetaData(final int reference, final BAMIndexMetaData metaData) { final int nChunks = metaData == null ? 0 : 2; pw.print(" Ref " + reference + " bin 37450 has n_chunk= " + nChunks); if (nChunks == 0) { pw.println(); } else { pw.println(" Chunk: " + // c.toString() + " start: " + Long.toString(metaData.getFirstOffset(), 16) + " end: " + Long.toString(metaData.getLastOffset(), 16)); pw.println(" Chunk: " + // c.toString() + " start: " + Long.toString(metaData.getAlignedRecordCount(), 16) + " end: " + Long.toString(metaData.getUnalignedRecordCount(), 16)); } }
/** * @return the count of records with no coordinate positions */ public long getNoCoordinateRecordCount() { return indexStats.getNoCoordinateRecordCount(); }
/** * Constructor used when writing an index * construct one instance for each index generated */ BAMIndexMetaData() { noCoordinateRecords = 0; newReference(); }
throw new SAMException("No index for bam file " + inputBamFile); BAMIndexMetaData[] data = getIndexStats(bam); continue; System.out.println("\tAligned= " + data[i].getAlignedRecordCount() + "\tUnaligned= " + data[i].getUnalignedRecordCount()); System.out.println("NoCoordinateCount= " + data[0].getNoCoordinateRecordCount()); } catch (IOException e) { throw new SAMException("Exception in getting index statistics", e);
public static long countSamTotalRecord(final File samFile) { final SamReader reader = SamReaderFactory.make().open(samFile); assert reader.hasIndex(); long total = 0; for (int i = 0; i < reader.getFileHeader().getSequenceDictionary().size(); i++) { total += reader.indexing().getIndex().getMetaData(i).getAlignedRecordCount(); total += reader.indexing().getIndex().getMetaData(i).getUnalignedRecordCount(); } return total; } }
count += meta.getAlignedRecordCount();
indexStats.recordMetaData(rec);
/** * Prints meta-data statistics from BAM index (.bai) file * Statistics include count of aligned and unaligned reads for each reference sequence * and a count of all records with no start coordinate */ static public BAMIndexMetaData[] getIndexStats(final BAMFileReader bam){ AbstractBAMFileIndex index = (AbstractBAMFileIndex) bam.getIndex(); // read through all the bins of every reference. int nRefs = index.getNumberOfReferences(); BAMIndexMetaData[] result = new BAMIndexMetaData[nRefs == 0 ? 1 : nRefs]; for (int i = 0; i < nRefs; i++) { result[i] = index.getMetaData(i); } if (result[0] == null){ result[0] = new BAMIndexMetaData(); } final Long noCoordCount = index.getNoCoordinateCount(); if (noCoordCount != null) // null in old index files without metadata result[0].setNoCoordinateRecordCount(noCoordCount); return result; } }
/** * Return meta data for the given reference including information about number of aligned, unaligned, and noCoordinate records * * @param reference the reference of interest * @return meta data for the reference */ @Override public BAMIndexMetaData getMetaData(final int reference) { seek(4); final List<Chunk> metaDataChunks = new ArrayList<Chunk>(); final int sequenceCount = readInteger(); if (reference >= sequenceCount) { return null; } skipToSequence(reference); final int binCount = readInteger(); for (int binNumber = 0; binNumber < binCount; binNumber++) { final int indexBin = readInteger(); final int nChunks = readInteger(); if (indexBin == GenomicIndexUtil.MAX_BINS) { readChunks(nChunks, metaDataChunks); } else { skipBytes(16 * nChunks); } } return new BAMIndexMetaData(metaDataChunks); }
throw new SAMException("No index for bam file " + inputBamFile); BAMIndexMetaData[] data = getIndexStats(bam); continue; System.out.println("\tAligned= " + data[i].getAlignedRecordCount() + "\tUnaligned= " + data[i].getUnalignedRecordCount()); System.out.println("NoCoordinateCount= " + data[0].getNoCoordinateRecordCount()); } catch (IOException e) { throw new SAMException("Exception in getting index statistics", e);
public static long getAlignedReadCount(String bam) throws IOException{ /* ------------------------------------------------------ */ /* This chunk prepares SamReader from local bam or URL bam */ UrlValidator urlValidator = new UrlValidator(); SamReaderFactory srf=SamReaderFactory.make(); srf.validationStringency(ValidationStringency.SILENT); SamReader samReader; if(urlValidator.isValid(bam)){ samReader = SamReaderFactory.makeDefault().open( SamInputResource.of(new URL(bam)).index(new URL(bam + ".bai")) ); } else { samReader= srf.open(new File(bam)); } /* ------------------------------------------------------ */ List<SAMSequenceRecord> sequences = samReader.getFileHeader().getSequenceDictionary().getSequences(); long alnCount= 0; for(SAMSequenceRecord x : sequences){ alnCount += samReader.indexing().getIndex().getMetaData(x.getSequenceIndex()).getAlignedRecordCount(); } samReader.close(); return alnCount; }
indexStats.recordMetaData(rec);
/** * Prints meta-data statistics from BAM index (.bai or .csi) file * Statistics include count of aligned and unaligned reads for each reference sequence * and a count of all records with no start coordinate */ static public BAMIndexMetaData[] getIndexStats(final BAMFileReader bam) { AbstractBAMFileIndex index = (AbstractBAMFileIndex) bam.getIndex(); // read through all the bins of every reference. int nRefs = index.getNumberOfReferences(); BAMIndexMetaData[] result = new BAMIndexMetaData[nRefs == 0 ? 1 : nRefs]; for (int i = 0; i < nRefs; i++) { result[i] = index.getMetaData(i); } if (result[0] == null) { result[0] = new BAMIndexMetaData(); } final Long noCoordCount = index.getNoCoordinateCount(); if (noCoordCount != null) // null in old index files without metadata result[0].setNoCoordinateRecordCount(noCoordCount); return result; } }
return new BAMIndexMetaData(metaDataChunks);
/** * Write the meta data represented by the chunkLists associated with bin MAX_BINS 37450 * * @param metaData information describing numAligned records, numUnAligned, etc */ private void writeChunkMetaData(final int reference, final BAMIndexMetaData metaData) { final int nChunks = metaData == null ? 0 : 2; pw.print(" Ref " + reference + " bin 37450 has n_chunk= " + nChunks); if (nChunks == 0) { pw.println(); } else { pw.println(" Chunk: " + // c.toString() + " start: " + Long.toString(metaData.getFirstOffset(), 16) + " end: " + Long.toString(metaData.getLastOffset(), 16)); pw.println(" Chunk: " + // c.toString() + " start: " + Long.toString(metaData.getAlignedRecordCount(), 16) + " end: " + Long.toString(metaData.getUnalignedRecordCount(), 16)); } }
BAMIndexMetaData[] data = getIndexStats(bam); if (data == null) { throw new SAMException("Exception in getting index statistics"); continue; System.out.println("\tAligned= " + data[i].getAlignedRecordCount() + "\tUnaligned= " + data[i].getUnalignedRecordCount()); System.out.println("NoCoordinateCount= " + data[0].getNoCoordinateRecordCount()); } catch (IOException e) { throw new SAMException("Exception in getting index statistics", e);
final BAMIndexMetaData metaData_0 = index.getMetaData(0); Assert.assertNotNull(metaData_0); Assert.assertEquals(metaData_0.getAlignedRecordCount(), 1); Assert.assertEquals(metaData_1.getAlignedRecordCount(), 3); Assert.assertEquals(metaData_2.getAlignedRecordCount(), 2);
/** * Constructor used when writing an index * construct one instance for each index generated */ BAMIndexMetaData() { noCoordinateRecords = 0; newReference(); }
/** * @return the count of records with no coordinate positions */ public long getNoCoordinateRecordCount() { return indexStats.getNoCoordinateRecordCount(); }