comparison tools/new_operations/gops_subtract.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 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 """
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.subtract 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 # Subtract requires coordinates in BED format.
63 g1.convert_to_bed_coord=True
64
65 g2 = in2_reader_wrapper( fileinput.FileInput( in2_fname ),
66 chrom_col=chr_col_2,
67 start_col=start_col_2,
68 end_col=end_col_2,
69 strand_col=strand_col_2,
70 fix_strand=True )
71 if in2_gff_format:
72 # Subtract requires coordinates in BED format.
73 g2.convert_to_bed_coord=True
74
75 out_file = open( out_fname, "w" )
76 try:
77 for feature in subtract( [g1,g2], pieces=pieces, mincols=mincols ):
78 if isinstance( feature, GFFFeature ):
79 # Convert back to GFF coordinates since reader converted automatically.
80 convert_bed_coords_to_gff( feature )
81 for interval in feature.intervals:
82 out_file.write( "%s\n" % "\t".join( interval.fields ) )
83 elif isinstance( feature, GenomicInterval ):
84 out_file.write( "%s\n" % "\t".join( feature.fields ) )
85 else:
86 out_file.write( "%s\n" % feature )
87 except ParseError, exc:
88 out_file.close()
89 fail( "Invalid file format: %s" % str( exc ) )
90
91 out_file.close()
92
93 if g1.skipped > 0:
94 print skipped( g1, filedesc=" of 2nd dataset" )
95 if g2.skipped > 0:
96 print skipped( g2, filedesc=" of 1st dataset" )
97
98 if __name__ == "__main__":
99 main()