annotate gops_concat.py @ 5:e24888154cb7 draft default tip

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