Metadata-Version: 2.5
Name: biocognia-markers
Version: 0.2.6
Summary: The marker graph SCIMAP Pro, Plexora and other BioCognia products share: markers, cell families and the relations between them, each backed by evidence a machine checks
Author-email: Ajit Johnson Nirmal <ajitjohnson.n@gmail.com>
License-Expression: LicenseRef-Proprietary
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.12
Requires-Dist: numpy>=1.26
Requires-Dist: platformdirs>=4
Requires-Dist: scipy>=1.11
Description-Content-Type: text/markdown

# biocognia-markers

The marker graph that SCIMAP Pro, Plexora and other BioCognia products share.

- **Nodes:** markers and cell families.
- **Edges:** what is known about them: exceptions, exclusions, subsets, carriers and co-expression.
- **Evidence:** the evidence behind each node and edge.
- **Verification:** an item counts as verified only when a machine has found every part of it in
  its source (`biocognia-markers verify`).

```python
import biocognia_markers as bm

graph = bm.load()                       # the shipped graph, with the user's overlay laid over it
graph.resolve(["CD3e", "PD-1"])
graph.edges_of("m:pd1")
```

## Phenotyping from it

```python
from biocognia_markers import phenotypes, inference

defs = phenotypes.definitions(["CD45", "CD3e", "CD8a", "FOXP3"], context="melanoma")
defs["coverage"]["types"]                # what this panel can resolve, and what would recover the rest
inference.evaluate(defs, {"CD45": 0.9, "CD3e": 0.8, "CD8a": 0.9})   # one cell: type, outcome, reason, confidence
```

`phenotypes` turns the graph and a panel into definitions; `inference` is the one engine every product runs
on them (SCIMAP Pro's `tl.autoPhenotype` streams cells through it): one decision per pattern of positive,
negative and unmeasured markers. A cell positive for two lineages the graph keeps apart climbs a ladder (spill
from a neighbour, unmixing on linear intensities, relative strength, its spatial and expression neighbours) and,
when nothing settles it, gets the best guess. Named functional states live in
the graph's `states` section (schema 2), how a transcript reads each marker in its `rna` field.

### Your own cell types

```python
from biocognia_markers import workflow

text = workflow.text_of(workflow.from_definitions(defs))     # the knowledge base's types, one bare name per line
defs = workflow.compile("""
Immune: CD45+
    T cell: CD3+
        CD4 T cell: CD4+
    Macrophage: (CD68+ or CD163+), CD3-, CD4±
""", channels)
workflow.check(text, channels)                                # every problem, with its line
```

`+` has, `-` lacks, `±` may also have, `?` if measured, `or` any of, indentation inside. A line with markers
compiles to `require` on its type and is held strictly by the engine (`strict`): no final label breaks it or an
ancestor's line. A bare name is the knowledge base's type, weighed as in automatic mode, and a workflow of bare
names only compiles to the automatic document itself. A written negative is judged after the absolute spill
test (`strict.waivers`). A document without `require` is decided exactly as before.

An optional `relations:` block (`CD3 needs CD45`, `CD8 suggests CD3 unless NK cell`, `MART1 supports SOX10`)
says which markers' cells carry which others; the knowledge base also proposes them from the tree (`inferred`).
They matter only to uncertainty-aware calls (`uncertainty`: zones around the gates from reference populations;
`uncertainty.check_relations` keeps an inferred relation only where the data agree), where a clearly positive
deeper marker may stand for an inherited marker just below its gate.

## Changing it safely

Every write to an overlay is validated first (`bm.validate.validate`, a dry run: schema, unknown names,
duplicates, contradictions) and journaled beside it (`bm.overlay.history`, `at`, `revert`). A write never
brings a citation with it; only `verify` earns one. `Graph.fingerprint(used)` records what a result
rested on, and `bm.graph.compare` says later which of those items changed.

## See the graph

```sh
biocognia-markers view          # the live graph (shipped + your overlays), opened in a browser
```

The page shows every marker, family and relation, with the quotes behind each; search it by any
name a panel uses, filter by relation, confidence or tissue, or read the relations as a table. It
is written beside your overlay, and every change to the graph through this package rewrites it,
so reloading the tab shows the current graph. `bm.view.write(path)` writes it anywhere.

On GitHub, [`docs/graph.html`](docs/graph.html) is the page of the shipped graph at that commit:
download it (Raw, then save) and open the file. `biocognia-markers merge` and `verify --write`
rewrite it when they change the shipped graph; after a hand edit, run
`biocognia-markers view --shipped --out docs/graph.html --no-open` (the tests fail until you do).

## Adding to it

To add to the graph, an agent follows the skill `biocognia-marker-graph`
(`src/biocognia_markers/skills/marker-graph/SKILL.md`). `tools/migrate.py` built the first graph
from SCIMAP Pro's rule library and Plexora's vocabulary. Where those two sources disagree is
listed in `MIGRATION_REPORT.md`.
