annotate gops_join.py @ 4:a10f49d9218a draft

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
author devteam
date Thu, 22 Jun 2017 18:52:36 -0400
parents ffbd1de29c28
children
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 """
4
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
11 from __future__ import print_function
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
12
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
13 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
14 import sys
4
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
15
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
16 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
17 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
18 from bx.intervals.operations.join import join
ffbd1de29c28 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents: 0
diff changeset
19 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
20 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
21
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
22 assert sys.version_info[:2] >= ( 2, 4 )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
23
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
24
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
25 def main():
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
26 mincols = 1
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
27 leftfill = False
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
28 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
29
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
30 options, args = doc_optparse.parse( __doc__ )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
31 try:
e56b47dce68a 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 )
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
33 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
34 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
35 mincols = int( options.mincols )
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
36 if options.fill:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
37 if options.fill == "both":
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
38 rightfill = leftfill = True
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
39 else:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
40 rightfill = options.fill == "right"
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
41 leftfill = options.fill == "left"
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
42 in_fname, in2_fname, out_fname = args
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
43 except:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
44 doc_optparse.exception()
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
45
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
46 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
47 chrom_col=chr_col_1,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
48 start_col=start_col_1,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
49 end_col=end_col_1,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
50 strand_col=strand_col_1,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
51 fix_strand=True )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
52 g2 = NiceReaderWrapper( fileinput.FileInput( in2_fname ),
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
53 chrom_col=chr_col_2,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
54 start_col=start_col_2,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
55 end_col=end_col_2,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
56 strand_col=strand_col_2,
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
57 fix_strand=True )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
58
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
59 out_file = open( out_fname, "w" )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
60
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
61 try:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
62 for outfields in join(g1, g2, mincols=mincols, rightfill=rightfill, leftfill=leftfill):
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
63 if type( outfields ) is list:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
64 out_file.write( "%s\n" % "\t".join( outfields ) )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
65 else:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
66 out_file.write( "%s\n" % outfields )
4
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
67 except ParseError as exc:
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
68 out_file.close()
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
69 fail( "Invalid file format: %s" % str( exc ) )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
70 except MemoryError:
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
71 out_file.close()
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
72 fail( "Input datasets were too large to complete the join operation." )
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
73
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
74 out_file.close()
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
75
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
76 if g1.skipped > 0:
4
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
77 print(skipped( g1, filedesc=" of 1st dataset" ))
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
78 if g2.skipped > 0:
4
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
79 print(skipped( g2, filedesc=" of 2nd dataset" ))
a10f49d9218a planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/join commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
80
0
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
81
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
82 if __name__ == "__main__":
e56b47dce68a Imported from capsule None
devteam
parents:
diff changeset
83 main()