root/galaxy-central/lib/galaxy/datatypes/converters/interval_to_interval_index_converter.py @ 2

リビジョン 2, 1.7 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1#!/usr/bin/env python
2
3"""
4Convert from interval file to interval index file. Default input file format is BED (0-based, half-open intervals).
5
6usage: %prog in_file out_file
7    -G, --gff: input is GFF format, meaning start and end coordinates are 1-based, closed interval
8"""
9
10from __future__ import division
11
12import sys, fileinput
13from galaxy import eggs
14import pkg_resources; pkg_resources.require( "bx-python" )
15from galaxy.visualization.tracks.summary import *
16from bx.cookbook import doc_optparse
17from galaxy.tools.util.gff_util import convert_gff_coords_to_bed
18from bx.interval_index_file import Indexes
19
20def 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
53if __name__ == "__main__":
54    main()
55   
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。