annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
2 """
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
3 Find regions of first interval file that do not overlap regions in a second
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
4 interval file. Interval files can either be BED or GFF format.
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
5
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
6 usage: %prog interval_file_1 interval_file_2 out_file
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
7 -1, --cols1=N,N,N,N: Columns for start, end, strand in first file
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
8 -2, --cols2=N,N,N,N: Columns for start, end, strand in second file
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
9 -m, --mincols=N: Require this much overlap (default 1bp)
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
10 -p, --pieces: just print pieces of second set (after padding)
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
11 -G, --gff1: input 1 is GFF format, meaning start and end coordinates are 1-based, closed interval
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
12 -H, --gff2: input 2 is GFF format, meaning start and end coordinates are 1-based, closed interval
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
13 """
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
14 import fileinput
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
15 import sys
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
16 from bx.intervals.io import GenomicInterval, NiceReaderWrapper
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
17 from bx.intervals.operations.subtract import subtract
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
18 from bx.cookbook import doc_optparse
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
19 from bx.tabular.io import ParseError
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
20 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
21 from utils.gff_util import GFFFeature, GFFReaderWrapper, convert_bed_coords_to_gff
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
22
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
23 assert sys.version_info[:2] >= ( 2, 4 )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
24
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
25
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
26 def main():
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
27 mincols = 1
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
28
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
29 options, args = doc_optparse.parse( __doc__ )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
30 try:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
31 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
32 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
33 if options.mincols:
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
34 mincols = int( options.mincols )
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
35 pieces = bool( options.pieces )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
36 in1_gff_format = bool( options.gff1 )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
37 in2_gff_format = bool( options.gff2 )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
38 in_fname, in2_fname, out_fname = args
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
39 except:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
40 doc_optparse.exception()
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
41
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
42 # Set readers to handle either GFF or default format.
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
43 if in1_gff_format:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
44 in1_reader_wrapper = GFFReaderWrapper
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
45 else:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
46 in1_reader_wrapper = NiceReaderWrapper
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
47 if in2_gff_format:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
48 in2_reader_wrapper = GFFReaderWrapper
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
49 else:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
50 in2_reader_wrapper = NiceReaderWrapper
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
51
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
52 g1 = in1_reader_wrapper( fileinput.FileInput( in_fname ),
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
53 chrom_col=chr_col_1,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
54 start_col=start_col_1,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
55 end_col=end_col_1,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
56 strand_col=strand_col_1,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
57 fix_strand=True )
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
58 if in1_gff_format:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
59 # Subtract requires coordinates in BED format.
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
60 g1.convert_to_bed_coord = True
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
61
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
62 g2 = in2_reader_wrapper( fileinput.FileInput( in2_fname ),
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
63 chrom_col=chr_col_2,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
64 start_col=start_col_2,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
65 end_col=end_col_2,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
66 strand_col=strand_col_2,
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
67 fix_strand=True )
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
68 if in2_gff_format:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
69 # Subtract requires coordinates in BED format.
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
70 g2.convert_to_bed_coord = True
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
71
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
72 out_file = open( out_fname, "w" )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
73 try:
3
ecb36112b056 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
74 for feature in subtract( [g1, g2], pieces=pieces, mincols=mincols ):
0
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
75 if isinstance( feature, GFFFeature ):
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
76 # Convert back to GFF coordinates since reader converted automatically.
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
77 convert_bed_coords_to_gff( feature )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
78 for interval in feature.intervals:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
79 out_file.write( "%s\n" % "\t".join( interval.fields ) )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
80 elif isinstance( feature, GenomicInterval ):
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
81 out_file.write( "%s\n" % "\t".join( feature.fields ) )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
82 else:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
83 out_file.write( "%s\n" % feature )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
84 except ParseError, exc:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
85 out_file.close()
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
86 fail( "Invalid file format: %s" % str( exc ) )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
87
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
88 out_file.close()
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
89
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
90 if g1.skipped > 0:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
91 print skipped( g1, filedesc=" of 2nd dataset" )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
92 if g2.skipped > 0:
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
93 print skipped( g2, filedesc=" of 1st dataset" )
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
94
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
95 if __name__ == "__main__":
5bc2dacbe729 Imported from capsule None
devteam
parents:
diff changeset
96 main()