annotate tools/annotation_profiler/annotation_profiler_for_interval.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 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 #For a set of intervals, this tool returns the same set of intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 #with 2 additional fields: the name of a Table/Feature and the number of
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 #bases covered. The original intervals are repeated for each Table/Feature.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys, struct, optparse, os, random
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import bx.intervals.io
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 import bx.bitset
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 import psyco
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 psyco.full()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 class CachedRangesInFile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 DEFAULT_STRUCT_FORMAT = '<I'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 def __init__( self, filename, profiler_info ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 self.file_size = os.stat( filename ).st_size
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 self.file = open( filename, 'rb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 self.filename = filename
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 self.fmt = profiler_info.get( 'profiler_struct_format', self.DEFAULT_STRUCT_FORMAT )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 self.fmt_size = int( profiler_info.get( 'profiler_struct_size', struct.calcsize( self.fmt ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 self.length = int( self.file_size / self.fmt_size / 2 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 self._cached_ranges = [ None for i in xrange( self.length ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 def __getitem__( self, i ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if self._cached_ranges[i] is not None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 return self._cached_ranges[i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if i < 0: i = self.length + i
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 offset = i * self.fmt_size * 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 self.file.seek( offset )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 start = struct.unpack( self.fmt, self.file.read( self.fmt_size ) )[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 end = struct.unpack( self.fmt, self.file.read( self.fmt_size ) )[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 raise IndexError, e
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 self._cached_ranges[i] = ( start, end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 return start, end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 def __len__( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 return self.length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 class RegionCoverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 def __init__( self, filename_base, profiler_info ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 self._coverage = CachedRangesInFile( "%s.covered" % filename_base, profiler_info )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 #print "Error loading coverage file %s: %s" % ( "%s.covered" % filename_base, e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 self._coverage = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 self._total_coverage = int( open( "%s.total_coverage" % filename_base ).read() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 #print "Error loading total coverage file %s: %s" % ( "%s.total_coverage" % filename_base, e )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 self._total_coverage = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 def get_start_index( self, start ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 #binary search: returns index of range closest to start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 if start > self._coverage[-1][1]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 return len( self._coverage ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 j = len( self._coverage) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 while i < j:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 k = ( i + j ) / 2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 if start <= self._coverage[k][1]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 j = k
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 i = k + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 return i
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 def get_coverage( self, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 return self.get_coverage_regions_overlap( start, end )[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 def get_coverage_regions_overlap( self, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 return self.get_coverage_regions_index_overlap( start, end )[0:2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 def get_coverage_regions_index_overlap( self, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if len( self._coverage ) < 1 or start > self._coverage[-1][1] or end < self._coverage[0][0]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 return 0, 0, 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if self._total_coverage and start <= self._coverage[0][0] and end >= self._coverage[-1][1]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 return self._total_coverage, len( self._coverage ), 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 coverage = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 region_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 start_index = self.get_start_index( start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 for i in xrange( start_index, len( self._coverage ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 c_start, c_end = self._coverage[i]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 if c_start > end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if c_start <= end and c_end >= start:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 coverage += min( end, c_end ) - max( start, c_start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 region_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 return coverage, region_count, start_index
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 class CachedCoverageReader:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 def __init__( self, base_file_path, buffer = 10, table_names = None, profiler_info = None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 self._base_file_path = base_file_path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 self._buffer = buffer #number of chromosomes to keep in memory at a time
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 self._coverage = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if table_names is None: table_names = [ table_dir for table_dir in os.listdir( self._base_file_path ) if os.path.isdir( os.path.join( self._base_file_path, table_dir ) ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 for tablename in table_names: self._coverage[tablename] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 if profiler_info is None: profiler_info = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 self._profiler_info = profiler_info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 def iter_table_coverage_by_region( self, chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 for tablename, coverage, regions in self.iter_table_coverage_regions_by_region( chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 yield tablename, coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 def iter_table_coverage_regions_by_region( self, chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 for tablename, coverage, regions, index in self.iter_table_coverage_regions_index_by_region( chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 yield tablename, coverage, regions
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 def iter_table_coverage_regions_index_by_region( self, chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 for tablename, chromosomes in self._coverage.iteritems():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 if chrom not in chromosomes:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 if len( chromosomes ) >= self._buffer:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 #randomly remove one chromosome from this table
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 del chromosomes[ chromosomes.keys().pop( random.randint( 0, self._buffer - 1 ) ) ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 chromosomes[chrom] = RegionCoverage( os.path.join ( self._base_file_path, tablename, chrom ), self._profiler_info )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 coverage, regions, index = chromosomes[chrom].get_coverage_regions_index_overlap( start, end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 yield tablename, coverage, regions, index
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 class TableCoverageSummary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 def __init__( self, coverage_reader, chrom_lengths ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 self.coverage_reader = coverage_reader
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 self.chrom_lengths = chrom_lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 self.chromosome_coverage = {} #dict of bitset by chromosome holding user's collapsed input intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 self.total_interval_size = 0 #total size of user's input intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 self.total_interval_count = 0 #total number of user's input intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 self.table_coverage = {} #dict of total coverage by user's input intervals by table
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 self.table_chromosome_size = {} #dict of dict of table:chrom containing total coverage of table for a chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 self.table_chromosome_count = {} #dict of dict of table:chrom containing total number of coverage ranges of table for a chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 self.table_regions_overlaped_count = {} #total number of table regions overlaping user's input intervals (non unique)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 self.interval_table_overlap_count = {} #total number of user input intervals which overlap table
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 self.region_size_errors = {} #dictionary of lists of invalid ranges by chromosome
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 def add_region( self, chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 chrom_length = self.chrom_lengths.get( chrom )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 region_start = min( start, chrom_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 region_end = min( end, chrom_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 region_length = region_end - region_start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 if region_length < 1 or region_start != start or region_end != end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 if chrom not in self.region_size_errors:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 self.region_size_errors[chrom] = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 self.region_size_errors[chrom].append( ( start, end ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 if region_length < 1: return
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 self.total_interval_size += region_length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 self.total_interval_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 if chrom not in self.chromosome_coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 self.chromosome_coverage[chrom] = bx.bitset.BitSet( chrom_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 self.chromosome_coverage[chrom].set_range( region_start, region_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 for table_name, coverage, regions in self.coverage_reader.iter_table_coverage_regions_by_region( chrom, region_start, region_end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 if table_name not in self.table_coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 self.table_coverage[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 self.table_chromosome_size[table_name] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 self.table_regions_overlaped_count[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 self.interval_table_overlap_count[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 self.table_chromosome_count[table_name] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 if chrom not in self.table_chromosome_size[table_name]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 self.table_chromosome_size[table_name][chrom] = self.coverage_reader._coverage[table_name][chrom]._total_coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 self.table_chromosome_count[table_name][chrom] = len( self.coverage_reader._coverage[table_name][chrom]._coverage )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 self.table_coverage[table_name] += coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 if coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 self.interval_table_overlap_count[table_name] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 self.table_regions_overlaped_count[table_name] += regions
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 def iter_table_coverage( self ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 def get_nr_coverage():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 #returns non-redundant coverage, where user's input intervals have been collapse to resolve overlaps
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 table_coverage = {} #dictionary of tables containing number of table bases overlaped by nr intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 interval_table_overlap_count = {} #dictionary of tables containing number of nr intervals overlaping table
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 table_regions_overlap_count = {} #dictionary of tables containing number of regions overlaped (unique)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 interval_count = 0 #total number of nr intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 interval_size = 0 #holds total size of nr intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 region_start_end = {} #holds absolute start,end for each user input chromosome
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 for chrom, chromosome_bitset in self.chromosome_coverage.iteritems():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 #loop through user's collapsed input intervals
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 end = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 last_end_index = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 interval_size += chromosome_bitset.count_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 if end >= chromosome_bitset.size: break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 start = chromosome_bitset.next_set( end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 if start >= chromosome_bitset.size: break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 end = chromosome_bitset.next_clear( start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 interval_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 if chrom not in region_start_end:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 region_start_end[chrom] = [start, end]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 region_start_end[chrom][1] = end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 for table_name, coverage, region_count, start_index in self.coverage_reader.iter_table_coverage_regions_index_by_region( chrom, start, end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 if table_name not in table_coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 table_coverage[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 interval_table_overlap_count[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 table_regions_overlap_count[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 table_coverage[table_name] += coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 if coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 interval_table_overlap_count[table_name] += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 table_regions_overlap_count[table_name] += region_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 if table_name in last_end_index and last_end_index[table_name] == start_index:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 table_regions_overlap_count[table_name] -= 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 last_end_index[table_name] = start_index + region_count - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 table_region_coverage = {} #total coverage for tables by bounding nr interval region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 table_region_count = {} #total number for tables by bounding nr interval region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 for chrom, start_end in region_start_end.items():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 for table_name, coverage, region_count in self.coverage_reader.iter_table_coverage_regions_by_region( chrom, start_end[0], start_end[1] ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 if table_name not in table_region_coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 table_region_coverage[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 table_region_count[table_name] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 table_region_coverage[table_name] += coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 table_region_count[table_name] += region_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 return table_region_coverage, table_region_count, interval_count, interval_size, table_coverage, table_regions_overlap_count, interval_table_overlap_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 table_region_coverage, table_region_count, nr_interval_count, nr_interval_size, nr_table_coverage, nr_table_regions_overlap_count, nr_interval_table_overlap_count = get_nr_coverage()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 for table_name in self.table_coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 #TODO: determine a type of statistic, then calculate and report here
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 yield table_name, sum( self.table_chromosome_size.get( table_name, {} ).values() ), sum( self.table_chromosome_count.get( table_name, {} ).values() ), table_region_coverage.get( table_name, 0 ), table_region_count.get( table_name, 0 ), self.total_interval_count, self.total_interval_size, self.table_coverage[table_name], self.table_regions_overlaped_count.get( table_name, 0), self.interval_table_overlap_count.get( table_name, 0 ), nr_interval_count, nr_interval_size, nr_table_coverage[table_name], nr_table_regions_overlap_count.get( table_name, 0 ), nr_interval_table_overlap_count.get( table_name, 0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 def profile_per_interval( interval_filename, chrom_col, start_col, end_col, out_filename, keep_empty, coverage_reader ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 out = open( out_filename, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 for region in bx.intervals.io.NiceReaderWrapper( open( interval_filename, 'rb' ), chrom_col = chrom_col, start_col = start_col, end_col = end_col, fix_strand = True, return_header = False, return_comments = False ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 for table_name, coverage, region_count in coverage_reader.iter_table_coverage_regions_by_region( region.chrom, region.start, region.end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 if keep_empty or coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 #only output regions that have atleast 1 base covered unless empty are requested
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 out.write( "%s\t%s\t%s\t%s\n" % ( "\t".join( region.fields ), table_name, coverage, region_count ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 def profile_summary( interval_filename, chrom_col, start_col, end_col, out_filename, keep_empty, coverage_reader, chrom_lengths ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 out = open( out_filename, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 table_coverage_summary = TableCoverageSummary( coverage_reader, chrom_lengths )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 for region in bx.intervals.io.NiceReaderWrapper( open( interval_filename, 'rb' ), chrom_col = chrom_col, start_col = start_col, end_col = end_col, fix_strand = True, return_header = False, return_comments = False ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 table_coverage_summary.add_region( region.chrom, region.start, region.end )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 out.write( "#tableName\ttableChromosomeCoverage\ttableChromosomeCount\ttableRegionCoverage\ttableRegionCount\tallIntervalCount\tallIntervalSize\tallCoverage\tallTableRegionsOverlaped\tallIntervalsOverlapingTable\tnrIntervalCount\tnrIntervalSize\tnrCoverage\tnrTableRegionsOverlaped\tnrIntervalsOverlapingTable\n" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 for table_name, table_chromosome_size, table_chromosome_count, table_region_coverage, table_region_count, total_interval_count, total_interval_size, total_coverage, table_regions_overlaped_count, interval_region_overlap_count, nr_interval_count, nr_interval_size, nr_coverage, nr_table_regions_overlaped_count, nr_interval_table_overlap_count in table_coverage_summary.iter_table_coverage():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 if keep_empty or total_coverage:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 #only output tables that have atleast 1 base covered unless empty are requested
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 out.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( table_name, table_chromosome_size, table_chromosome_count, table_region_coverage, table_region_count, total_interval_count, total_interval_size, total_coverage, table_regions_overlaped_count, interval_region_overlap_count, nr_interval_count, nr_interval_size, nr_coverage, nr_table_regions_overlaped_count, nr_interval_table_overlap_count ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 #report chrom size errors as needed:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 if table_coverage_summary.region_size_errors:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 print "Regions provided extended beyond known chromosome lengths, and have been truncated as necessary, for the following intervals:"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 for chrom, regions in table_coverage_summary.region_size_errors.items():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 if len( regions ) > 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 extra_region_info = ", ... "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 extra_region_info = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 print "%s has max length of %s, exceeded by %s%s." % ( chrom, chrom_lengths.get( chrom ), ", ".join( map( str, regions[:3] ) ), extra_region_info )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 class ChromosomeLengths:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 def __init__( self, profiler_info ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 self.chroms = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 self.default_bitset_size = int( profiler_info.get( 'bitset_size', bx.bitset.MAX ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 chroms = profiler_info.get( 'chromosomes', None )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 if chroms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 for chrom in chroms.split( ',' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 for fields in chrom.rsplit( '=', 1 ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 if len( fields ) == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 self.chroms[ fields[0] ] = int( fields[1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 self.chroms[ fields[0] ] = self.default_bitset_size
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 def get( self, name ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 return self.chroms.get( name, self.default_bitset_size )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 def parse_profiler_info( filename ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 profiler_info = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 for line in open( filename ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 fields = line.rstrip( '\n\r' ).split( '\t', 1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 if len( fields ) == 2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 if fields[0] in profiler_info:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 if not isinstance( profiler_info[ fields[0] ], list ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 profiler_info[ fields[0] ] = [ profiler_info[ fields[0] ] ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 profiler_info[ fields[0] ].append( fields[1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 profiler_info[ fields[0] ] = fields[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 pass #likely missing file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 return profiler_info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 parser = optparse.OptionParser()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 '-k','--keep_empty',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 action="store_true",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 dest='keep_empty',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 help='Keep tables with 0 coverage'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286 '-b','--buffer',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287 dest='buffer',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 type='int',default=10,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289 help='Number of Chromosomes to keep buffered'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292 '-c','--chrom_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293 dest='chrom_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294 type='int',default=1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295 help='Chromosome column'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298 '-s','--start_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299 dest='start_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 type='int',default=2,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301 help='Start Column'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304 '-e','--end_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 dest='end_col',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306 type='int',default=3,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 help='End Column'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310 '-p','--path',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 dest='path',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312 type='str',default='/galaxy/data/annotation_profiler/hg18',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 help='Path to profiled data for this organism'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316 '-t','--table_names',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 dest='table_names',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 type='str',default='None',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 help='Table names requested'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
321 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
322 '-i','--input',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
323 dest='interval_filename',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
324 type='str',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
325 help='Input Interval File'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
326 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
327 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
328 '-o','--output',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
329 dest='out_filename',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
330 type='str',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
331 help='Input Interval File'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
332 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
333 parser.add_option(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
334 '-S','--summary',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
335 action="store_true",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
336 dest='summary',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
337 default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
338 help='Display Summary Results'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
339 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
340
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
341 options, args = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
342
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
343 assert os.path.isdir( options.path ), IOError( "Configuration error: Table directory is missing (%s)" % options.path )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
344
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
345 #get profiler_info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
346 profiler_info = parse_profiler_info( os.path.join( options.path, 'profiler_info.txt' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
347
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
348 table_names = options.table_names.split( "," )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
349 if table_names == ['None']: table_names = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
350 coverage_reader = CachedCoverageReader( options.path, buffer = options.buffer, table_names = table_names, profiler_info = profiler_info )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
351
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
352 if options.summary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
353 profile_summary( options.interval_filename, options.chrom_col - 1, options.start_col - 1, options.end_col -1, options.out_filename, options.keep_empty, coverage_reader, ChromosomeLengths( profiler_info ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
354 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
355 profile_per_interval( options.interval_filename, options.chrom_col - 1, options.start_col - 1, options.end_col -1, options.out_filename, options.keep_empty, coverage_reader )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
356
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
357 #print out data version info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
358 print 'Data version (%s:%s:%s)' % ( profiler_info.get( 'dbkey', 'unknown' ), profiler_info.get( 'profiler_hash', 'unknown' ), profiler_info.get( 'dump_time', 'unknown' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
359
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
360 if __name__ == "__main__": __main__()