Analyze multi-modal single-cell data!
Project description
scran, in Python
Overview
The scranpy package provides Python bindings to the single-cell analysis methods in libscran and related C++ libraries. It performs the standard steps in a typical single-cell analysis including quality control, normalization, feature selection, dimensionality reduction, clustering and marker detection. scranpy makes heavy use of the BiocPy data structures in its user interface, while it uses the mattress package to provide a C++ representation of the underlying matrix data. This package is effectively a mirror of its counterparts in Javascript (scran.js) and R (scran.chan), which are based on the same underlying C++ libraries and concepts.
Quick start
Let's load in the famous PBMC 4k dataset from 10X Genomics (available here):
import singlecellexperiment
sce = singlecellexperiment.read_tenx_h5("pbmc4k-tenx.h5")
Then we just need to call one of scranpy's analyze()
functions.
(We do have to tell it what the mitochondrial genes are, though.)
import scranpy
options = scranpy.AnalyzeOptions()
options.per_cell_rna_qc_metrics_options.subsets = {
"mito": scranpy.guess_mito_from_symbols(sce.row_data["name"], "mt-")
}
results = scranpy.analyze_sce(sce, options=options)
This will perform all of the usual steps for a routine single-cell analysis, as described in Bioconductor's Orchestrating single cell analysis book. It returns an object containing clusters, t-SNEs, UMAPs, marker genes, and so on:
results.clusters
results.tsne
results.umap
results.rna_markers
We won't go over the theory here as it's explained more thoroughly in the book. Check out the reference documentation for more details.
Multiple batches
To demonstrate, let's grab two batches of PBMC datasets from 10X Genomics (again, available here):
import singlecellexperiment
sce3k = singlecellexperiment.read_tenx_h5("pbmc3k-tenx.h5")
sce4k = singlecellexperiment.read_tenx_h5("pbmc4k-tenx.h5")
They don't have the same features, so we'll just take the intersection of their Ensembl IDs before combining them:
import biocutils
common = biocutils.intersect(sce3k.row_data["id"], sce4k.row_data["id"])
sce3k_common = sce3k[biocutils.match(common, sce3k.row_data["id"]), :]
sce4k_common = sce4k[biocutils.match(common, sce4k.row_data["id"]), :]
import scipy.sparse
combined = scipy.sparse.hstack((sce3k_common.assay(0), sce4k_common.assay(0)))
batch = ["3k"] * sce3k_common.shape[1] + ["4k"] * sce4k_common.shape[1]
We can now perform a batch-aware analysis:
import scranpy
options = scranpy.AnalyzeOptions()
options.per_cell_rna_qc_metrics_options.subsets = {
"mito": scranpy.guess_mito_from_symbols(sce3k_common.row_data["name"], "mt-")
}
options.miscellaneous_options.block = batch
results = scranpy.analyze(combined, options=options)
This yields mostly the same set of results as before, but with an extra MNN-corrected embedding for clustering, visualization, etc.
results.mnn
Multiple modalities
Let's grab a 10X Genomics immune profiling dataset (see here):
import singlecellexperiment
sce = singlecellexperiment.read_tenx_h5("immune_3.0.0-tenx.h5")
We need to split it to genes and ADTs:
is_gene = [x == "Gene Expression" for x in sce.row_data["feature_type"]]
gene_data = sce[is_gene,:]
is_adt = [x == "Antibody Capture" for x in sce.row_data["feature_type"]]
adt_data = sce[is_adt,:]
And now we can run the analysis:
import scranpy
options = scranpy.AnalyzeOptions()
options.per_cell_rna_qc_metrics_options.subsets = {
"mito": scranpy.guess_mito_from_symbols(gene_data.row_data["name"], "mt-")
}
options.per_cell_adt_qc_metrics_options.subsets = {
"igg": [n.lower().startswith("igg") for n in adt_data.row_data["name"]]
}
results = scranpy.analyze_se(gene_data, adt_se = adt_data, options=options)
This returns ADT-specific results in the relevant fields, as well as a set of combined PCs for use in clustering, visualization, etc.
results.adt_size_factors
results.adt_markers
results.combined_pcs
Customizing the analysis
Most parameters can be changed by setting the relevant fields in the AnalyzeOptions
object.
For example, we can modify the number of neighbors and resolution used for graph-based clustering:
options.build_snn_graph_options.num_neighbors = 10
options.miscellaneous_options.snn_graph_multilevel_resolution = 2
Or we can fiddle the the various dimensionality reduction parameters:
options.run_pca_options.rank = 50
options.run_tsne_options.perplexity = 20
options.run_umap_options.min_dist = 0.5
The AnalyzeOptions
has a few convenience methods to easily set the same parameter across multiple *_options
attributes.
For example, to enable parallel processing in every step:
options.set_threads(5)
Advanced users can even obtain the sequence of steps used internally by analyze()
by calling it with dry_run = True
:
commands = scranpy.analyze(sce, dry_run = True)
print(commands)
## import scranpy
## import numpy
##
## results = AnalyzeResults()
## ...
Users can then add, remove or replace steps as desired.
Developer Notes
Steps to setup dependencies -
-
initialize git submodules in
extern/libscran
. -
run
cmake .
inside theextern/knncolle
to download the annoy library. a future version of this will use a cmake to setup the extern directory.
First one needs to build the extern library, this would generate a shared object file to src/scranpy/core-[*].so
python setup.py build_ext --inplace
For typical development workflows, run this for tests
python setup.py build_ext --inplace && tox
To rebuild the ctypes bindings cpptypes:
cpptypes src/scranpy/lib --py src/scranpy/_cpphelpers.py --cpp src/scranpy/lib/bindings.cpp --dll _core
To rebuild the dry run analysis source code:
./scripts/dryrun.py src/scranpy/analyze/live_analyze.py > src/scranpy/analyze/dry_analyze.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file scranpy-0.1.3.tar.gz
.
File metadata
- Download URL: scranpy-0.1.3.tar.gz
- Upload date:
- Size: 93.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2396ea3abbf503543262df4211becdff6a86d92fc403fda17564d6e6aa86ccff |
|
MD5 | 3c9fac672c9fcb4bdc092b8d5b5f7aec |
|
BLAKE2b-256 | 3ef76da092586f3f376d03ca9586f9f6d48c17765a72ad5e6797e2013134ae07 |
File details
Details for the file scranpy-0.1.3-cp311-cp311-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp311-cp311-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 40.1 MB
- Tags: CPython 3.11, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c931c5365aeeb09fac43d4ae8c1f094ae258afd428ce2e51938e2739a9a5e24 |
|
MD5 | 9dd7aead5334dc4294fe061299fb5125 |
|
BLAKE2b-256 | fc2bcb0350d32d5c82ce59f16733485b1f6d24cced56974e7ca5239f19f314f5 |
File details
Details for the file scranpy-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 39.4 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3abb06279e19a19861917cf6540e44f7321e339acc3d1074b72e6c36a4aeced |
|
MD5 | 4d386920a9514fe09cff280a63a96168 |
|
BLAKE2b-256 | 110e06c7cb21ebf92b9e1102a49a14186f582616e77eba197797734295af6905 |
File details
Details for the file scranpy-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 833.8 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab13d044e0d033c8a09eee4c76ac94339dc29b0335639f1c83bf487f7c9049c2 |
|
MD5 | 45a98da1c9ffd8ccd52d16b803331bb0 |
|
BLAKE2b-256 | ccc6b512e2fa00ae65e90e2dea4ded51b317521d509b386141ef87286721238e |
File details
Details for the file scranpy-0.1.3-cp311-cp311-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp311-cp311-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.11, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae3f55a13427d99b4c84eefc814446c25071bc043fc06e637ed07310059beae6 |
|
MD5 | 3718abe8fa3f2bc67268f9437c4f41be |
|
BLAKE2b-256 | 3f70bb588b978b631349cd1b182e257c61d64eef5031c065ad2a47990d53dcc3 |
File details
Details for the file scranpy-0.1.3-cp310-cp310-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp310-cp310-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 40.1 MB
- Tags: CPython 3.10, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9fd73ed3346ffd2ab0776a5965791fceb70baa2320a55d5c975adb27155efee |
|
MD5 | 81755a15cde94ce4170c2cbdac2645b7 |
|
BLAKE2b-256 | b785d844bff5ba855f403447eb110d8f38d4381d4acc91476506cf2456466e5f |
File details
Details for the file scranpy-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 39.4 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12244049c388678bb4823a4e330791ec5a543b3e9c1f508bdc0f6fc7fda00a1b |
|
MD5 | 463e448357548ecbad19044f61382123 |
|
BLAKE2b-256 | a56078dddb90cd4f97c4f8d78813ab9a7c8e19a85a3b3e1e9affa210d61b5a67 |
File details
Details for the file scranpy-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 833.8 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f008210a8295a288f22125c9107d3abc99f5c569e149513feb7896d4562fc4a |
|
MD5 | aeeeb4cf5e6a715864aa8c35578673d6 |
|
BLAKE2b-256 | c4fd82881b8433b6d900f287ac144d20fbf1e639fa387a3f8fc6c31eb3960791 |
File details
Details for the file scranpy-0.1.3-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2514340feb53e0474ac12ee901d567220b3a547d7ce23efe53f7fefd7a3505d |
|
MD5 | 9aa6f72081e4bde219d647875ca7f53b |
|
BLAKE2b-256 | 254139d983472105b5f80a44535425a0cac219a887f269f279dcad96aea8bedf |
File details
Details for the file scranpy-0.1.3-cp39-cp39-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp39-cp39-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 40.1 MB
- Tags: CPython 3.9, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fe2a9589cd65581da0d7288295514ce1549a481b31559fb2a1b4519605371d6 |
|
MD5 | 5e2a3296122913bc37862ccefe2033f9 |
|
BLAKE2b-256 | d48f20a2bc17221a5a27397d447ccefcd957a3b1e3609af86f73a469f0367dfe |
File details
Details for the file scranpy-0.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 39.4 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e7cb9726cff561f7905932360ba9cad604e883f23c5aa04cf30d9cde4c6e2eb |
|
MD5 | 756fa6daac5a9ff589d32b9656999804 |
|
BLAKE2b-256 | 68130a6447672ed0688c69d538305ee9b8f51dfa046c6df7327ea5f944a3c3eb |
File details
Details for the file scranpy-0.1.3-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 833.8 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a03e36bb159b3b79dfc3320509c6fb17dbdbc25985e2da265c3745172ac5b02 |
|
MD5 | ffa6e6beb89c75d738926639888e582d |
|
BLAKE2b-256 | 92d4b158d267067c1eae77ba8cc7fb0010664fb3693b00e04cc340acb4eedbb6 |
File details
Details for the file scranpy-0.1.3-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94a50c736c769f2f979caf6c1306ca70729066c43bec6926194cfa7ff1c78e0a |
|
MD5 | 78e40cf6b29319e871ad5aaf65c46e12 |
|
BLAKE2b-256 | 71832cc8756fe2e70f1dafd17f06b046d935eb736acd36925193e285e6bc177c |
File details
Details for the file scranpy-0.1.3-cp38-cp38-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp38-cp38-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 40.1 MB
- Tags: CPython 3.8, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b900dd1cc7b902c33d113074a7604939f5ca428e36547c31eb23144e2f954a5d |
|
MD5 | 1e6ba87f173fa260f77362e68164a9ef |
|
BLAKE2b-256 | 7f318063b13aad3c5b327cf2dea8ae12e942bb9036634727263debb7fdd88842 |
File details
Details for the file scranpy-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 39.4 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf1cd9a37ec7f1bbc5da553275cc018f01277108afedfb775450a12e10467fb0 |
|
MD5 | 0b049f4d59924ccab3726f109dbb625d |
|
BLAKE2b-256 | 4f33c95377dc20aa98e8f7feece2f1b380f5c433ef46542bbd8e9cac1d3ce383 |
File details
Details for the file scranpy-0.1.3-cp38-cp38-macosx_11_0_arm64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 833.8 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d7338b3bc0474abf6df37017589a6b63549971110b6e1d2c198a1d245d6531c |
|
MD5 | 5b137bb7cf39c61bec0e25aeece9b37e |
|
BLAKE2b-256 | 238df91801394d6a1df08c58e25df7de22f9709dc175a9e773212d0092270ba5 |
File details
Details for the file scranpy-0.1.3-cp38-cp38-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: scranpy-0.1.3-cp38-cp38-macosx_10_9_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.8, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5180d205789609fda43a8113661cb3e3f111512eab08109f322803befdc76a39 |
|
MD5 | 1ddf4d7eb3838a9c3c5dfb8de51487c6 |
|
BLAKE2b-256 | ef2be5925a30baca4e9cfb8bf19024c66f65422ce893897b4f6389a63c434b4c |