annotate tools/regVariation/featureCounter.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 #Guruprasad Ananda
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Calculate count and coverage of one query on another, and append the Coverage and counts to
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 the last four columns as bases covered, percent coverage, number of completely present features, number of partially present/overlapping features.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 usage: %prog bed_file_1 bed_file_2 out_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -1, --cols1=N,N,N,N: Columns for chr, start, end, strand in first file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -2, --cols2=N,N,N,N: Columns for chr, start, end, strand in second file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 import sys, traceback, fileinput
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 from warnings import warn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 from bx.intervals.io import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 from bx.intervals.operations import quicksect
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 from galaxy.tools.util.galaxyops import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 def counter(node, start, end):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 global full, partial
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 if node.start <= start and node.maxend > start:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 if node.end >= end or (node.start == start and end > node.end > start):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 full += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 elif end > node.end > start:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 partial += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 if node.left and node.left.maxend > start:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 counter(node.left, start, end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if node.right:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 counter(node.right, start, end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 elif start < node.start < end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 if node.end <= end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 full += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 partial += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 if node.left and node.left.maxend > start:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 counter(node.left, start, end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if node.right:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 counter(node.right, start, end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 if node.left:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 counter(node.left, start, end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 def count_coverage( readers, comments=True ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 primary = readers[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 secondary = readers[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 secondary_copy = readers[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 rightTree = quicksect.IntervalTree()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 for item in secondary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if type( item ) is GenomicInterval:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 rightTree.insert( item, secondary.linenum, item.fields )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 bitsets = secondary_copy.binned_bitsets()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 global full, partial
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 for interval in primary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 if type( interval ) is Header:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 yield interval
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if type( interval ) is Comment and comments:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 yield interval
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 elif type( interval ) == GenomicInterval:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 chrom = interval.chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 start = int(interval.start)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 end = int(interval.end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 full = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 partial = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if chrom not in bitsets:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 bases_covered = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 percent = 0.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 full = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 partial = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 bases_covered = bitsets[ chrom ].count_range( start, end-start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if (end - start) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 percent = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 percent = float(bases_covered) / float(end - start)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if bases_covered:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 root = rightTree.chroms[chrom] #root node for the chrom tree
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 counter(root, start, end)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 interval.fields.append(str(bases_covered))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 interval.fields.append(str(percent))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 interval.fields.append(str(full))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 interval.fields.append(str(partial))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 yield interval
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 in1_fname, in2_fname, out_fname = args
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 stop_err( "Data issue: click the pencil icon in the history item to correct the metadata attributes." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 g1 = NiceReaderWrapper( fileinput.FileInput( in1_fname ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 chrom_col=chr_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 start_col=start_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 end_col=end_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 strand_col=strand_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 g2 = NiceReaderWrapper( fileinput.FileInput( in2_fname ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 chrom_col=chr_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 start_col=start_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 end_col=end_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 strand_col=strand_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 g2_copy = NiceReaderWrapper( fileinput.FileInput( in2_fname ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 chrom_col=chr_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 start_col=start_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 end_col=end_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 strand_col=strand_col_2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 out_file = open( out_fname, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 for line in count_coverage([g1,g2,g2_copy]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 if type( line ) is GenomicInterval:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 out_file.write( "%s\n" % "\t".join( line.fields ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 out_file.write( "%s\n" % line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 except ParseError, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 fail( str( exc ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 if g1.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 print skipped( g1, filedesc=" of 1st dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 if g2.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 print skipped( g2, filedesc=" of 2nd dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 elif g2_copy.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 print skipped( g2_copy, filedesc=" of 2nd dataset" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 main()