Skip to content

Commit

Permalink
Release/0.2.4.post1 (#50)
Browse files Browse the repository at this point in the history
* Feature/plot paga (#48)

* Make plot paga overlay paga on arbitrary cell embeddings

* Fix test

* Fix "--svd-solver=auto"

* Add "--edge-width-scale" (#49)

* Add "--edge-width-scale"

* Add test for "--edge-width-scale"

* Bump version
  • Loading branch information
nh3 authored Aug 29, 2019
1 parent 4a3f918 commit 96308e9
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 31 deletions.
10 changes: 5 additions & 5 deletions scanpy-scripts-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ setup() {
scale_opt="-m 10 --show-obj stdout"
scale_obj="${output_dir}/scale.h5ad"
pca_embed="${output_dir}/pca.tsv"
pca_opt="--n-comps 50 -V arpack --show-obj stdout -E ${pca_embed}"
pca_opt="--n-comps 50 -V auto --show-obj stdout -E ${pca_embed}"
pca_obj="${output_dir}/pca.h5ad"
neighbor_opt="-k 5,10,20 -n 25 -m umap --show-obj stdout"
neighbor_obj="${output_dir}/neighbor.h5ad"
Expand All @@ -46,13 +46,13 @@ setup() {
paga_opt="--use-graph neighbors_k10 --key-added k10_r0_7 --groups leiden_k10_r0_7 --model v1.2 -f loom"
paga_obj="${output_dir}/paga.h5ad"
diffmap_embed="${output_dir}/diffmap.tsv"
diffmap_opt="--use-graph neighbors_k10 --n-comps 10 -E ${diffmap_embed} -f loom"
diffmap_opt="--use-graph neighbors_k10 --n-comps 10 -E ${diffmap_embed}"
diffmap_obj="${output_dir}/diffmap.h5ad"
dpt_opt="--use-graph neighbors_k10 --key-added k10 --n-dcs 10 --root leiden_k10_r0_7 0"
dpt_obj="${output_dir}/dpt.h5ad"
plt_embed_opt="--color leiden_k10_r0_7 -f loom"
plt_embed_pdf="${output_dir}/umap_leiden_k10_r0_7.pdf"
plt_paga_opt="--use-key paga_k10_r0_7 --node-size-scale 2"
plt_paga_opt="--use-key paga_k10_r0_7 --node-size-scale 2 --edge-width-scale 0.5 --basis diffmap --color dpt_pseudotime_k10 --frameoff"
plt_paga_pdf="${output_dir}/paga_k10_r0_7.pdf"

if [ ! -d "$data_dir" ]; then
Expand Down Expand Up @@ -277,7 +277,7 @@ setup() {
skip "$diffmap_obj exists and resume is set to 'true'"
fi

run rm -f $diffmap_obj && $scanpy embed diffmap $diffmap_opt $leiden_obj $diffmap_obj
run rm -f $diffmap_obj && $scanpy embed diffmap $diffmap_opt $paga_obj $diffmap_obj

[ "$status" -eq 0 ]
[ -f "$diffmap_obj" ] && [ -f "$diffmap_embed" ]
Expand Down Expand Up @@ -316,7 +316,7 @@ setup() {
skip "$plt_paga_pdf exists and resume is set to 'true'"
fi

run rm -f $plt_paga_pdf && $scanpy plot paga $plt_paga_opt $paga_obj $plt_paga_pdf
run rm -f $plt_paga_pdf && $scanpy plot paga $plt_paga_opt $dpt_obj $plt_paga_pdf

[ "$status" -eq 0 ]
[ -f "$dpt_obj" ]
Expand Down
46 changes: 34 additions & 12 deletions scanpy_scripts/cmd_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,6 @@
PLOT_PAGA_CMD_OPTIONS = [
*COMMON_OPTIONS['input'],
*COMMON_OPTIONS['plot'],
COMMON_OPTIONS['random_state'],
click.option(
'--type', 'prefix',
type=click.Choice(['paga', 'paga_compare']),
default='paga',
show_default=True,
help='The type of visualisation.',
),
click.option(
'--use-key',
type=click.STRING,
Expand Down Expand Up @@ -997,12 +989,35 @@
help='Key for `.uns["paga"]` that specifies the matrix that stores the '
'edges to be drawn solid black.',
),
click.option(
'--basis',
type=click.STRING,
default=None,
show_default=True,
help='Name of the embedding to plot, must be a key of `.obsm` without '
'the prefix "X_".',
),
click.option(
'--color',
type=CommaSeparatedText(simplify=True),
type=click.STRING,
default=None,
show_default=True,
help='Keys for annotations of observations/cells or variables/genes.',
help='Key for annotation of observations/cells or variables/genes.',
),
click.option(
'--legend-loc',
type=click.Choice(['right margin', 'on data']),
default='right margin',
show_default=True,
help='Location of legend, either "on data", "right margin" or valid '
'keywords for `matplotlib.legend`.',
),
click.option(
'--size',
type=click.FLOAT,
default=None,
show_default=True,
help='Point size. Automatically computed if not specified.',
),
click.option(
'--node-size-scale',
Expand All @@ -1016,13 +1031,20 @@
type=click.INT,
default=None,
show_default=True,
help='Font size for node labels',
help='Font size for node labels.',
),
click.option(
'--edge-width-scale',
type=click.FLOAT,
default=1.0,
show_default=True,
help='Increase of decrease the width of the edges.',
),
click.option(
'--arrowsize',
type=click.INT,
default=30,
show_default=True,
help='For directed graphs, specify the length and width of the arrowhead',
help='For directed graphs, specify the length and width of the arrowhead.',
),
]
6 changes: 3 additions & 3 deletions scanpy_scripts/cmd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def write_embedding(adata, key, embed_fn, n_comp=None, sep='\t', key_added=None)
embed_fn, sep=sep, header=False, index=True)


def make_plot_function(FUN):
def make_plot_function(FUN, kind=None):
"""Make plot function that handles common plotting parameters
"""
def plot_function(
Expand All @@ -199,7 +199,7 @@ def plot_function(
if output_fig:
import os
import matplotlib.pyplot as plt
sc.settings.figdir = os.path.dirname(output_fig)
sc.settings.figdir = os.path.dirname(output_fig) or '.'

figname = os.path.basename(output_fig)
showfig = False
Expand All @@ -213,7 +213,7 @@ def plot_function(
**kwargs)

if output_fig:
prefix = kwargs.get('prefix', kwargs.get('basis', FUN.__name__))
prefix = kind if kind else kwargs.get('basis', FUN.__name__)
os.rename(
os.path.join(sc.settings.figdir, prefix + figname), output_fig)
plt.close()
Expand Down
2 changes: 1 addition & 1 deletion scanpy_scripts/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
PLOT_PAGA_CMD = make_subcmd(
'paga',
PLOT_PAGA_CMD_OPTIONS,
make_plot_function(plot_paga),
make_plot_function(plot_paga, kind='paga'),
cmd_desc='Plot PAGA trajectories.',
arg_desc=_IP_DESC,
)
2 changes: 1 addition & 1 deletion scanpy_scripts/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
from ._diffexp import diffexp, diffexp_paired, extract_de_table
from ._diffmap import diffmap
from ._dpt import dpt
from ._paga import paga
from ._paga import paga, plot_paga
from ..cmd_utils import _read_obj as read_obj
from ..cmd_utils import _write_obj as write_obj
69 changes: 61 additions & 8 deletions scanpy_scripts/lib/_paga.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
scanpy paga
"""

import numpy as np
import scanpy as sc
from ..cmd_utils import (
_backup_default_key,
Expand Down Expand Up @@ -39,17 +40,69 @@ def paga(

def plot_paga(
adata,
prefix='paga',
use_key='paga',
basis=None,
layout=None,
init_pos=None,
legend_loc='on data',
color=None,
size=None,
title=None,
show=None,
save=None,
**kwargs,
):
"""Make PAGA plot
"""
_set_default_key(adata.uns, 'paga', use_key)
if prefix == 'paga':
sc.pl.paga(adata, **kwargs)
elif prefix == 'paga_compare':
sc.pl.paga_compare(adata, **kwargs)
if basis is not None and f'X_{basis}' in adata.obsm.keys():
ax = sc.plotting._tools.scatterplots.plot_scatter(
adata,
basis=basis,
color=color,
legend_loc=legend_loc,
size=size,
title=None,
save=False,
show=False,
)

grouping = adata.uns[use_key]['groups']
categories = list(adata.obs[grouping].cat.categories)
obsm = adata.obsm[f'X_{basis}']
group_pos = np.zeros((len(categories), 2))
for i, label in enumerate(categories):
offset = 1 if basis.startswith('diffmap') else 0
_scatter = obsm[adata.obs[grouping] == label, (0+offset):(2+offset)]
x_pos, y_pos = np.median(_scatter, axis=0)
group_pos[i] = [x_pos, y_pos]

_set_default_key(adata.uns, 'paga', use_key)
kwargs['node_size_scale'] = 0
kwargs['fontsize'] = 1
kwargs['pos'] = group_pos
kwargs['color'] = None
try:
sc.pl.paga(
adata,
ax=ax,
title=title,
show=show,
save=save,
**kwargs,
)
finally:
_restore_default_key(adata.uns, 'paga', use_key)
else:
raise ValueError(f'{prefix}: unknown plot type for PAGA')
_restore_default_key(adata.uns, 'paga', use_key)
_set_default_key(adata.uns, 'paga', use_key)
try:
sc.pl.paga(
adata,
layout=layout,
init_pos=init_pos,
title=title,
show=show,
save=save,
**kwargs
)
finally:
_restore_default_key(adata.uns, 'paga', use_key)
4 changes: 4 additions & 0 deletions scanpy_scripts/lib/_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def pca(adata, key_added=None, export_embedding=None, **kwargs):
"""
Wrapper function for sc.pp.pca, for supporting named slot
"""
# omit "svd_solver" to let scanpy choose automatically
if 'svd_solver' in kwargs and kwargs['svd_solver'] == 'auto':
del kwargs['svd_solver']

if key_added:
if 'X_pca' in adata.obsm.keys():
adata.obsm['X_pca_bkup'] = adata.obsm['X_pca']
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='scanpy-scripts',
version='0.2.4',
version='0.2.4.post1',
author='nh3',
author_email='[email protected]',
description='Scripts for using scanpy from the command line',
Expand Down

0 comments on commit 96308e9

Please sign in to comment.