comparison gops_subtract.py @ 3:ecb36112b056 draft

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
author devteam
date Wed, 11 Nov 2015 12:49:24 -0500
parents 5bc2dacbe729
children 0145969324c4
comparison
equal deleted inserted replaced
2:c19a2a29c561 3:ecb36112b056
9 -m, --mincols=N: Require this much overlap (default 1bp) 9 -m, --mincols=N: Require this much overlap (default 1bp)
10 -p, --pieces: just print pieces of second set (after padding) 10 -p, --pieces: just print pieces of second set (after padding)
11 -G, --gff1: input 1 is GFF format, meaning start and end coordinates are 1-based, closed interval 11 -G, --gff1: input 1 is GFF format, meaning start and end coordinates are 1-based, closed interval
12 -H, --gff2: input 2 is GFF format, meaning start and end coordinates are 1-based, closed interval 12 -H, --gff2: input 2 is GFF format, meaning start and end coordinates are 1-based, closed interval
13 """ 13 """
14 import sys, traceback, fileinput 14 import fileinput
15 from warnings import warn 15 import sys
16 from bx.intervals import * 16 from bx.intervals.io import GenomicInterval, NiceReaderWrapper
17 from bx.intervals.io import * 17 from bx.intervals.operations.subtract import subtract
18 from bx.intervals.operations.subtract import *
19 from bx.cookbook import doc_optparse 18 from bx.cookbook import doc_optparse
20 from galaxy.tools.util.galaxyops import * 19 from bx.tabular.io import ParseError
20 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped
21 from utils.gff_util import GFFFeature, GFFReaderWrapper, convert_bed_coords_to_gff 21 from utils.gff_util import GFFFeature, GFFReaderWrapper, convert_bed_coords_to_gff
22 22
23 assert sys.version_info[:2] >= ( 2, 4 ) 23 assert sys.version_info[:2] >= ( 2, 4 )
24 24
25
25 def main(): 26 def main():
26 mincols = 1 27 mincols = 1
27 upstream_pad = 0
28 downstream_pad = 0
29 28
30 options, args = doc_optparse.parse( __doc__ ) 29 options, args = doc_optparse.parse( __doc__ )
31 try: 30 try:
32 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) 31 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
33 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 ) 32 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
34 if options.mincols: mincols = int( options.mincols ) 33 if options.mincols:
34 mincols = int( options.mincols )
35 pieces = bool( options.pieces ) 35 pieces = bool( options.pieces )
36 in1_gff_format = bool( options.gff1 ) 36 in1_gff_format = bool( options.gff1 )
37 in2_gff_format = bool( options.gff2 ) 37 in2_gff_format = bool( options.gff2 )
38 in_fname, in2_fname, out_fname = args 38 in_fname, in2_fname, out_fname = args
39 except: 39 except:
46 in1_reader_wrapper = NiceReaderWrapper 46 in1_reader_wrapper = NiceReaderWrapper
47 if in2_gff_format: 47 if in2_gff_format:
48 in2_reader_wrapper = GFFReaderWrapper 48 in2_reader_wrapper = GFFReaderWrapper
49 else: 49 else:
50 in2_reader_wrapper = NiceReaderWrapper 50 in2_reader_wrapper = NiceReaderWrapper
51 51
52 g1 = in1_reader_wrapper( fileinput.FileInput( in_fname ), 52 g1 = in1_reader_wrapper( fileinput.FileInput( in_fname ),
53 chrom_col=chr_col_1, 53 chrom_col=chr_col_1,
54 start_col=start_col_1, 54 start_col=start_col_1,
55 end_col=end_col_1, 55 end_col=end_col_1,
56 strand_col=strand_col_1, 56 strand_col=strand_col_1,
57 fix_strand=True ) 57 fix_strand=True )
58 if in1_gff_format: 58 if in1_gff_format:
59 # Subtract requires coordinates in BED format. 59 # Subtract requires coordinates in BED format.
60 g1.convert_to_bed_coord=True 60 g1.convert_to_bed_coord = True
61 61
62 g2 = in2_reader_wrapper( fileinput.FileInput( in2_fname ), 62 g2 = in2_reader_wrapper( fileinput.FileInput( in2_fname ),
63 chrom_col=chr_col_2, 63 chrom_col=chr_col_2,
64 start_col=start_col_2, 64 start_col=start_col_2,
65 end_col=end_col_2, 65 end_col=end_col_2,
66 strand_col=strand_col_2, 66 strand_col=strand_col_2,
67 fix_strand=True ) 67 fix_strand=True )
68 if in2_gff_format: 68 if in2_gff_format:
69 # Subtract requires coordinates in BED format. 69 # Subtract requires coordinates in BED format.
70 g2.convert_to_bed_coord=True 70 g2.convert_to_bed_coord = True
71 71
72 out_file = open( out_fname, "w" ) 72 out_file = open( out_fname, "w" )
73 try: 73 try:
74 for feature in subtract( [g1,g2], pieces=pieces, mincols=mincols ): 74 for feature in subtract( [g1, g2], pieces=pieces, mincols=mincols ):
75 if isinstance( feature, GFFFeature ): 75 if isinstance( feature, GFFFeature ):
76 # Convert back to GFF coordinates since reader converted automatically. 76 # Convert back to GFF coordinates since reader converted automatically.
77 convert_bed_coords_to_gff( feature ) 77 convert_bed_coords_to_gff( feature )
78 for interval in feature.intervals: 78 for interval in feature.intervals:
79 out_file.write( "%s\n" % "\t".join( interval.fields ) ) 79 out_file.write( "%s\n" % "\t".join( interval.fields ) )