PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/datatypes/converters/interval_to_summary_tree_converter.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 53 lines | 36 code | 6 blank | 11 comment | 6 complexity | cd512dff51d116eda5124031089a99f8 MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""
 4Convert from interval file to summary tree 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"""
 9from __future__ import division
10
11import sys, fileinput
12from galaxy import eggs
13import pkg_resources; pkg_resources.require( "bx-python" )
14from galaxy.visualization.tracks.summary import *
15from bx.intervals.io import *
16from bx.cookbook import doc_optparse
17from galaxy.datatypes.util.gff_util import *
18
19def main():
20    # Read options, args.
21    options, args = doc_optparse.parse( __doc__ )
22    try:
23        gff_format = bool( options.gff )
24        input_fname, out_fname = args
25    except:
26        doc_optparse.exception()
27        
28    # Do conversion.
29    # TODO: take column numbers from command line.
30    if gff_format:
31        reader_wrapper_class = GFFReaderWrapper
32        chr_col, start_col, end_col, strand_col = ( 0, 3, 4, 6 )
33    else:
34        reader_wrapper_class = NiceReaderWrapper
35        chr_col, start_col, end_col, strand_col = ( 0, 1, 2, 5 )
36    reader_wrapper = reader_wrapper_class( fileinput.FileInput( input_fname ),
37                                            chrom_col=chr_col,
38                                            start_col=start_col,
39                                            end_col=end_col,
40                                            strand_col=strand_col,
41                                            fix_strand=True )
42    st = SummaryTree(block_size=25, levels=6, draw_cutoff=150, detail_cutoff=30)
43    for feature in list( reader_wrapper ):
44        if isinstance( feature, GenomicInterval ):
45            # Tree expects BED coordinates.
46            if type( feature ) is GFFFeature:
47                convert_gff_coords_to_bed( feature )
48            st.insert_range( feature.chrom, long( feature.start ), long( feature.end ) )
49    
50    st.write(out_fname)
51
52if __name__ == "__main__": 
53    main()