Mercurial > repos > xuebing > sharplabtool
comparison tools/new_operations/gops_intersect.py @ 0:9071e359b9a3
Uploaded
author | xuebing |
---|---|
date | Fri, 09 Mar 2012 19:37:19 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9071e359b9a3 |
---|---|
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 """ | |
14 from galaxy import eggs | |
15 import pkg_resources | |
16 pkg_resources.require( "bx-python" ) | |
17 import sys, traceback, fileinput | |
18 from warnings import warn | |
19 from bx.intervals import * | |
20 from bx.intervals.io import * | |
21 from bx.intervals.operations.intersect import * | |
22 from bx.cookbook import doc_optparse | |
23 from galaxy.tools.util.galaxyops import * | |
24 from galaxy.datatypes.util.gff_util import GFFFeature, GFFReaderWrapper, convert_bed_coords_to_gff | |
25 | |
26 assert sys.version_info[:2] >= ( 2, 4 ) | |
27 | |
28 def main(): | |
29 mincols = 1 | |
30 upstream_pad = 0 | |
31 downstream_pad = 0 | |
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 ) | |
36 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 ) | |
37 if options.mincols: mincols = int( options.mincols ) | |
38 pieces = bool( options.pieces ) | |
39 in1_gff_format = bool( options.gff1 ) | |
40 in2_gff_format = bool( options.gff2 ) | |
41 in_fname, in2_fname, out_fname = args | |
42 except: | |
43 doc_optparse.exception() | |
44 | |
45 # Set readers to handle either GFF or default format. | |
46 if in1_gff_format: | |
47 in1_reader_wrapper = GFFReaderWrapper | |
48 else: | |
49 in1_reader_wrapper = NiceReaderWrapper | |
50 if in2_gff_format: | |
51 in2_reader_wrapper = GFFReaderWrapper | |
52 else: | |
53 in2_reader_wrapper = NiceReaderWrapper | |
54 | |
55 g1 = in1_reader_wrapper( fileinput.FileInput( in_fname ), | |
56 chrom_col=chr_col_1, | |
57 start_col=start_col_1, | |
58 end_col=end_col_1, | |
59 strand_col=strand_col_1, | |
60 fix_strand=True ) | |
61 if in1_gff_format: | |
62 # Intersect requires coordinates in BED format. | |
63 g1.convert_to_bed_coord=True | |
64 g2 = in2_reader_wrapper( fileinput.FileInput( in2_fname ), | |
65 chrom_col=chr_col_2, | |
66 start_col=start_col_2, | |
67 end_col=end_col_2, | |
68 strand_col=strand_col_2, | |
69 fix_strand=True ) | |
70 if in2_gff_format: | |
71 # Intersect requires coordinates in BED format. | |
72 g2.convert_to_bed_coord=True | |
73 | |
74 out_file = open( out_fname, "w" ) | |
75 try: | |
76 for feature in intersect( [g1,g2], pieces=pieces, mincols=mincols ): | |
77 if isinstance( feature, GFFFeature ): | |
78 # Convert back to GFF coordinates since reader converted automatically. | |
79 convert_bed_coords_to_gff( feature ) | |
80 for interval in feature.intervals: | |
81 out_file.write( "%s\n" % "\t".join( interval.fields ) ) | |
82 elif isinstance( feature, GenomicInterval ): | |
83 out_file.write( "%s\n" % "\t".join( feature.fields ) ) | |
84 else: | |
85 out_file.write( "%s\n" % feature ) | |
86 except ParseError, e: | |
87 out_file.close() | |
88 fail( "Invalid file format: %s" % str( e ) ) | |
89 | |
90 out_file.close() | |
91 | |
92 if g1.skipped > 0: | |
93 print skipped( g1, filedesc=" of 1st dataset" ) | |
94 if g2.skipped > 0: | |
95 print skipped( g2, filedesc=" of 2nd dataset" ) | |
96 | |
97 if __name__ == "__main__": | |
98 main() |