annotate gops_merge.py @ 5:dfbbc0291b36 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
author devteam
date Thu, 22 Jun 2017 18:51:08 -0400
parents b9c97d3233bb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
2 """
5
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
3 Merge overlapping regions.
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
4
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
5 usage: %prog in_file out_file
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
6 -1, --cols1=N,N,N,N: Columns for start, end, strand in first file
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
7 -m, --mincols=N: Require this much overlap (default 1bp)
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
8 -3, --threecol: Output 3 column bed
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
9 """
5
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
10 from __future__ import print_function
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
11
3
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
12 import fileinput
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
13 import sys
5
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
14
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
15 from bx.cookbook import doc_optparse
3
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
16 from bx.intervals.io import GenomicInterval, NiceReaderWrapper
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
17 from bx.intervals.operations.merge import merge
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
18 from bx.tabular.io import ParseError
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
19 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
20
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
21 assert sys.version_info[:2] >= ( 2, 4 )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
22
3
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
23
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
24 def main():
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
25 mincols = 1
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
26
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
27 options, args = doc_optparse.parse( __doc__ )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
28 try:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
29 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
3
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
30 if options.mincols:
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
31 mincols = int( options.mincols )
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
32 in_fname, out_fname = args
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
33 except:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
34 doc_optparse.exception()
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
35
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
36 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
37 chrom_col=chr_col_1,
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
38 start_col=start_col_1,
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
39 end_col=end_col_1,
3
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
40 strand_col=strand_col_1,
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
41 fix_strand=True )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
42
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
43 out_file = open( out_fname, "w" )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
44
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
45 try:
3
b9c97d3233bb planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents: 0
diff changeset
46 for line in merge(g1, mincols=mincols):
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
47 if options.threecol:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
48 if type( line ) is GenomicInterval:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
49 out_file.write( "%s\t%s\t%s\n" % ( line.chrom, str( line.startCol ), str( line.endCol ) ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
50 elif type( line ) is list:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
51 out_file.write( "%s\t%s\t%s\n" % ( line[chr_col_1], str( line[start_col_1] ), str( line[end_col_1] ) ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
52 else:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
53 out_file.write( "%s\n" % line )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
54 else:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
55 if type( line ) is GenomicInterval:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
56 out_file.write( "%s\n" % "\t".join( line.fields ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
57 elif type( line ) is list:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
58 out_file.write( "%s\n" % "\t".join( line ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
59 else:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
60 out_file.write( "%s\n" % line )
5
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
61 except ParseError as exc:
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
62 out_file.close()
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
63 fail( "Invalid file format: %s" % str( exc ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
64
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
65 out_file.close()
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
66
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
67 if g1.skipped > 0:
5
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
68 print(skipped( g1, filedesc=" of 1st dataset" ))
dfbbc0291b36 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/merge commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents: 3
diff changeset
69
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
70
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
71 if __name__ == "__main__":
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
72 main()