[2] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Convert from interval file to summary tree 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 | from __future__ import division |
---|
| 10 | |
---|
| 11 | import sys, fileinput |
---|
| 12 | from galaxy import eggs |
---|
| 13 | import pkg_resources; pkg_resources.require( "bx-python" ) |
---|
| 14 | from galaxy.visualization.tracks.summary import * |
---|
| 15 | from bx.intervals.io import * |
---|
| 16 | from bx.cookbook import doc_optparse |
---|
| 17 | from galaxy.tools.util.gff_util import GFFReaderWrapper |
---|
| 18 | |
---|
| 19 | def 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 line in list( reader_wrapper ): |
---|
| 44 | if type( line ) is GenomicInterval: |
---|
| 45 | st.insert_range( line.chrom, long( line.start ), long( line.end ) ) |
---|
| 46 | |
---|
| 47 | st.write(out_fname) |
---|
| 48 | |
---|
| 49 | if __name__ == "__main__": |
---|
| 50 | main() |
---|