annotate tools/new_operations/gops_concat.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 Concatenate two bed files. The concatenated files are returned in the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 same format as the first. If --sameformat is specified, then all
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 columns will be treated as the same, and all fields will be saved,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 although the output will be trimmed to match the primary input. In
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 addition, if --sameformat is specified, missing fields will be padded
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 with a period(.).
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 usage: %prog in_file_1 in_file_2 out_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in first file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 -2, --cols2=N,N,N,N: Columns for chrom, start, end, strand in second file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 -s, --sameformat: All files are precisely the same format.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 import sys, traceback, fileinput
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 from warnings import warn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 from bx.intervals import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 from bx.intervals.io import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 from bx.intervals.operations.concat import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 from galaxy.tools.util.galaxyops import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 sameformat=False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 upstream_pad = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 downstream_pad = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 if options.sameformat: sameformat = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 in_file_1, in_file_2, out_fname = args
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 doc_optparse.exception()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 g1 = NiceReaderWrapper( fileinput.FileInput( in_file_1 ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 chrom_col=chr_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 start_col=start_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 end_col=end_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 g2 = NiceReaderWrapper( fileinput.FileInput( in_file_2 ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 chrom_col=chr_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 start_col=start_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 end_col=end_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 strand_col=strand_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if strand_col_1 >= 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 g1.strand_col = strand_col_1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 out_file = open( out_fname, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 for line in concat( [g1, g2], sameformat=sameformat ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 if type( line ) is GenomicInterval:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 out_file.write( "%s\n" % "\t".join( line.fields ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 out_file.write( "%s\n" % line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 except ParseError, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 fail( "Invalid file format: %s" % str( exc ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if g1.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 print skipped( g1, filedesc=" of 1st dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if g2.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 print skipped( g2, filedesc=" of 2nd dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 main()