use of htsjdk.tribble.TribbleException.MalformedFeatureFile in project ASCIIGenome by dariober.
the class MakeTabixIndex method blockCompressAndIndex.
/**
* Block compress input file and create associated tabix index.
* @throws IOException
* @throws InvalidRecordException
*/
private void blockCompressAndIndex(String intab, File bgzfOut, TabixFormat fmt) throws IOException, InvalidRecordException {
LineIterator lin = utils.IOUtils.openURIForLineIterator(intab);
BlockCompressedOutputStream writer = new BlockCompressedOutputStream(bgzfOut);
long filePosition = writer.getFilePointer();
TabixIndexCreator indexCreator = new TabixIndexCreator(fmt);
boolean first = true;
// This is relevant to vcf files only: Prepare header and codec
// ------------------------------------------------------------
VCFHeader vcfHeader = null;
VCFCodec vcfCodec = null;
if (fmt.equals(TabixFormat.VCF)) {
try {
VCFFileReader vcfr = new VCFFileReader(new File(intab), false);
// new VCFHeader();
vcfHeader = vcfr.getFileHeader();
vcfr.close();
} catch (MalformedFeatureFile e) {
vcfHeader = new VCFHeader();
}
vcfCodec = new VCFCodec();
vcfCodec.setVCFHeader(vcfHeader, Utils.getVCFHeaderVersion(vcfHeader));
}
// ------------------------------------------------------------
int nWarnings = 10;
while (lin.hasNext()) {
String line = lin.next().trim();
try {
if (line.isEmpty() || line.startsWith("track ")) {
continue;
}
if (line.startsWith("#")) {
writer.write((line + "\n").getBytes());
filePosition = writer.getFilePointer();
continue;
}
if (line.startsWith("##FASTA")) {
break;
}
if (first && !fmt.equals(TabixFormat.VCF)) {
String dummy = this.makeDummyLine(line, fmt);
addLineToIndex(dummy, indexCreator, filePosition, fmt, null, null);
writer.write(dummy.getBytes());
writer.write('\n');
filePosition = writer.getFilePointer();
first = false;
}
addLineToIndex(line, indexCreator, filePosition, fmt, vcfHeader, vcfCodec);
writer.write(line.getBytes());
writer.write('\n');
filePosition = writer.getFilePointer();
} catch (Exception e) {
if (e.getMessage().contains("added out sequence of order") || e.getMessage().contains("Features added out of order")) {
// Get a string marker for out-of-order from htsjdk/tribble/index/tabix/TabixIndexCreator.java
throw new InvalidRecordException();
}
if (nWarnings >= 0) {
System.err.println("Warning: " + e.getMessage() + ". Skipping:\n" + line);
}
if (nWarnings == 0) {
System.err.println("Additional warnings will not be show.");
}
nWarnings--;
}
}
writer.flush();
Index index = indexCreator.finalizeIndex(writer.getFilePointer());
index.writeBasedOnFeatureFile(bgzfOut);
writer.close();
CloserUtil.close(lin);
}
Aggregations