Skip to main content

bgsmap

简体中文

bgsmap integrates gene-level rare-variant burden summaries with gene specificity scores (GSS) from gsMap. It computes enrichment per spatial spot and domain and exports TSV, interactive HTML, and vector PDF.

Version 1.0.0 uses normal-tail approximations for its primary P values and saves optional empirical permutation diagnostics in separate files. See the statistical model or the P-value migration guide (Chinese).

Install

Python 3.10 or later. Once bgsmap is published to PyPI, install it with either command:

python -m pip install bgsmap
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple bgsmap

The Tsinghua mirror makes new releases available after it syncs with PyPI. Until then, install from the release wheel or an unpacked source tree:

python -m pip install ./bgsmap-1.0.0-py3-none-any.whl
# Or run this from the unpacked source tree:
python -m pip install .

For Feather and H5AD support, install 'bgsmap[all]' from PyPI or '.[all]' from source. The feather extra also enables Parquet reading. The optional AnnData spot-ID fallback requires a separate anndata installation. The quick command additionally requires gsMap in a compatible Python environment.

bgsmap --version
python -m bgsmap --help

Workflow

Spatial H5AD ── gsMap GNN latent ── gsMap latent-to-gene GSS (gene × spot)
                                                    │
Gene-level burden summary (gene, p, mask) ── gene ID alignment and QC
                                                    │
                                GSS standardization + burden score
                                                    │
                           analytic conditional null (optional permutation diagnostics)
                                                    │
                     spot / region / competitive continuous P, logP, BH, Holm
                                                    │
                         interactive HTML + vector PDF + TSV

Crucially, no GWAS SNP-to-gene annotation, PLINK LD weights, LD-score generation, or S-LDSC are performed on burden summaries.

1. Demonstration without real data

bgsmap demo --out examples/demo_data
bgsmap run \
  --gss examples/demo_data/GSS.tsv.gz \
  --burden examples/demo_data/burden.tsv.gz \
  --coords examples/demo_data/spatial_coords.tsv \
  --trait-name SyntheticTrait \
  --permutations 0 \
  --out results/demo

The demo generates 160 genes, 96 spots, and a planted Domain 3 signal.

Commands print timestamped progress to stderr. The run and inspect commands write JSON to stdout. To save both streams separately, append > summary.json 2> run.log to a run command.

Upstream gsMap's actual output format is a Feather file, with a HUMAN_GENE_SYM gene column and one column per spatial barcode:

{workdir}/{sample}/latent_to_gene/{sample}_gene_marker_score.feather

Use the matching H5AD (same spot barcodes) and a real burden summary. Select the actual annotation and column names by inspection:

bgsmap inspect --h5ad /path/to/Visium.h5ad --burden /path/to/Finngen_gene_burden.tsv.gz

bgsmap run \
  --gss /path/to/Visium_gene_marker_score.feather \
  --h5ad /path/to/Visium_add_latent.h5ad \
  --annotation domain \
  --burden /path/to/Finngen_gene_burden.tsv.gz \
  --gene-col gene --p-col p \
  --trait-name G6_MIGRAINE \
  --permutations 0 --seed 42 \
  --out results/G6_MIGRAINE

The H5AD reader loads only /obs and /obsm/spatial (not the expression matrix), and supports AnnData categorical obs encoding. If your domain annotation is stored elsewhere, export spot,x,y,domain as TSV and use --coords instead of --h5ad. A multi-domain annotation is needed for the competitive region comparison.

To select one mask or trait from a multi-mask/multi-trait summary use --mask-col Mask --mask Mask1.0.01 or --trait-col trait --trait G6_MIGRAINE. Ambiguous duplicate gene tests are rejected by default; the optional --duplicate-policy bonferroni conservatively corrects the selected minimum p-value for the number of tests per gene. Never mix masks without documenting that strategy.

A gene's burden p-value distribution can depend on gene length, number of qualifying variants, carrier counts, coverage, variant mask and other factors. Permuting all gene labels without correction is exploratory; for interpretable inference, match genes on available testing-power covariates:

bgsmap run \
  --gss /path/to/GSS.feather --h5ad /path/to/Visium.h5ad --annotation domain \
  --burden /path/to/burden.tsv.gz --gene-col Gene --p-col P \
  --covariates /path/to/gene_test_covariates.tsv \
  --match-cols gene_length n_tested_variants \
  --match-bins 4 --permutations 0 \
  --out results/matched

gene_test_covariates.tsv needs gene,gene_length,n_tested_variants. These columns may instead be present in the burden summary itself, in which case omit --covariates. Each joint stratum must contain at least 12 matched genes (small strata error rather than silent fallback). For actual burden results, a matched null is preferred to an unrestricted gene-label shuffle, but it is not a proof of calibration; gene-gene correlation and differences in burden test power can remain.

Mouse tissue versus human burden

Original gsMap latent-to-gene can convert mouse genes to human symbols with a species–human homolog file. If your existing GSS is not already human-annotated, pass a strictly one-to-one table:

st_gene	burden_gene
MouseSymbol1	HumanSymbol1
MouseSymbol2	HumanSymbol2

and add --gene-map /path/to/one_to_one.tsv. This pipeline never silently transforms mouse gene symbols into human genes. The run_metadata.json file records the retained gene/spot counts.

3. From raw spatial H5AD (reuse original gsMap)

pip install gsMap

bgsmap quick \
  --h5ad /path/to/V1_Adult_Mouse_Brain_Coronal_Section.h5ad \
  --workdir work/gsMap_precompute --sample-name VisiumCoronal \
  --data-layer counts --annotation domain \
  --homolog-file /path/to/mouse_human_homolog.tsv \
  --burden /path/to/FinnGen_MIGRAINE.tsv.gz \
  --gene-col gene --p-col p \
  --permutations 0 --out results/coronal

quick uses upstream gsMap.find_latent_representation and gsMap.latent_to_gene and then calls this package's gene-level inference. It has not been integration-tested against your server's gsMap version. It does not call gsMap's GWAS analysis. To skip expensive GNN training when H5AD already has an embedding, pass --latent-key latent_GVAE (replace with your actual obsm key). Upstream gsMap may copy the H5AD into its workdir; keep sufficient disk space. Use --force-gss to recompute upstream steps.

4. Configuration / figures

# Edit the input and column names first
bgsmap run --config examples/visium_finngen.json
# Regenerate plots from tables that include analytic P values
bgsmap plot --out results/coronal --trait-name G6_MIGRAINE

Output directory:

results/coronal/
├── aligned_burden.tsv.gz
├── spot_results.tsv.gz                # spot, x/y, domain, T, p_analytic, logp_analytic, q_bh_analytic, p_holm_analytic
├── region_results.tsv                # region / competitive analytic p, logp, BH, Holm
├── region_permutation_diagnostics.tsv # optional, only if permutations>0
├── spot_permutation_diagnostics.tsv.gz # optional, only if permutations>0
├── gene_contributions.tsv.gz         # descriptive gene contributions
├── run_metadata.json                 # seeds, method and gene/spots overlap
└── plots/
    ├── report.html                   # offline report, links to interactive figures
    ├── spatial_enrichment.html       # offline Plotly, interactive (self-contained)
    ├── spatial_enrichment.pdf        # vector point symbols, two spatial panels
    ├── region_enrichment.html
    ├── region_enrichment.pdf
    └── gene_contributions.pdf

PDFs use Matplotlib vector artists and embedded TrueType text (pdf.fonttype=42), editable with Adobe Illustrator (subject to font availability). HTML is standalone and can be opened locally without a web server. Copy the entire plots folder to preserve report iframe references.

Statistical model

See the statistical model for the exact conditional variance formula, Gaussian-tail approximation, competitive test, within-run multiplicity and validity limitations. Evaluate tail calibration on the intended data before drawing research conclusions.

Development

python -m pip install -e '.[all,dev]'
python -m pytest -q
python -m build

CI covers Python 3.10 and 3.12. Tests exercise the synthetic analysis and the HTML/PDF outputs.

License and citation

bgsmap is released under the MIT license. When using GSS computed by gsMap, cite the upstream paper. Song et al., Nature 641, 932–941 (2025).

Metadata

Release files for bgsmap 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bgsmap 1.0.0
File Size Uploaded Provenance Actions
bgsmap-1.0.0.tar.gz 45.2 kB With provenance Details for bgsmap-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bgsmap 1.0.0
File Interpreter ABI Platform Actions
bgsmap-1.0.0-py3-none-any.whl Python 3 none any Details for bgsmap-1.0.0-py3-none-any.whl

Total release size: 74.4 kB

Release files / bgsmap-1.0.0.tar.gz

Download URL bgsmap-1.0.0.tar.gz
Size 45.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c79a9dd46e853d92dd9901d616974b022e185364d23c48943fdba966f125b7ed
BLAKE2b-256 checksum
How to use checksums
d5b8d36c97680ee763b757d44cfd8745cf5b9d11deedeebc76d9db4dc05e35f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / bgsmap-1.0.0-py3-none-any.whl

Download URL bgsmap-1.0.0-py3-none-any.whl
Size 29.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b2c9de3a9340b9a30a094c31e17bc3aebb3c1ba2f33b64fb8d47d7272a81dfa
BLAKE2b-256 checksum
How to use checksums
d195db202f8d0932810e987d487badd0591be292c38a1fc6ec396482bbef9445
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for bgsmap 1.0.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bgsmap-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page