Mercurial > repos > devteam > merge
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 |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 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 | 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 """ | |
|
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 | 20 |
| 21 assert sys.version_info[:2] >= ( 2, 4 ) | |
| 22 | |
|
3
b9c97d3233bb
planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents:
0
diff
changeset
|
23 |
| 0 | 24 def main(): |
| 25 mincols = 1 | |
| 26 | |
| 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
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 | 32 in_fname, out_fname = args |
| 33 except: | |
| 34 doc_optparse.exception() | |
| 35 | |
| 36 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ), | |
| 37 chrom_col=chr_col_1, | |
| 38 start_col=start_col_1, | |
| 39 end_col=end_col_1, | |
|
3
b9c97d3233bb
planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents:
0
diff
changeset
|
40 strand_col=strand_col_1, |
| 0 | 41 fix_strand=True ) |
| 42 | |
| 43 out_file = open( out_fname, "w" ) | |
| 44 | |
| 45 try: | |
|
3
b9c97d3233bb
planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
devteam
parents:
0
diff
changeset
|
46 for line in merge(g1, mincols=mincols): |
| 0 | 47 if options.threecol: |
| 48 if type( line ) is GenomicInterval: | |
| 49 out_file.write( "%s\t%s\t%s\n" % ( line.chrom, str( line.startCol ), str( line.endCol ) ) ) | |
| 50 elif type( line ) is list: | |
| 51 out_file.write( "%s\t%s\t%s\n" % ( line[chr_col_1], str( line[start_col_1] ), str( line[end_col_1] ) ) ) | |
| 52 else: | |
| 53 out_file.write( "%s\n" % line ) | |
| 54 else: | |
| 55 if type( line ) is GenomicInterval: | |
| 56 out_file.write( "%s\n" % "\t".join( line.fields ) ) | |
| 57 elif type( line ) is list: | |
| 58 out_file.write( "%s\n" % "\t".join( line ) ) | |
| 59 else: | |
| 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 | 62 out_file.close() |
| 63 fail( "Invalid file format: %s" % str( exc ) ) | |
| 64 | |
| 65 out_file.close() | |
| 66 | |
| 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 | 70 |
| 71 if __name__ == "__main__": | |
| 72 main() |
