Cell typing (PySingleCellNet)¶
Classify each cell by type with a trained PySingleCellNet classifier, or train one from a dataset whose cells you have already labeled. Use it to annotate a new dataset against a reference atlas, or to check how well a query matches the cell types a reference contains.
Where to find it
Cell Typing button in the toolbar. The dialog has two tabs: Classify and Train.
How it works¶
PySingleCellNet (Tan and Cahan 2019) represents each cell by top-scoring gene pairs: for a pair of genes A and B, it asks whether A is expressed above B in that cell (Geman et al. 2004). A random forest votes over those pair features and returns, for every cell, a score per cell type.
Because each question compares two genes within one cell, the answer does not change when a cell's values are rescaled. Raw counts, CPM and log-normalized values give the same pair features. Per-gene scaling is different: centering each gene on its own mean reorders genes within a cell, which is exactly what the pair transform reads. That is why the dialog warns about z-scored input on Classify and refuses it on Train.
Install the optional dependency¶
PySingleCellNet is not in the default environment because it pulls in a large extra stack. Install the pyscn environment and start the backend from it:
Without it, the dialog shows install instructions and Classify cells and Train classifier stay disabled. You can still pick and inspect a classifier against your data, since inspection needs only scikit-learn. That lets you check gene coverage before you commit to the install.
Classify¶
- Open Cell Typing and stay on the Classify tab.
- Under Classifier file (.pkl), paste the path to a pickled classifier and press Inspect (or Enter), or click Browse to pick one. Browse is the same file browser as File → Load, filtered to
.pkland.picklefiles, and it remembers the folder you last used. Picking a file with Browse inspects it at once. - Read the two panels that appear before you run anything:
- Classifier: its cell types (with the classifier's own colors), the number of gene pairs, the genes they use and the number of trees.
- Gene coverage: how many of the classifier's genes your dataset has (see below).
- Pick the Expression source (
.Xor a layer). The detected scale is shown beside it. - Set the Result prefix (default
SCN). Every output is named after it, so a second run under another prefix keeps both results. - Leave Also call each cell Singular / Ambiguous / None / Rand on (the default) to get a call-quality column, and set its threshold quantile (default 0.05) if needed.
- Click Classify cells. The job runs in the background with a progress bar and can be cancelled. Every cell in the dataset is classified.
Gene coverage¶
PySingleCellNet fills any classifier gene it cannot find in the query with zeros. A badly matched query therefore still returns confident-looking scores rather than an error, because every missing gene corrupts every pair it takes part in. Skopia reports coverage before the run so you can see this:
| Band | Share of classifier genes present | Meaning |
|---|---|---|
| ok | 95% or more | Results can be trusted on this count. |
| warning | 60% to 95% | Results are degraded; read them with care. |
| error | below 60% | The dialog states that the scores are not meaningful at this coverage. |
Missing symbols are listed (the first 12, then a count). When the only difference is letter case, as with human ACTB against mouse-style Actb, the panel offers a checkbox, Match gene symbols ignoring case, and shows how many genes that recovers. It is off by default because case-insensitive matching can create false matches between unrelated symbols; use it when the mismatch is purely a naming convention.
What it writes¶
For a result prefix SCN:
| Where | What |
|---|---|
obs['SCN_class_argmax'] |
The cell type call (categorical), colored with the classifier's own palette via uns['SCN_class_argmax_colors']. |
obs['SCN_class_score'] |
The winning vote proportion: how confident that call is. |
obs['SCN_class_type'] |
Singular, Ambiguous, None or Rand (only when the call-quality option is on). |
obsm['SCN_score'] |
The full cells × cell types score matrix, registered in uns['skopia_score_matrices'] so its columns are labeled. |
uns['pyscn']['SCN'] |
A record of the run. |
The results panel shows the composition and call quality, with buttons to color by each output; Classify again returns to the form. The score matrix appears under the score-matrix pills of the Genes panel as ◈ SCN_score, so you can color by one cell type's score, and in the embedding picker, so you can plot two cell types' scores against each other.
Singular, Ambiguous, None and Rand¶
Each cell type gets its own threshold: the chosen quantile (default 0.05) of the scores of the cells that type won, as in PySingleCellNet's comp_ct_thresh. A cell is then called:
| Call | Rule |
|---|---|
| Singular | Exactly one cell type scores above its threshold. |
| Ambiguous | More than one cell type scores above its threshold. |
| None | No cell type scores above its threshold. |
| Rand | The top class is the classifier's synthetic random class (rand), not a cell type. |
PySingleCellNet splits the multi-type case into Intermediate and Hybrid using a graph of how related the cell types are, built from the training data. Skopia does not have that graph, so it reports those cells as Ambiguous rather than guessing at a biological distinction.
Train¶
Train builds a classifier from a categorical .obs column, writes it to disk as a pickle and hands it to the Classify tab.
- Switch to the Train tab.
- Pick Cell type labels (.obs column).
- Pick the Counts source (
.Xor a layer) and check Scale of that matrix (see below). - Under Save classifier to, type a path or click Browse. Browse chooses the destination folder and lists the classifiers already in it, so you can see what you have, reuse a name, or be warned before overwriting one. A name without an extension gets
.pkl. - Adjust the training parameters if needed and click Train classifier.
- When it finishes, the result panel states the preprocessing applied, where the file was saved and how many cells per type were used. Use it to classify this dataset moves it to the Classify tab.
Training works on a private copy. Your loaded dataset is not modified.
Preprocessing depends on the source scale¶
PySingleCellNet trains on log-normalized values with highly variable genes marked. Many public references are distributed only as log-normalized values, and normalizing and logging those again distorts the marker ranking that picks the gene pairs. Skopia detects the scale of the source matrix (see Expression scale detection) and applies only the steps still owed. The Scale of that matrix dropdown shows the detected scale and lets you override it, since detection is a heuristic run on sampled values.
| Source scale | What training does |
|---|---|
| Raw counts | normalize_total, then log1p, then highly variable genes (seurat_v3 flavor, on the counts) |
| Normalized, not logged | log1p, then highly variable genes (seurat flavor) |
| Already log-normalized | Nothing, then highly variable genes (seurat flavor) |
| Already log-scaled (library sizes not equalized) | Nothing, then highly variable genes (seurat flavor); the result warns that marker selection may be depth-biased |
| Could not be identified | Left untransformed, since transforming already-processed data is the worse error; set the scale yourself if you know it |
| Scaled / z-scored | Refused |
| Binary (presence/absence) or empty | Refused: a pair of genes that are both 0 or both 1 cannot be ranked |
Up to 2,000 highly variable genes are selected with scanpy (Wolf et al. 2018). If that step fails, every gene is treated as variable and training continues; the result panel then reports the flavor as none (fallback).
Training parameters¶
| Parameter | Default | What it does |
|---|---|---|
| cells / type | 100 | Cells sampled per cell type to balance the classes. 0 uses every cell. |
| top genes | 30 | Marker genes per cell type considered for pairs. |
| gene pairs | 40 | Gene pairs kept per cell type. |
| trees | 1000 | Trees in the random forest. |
| PCs | 30 | Principal components used to find similar cell types (capped by the data's size). |
After balancing, cell types with fewer than 3 cells are dropped and listed in the result. Training stops if fewer than 2 cell types remain.
Tips and pitfalls¶
Gene names with underscores
PySingleCellNet stores each gene pair as the string geneA_geneB and splits it on _ to decode it, so a symbol that contains an underscore cannot be represented. Training excludes such genes and reports how many; if fewer than 20 genes remain, it stops and says so. Loading a classifier trained on such symbols fails with a message rather than at prediction time. The bundled toy dataset is this case (its genes are named like Mesen_1), so use a dataset with conventional symbols, or rename the genes first with Analyze → Genes → Rename genes (see Multi-genome data).
Coverage below the ok band
A classifier gene missing from the query is a zero, not an absence. Every pair that uses it reads "A below B" or "A above B" for a reason that has nothing to do with the cell. Fix symbol conventions (case, species prefixes, Ensembl IDs versus symbols) before trusting a run with coverage under 95%.
Compare two references
Run Classify twice with different prefixes (for example SCN_atlasA and SCN_atlasB) and color by each _class_argmax, or plot the two runs' scores for one cell type against each other from the embedding picker.
Not scoped to the cell mask
Classification runs on every cell, and training on every cell of the chosen label column (after balancing). To train on part of a dataset, label only those cells in a separate column.