seqmat 1.5.0__tar.gz → 1.6.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.
- seqmat-1.6.0/PKG-INFO +286 -0
- seqmat-1.6.0/README.md +250 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/examples/basic_usage.py +7 -7
- {seqmat-1.5.0 → seqmat-1.6.0}/examples/data_inspection.py +0 -14
- {seqmat-1.5.0 → seqmat-1.6.0}/pyproject.toml +8 -11
- seqmat-1.6.0/requirements.txt +6 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/__init__.py +2 -8
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/cli.py +104 -64
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/config.py +64 -40
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/conservation.py +2 -4
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/data_setup.py +14 -54
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/discovery.py +10 -22
- seqmat-1.6.0/seqmat/download.py +280 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/gene.py +77 -72
- seqmat-1.6.0/seqmat/genes_db.py +230 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/gtf.py +73 -163
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/locator.py +60 -68
- seqmat-1.6.0/seqmat/seqmat.py +869 -0
- seqmat-1.6.0/seqmat/sqlite_store.py +62 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/transcript.py +102 -50
- seqmat-1.6.0/seqmat.egg-info/PKG-INFO +286 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat.egg-info/SOURCES.txt +9 -4
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat.egg-info/requires.txt +2 -6
- seqmat-1.6.0/tests/test_download.py +27 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/tests/test_gene.py +69 -32
- seqmat-1.6.0/tests/test_genes_db.py +46 -0
- seqmat-1.6.0/tests/test_gtf.py +86 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/tests/test_locator.py +35 -0
- seqmat-1.6.0/tests/test_mutation_oracle.py +114 -0
- seqmat-1.6.0/tests/test_prebuilt_download.py +96 -0
- seqmat-1.6.0/tests/test_protein.py +138 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/tests/test_seqmat.py +98 -2
- seqmat-1.6.0/tests/test_transcript.py +112 -0
- seqmat-1.5.0/PKG-INFO +0 -225
- seqmat-1.5.0/README.md +0 -166
- seqmat-1.5.0/requirements.txt +0 -8
- seqmat-1.5.0/seqmat/_io.py +0 -34
- seqmat-1.5.0/seqmat/download.py +0 -228
- seqmat-1.5.0/seqmat/lmdb_store.py +0 -150
- seqmat-1.5.0/seqmat/seqmat.py +0 -733
- seqmat-1.5.0/seqmat/sqlite_store.py +0 -50
- seqmat-1.5.0/seqmat/utils.py +0 -169
- seqmat-1.5.0/seqmat.egg-info/PKG-INFO +0 -225
- {seqmat-1.5.0 → seqmat-1.6.0}/LICENSE +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/MANIFEST.in +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat/errors.py +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat.egg-info/dependency_links.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat.egg-info/entry_points.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/seqmat.egg-info/top_level.txt +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/setup.cfg +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/tests/__init__.py +0 -0
- {seqmat-1.5.0 → seqmat-1.6.0}/tests/bench_gene_creation.py +0 -0
seqmat-1.6.0/PKG-INFO
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: seqmat
|
|
3
|
+
Version: 1.6.0
|
|
4
|
+
Summary: Fast, vectorized genomic sequences with first-class mutation tracking.
|
|
5
|
+
Author-email: Nicolas Lynn Vila <nicolasalynn@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/nicolasalynn/seqmat
|
|
8
|
+
Project-URL: Repository, https://github.com/nicolasalynn/seqmat
|
|
9
|
+
Project-URL: Issues, https://github.com/nicolasalynn/seqmat/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/nicolasalynn/seqmat/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: bioinformatics,genomics,sequence,mutation,splicing,transcript,hg38,mm39
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Operating System :: OS Independent
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: numpy<3,>=1.20.0
|
|
25
|
+
Requires-Dist: pandas<4,>=2.2.0
|
|
26
|
+
Requires-Dist: pyarrow>=15.0.0
|
|
27
|
+
Requires-Dist: pysam>=0.19.0
|
|
28
|
+
Requires-Dist: platformdirs>=3.0.0
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest-cov>=2.0; extra == "dev"
|
|
32
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
33
|
+
Requires-Dist: twine>=4.0; extra == "dev"
|
|
34
|
+
Requires-Dist: pdoc>=14.0; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
<p align="center">
|
|
38
|
+
<img src="https://raw.githubusercontent.com/nicolasalynn/seqmat/main/docs/assets/logo.svg" alt="SeqMat" width="540">
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
[](https://pypi.org/project/seqmat/)
|
|
42
|
+
[](https://pypi.org/project/seqmat/)
|
|
43
|
+
[](https://github.com/nicolasalynn/seqmat/actions/workflows/test.yml)
|
|
44
|
+
[](https://codecov.io/gh/nicolasalynn/seqmat)
|
|
45
|
+
[](https://github.com/nicolasalynn/seqmat/blob/main/LICENSE)
|
|
46
|
+
[](https://pepy.tech/project/seqmat)
|
|
47
|
+
|
|
48
|
+
A small Python library for **variant-effect and splicing workflows** — the kind where keeping a genomic coordinate and the original reference attached to each base is most of the value.
|
|
49
|
+
|
|
50
|
+
SeqMat is not a replacement for Biopython. Biopython is the right default for sequence I/O, alignments, raw byte operations, and most of bioinformatics. SeqMat fills a narrower slot: when you need to apply a variant at a genomic coordinate, splice introns out, re-translate, and ask *"what was here originally?"* — that bookkeeping lives in the data model instead of in your code.
|
|
51
|
+
|
|
52
|
+
A DNA sequence is stored 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 the `index` (genomic coordinates) and the `ref` (original bases) through every transform, and 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 NumPy index.
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from seqmat import Gene, SeqMat
|
|
56
|
+
|
|
57
|
+
# What gene is at chr12:25,245,350?
|
|
58
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)]
|
|
59
|
+
|
|
60
|
+
# Load it, assemble the mature mRNA, translate.
|
|
61
|
+
kras = Gene.from_file("KRAS")
|
|
62
|
+
tx = kras.transcript()
|
|
63
|
+
tx.generate_mature_mrna()
|
|
64
|
+
tx.generate_protein()
|
|
65
|
+
|
|
66
|
+
# Apply mutations; history is kept on the SeqMat.
|
|
67
|
+
seq = SeqMat("ATCGATCGATCG")
|
|
68
|
+
seq.apply_mutations([(3, "C", "G"), (6, "-", "AAA"), (10, "TC", "-")])
|
|
69
|
+
seq.mutations
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Worked examples:
|
|
73
|
+
|
|
74
|
+
- [KRAS G12D — coordinate to protein](https://github.com/nicolasalynn/seqmat/blob/main/examples/kras_g12d_analysis.ipynb): chromosome coordinate → mature mRNA → protein, comparing wild type to the G12D mutant.
|
|
75
|
+
- [SeqMat vs Biopython, side by side](https://github.com/nicolasalynn/seqmat/blob/main/examples/seqmat_vs_biopython.ipynb): the same tasks in both libraries, with timings, calling out where each one wins.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## When SeqMat fits — and when it doesn't
|
|
80
|
+
|
|
81
|
+
Reach for SeqMat when:
|
|
82
|
+
|
|
83
|
+
- You have a genomic coordinate and want the gene, transcript, mature mRNA, or protein-level effect of a variant at it.
|
|
84
|
+
- You need to splice introns out of a pre-mRNA while keeping a coordinate map for everything that survives.
|
|
85
|
+
- You want a mutation history attached to the sequence without writing your own side-car dict.
|
|
86
|
+
- You're doing splicing or variant-effect analyses where "what was originally here?" comes up repeatedly.
|
|
87
|
+
|
|
88
|
+
Reach for Biopython (or another tool) when:
|
|
89
|
+
|
|
90
|
+
- You're reading or writing FASTA, GenBank, EMBL, alignments, BLAST output, NCBI fetch, etc. Biopython has decades of format coverage that SeqMat will not match.
|
|
91
|
+
- You need maximum throughput on raw byte operations — reverse-complement on long chromosomes, mass translations, big alignment passes. SeqMat's structured-array overhead is real and `Bio.Seq` will win.
|
|
92
|
+
- You're doing batch interval-set algebra over many feature tables — that's PyRanges' job.
|
|
93
|
+
|
|
94
|
+
The two libraries compose. Many real pipelines load FASTA via Biopython, do byte-level work in `Bio.Seq`, and reach for SeqMat where coordinate tracking and mutation history would otherwise be hand-written. See [examples/seqmat_vs_biopython.ipynb](https://github.com/nicolasalynn/seqmat/blob/main/examples/seqmat_vs_biopython.ipynb) for a head-to-head.
|
|
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` records the data paths in `config.json` 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("hg38.fa", "chr12", start=25_245_300, end=25_245_400)
|
|
156
|
+
seq.apply_mutations([(25_245_350, "C", "T")]) # KRAS G12D (c.35G>A; alleles are always + strand)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Performance
|
|
160
|
+
|
|
161
|
+
Two short statements first, then the numbers.
|
|
162
|
+
|
|
163
|
+
1. **For the operations SeqMat is built around** (coordinate-aware gene lookup, splicing-with-coordinate-preservation, mutation-tracked sequence assembly), it's fast enough that performance won't be the bottleneck in your pipeline.
|
|
164
|
+
2. **For raw byte operations** (reverse-complement, mass translation, simple string mutations without coordinate tracking), Biopython is faster, often substantially. Use it.
|
|
165
|
+
|
|
166
|
+
Measured on an M-series Mac, hg38, one core, warm caches. Reproducible via the scripts in `benchmarks/`.
|
|
167
|
+
|
|
168
|
+
### Operations SeqMat is built for
|
|
169
|
+
|
|
170
|
+
| Operation | Time |
|
|
171
|
+
| -------------------------------------------------- | -------: |
|
|
172
|
+
| `gene_names_at_position(chrm, pos)` | 2.5 µs |
|
|
173
|
+
| KRAS mature mRNA assembly (splice + translate) | 0.2 ms |
|
|
174
|
+
| 1,000-SNP batch on 45 kb sequence (with history) | 0.5 ms |
|
|
175
|
+
| `Gene.from_file("KRAS")` (SQLite + unpickle) | 24 ms |
|
|
176
|
+
| `Gene.from_position(chrm, pos)` end-to-end | 24 ms |
|
|
177
|
+
|
|
178
|
+
### Position → gene, head-to-head
|
|
179
|
+
|
|
180
|
+
Same 63,241 hg38 gene intervals, same 10,000 random point queries. PyRanges is the natural comparison; both libraries are good at this, in different idioms. Reproduce with `python benchmarks/bench_position_lookup.py`.
|
|
181
|
+
|
|
182
|
+
Per-query (one coordinate, one answer, in a loop — `Gene.from_position`'s pattern):
|
|
183
|
+
|
|
184
|
+
| Implementation | Per query |
|
|
185
|
+
| --------------------------------------- | --------: |
|
|
186
|
+
| SeqMat (locator) | 2.5 µs |
|
|
187
|
+
| Python `dict` + `bisect` | 21 µs |
|
|
188
|
+
| pandas (`groupby` chrm + boolean mask) | 79 µs |
|
|
189
|
+
| PyRanges constructed per call | 2.0 ms |
|
|
190
|
+
|
|
191
|
+
Batched (all queries handed in at once — PyRanges' native idiom):
|
|
192
|
+
|
|
193
|
+
| Implementation | Per query |
|
|
194
|
+
| ----------------------------- | --------: |
|
|
195
|
+
| PyRanges `.join` | 2.07 µs |
|
|
196
|
+
| SeqMat (serial loop) | 2.59 µs |
|
|
197
|
+
|
|
198
|
+
Each library wins its native workload. Constructing a `PyRanges` per call is slow (and not what its authors intend) — listed only because it's a common mistake.
|
|
199
|
+
|
|
200
|
+
### Where Biopython is faster
|
|
201
|
+
|
|
202
|
+
For completeness, here are the operations where SeqMat is the wrong choice. The gap is the cost of the structured-array data model, which carries `(nt, ref, index, mut_type, valid)` through every transform.
|
|
203
|
+
|
|
204
|
+
| Workload | Biopython / native | SeqMat |
|
|
205
|
+
| ------------------------------ | ------------------------------: | ------: |
|
|
206
|
+
| 1,000 SNPs (no history needed) | `MutableSeq`: 0.21 ms | 0.5 ms (with history) |
|
|
207
|
+
| 10-intron splice (50 kb) | `str.join`: 0.002 ms | 0.8 ms (coordinate-preserving) |
|
|
208
|
+
| Reverse-complement (1 Mb) | `Seq.reverse_complement`: 0.57 ms | 9.4 ms |
|
|
209
|
+
|
|
210
|
+
If your hot path is one of these and you don't need coordinates or history attached to the result, use `Bio.Seq` or `bytearray`. The two libraries compose fine.
|
|
211
|
+
|
|
212
|
+
### Implementation notes
|
|
213
|
+
|
|
214
|
+
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`](https://github.com/nicolasalynn/seqmat/blob/main/seqmat/seqmat.py) and [`seqmat/locator.py`](https://github.com/nicolasalynn/seqmat/blob/main/seqmat/locator.py).
|
|
215
|
+
|
|
216
|
+
## Command-line interface
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
seqmat setup [--organism hg38|mm39] [--path PATH] [--build-from-sources]
|
|
220
|
+
seqmat summary
|
|
221
|
+
seqmat info --organism hg38
|
|
222
|
+
seqmat search --organism hg38 --query KRAS
|
|
223
|
+
seqmat list --organism hg38 --biotype protein_coding --limit 20
|
|
224
|
+
seqmat count --organism hg38
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Data setup
|
|
228
|
+
|
|
229
|
+
By default `seqmat setup` downloads prebuilt `genes.db` and a bgzipped FASTA from Hugging Face (`nicolasalynn/shared-assets`, with the old S3 bucket as fallback), verifying sha256 checksums. To regenerate `genes.db` from a specific Ensembl release or a custom GTF:
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
seqmat setup --organism hg38 --build-from-sources
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Custom organisms, mirror buckets, ephemeral environments (Docker / Run.ai), shared multi-user installs, and the full configuration system are documented in [docs/SETUP.md](https://github.com/nicolasalynn/seqmat/blob/main/docs/SETUP.md).
|
|
236
|
+
|
|
237
|
+
## API at a glance
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from seqmat import (
|
|
241
|
+
SeqMat, # coordinate-tracked sequence with mutation history
|
|
242
|
+
Gene, Transcript, # gene/transcript model
|
|
243
|
+
gene_names_at_position, # name-only positional lookup
|
|
244
|
+
build_location_index, # force-rebuild the position index
|
|
245
|
+
setup_genomics_data, # programmatic setup
|
|
246
|
+
search_genes, available_genes, # discovery helpers
|
|
247
|
+
)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Key classes:
|
|
251
|
+
|
|
252
|
+
- `SeqMat` — `apply_mutations`, `clone`, `complement`, `reverse_complement`, `remove_regions`, `from_fasta_file`
|
|
253
|
+
- `Gene` — `from_file`, `from_position`, `get`, `transcript`, `splice_sites`, `primary_transcript`
|
|
254
|
+
- `Transcript` — `generate_pre_mrna`, `generate_mature_mrna`, `generate_protein`, `exons`, `introns`
|
|
255
|
+
|
|
256
|
+
Full API reference (auto-generated from docstrings): [nicolasalynn.github.io/seqmat](https://nicolasalynn.github.io/seqmat/seqmat.html).
|
|
257
|
+
|
|
258
|
+
## Requirements
|
|
259
|
+
|
|
260
|
+
Python ≥ 3.10. Core deps: `numpy`, `pandas`, `pyarrow`, `pysam`, `platformdirs`.
|
|
261
|
+
|
|
262
|
+
## A note on indexing
|
|
263
|
+
|
|
264
|
+
`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.
|
|
265
|
+
|
|
266
|
+
## Roadmap
|
|
267
|
+
|
|
268
|
+
[ROADMAP.md](https://github.com/nicolasalynn/seqmat/blob/main/ROADMAP.md) — what's coming, what isn't, and why. The headline themes: VCF + per-patient `Patient` / `Cohort` objects with lazy region-scoped variant application, reference-build-aware coordinate conversion, and splicing-impact integration.
|
|
269
|
+
|
|
270
|
+
## Contributing
|
|
271
|
+
|
|
272
|
+
PRs welcome. Run the test suite with `pytest tests/`. Comparative benchmarks are under `benchmarks/`.
|
|
273
|
+
|
|
274
|
+
## License
|
|
275
|
+
|
|
276
|
+
MIT — see [LICENSE](https://github.com/nicolasalynn/seqmat/blob/main/LICENSE).
|
|
277
|
+
|
|
278
|
+
## Citation
|
|
279
|
+
|
|
280
|
+
If SeqMat is useful in your research:
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
Lynn Vila, N. (2025). SeqMat: a genomic sequence library with
|
|
284
|
+
coordinate tracking and mutation history.
|
|
285
|
+
https://github.com/nicolasalynn/seqmat
|
|
286
|
+
```
|
seqmat-1.6.0/README.md
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/nicolasalynn/seqmat/main/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
|
+
[](https://github.com/nicolasalynn/seqmat/blob/main/LICENSE)
|
|
10
|
+
[](https://pepy.tech/project/seqmat)
|
|
11
|
+
|
|
12
|
+
A small Python library for **variant-effect and splicing workflows** — the kind where keeping a genomic coordinate and the original reference attached to each base is most of the value.
|
|
13
|
+
|
|
14
|
+
SeqMat is not a replacement for Biopython. Biopython is the right default for sequence I/O, alignments, raw byte operations, and most of bioinformatics. SeqMat fills a narrower slot: when you need to apply a variant at a genomic coordinate, splice introns out, re-translate, and ask *"what was here originally?"* — that bookkeeping lives in the data model instead of in your code.
|
|
15
|
+
|
|
16
|
+
A DNA sequence is stored 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 the `index` (genomic coordinates) and the `ref` (original bases) through every transform, and 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 NumPy index.
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from seqmat import Gene, SeqMat
|
|
20
|
+
|
|
21
|
+
# What gene is at chr12:25,245,350?
|
|
22
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)]
|
|
23
|
+
|
|
24
|
+
# Load it, assemble the mature mRNA, translate.
|
|
25
|
+
kras = Gene.from_file("KRAS")
|
|
26
|
+
tx = kras.transcript()
|
|
27
|
+
tx.generate_mature_mrna()
|
|
28
|
+
tx.generate_protein()
|
|
29
|
+
|
|
30
|
+
# Apply mutations; history is kept on the SeqMat.
|
|
31
|
+
seq = SeqMat("ATCGATCGATCG")
|
|
32
|
+
seq.apply_mutations([(3, "C", "G"), (6, "-", "AAA"), (10, "TC", "-")])
|
|
33
|
+
seq.mutations
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Worked examples:
|
|
37
|
+
|
|
38
|
+
- [KRAS G12D — coordinate to protein](https://github.com/nicolasalynn/seqmat/blob/main/examples/kras_g12d_analysis.ipynb): chromosome coordinate → mature mRNA → protein, comparing wild type to the G12D mutant.
|
|
39
|
+
- [SeqMat vs Biopython, side by side](https://github.com/nicolasalynn/seqmat/blob/main/examples/seqmat_vs_biopython.ipynb): the same tasks in both libraries, with timings, calling out where each one wins.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## When SeqMat fits — and when it doesn't
|
|
44
|
+
|
|
45
|
+
Reach for SeqMat when:
|
|
46
|
+
|
|
47
|
+
- You have a genomic coordinate and want the gene, transcript, mature mRNA, or protein-level effect of a variant at it.
|
|
48
|
+
- You need to splice introns out of a pre-mRNA while keeping a coordinate map for everything that survives.
|
|
49
|
+
- You want a mutation history attached to the sequence without writing your own side-car dict.
|
|
50
|
+
- You're doing splicing or variant-effect analyses where "what was originally here?" comes up repeatedly.
|
|
51
|
+
|
|
52
|
+
Reach for Biopython (or another tool) when:
|
|
53
|
+
|
|
54
|
+
- You're reading or writing FASTA, GenBank, EMBL, alignments, BLAST output, NCBI fetch, etc. Biopython has decades of format coverage that SeqMat will not match.
|
|
55
|
+
- You need maximum throughput on raw byte operations — reverse-complement on long chromosomes, mass translations, big alignment passes. SeqMat's structured-array overhead is real and `Bio.Seq` will win.
|
|
56
|
+
- You're doing batch interval-set algebra over many feature tables — that's PyRanges' job.
|
|
57
|
+
|
|
58
|
+
The two libraries compose. Many real pipelines load FASTA via Biopython, do byte-level work in `Bio.Seq`, and reach for SeqMat where coordinate tracking and mutation history would otherwise be hand-written. See [examples/seqmat_vs_biopython.ipynb](https://github.com/nicolasalynn/seqmat/blob/main/examples/seqmat_vs_biopython.ipynb) for a head-to-head.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Install
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install seqmat
|
|
66
|
+
seqmat setup # one-time: downloads hg38 genes.db + FASTA (~4 GB)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`seqmat setup` records the data paths in `config.json` so the data is found automatically next session. Mouse: `seqmat setup --organism mm39`.
|
|
70
|
+
|
|
71
|
+
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.
|
|
72
|
+
|
|
73
|
+
## Quick start
|
|
74
|
+
|
|
75
|
+
### Sequence operations
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from seqmat import SeqMat
|
|
79
|
+
import numpy as np
|
|
80
|
+
|
|
81
|
+
seq = SeqMat("ATCGATCGATCG", indices=np.arange(1000, 1012))
|
|
82
|
+
seq[1005] # base at genomic position 1005
|
|
83
|
+
seq[1003:1008].seq # "GATCG"
|
|
84
|
+
seq.reverse_complement() # in place
|
|
85
|
+
seq.remove_regions([(1003, 1005), (1008, 1009)]) # splice out introns
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Genes and transcripts
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from seqmat import Gene
|
|
92
|
+
|
|
93
|
+
kras = Gene.from_file("KRAS")
|
|
94
|
+
kras # Gene: KRAS, ID: ENSG00000133703, Chr: 12, Transcripts: 14
|
|
95
|
+
|
|
96
|
+
tx = kras.transcript() # primary transcript
|
|
97
|
+
tx.generate_mature_mrna()
|
|
98
|
+
tx.generate_protein()
|
|
99
|
+
tx.protein[:20] # 'MTEYKLVVVGAGGVGKSALT'
|
|
100
|
+
|
|
101
|
+
acceptors, donors = kras.splice_sites() # Counter across all transcripts
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Position → gene (added in 1.4.0)
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from seqmat import Gene, gene_names_at_position
|
|
108
|
+
|
|
109
|
+
Gene.from_position("12", 25_245_350) # [Gene(KRAS)] — point query
|
|
110
|
+
Gene.from_position("chr12", (25_200_000, 25_300_000)) # all overlapping genes in a range
|
|
111
|
+
gene_names_at_position("X", 100_000) # names only (no BLOB load)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
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.
|
|
115
|
+
|
|
116
|
+
### Loading from FASTA
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
seq = SeqMat.from_fasta_file("hg38.fa", "chr12", start=25_245_300, end=25_245_400)
|
|
120
|
+
seq.apply_mutations([(25_245_350, "C", "T")]) # KRAS G12D (c.35G>A; alleles are always + strand)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Performance
|
|
124
|
+
|
|
125
|
+
Two short statements first, then the numbers.
|
|
126
|
+
|
|
127
|
+
1. **For the operations SeqMat is built around** (coordinate-aware gene lookup, splicing-with-coordinate-preservation, mutation-tracked sequence assembly), it's fast enough that performance won't be the bottleneck in your pipeline.
|
|
128
|
+
2. **For raw byte operations** (reverse-complement, mass translation, simple string mutations without coordinate tracking), Biopython is faster, often substantially. Use it.
|
|
129
|
+
|
|
130
|
+
Measured on an M-series Mac, hg38, one core, warm caches. Reproducible via the scripts in `benchmarks/`.
|
|
131
|
+
|
|
132
|
+
### Operations SeqMat is built for
|
|
133
|
+
|
|
134
|
+
| Operation | Time |
|
|
135
|
+
| -------------------------------------------------- | -------: |
|
|
136
|
+
| `gene_names_at_position(chrm, pos)` | 2.5 µs |
|
|
137
|
+
| KRAS mature mRNA assembly (splice + translate) | 0.2 ms |
|
|
138
|
+
| 1,000-SNP batch on 45 kb sequence (with history) | 0.5 ms |
|
|
139
|
+
| `Gene.from_file("KRAS")` (SQLite + unpickle) | 24 ms |
|
|
140
|
+
| `Gene.from_position(chrm, pos)` end-to-end | 24 ms |
|
|
141
|
+
|
|
142
|
+
### Position → gene, head-to-head
|
|
143
|
+
|
|
144
|
+
Same 63,241 hg38 gene intervals, same 10,000 random point queries. PyRanges is the natural comparison; both libraries are good at this, in different idioms. Reproduce with `python benchmarks/bench_position_lookup.py`.
|
|
145
|
+
|
|
146
|
+
Per-query (one coordinate, one answer, in a loop — `Gene.from_position`'s pattern):
|
|
147
|
+
|
|
148
|
+
| Implementation | Per query |
|
|
149
|
+
| --------------------------------------- | --------: |
|
|
150
|
+
| SeqMat (locator) | 2.5 µs |
|
|
151
|
+
| Python `dict` + `bisect` | 21 µs |
|
|
152
|
+
| pandas (`groupby` chrm + boolean mask) | 79 µs |
|
|
153
|
+
| PyRanges constructed per call | 2.0 ms |
|
|
154
|
+
|
|
155
|
+
Batched (all queries handed in at once — PyRanges' native idiom):
|
|
156
|
+
|
|
157
|
+
| Implementation | Per query |
|
|
158
|
+
| ----------------------------- | --------: |
|
|
159
|
+
| PyRanges `.join` | 2.07 µs |
|
|
160
|
+
| SeqMat (serial loop) | 2.59 µs |
|
|
161
|
+
|
|
162
|
+
Each library wins its native workload. Constructing a `PyRanges` per call is slow (and not what its authors intend) — listed only because it's a common mistake.
|
|
163
|
+
|
|
164
|
+
### Where Biopython is faster
|
|
165
|
+
|
|
166
|
+
For completeness, here are the operations where SeqMat is the wrong choice. The gap is the cost of the structured-array data model, which carries `(nt, ref, index, mut_type, valid)` through every transform.
|
|
167
|
+
|
|
168
|
+
| Workload | Biopython / native | SeqMat |
|
|
169
|
+
| ------------------------------ | ------------------------------: | ------: |
|
|
170
|
+
| 1,000 SNPs (no history needed) | `MutableSeq`: 0.21 ms | 0.5 ms (with history) |
|
|
171
|
+
| 10-intron splice (50 kb) | `str.join`: 0.002 ms | 0.8 ms (coordinate-preserving) |
|
|
172
|
+
| Reverse-complement (1 Mb) | `Seq.reverse_complement`: 0.57 ms | 9.4 ms |
|
|
173
|
+
|
|
174
|
+
If your hot path is one of these and you don't need coordinates or history attached to the result, use `Bio.Seq` or `bytearray`. The two libraries compose fine.
|
|
175
|
+
|
|
176
|
+
### Implementation notes
|
|
177
|
+
|
|
178
|
+
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`](https://github.com/nicolasalynn/seqmat/blob/main/seqmat/seqmat.py) and [`seqmat/locator.py`](https://github.com/nicolasalynn/seqmat/blob/main/seqmat/locator.py).
|
|
179
|
+
|
|
180
|
+
## Command-line interface
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
seqmat setup [--organism hg38|mm39] [--path PATH] [--build-from-sources]
|
|
184
|
+
seqmat summary
|
|
185
|
+
seqmat info --organism hg38
|
|
186
|
+
seqmat search --organism hg38 --query KRAS
|
|
187
|
+
seqmat list --organism hg38 --biotype protein_coding --limit 20
|
|
188
|
+
seqmat count --organism hg38
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Data setup
|
|
192
|
+
|
|
193
|
+
By default `seqmat setup` downloads prebuilt `genes.db` and a bgzipped FASTA from Hugging Face (`nicolasalynn/shared-assets`, with the old S3 bucket as fallback), verifying sha256 checksums. To regenerate `genes.db` from a specific Ensembl release or a custom GTF:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
seqmat setup --organism hg38 --build-from-sources
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Custom organisms, mirror buckets, ephemeral environments (Docker / Run.ai), shared multi-user installs, and the full configuration system are documented in [docs/SETUP.md](https://github.com/nicolasalynn/seqmat/blob/main/docs/SETUP.md).
|
|
200
|
+
|
|
201
|
+
## API at a glance
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
from seqmat import (
|
|
205
|
+
SeqMat, # coordinate-tracked sequence with mutation history
|
|
206
|
+
Gene, Transcript, # gene/transcript model
|
|
207
|
+
gene_names_at_position, # name-only positional lookup
|
|
208
|
+
build_location_index, # force-rebuild the position index
|
|
209
|
+
setup_genomics_data, # programmatic setup
|
|
210
|
+
search_genes, available_genes, # discovery helpers
|
|
211
|
+
)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Key classes:
|
|
215
|
+
|
|
216
|
+
- `SeqMat` — `apply_mutations`, `clone`, `complement`, `reverse_complement`, `remove_regions`, `from_fasta_file`
|
|
217
|
+
- `Gene` — `from_file`, `from_position`, `get`, `transcript`, `splice_sites`, `primary_transcript`
|
|
218
|
+
- `Transcript` — `generate_pre_mrna`, `generate_mature_mrna`, `generate_protein`, `exons`, `introns`
|
|
219
|
+
|
|
220
|
+
Full API reference (auto-generated from docstrings): [nicolasalynn.github.io/seqmat](https://nicolasalynn.github.io/seqmat/seqmat.html).
|
|
221
|
+
|
|
222
|
+
## Requirements
|
|
223
|
+
|
|
224
|
+
Python ≥ 3.10. Core deps: `numpy`, `pandas`, `pyarrow`, `pysam`, `platformdirs`.
|
|
225
|
+
|
|
226
|
+
## A note on indexing
|
|
227
|
+
|
|
228
|
+
`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.
|
|
229
|
+
|
|
230
|
+
## Roadmap
|
|
231
|
+
|
|
232
|
+
[ROADMAP.md](https://github.com/nicolasalynn/seqmat/blob/main/ROADMAP.md) — what's coming, what isn't, and why. The headline themes: VCF + per-patient `Patient` / `Cohort` objects with lazy region-scoped variant application, reference-build-aware coordinate conversion, and splicing-impact integration.
|
|
233
|
+
|
|
234
|
+
## Contributing
|
|
235
|
+
|
|
236
|
+
PRs welcome. Run the test suite with `pytest tests/`. Comparative benchmarks are under `benchmarks/`.
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT — see [LICENSE](https://github.com/nicolasalynn/seqmat/blob/main/LICENSE).
|
|
241
|
+
|
|
242
|
+
## Citation
|
|
243
|
+
|
|
244
|
+
If SeqMat is useful in your research:
|
|
245
|
+
|
|
246
|
+
```
|
|
247
|
+
Lynn Vila, N. (2025). SeqMat: a genomic sequence library with
|
|
248
|
+
coordinate tracking and mutation history.
|
|
249
|
+
https://github.com/nicolasalynn/seqmat
|
|
250
|
+
```
|
|
@@ -33,16 +33,16 @@ def mutation_examples():
|
|
|
33
33
|
print(f"Original: {seq.seq}")
|
|
34
34
|
|
|
35
35
|
# Single nucleotide polymorphism
|
|
36
|
-
seq.apply_mutations((5, "
|
|
37
|
-
print(f"After SNP (T
|
|
36
|
+
seq.apply_mutations((5, "A", "T"))
|
|
37
|
+
print(f"After SNP (A->T at pos 5): {seq.seq}")
|
|
38
38
|
|
|
39
39
|
# Insertion
|
|
40
40
|
seq.apply_mutations((8, "-", "GGG"))
|
|
41
41
|
print(f"After insertion (GGG at pos 8): {seq.seq}")
|
|
42
42
|
|
|
43
43
|
# Deletion
|
|
44
|
-
seq.apply_mutations((3, "
|
|
45
|
-
print(f"After deletion (
|
|
44
|
+
seq.apply_mutations((3, "CG", "-"))
|
|
45
|
+
print(f"After deletion (CG at pos 3-4): {seq.seq}")
|
|
46
46
|
|
|
47
47
|
print(f"Total mutations applied: {len(seq.mutations)}")
|
|
48
48
|
print(f"Modified positions: {sorted(seq.mutated_positions)}")
|
|
@@ -56,7 +56,7 @@ def sequence_transformations():
|
|
|
56
56
|
print(f"Original: {seq.seq}")
|
|
57
57
|
|
|
58
58
|
# Complement
|
|
59
|
-
comp = seq.complement()
|
|
59
|
+
comp = seq.complement(copy=True) # default is in place!
|
|
60
60
|
print(f"Complement: {comp.seq}")
|
|
61
61
|
|
|
62
62
|
# Reverse complement
|
|
@@ -113,7 +113,7 @@ def advanced_mutations():
|
|
|
113
113
|
mutations = [
|
|
114
114
|
(2, "T", "A"), # SNP
|
|
115
115
|
(6, "-", "AAA"), # Insertion
|
|
116
|
-
(
|
|
116
|
+
(11, "CGA", "-"), # Deletion (positions 11-13)
|
|
117
117
|
(14, "TC", "GG") # Complex substitution
|
|
118
118
|
]
|
|
119
119
|
|
|
@@ -140,7 +140,7 @@ def export_operations():
|
|
|
140
140
|
print("=== Export Operations ===")
|
|
141
141
|
|
|
142
142
|
seq = SeqMat("ATCGATCGATCG", name="export_demo")
|
|
143
|
-
seq.apply_mutations([(5, "
|
|
143
|
+
seq.apply_mutations([(5, "A", "T"), (8, "-", "GG")])
|
|
144
144
|
|
|
145
145
|
# Export as FASTA
|
|
146
146
|
fasta = seq.to_fasta(wrap=40)
|
|
@@ -6,7 +6,6 @@ This script demonstrates the new data inspection utilities in SeqMat.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
from seqmat import (
|
|
9
|
-
list_supported_organisms,
|
|
10
9
|
list_available_organisms,
|
|
11
10
|
get_organism_info,
|
|
12
11
|
list_gene_biotypes,
|
|
@@ -21,10 +20,6 @@ def basic_organism_info():
|
|
|
21
20
|
"""Demonstrate basic organism information retrieval"""
|
|
22
21
|
print("=== Basic Organism Information ===")
|
|
23
22
|
|
|
24
|
-
# Check what organisms are supported
|
|
25
|
-
supported = list_supported_organisms()
|
|
26
|
-
print(f"Supported organisms: {supported}")
|
|
27
|
-
|
|
28
23
|
# Check what's actually configured
|
|
29
24
|
available = list_available_organisms()
|
|
30
25
|
print(f"Configured organisms: {available}")
|
|
@@ -76,12 +71,6 @@ def explore_organism_data():
|
|
|
76
71
|
percentage = (count / total_genes) * 100
|
|
77
72
|
print(f" {biotype}: {count:,} genes ({percentage:.1f}%)")
|
|
78
73
|
|
|
79
|
-
# Show chromosome data
|
|
80
|
-
if "chromosomes" in data_avail:
|
|
81
|
-
chroms = data_avail["chromosomes"]
|
|
82
|
-
print(f"✅ Chromosomes: {len(chroms)} available")
|
|
83
|
-
print(f" Available: {', '.join(sorted(chroms)[:10])}{'...' if len(chroms) > 10 else ''}")
|
|
84
|
-
|
|
85
74
|
print()
|
|
86
75
|
|
|
87
76
|
def gene_exploration_examples():
|
|
@@ -179,10 +168,7 @@ def data_exploration_workflow():
|
|
|
179
168
|
print("=== Complete Data Exploration Workflow ===")
|
|
180
169
|
|
|
181
170
|
print("Step 1: Check organism support")
|
|
182
|
-
supported = list_supported_organisms()
|
|
183
171
|
available = list_available_organisms()
|
|
184
|
-
|
|
185
|
-
print(f" Supported: {supported}")
|
|
186
172
|
print(f" Configured: {available}")
|
|
187
173
|
|
|
188
174
|
if not available:
|