annotate gops_concat.py @ 0:8aa939ace6ba

Imported from capsule None
author devteam
date Tue, 01 Apr 2014 10:53:46 -0400
parents
children d491589307e7
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 """
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
15
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
16 import sys, traceback, fileinput
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
17 from warnings import warn
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
18 from bx.intervals import *
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
19 from bx.intervals.io import *
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
20 from bx.intervals.operations.concat import *
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
21 from bx.cookbook import doc_optparse
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
22 from galaxy.tools.util.galaxyops import *
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
23
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
24 assert sys.version_info[:2] >= ( 2, 4 )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
25
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
26 def main():
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
27 sameformat=False
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
28 upstream_pad = 0
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
29 downstream_pad = 0
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
30
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
31 options, args = doc_optparse.parse( __doc__ )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
32 try:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
33 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
34 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
35 if options.sameformat: sameformat = True
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
36 in_file_1, in_file_2, out_fname = args
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
37 except:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
38 doc_optparse.exception()
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
39
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
40 g1 = NiceReaderWrapper( fileinput.FileInput( in_file_1 ),
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
41 chrom_col=chr_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
42 start_col=start_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
43 end_col=end_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
44 strand_col=strand_col_1,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
45 fix_strand=True )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
46
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
47 g2 = NiceReaderWrapper( fileinput.FileInput( in_file_2 ),
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
48 chrom_col=chr_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
49 start_col=start_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
50 end_col=end_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
51 strand_col=strand_col_2,
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
52 fix_strand=True )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
53
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
54 out_file = open( out_fname, "w" )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
55
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
56 try:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
57 for line in concat( [g1, g2], sameformat=sameformat ):
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
58 if type( line ) is GenomicInterval:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
59 out_file.write( "%s\n" % "\t".join( line.fields ) )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
60 else:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
61 out_file.write( "%s\n" % line )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
62 except ParseError, exc:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
63 out_file.close()
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
64 fail( "Invalid file format: %s" % str( exc ) )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
65
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
66 out_file.close()
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
67
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
68 if g1.skipped > 0:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
69 print skipped( g1, filedesc=" of 1st dataset" )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
70 if g2.skipped > 0:
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
71 print skipped( g2, filedesc=" of 2nd dataset" )
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
72
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
73 if __name__ == "__main__":
8aa939ace6ba Imported from capsule None
devteam
parents:
diff changeset
74 main()