Skip to content

Commit

Permalink
Fixed tribar plot
Browse files Browse the repository at this point in the history
  • Loading branch information
asntech committed Feb 16, 2017
1 parent 0b4b833 commit c5bdda4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
# built documents.
#
# The short X.Y version.
version = '0.41.0'
version = '0.5.0'
# The full version, including alpha/beta/rc tags.
release = 'v0.41.0'
release = 'v0.5.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 2 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ This will save the results in the current working directory with a folder named
" ","Default is ``frac``"
"--htype","{tribar,color,pie,circle,square,ellipse,number,shade}. Heatmap plot type. Default is ``pie``."
" ", "Read the below note for ``tribar`` option."
"--triangle","Show lower/upper triangle of the matrix as heatmap. Default is ``lower``"
"--names","Comma-separated list of names for input files. Default is base name of input files."
"--filenames","Use file names as labels instead. Default is ``False``."
"--sort","Set this only if your files are not sorted. Default is ``False``."
Expand All @@ -177,4 +178,5 @@ This will save the results in the current working directory with a folder named
"--dpi","Dots-per-inch (DPI) for the output. Default is: ``300``."
"--test","This will run the program on test data."

.. note:: The option ``--htype=tribar`` will generate a horizontal bar plot with an adjacent heatmap rotated 45 degrees to show the lower triangle of the matrix comparing all sets of bars.
2 changes: 1 addition & 1 deletion intervene/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.41.0'
__version__ = '0.5.0'
5 changes: 4 additions & 1 deletion intervene/intervene
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ def main():

pairwise_parser.add_argument('--htype', dest="htype",choices=("tribar","color", "pie","circle", "square", "ellipse", "number", "shade"),
default='pie',help='Heatmap plot type. '
'Default is "%(default)s".\n\n')
'Default is "%(default)s".\n\n')
pairwise_parser.add_argument('--triangle', dest="triangle",choices=("lower","upper"),
default='lower',help='Show lower/upper triangle of the matrix as heatmap. '
'Default is "%(default)s".\n\n')

pairwise_parser.add_argument('--names', dest='labels',
help='Comma-separated list of names for input files. \n'
Expand Down
52 changes: 32 additions & 20 deletions intervene/modules/pairwise/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def barplot(series, matrix, outfile, options, max_size=1):
#ax = fig.add_subplot(121, frame_on=False, aspect=2.0)

# Get the heatmap triangle's axes and the order of the clustered samples.
cax, order = heatmap_triangle(matrix, ax, options.hlabel)
cax, order = heatmap_triangle(matrix, ax, options)

# Adjust spacing between the heatmap triangle and the barplot.
#fig.subplots_adjust(wspace=-0.25, hspace=0, left=0, right=1)
Expand Down Expand Up @@ -159,7 +159,7 @@ def shorten(x, n=48):
return x[:n/2] + '..' + x[-n/2:]
return x

def heatmap_triangle(dataframe, axes, hlabel='Fraction of overlap'):
def heatmap_triangle(dataframe, axes, options):
"""Create a heatmap of the lower triangle of a pairwise correlation
matrix of all pairs of columns in the given dataframe. The heatmap
triangle is rotated 45 degrees clockwise and drawn on the given axes.
Expand All @@ -172,8 +172,8 @@ def heatmap_triangle(dataframe, axes, hlabel='Fraction of overlap'):
"""
N = dataframe.shape[1]
D = dataframe
tri_type = "lower"

tri_type = options.triangle

#D = dataframe.corr(method='pearson')

Expand All @@ -190,8 +190,8 @@ def heatmap_triangle(dataframe, axes, hlabel='Fraction of overlap'):

if tri_type == "upper":
# Get the upper triangle of the matrix.
D = np.transpose(D)
C = np.triu(D)
C = np.transpose(C)
elif tri_type == "full":
C = D
# Get the lower triangle of the matrix.
Expand All @@ -201,43 +201,55 @@ def heatmap_triangle(dataframe, axes, hlabel='Fraction of overlap'):
# Mask the upper triangle.
C = np.ma.masked_array(C, C == 0)

diagonal_val = 0
#diagonal_val = 1
# Set the diagonal to zero.
for i in range(N):
C[i, i] = diagonal_val
#for i in range(N):
# C[i, i] = diagonal_val

# Transformation matrix for rotating the heatmap.
Aa = np.array([(y, x) for x in range(N, -1, -1) for y in range(N + 1)])
A = np.array([(y, x) for x in range(N, -1, -1) for y in range(N + 1)])
t = np.array([[0.5, 1], [0.5, -1]])
A = np.dot(Aa, t)
A = np.dot(A, t)

min_val = np.round(np.amin(C), decimals=1)
max_val = np.round(np.amax(C), decimals=1)

#if min_val == 0:
# min_val = -1
# -1.0 correlation is blue, 0.0 is white, 1.0 is red.
# 1.0 correlation is blue, 0.0 is white, 1.0 is red.
cmap = pl.cm.RdBu_r
norm = mp.colors.BoundaryNorm(np.linspace(0, 1, 10), cmap.N)
norm = mp.colors.BoundaryNorm(np.linspace(min_val, max_val, 20), cmap.N)

# This MUST be before the call to pl.pcolormesh() to align properly.
axes.set_xticks([])
axes.set_yticks([])

# Plot the correlation heatmap triangle.
if tri_type != "full":
X = Aa[:, 1].reshape(N + 1, N + 1)
Y = Aa[:, 0].reshape(N + 1, N + 1)
if tri_type == "full":
X = A[:, 1].reshape(N + 1, N + 1)
Y = A[:, 1].reshape(N + 1, N + 1)
caxes = pl.pcolormesh(X, Y, np.flipud(C), axes=axes, cmap=cmap, norm=norm)
else:
X = A[:, 0].reshape(N + 1, N + 1)
Y = A[:, 1].reshape(N + 1, N + 1)
X = A[:, 1].reshape(N + 1, N + 1)
Y = A[:, 0].reshape(N + 1, N + 1)
caxes = pl.pcolormesh(X, Y, np.flipud(C), axes=axes, cmap=cmap, norm=norm)

# Remove the ticks and reset the x limit.
axes.set_xlim(right=1)

#axes.labelsize = "small"
axes.tick_params(labelsize=6)
if options.type == 'count':
ticks = np.linspace(min_val, max_val, 3)
elif options.type == 'reldist':
ticks = np.linspace(min_val, max_val, 5)
else:
ticks = np.linspace(min_val, max_val, 5)
# Add a colorbar below the heatmap triangle.
cb = pl.colorbar(caxes, ax=axes, orientation='horizontal', shrink=0.5825,
fraction=0.02, pad=0, ticks=np.linspace(-1, 1, 5),
fraction=0.02, pad=0, ticks=ticks,
use_gridspec=True)
cb.set_label(hlabel)
cb.set_label(options.hlabel)

return caxes, D.index

Expand Down Expand Up @@ -313,7 +325,7 @@ def pairwise_intersection(options):
series = pd.Series(bed_sizes, index=labels)
#Set heatmap label
if options.type == 'count':
options.hlabel = 'Number of overlap'
options.hlabel = 'Number of overlaps'
if options.type == 'frac':
options.hlabel = 'Fraction of overlap'
if options.type == 'jaccard':
Expand Down

0 comments on commit c5bdda4

Please sign in to comment.