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