annotate gops_join.py @ 0:e56b47dce68a

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