Skip to content

Commit

Permalink
Allow separate colorbars for each row or subplot
Browse files Browse the repository at this point in the history
  • Loading branch information
Phlya committed Jun 3, 2019
1 parent 5cd387f commit 628f2db
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

A versatile tool to perform pile-up analysis on Hi-C data in .cool format (https://github.com/mirnylab/cooler). And who doesn't like cool pupppies?

.cool is a modern and flexible (and the best, in my opinion) format to store Hi-C data.
.cool is a modern and flexible (and the best, in my opinion) format to store Hi-C data.
It uses HDF5 to store sparse a representation of the Hi-C data, which allows low memory requirements when dealing with high resolution datasets. Another popular format to store Hi-C data, .hic, can be converted into .cool files using `hic2cool` (https://github.com/4dn-dcic/hic2cool).

See for details:
Expand Down Expand Up @@ -152,9 +152,11 @@ Currently, `coolpup.py` doesn't support inter-chromosomal pileups, but this is a
For flexible plotting, I suggest to use `matplotlib`. However simple plotting capabilities are included in this package. Just run `plotpup.py` with desired options and list all the output files of `coolpup.py` you'd like to plot.
```
Usage: plotpup.py [-h] [--cmap CMAP] [--symmetric SYMMETRIC] [--vmin VMIN]
[--vmax VMAX] [--scale {linear,log}] [--n_cols N_COLS]
[--vmax VMAX] [--scale {linear,log}]
[--cbar_mode {edge,each,single}] [--n_cols N_COLS]
[--col_names COL_NAMES] [--row_names ROW_NAMES]
[--enrichment ENRICHMENT] [--output OUTPUT]
[--norm_corners NORM_CORNERS] [--enrichment ENRICHMENT]
[--output OUTPUT]
[pileup_files [pileup_files ...]]
positional arguments:
Expand All @@ -172,18 +174,26 @@ optional arguments:
--vmax VMAX Value for the highest colour (default: None)
--scale {linear,log} Whether to use linear or log scaling for mapping
colours (default: log)
--cbar_mode {edge,each,single}
Whether to show a single colorbar, one per row or one
for each subplot (default: single)
--n_cols N_COLS How many columns to use for plotting the data. If 0,
automatically make the figure as square as possible
(default: 0)
--col_names COL_NAMES
A comma separated list of column names (default: None)
--row_names ROW_NAMES
A comma separated list of row names (default: None)
--norm_corners NORM_CORNERS
Whether to normalize pileups by their top left and
bottom right corners. 0 for no normalization, positive
number to define the size of the corner squares whose
values are averaged (default: 0)
--enrichment ENRICHMENT
Whether to show the level of enrichment in the central
pixels. 0 to not show, odd positive number to define
the size of the central square which values are
averaged. (default: 1)
the size of the central square whose values are
averaged (default: 1)
--output OUTPUT Where to save the plot (default: pup.pdf)
```

Expand Down
70 changes: 48 additions & 22 deletions coolpuppy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ def plotpuppy():
required=False, choices={"linear", "log"},
help="""Whether to use linear or log scaling for mapping
colours""")
parser.add_argument("--cbar_mode", type=str, default='single',
required=False, choices={"single", "edge", "each"},
help="""Whether to show a single colorbar, one per row
or one for each subplot""")
parser.add_argument("--n_cols", type=int, default=0,
required=False,
help="""How many columns to use for plotting the data.
Expand Down Expand Up @@ -482,14 +486,14 @@ def plotpuppy():
to be an odd number""")

f = plt.figure(dpi=300, figsize=(max(3.5, n_cols+0.5), max(3, n_rows)))
grid = ImageGrid(f, 111, share_all=False,# similar to subplot(111)
grid = ImageGrid(f, 111, share_all=True,# similar to subplot(111)
nrows_ncols=(n_rows, n_cols),
# direction='column',
axes_pad=0.05,
add_all=True,
label_mode="L",
cbar_location="right",
cbar_mode="single",
cbar_mode=args.cbar_mode,
cbar_size="5%",
cbar_pad="3%",
)
Expand All @@ -508,33 +512,55 @@ def plotpuppy():
else:
norm=Normalize

vmin, vmax = get_min_max(pups, args.vmin, args.vmax, sym=sym)

for i, j in product(range(n_rows), range(n_cols)):
n = i*n_cols+(j%n_cols)
if n<len(pups):
ax = axarr[i, j]
m = ax.imshow(pups[n],
norm=norm(vmax=vmax, vmin=vmin),
cmap=args.cmap)
ax.set_xticks([])
ax.set_yticks([])
if args.enrichment > 0:
enr = get_enrichment(pups[n], args.enrichment, 2)
ax.text(s=enr, y=0.95, x=0.05, ha='left', va='top',
size='x-small',
transform = ax.transAxes)
else:
axarr[i, j].axis('off')
if args.cbar_mode == 'single':
vmin, vmax = get_min_max(pups, args.vmin, args.vmax, sym=sym)
elif args.cbar_mode=='edge':
colorscales = [get_min_max(row, args.vmin, args.vmax, sym=sym) for row in pups]
elif args.cbar_mode=='each':
grid.cbar_axes = np.asarray(grid.cbar_axes).reshape((n_rows, n_cols))

n_grid = n_rows * n_cols
extra = [None for i in range(n_grid-len(pups))]
pups = np.asarray(pups+extra).reshape((n_rows, n_cols))


for i in range(n_rows):
if args.cbar_mode == 'edge':
vmin, vmax = colorscales[i]
for j in range(n_cols):
# n = i*n_cols+(j%n_cols)
if pups[i, j] is not None:
if args.cbar_mode== 'each':
vmin = np.nanmin(pups[i, j])
vmax = np.nanmax(pups[i, j])
ax = axarr[i, j]
m = ax.imshow(pups[i, j], interpolation='nearest',
norm=norm(vmax=vmax, vmin=vmin),
cmap=args.cmap,
extent=(0, 1, 0, 1))
ax.set_xticks([])
ax.set_yticks([])
if args.enrichment > 0:
enr = get_enrichment(pups[i, j], args.enrichment, 2)
ax.text(s=enr, y=0.95, x=0.05, ha='left', va='top',
size='x-small',
transform = ax.transAxes)
if args.cbar_mode == 'each':
cb = plt.colorbar(m, cax=grid.cbar_axes[i, j])
else:
axarr[i, j].axis('off')
grid.cbar_axes[i, j].axis('off')
if args.cbar_mode == 'edge':
cb = plt.colorbar(m, cax=grid.cbar_axes[i])

if args.col_names is not None:
for i, name in enumerate(args.col_names):
axarr[-1, i].set_xlabel(name)
if args.row_names is not None:
for i, name in enumerate(args.row_names):
axarr[i, 0].set_ylabel(name)

cb = plt.colorbar(m, cax=grid.cbar_axes[0])#, format=FormatStrFormatter('%.2f'))
if args.cbar_mode == 'single':
cb = plt.colorbar(m, cax=grid.cbar_axes[0])#, format=FormatStrFormatter('%.2f'))
# if sym:
# cb.ax.yaxis.set_ticks([vmin, 1, vmax])
plt.savefig(args.output, bbox_inches='tight')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='coolpuppy',
version='0.8.6',
version='0.8.7',
packages=['coolpuppy'],
entry_points={
'console_scripts': ['coolpup.py = coolpuppy.__main__:main',
Expand Down

0 comments on commit 628f2db

Please sign in to comment.