seqmat 1.5.0__tar.gz → 1.5.1__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.
- seqmat-1.5.1/PKG-INFO +278 -0
- seqmat-1.5.1/README.md +218 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/pyproject.toml +2 -1
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/__init__.py +1 -1
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/gene.py +48 -42
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/seqmat.py +191 -90
- seqmat-1.5.1/seqmat.egg-info/PKG-INFO +278 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat.egg-info/requires.txt +1 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/tests/test_gene.py +27 -3
- {seqmat-1.5.0 → seqmat-1.5.1}/tests/test_seqmat.py +31 -0
- seqmat-1.5.0/PKG-INFO +0 -225
- seqmat-1.5.0/README.md +0 -166
- seqmat-1.5.0/seqmat.egg-info/PKG-INFO +0 -225
- {seqmat-1.5.0 → seqmat-1.5.1}/LICENSE +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/MANIFEST.in +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/examples/basic_usage.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/examples/data_inspection.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/requirements.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/_io.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/cli.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/config.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/conservation.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/data_setup.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/discovery.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/download.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/errors.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/gtf.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/lmdb_store.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/locator.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/sqlite_store.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/transcript.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat/utils.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat.egg-info/SOURCES.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat.egg-info/dependency_links.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat.egg-info/entry_points.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/seqmat.egg-info/top_level.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/setup.cfg +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/tests/__init__.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/tests/bench_gene_creation.py +0 -0
- {seqmat-1.5.0 → seqmat-1.5.1}/tests/test_locator.py +0 -0
seqmat-1.5.1/PKG-INFO
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: seqmat
|
|
3
|
+
Version: 1.5.1
|
|
4
|
+
Summary: Fast, vectorized genomic sequences with first-class mutation tracking.
|
|
5
|
+
Author-email: Nicolas Lynn Vila <nicolasalynn@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 SeqMat Contributors
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
Project-URL: Homepage, https://github.com/nicolasalynn/seqmat
|
|
28
|
+
Project-URL: Repository, https://github.com/nicolasalynn/seqmat
|
|
29
|
+
Project-URL: Issues, https://github.com/nicolasalynn/seqmat/issues
|
|
30
|
+
Project-URL: Changelog, https://github.com/nicolasalynn/seqmat/blob/main/CHANGELOG.md
|
|
31
|
+
Keywords: bioinformatics,genomics,sequence,mutation,splicing,transcript,hg38,mm39
|
|
32
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
33
|
+
Classifier: Intended Audience :: Science/Research
|
|
34
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Programming Language :: Python :: 3
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Operating System :: OS Independent
|
|
41
|
+
Requires-Python: >=3.10
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
License-File: LICENSE
|
|
44
|
+
Requires-Dist: numpy<3,>=1.20.0
|
|
45
|
+
Requires-Dist: pandas<3,>=2.2.0
|
|
46
|
+
Requires-Dist: pyarrow>=15.0.0
|
|
47
|
+
Requires-Dist: pysam>=0.19.0
|
|
48
|
+
Requires-Dist: requests>=2.26.0
|
|
49
|
+
Requires-Dist: tqdm>=4.62.0
|
|
50
|
+
Requires-Dist: platformdirs>=3.0.0
|
|
51
|
+
Provides-Extra: lmdb
|
|
52
|
+
Requires-Dist: lmdb>=1.4.0; extra == "lmdb"
|
|
53
|
+
Provides-Extra: dev
|
|
54
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
55
|
+
Requires-Dist: pytest-cov>=2.0; extra == "dev"
|
|
56
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
57
|
+
Requires-Dist: twine>=4.0; extra == "dev"
|
|
58
|
+
Requires-Dist: pdoc>=14.0; extra == "dev"
|
|
59
|
+
Dynamic: license-file
|
|
60
|
+
|
|
61
|
+
<p align="center">
|
|
62
|
+
<img src="docs/assets/logo.svg" alt="SeqMat" width="540">
|
|
63
|
+
</p>
|
|
64
|
+
|
|
65
|
+
[](https://pypi.org/project/seqmat/)
|
|
66
|
+
[](https://pypi.org/project/seqmat/)
|
|
67
|
+
[](https://github.com/nicolasalynn/seqmat/actions/workflows/test.yml)
|
|
68
|
+
[](https://codecov.io/gh/nicolasalynn/seqmat)
|
|
69
|
+
[](LICENSE)
|
|
70
|
+
[](https://pepy.tech/project/seqmat)
|
|
71
|
+
|
|
72
|
+
A Python library for genomic sequences that keeps genomic coordinates and mutation history attached to the bases.
|
|
73
|
+
|
|
74
|
+
SeqMat stores a DNA sequence as a NumPy structured array — `(nt, ref, index, mut_type, valid)` in parallel columns rather than a byte string. Slicing, mutation, complement, and splicing preserve genomic coordinates and the reference state through every transform, and the mutation history is recorded as a side product. The library also includes a gene/transcript model loaded from a single SQLite file built from Ensembl annotations, and a position → gene lookup over a sidecar index.
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from seqmat import Gene, SeqMat
|
|
78
|
+
|
|
79
|
+
# What gene is at chr12:25,245,350?
|
|
80
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)]
|
|
81
|
+
|
|
82
|
+
# Load it, assemble the mature mRNA, translate.
|
|
83
|
+
kras = Gene.from_file("KRAS")
|
|
84
|
+
tx = kras.transcript()
|
|
85
|
+
tx.generate_mature_mrna()
|
|
86
|
+
tx.generate_protein()
|
|
87
|
+
|
|
88
|
+
# Apply mutations; history is kept on the SeqMat.
|
|
89
|
+
seq = SeqMat("ATCGATCGATCG")
|
|
90
|
+
seq.apply_mutations([(3, "C", "G"), (6, "-", "AAA"), (10, "TC", "-")])
|
|
91
|
+
seq.mutations
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
A worked example, end to end: [KRAS G12D — coordinate to protein](examples/kras_g12d_analysis.ipynb).
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Install
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pip install seqmat
|
|
102
|
+
seqmat setup # one-time: downloads hg38 genes.db + FASTA (~4 GB)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`seqmat setup` appends `SEQMAT_DATA_DIR` and `SEQMAT_DEFAULT_ORGANISM` to your shell rc so the data is found automatically next session. Mouse: `seqmat setup --organism mm39`.
|
|
106
|
+
|
|
107
|
+
If you only want `SeqMat` for in-memory sequence work, you can skip `seqmat setup` — `Gene` and `Transcript` are the only objects that need the gene database.
|
|
108
|
+
|
|
109
|
+
## Quick start
|
|
110
|
+
|
|
111
|
+
### Sequence operations
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from seqmat import SeqMat
|
|
115
|
+
import numpy as np
|
|
116
|
+
|
|
117
|
+
seq = SeqMat("ATCGATCGATCG", indices=np.arange(1000, 1012))
|
|
118
|
+
seq[1005] # base at genomic position 1005
|
|
119
|
+
seq[1003:1008].seq # "GATCG"
|
|
120
|
+
seq.reverse_complement() # in place
|
|
121
|
+
seq.remove_regions([(1003, 1005), (1008, 1009)]) # splice out introns
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Genes and transcripts
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from seqmat import Gene
|
|
128
|
+
|
|
129
|
+
kras = Gene.from_file("KRAS")
|
|
130
|
+
kras # Gene: KRAS, ID: ENSG00000133703, Chr: 12, Transcripts: 14
|
|
131
|
+
|
|
132
|
+
tx = kras.transcript() # primary transcript
|
|
133
|
+
tx.generate_mature_mrna()
|
|
134
|
+
tx.generate_protein()
|
|
135
|
+
tx.protein[:20] # 'MTEYKLVVVGAGGVGKSALT'
|
|
136
|
+
|
|
137
|
+
acceptors, donors = kras.splice_sites() # Counter across all transcripts
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Position → gene (added in 1.4.0)
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from seqmat import Gene, gene_names_at_position
|
|
144
|
+
|
|
145
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)] — point query
|
|
146
|
+
Gene.from_position("chr12", (25_200_000, 25_300_000)) # all overlapping genes in a range
|
|
147
|
+
gene_names_at_position("X", 100_000) # names only (no BLOB load)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Backed by a per-chromosome sorted NumPy index persisted as a sidecar `gene_locations.npz` next to `genes.db`. Built lazily on first call; fresh `seqmat setup` builds also emit it.
|
|
151
|
+
|
|
152
|
+
### Loading from FASTA
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
seq = SeqMat.from_fasta_file("chr12.fasta", "chr12", start=25398284, end=25398384)
|
|
156
|
+
seq.apply_mutations([(25398290, "G", "A")]) # G12D, a common KRAS variant
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Performance
|
|
160
|
+
|
|
161
|
+
Measured on an M-series Mac, hg38, one core, warm caches. Reproducible via the scripts in `benchmarks/`.
|
|
162
|
+
|
|
163
|
+
### Headline numbers
|
|
164
|
+
|
|
165
|
+
| Operation | Time |
|
|
166
|
+
| -------------------------------------------------- | -------: |
|
|
167
|
+
| `gene_names_at_position(chrm, pos)` | 2.5 µs |
|
|
168
|
+
| KRAS mature mRNA assembly (splice + translate) | 0.2 ms |
|
|
169
|
+
| 1,000-SNP batch on 45 kb sequence (with history) | 0.5 ms |
|
|
170
|
+
| `Gene.from_file("KRAS")` (SQLite + unpickle) | 24 ms |
|
|
171
|
+
| `Gene.from_position(chrm, pos)` end-to-end | 24 ms |
|
|
172
|
+
|
|
173
|
+
### Position → gene
|
|
174
|
+
|
|
175
|
+
Same 63,241 hg38 gene intervals, same 10,000 random point queries. Different libraries are designed for different access patterns, so we report two workloads. Reproduce with `python benchmarks/bench_position_lookup.py`.
|
|
176
|
+
|
|
177
|
+
Per-query (one coordinate, one answer, in a loop — the `Gene.from_position` pattern):
|
|
178
|
+
|
|
179
|
+
| Implementation | Per query |
|
|
180
|
+
| --------------------------------------- | --------: |
|
|
181
|
+
| SeqMat (locator) | 2.5 µs |
|
|
182
|
+
| Python `dict` + `bisect` | 21 µs |
|
|
183
|
+
| pandas (`groupby` chrm + boolean mask) | 79 µs |
|
|
184
|
+
| PyRanges constructed per call | 2.0 ms |
|
|
185
|
+
|
|
186
|
+
Batched (the whole query set in one call — PyRanges' native idiom):
|
|
187
|
+
|
|
188
|
+
| Implementation | Per query |
|
|
189
|
+
| ----------------------------- | --------: |
|
|
190
|
+
| PyRanges `.join` | 2.07 µs |
|
|
191
|
+
| SeqMat (serial loop) | 2.59 µs |
|
|
192
|
+
|
|
193
|
+
PyRanges and SeqMat are in roughly the same range when each is used the way it was designed for. Constructing a `PyRanges` per call is the slow path and not the PyRanges authors' intent — included here only because it's a common mistake.
|
|
194
|
+
|
|
195
|
+
### Sequence operations vs Biopython
|
|
196
|
+
|
|
197
|
+
50 kb sequence, 1,000 SNPs / 10 introns; 1 Mb reverse-complement. Reproduce with `python benchmarks/bench_sequence_ops.py`.
|
|
198
|
+
|
|
199
|
+
| Workload | Biopython / native | SeqMat |
|
|
200
|
+
| ---------------------------------------------- | ------------------------------: | ------: |
|
|
201
|
+
| 1,000 SNPs (no history) | `MutableSeq`: 0.21 ms | — |
|
|
202
|
+
| 1,000 SNPs with full mutation history | — | 0.5 ms |
|
|
203
|
+
| 10-intron splice (50 kb) | `str.join`: 0.002 ms | 0.8 ms |
|
|
204
|
+
| Reverse-complement (1 Mb) | `Seq.reverse_complement`: 0.57 ms | 9.4 ms |
|
|
205
|
+
|
|
206
|
+
For raw byte-string throughput, `Bio.Seq` and `bytearray` are faster. SeqMat's structured array adds bookkeeping that those types don't carry — genomic coordinates that survive indels, the reference held alongside the current sequence, and a mutation history list. The trade-off makes sense when you need that bookkeeping anyway (the KRAS G12D walkthrough is a typical case) and doesn't when you don't.
|
|
207
|
+
|
|
208
|
+
### Implementation notes
|
|
209
|
+
|
|
210
|
+
NumPy structured arrays, LUT-based complement, `np.searchsorted` on sorted exon and gene-start coordinates, FASTA range-scoped reads, copy-on-write `clone()`. See [`seqmat/seqmat.py`](seqmat/seqmat.py) and [`seqmat/locator.py`](seqmat/locator.py).
|
|
211
|
+
|
|
212
|
+
## Command-line interface
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
seqmat setup [--organism hg38|mm39] [--path PATH] [--build-from-sources]
|
|
216
|
+
seqmat summary
|
|
217
|
+
seqmat info --organism hg38
|
|
218
|
+
seqmat search --organism hg38 --query KRAS
|
|
219
|
+
seqmat list --organism hg38 --biotype protein_coding --limit 20
|
|
220
|
+
seqmat count --organism hg38
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Data setup
|
|
224
|
+
|
|
225
|
+
By default `seqmat setup` downloads prebuilt `genes.db` and FASTA from the SeqMat S3 bucket. To regenerate `genes.db` from a specific Ensembl release or a custom GTF:
|
|
226
|
+
|
|
227
|
+
```bash
|
|
228
|
+
seqmat setup --organism hg38 --build-from-sources
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Custom organisms, mirror buckets, ephemeral environments (Docker / Run.ai), shared multi-user installs, and the full configuration system are documented in [docs/SETUP.md](docs/SETUP.md).
|
|
232
|
+
|
|
233
|
+
## API at a glance
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
from seqmat import (
|
|
237
|
+
SeqMat, # coordinate-tracked sequence with mutation history
|
|
238
|
+
Gene, Transcript, # gene/transcript model
|
|
239
|
+
gene_names_at_position, # name-only positional lookup
|
|
240
|
+
build_location_index, # force-rebuild the position index
|
|
241
|
+
setup_genomics_data, # programmatic setup
|
|
242
|
+
search_genes, available_genes, # discovery helpers
|
|
243
|
+
)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Key classes:
|
|
247
|
+
|
|
248
|
+
- `SeqMat` — `apply_mutations`, `clone`, `complement`, `reverse_complement`, `remove_regions`, `from_fasta_file`
|
|
249
|
+
- `Gene` — `from_file`, `from_position`, `get`, `transcript`, `splice_sites`, `primary_transcript`
|
|
250
|
+
- `Transcript` — `generate_pre_mrna`, `generate_mature_mrna`, `generate_protein`, `exons`, `introns`
|
|
251
|
+
|
|
252
|
+
Full API reference (auto-generated from docstrings): [nicolasalynn.github.io/seqmat](https://nicolasalynn.github.io/seqmat/seqmat.html).
|
|
253
|
+
|
|
254
|
+
## Requirements
|
|
255
|
+
|
|
256
|
+
Python ≥ 3.10. Core deps: `numpy`, `pandas`, `pyarrow`, `pysam`, `requests`, `tqdm`, `platformdirs`. Optional: `lmdb` for faster gene loading on large workloads (`pip install seqmat[lmdb]`).
|
|
257
|
+
|
|
258
|
+
## A note on indexing
|
|
259
|
+
|
|
260
|
+
`SeqMat` defaults to 1-based indices to match genomic conventions (UCSC, GenBank). If you'd rather work in 0-based offsets — e.g. for a comparison with `bytearray` or `Bio.Seq` — pass `indices=np.arange(len(seq))` at construction.
|
|
261
|
+
|
|
262
|
+
## Contributing
|
|
263
|
+
|
|
264
|
+
PRs welcome. Run the test suite with `pytest tests/`. Comparative benchmarks are under `benchmarks/`.
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
MIT — see [LICENSE](LICENSE).
|
|
269
|
+
|
|
270
|
+
## Citation
|
|
271
|
+
|
|
272
|
+
If SeqMat is useful in your research:
|
|
273
|
+
|
|
274
|
+
```
|
|
275
|
+
Lynn Vila, N. (2025). SeqMat: a genomic sequence library with
|
|
276
|
+
coordinate tracking and mutation history.
|
|
277
|
+
https://github.com/nicolasalynn/seqmat
|
|
278
|
+
```
|
seqmat-1.5.1/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="docs/assets/logo.svg" alt="SeqMat" width="540">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
[](https://pypi.org/project/seqmat/)
|
|
6
|
+
[](https://pypi.org/project/seqmat/)
|
|
7
|
+
[](https://github.com/nicolasalynn/seqmat/actions/workflows/test.yml)
|
|
8
|
+
[](https://codecov.io/gh/nicolasalynn/seqmat)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
[](https://pepy.tech/project/seqmat)
|
|
11
|
+
|
|
12
|
+
A Python library for genomic sequences that keeps genomic coordinates and mutation history attached to the bases.
|
|
13
|
+
|
|
14
|
+
SeqMat stores a DNA sequence as a NumPy structured array — `(nt, ref, index, mut_type, valid)` in parallel columns rather than a byte string. Slicing, mutation, complement, and splicing preserve genomic coordinates and the reference state through every transform, and the mutation history is recorded as a side product. The library also includes a gene/transcript model loaded from a single SQLite file built from Ensembl annotations, and a position → gene lookup over a sidecar index.
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from seqmat import Gene, SeqMat
|
|
18
|
+
|
|
19
|
+
# What gene is at chr12:25,245,350?
|
|
20
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)]
|
|
21
|
+
|
|
22
|
+
# Load it, assemble the mature mRNA, translate.
|
|
23
|
+
kras = Gene.from_file("KRAS")
|
|
24
|
+
tx = kras.transcript()
|
|
25
|
+
tx.generate_mature_mrna()
|
|
26
|
+
tx.generate_protein()
|
|
27
|
+
|
|
28
|
+
# Apply mutations; history is kept on the SeqMat.
|
|
29
|
+
seq = SeqMat("ATCGATCGATCG")
|
|
30
|
+
seq.apply_mutations([(3, "C", "G"), (6, "-", "AAA"), (10, "TC", "-")])
|
|
31
|
+
seq.mutations
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
A worked example, end to end: [KRAS G12D — coordinate to protein](examples/kras_g12d_analysis.ipynb).
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install seqmat
|
|
42
|
+
seqmat setup # one-time: downloads hg38 genes.db + FASTA (~4 GB)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`seqmat setup` appends `SEQMAT_DATA_DIR` and `SEQMAT_DEFAULT_ORGANISM` to your shell rc so the data is found automatically next session. Mouse: `seqmat setup --organism mm39`.
|
|
46
|
+
|
|
47
|
+
If you only want `SeqMat` for in-memory sequence work, you can skip `seqmat setup` — `Gene` and `Transcript` are the only objects that need the gene database.
|
|
48
|
+
|
|
49
|
+
## Quick start
|
|
50
|
+
|
|
51
|
+
### Sequence operations
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from seqmat import SeqMat
|
|
55
|
+
import numpy as np
|
|
56
|
+
|
|
57
|
+
seq = SeqMat("ATCGATCGATCG", indices=np.arange(1000, 1012))
|
|
58
|
+
seq[1005] # base at genomic position 1005
|
|
59
|
+
seq[1003:1008].seq # "GATCG"
|
|
60
|
+
seq.reverse_complement() # in place
|
|
61
|
+
seq.remove_regions([(1003, 1005), (1008, 1009)]) # splice out introns
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Genes and transcripts
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from seqmat import Gene
|
|
68
|
+
|
|
69
|
+
kras = Gene.from_file("KRAS")
|
|
70
|
+
kras # Gene: KRAS, ID: ENSG00000133703, Chr: 12, Transcripts: 14
|
|
71
|
+
|
|
72
|
+
tx = kras.transcript() # primary transcript
|
|
73
|
+
tx.generate_mature_mrna()
|
|
74
|
+
tx.generate_protein()
|
|
75
|
+
tx.protein[:20] # 'MTEYKLVVVGAGGVGKSALT'
|
|
76
|
+
|
|
77
|
+
acceptors, donors = kras.splice_sites() # Counter across all transcripts
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Position → gene (added in 1.4.0)
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from seqmat import Gene, gene_names_at_position
|
|
84
|
+
|
|
85
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)] — point query
|
|
86
|
+
Gene.from_position("chr12", (25_200_000, 25_300_000)) # all overlapping genes in a range
|
|
87
|
+
gene_names_at_position("X", 100_000) # names only (no BLOB load)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Backed by a per-chromosome sorted NumPy index persisted as a sidecar `gene_locations.npz` next to `genes.db`. Built lazily on first call; fresh `seqmat setup` builds also emit it.
|
|
91
|
+
|
|
92
|
+
### Loading from FASTA
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
seq = SeqMat.from_fasta_file("chr12.fasta", "chr12", start=25398284, end=25398384)
|
|
96
|
+
seq.apply_mutations([(25398290, "G", "A")]) # G12D, a common KRAS variant
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Performance
|
|
100
|
+
|
|
101
|
+
Measured on an M-series Mac, hg38, one core, warm caches. Reproducible via the scripts in `benchmarks/`.
|
|
102
|
+
|
|
103
|
+
### Headline numbers
|
|
104
|
+
|
|
105
|
+
| Operation | Time |
|
|
106
|
+
| -------------------------------------------------- | -------: |
|
|
107
|
+
| `gene_names_at_position(chrm, pos)` | 2.5 µs |
|
|
108
|
+
| KRAS mature mRNA assembly (splice + translate) | 0.2 ms |
|
|
109
|
+
| 1,000-SNP batch on 45 kb sequence (with history) | 0.5 ms |
|
|
110
|
+
| `Gene.from_file("KRAS")` (SQLite + unpickle) | 24 ms |
|
|
111
|
+
| `Gene.from_position(chrm, pos)` end-to-end | 24 ms |
|
|
112
|
+
|
|
113
|
+
### Position → gene
|
|
114
|
+
|
|
115
|
+
Same 63,241 hg38 gene intervals, same 10,000 random point queries. Different libraries are designed for different access patterns, so we report two workloads. Reproduce with `python benchmarks/bench_position_lookup.py`.
|
|
116
|
+
|
|
117
|
+
Per-query (one coordinate, one answer, in a loop — the `Gene.from_position` pattern):
|
|
118
|
+
|
|
119
|
+
| Implementation | Per query |
|
|
120
|
+
| --------------------------------------- | --------: |
|
|
121
|
+
| SeqMat (locator) | 2.5 µs |
|
|
122
|
+
| Python `dict` + `bisect` | 21 µs |
|
|
123
|
+
| pandas (`groupby` chrm + boolean mask) | 79 µs |
|
|
124
|
+
| PyRanges constructed per call | 2.0 ms |
|
|
125
|
+
|
|
126
|
+
Batched (the whole query set in one call — PyRanges' native idiom):
|
|
127
|
+
|
|
128
|
+
| Implementation | Per query |
|
|
129
|
+
| ----------------------------- | --------: |
|
|
130
|
+
| PyRanges `.join` | 2.07 µs |
|
|
131
|
+
| SeqMat (serial loop) | 2.59 µs |
|
|
132
|
+
|
|
133
|
+
PyRanges and SeqMat are in roughly the same range when each is used the way it was designed for. Constructing a `PyRanges` per call is the slow path and not the PyRanges authors' intent — included here only because it's a common mistake.
|
|
134
|
+
|
|
135
|
+
### Sequence operations vs Biopython
|
|
136
|
+
|
|
137
|
+
50 kb sequence, 1,000 SNPs / 10 introns; 1 Mb reverse-complement. Reproduce with `python benchmarks/bench_sequence_ops.py`.
|
|
138
|
+
|
|
139
|
+
| Workload | Biopython / native | SeqMat |
|
|
140
|
+
| ---------------------------------------------- | ------------------------------: | ------: |
|
|
141
|
+
| 1,000 SNPs (no history) | `MutableSeq`: 0.21 ms | — |
|
|
142
|
+
| 1,000 SNPs with full mutation history | — | 0.5 ms |
|
|
143
|
+
| 10-intron splice (50 kb) | `str.join`: 0.002 ms | 0.8 ms |
|
|
144
|
+
| Reverse-complement (1 Mb) | `Seq.reverse_complement`: 0.57 ms | 9.4 ms |
|
|
145
|
+
|
|
146
|
+
For raw byte-string throughput, `Bio.Seq` and `bytearray` are faster. SeqMat's structured array adds bookkeeping that those types don't carry — genomic coordinates that survive indels, the reference held alongside the current sequence, and a mutation history list. The trade-off makes sense when you need that bookkeeping anyway (the KRAS G12D walkthrough is a typical case) and doesn't when you don't.
|
|
147
|
+
|
|
148
|
+
### Implementation notes
|
|
149
|
+
|
|
150
|
+
NumPy structured arrays, LUT-based complement, `np.searchsorted` on sorted exon and gene-start coordinates, FASTA range-scoped reads, copy-on-write `clone()`. See [`seqmat/seqmat.py`](seqmat/seqmat.py) and [`seqmat/locator.py`](seqmat/locator.py).
|
|
151
|
+
|
|
152
|
+
## Command-line interface
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
seqmat setup [--organism hg38|mm39] [--path PATH] [--build-from-sources]
|
|
156
|
+
seqmat summary
|
|
157
|
+
seqmat info --organism hg38
|
|
158
|
+
seqmat search --organism hg38 --query KRAS
|
|
159
|
+
seqmat list --organism hg38 --biotype protein_coding --limit 20
|
|
160
|
+
seqmat count --organism hg38
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Data setup
|
|
164
|
+
|
|
165
|
+
By default `seqmat setup` downloads prebuilt `genes.db` and FASTA from the SeqMat S3 bucket. To regenerate `genes.db` from a specific Ensembl release or a custom GTF:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
seqmat setup --organism hg38 --build-from-sources
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Custom organisms, mirror buckets, ephemeral environments (Docker / Run.ai), shared multi-user installs, and the full configuration system are documented in [docs/SETUP.md](docs/SETUP.md).
|
|
172
|
+
|
|
173
|
+
## API at a glance
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from seqmat import (
|
|
177
|
+
SeqMat, # coordinate-tracked sequence with mutation history
|
|
178
|
+
Gene, Transcript, # gene/transcript model
|
|
179
|
+
gene_names_at_position, # name-only positional lookup
|
|
180
|
+
build_location_index, # force-rebuild the position index
|
|
181
|
+
setup_genomics_data, # programmatic setup
|
|
182
|
+
search_genes, available_genes, # discovery helpers
|
|
183
|
+
)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Key classes:
|
|
187
|
+
|
|
188
|
+
- `SeqMat` — `apply_mutations`, `clone`, `complement`, `reverse_complement`, `remove_regions`, `from_fasta_file`
|
|
189
|
+
- `Gene` — `from_file`, `from_position`, `get`, `transcript`, `splice_sites`, `primary_transcript`
|
|
190
|
+
- `Transcript` — `generate_pre_mrna`, `generate_mature_mrna`, `generate_protein`, `exons`, `introns`
|
|
191
|
+
|
|
192
|
+
Full API reference (auto-generated from docstrings): [nicolasalynn.github.io/seqmat](https://nicolasalynn.github.io/seqmat/seqmat.html).
|
|
193
|
+
|
|
194
|
+
## Requirements
|
|
195
|
+
|
|
196
|
+
Python ≥ 3.10. Core deps: `numpy`, `pandas`, `pyarrow`, `pysam`, `requests`, `tqdm`, `platformdirs`. Optional: `lmdb` for faster gene loading on large workloads (`pip install seqmat[lmdb]`).
|
|
197
|
+
|
|
198
|
+
## A note on indexing
|
|
199
|
+
|
|
200
|
+
`SeqMat` defaults to 1-based indices to match genomic conventions (UCSC, GenBank). If you'd rather work in 0-based offsets — e.g. for a comparison with `bytearray` or `Bio.Seq` — pass `indices=np.arange(len(seq))` at construction.
|
|
201
|
+
|
|
202
|
+
## Contributing
|
|
203
|
+
|
|
204
|
+
PRs welcome. Run the test suite with `pytest tests/`. Comparative benchmarks are under `benchmarks/`.
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
208
|
+
MIT — see [LICENSE](LICENSE).
|
|
209
|
+
|
|
210
|
+
## Citation
|
|
211
|
+
|
|
212
|
+
If SeqMat is useful in your research:
|
|
213
|
+
|
|
214
|
+
```
|
|
215
|
+
Lynn Vila, N. (2025). SeqMat: a genomic sequence library with
|
|
216
|
+
coordinate tracking and mutation history.
|
|
217
|
+
https://github.com/nicolasalynn/seqmat
|
|
218
|
+
```
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "seqmat"
|
|
7
|
-
version = "1.5.
|
|
7
|
+
version = "1.5.1"
|
|
8
8
|
description = "Fast, vectorized genomic sequences with first-class mutation tracking."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
@@ -48,6 +48,7 @@ dev = [
|
|
|
48
48
|
"pytest-cov>=2.0",
|
|
49
49
|
"build>=1.0",
|
|
50
50
|
"twine>=4.0",
|
|
51
|
+
"pdoc>=14.0",
|
|
51
52
|
]
|
|
52
53
|
|
|
53
54
|
[project.urls]
|
|
@@ -6,6 +6,7 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
|
|
6
6
|
|
|
7
7
|
from ._io import unload_pickle
|
|
8
8
|
from .config import get_default_organism, get_organism_config
|
|
9
|
+
from .errors import GeneNotFoundError, OrganismNotConfiguredError
|
|
9
10
|
from .locator import PosArg, gene_names_at_position
|
|
10
11
|
from .transcript import Transcript
|
|
11
12
|
|
|
@@ -70,76 +71,81 @@ class Gene:
|
|
|
70
71
|
return Transcript(self.transcripts[item], organism=self.organism)
|
|
71
72
|
|
|
72
73
|
@classmethod
|
|
73
|
-
def
|
|
74
|
-
"""
|
|
75
|
-
Load gene data from file.
|
|
74
|
+
def _load_dict(cls, gene_name: str, organism: str) -> Optional[Dict[str, Any]]:
|
|
75
|
+
"""Load the raw gene dict from LMDB → SQLite → per-gene pickle, in that order.
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
To use the DB, ensure genes.db exists in your organism data dir (run setup_genomics_data,
|
|
79
|
-
or set SEQMAT_DATA_DIR to a path whose organism subdirs contain genes.db).
|
|
80
|
-
|
|
81
|
-
Args:
|
|
82
|
-
gene_name: Name of the gene to load
|
|
83
|
-
organism: Organism reference build
|
|
84
|
-
|
|
85
|
-
Returns:
|
|
86
|
-
Gene object or None if not found
|
|
77
|
+
Returns ``None`` when the gene isn't found in any backend.
|
|
87
78
|
"""
|
|
88
|
-
if organism is None:
|
|
89
|
-
organism = get_default_organism()
|
|
90
79
|
try:
|
|
91
80
|
from .lmdb_store import load_gene_from_lmdb
|
|
92
81
|
data = load_gene_from_lmdb(gene_name, organism)
|
|
93
82
|
if data is not None:
|
|
94
|
-
return
|
|
95
|
-
gene_name=data.get('gene_name'),
|
|
96
|
-
gene_id=data.get('gene_id'),
|
|
97
|
-
rev=data.get('rev'),
|
|
98
|
-
chrm=data.get('chrm'),
|
|
99
|
-
transcripts=data.get('transcripts', {}),
|
|
100
|
-
organism=organism
|
|
101
|
-
)
|
|
83
|
+
return data
|
|
102
84
|
except ImportError:
|
|
103
85
|
pass
|
|
104
86
|
try:
|
|
105
87
|
from .sqlite_store import load_gene_from_sqlite
|
|
106
88
|
data = load_gene_from_sqlite(gene_name, organism)
|
|
107
89
|
if data is not None:
|
|
108
|
-
return
|
|
109
|
-
gene_name=data.get('gene_name'),
|
|
110
|
-
gene_id=data.get('gene_id'),
|
|
111
|
-
rev=data.get('rev'),
|
|
112
|
-
chrm=data.get('chrm'),
|
|
113
|
-
transcripts=data.get('transcripts', {}),
|
|
114
|
-
organism=organism
|
|
115
|
-
)
|
|
90
|
+
return data
|
|
116
91
|
except ImportError:
|
|
117
92
|
pass
|
|
118
93
|
try:
|
|
119
94
|
config = get_organism_config(organism)
|
|
120
|
-
except ValueError:
|
|
121
|
-
|
|
122
|
-
|
|
95
|
+
except ValueError as exc:
|
|
96
|
+
raise OrganismNotConfiguredError(
|
|
97
|
+
f"Organism '{organism}' not configured. Run setup_genomics_data() first."
|
|
98
|
+
) from exc
|
|
123
99
|
mrna_path = Path(config["MRNA_PATH"])
|
|
124
|
-
gene_files = []
|
|
125
100
|
if mrna_path.exists():
|
|
126
101
|
for biotype_dir in mrna_path.iterdir():
|
|
127
102
|
if biotype_dir.is_dir():
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
103
|
+
for pkl in biotype_dir.glob(f"*_{gene_name}.pkl"):
|
|
104
|
+
return unload_pickle(pkl)
|
|
105
|
+
return None
|
|
106
|
+
|
|
107
|
+
@classmethod
|
|
108
|
+
def get(cls, gene_name: str, organism: Optional[str] = None) -> 'Gene':
|
|
109
|
+
"""Load a gene, raising on failure.
|
|
110
|
+
|
|
111
|
+
Raises:
|
|
112
|
+
OrganismNotConfiguredError: when the organism has no data set up.
|
|
113
|
+
GeneNotFoundError: when the organism is configured but the gene is missing.
|
|
114
|
+
"""
|
|
115
|
+
if organism is None:
|
|
116
|
+
organism = get_default_organism()
|
|
117
|
+
data = cls._load_dict(gene_name, organism)
|
|
118
|
+
if data is None:
|
|
119
|
+
raise GeneNotFoundError(
|
|
120
|
+
f"Gene '{gene_name}' not found in organism '{organism}'."
|
|
121
|
+
)
|
|
134
122
|
return cls(
|
|
135
123
|
gene_name=data.get('gene_name'),
|
|
136
124
|
gene_id=data.get('gene_id'),
|
|
137
125
|
rev=data.get('rev'),
|
|
138
126
|
chrm=data.get('chrm'),
|
|
139
127
|
transcripts=data.get('transcripts', {}),
|
|
140
|
-
organism=organism
|
|
128
|
+
organism=organism,
|
|
141
129
|
)
|
|
142
130
|
|
|
131
|
+
@classmethod
|
|
132
|
+
def from_file(cls, gene_name: str, organism: Optional[str] = None) -> Optional['Gene']:
|
|
133
|
+
"""Load a gene by name, returning ``None`` if not found.
|
|
134
|
+
|
|
135
|
+
Load order: LMDB (if installed) → SQLite (``genes.db``) → per-gene pickle files.
|
|
136
|
+
Prefer :meth:`get` if you'd rather have a typed exception than ``None``.
|
|
137
|
+
"""
|
|
138
|
+
if organism is None:
|
|
139
|
+
organism = get_default_organism()
|
|
140
|
+
try:
|
|
141
|
+
return cls.get(gene_name, organism=organism)
|
|
142
|
+
except OrganismNotConfiguredError as exc:
|
|
143
|
+
_log.warning("%s", exc)
|
|
144
|
+
return None
|
|
145
|
+
except GeneNotFoundError as exc:
|
|
146
|
+
_log.warning("%s", exc)
|
|
147
|
+
return None
|
|
148
|
+
|
|
143
149
|
@classmethod
|
|
144
150
|
def from_position(
|
|
145
151
|
cls,
|