/*******************************************************************************
* Copyright 2013 EMBL-EBI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package net.sf.cram.index;
import htsjdk.samtools.CRAMBAIIndexer;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.ValidationStringency;
import htsjdk.samtools.cram.build.CramIO;
import htsjdk.samtools.cram.io.CountingInputStream;
import htsjdk.samtools.cram.structure.Container;
import htsjdk.samtools.cram.structure.ContainerIO;
import htsjdk.samtools.cram.structure.CramHeader;
import htsjdk.samtools.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
class BaiIndexer {
private static Log log = Log.getInstance(BaiIndexer.class);
public CountingInputStream is;
public SAMFileHeader samFileHeader;
public CRAMBAIIndexer indexer;
private CramHeader cramHeader;
public BaiIndexer(InputStream is, SAMFileHeader samFileHeader, File output) {
this.is = new CountingInputStream(is);
this.samFileHeader = samFileHeader;
indexer = new CRAMBAIIndexer(output, samFileHeader);
}
public BaiIndexer(InputStream is, OutputStream output) throws IOException {
this.is = new CountingInputStream(is);
cramHeader = CramIO.readCramHeader(this.is);
samFileHeader = cramHeader.getSamFileHeader();
indexer = new CRAMBAIIndexer(output, samFileHeader);
}
private boolean nextContainer() throws IOException {
long offset = is.getCount();
Container c = ContainerIO.readContainer(cramHeader.getVersion(), is);
if (c.isEOF())
return false;
c.offset = offset;
indexer.processContainer(c, ValidationStringency.DEFAULT_STRINGENCY);
log.info("INDEXED: " + c.toString());
return true;
}
private void index() throws IOException {
while (true) {
if (!nextContainer())
break;
}
}
public void run() throws IOException {
index();
indexer.finish();
}
}