annotate gops_intersect.py @ 5:33b3f3688db4 draft

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