gracer 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gracer-1.0.0/LICENSE +21 -0
- gracer-1.0.0/MANIFEST.in +10 -0
- gracer-1.0.0/PKG-INFO +217 -0
- gracer-1.0.0/README.md +179 -0
- gracer-1.0.0/gracer/__init__.py +103 -0
- gracer-1.0.0/gracer/__main__.py +10 -0
- gracer-1.0.0/gracer/_ncbi.py +88 -0
- gracer-1.0.0/gracer/_parallel.py +97 -0
- gracer-1.0.0/gracer/_version.py +29 -0
- gracer-1.0.0/gracer/annotation/__init__.py +0 -0
- gracer-1.0.0/gracer/annotation/germline.py +1297 -0
- gracer-1.0.0/gracer/annotation/igblast.py +1118 -0
- gracer-1.0.0/gracer/annotation/vhh_score.py +133 -0
- gracer-1.0.0/gracer/cli/__init__.py +10 -0
- gracer-1.0.0/gracer/cli/__main__.py +9 -0
- gracer-1.0.0/gracer/cli/common.py +123 -0
- gracer-1.0.0/gracer/cli/db.py +424 -0
- gracer-1.0.0/gracer/cli/renumber.py +129 -0
- gracer-1.0.0/gracer/cli/run.py +519 -0
- gracer-1.0.0/gracer/cli/validate.py +114 -0
- gracer-1.0.0/gracer/example_data/OAS_Jaffe2022_subset.fasta +20 -0
- gracer-1.0.0/gracer/exceptions.py +49 -0
- gracer-1.0.0/gracer/germline_db/__init__.py +89 -0
- gracer-1.0.0/gracer/germline_db/_imgt.py +158 -0
- gracer-1.0.0/gracer/germline_db/alignments/__init__.py +3 -0
- gracer-1.0.0/gracer/germline_db/alignments/formatting.py +681 -0
- gracer-1.0.0/gracer/germline_db/alignments/regap.py +405 -0
- gracer-1.0.0/gracer/germline_db/build_config_template.yml +92 -0
- gracer-1.0.0/gracer/germline_db/builder/__init__.py +4 -0
- gracer-1.0.0/gracer/germline_db/builder/_common.py +83 -0
- gracer-1.0.0/gracer/germline_db/builder/config.py +844 -0
- gracer-1.0.0/gracer/germline_db/builder/expand.py +270 -0
- gracer-1.0.0/gracer/germline_db/builder/igblast.py +257 -0
- gracer-1.0.0/gracer/germline_db/builder/lockfile.py +272 -0
- gracer-1.0.0/gracer/germline_db/builder/pipeline.py +949 -0
- gracer-1.0.0/gracer/germline_db/builder/steps.py +558 -0
- gracer-1.0.0/gracer/germline_db/seed_v.hmm +2415 -0
- gracer-1.0.0/gracer/germline_db/sources/__init__.py +5 -0
- gracer-1.0.0/gracer/germline_db/sources/_http.py +252 -0
- gracer-1.0.0/gracer/germline_db/sources/_pinned.py +24 -0
- gracer-1.0.0/gracer/germline_db/sources/custom_fasta.py +512 -0
- gracer-1.0.0/gracer/germline_db/sources/genedb.py +810 -0
- gracer-1.0.0/gracer/germline_db/sources/list_available.py +475 -0
- gracer-1.0.0/gracer/germline_db/sources/ogrdb.py +830 -0
- gracer-1.0.0/gracer/germline_db/sources/vquest.py +341 -0
- gracer-1.0.0/gracer/germline_db/translate.py +121 -0
- gracer-1.0.0/gracer/io.py +327 -0
- gracer-1.0.0/gracer/loci.py +64 -0
- gracer-1.0.0/gracer/models.py +861 -0
- gracer-1.0.0/gracer/nucleotide.py +188 -0
- gracer-1.0.0/gracer/numbering/__init__.py +129 -0
- gracer-1.0.0/gracer/numbering/_numba.py +29 -0
- gracer-1.0.0/gracer/numbering/cdr3_tail.py +144 -0
- gracer-1.0.0/gracer/numbering/compact.py +205 -0
- gracer-1.0.0/gracer/numbering/germline_layout.py +338 -0
- gracer-1.0.0/gracer/numbering/hmm.py +490 -0
- gracer-1.0.0/gracer/numbering/profile_nw.py +724 -0
- gracer-1.0.0/gracer/numbering/profiles.py +472 -0
- gracer-1.0.0/gracer/numbering/schemes/__init__.py +10 -0
- gracer-1.0.0/gracer/numbering/schemes/_base.py +68 -0
- gracer-1.0.0/gracer/numbering/schemes/engine.py +647 -0
- gracer-1.0.0/gracer/numbering/schemes/junctions.py +144 -0
- gracer-1.0.0/gracer/numbering/schemes/tables.py +419 -0
- gracer-1.0.0/gracer/output/__init__.py +14 -0
- gracer-1.0.0/gracer/output/columns.py +404 -0
- gracer-1.0.0/gracer/output/encoding.py +370 -0
- gracer-1.0.0/gracer/output/parallel_writer.py +539 -0
- gracer-1.0.0/gracer/output/regions.py +307 -0
- gracer-1.0.0/gracer/output/rows.py +384 -0
- gracer-1.0.0/gracer/output/writers.py +261 -0
- gracer-1.0.0/gracer/paths.py +51 -0
- gracer-1.0.0/gracer/py.typed +0 -0
- gracer-1.0.0/gracer/runner/__init__.py +13 -0
- gracer-1.0.0/gracer/runner/core.py +899 -0
- gracer-1.0.0/gracer/runner/domains.py +699 -0
- gracer-1.0.0/gracer/runner/notices.py +353 -0
- gracer-1.0.0/gracer/runner/pool.py +534 -0
- gracer-1.0.0/gracer/runner/provenance.py +98 -0
- gracer-1.0.0/gracer/runner/tables.py +312 -0
- gracer-1.0.0/gracer/runner/workers.py +402 -0
- gracer-1.0.0/gracer/structure.py +898 -0
- gracer-1.0.0/gracer.egg-info/PKG-INFO +217 -0
- gracer-1.0.0/gracer.egg-info/SOURCES.txt +87 -0
- gracer-1.0.0/gracer.egg-info/dependency_links.txt +1 -0
- gracer-1.0.0/gracer.egg-info/entry_points.txt +2 -0
- gracer-1.0.0/gracer.egg-info/requires.txt +19 -0
- gracer-1.0.0/gracer.egg-info/top_level.txt +1 -0
- gracer-1.0.0/pyproject.toml +143 -0
- gracer-1.0.0/setup.cfg +4 -0
gracer-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alchemab Therapeutics Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
gracer-1.0.0/MANIFEST.in
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# The sdist is the package source plus its licences and README. MANIFEST.in
|
|
2
|
+
# rules apply after setuptools reads an existing gracer.egg-info/SOURCES.txt,
|
|
3
|
+
# so these prunes also drop what a stale one lists: a SOURCES.txt from a tree
|
|
4
|
+
# with a built database shipped that database in the sdist and in the wheel
|
|
5
|
+
# built from it.
|
|
6
|
+
prune gracer/germline_db/database
|
|
7
|
+
prune build
|
|
8
|
+
# setuptools adds tests/test_*.py by default, without conftest.py or the
|
|
9
|
+
# fixtures they need, so they could not run from the sdist anyway.
|
|
10
|
+
prune tests
|
gracer-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gracer
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Numbering of antibody, TCR, VHH, and VNAR sequences with AIRR-style output
|
|
5
|
+
Author-email: Aretas Gaspariūnas <aretas.gaspariunas@alchemab.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/alchemab/gracer
|
|
8
|
+
Keywords: gracer,antibody,TCR,VHH,nanobody,VNAR,numbering,annotation
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Python: <3.15,>=3.12
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: biopython>=1.83
|
|
20
|
+
Requires-Dist: pyhmmer==0.12.3
|
|
21
|
+
Requires-Dist: typer>=0.20.1
|
|
22
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
23
|
+
Requires-Dist: requests>=2.32.4
|
|
24
|
+
Requires-Dist: urllib3>=2.6.3
|
|
25
|
+
Requires-Dist: rich>=14.3.4
|
|
26
|
+
Requires-Dist: pandas>=2.1.1
|
|
27
|
+
Requires-Dist: pyarrow>=14.0.1
|
|
28
|
+
Requires-Dist: numpy>=1.26
|
|
29
|
+
Requires-Dist: numba>=0.59
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=9.0.3; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov>=7; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=2.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff==0.17.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pandas-stubs<3.1,>=3.0; extra == "dev"
|
|
36
|
+
Requires-Dist: airr>=2.0; extra == "dev"
|
|
37
|
+
Dynamic: license-file
|
|
38
|
+
|
|
39
|
+
# GRACER: Germline-Resolved Adaptive receptor Characterization Engine for Repertoires
|
|
40
|
+
|
|
41
|
+
[](LICENSE)
|
|
42
|
+
[](https://github.com/alchemab/gracer/actions/workflows/ci.yml)
|
|
43
|
+
[](https://codecov.io/gh/alchemab/gracer)
|
|
44
|
+
[](pyproject.toml)
|
|
45
|
+
[](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-strict)
|
|
46
|
+
[](https://github.com/astral-sh/ruff)
|
|
47
|
+
|
|
48
|
+
Numbers antibody, TCR, camelid VHH and shark VNAR variable domains, calls their germline genes and writes AIRR-style output, in one pass.
|
|
49
|
+
|
|
50
|
+
Input is amino-acid sequences, or nucleotide reads with `--nucleotide`, which translates each read in its six frames and numbers the best one ([nucleotide input](docs/nucleotide-input.md)). The optional [IgBLAST backend](docs/igblast.md) (`--backend igblastn`) aligns nucleotides instead, for nucleotide identities and frameshift-aware calls, but does not number.
|
|
51
|
+
|
|
52
|
+
- **Numbering** in IMGT, Chothia, Kabat, Martin and AHo, with CDR/FWR regions under any of seven CDR definitions (IMGT, Kabat, Chothia, AbM, Contact, North, …), for every domain of a construct (scFv, bispecific, tandem)
|
|
53
|
+
- **Germline calls**: V and J (D and C optional) with identities, mismatches and per-position mutation lists, against a germline database you build from pinned IMGT/GENE-DB, OGRDB or IMGT/V-QUEST releases, or your own FASTA, and reproduce from a lockfile
|
|
54
|
+
- **AIRR rearrangement output** (amino-acid subset) as TSV, CSV or Parquet, or a pandas DataFrame; `--airr-strict` writes every required column so files pass AIRR validation
|
|
55
|
+
- **Fast, bounded memory**: ~27,000 sequences per second with HMMER3 and ~43,000 with the profile Needleman–Wunsch backend on a 10-core laptop, 32× and 52× faster than ANARCI ([benchmarks](docs/performance.md#benchmarks)); streaming keeps memory flat whatever the input size (about 1 GB at 10 cores)
|
|
56
|
+
- **No external binaries**: HMMER3 runs in-process through [pyhmmer](https://pyhmmer.readthedocs.io); [NCBI IgBLAST](https://ncbi.github.io/igblast/) is an optional backend (installed separately; see [IgBLAST setup](docs/igblast.md#setup))
|
|
57
|
+
- **Structures and ML**: renumber PDB/mmCIF files, and encode numbered sequences or language-model embeddings on a common position axis
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install gracer # Python 3.12 to 3.14
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
gracer runs on glibc Linux (x86_64 and arm64) and Apple-silicon macOS. Intel macOS and musl Linux (Alpine) have no numba wheels, so the install fails there. On Windows, use [WSL2](https://learn.microsoft.com/windows/wsl/install).
|
|
68
|
+
|
|
69
|
+
To run the development version, install from a clone, editable so that `git pull` updates it:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
git clone https://github.com/alchemab/gracer.git
|
|
73
|
+
cd gracer
|
|
74
|
+
pip install -e .
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
For tests, linting and other development notes, see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
78
|
+
|
|
79
|
+
Then build a germline database, needed once per environment before numbering:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
gracer db build --preset antibody # human IG (IGH, IGK, IGL) from IMGT/GENE-DB
|
|
83
|
+
gracer db build --preset tcr # human and mouse TCR
|
|
84
|
+
gracer db build --preset vhh # alpaca IGH
|
|
85
|
+
gracer db build --preset vnar # shark VNAR (IMGT scheme only)
|
|
86
|
+
gracer db build --preset vertebrate # an IG panel across vertebrate clades, for mixed-origin data
|
|
87
|
+
gracer db build --preset antibody --preset tcr --force # combine presets; --force replaces an existing database
|
|
88
|
+
gracer db build --preset antibody --species "Rattus norvegicus" # any species, from whichever source has it
|
|
89
|
+
gracer db add --species "Macaca mulatta" # add to the installed database
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Presets and species requests download a pinned [IMGT/GENE-DB](https://www.imgt.org/genedb/) or IMGT/V-QUEST release, or [OGRDB](https://ogrdb.airr-community.org)'s newest germline sets (the lockfile records which); `gracer db list-species` lists the species. For strains, pinned releases, filters or your own germlines, write a build config: see [Customising the build](docs/build-config.md).
|
|
93
|
+
|
|
94
|
+
The database lives inside the installed package and survives upgrades. To keep it elsewhere (a read-only install, or one database shared by several environments), set `GRACER_DB_DIR` wherever you build and wherever you run gracer:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
export GRACER_DB_DIR=~/.local/share/gracer/db
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`gracer validate` checks the install end to end. After a major gracer upgrade, gracer tells you how to rebuild the database from the same germline releases ([Upgrading gracer](docs/build-config.md#upgrading-gracer)).
|
|
101
|
+
|
|
102
|
+
### Docker
|
|
103
|
+
|
|
104
|
+
[Dockerfile](Dockerfile) and [docker-compose.yml](docker-compose.yml) build an image with the `antibody` database already built:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
docker compose build
|
|
108
|
+
docker compose run --rm --user "$(id -u):$(id -g)" gracer annotate sequences.fasta --scheme chothia --assign-germline
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`--user` writes the output as you; without it, files on the mounted directory are owned by root.
|
|
112
|
+
|
|
113
|
+
With `docker run`, add `--init` so `docker stop` ends gracer at once rather than after 10 seconds.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Quick start
|
|
118
|
+
|
|
119
|
+
**Number sequences from a file:**
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
gracer annotate sequences.fasta # FASTA (.fa/.fasta/.faa/.fna, optionally .gz)
|
|
123
|
+
gracer annotate repertoire.tsv # AIRR TSV, CSV or Parquet with a sequence_aa column
|
|
124
|
+
gracer annotate table.csv --sequence-column heavy # any other column name
|
|
125
|
+
gracer annotate sequences.fasta -s kabat -a -r -o numbered.csv # Kabat, germline calls, regions, CSV output
|
|
126
|
+
cat sequences.fasta | gracer annotate -o - > out.tsv # stdin to stdout
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Output goes next to the input as `<input>_gracer_output.tsv` unless `--outfile` says otherwise; its extension picks the format (`.tsv`, `.csv`, either optionally `.gz`, or `.parquet`). See [Output format](docs/output-format.md) for the columns.
|
|
130
|
+
|
|
131
|
+
**Number sequences in Python:**
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from gracer import number
|
|
135
|
+
|
|
136
|
+
sequences = [
|
|
137
|
+
"QVQLVQSGVEVKKPGASVKVSCKASGYTFTNYYMYWVRQAPGQGLEWMGGINPSNGGTNFNEKFKNRVTLTTDSSTTTAYMELKSLQFDDTAVYYCARRDYRFDMGFDYWGQGTTVTVSS",
|
|
138
|
+
"DIEIVLTQSPATLSLSPGERATLSCRASKGVSTSGYSYLHWYQQKPGQAPRLLIYLASYLESGVPARFSGSGSGTDFTLTISSLEPEDFAVYYCQHSRDLPLTFGGGTKVEIK",
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
results = number(sequences, scheme="imgt", assign_germline=True, species=["Homo sapiens"])
|
|
142
|
+
print(results[0].v_call, results[0].cdr3_aa)
|
|
143
|
+
df = results.to_pandas()
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
**Renumber a structure (PDB / mmCIF):**
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
gracer renumber 1fvc.cif -s chothia -o 1fvc_chothia.pdb
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**Share a reproducible germline database:**
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
gracer db export-lock # writes gracer-build.lock.yml
|
|
156
|
+
gracer db build --build-config gracer-build.lock.yml # rebuilds the identical database elsewhere
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Documentation
|
|
162
|
+
|
|
163
|
+
| Topic | File |
|
|
164
|
+
| --- | --- |
|
|
165
|
+
| CLI reference | [docs/cli-reference.md](docs/cli-reference.md) |
|
|
166
|
+
| Python API reference | [docs/python-api.md](docs/python-api.md) |
|
|
167
|
+
| Numbering schemes | [docs/numbering-schemes.md](docs/numbering-schemes.md) |
|
|
168
|
+
| Customising the build | [docs/build-config.md](docs/build-config.md) |
|
|
169
|
+
| Output format (AIRR TSV columns, conformance scope) | [docs/output-format.md](docs/output-format.md) |
|
|
170
|
+
| IgBLAST | [docs/igblast.md](docs/igblast.md) |
|
|
171
|
+
| Score thresholds | [docs/score_threshold_calibration.md](docs/score_threshold_calibration.md) |
|
|
172
|
+
| Performance and benchmarks | [docs/performance.md](docs/performance.md) |
|
|
173
|
+
| How it works | [docs/how_it_works.md](docs/how_it_works.md) |
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## Support
|
|
178
|
+
|
|
179
|
+
GRACER is maintained by Alchemab Therapeutics as time permits, without a guaranteed level of support. We prioritise fixing bugs, especially ones that give wrong numbering or germline calls, and will also try to answer questions, improve the documentation and consider suggested features.
|
|
180
|
+
|
|
181
|
+
- Report a bug, ask a question or suggest a feature by [opening an issue](https://github.com/alchemab/gracer/issues) (preferred). For a bug, include `gracer --version`, the `gracer db info` output, the command and a sequence that shows the problem.
|
|
182
|
+
- [Pull requests](https://github.com/alchemab/gracer/pulls) are welcome; see [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup.
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## Citing
|
|
187
|
+
|
|
188
|
+
A GRACER preprint is in preparation. Until it is out, please cite this repository:
|
|
189
|
+
|
|
190
|
+
> Gaspariūnas, A. *et al.* GRACER: Germline-Resolved Adaptive receptor Characterization Engine for Repertoires. <https://github.com/alchemab/gracer>
|
|
191
|
+
|
|
192
|
+
<!-- TODO: replace with the preprint reference and DOI once posted. -->
|
|
193
|
+
|
|
194
|
+
Please also cite the germline databases you built against:
|
|
195
|
+
|
|
196
|
+
- [IMGT/GENE-DB](https://doi.org/10.1093/nar/gki010) (presets `antibody`, `tcr`, `vhh`, `vertebrate`): Giudicelli, V., Chaume, D. & Lefranc, M.-P. (2005). *Nucleic Acids Research* 33, D256–D261.
|
|
197
|
+
- [IMGT/V-QUEST](https://doi.org/10.1093/nar/gkn316) reference directory (presets `vnar`, `vertebrate`): Brochet, X., Lefranc, M.-P. & Giudicelli, V. (2008). *Nucleic Acids Research* 36, W503–W508.
|
|
198
|
+
- [OGRDB](https://doi.org/10.1093/nar/gkaf1094) (`ogrdb` source in a build config): Lees, W.D. *et al.* (2026). *Nucleic Acids Research* 54, D932–D937.
|
|
199
|
+
|
|
200
|
+
and the HMM tools behind both numbering backends and the database build:
|
|
201
|
+
|
|
202
|
+
- [HMMER](https://doi.org/10.1371/journal.pcbi.1002195): Eddy, S.R. (2011). *PLOS Computational Biology* 7, e1002195.
|
|
203
|
+
- [pyHMMER](https://doi.org/10.1093/bioinformatics/btad214): Larralde, M. & Zeller, G. (2023). *Bioinformatics* 39, btad214.
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## Acknowledgements
|
|
208
|
+
|
|
209
|
+
Germline sequences are fetched at build time from [OGRDB](https://ogrdb.airr-community.org) (Open Germline Receptor Database) and [IMGT/GENE-DB](https://www.imgt.org/genedb/), the latter via the [JamieHeather/genedb-releases](https://github.com/JamieHeather/genedb-releases) archive maintained by Jamie Heather. The `vnar` and `vertebrate` presets use shark FASTAs from the [IMGT/V-QUEST reference directory](https://www.imgt.org/download/V-QUEST/IMGT_V-QUEST_reference_directory/) via [aretasg/vquest-releases](https://github.com/aretasg/vquest-releases), a companion archive that mirrors V-QUEST weekly so historical releases stay pinnable. As of 1 July 2026, IMGT® is an [ELIXIR Core Data Resource](https://elixir-europe.org/platforms/data/core-data-resources) and its data are provided under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) licence to both public and private users. `gracer db build` prints an attribution reminder for each source it used; see [Citing](#citing) for the references.
|
|
210
|
+
|
|
211
|
+
GRACER began from the code of [ANARCI](https://github.com/oxpig/ANARCI) ([Dunbar & Deane, 2016](https://doi.org/10.1093/bioinformatics/btv552)) at commit [79f6c57](https://github.com/oxpig/ANARCI/commit/79f6c575056dedef86cb8f405ebb039197923eec) and has since been rewritten; no ANARCI code remains. Where the scheme publications leave a choice open (gap orders for CDR lengths they never observed, where an insertion goes), gracer keeps ANARCI's conventions, and ANARCI's numbering remains the reference gracer's is tested against.
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## Licence
|
|
216
|
+
|
|
217
|
+
GRACER is MIT-licensed. See [LICENSE](LICENSE).
|
gracer-1.0.0/README.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# GRACER: Germline-Resolved Adaptive receptor Characterization Engine for Repertoires
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://github.com/alchemab/gracer/actions/workflows/ci.yml)
|
|
5
|
+
[](https://codecov.io/gh/alchemab/gracer)
|
|
6
|
+
[](pyproject.toml)
|
|
7
|
+
[](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-strict)
|
|
8
|
+
[](https://github.com/astral-sh/ruff)
|
|
9
|
+
|
|
10
|
+
Numbers antibody, TCR, camelid VHH and shark VNAR variable domains, calls their germline genes and writes AIRR-style output, in one pass.
|
|
11
|
+
|
|
12
|
+
Input is amino-acid sequences, or nucleotide reads with `--nucleotide`, which translates each read in its six frames and numbers the best one ([nucleotide input](docs/nucleotide-input.md)). The optional [IgBLAST backend](docs/igblast.md) (`--backend igblastn`) aligns nucleotides instead, for nucleotide identities and frameshift-aware calls, but does not number.
|
|
13
|
+
|
|
14
|
+
- **Numbering** in IMGT, Chothia, Kabat, Martin and AHo, with CDR/FWR regions under any of seven CDR definitions (IMGT, Kabat, Chothia, AbM, Contact, North, …), for every domain of a construct (scFv, bispecific, tandem)
|
|
15
|
+
- **Germline calls**: V and J (D and C optional) with identities, mismatches and per-position mutation lists, against a germline database you build from pinned IMGT/GENE-DB, OGRDB or IMGT/V-QUEST releases, or your own FASTA, and reproduce from a lockfile
|
|
16
|
+
- **AIRR rearrangement output** (amino-acid subset) as TSV, CSV or Parquet, or a pandas DataFrame; `--airr-strict` writes every required column so files pass AIRR validation
|
|
17
|
+
- **Fast, bounded memory**: ~27,000 sequences per second with HMMER3 and ~43,000 with the profile Needleman–Wunsch backend on a 10-core laptop, 32× and 52× faster than ANARCI ([benchmarks](docs/performance.md#benchmarks)); streaming keeps memory flat whatever the input size (about 1 GB at 10 cores)
|
|
18
|
+
- **No external binaries**: HMMER3 runs in-process through [pyhmmer](https://pyhmmer.readthedocs.io); [NCBI IgBLAST](https://ncbi.github.io/igblast/) is an optional backend (installed separately; see [IgBLAST setup](docs/igblast.md#setup))
|
|
19
|
+
- **Structures and ML**: renumber PDB/mmCIF files, and encode numbered sequences or language-model embeddings on a common position axis
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install gracer # Python 3.12 to 3.14
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
gracer runs on glibc Linux (x86_64 and arm64) and Apple-silicon macOS. Intel macOS and musl Linux (Alpine) have no numba wheels, so the install fails there. On Windows, use [WSL2](https://learn.microsoft.com/windows/wsl/install).
|
|
30
|
+
|
|
31
|
+
To run the development version, install from a clone, editable so that `git pull` updates it:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
git clone https://github.com/alchemab/gracer.git
|
|
35
|
+
cd gracer
|
|
36
|
+
pip install -e .
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
For tests, linting and other development notes, see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
40
|
+
|
|
41
|
+
Then build a germline database, needed once per environment before numbering:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
gracer db build --preset antibody # human IG (IGH, IGK, IGL) from IMGT/GENE-DB
|
|
45
|
+
gracer db build --preset tcr # human and mouse TCR
|
|
46
|
+
gracer db build --preset vhh # alpaca IGH
|
|
47
|
+
gracer db build --preset vnar # shark VNAR (IMGT scheme only)
|
|
48
|
+
gracer db build --preset vertebrate # an IG panel across vertebrate clades, for mixed-origin data
|
|
49
|
+
gracer db build --preset antibody --preset tcr --force # combine presets; --force replaces an existing database
|
|
50
|
+
gracer db build --preset antibody --species "Rattus norvegicus" # any species, from whichever source has it
|
|
51
|
+
gracer db add --species "Macaca mulatta" # add to the installed database
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Presets and species requests download a pinned [IMGT/GENE-DB](https://www.imgt.org/genedb/) or IMGT/V-QUEST release, or [OGRDB](https://ogrdb.airr-community.org)'s newest germline sets (the lockfile records which); `gracer db list-species` lists the species. For strains, pinned releases, filters or your own germlines, write a build config: see [Customising the build](docs/build-config.md).
|
|
55
|
+
|
|
56
|
+
The database lives inside the installed package and survives upgrades. To keep it elsewhere (a read-only install, or one database shared by several environments), set `GRACER_DB_DIR` wherever you build and wherever you run gracer:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
export GRACER_DB_DIR=~/.local/share/gracer/db
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`gracer validate` checks the install end to end. After a major gracer upgrade, gracer tells you how to rebuild the database from the same germline releases ([Upgrading gracer](docs/build-config.md#upgrading-gracer)).
|
|
63
|
+
|
|
64
|
+
### Docker
|
|
65
|
+
|
|
66
|
+
[Dockerfile](Dockerfile) and [docker-compose.yml](docker-compose.yml) build an image with the `antibody` database already built:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
docker compose build
|
|
70
|
+
docker compose run --rm --user "$(id -u):$(id -g)" gracer annotate sequences.fasta --scheme chothia --assign-germline
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`--user` writes the output as you; without it, files on the mounted directory are owned by root.
|
|
74
|
+
|
|
75
|
+
With `docker run`, add `--init` so `docker stop` ends gracer at once rather than after 10 seconds.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Quick start
|
|
80
|
+
|
|
81
|
+
**Number sequences from a file:**
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
gracer annotate sequences.fasta # FASTA (.fa/.fasta/.faa/.fna, optionally .gz)
|
|
85
|
+
gracer annotate repertoire.tsv # AIRR TSV, CSV or Parquet with a sequence_aa column
|
|
86
|
+
gracer annotate table.csv --sequence-column heavy # any other column name
|
|
87
|
+
gracer annotate sequences.fasta -s kabat -a -r -o numbered.csv # Kabat, germline calls, regions, CSV output
|
|
88
|
+
cat sequences.fasta | gracer annotate -o - > out.tsv # stdin to stdout
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Output goes next to the input as `<input>_gracer_output.tsv` unless `--outfile` says otherwise; its extension picks the format (`.tsv`, `.csv`, either optionally `.gz`, or `.parquet`). See [Output format](docs/output-format.md) for the columns.
|
|
92
|
+
|
|
93
|
+
**Number sequences in Python:**
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from gracer import number
|
|
97
|
+
|
|
98
|
+
sequences = [
|
|
99
|
+
"QVQLVQSGVEVKKPGASVKVSCKASGYTFTNYYMYWVRQAPGQGLEWMGGINPSNGGTNFNEKFKNRVTLTTDSSTTTAYMELKSLQFDDTAVYYCARRDYRFDMGFDYWGQGTTVTVSS",
|
|
100
|
+
"DIEIVLTQSPATLSLSPGERATLSCRASKGVSTSGYSYLHWYQQKPGQAPRLLIYLASYLESGVPARFSGSGSGTDFTLTISSLEPEDFAVYYCQHSRDLPLTFGGGTKVEIK",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
results = number(sequences, scheme="imgt", assign_germline=True, species=["Homo sapiens"])
|
|
104
|
+
print(results[0].v_call, results[0].cdr3_aa)
|
|
105
|
+
df = results.to_pandas()
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Renumber a structure (PDB / mmCIF):**
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
gracer renumber 1fvc.cif -s chothia -o 1fvc_chothia.pdb
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Share a reproducible germline database:**
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
gracer db export-lock # writes gracer-build.lock.yml
|
|
118
|
+
gracer db build --build-config gracer-build.lock.yml # rebuilds the identical database elsewhere
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## Documentation
|
|
124
|
+
|
|
125
|
+
| Topic | File |
|
|
126
|
+
| --- | --- |
|
|
127
|
+
| CLI reference | [docs/cli-reference.md](docs/cli-reference.md) |
|
|
128
|
+
| Python API reference | [docs/python-api.md](docs/python-api.md) |
|
|
129
|
+
| Numbering schemes | [docs/numbering-schemes.md](docs/numbering-schemes.md) |
|
|
130
|
+
| Customising the build | [docs/build-config.md](docs/build-config.md) |
|
|
131
|
+
| Output format (AIRR TSV columns, conformance scope) | [docs/output-format.md](docs/output-format.md) |
|
|
132
|
+
| IgBLAST | [docs/igblast.md](docs/igblast.md) |
|
|
133
|
+
| Score thresholds | [docs/score_threshold_calibration.md](docs/score_threshold_calibration.md) |
|
|
134
|
+
| Performance and benchmarks | [docs/performance.md](docs/performance.md) |
|
|
135
|
+
| How it works | [docs/how_it_works.md](docs/how_it_works.md) |
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## Support
|
|
140
|
+
|
|
141
|
+
GRACER is maintained by Alchemab Therapeutics as time permits, without a guaranteed level of support. We prioritise fixing bugs, especially ones that give wrong numbering or germline calls, and will also try to answer questions, improve the documentation and consider suggested features.
|
|
142
|
+
|
|
143
|
+
- Report a bug, ask a question or suggest a feature by [opening an issue](https://github.com/alchemab/gracer/issues) (preferred). For a bug, include `gracer --version`, the `gracer db info` output, the command and a sequence that shows the problem.
|
|
144
|
+
- [Pull requests](https://github.com/alchemab/gracer/pulls) are welcome; see [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup.
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Citing
|
|
149
|
+
|
|
150
|
+
A GRACER preprint is in preparation. Until it is out, please cite this repository:
|
|
151
|
+
|
|
152
|
+
> Gaspariūnas, A. *et al.* GRACER: Germline-Resolved Adaptive receptor Characterization Engine for Repertoires. <https://github.com/alchemab/gracer>
|
|
153
|
+
|
|
154
|
+
<!-- TODO: replace with the preprint reference and DOI once posted. -->
|
|
155
|
+
|
|
156
|
+
Please also cite the germline databases you built against:
|
|
157
|
+
|
|
158
|
+
- [IMGT/GENE-DB](https://doi.org/10.1093/nar/gki010) (presets `antibody`, `tcr`, `vhh`, `vertebrate`): Giudicelli, V., Chaume, D. & Lefranc, M.-P. (2005). *Nucleic Acids Research* 33, D256–D261.
|
|
159
|
+
- [IMGT/V-QUEST](https://doi.org/10.1093/nar/gkn316) reference directory (presets `vnar`, `vertebrate`): Brochet, X., Lefranc, M.-P. & Giudicelli, V. (2008). *Nucleic Acids Research* 36, W503–W508.
|
|
160
|
+
- [OGRDB](https://doi.org/10.1093/nar/gkaf1094) (`ogrdb` source in a build config): Lees, W.D. *et al.* (2026). *Nucleic Acids Research* 54, D932–D937.
|
|
161
|
+
|
|
162
|
+
and the HMM tools behind both numbering backends and the database build:
|
|
163
|
+
|
|
164
|
+
- [HMMER](https://doi.org/10.1371/journal.pcbi.1002195): Eddy, S.R. (2011). *PLOS Computational Biology* 7, e1002195.
|
|
165
|
+
- [pyHMMER](https://doi.org/10.1093/bioinformatics/btad214): Larralde, M. & Zeller, G. (2023). *Bioinformatics* 39, btad214.
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Acknowledgements
|
|
170
|
+
|
|
171
|
+
Germline sequences are fetched at build time from [OGRDB](https://ogrdb.airr-community.org) (Open Germline Receptor Database) and [IMGT/GENE-DB](https://www.imgt.org/genedb/), the latter via the [JamieHeather/genedb-releases](https://github.com/JamieHeather/genedb-releases) archive maintained by Jamie Heather. The `vnar` and `vertebrate` presets use shark FASTAs from the [IMGT/V-QUEST reference directory](https://www.imgt.org/download/V-QUEST/IMGT_V-QUEST_reference_directory/) via [aretasg/vquest-releases](https://github.com/aretasg/vquest-releases), a companion archive that mirrors V-QUEST weekly so historical releases stay pinnable. As of 1 July 2026, IMGT® is an [ELIXIR Core Data Resource](https://elixir-europe.org/platforms/data/core-data-resources) and its data are provided under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) licence to both public and private users. `gracer db build` prints an attribution reminder for each source it used; see [Citing](#citing) for the references.
|
|
172
|
+
|
|
173
|
+
GRACER began from the code of [ANARCI](https://github.com/oxpig/ANARCI) ([Dunbar & Deane, 2016](https://doi.org/10.1093/bioinformatics/btv552)) at commit [79f6c57](https://github.com/oxpig/ANARCI/commit/79f6c575056dedef86cb8f405ebb039197923eec) and has since been rewritten; no ANARCI code remains. Where the scheme publications leave a choice open (gap orders for CDR lengths they never observed, where an insertion goes), gracer keeps ANARCI's conventions, and ANARCI's numbering remains the reference gracer's is tested against.
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## Licence
|
|
178
|
+
|
|
179
|
+
GRACER is MIT-licensed. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"GracerError",
|
|
3
|
+
"SchemeError",
|
|
4
|
+
"DatabaseError",
|
|
5
|
+
"AlignmentError",
|
|
6
|
+
"WorkerError",
|
|
7
|
+
"GracerWarning",
|
|
8
|
+
"DomainResult",
|
|
9
|
+
"SequenceResult",
|
|
10
|
+
"GracerResults",
|
|
11
|
+
"number",
|
|
12
|
+
"stream",
|
|
13
|
+
"annotate",
|
|
14
|
+
"number_dataframe",
|
|
15
|
+
"renumber",
|
|
16
|
+
"to_dataframe",
|
|
17
|
+
"encode_positions",
|
|
18
|
+
"align_embeddings",
|
|
19
|
+
"available_species",
|
|
20
|
+
"scheme_supports_species",
|
|
21
|
+
"CHAIN_TYPE_TO_LOCUS",
|
|
22
|
+
"LOCUS_TO_CHAIN",
|
|
23
|
+
"get_genedb_dataframe",
|
|
24
|
+
"get_ogrdb_dataframe",
|
|
25
|
+
"build_database",
|
|
26
|
+
"export_lockfile",
|
|
27
|
+
"init_build_config",
|
|
28
|
+
"get_database_info",
|
|
29
|
+
"list_ogrdb_available",
|
|
30
|
+
"list_genedb_available",
|
|
31
|
+
"list_vquest_available",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
from .exceptions import (
|
|
35
|
+
AlignmentError,
|
|
36
|
+
DatabaseError,
|
|
37
|
+
GracerError,
|
|
38
|
+
GracerWarning,
|
|
39
|
+
SchemeError,
|
|
40
|
+
WorkerError,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Typed for mypy and IDEs without importing anything at runtime: `import
|
|
44
|
+
# gracer` stays cheap (no pyhmmer, numba or germline JSON) until a name is
|
|
45
|
+
# used. A local constant rather than typing.TYPE_CHECKING keeps typing itself
|
|
46
|
+
# out of the import. Every name here must also be in _LAZY_MODULES;
|
|
47
|
+
# tests/test_public_api.py checks the two agree.
|
|
48
|
+
TYPE_CHECKING = False
|
|
49
|
+
if TYPE_CHECKING:
|
|
50
|
+
from .germline_db.builder.lockfile import export_lockfile
|
|
51
|
+
from .germline_db.builder.pipeline import build_database, get_database_info, init_build_config
|
|
52
|
+
from .germline_db.sources.genedb import get_genedb_dataframe
|
|
53
|
+
from .germline_db.sources.list_available import (
|
|
54
|
+
list_genedb_available,
|
|
55
|
+
list_ogrdb_available,
|
|
56
|
+
list_vquest_available,
|
|
57
|
+
)
|
|
58
|
+
from .germline_db.sources.ogrdb import get_ogrdb_dataframe
|
|
59
|
+
from .loci import CHAIN_TYPE_TO_LOCUS, LOCUS_TO_CHAIN
|
|
60
|
+
from .models import DomainResult, GracerResults, SequenceResult
|
|
61
|
+
from .output.encoding import align_embeddings, encode_positions
|
|
62
|
+
from .output.writers import to_dataframe
|
|
63
|
+
from .runner.core import number, stream
|
|
64
|
+
from .runner.domains import available_species, scheme_supports_species
|
|
65
|
+
from .runner.tables import annotate, number_dataframe
|
|
66
|
+
from .structure import renumber
|
|
67
|
+
|
|
68
|
+
# Where each lazily exported name is defined, relative to this package.
|
|
69
|
+
_LAZY_MODULES: dict[str, tuple[str, ...]] = {
|
|
70
|
+
".models": ("DomainResult", "SequenceResult", "GracerResults"),
|
|
71
|
+
".runner.core": ("number", "stream"),
|
|
72
|
+
".runner.tables": ("annotate", "number_dataframe"),
|
|
73
|
+
".output.writers": ("to_dataframe",),
|
|
74
|
+
".structure": ("renumber",),
|
|
75
|
+
".output.encoding": ("encode_positions", "align_embeddings"),
|
|
76
|
+
".runner.domains": ("available_species", "scheme_supports_species"),
|
|
77
|
+
".loci": ("CHAIN_TYPE_TO_LOCUS", "LOCUS_TO_CHAIN"),
|
|
78
|
+
".germline_db.sources.genedb": ("get_genedb_dataframe",),
|
|
79
|
+
".germline_db.sources.ogrdb": ("get_ogrdb_dataframe",),
|
|
80
|
+
".germline_db.builder.lockfile": ("export_lockfile",),
|
|
81
|
+
".germline_db.builder.pipeline": ("build_database", "get_database_info", "init_build_config"),
|
|
82
|
+
".germline_db.sources.list_available": (
|
|
83
|
+
"list_ogrdb_available",
|
|
84
|
+
"list_genedb_available",
|
|
85
|
+
"list_vquest_available",
|
|
86
|
+
),
|
|
87
|
+
}
|
|
88
|
+
_LAZY = {name: module for module, names in _LAZY_MODULES.items() for name in names}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def __getattr__(name: str) -> object:
|
|
92
|
+
if name == "__version__":
|
|
93
|
+
from gracer._version import gracer_version
|
|
94
|
+
|
|
95
|
+
globals()["__version__"] = gracer_version()
|
|
96
|
+
return globals()["__version__"]
|
|
97
|
+
if name in _LAZY:
|
|
98
|
+
from importlib import import_module
|
|
99
|
+
|
|
100
|
+
value = getattr(import_module(_LAZY[name], __name__), name)
|
|
101
|
+
globals()[name] = value
|
|
102
|
+
return value
|
|
103
|
+
raise AttributeError(f"module 'gracer' has no attribute {name!r}")
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""``python -m gracer``: the gracer command line, as the ``gracer`` script runs it.
|
|
2
|
+
|
|
3
|
+
The one module outside ``gracer/cli/`` that imports it: this is the command
|
|
4
|
+
line's own entry point, not library code that would come to depend on it.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from gracer.cli.__main__ import main
|
|
8
|
+
|
|
9
|
+
if __name__ == "__main__":
|
|
10
|
+
main()
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Running NCBI BLAST+ / IgBLAST tools without trusting the working directory.
|
|
2
|
+
|
|
3
|
+
The NCBI toolkit looks for its configuration (``<tool>.ini``, then
|
|
4
|
+
``.ncbirc``) in ``.`` before anywhere else (``CMetaRegistry::
|
|
5
|
+
GetDefaultSearchPath``), and IgBLAST reads ``internal_data`` from ``.``
|
|
6
|
+
before ``$IGDATA``. A config planted in the directory gracer runs from can
|
|
7
|
+
log the environment, secrets included, to a file of the planter's choosing;
|
|
8
|
+
a planted ``internal_data`` shifts IgBLAST's region calls. :func:`run` starts
|
|
9
|
+
every tool in a private, empty temporary directory, with only the
|
|
10
|
+
environment the tools need, and :func:`find_tool` skips relative ``PATH``
|
|
11
|
+
entries (an empty one is the cwd).
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import contextlib
|
|
15
|
+
import os
|
|
16
|
+
import shutil
|
|
17
|
+
import subprocess
|
|
18
|
+
import tempfile
|
|
19
|
+
from collections.abc import Sequence
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
# What the tools read from the environment. IGDATA locates IgBLAST's
|
|
23
|
+
# internal_data and optional_file (bioconda's wrappers set it); NCBI and
|
|
24
|
+
# NCBI_CONFIG_PATH are where a user keeps their own config. Everything else,
|
|
25
|
+
# tokens and cloud credentials included, stays out of reach of a config's
|
|
26
|
+
# LogEnvironment.
|
|
27
|
+
_KEPT_ENV = ("PATH", "HOME", "TMPDIR", "LANG", "IGDATA", "NCBI", "NCBI_CONFIG_PATH")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _search_path() -> str:
|
|
31
|
+
return os.pathsep.join(
|
|
32
|
+
p for p in os.environ.get("PATH", os.defpath).split(os.pathsep) if os.path.isabs(p)
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def find_tool(name: str) -> str | None:
|
|
37
|
+
"""Absolute path of *name* on ``PATH``, ignoring relative entries."""
|
|
38
|
+
return shutil.which(name, path=_search_path())
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _env() -> dict[str, str]:
|
|
42
|
+
env = {k: v for k, v in os.environ.items() if k in _KEPT_ENV or k.startswith("LC_")}
|
|
43
|
+
env["PATH"] = _search_path()
|
|
44
|
+
return env
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def run(cmd: Sequence[str | Path], cwd: Path | None = None) -> "subprocess.CompletedProcess[str]":
|
|
48
|
+
"""Run an NCBI tool from a private empty directory, capturing its output.
|
|
49
|
+
|
|
50
|
+
``cmd[0]`` must be an absolute path (from :func:`find_tool`). Every
|
|
51
|
+
``Path`` argument is made absolute against the caller's cwd before the
|
|
52
|
+
tool starts elsewhere, so pass file arguments as ``Path``, never ``str``.
|
|
53
|
+
|
|
54
|
+
*cwd* runs the tool in a directory of gracer's own making instead (one
|
|
55
|
+
nothing else can write to), for a ``str`` argument naming a file in it:
|
|
56
|
+
an alias database records such a name as given, and BLAST resolves it
|
|
57
|
+
from the alias file's own directory, wherever that is reached from.
|
|
58
|
+
"""
|
|
59
|
+
tool = str(cmd[0])
|
|
60
|
+
if not os.path.isabs(tool):
|
|
61
|
+
raise ValueError(f"NCBI tool path must be absolute, got {tool!r}")
|
|
62
|
+
with contextlib.ExitStack() as stack:
|
|
63
|
+
where = cwd or Path(stack.enter_context(tempfile.TemporaryDirectory(prefix="gracer_ncbi_")))
|
|
64
|
+
args = [tool] + [_argument(a, where) for a in cmd[1:]]
|
|
65
|
+
return subprocess.run(args, cwd=where, env=_env(), capture_output=True, text=True)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _argument(arg: str | Path, cwd: Path) -> str:
|
|
69
|
+
"""*arg* as the tool should see it, from *cwd*.
|
|
70
|
+
|
|
71
|
+
BLAST splits database names on whitespace (several databases can be given
|
|
72
|
+
in one argument), even within one argv entry, so a path with a space in a
|
|
73
|
+
directory name is reached through a link in *cwd* instead: a relative
|
|
74
|
+
database name is looked up from the working directory first."""
|
|
75
|
+
if not isinstance(arg, Path):
|
|
76
|
+
return arg
|
|
77
|
+
path = arg.absolute()
|
|
78
|
+
if not any(c.isspace() for c in str(path.parent)):
|
|
79
|
+
return str(path)
|
|
80
|
+
# One link per directory: igblastn crashes when one database is reached
|
|
81
|
+
# under two names (one passed as two segments' database, say).
|
|
82
|
+
for link in cwd.iterdir():
|
|
83
|
+
if link.is_symlink() and link.readlink() == path.parent:
|
|
84
|
+
break
|
|
85
|
+
else:
|
|
86
|
+
link = cwd / f"p{len(list(cwd.iterdir()))}"
|
|
87
|
+
link.symlink_to(path.parent, target_is_directory=True)
|
|
88
|
+
return f"{link.name}/{path.name}"
|