Mercurial > repos > devteam > subtract
annotate gops_subtract.py @ 5:0145969324c4 draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
author | devteam |
---|---|
date | Thu, 22 Jun 2017 18:53:03 -0400 |
parents | ecb36112b056 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 Find regions of first interval file that do not overlap regions in a second | |
4 interval file. 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 """ | |
5
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
14 from __future__ import print_function |
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
15 |
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
|
16 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
|
17 import sys |
5
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
18 |
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
19 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
|
20 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
|
21 from bx.intervals.operations.subtract import subtract |
ecb36112b056
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
22 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
|
23 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped |
5
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
24 |
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
25 from utils.gff_util import convert_bed_coords_to_gff, GFFFeature, GFFReaderWrapper |
0 | 26 |
27 assert sys.version_info[:2] >= ( 2, 4 ) | |
28 | |
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
|
29 |
0 | 30 def main(): |
31 mincols = 1 | |
32 | |
33 options, args = doc_optparse.parse( __doc__ ) | |
34 try: | |
35 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
|
36 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
|
37 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
|
38 mincols = int( options.mincols ) |
0 | 39 pieces = bool( options.pieces ) |
40 in1_gff_format = bool( options.gff1 ) | |
41 in2_gff_format = bool( options.gff2 ) | |
42 in_fname, in2_fname, out_fname = args | |
43 except: | |
44 doc_optparse.exception() | |
45 | |
46 # Set readers to handle either GFF or default format. | |
47 if in1_gff_format: | |
48 in1_reader_wrapper = GFFReaderWrapper | |
49 else: | |
50 in1_reader_wrapper = NiceReaderWrapper | |
51 if in2_gff_format: | |
52 in2_reader_wrapper = GFFReaderWrapper | |
53 else: | |
54 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
|
55 |
0 | 56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 fix_strand=True ) |
0 | 62 if in1_gff_format: |
63 # 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
|
64 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
|
65 |
0 | 66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 fix_strand=True ) |
0 | 72 if in2_gff_format: |
73 # 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
|
74 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
|
75 |
0 | 76 out_file = open( out_fname, "w" ) |
77 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
|
78 for feature in subtract( [g1, g2], pieces=pieces, mincols=mincols ): |
0 | 79 if isinstance( feature, GFFFeature ): |
80 # Convert back to GFF coordinates since reader converted automatically. | |
81 convert_bed_coords_to_gff( feature ) | |
82 for interval in feature.intervals: | |
83 out_file.write( "%s\n" % "\t".join( interval.fields ) ) | |
84 elif isinstance( feature, GenomicInterval ): | |
85 out_file.write( "%s\n" % "\t".join( feature.fields ) ) | |
86 else: | |
87 out_file.write( "%s\n" % feature ) | |
5
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
88 except ParseError as exc: |
0 | 89 out_file.close() |
90 fail( "Invalid file format: %s" % str( exc ) ) | |
91 | |
92 out_file.close() | |
93 | |
94 if g1.skipped > 0: | |
5
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
95 print(skipped( g1, filedesc=" of 2nd dataset" )) |
0 | 96 if g2.skipped > 0: |
5
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
97 print(skipped( g2, filedesc=" of 1st dataset" )) |
0145969324c4
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/subtract commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
98 |
0 | 99 |
100 if __name__ == "__main__": | |
101 main() |