Mercurial > repos > devteam > concat
annotate gops_concat.py @ 2:d491589307e7 draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit a1517c9d22029095120643bbe2c8fa53754dd2b7
author | devteam |
---|---|
date | Wed, 11 Nov 2015 12:47:51 -0500 |
parents | 8aa939ace6ba |
children | 32e1c8dac438 |
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 """ | |
15 | |
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
|
16 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
|
17 import sys |
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 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
|
19 from bx.intervals.operations.concat import concat |
0 | 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.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
|
22 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped |
0 | 23 |
24 assert sys.version_info[:2] >= ( 2, 4 ) | |
25 | |
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
|
26 |
0 | 27 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
|
28 sameformat = False |
0 | 29 |
30 options, args = doc_optparse.parse( __doc__ ) | |
31 try: | |
32 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) | |
33 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
|
34 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
|
35 sameformat = True |
0 | 36 in_file_1, in_file_2, out_fname = args |
37 except: | |
38 doc_optparse.exception() | |
39 | |
40 g1 = NiceReaderWrapper( fileinput.FileInput( in_file_1 ), | |
41 chrom_col=chr_col_1, | |
42 start_col=start_col_1, | |
43 end_col=end_col_1, | |
44 strand_col=strand_col_1, | |
45 fix_strand=True ) | |
46 | |
47 g2 = NiceReaderWrapper( fileinput.FileInput( in_file_2 ), | |
48 chrom_col=chr_col_2, | |
49 start_col=start_col_2, | |
50 end_col=end_col_2, | |
51 strand_col=strand_col_2, | |
52 fix_strand=True ) | |
53 | |
54 out_file = open( out_fname, "w" ) | |
55 | |
56 try: | |
57 for line in concat( [g1, g2], sameformat=sameformat ): | |
58 if type( line ) is GenomicInterval: | |
59 out_file.write( "%s\n" % "\t".join( line.fields ) ) | |
60 else: | |
61 out_file.write( "%s\n" % line ) | |
62 except ParseError, exc: | |
63 out_file.close() | |
64 fail( "Invalid file format: %s" % str( exc ) ) | |
65 | |
66 out_file.close() | |
67 | |
68 if g1.skipped > 0: | |
69 print skipped( g1, filedesc=" of 1st dataset" ) | |
70 if g2.skipped > 0: | |
71 print skipped( g2, filedesc=" of 2nd dataset" ) | |
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
|
72 |
0 | 73 if __name__ == "__main__": |
74 main() |