annotate featureCounter.py @ 0:ac6218e2b686 draft default tip

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