Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move legend to up and fix y-axis inward if using different ymax #1078

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions deeptools/heatmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,9 +1308,9 @@ def hmcluster(self, k, evaluate_silhouette=True, method='kmeans', clustering_sam
_clustered_matrix = []
cluster_number = 1
for cluster in cluster_order:
self.group_labels.append("cluster_{}".format(cluster_number))
cluster_number += 1
cluster_ids = _cluster_ids_list[cluster]
self.group_labels.append("cluster_{}_n{}".format(cluster_number,len(cluster_ids)))
cluster_number += 1
self.group_boundaries.append(self.group_boundaries[-1] +
len(cluster_ids))
_clustered_matrix.append(self.matrix[cluster_ids, :])
Expand Down
21 changes: 18 additions & 3 deletions deeptools/heatmapper_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

old_settings = np.seterr(all='ignore')

debug = 0
if debug:
from ipdb import set_trace

def plot_single(ax, ma, average_type, color, label, plot_type='lines'):
"""
Expand All @@ -18,7 +21,7 @@ def plot_single(ax, ma, average_type, color, label, plot_type='lines'):
----------
ax : matplotlib axis
matplotlib axis
ma : numpy array
ma : numpy array or list of numpy array(for plot with --repgrplist, take average between replicates )
numpy array The data on this matrix is summarized according
to the `average_type` argument.
average_type : str
Expand All @@ -32,7 +35,9 @@ def plot_single(ax, ma, average_type, color, label, plot_type='lines'):
type of plot. Either 'se' for standard error, 'std' for
standard deviation, 'overlapped_lines' to plot each line of the matrix,
fill to plot the area between the x axis and the value or any other string to
just plot the average line.
just plot the average line. When assign samples to replicates group such as
'--repgrplist WT WT KO KO' : 'std' would be the standard deviation between replicates groups.
'se' for standard error between replicates groups.

Returns
-------
Expand Down Expand Up @@ -63,7 +68,15 @@ def plot_single(ax, ma, average_type, color, label, plot_type='lines'):


"""
summary = np.ma.__getattribute__(average_type)(ma, axis=0)
if isinstance(ma,list):
summary_list = []
for ma_each in ma:
tmp = np.ma.__getattribute__(average_type)(ma_each, axis=0)
summary_list.append(tmp)
ma = np.array(summary_list)
summary = np.ma.__getattribute__("average")(ma, axis=0)
else:
summary = np.ma.__getattribute__(average_type)(ma, axis=0)
# only plot the average profiles without error regions
x = np.arange(len(summary))
if isinstance(color, np.ndarray):
Expand All @@ -72,6 +85,8 @@ def plot_single(ax, ma, average_type, color, label, plot_type='lines'):
if plot_type == 'fill':
ax.fill_between(x, summary, facecolor=color, alpha=0.6, edgecolor='none')

if debug:
set_trace()
if plot_type in ['se', 'std']:
if plot_type == 'se': # standard error
std = np.std(ma, axis=0) / np.sqrt(ma.shape[0])
Expand Down
8 changes: 8 additions & 0 deletions deeptools/parserCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,14 @@ def heatmapperOptionalArgs(mode=['heatmap', 'profile'][0]):
'group.',
action='store_true')

optional.add_argument('--repgrplist',
default=None,
nargs='+',
help='Group profiles by genotype'
'assign each profile to a group(as replicates) to plot average +/- se/std.'
'If the number of group values is smaller than'
'the number of samples, the values will be equally divide into groups.')

optional.add_argument('--plotFileFormat',
metavar='',
help='Image format type. If given, this '
Expand Down
37 changes: 29 additions & 8 deletions deeptools/plotHeatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from matplotlib.font_manager import FontProperties
import matplotlib.gridspec as gridspec
from matplotlib import ticker
import copy
import sys

import sys, re, os, copy
import plotly.offline as py
import plotly.graph_objs as go

Expand Down Expand Up @@ -117,6 +117,21 @@ def prepare_layout(hm_matrix, heatmapsize, showSummaryPlot, showColorbar, perGro

return grids

def autobreaklinetitle(title,sep="[-_,.:]",lmax=15):
outsep = "-"
sss = [ rr for rr in re.split(sep,title) if len(rr) ]
newtitle, tmp = "", ""
for ss in sss:
tmp0 = tmp
tmp += ss
if len(tmp) > lmax:
newtitle += tmp0.strip(outsep) + "\n"
tmp = ss
else:
tmp += outsep
newtitle += tmp.strip(outsep)
newtitle = "\n" + newtitle
return newtitle

def addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType, plot_type, yAxisLabel, color_list, yMin, yMax, wspace, hspace, colorbar_position, label_rotation=0.0):
"""
Expand Down Expand Up @@ -146,7 +161,7 @@ def addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType
else:
ax_profile = fig.add_subplot(grids[0, sample_id])

ax_profile.set_title(title)
ax_profile.set_title(autobreaklinetitle(title))
for group in range(iterNum2):
if perGroup:
sub_matrix = hm.matrix.get_matrix(sample_id, group)
Expand All @@ -163,6 +178,7 @@ def addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType
if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1:
plt.setp(ax_profile.get_yticklabels(), visible=False)

ax_profile.get_yaxis().set_tick_params(direction='in',pad=-22) # beisi
if sample_id == 0 and yAxisLabel != '':
ax_profile.set_ylabel(yAxisLabel)
xticks, xtickslabel = hm.getTicks(tickIdx)
Expand Down Expand Up @@ -595,8 +611,13 @@ def plotMatrix(hm, outFileName,
ax_list = addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType, plot_type, yAxisLabel, color_list, yMin, yMax, kwargs['wspace'], kwargs['hspace'], colorbar_position, label_rotation)

if legend_location != 'none':
ax_list[-1].legend(loc=legend_location.replace('-', ' '), ncol=1, prop=fontP,
frameon=False, markerscale=0.5)
ax = ax_list[-1] # beisi
box = ax.get_position()
ax.set_position([box.x0, box.y0 - box.height * 0.1, box.width, box.height * 0.9])
legend = ax.legend(loc='lower right', shadow=False, fontsize='x-large', bbox_to_anchor=(0, 1.3, 1, .22), ncol=10, frameon=False, prop=fontP) # beisi, legend line
ax.add_artist(legend)
# ax_list[-1].legend(loc=legend_location.replace('-', ' '), ncol=1, prop=fontP,
# frameon=False, markerscale=0.5)

first_group = 0 # helper variable to place the title per sample/group
for sample in range(hm.matrix.get_num_samples()):
Expand Down Expand Up @@ -632,7 +653,7 @@ def plotMatrix(hm, outFileName,

if group == first_group and not showSummaryPlot and not perGroup:
title = hm.matrix.sample_labels[sample]
ax.set_title(title)
ax.set_title(autobreaklinetitle(title))

if box_around_heatmaps is False:
# Turn off the boxes around the individual heatmaps
Expand Down Expand Up @@ -685,9 +706,9 @@ def plotMatrix(hm, outFileName,
ax.axes.set_xlabel(xAxisLabel)
ax.axes.set_yticks([])
if perGroup and group == 0:
ax.axes.set_ylabel(sub_matrix['sample'])
ax.axes.set_ylabel(sub_matrix['sample'],rotation=75,labelpad=0,fontsize=15)
elif not perGroup and sample == 0:
ax.axes.set_ylabel(sub_matrix['group'])
ax.axes.set_ylabel(sub_matrix['group'],rotation=75,labelpad=0,horizontalalignment='right',fontsize=15)

# Plot vertical lines at tick marks if desired
if linesAtTickMarks:
Expand Down
Loading