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