PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/datatypes/converters/vcf_to_interval_index_converter.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 34 lines | 19 code | 7 blank | 8 comment | 3 complexity | 794207671d1d30b210f8c39dfbf9f6f9 MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""
 4Convert from VCF file to interval index file.
 5"""
 6
 7from __future__ import division
 8
 9import optparse
10from galaxy import eggs
11import pkg_resources; pkg_resources.require( "bx-python" )
12import galaxy_utils.sequence.vcf
13from bx.interval_index_file import Indexes
14
15def main():
16    # Read options, args.
17    parser = optparse.OptionParser()
18    (options, args) = parser.parse_args()
19    in_file, out_file = args
20
21    # Do conversion.
22    index = Indexes()
23    reader = galaxy_utils.sequence.vcf.Reader( open( in_file ) )
24    offset = reader.metadata_len
25    for vcf_line in reader:
26        # VCF format provides a chrom and 1-based position for each variant.
27        # IntervalIndex expects 0-based coordinates.
28        index.add( vcf_line.chrom, vcf_line.pos-1, vcf_line.pos, offset )
29        offset += len( vcf_line.raw_line )
30
31    index.write( open( out_file, "w" ) )
32
33if __name__ == "__main__":
34    main()