annotate tools/new_operations/gops_join.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 Join two sets of intervals using their overlap as the key.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 usage: %prog bed_file_1 bed_file_2 out_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 -1, --cols1=N,N,N,N: Columns for start, end, strand in first file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -2, --cols2=N,N,N,N: Columns for start, end, strand in second file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -m, --mincols=N: Require this much overlap (default 1bp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -f, --fill=N: none, right, left, both
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 import sys, traceback, fileinput
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 from warnings import warn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 from bx.intervals import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 from bx.intervals.io import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 from bx.intervals.operations.join import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 from galaxy.tools.util.galaxyops import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 mincols = 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 upstream_pad = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 downstream_pad = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 leftfill = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 rightfill = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 if options.mincols: mincols = int( options.mincols )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if options.fill:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 if options.fill == "both":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 rightfill = leftfill = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 rightfill = options.fill == "right"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 leftfill = options.fill == "left"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 in_fname, in2_fname, out_fname = args
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 doc_optparse.exception()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 chrom_col=chr_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 start_col=start_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 end_col=end_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 strand_col=strand_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 g2 = NiceReaderWrapper( fileinput.FileInput( in2_fname ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 chrom_col=chr_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 start_col=start_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 end_col=end_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 strand_col=strand_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 out_file = open( out_fname, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 for outfields in join(g1, g2, mincols=mincols, rightfill=rightfill, leftfill=leftfill):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if type( outfields ) is list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 out_file.write( "%s\n" % "\t".join( outfields ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 out_file.write( "%s\n" % outfields )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 except ParseError, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 fail( "Invalid file format: %s" % str( exc ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 except MemoryError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 fail( "Input datasets were too large to complete the join operation." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if g1.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 print skipped( g1, filedesc=" of 1st dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if g2.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 print skipped( g2, filedesc=" of 2nd dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 main()