cellstate_c2s
Cell2Sentence 'sentences' are redefined for fine tuning and prediction to find cell states accurately.
Credits/Disclaimer: The original package is Cell2Sentence . Our approach is modification to highlight cell state differences.
Cell2Sentence creates a cell sentence for every cell. The order of the words is based on the expression level. It orders genes from highest expressing to the lowest expressing. While this method works well for cell types, it struggles for cell states. The reason is intuitive. Cell states share the same high expressing genes and trying to differentiate the cell states becomes a difficult task because the cell state defining features are not placed in the early positions of the sentence. We propose reframing the solution. For every cell type independently, we calculated the mean of every gene in that cell type. Then, we look at how the expression is deviated from the mean cell type signature. This provides a modified cell sentence where important markers of the cell states are being placed in the beginning of the sentence. This increases the accuracy of the predictions.
Install
pip install cellstate_c2s # core: numpy, scipy, pandas, anndata
pip install "cellstate_c2s[c2s]" # plus cell2sentence and datasets
From a local clone:
pip install -e ".[dev]"
pytest
Usage
import cell2sentence as cs
from cellstate_c2s import DeviationSentenceBuilder, replace_sentences
# 1. Build the arrow dataset with Cell2Sentence as usual
arrow_ds, vocabulary = cs.CSData.adata_to_arrow(
adata=adata_train, random_state=SEED, sentence_delimiter=" ",
label_col_names=["annotation_cell_states", "organism", "tissue"],
)
# 2. Fit the reference and build deviation sentences
builder = DeviationSentenceBuilder(top_k=200)
builder.fit(adata_train, mask=adata_train.obs["cell_type"] == "AT2") # optional mask
names, sentences = builder.transform(adata_train)
builder.save("reference.npz")
# 3. Swap the sentences in, matched by cell name, then continue with Cell2Sentence
arrow_ds = replace_sentences(arrow_ds, names, sentences)
csdata = cs.CSData.csdata_from_arrow(
arrow_dataset=arrow_ds, vocabulary=vocabulary,
save_dir=save_dir, save_name=save_name, dataset_backend="arrow",
)
Held-out test data (same platform): reuse the reference fitted on training data. Do not fit on the test set.
builder = DeviationSentenceBuilder.load("reference.npz")
names, sentences = builder.transform(adata_test)
Another platform (e.g. Xenium): fit a separate reference on that dataset, then do the Cell2Sentence vocabulary swap yourself.
names_x, sents_x = DeviationSentenceBuilder(top_k=200).fit_transform(adata_xenium)
arrow_ds_xenium = replace_sentences(arrow_ds_xenium, names_x, sents_x)
How sentences are built
For each cell, deviation = expression - reference_mean. Genes are sorted by deviation, highest first, and the first top_k are kept. Ties keep the order of var_names (stable sort), so the same input always gives the same sentence.
| Option | Default | Effect |
|---|---|---|
top_k |
200 | Maximum genes per sentence. |
mask_unexpressed |
True |
Drops genes whose value in that cell is exactly 0, so sentences can be shorter than top_k. Set False for a pure ranking of all genes. The original sentence drops zero expressing cells. In our experiments we tested both versions for the modified model. We also recommend dropping them for cell states. |
chunk_size |
2048 | Cells densified at a time. (Most of the time it is not a bottleneck) |
fit(..., mask=) |
all cells | Boolean array selecting the cells that define the reference. |
transform uses only the genes shared by the reference and the AnnData passed in, warns about missing reference genes, and ignores extra genes.
Before you use this
This package modifies cell sentences. You need to implement recommended steps on the cell2sentence documentation. After you create the sentences, you can use this package to modify the sentences. There are some assumptions that this package makes.
adata.Xis normalized, the same way in every dataset. Cell2Sentence requires the normalized values, this assumes it is done and you made sure X holds the normalized values.- Zero means "not expressed." With
mask_unexpressed=True, any gene with a value of exactly 0 in a cell is dropped [Recommended to drop]. - The cells passed to
fitdefine the reference. Pass only the cells you want (e.g. AT2), or usemask=. - Fit on training data only for evaluation.
fit_transformon a test set computes the reference from test cells. - Gene names are unique and use one naming system Both the finetuning and prediction datasets have the same gene names and you are using the common genes on both.
- Cell names (
obs_names) are unique and match the arrow dataset'scell_namecolumn. Field names are parameters ofreplace_sentencesif your Cell2Sentence version differs. - The datasets are already subset to the genes you want. You must subset the cell type of interest before finetuning, and also for prediction.
What is checked
| Checked in code | Not checked |
|---|---|
fit called before transform |
Normalization |
| Unique gene names | Gene naming system |
| Shared genes exist; warns on missing ones | Whether the reference cells are the right ones |
| Every arrow row has a sentence; no duplicate cell names | Train/test separation |
| Required arrow columns exist | Meaning of zeros |
License
MIT
Metadata
Release files for cellstate-c2s 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cellstate_c2s-0.1.1.tar.gz | 8.8 kB | Without provenance | Details for cellstate_c2s-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cellstate_c2s-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for cellstate_c2s-0.1.1-py3-none-any.whl |
Total release size: 16.7 kB
Release files / cellstate_c2s-0.1.1.tar.gz
| Download URL | cellstate_c2s-0.1.1.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7a6a61fe0a5d7af228a0a1fc83871cc245af49e2b9029284608b7f210344153a
|
|
BLAKE2b-256 checksum How to use checksums |
cad9393761df94cdf3461e643092760c5dae97edce3488db79a15ca0a8ba9192
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.2
|
Release files / cellstate_c2s-0.1.1-py3-none-any.whl
| Download URL | cellstate_c2s-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f8ebdf4ff669744e10e32331c760d5760568919bfe777c168a4bfb2044441a69
|
|
BLAKE2b-256 checksum How to use checksums |
892389808da228bb50a155c27416ac4e099db4b955666a42a994d3e5c76f5016
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.2
|
Security for cellstate-c2s 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.