methdb 0.0.2__tar.gz → 0.0.4__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.
Files changed (43) hide show
  1. methdb-0.0.4/PKG-INFO +426 -0
  2. methdb-0.0.4/README.md +392 -0
  3. methdb-0.0.4/mdb/__init__.py +28 -0
  4. methdb-0.0.4/mdb/asmpca.py +816 -0
  5. methdb-0.0.4/mdb/create.py +178 -0
  6. methdb-0.0.4/mdb/main.py +48 -0
  7. methdb-0.0.4/mdb/merge.py +562 -0
  8. {methdb-0.0.2 → methdb-0.0.4}/mdb/ont_bed_parsing.py +29 -19
  9. {methdb-0.0.2 → methdb-0.0.4}/mdb/pacbio_bed_parsing.py +18 -16
  10. methdb-0.0.4/mdb/parse_args.py +458 -0
  11. methdb-0.0.4/mdb/pca.py +1781 -0
  12. methdb-0.0.4/mdb/plot.py +1286 -0
  13. methdb-0.0.4/mdb/plotting.py +417 -0
  14. methdb-0.0.4/mdb/query.py +48 -0
  15. methdb-0.0.4/mdb/schema.py +103 -0
  16. methdb-0.0.4/mdb/stats.py +732 -0
  17. methdb-0.0.4/mdb/storage.py +1260 -0
  18. methdb-0.0.4/mdb/strand.py +1120 -0
  19. methdb-0.0.4/mdb/viz.py +1382 -0
  20. methdb-0.0.4/methdb.egg-info/PKG-INFO +426 -0
  21. {methdb-0.0.2 → methdb-0.0.4}/methdb.egg-info/SOURCES.txt +19 -1
  22. methdb-0.0.4/methdb.egg-info/requires.txt +12 -0
  23. {methdb-0.0.2 → methdb-0.0.4}/setup.cfg +10 -2
  24. {methdb-0.0.2 → methdb-0.0.4}/setup.py +1 -1
  25. methdb-0.0.4/tests/test_pca_workflow.py +202 -0
  26. methdb-0.0.4/tests/test_zarr_workflow.py +286 -0
  27. methdb-0.0.2/PKG-INFO +0 -63
  28. methdb-0.0.2/README.md +0 -37
  29. methdb-0.0.2/mdb/__init__.py +0 -1
  30. methdb-0.0.2/mdb/create.py +0 -84
  31. methdb-0.0.2/mdb/main.py +0 -27
  32. methdb-0.0.2/mdb/merge.py +0 -252
  33. methdb-0.0.2/mdb/parse_args.py +0 -68
  34. methdb-0.0.2/mdb/pca.py +0 -440
  35. methdb-0.0.2/mdb/strand.py +0 -3
  36. methdb-0.0.2/methdb.egg-info/PKG-INFO +0 -63
  37. methdb-0.0.2/methdb.egg-info/requires.txt +0 -4
  38. {methdb-0.0.2 → methdb-0.0.4}/LICENSE +0 -0
  39. {methdb-0.0.2 → methdb-0.0.4}/mdb/build_methyl_mats.py +0 -0
  40. {methdb-0.0.2 → methdb-0.0.4}/mdb/index.py +0 -0
  41. {methdb-0.0.2 → methdb-0.0.4}/methdb.egg-info/dependency_links.txt +0 -0
  42. {methdb-0.0.2 → methdb-0.0.4}/methdb.egg-info/entry_points.txt +0 -0
  43. {methdb-0.0.2 → methdb-0.0.4}/methdb.egg-info/top_level.txt +0 -0
methdb-0.0.4/PKG-INFO ADDED
@@ -0,0 +1,426 @@
1
+ Metadata-Version: 2.4
2
+ Name: methdb
3
+ Version: 0.0.4
4
+ Summary: mdb: population-level DNA methylation analysis toolkit
5
+ Home-page: https://github.com/Fu-Yilei/mdb
6
+ Author: Yilei Fu
7
+ Author-email:
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: polars>=0.17.9
16
+ Requires-Dist: numpy>=2.2.0
17
+ Requires-Dist: pysam
18
+ Requires-Dist: pandas
19
+ Requires-Dist: scikit-learn
20
+ Requires-Dist: umap-learn
21
+ Requires-Dist: seaborn
22
+ Requires-Dist: matplotlib
23
+ Requires-Dist: plotly
24
+ Requires-Dist: tqdm
25
+ Requires-Dist: zarr>=3.1.1
26
+ Requires-Dist: numcodecs>=0.12
27
+ Dynamic: author
28
+ Dynamic: description
29
+ Dynamic: description-content-type
30
+ Dynamic: home-page
31
+ Dynamic: license
32
+ Dynamic: license-file
33
+ Dynamic: summary
34
+
35
+ # mdb
36
+
37
+ [![PyPI version](https://img.shields.io/pypi/v/methdb?logo=pypi&logoColor=white)](https://pypi.org/project/methdb/)
38
+ [![Python versions](https://img.shields.io/pypi/pyversions/methdb?logo=python&logoColor=white)](https://pypi.org/project/methdb/)
39
+ [![License](https://img.shields.io/github/license/Fu-Yilei/mdb)](LICENSE)
40
+
41
+ `mdb` builds, merges, queries, and analyzes CpG-by-sample methylation matrices
42
+ from ONT and PacBio BED inputs. It also provides cohort PCA/UMAP, summary
43
+ statistics, interactive methylation visualization, ONT ASM PCA, and
44
+ strand-bias hotspot detection.
45
+
46
+ - PyPI package: `methdb`
47
+ - CLI command: `mdb`
48
+
49
+ ## Install
50
+
51
+ ```bash
52
+ pip install methdb
53
+ ```
54
+
55
+ To install from a local checkout instead:
56
+
57
+ ```bash
58
+ pip install .
59
+ ```
60
+
61
+ Verify:
62
+
63
+ ```bash
64
+ mdb --help
65
+ mdb --version
66
+ ```
67
+
68
+ ## Core Concepts
69
+
70
+ - **Sample bundle (`.smdb`)**: one sample, multiple track views (assay/haplotype/strand).
71
+ - **Cohort store (`.mmdb`)**: merged sample bundles for population-scale queries.
72
+ - **Backends**:
73
+ - `zarr` (default, compressed, block-aligned merge writes)
74
+ - `npy` (optional compatibility backend)
75
+
76
+ ## Quick Start
77
+
78
+ ### 1) Build CpG index
79
+
80
+ ```bash
81
+ mdb index -r GRCh38_no_alt.fa -o GRCh38.cpg_index.npz
82
+ ```
83
+
84
+ Include `chrX/chrY`:
85
+
86
+ ```bash
87
+ mdb index -r GRCh38_no_alt.fa -o GRCh38.cpg_index.npz --sex
88
+ ```
89
+
90
+ ### 2) Create sample bundle
91
+
92
+ ONT (modkit output file, directory, or prefix):
93
+
94
+ ```bash
95
+ mdb create \
96
+ -p ont \
97
+ -n GRCh38.cpg_index.npz \
98
+ -b /path/to/ont_input \
99
+ -o sample_ont.smdb \
100
+ -c 5 \
101
+ --sample-id SAMPLE_ONT
102
+ ```
103
+
104
+ Accepted ONT layouts are:
105
+
106
+ - a BED/BED.GZ file, treated as the `combined` track;
107
+ - a directory containing any of `combined.bed(.gz)`, `hp1.bed(.gz)`, or
108
+ `hp2.bed(.gz)`; or
109
+ - a prefix resolving to `<prefix>.combined.bed.gz`,
110
+ `<prefix>.hap1.bed.gz`, and/or `<prefix>.hap2.bed.gz`.
111
+
112
+ When a modkit BED contains both `+` and `-` records, `mdb create`
113
+ automatically builds separate strand tracks.
114
+
115
+ PacBio (prefix or a specific BED file):
116
+
117
+ ```bash
118
+ mdb create \
119
+ -p pacbio \
120
+ -n GRCh38.cpg_index.npz \
121
+ -b /path/to/pacbio_prefix \
122
+ -o sample_pb.smdb \
123
+ -c 5 \
124
+ --sample-id SAMPLE_PB
125
+ ```
126
+
127
+ A PacBio prefix resolves to `<prefix>.combined.bed(.gz)`,
128
+ `<prefix>.hap1.bed(.gz)`, and `<prefix>.hap2.bed(.gz)`. Pointing directly to
129
+ a BED/BED.GZ file creates a `combined` track. Prefix or direct-file input is
130
+ recommended. Do not rely on PacBio directory discovery in the current
131
+ implementation; pass the prefix or a specific BED path instead.
132
+
133
+ ### 3) Merge sample bundles into a cohort
134
+
135
+ Default backend (`zarr`):
136
+
137
+ ```bash
138
+ mdb merge \
139
+ -i sample_ont.smdb sample_pb.smdb \
140
+ -o cohort.mmdb \
141
+ --workers 2 \
142
+ --block-size 64 \
143
+ --zarr-row-chunk 65536 \
144
+ --zarr-codec zstd \
145
+ --zarr-clevel 5 \
146
+ --zarr-shuffle bitshuffle \
147
+ --zarr-codec-threads 4
148
+ ```
149
+
150
+ NPY backend (explicit):
151
+
152
+ ```bash
153
+ mdb merge \
154
+ -i sample_ont.smdb sample_pb.smdb \
155
+ -o cohort_npy.mmdb \
156
+ --cohort-backend npy \
157
+ --workers 2 \
158
+ --block-size 64
159
+ ```
160
+
161
+ Build modifiedC view (`5mC + 5hmC` where available):
162
+
163
+ ```bash
164
+ mdb merge -i sample_ont.smdb sample_pb.smdb -o cohort_modifiedc.mmdb -m
165
+ ```
166
+
167
+ ### 4) Append new samples to existing cohort
168
+
169
+ ```bash
170
+ mdb append \
171
+ -c cohort.mmdb \
172
+ -i new_sample1.smdb new_sample2.smdb
173
+ ```
174
+
175
+ ### 5) Query values
176
+
177
+ Point query:
178
+
179
+ ```bash
180
+ mdb query \
181
+ -i cohort.mmdb \
182
+ --sample-id SAMPLE_PB \
183
+ --assay 5mC \
184
+ --haplotype combined \
185
+ --strand combined \
186
+ --locus chr1:10469
187
+ ```
188
+
189
+ Range query:
190
+
191
+ ```bash
192
+ mdb query \
193
+ -i cohort.mmdb \
194
+ --sample-id SAMPLE_PB \
195
+ --assay 5mC \
196
+ --haplotype combined \
197
+ --strand combined \
198
+ --region chr1:10469-12000
199
+ ```
200
+
201
+ ### 6) Run PCA on cohort view
202
+
203
+ ```bash
204
+ mdb pca \
205
+ -i cohort.mmdb \
206
+ -o cohort_pca \
207
+ --assay 5mC \
208
+ --haplotype combined \
209
+ --strand combined \
210
+ --n_pcs 10 \
211
+ --frac_cpgs 0.1 \
212
+ --plot_style studio \
213
+ --plot_style_variants \
214
+ --outlier_detect \
215
+ --outlier_alpha 0.999 \
216
+ --outlier_n_pcs 10
217
+ ```
218
+
219
+ When `--outlier_detect` is enabled, `pca` also writes:
220
+
221
+ - `outlier_report.tsv` (all samples with outlier columns),
222
+ - `outliers_only.tsv` (flagged samples),
223
+ - `pca_with_outliers_marked.html`,
224
+ - `pca_no_outliers.html`,
225
+ - `pca_pairplot_no_outliers.html`,
226
+ - `pca_pairplot_no_outliers.png`.
227
+
228
+ The no-outlier plots require at least one inlier, and the no-outlier pairplot
229
+ requires at least two. Plotly PNG exports are written only when a compatible
230
+ image engine is available.
231
+
232
+ When `--plot_style_variants` is enabled, extra style comparison HTML files are written (for example `pca_studio.html`, `pca_sunrise.html`, `pca_paper.html` depending on selected primary style).
233
+
234
+ Standard PCA outputs include:
235
+
236
+ - `embedding.tsv`,
237
+ - `params.json`,
238
+ - `pca_umap.log`,
239
+ - `pca.html`,
240
+ - `pca_pairplot.html`,
241
+ - `pca_pairplot.png`.
242
+
243
+ Add `-m metadata.tsv` to make aligned metadata columns available for plot
244
+ coloring. Add `--umap` to calculate a UMAP embedding and write `umap.html`.
245
+
246
+ Restrict PCA to CpGs overlapping BED intervals:
247
+
248
+ ```bash
249
+ mdb pca \
250
+ -i cohort.mmdb \
251
+ -o cohort_pca_regions \
252
+ --cpg-bed regions.bed
253
+ ```
254
+
255
+ Alternatively, average all CpGs within each BED interval and run PCA on the
256
+ resulting region-by-sample matrix:
257
+
258
+ ```bash
259
+ mdb pca \
260
+ -i cohort.mmdb \
261
+ -o cohort_pca_region_means \
262
+ --cpg-bed regions.bed \
263
+ --cpg-bed-agg
264
+ ```
265
+
266
+ The aggregation mode also writes `region_avg.tsv`. Both BED modes require a
267
+ current cohort store (`.mmdb`) because legacy flat NPY folders do not contain
268
+ genomic positions.
269
+
270
+ ### 7) Summarize cohort stats
271
+
272
+ ```bash
273
+ mdb stats \
274
+ -i cohort.mmdb \
275
+ -o cohort_stats \
276
+ -m metadata.tsv \
277
+ --assay all \
278
+ --haplotype all \
279
+ --strand all \
280
+ --plot_style studio
281
+ ```
282
+
283
+ `stats` writes:
284
+
285
+ - `sample_stats.tsv`: one row per sample per cohort track with observed CpG counts and fractions.
286
+ - `track_stats.tsv`: per-track summary across assay / haplotype / strand subclasses.
287
+ - `metadata_group_stats.tsv`: per-track grouped summaries for plotted metadata columns.
288
+ - `cpg_count_scatter.html`: interactive sample-rank scatter with PCA-style color dropdown.
289
+ - `frac_cpg_scatter.html`: corresponding sample-rank scatter for the observed CpG fraction.
290
+ - `cpg_count_by_track.html`: interactive box/point plot of observed CpGs stratified by metadata across tracks.
291
+ - `frac_cpg_by_track.html`: corresponding box/point plot for the observed CpG fraction.
292
+ - `params.json` and `stats.log`: run parameters and detailed logging.
293
+
294
+ `metadata_group_stats.tsv` is written only when the aligned metadata provides
295
+ usable grouping columns. PNG versions of the plots are written when Plotly PNG
296
+ export is available.
297
+
298
+ ### 8) Build binned methylation profile HTML
299
+
300
+ ```bash
301
+ mdb viz \
302
+ -i /path/to/cohort.mmdb \
303
+ -o cohort_viz \
304
+ -m metadata.tsv \
305
+ --assay 5mC,5hmC \
306
+ --haplotype combined \
307
+ --strand combined \
308
+ --bin-length 100000
309
+ ```
310
+
311
+ `viz` also accepts a directory of `.smdb` bundles, bundle globs, or a `.txt` manifest of bundle paths. It writes:
312
+
313
+ - `methylation_viz.html`: interactive heatmap HTML for switching tracks, chromosomes, metadata-group rows, and selected sample rows.
314
+ - `binned_profiles.npz`: compressed sample-by-bin matrices for each selected track.
315
+ - `sample_metadata_aligned.tsv`: aligned metadata used by the HTML.
316
+ - `tissue_name_group_profiles.tsv.gz`: precomputed tissue-level mean profiles when `tissue_name` metadata is available.
317
+ - `viz_manifest.json`: run parameters, resolved inputs, selected tracks, and output paths.
318
+ - `viz.log`: detailed run log.
319
+
320
+ ### 9) Plot a locus-focused methylation profile from a cohort
321
+
322
+ ```bash
323
+ mdb plot \
324
+ -i cohort.mmdb \
325
+ -o locus_plot \
326
+ -r chr15:80150000-80200000 \
327
+ -m metadata.tsv \
328
+ --sample-id sampleA,sampleB \
329
+ --assay 5mC,5hmC \
330
+ --combine-tracks sum \
331
+ --window-size 20 \
332
+ --color-by tissue_name
333
+ ```
334
+
335
+ `plot` is separate from `viz`: it focuses on one genomic interval from a cohort store and writes an interactive line-profile HTML with controls for:
336
+
337
+ - selected track view(s) within the requested assay / haplotype / strand filters
338
+ - sample-line mode or grouped-mean mode using the chosen metadata color field
339
+ - browser-side sliding-window smoothing
340
+ - metadata-driven recoloring when aligned metadata is provided
341
+
342
+ It writes:
343
+
344
+ - `methylation_plot.html`: interactive locus-focused profile viewer with track, color, grouping, and window controls.
345
+ - `region_profiles.npz`: compressed CpG-level matrices for the plotted region and selected tracks.
346
+ - `smoothed_profiles.tsv.gz`: smoothed profile coordinates and values using the initial command-line window size.
347
+ - `sample_metadata_aligned.tsv`: aligned metadata used by the HTML controls.
348
+ - `plot_manifest.json`: run parameters and output paths.
349
+ - `plot.log`: detailed run log.
350
+
351
+ ### 10) Run PCA on ONT ASM segments
352
+
353
+ `asmpca` expects modkit DMR segment BEDs containing fields such as `name`,
354
+ `effect_size`, and `cohen_h`:
355
+
356
+ ```bash
357
+ mdb asmpca \
358
+ -i sample1.segments.bed.gz sample2.segments.bed.gz \
359
+ -o asm_pca \
360
+ -m metadata.tsv \
361
+ --feature-mode dmr_location \
362
+ --min-region-samples 2 \
363
+ --n-pcs 10
364
+ ```
365
+
366
+ Inputs may be BED paths, directories, globs, or a text manifest of paths. By
367
+ default, cohort DMR regions are the merged union of input rows whose
368
+ `name` is `different`. Supply `--dmr-regions regions.bed` to use an external
369
+ region set.
370
+
371
+ The default `dmr_location` mode uses binary DMR presence as the PCA features.
372
+ To project a segment statistic onto the regions instead:
373
+
374
+ ```bash
375
+ mdb asmpca \
376
+ -i asm_segments/*.bed.gz \
377
+ -o asm_metric_pca \
378
+ --feature-mode segment_metric \
379
+ --metric effect_size
380
+ ```
381
+
382
+ Core outputs are `embedding.tsv`, `dmr_regions_used.bed`, `params.json`,
383
+ `pca.html`, `pca_pairplot.html`, `pca_pairplot.png`, and `pca_umap.log`.
384
+
385
+ ### 11) Detect strand-biased methylation hotspots
386
+
387
+ `strand` requires a cohort store containing matching `plus` and `minus` views
388
+ for the requested assay and haplotype:
389
+
390
+ ```bash
391
+ mdb strand \
392
+ -i cohort.mmdb \
393
+ -o strand_hotspots \
394
+ -m metadata.tsv \
395
+ --group-by tissue_broad \
396
+ --assay 5hmC \
397
+ --haplotype combined \
398
+ --min-paired-frac 0.8 \
399
+ --min-mean-total 0.005 \
400
+ --cluster-gap-bp 1000 \
401
+ --top-n-hotspots 500 \
402
+ --workers 4
403
+ ```
404
+
405
+ The command scores strand imbalance genome-wide, clusters adjacent
406
+ same-direction CpGs, and optionally repeats hotspot calling within metadata
407
+ groups. Core outputs include:
408
+
409
+ - `per_sample_metrics.tsv.gz`,
410
+ - `hotspots_global.tsv` and `hotspots_global.bed`,
411
+ - `hotspot_sample_profiles.tsv.gz` when hotspot profiles are available,
412
+ - per-group hotspot TSV/BED and summary files when `--group-by` is used,
413
+ - interactive strand-bias and hotspot HTML reports,
414
+ - `params.json` and `strand.log`.
415
+
416
+ ## Important Notes
417
+
418
+ - `create --reader` currently defaults to `scan` and the active create path uses scan-based reading.
419
+ - `create --workers` is currently accepted by the CLI but is not used by the active scan-based create path.
420
+ - `merge` and `append` require sample bundles created by current `mdb create` (manifest-based `.smdb` layout).
421
+ - `pca` now supports both current cohort stores (`.mmdb`) and legacy flat merged `.npy` folders.
422
+ - PCA color categories are sorted before plotting so colors stay stable across full vs no-outlier comparisons.
423
+
424
+ ## License
425
+
426
+ MIT (`LICENSE`).