Mercurial > repos > devteam > concat
annotate gops_concat.py @ 3:32e1c8dac438 draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
author | devteam |
---|---|
date | Thu, 22 Jun 2017 18:40:46 -0400 |
parents | d491589307e7 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 Concatenate two bed files. The concatenated files are returned in the | |
4 same format as the first. If --sameformat is specified, then all | |
5 columns will be treated as the same, and all fields will be saved, | |
6 although the output will be trimmed to match the primary input. In | |
7 addition, if --sameformat is specified, missing fields will be padded | |
8 with a period(.). | |
9 | |
10 usage: %prog in_file_1 in_file_2 out_file | |
11 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in first file | |
12 -2, --cols2=N,N,N,N: Columns for chrom, start, end, strand in second file | |
13 -s, --sameformat: All files are precisely the same format. | |
14 """ | |
3
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
15 from __future__ import print_function |
0 | 16 |
2
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
17 import fileinput |
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
18 import sys |
3
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
19 |
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
20 from bx.cookbook import doc_optparse |
2
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
21 from bx.intervals.io import GenomicInterval, NiceReaderWrapper |
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
22 from bx.intervals.operations.concat import concat |
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
23 from bx.tabular.io import ParseError |
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
24 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped |
0 | 25 |
26 assert sys.version_info[:2] >= ( 2, 4 ) | |
27 | |
2
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
28 |
0 | 29 def main(): |
2
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
30 sameformat = False |
0 | 31 |
32 options, args = doc_optparse.parse( __doc__ ) | |
33 try: | |
34 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) | |
35 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 ) | |
2
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
36 if options.sameformat: |
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
37 sameformat = True |
0 | 38 in_file_1, in_file_2, out_fname = args |
39 except: | |
40 doc_optparse.exception() | |
41 | |
42 g1 = NiceReaderWrapper( fileinput.FileInput( in_file_1 ), | |
43 chrom_col=chr_col_1, | |
44 start_col=start_col_1, | |
45 end_col=end_col_1, | |
46 strand_col=strand_col_1, | |
47 fix_strand=True ) | |
48 | |
49 g2 = NiceReaderWrapper( fileinput.FileInput( in_file_2 ), | |
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 line in concat( [g1, g2], sameformat=sameformat ): | |
60 if type( line ) is GenomicInterval: | |
61 out_file.write( "%s\n" % "\t".join( line.fields ) ) | |
62 else: | |
63 out_file.write( "%s\n" % line ) | |
3
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
64 except ParseError as exc: |
0 | 65 out_file.close() |
66 fail( "Invalid file format: %s" % str( exc ) ) | |
67 | |
68 out_file.close() | |
69 | |
70 if g1.skipped > 0: | |
3
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
71 print(skipped( g1, filedesc=" of 1st dataset" )) |
0 | 72 if g2.skipped > 0: |
3
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
73 print(skipped( g2, filedesc=" of 2nd dataset" )) |
32e1c8dac438
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
74 |
2
d491589307e7
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
75 |
0 | 76 if __name__ == "__main__": |
77 main() |