Skip to content

Commit

Permalink
Implement #709, plotCorrelation scatterplot with X and Y axis ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpryan79 committed May 15, 2018
1 parent b3d3c3b commit 04004ab
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
46 changes: 29 additions & 17 deletions deeptools/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def plot_correlation(self, plot_filename, plot_title='', vmax=None,
fig.savefig(plot_filename, format=image_format)
plt.close()

def plotly_scatter(self, plot_filename, corr_matrix, plot_title='', minVal=None, maxVal=None):
def plotly_scatter(self, plot_filename, corr_matrix, plot_title='', minXVal=None, maxXVal=None, minYVal=None, maxYVal=None):
"""Make the scatter plot of a matrix with plotly"""
n = self.matrix.shape[1]
self.matrix = self.matrix
Expand All @@ -393,13 +393,13 @@ def plotly_scatter(self, plot_filename, corr_matrix, plot_title='', minVal=None,
domain = [base, base + domainWidth]
if x > 0:
base = 1 - base
fig['layout']['xaxis{}'.format(x + 1)] = dict(domain=domain, range=[minVal, maxVal], anchor='free', position=base)
fig['layout']['xaxis{}'.format(x + 1)] = dict(domain=domain, range=[minXVal, maxXVal], anchor='free', position=base)
for y in range(0, n):
yanchor = 'y{}'.format(y + 1)
if x == 1:
base = 1 - y * domainWidth
domain = [base - domainWidth, base]
fig['layout']['yaxis{}'.format(y + 1)] = dict(domain=domain, range=[minVal, maxVal], side='right', anchor='free', position=1.0)
fig['layout']['yaxis{}'.format(y + 1)] = dict(domain=domain, range=[minYVal, maxYVal], side='right', anchor='free', position=1.0)

if x > y:
vector1 = self.matrix[:, x]
Expand All @@ -425,7 +425,7 @@ def plotly_scatter(self, plot_filename, corr_matrix, plot_title='', minVal=None,

offline.plot(fig, filename=plot_filename, auto_open=False)

def plot_scatter(self, plot_filename, plot_title='', image_format=None, log1p=False, maxRange=None):
def plot_scatter(self, plot_filename, plot_title='', image_format=None, log1p=False, xRange=None, yRange=None):
"""
Plot the scatter plots of a matrix
in which each row is a sample
Expand All @@ -440,18 +440,28 @@ def plot_scatter(self, plot_filename, plot_title='', image_format=None, log1p=Fa
plt.suptitle(plot_title)
if log1p is True:
self.matrix = np.log1p(self.matrix)
min_value = self.matrix.min()
max_value = self.matrix.max()
if maxRange is not None:
max_value = maxRange
if (min_value % 2 == 0 and max_value % 2 == 0) or \
(min_value % 1 == 0 and max_value % 2 == 1):
min_xvalue = self.matrix.min()
max_xvalue = self.matrix.max()
min_yvalue = min_xvalue
max_yvalue = max_xvalue
if xRange is not None:
min_xvalue = xRange[0]
max_xvalue = xRange[1]
if yRange is not None:
min_yvalue = yRange[0]
max_yvalue = yRange[1]
if (min_xvalue % 2 == 0 and max_xvalue % 2 == 0) or \
(min_xvalue % 1 == 0 and max_xvalue % 2 == 1):
# make one value odd and the other even
max_value += 1
max_xvalue += 1
if (min_yvalue % 2 == 0 and max_yvalue % 2 == 0) or \
(min_yvalue % 1 == 0 and max_yvalue % 2 == 1):
# make one value odd and the other even
max_yvalue += 1

# plotly output
if image_format == 'plotly':
self.plotly_scatter(plot_filename, corr_matrix, plot_title=plot_title, minVal=min_value, maxVal=max_value)
self.plotly_scatter(plot_filename, corr_matrix, plot_title=plot_title, minXVal=min_xvalue, maxXVal=max_xvalue, minYVal=min_yvalue, maxYVal=max_yvalue)
return

rows, cols = np.triu_indices(num_samples)
Expand Down Expand Up @@ -518,12 +528,14 @@ def plot_scatter(self, plot_filename, plot_title='', image_format=None, log1p=Fa

ax.hist2d(vector1, vector2, bins=200, cmin=0.1)

if maxRange is not None:
ax.set_xlim(min_value, min(maxRange, ax.get_xlim()[1]))
ax.set_ylim(min_value, min(maxRange, ax.get_ylim()[1]))
if xRange is not None:
ax.set_xlim(xRange)
else:
ax.set_xlim(min_xvalue, ax.get_xlim()[1])
if yRange is not None:
ax.set_ylim(min_yvalue, min(yRange, ax.get_ylim()[1]))
else:
ax.set_xlim(min_value, ax.get_xlim()[1])
ax.set_ylim(min_value, ax.get_ylim()[1])
ax.set_ylim(min_yvalue, ax.get_ylim()[1])

plt.savefig(plot_filename, format=image_format)
plt.close()
Expand Down
14 changes: 11 additions & 3 deletions deeptools/plotCorrelation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ def scatterplot_options():
parser = argparse.ArgumentParser(add_help=False)
scatter_opts = parser.add_argument_group('Scatter plot options')

scatter_opts.add_argument('--maxRange',
help='Maximum (integer) value for the X and Y axes. The default scales these such that the full range of dots is displayed. If you specify --log1p, please ensure that this value is within the range it will produce.',
scatter_opts.add_argument('--xRange',
help='The X axis range. The default scales these such that the full range of dots is displayed.',
type=int,
nargs=2,
default=None)

scatter_opts.add_argument('--yRange',
help='The Y axis range. The default scales these such that the full range of dots is displayed.',
type=int,
nargs=2,
default=None)

scatter_opts.add_argument('--log1p',
Expand Down Expand Up @@ -229,7 +236,8 @@ def main(args=None):
corr.plot_scatter(args.plotFile,
plot_title=args.plotTitle,
image_format=args.plotFileFormat,
maxRange=args.maxRange,
xRange=args.xRange,
yRange=args.yRange,
log1p=args.log1p)
else:
corr.plot_correlation(args.plotFile,
Expand Down
16 changes: 11 additions & 5 deletions galaxy/wrapper/plotCorrelation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
--plotHeight $plotting_type.plotHeight
#else:
--plotTitle '$plotting_type.plotTitle'
#if str($plotting_type.maxRange) != '':
--maxRange '$plotting_type.maxRange'
#if str($plotting_type.xRange) != '':
--xRange '$plotting_type.xRange'
#end if
#if str($plotting_type.yRange) != '':
--yRange '$plotting_type.yRange'
#end if
$plotting_type.log1p
#end if
Expand Down Expand Up @@ -47,9 +50,12 @@
</when>
<when value="scatterplot">
<expand macro="plotTitle" />
<param argument="--maxRange" type="integer" value="" optional="true"
label="Maximum Range"
help="Maximum (integer) value for the X and Y axes. The default scales these such that the full range of dots is displayed. If you specify --log1p, please ensure that this value is within the range it will produce." />
<param argument="--xRange" value="" optional="true"
label="X axis range"
help="X axis range, the default scales these such that the full range of dots is displayed. The input should be two integers separated by a space." />
<param argument="--yRange" value="" optional="true"
label="Y axis range"
help="Y axis range, the default scales these such that the full range of dots is displayed. The input should be two integers separated by a space." />
<param argument="--log1p" type="boolean" truevalue="--log1p" falsevalue=""
label="Log transform"
help="Plot the natural log of the scatter plot after adding 1. Note that this is ONLY for plotting, the correlation is unaffected." />
Expand Down

0 comments on commit 04004ab

Please sign in to comment.