Search in sources :

Example 1 with GtfLine

use of utils.GtfLine in project ASCIIGenome by dariober.

the class UcscFetch method blockCompressAndIndex.

/**
 * Block compress input file and create associated tabix index. Newly created file and index are
 * deleted on exit if deleteOnExit true.
 * @throws IOException
 * @throws InvalidRecordException
 */
private void blockCompressAndIndex(String in, String bgzfOut, boolean deleteOnExit) throws IOException, InvalidRecordException {
    File inFile = new File(in);
    File outFile = new File(bgzfOut);
    LineIterator lin = utils.IOUtils.openURIForLineIterator(inFile.getAbsolutePath());
    BlockCompressedOutputStream writer = new BlockCompressedOutputStream(outFile);
    long filePosition = writer.getFilePointer();
    TabixIndexCreator indexCreator = new TabixIndexCreator(TabixFormat.GFF);
    while (lin.hasNext()) {
        String line = lin.next();
        GtfLine gtf = new GtfLine(line.split("\t"));
        writer.write(line.getBytes());
        writer.write('\n');
        indexCreator.addFeature(gtf, filePosition);
        filePosition = writer.getFilePointer();
    }
    writer.flush();
    File tbi = new File(bgzfOut + TabixUtils.STANDARD_INDEX_EXTENSION);
    if (tbi.exists() && tbi.isFile()) {
        writer.close();
        throw new RuntimeException("Index file exists: " + tbi);
    }
    Index index = indexCreator.finalizeIndex(writer.getFilePointer());
    index.writeBasedOnFeatureFile(outFile);
    writer.close();
    if (deleteOnExit) {
        outFile.deleteOnExit();
        File idx = new File(outFile.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
        idx.deleteOnExit();
    }
}
Also used : GtfLine(utils.GtfLine) BlockCompressedOutputStream(htsjdk.samtools.util.BlockCompressedOutputStream) TabixIndexCreator(htsjdk.tribble.index.tabix.TabixIndexCreator) Index(htsjdk.tribble.index.Index) File(java.io.File) LineIterator(htsjdk.tribble.readers.LineIterator)

Example 2 with GtfLine

use of utils.GtfLine in project ASCIIGenome by dariober.

the class MakeTabixIndex method addLineToIndex.

/**
 * Set vcfHeader and vcfCodec to null if reading non-vcf line.
 */
private void addLineToIndex(String line, TabixIndexCreator indexCreator, long filePosition, TabixFormat fmt, VCFHeader vcfHeader, VCFCodec vcfCodec) throws InvalidRecordException {
    if (fmt.equals(TabixFormat.BED)) {
        BedLineCodec bedCodec = new BedLineCodec();
        BedLine bed = bedCodec.decode(line);
        indexCreator.addFeature(bed, filePosition);
    } else if (fmt.equals(TabixFormat.GFF)) {
        GtfLine gtf = new GtfLine(line.split("\t"));
        indexCreator.addFeature(gtf, filePosition);
    } else if (fmt.equals(TabixFormat.VCF)) {
        VariantContext vcf = vcfCodec.decode(line);
        indexCreator.addFeature(vcf, filePosition);
    } else {
        System.err.println("Unexpected TabixFormat: " + fmt.sequenceColumn + " " + fmt.startPositionColumn);
        throw new InvalidRecordException();
    }
}
Also used : GtfLine(utils.GtfLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) InvalidRecordException(exceptions.InvalidRecordException) BedLineCodec(utils.BedLineCodec) BedLine(utils.BedLine)

Aggregations

GtfLine (utils.GtfLine)2 InvalidRecordException (exceptions.InvalidRecordException)1 BlockCompressedOutputStream (htsjdk.samtools.util.BlockCompressedOutputStream)1 Index (htsjdk.tribble.index.Index)1 TabixIndexCreator (htsjdk.tribble.index.tabix.TabixIndexCreator)1 LineIterator (htsjdk.tribble.readers.LineIterator)1 VariantContext (htsjdk.variant.variantcontext.VariantContext)1 File (java.io.File)1 BedLine (utils.BedLine)1 BedLineCodec (utils.BedLineCodec)1