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