Skip to content

Commit

Permalink
fix problem occuring with bigwig files with frequent NaN values which…
Browse files Browse the repository at this point in the history
… resulted in only nan averages
  • Loading branch information
fidelram committed Jul 26, 2017
1 parent e92e001 commit 93cc1a3
Showing 1 changed file with 42 additions and 55 deletions.
97 changes: 42 additions & 55 deletions hicexplorer/trackPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,69 +670,56 @@ def plot(self, ax, label_ax, chrom_region, start_region, end_region):
"{}\n\nPlease check that the chromosome name is part of the bigwig file "
"and that the region is valid".format(formated_region, self.properties['file']))

if end_region - start_region < 2e6:
scores = self.bw.values(chrom_region, start_region, end_region)

if 'nans to zeros' in self.properties and self.properties['nans to zeros'] is True:
scores[np.isnan(scores)] = 0

lins = np.linspace(0, len(scores), num_bins).astype(int)
scores_per_bin = [np.mean(scores[lins[x]:lins[x + 1]]) for x in range(len(lins) - 1)]
_x = lins + start_region
x_values = [float(_x[x] + _x[x + 1]) / 2 for x in range(len(lins) - 1)]
if 'type' in self.properties and self.properties != 'fill':
if self.properties['type'].find(":") > 0:
plot_type, size = self.properties['type'].split(":")
try:
size = float(size)
except ValueError:
exit("Invalid value: 'type = {}' in section: {}\n"
"A number was expected and found '{}'".format(self.properties['type'],
self.properties['section_name'],
size))
else:
plot_type = self.properties['type']
size = None
# on rare occasions pyBigWig may throw an error, apparently caused by a corruption
# of the memory. This only occurs when calling trackPlot from different
# processors. Reloading the file solves the problem.
num_tries = 0
while num_tries < 5:
num_tries += 1
try:
scores_per_bin = np.array(self.bw.stats(chrom_region, start_region, end_region, nBins=num_bins)).astype(float)
except Exception as e:
import pyBigWig
self.bw = pyBigWig.open(self.properties['file'])

if plot_type == 'line':
self.ax.plot(x_values, scores_per_bin, '-', linewidth=size, color=self.properties['color'])
print "error found while reading bigwig scores ({}).\nTrying again. Iter num: {}".format(e, num_tries)
pass
else:
if num_tries > 1:
print "After {} the scores could be computed".format(num_tries)
break

elif plot_type == 'points':
self.ax.plot(x_values, scores_per_bin, '.', markersize=size, color=self.properties['color'])
x_values = np.linspace(start_region, end_region, num_bins)

else:
exit("Invalid: 'type = {}' in section: {}\n".format(self.properties['type'],
self.properties['section_name'],
size))
if 'type' in self.properties and self.properties != 'fill':
if self.properties['type'].find(":") > 0:
plot_type, size = self.properties['type'].split(":")
try:
size = float(size)
except ValueError:
exit("Invalid value: 'type = {}' in section: {}\n"
"A number was expected and found '{}'".format(self.properties['type'],
self.properties['section_name'],
size))
else:
self.ax.fill_between(x_values, scores_per_bin, linewidth=0.1,
color=self.properties['color'],
facecolor=self.properties['color'])
plot_type = self.properties['type']
size = None

else:
# on rare occasions pyBigWig may throw an error, apparently caused by a corruption
# of the memory. This only occurs when calling trackPlot from different
# processors. Reloading the file solves the problem.
num_tries = 0
while num_tries < 5:
num_tries += 1
try:
scores = np.array(self.bw.stats(chrom_region, start_region, end_region, nBins=num_bins)).astype(float)
except Exception as e:
import pyBigWig
self.bw = pyBigWig.open(self.properties['file'])
if plot_type == 'line':
self.ax.plot(x_values, scores_per_bin, '-', linewidth=size, color=self.properties['color'])

print "error found while reading bigwig scores ({}).\nTrying again. Iter num: {}".format(e, num_tries)
pass
else:
if num_tries > 1:
print "After {} the scores could be computed".format(num_tries)
break
elif plot_type == 'points':
self.ax.plot(x_values, scores_per_bin, '.', markersize=size, color=self.properties['color'])

x_values = np.linspace(start_region, end_region, num_bins)
self.ax.fill_between(x_values, scores, linewidth=0.1,
else:
exit("Invalid: 'type = {}' in section: {}\n".format(self.properties['type'],
self.properties['section_name'],
size))
else:
self.ax.fill_between(x_values, scores_per_bin, linewidth=0.1,
color=self.properties['color'],
facecolor=self.properties['color'], zorder=1)
facecolor=self.properties['color'])

self.ax.set_xlim(start_region, end_region)
ymin, ymax = self.ax.get_ylim()
if 'max_value' in self.properties and self.properties['max_value'] != 'auto':
Expand Down

0 comments on commit 93cc1a3

Please sign in to comment.