1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | Convert from interval file to interval index file. Default input file format is BED (0-based, half-open intervals). |
---|
5 | |
---|
6 | usage: %prog in_file out_file |
---|
7 | -G, --gff: input is GFF format, meaning start and end coordinates are 1-based, closed interval |
---|
8 | """ |
---|
9 | |
---|
10 | from __future__ import division |
---|
11 | |
---|
12 | import sys, fileinput |
---|
13 | from galaxy import eggs |
---|
14 | import pkg_resources; pkg_resources.require( "bx-python" ) |
---|
15 | from galaxy.visualization.tracks.summary import * |
---|
16 | from bx.cookbook import doc_optparse |
---|
17 | from galaxy.tools.util.gff_util import convert_gff_coords_to_bed |
---|
18 | from bx.interval_index_file import Indexes |
---|
19 | |
---|
20 | def main(): |
---|
21 | |
---|
22 | # Read options, args. |
---|
23 | options, args = doc_optparse.parse( __doc__ ) |
---|
24 | try: |
---|
25 | gff_format = bool( options.gff ) |
---|
26 | input_fname, out_fname = args |
---|
27 | except: |
---|
28 | doc_optparse.exception() |
---|
29 | |
---|
30 | # Do conversion. |
---|
31 | # TODO: take column numbers from command line. |
---|
32 | if gff_format: |
---|
33 | chr_col, start_col, end_col = ( 0, 3, 4 ) |
---|
34 | else: |
---|
35 | chr_col, start_col, end_col = ( 0, 1, 2 ) |
---|
36 | index = Indexes() |
---|
37 | offset = 0 |
---|
38 | for line in open(input_fname, "r"): |
---|
39 | feature = line.strip().split() |
---|
40 | if not feature or feature[0].startswith("track") or feature[0].startswith("#"): |
---|
41 | offset += len(line) |
---|
42 | continue |
---|
43 | chrom = feature[ chr_col ] |
---|
44 | chrom_start = int( feature[ start_col ] ) |
---|
45 | chrom_end = int( feature[ end_col ] ) |
---|
46 | if gff_format: |
---|
47 | chrom_start, chrom_end = convert_gff_coords_to_bed( [chrom_start, chrom_end ] ) |
---|
48 | index.add( chrom, chrom_start, chrom_end, offset ) |
---|
49 | offset += len(line) |
---|
50 | |
---|
51 | index.write( open(out_fname, "w") ) |
---|
52 | |
---|
53 | if __name__ == "__main__": |
---|
54 | main() |
---|
55 | |
---|