annotate featureCounter.py @ 0:56dab41a4c5f draft default tip

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