annotate gops_intersect.py @ 2:9bf4f3968e07

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