private List<SAMRecord> toSAMRecord(BAMRecordView view, SAMFileHeader samHeader) { BAMRecordCodec bc = new BAMRecordCodec(samHeader); bc.setInputStream(new ByteArrayInputStream(view.buf, 0, view.start)); List<SAMRecord> records = new ArrayList<SAMRecord>(); SAMRecord record; while ((record = bc.decode()) != null) { records.add(record); } return records; }
@Override public void write(DataOutput out) throws IOException { // In theory, it shouldn't matter whether we give a header to // BAMRecordCodec or not, since the representation of an alignment in BAM // doesn't depend on the header data at all. Only its interpretation // does, and a simple read/write codec shouldn't really have anything to // say about that. (But in practice, it already does matter for decode(), // which is why LazyBAMRecordFactory exists.) final BAMRecordCodec codec = new BAMRecordCodec(record.getHeader()); codec.setOutputStream(new DataOutputWrapper(out)); codec.encode(record); } @Override public void readFields(DataInput in) throws IOException {
@Override public SortingCollection.Codec<SAMRecordsForCluster> clone() { return new Codec(numRecords, bamCodec.clone()); } }
/** * Read the next record from the input stream and convert into a java object. * * @return null if no more records. Should throw exception if EOF is encountered in the middle of * a record. */ @Override public SAMRecord decode() { final Integer recordLength = decodeRecordLength(); if (recordLength == null) { return null; } return decode(recordLength); }
@Override public void readFields(DataInput in) throws IOException { lazyCodec.setInputStream(new DataInputWrapper(in)); record = lazyCodec.decode(); }
@Override public BAMRecordCodec clone() { // Do not clone the references to codecs, as they must be distinct for each instance. return new BAMRecordCodec(this.header, this.samRecordFactory); }
/** * @param advance Trick to enable subclass to do more setup before advancing */ BAMFileIterator(final boolean advance) { this.bamRecordCodec = new BAMRecordCodec(getFileHeader(), samRecordFactory); this.bamRecordCodec.setInputStream(BAMFileReader.this.mStream.getInputStream(), BAMFileReader.this.mStream.getInputFileName()); if (advance) { advance(); } }
private void prepareToWriteAlignments() { if (bamRecordCodec == null) { bamRecordCodec = new BAMRecordCodec(getFileHeader()); bamRecordCodec.setOutputStream(outputBinaryCodec.getOutputStream(), getFilename()); } }
private void decode(BamRecordDecodingInfo info) { if (info.buffer == null) { // decode directly from the parent codec info.record = bamRecordCodec.decode(info.recordLength); BAMRecordCodec codec = mAvailableIdleCodec.poll(); if (codec == null) { codec = bamRecordCodec.clone(); codec.setInputStream(is); } else { codec.setInputStream(is, mStream.getInputFileName()); info.record = codec.decode(info.recordLength); mAvailableIdleCodec.add(codec); info.buffer = null;
@Override public SAMRecordsForCluster decode() { final SAMRecord zerothRecord = bamCodec.decode(); if (zerothRecord == null) return null; final SAMRecordsForCluster ret = new SAMRecordsForCluster(numRecords); ret.records[0] = zerothRecord; for (int i = 1; i < numRecords; ++i) { ret.records[i] = bamCodec.decode(); if (ret.records[i] == null) { throw new IllegalStateException(String.format("Expected to read %d records but read only %d", numRecords, i)); } } return ret; }
@Override public void encode(final SAMRecordsForCluster val) { if (val.records.length != numRecords) { throw new IllegalStateException(String.format("Expected number of clusters %d != actual %d", numRecords, val.records.length)); } for (final SAMRecord rec : val.records) { bamCodec.encode(rec); } }
codec.setOutputStream(os); for (CramCompressionRecord record : records) { SAMRecord samRecord = f.create(record); codec.encode(samRecord);
@Override public void setInputStream(final InputStream is) { bamCodec.setInputStream(is); }
@Override public void setOutputStream(final OutputStream os) { bamCodec.setOutputStream(os); }
alignment.setCigar(makeSentinelCigar(alignment.getCigar())); alignment.setAttribute(CG.name(), cigarEncoding); warnIfReferenceIsTooLargeForBinField(alignment); indexBin = alignment.computeIndexingBinIfAbsent(alignment);
@Override public BAMRecordCodec clone() { // Do not clone the references to codecs, as they must be distinct for each instance. return new BAMRecordCodec(this.header, this.samRecordFactory); }
/** * @param advance Trick to enable subclass to do more setup before advancing */ BAMFileIterator(final boolean advance) { this.bamRecordCodec = new BAMRecordCodec(getFileHeader(), samRecordFactory); this.bamRecordCodec.setInputStream(BAMFileReader.this.mStream.getInputStream(), BAMFileReader.this.mStream.getInputFileName()); if (advance) { advance(); } }
@Override public void readFields(DataInput in) throws IOException { lazyCodec.setInputStream(new DataInputWrapper(in)); record = lazyCodec.decode(); }
private void prepareToWriteAlignments() { if (bamRecordCodec == null) { bamRecordCodec = new BAMRecordCodec(getFileHeader()); bamRecordCodec.setOutputStream(outputBinaryCodec.getOutputStream(), getFilename()); } }
@Override public SAMRecordsForCluster decode() { final SAMRecord zerothRecord = bamCodec.decode(); if (zerothRecord == null) return null; final SAMRecordsForCluster ret = new SAMRecordsForCluster(numRecords); ret.records[0] = zerothRecord; for (int i = 1; i < numRecords; ++i) { ret.records[i] = bamCodec.decode(); if (ret.records[i] == null) { throw new IllegalStateException(String.format("Expected to read %d records but read only %d", numRecords, i)); } } return ret; }