annotate gops_concat.py @ 5:73ca13a7ec5f draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/concat commit d7b1a60c0aecc46b7f625c3e32f882562b512fd9
author devteam
date Mon, 13 Jun 2022 16:21:39 +0000
parents 32e1c8dac438
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
2 """
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
3 Concatenate two bed files. The concatenated files are returned in the
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
4 same format as the first. If --sameformat is specified, then all
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
5 columns will be treated as the same, and all fields will be saved,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
6 although the output will be trimmed to match the primary input. In
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
7 addition, if --sameformat is specified, missing fields will be padded
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
8 with a period(.).
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
9
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
10 usage: %prog in_file_1 in_file_2 out_file
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
11 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in first file
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
12 -2, --cols2=N,N,N,N: Columns for chrom, start, end, strand in second file
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
13 -s, --sameformat: All files are precisely the same format.
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
25
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
26 assert sys.version_info[:2] >= ( 2, 4 )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
31
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
32 options, args = doc_optparse.parse( __doc__ )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
33 try:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
34 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
38 in_file_1, in_file_2, out_fname = args
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
39 except:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
40 doc_optparse.exception()
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
41
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
42 g1 = NiceReaderWrapper( fileinput.FileInput( in_file_1 ),
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
43 chrom_col=chr_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
44 start_col=start_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
45 end_col=end_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
46 strand_col=strand_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
47 fix_strand=True )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
48
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
49 g2 = NiceReaderWrapper( fileinput.FileInput( in_file_2 ),
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
50 chrom_col=chr_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
51 start_col=start_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
52 end_col=end_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
53 strand_col=strand_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
54 fix_strand=True )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
55
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
56 out_file = open( out_fname, "w" )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
57
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
58 try:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
59 for line in concat( [g1, g2], sameformat=sameformat ):
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
60 if type( line ) is GenomicInterval:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
61 out_file.write( "%s\n" % "\t".join( line.fields ) )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
62 else:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
65 out_file.close()
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
66 fail( "Invalid file format: %s" % str( exc ) )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
67
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
68 out_file.close()
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
69
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
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
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
76 if __name__ == "__main__":
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
77 main()