annotate tools/indels/indel_table.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
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 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Combines several interval files containing indels with counts. All input files need to have the same number of columns.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 usage: %prog [options] [input3 sum3[ input4 sum4[ input5 sum5[...]]]]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -1, --input1=1: The first input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -s, --sum1=s: Whether or not to include the totals from first file in overall total
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -2, --input2=2: The second input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -S, --sum2=S: Whether or not to include the totals from second file in overall total
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -o, --output=o: The interval output file for the combined files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 import re, sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 sys.stderr.write( '%s\n' % msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 def numeric_sort( text1, text2 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 For two items containing space-separated text, compares equivalent pieces
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 numerically if both numeric or as text otherwise
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 pieces1 = text1.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 pieces2 = text2.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if len( pieces1 ) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if len( pieces2 ) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 return -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 for i, pc1 in enumerate( pieces1 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if i == len( pieces2 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if not pieces2[i].isdigit():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 if pc1.isdigit():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 return -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if pc1 > pieces2[i]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 elif pc1 < pieces2[i]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 return -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if not pc1.isdigit():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if int( pc1 ) > int( pieces2[i] ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 return 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 elif int( pc1 ) < int( pieces2[i] ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 return -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if i < len( pieces2 ) - 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 return -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 return 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 # Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 inputs = [ options.input1, options.input2 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 includes = [ options.sum1, options.sum2 ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 inputs.extend( [ a for i, a in enumerate( args ) if i % 2 == 0 ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 includes.extend( [ a for i, a in enumerate( args ) if i % 2 == 1 ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 num_cols = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 counts = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 # read in data from all files and get total counts
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 for i, input in enumerate( inputs ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 for line in open( input, 'rb' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 sp_line = line.strip().split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 # set num_cols on first pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 if num_cols == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if len( sp_line ) < 4:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 raise Exception, 'There need to be at least 4 columns in the file: Chrom, Start, End, and Count'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 num_cols = len( sp_line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 # deal with differing number of columns
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 elif len( sp_line ) != num_cols:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 raise Exception, 'All of the files need to have the same number of columns (current %s != %s of first line)' % ( len( sp_line ), num_cols )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 # get actual counts for each indel
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 indel = '\t'.join( sp_line[:-1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 count = int( sp_line[-1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 except ValueError, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 raise Exception, 'The last column of each file must be numeric, with the count of the number of instances of that indel: %s' % str( e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 # total across all included files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if includes[i] == "true":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 counts[ indel ]['tot'] += count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 except ( IndexError, KeyError ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 counts[ indel ] = { 'tot': count }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 # counts for ith file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 counts[ indel ][i] = count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 stop_err( 'Failed to read all input files:\n%s' % str( e ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 # output combined results to table file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 output = open( options.output, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 count_keys = counts.keys()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 count_keys.sort( numeric_sort )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 for indel in count_keys:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 count_out = [ str( counts[ indel ][ 'tot' ] ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 for i in range( len( inputs ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 count_out.append( str( counts[ indel ][i] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 except KeyError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 count_out.append( '0' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 output.write( '%s\t%s\n' % ( indel, '\t'.join( count_out ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 output.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 stop_err( 'Failed to output data: %s' % str( e ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 if __name__=="__main__": __main__()