tadclust 0.1.0a1__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.
- tadclust-0.1.0a1/.gitignore +17 -0
- tadclust-0.1.0a1/LICENSE +21 -0
- tadclust-0.1.0a1/PKG-INFO +159 -0
- tadclust-0.1.0a1/README.md +130 -0
- tadclust-0.1.0a1/pyproject.toml +72 -0
- tadclust-0.1.0a1/src/tadclust/__init__.py +11 -0
- tadclust-0.1.0a1/src/tadclust/__main__.py +7 -0
- tadclust-0.1.0a1/src/tadclust/bedlike.py +102 -0
- tadclust-0.1.0a1/src/tadclust/cli.py +210 -0
- tadclust-0.1.0a1/src/tadclust/clustering.py +66 -0
- tadclust-0.1.0a1/src/tadclust/config.py +148 -0
- tadclust-0.1.0a1/src/tadclust/domains.py +725 -0
- tadclust-0.1.0a1/src/tadclust/errors.py +61 -0
- tadclust-0.1.0a1/src/tadclust/matrix.py +534 -0
- tadclust-0.1.0a1/src/tadclust/mcool.py +157 -0
- tadclust-0.1.0a1/src/tadclust/metadata.py +78 -0
- tadclust-0.1.0a1/src/tadclust/parallel.py +187 -0
- tadclust-0.1.0a1/src/tadclust/pipeline.py +941 -0
- tadclust-0.1.0a1/src/tadclust/processes.py +52 -0
- tadclust-0.1.0a1/src/tadclust/py.typed +0 -0
- tadclust-0.1.0a1/src/tadclust/schema.py +140 -0
- tadclust-0.1.0a1/src/tadclust/signals.py +55 -0
- tadclust-0.1.0a1/src/tadclust/sources.py +127 -0
- tadclust-0.1.0a1/src/tadclust/tads.py +224 -0
- tadclust-0.1.0a1/src/tadclust/track.py +207 -0
- tadclust-0.1.0a1/src/tadclust/trans.py +666 -0
- tadclust-0.1.0a1/src/tadclust/trees.py +711 -0
- tadclust-0.1.0a1/src/tadclust/writer.py +312 -0
- tadclust-0.1.0a1/tests/conftest.py +207 -0
- tadclust-0.1.0a1/tests/interrupt_cli_helper.py +54 -0
- tadclust-0.1.0a1/tests/test_ab.py +409 -0
- tadclust-0.1.0a1/tests/test_cli.py +561 -0
- tadclust-0.1.0a1/tests/test_config.py +105 -0
- tadclust-0.1.0a1/tests/test_domains.py +557 -0
- tadclust-0.1.0a1/tests/test_flat_tree.py +38 -0
- tadclust-0.1.0a1/tests/test_matrix.py +377 -0
- tadclust-0.1.0a1/tests/test_mcool.py +113 -0
- tadclust-0.1.0a1/tests/test_metadata.py +86 -0
- tadclust-0.1.0a1/tests/test_packaging.py +21 -0
- tadclust-0.1.0a1/tests/test_pipeline.py +917 -0
- tadclust-0.1.0a1/tests/test_schema.py +452 -0
- tadclust-0.1.0a1/tests/test_tads.py +237 -0
- tadclust-0.1.0a1/tests/test_track.py +205 -0
- tadclust-0.1.0a1/tests/test_trans.py +540 -0
- tadclust-0.1.0a1/tests/test_trees.py +418 -0
- tadclust-0.1.0a1/tests/test_writer.py +554 -0
- tadclust-0.1.0a1/tests/worker_signal_helper.py +22 -0
tadclust-0.1.0a1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) snsinfu
|
|
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.
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tadclust
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Hierarchical clustering of TADs
|
|
5
|
+
Project-URL: Homepage, https://github.com/snsinfu/tadclust
|
|
6
|
+
Project-URL: Repository, https://github.com/snsinfu/tadclust
|
|
7
|
+
Project-URL: Issues, https://github.com/snsinfu/tadclust/issues
|
|
8
|
+
Author-email: snsinfu <snsinfu@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: Hi-C,TAD,bioinformatics,hierarchical clustering,topologically associating domains
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Requires-Dist: cooler<0.11,>=0.10.4
|
|
21
|
+
Requires-Dist: h5py<4,>=3.10
|
|
22
|
+
Requires-Dist: numpy<3,>=2
|
|
23
|
+
Requires-Dist: polars<2,>=1.30
|
|
24
|
+
Requires-Dist: pyarrow
|
|
25
|
+
Requires-Dist: scikit-learn<2,>=1.5
|
|
26
|
+
Requires-Dist: scipy<2,>=1.11
|
|
27
|
+
Requires-Dist: threadpoolctl>=3
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# TADCLUST - Hierarchical clustering of TADs
|
|
31
|
+
|
|
32
|
+
Given any TAD annotation and a contact map, TADCLUST applies hierarchical clustering of the TADs based on TAD-level O/E matrix, to build a tree on each chromosome. The clustering analysis identifies a higher-order organization of locally-defined TADs, which is correlated to chromosome compartmentalization.
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
TADCLUST is experimental and not released yet. Git clone, and use uv to try it out:
|
|
39
|
+
|
|
40
|
+
```console
|
|
41
|
+
$ git clone https://github.com/snsinfu/tadclust.git
|
|
42
|
+
$ cd tadclust
|
|
43
|
+
$ uv run tadclust -h
|
|
44
|
+
````
|
|
45
|
+
|
|
46
|
+
Or, use docker (currently not regularly updated):
|
|
47
|
+
|
|
48
|
+
```console
|
|
49
|
+
$ docker run --rm ghcr.io/snsinfu/tadclust tadclust -h
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Installing to a uv/pixi project from the GitHub repository:
|
|
53
|
+
|
|
54
|
+
```console
|
|
55
|
+
$ uv add git+https://github.com/snsinfu/tadclust.git
|
|
56
|
+
$ pixi add --pypi --git https://github.com/snsinfu/tadclust.git tadclust
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
Give an mcool contact matrix, a TAD annotation table, optionally an A/B feature track, and the output directory.
|
|
62
|
+
|
|
63
|
+
```console
|
|
64
|
+
$ tadclust \
|
|
65
|
+
--output-dir out \
|
|
66
|
+
--mcool contact_map.mcool \
|
|
67
|
+
--tad-table tad_annotation.bed \
|
|
68
|
+
--ab-feature-track active_signal.bedGraph
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The TAD table can be any disjoint or hierarchical TAD annotation in the BED3+ format, or a TSV with `chrom, start, end` columns. TADCLUST selects an optimal set of sub-TADs for clustering if the annotation is hierarchical. `--ab-feature-track` is any 4-column, BedGraph-like table or BED5+ where the annotated score is larger in the A compartment (like ATAC, H4ac, and gene density). It is used to define the A/B compartment boundary, to enable A/B annotation of TADs and cluster nodes.
|
|
72
|
+
|
|
73
|
+
### Options
|
|
74
|
+
|
|
75
|
+
| option | default | description |
|
|
76
|
+
|:--------------------|:--------|:------------|
|
|
77
|
+
| --tree-branch-k K | 2 | Split count of recursive k-means. |
|
|
78
|
+
| --with-trans | no | Enable trans-chromosomal clustering (experimental and expensive). |
|
|
79
|
+
| --with-residuals | no | Fill gaps with `residual` pseudo-domains. |
|
|
80
|
+
| --overwrite | no | Allow overwriting existing files in the output directory. |
|
|
81
|
+
| --workers N | 1 | Run analysis in parallel using N processes. |
|
|
82
|
+
| --seed N | 0 | Random seed for reproducibility. |
|
|
83
|
+
|
|
84
|
+
### Output
|
|
85
|
+
|
|
86
|
+
Results are stored as parquet/tsv tables and a json summary in the output directory:
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
out/
|
|
90
|
+
├── cis_nodes.parquet
|
|
91
|
+
├── cis_nodes_ab.parquet # optional
|
|
92
|
+
├── cis_leaves.parquet
|
|
93
|
+
├── cis_leaves.tsv
|
|
94
|
+
├── cis_leaves_ab.parquet # optional
|
|
95
|
+
├── cis_leaves_ab.tsv # optional
|
|
96
|
+
├── trans_nodes.parquet # optional
|
|
97
|
+
└── manifest.json
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Main output is `cis_nodes.parquet` which contains full tree nodes for all chromosomes. `cis_leaves.{parquet,tsv}` are leaf-only subsets created for convenience. `_ab` suffixed tables store the result for A/B-rooted clustering variant. A copy of `manifest.json` is stored in every parquet file as a parquet metadata keyed by "tadclust"; the manifest contains version info, algorithm params, and QC stats.
|
|
101
|
+
|
|
102
|
+
Columns in the main table:
|
|
103
|
+
|
|
104
|
+
| column | type | description |
|
|
105
|
+
|:------------------|:-----------------|:------------|
|
|
106
|
+
| chrom | string | The chromosome the node belongs to. |
|
|
107
|
+
| node_id | int | Unique ID of the node within the table. |
|
|
108
|
+
| parent_id | int | Parent's node_id or null. |
|
|
109
|
+
| child_ids | int[] | Children's node_ids or empty. |
|
|
110
|
+
| depth | int | Depth of the node, 0 = root. |
|
|
111
|
+
| is_leaf | bool | (leaf) True or (branch) false. |
|
|
112
|
+
| unit_id | int | (leaf) Unique ID of the clustered leaf unit or (branch) null. |
|
|
113
|
+
| unit_kind | tad/residual/bin | (leaf) Type of the unit or (branch) null. |
|
|
114
|
+
| unit_start | int | (leaf) Start coord of the unit or (branch) null. |
|
|
115
|
+
| unit_end | int | (leaf) End coord of the unit or (branch) null. |
|
|
116
|
+
| tad_table_row | int | (leaf) Row index of the TAD table or (branch/non-TAD) null. |
|
|
117
|
+
| root_label | chrom/A/B/genome | Root of the tree the node belongs to. |
|
|
118
|
+
| path | int[] | Branches to take to arrive at the node from the root, like \[0,1,0,1,1,1\]. |
|
|
119
|
+
| path_label | string | Root + path as a dot-delimited string, like "chrom.0.1.0.1.1.1". |
|
|
120
|
+
| ab_label | A/B | A/B classification (ab_signal <=> 0) of the node. |
|
|
121
|
+
| ab_signal | float | A/B compartment signal for the node. |
|
|
122
|
+
| ab_frac_a | float | Fraction of A leaves under the node. |
|
|
123
|
+
| ab_frac_b | float | Fraction of B leaves under the node. |
|
|
124
|
+
| ab_purity | float | Max(ab_frac_a, ab_frac_b). |
|
|
125
|
+
| pc | float[] | (leaf) PC feature vector or (branch) centroid. |
|
|
126
|
+
| n_units | int | Number of units under the node. |
|
|
127
|
+
| n_working_bins | int | Number of working bins covered by the units under the node. |
|
|
128
|
+
| node_oe | float | Mean log2 O/E over all units and unit pairs under the node. |
|
|
129
|
+
| node_internal_sse | float | Feature-space SSE of leaves under the node. |
|
|
130
|
+
| node_split_gain | float | Relative SSE cost of splitting at this node. |
|
|
131
|
+
| stop_reason | degenerate | Reason of abruptly stopped splitting or null. |
|
|
132
|
+
|
|
133
|
+
Notes:
|
|
134
|
+
|
|
135
|
+
- Node's children (.0 .1) are ordered by centroid PC1.
|
|
136
|
+
- When `--ab-feature-track` is given, the PC1 axis itself is oriented towards higher A signal. So nodes are B-to-A ordered.
|
|
137
|
+
- In the tsv outputs, array/list columns are not emitted, and nulls are replaced by (int) -1, (float) NaN, and (string) "NA".
|
|
138
|
+
|
|
139
|
+
## Method notes
|
|
140
|
+
|
|
141
|
+
**TAD-level L2-normalized log-O/E** is diagonalized (without centering; zero is the natural center for log-O/E) to produce principal components. This PC feature space is hierarchically clustered like CALDER. The first branch gives a similar result to the conventional compartment analysis, and deeper branches describe subcompartment organization.
|
|
142
|
+
|
|
143
|
+
When **hierarchical TAD annotation** is given (i.e., there are nested intervals), the optimal levels of sub-TAD hierarchies are decided by dynamic programming so that within-TAD variance (among sub-TADs and gaps) is minimized in the feature space. The selected (sub-)TADs are output as leaf units with `unit_kind` = "tad". Besides clustering, this selection of the "most coherent" sub-TADs can be useful on its own.
|
|
144
|
+
|
|
145
|
+
**Recursive K-means** seems superior to other methods like Ward, HDBSCAN, spectral, etc. for clustering TADs. This is probably because the domains in the feature space are not cleanly separable (strong noise, broad lobes, and biological intermediates). Bisection (k=2) is good enough to maintain A/B coherence in subtree. k=3 performs similarly good.
|
|
146
|
+
|
|
147
|
+
For **A/B compartment** annotation, an auxiliary binary split of an augmented feature space is made. Bisection of the raw PC space does not reliably give a useful "A/B"-like clusters except for mammalian datasets, so the PC space needs to be augmented by a reference signal track (`--ab-feature-track`; ATAC, H4ac, and gene density tracks work well) to determine the A/B decision boundary and the orientation. Once the boundary is determined, it is projected onto the original PC space, and the TADs are A/B-annotated. This auxiliary split is done solely for annotation and it does not affect the cluster hierarchy.
|
|
148
|
+
|
|
149
|
+
The **A/B signal** is the 1D projection of the node's PC centroid onto the A-B axis in the PC space (the axis along the A and B centroids). This is a generalization of the conventional PC1 compartment signal to the bisected, higher-dimensional PC space. The magnitude of the signal is informative: it is the distance from the decision boundary, so close-to-zero signal indicates poor compartmentalization. The PC space is standardized in A/B analysis, so the typical values of the signal distribute in the ~3 sigma range, or within approximtely \[-3,3\], so the "normal" magnitude would be around ~1.
|
|
150
|
+
|
|
151
|
+
**Genome-wide clustering** (experimental) is also done when `--with-trans` flag is passed. Genome-wide, trans-chrom O/E is built using the cis-defined TADs as units, diagonalized, and recursively clustered. The trans tree tends to capture chromosome territories or telomere clustering rather than genome-wide A/B compartments. Still, the `unit_id` is shared between the cis table, so one can JOIN cis and trans tables to examine cis-determined A/B on the trans tree.
|
|
152
|
+
|
|
153
|
+
## AI disclosure
|
|
154
|
+
|
|
155
|
+
LLM coding agent is used for development. Algorithm is designed and instructed by me, then implemented by AI, benchmarking on real datasets. I review every changeset and adjust it until it looks fine. This README is 100% human-written.
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
MIT
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# TADCLUST - Hierarchical clustering of TADs
|
|
2
|
+
|
|
3
|
+
Given any TAD annotation and a contact map, TADCLUST applies hierarchical clustering of the TADs based on TAD-level O/E matrix, to build a tree on each chromosome. The clustering analysis identifies a higher-order organization of locally-defined TADs, which is correlated to chromosome compartmentalization.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
TADCLUST is experimental and not released yet. Git clone, and use uv to try it out:
|
|
10
|
+
|
|
11
|
+
```console
|
|
12
|
+
$ git clone https://github.com/snsinfu/tadclust.git
|
|
13
|
+
$ cd tadclust
|
|
14
|
+
$ uv run tadclust -h
|
|
15
|
+
````
|
|
16
|
+
|
|
17
|
+
Or, use docker (currently not regularly updated):
|
|
18
|
+
|
|
19
|
+
```console
|
|
20
|
+
$ docker run --rm ghcr.io/snsinfu/tadclust tadclust -h
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Installing to a uv/pixi project from the GitHub repository:
|
|
24
|
+
|
|
25
|
+
```console
|
|
26
|
+
$ uv add git+https://github.com/snsinfu/tadclust.git
|
|
27
|
+
$ pixi add --pypi --git https://github.com/snsinfu/tadclust.git tadclust
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
Give an mcool contact matrix, a TAD annotation table, optionally an A/B feature track, and the output directory.
|
|
33
|
+
|
|
34
|
+
```console
|
|
35
|
+
$ tadclust \
|
|
36
|
+
--output-dir out \
|
|
37
|
+
--mcool contact_map.mcool \
|
|
38
|
+
--tad-table tad_annotation.bed \
|
|
39
|
+
--ab-feature-track active_signal.bedGraph
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The TAD table can be any disjoint or hierarchical TAD annotation in the BED3+ format, or a TSV with `chrom, start, end` columns. TADCLUST selects an optimal set of sub-TADs for clustering if the annotation is hierarchical. `--ab-feature-track` is any 4-column, BedGraph-like table or BED5+ where the annotated score is larger in the A compartment (like ATAC, H4ac, and gene density). It is used to define the A/B compartment boundary, to enable A/B annotation of TADs and cluster nodes.
|
|
43
|
+
|
|
44
|
+
### Options
|
|
45
|
+
|
|
46
|
+
| option | default | description |
|
|
47
|
+
|:--------------------|:--------|:------------|
|
|
48
|
+
| --tree-branch-k K | 2 | Split count of recursive k-means. |
|
|
49
|
+
| --with-trans | no | Enable trans-chromosomal clustering (experimental and expensive). |
|
|
50
|
+
| --with-residuals | no | Fill gaps with `residual` pseudo-domains. |
|
|
51
|
+
| --overwrite | no | Allow overwriting existing files in the output directory. |
|
|
52
|
+
| --workers N | 1 | Run analysis in parallel using N processes. |
|
|
53
|
+
| --seed N | 0 | Random seed for reproducibility. |
|
|
54
|
+
|
|
55
|
+
### Output
|
|
56
|
+
|
|
57
|
+
Results are stored as parquet/tsv tables and a json summary in the output directory:
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
out/
|
|
61
|
+
├── cis_nodes.parquet
|
|
62
|
+
├── cis_nodes_ab.parquet # optional
|
|
63
|
+
├── cis_leaves.parquet
|
|
64
|
+
├── cis_leaves.tsv
|
|
65
|
+
├── cis_leaves_ab.parquet # optional
|
|
66
|
+
├── cis_leaves_ab.tsv # optional
|
|
67
|
+
├── trans_nodes.parquet # optional
|
|
68
|
+
└── manifest.json
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Main output is `cis_nodes.parquet` which contains full tree nodes for all chromosomes. `cis_leaves.{parquet,tsv}` are leaf-only subsets created for convenience. `_ab` suffixed tables store the result for A/B-rooted clustering variant. A copy of `manifest.json` is stored in every parquet file as a parquet metadata keyed by "tadclust"; the manifest contains version info, algorithm params, and QC stats.
|
|
72
|
+
|
|
73
|
+
Columns in the main table:
|
|
74
|
+
|
|
75
|
+
| column | type | description |
|
|
76
|
+
|:------------------|:-----------------|:------------|
|
|
77
|
+
| chrom | string | The chromosome the node belongs to. |
|
|
78
|
+
| node_id | int | Unique ID of the node within the table. |
|
|
79
|
+
| parent_id | int | Parent's node_id or null. |
|
|
80
|
+
| child_ids | int[] | Children's node_ids or empty. |
|
|
81
|
+
| depth | int | Depth of the node, 0 = root. |
|
|
82
|
+
| is_leaf | bool | (leaf) True or (branch) false. |
|
|
83
|
+
| unit_id | int | (leaf) Unique ID of the clustered leaf unit or (branch) null. |
|
|
84
|
+
| unit_kind | tad/residual/bin | (leaf) Type of the unit or (branch) null. |
|
|
85
|
+
| unit_start | int | (leaf) Start coord of the unit or (branch) null. |
|
|
86
|
+
| unit_end | int | (leaf) End coord of the unit or (branch) null. |
|
|
87
|
+
| tad_table_row | int | (leaf) Row index of the TAD table or (branch/non-TAD) null. |
|
|
88
|
+
| root_label | chrom/A/B/genome | Root of the tree the node belongs to. |
|
|
89
|
+
| path | int[] | Branches to take to arrive at the node from the root, like \[0,1,0,1,1,1\]. |
|
|
90
|
+
| path_label | string | Root + path as a dot-delimited string, like "chrom.0.1.0.1.1.1". |
|
|
91
|
+
| ab_label | A/B | A/B classification (ab_signal <=> 0) of the node. |
|
|
92
|
+
| ab_signal | float | A/B compartment signal for the node. |
|
|
93
|
+
| ab_frac_a | float | Fraction of A leaves under the node. |
|
|
94
|
+
| ab_frac_b | float | Fraction of B leaves under the node. |
|
|
95
|
+
| ab_purity | float | Max(ab_frac_a, ab_frac_b). |
|
|
96
|
+
| pc | float[] | (leaf) PC feature vector or (branch) centroid. |
|
|
97
|
+
| n_units | int | Number of units under the node. |
|
|
98
|
+
| n_working_bins | int | Number of working bins covered by the units under the node. |
|
|
99
|
+
| node_oe | float | Mean log2 O/E over all units and unit pairs under the node. |
|
|
100
|
+
| node_internal_sse | float | Feature-space SSE of leaves under the node. |
|
|
101
|
+
| node_split_gain | float | Relative SSE cost of splitting at this node. |
|
|
102
|
+
| stop_reason | degenerate | Reason of abruptly stopped splitting or null. |
|
|
103
|
+
|
|
104
|
+
Notes:
|
|
105
|
+
|
|
106
|
+
- Node's children (.0 .1) are ordered by centroid PC1.
|
|
107
|
+
- When `--ab-feature-track` is given, the PC1 axis itself is oriented towards higher A signal. So nodes are B-to-A ordered.
|
|
108
|
+
- In the tsv outputs, array/list columns are not emitted, and nulls are replaced by (int) -1, (float) NaN, and (string) "NA".
|
|
109
|
+
|
|
110
|
+
## Method notes
|
|
111
|
+
|
|
112
|
+
**TAD-level L2-normalized log-O/E** is diagonalized (without centering; zero is the natural center for log-O/E) to produce principal components. This PC feature space is hierarchically clustered like CALDER. The first branch gives a similar result to the conventional compartment analysis, and deeper branches describe subcompartment organization.
|
|
113
|
+
|
|
114
|
+
When **hierarchical TAD annotation** is given (i.e., there are nested intervals), the optimal levels of sub-TAD hierarchies are decided by dynamic programming so that within-TAD variance (among sub-TADs and gaps) is minimized in the feature space. The selected (sub-)TADs are output as leaf units with `unit_kind` = "tad". Besides clustering, this selection of the "most coherent" sub-TADs can be useful on its own.
|
|
115
|
+
|
|
116
|
+
**Recursive K-means** seems superior to other methods like Ward, HDBSCAN, spectral, etc. for clustering TADs. This is probably because the domains in the feature space are not cleanly separable (strong noise, broad lobes, and biological intermediates). Bisection (k=2) is good enough to maintain A/B coherence in subtree. k=3 performs similarly good.
|
|
117
|
+
|
|
118
|
+
For **A/B compartment** annotation, an auxiliary binary split of an augmented feature space is made. Bisection of the raw PC space does not reliably give a useful "A/B"-like clusters except for mammalian datasets, so the PC space needs to be augmented by a reference signal track (`--ab-feature-track`; ATAC, H4ac, and gene density tracks work well) to determine the A/B decision boundary and the orientation. Once the boundary is determined, it is projected onto the original PC space, and the TADs are A/B-annotated. This auxiliary split is done solely for annotation and it does not affect the cluster hierarchy.
|
|
119
|
+
|
|
120
|
+
The **A/B signal** is the 1D projection of the node's PC centroid onto the A-B axis in the PC space (the axis along the A and B centroids). This is a generalization of the conventional PC1 compartment signal to the bisected, higher-dimensional PC space. The magnitude of the signal is informative: it is the distance from the decision boundary, so close-to-zero signal indicates poor compartmentalization. The PC space is standardized in A/B analysis, so the typical values of the signal distribute in the ~3 sigma range, or within approximtely \[-3,3\], so the "normal" magnitude would be around ~1.
|
|
121
|
+
|
|
122
|
+
**Genome-wide clustering** (experimental) is also done when `--with-trans` flag is passed. Genome-wide, trans-chrom O/E is built using the cis-defined TADs as units, diagonalized, and recursively clustered. The trans tree tends to capture chromosome territories or telomere clustering rather than genome-wide A/B compartments. Still, the `unit_id` is shared between the cis table, so one can JOIN cis and trans tables to examine cis-determined A/B on the trans tree.
|
|
123
|
+
|
|
124
|
+
## AI disclosure
|
|
125
|
+
|
|
126
|
+
LLM coding agent is used for development. Algorithm is designed and instructed by me, then implemented by AI, benchmarking on real datasets. I review every changeset and adjust it until it looks fine. This README is 100% human-written.
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tadclust"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Hierarchical clustering of TADs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "snsinfu", email = "snsinfu@gmail.com" }]
|
|
14
|
+
keywords = [
|
|
15
|
+
"Hi-C",
|
|
16
|
+
"TAD",
|
|
17
|
+
"topologically associating domains",
|
|
18
|
+
"hierarchical clustering",
|
|
19
|
+
"bioinformatics",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 3 - Alpha",
|
|
23
|
+
"Intended Audience :: Science/Research",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Topic :: Scientific/Engineering :: Bio-Informatics",
|
|
28
|
+
"Typing :: Typed",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"cooler>=0.10.4,<0.11",
|
|
32
|
+
"h5py>=3.10,<4",
|
|
33
|
+
"numpy>=2,<3",
|
|
34
|
+
"polars>=1.30,<2",
|
|
35
|
+
"pyarrow",
|
|
36
|
+
"scikit-learn>=1.5,<2",
|
|
37
|
+
"scipy>=1.11,<2",
|
|
38
|
+
"threadpoolctl>=3",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
tadclust = "tadclust.cli:main"
|
|
43
|
+
|
|
44
|
+
[project.urls]
|
|
45
|
+
Homepage = "https://github.com/snsinfu/tadclust"
|
|
46
|
+
Repository = "https://github.com/snsinfu/tadclust"
|
|
47
|
+
Issues = "https://github.com/snsinfu/tadclust/issues"
|
|
48
|
+
|
|
49
|
+
[dependency-groups]
|
|
50
|
+
dev = [
|
|
51
|
+
"pandas",
|
|
52
|
+
"pyright",
|
|
53
|
+
"pytest",
|
|
54
|
+
"ruff",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.hatch.version]
|
|
58
|
+
path = "src/tadclust/__init__.py"
|
|
59
|
+
|
|
60
|
+
[tool.hatch.build.targets.wheel]
|
|
61
|
+
packages = ["src/tadclust"]
|
|
62
|
+
exclude = ["**/__pycache__", "**/*.pyc"]
|
|
63
|
+
|
|
64
|
+
[tool.hatch.build.targets.sdist]
|
|
65
|
+
include = [
|
|
66
|
+
"src/tadclust",
|
|
67
|
+
"tests",
|
|
68
|
+
"pyproject.toml",
|
|
69
|
+
"README.md",
|
|
70
|
+
"LICENSE",
|
|
71
|
+
]
|
|
72
|
+
exclude = ["**/__pycache__", "**/*.pyc"]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Hierarchical clustering of TADs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .config import ClusterConfig
|
|
6
|
+
from .metadata import read_manifest
|
|
7
|
+
from .pipeline import ClusterResult, cluster
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0a1"
|
|
10
|
+
|
|
11
|
+
__all__ = ["ClusterConfig", "ClusterResult", "__version__", "cluster", "read_manifest"]
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Shared reader for tab-separated BED-like tables."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable, Sequence
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import TextIO
|
|
9
|
+
|
|
10
|
+
import polars as pl
|
|
11
|
+
|
|
12
|
+
type SchemaFactory = Callable[[bool, int], dict[str, type]]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class BedlikeTable:
|
|
17
|
+
"""A parsed table plus the header and width inferred from its first line."""
|
|
18
|
+
|
|
19
|
+
table: pl.DataFrame
|
|
20
|
+
has_header: bool
|
|
21
|
+
width: int
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def read_bedlike(
|
|
25
|
+
path: str | Path,
|
|
26
|
+
*,
|
|
27
|
+
header_names: Sequence[str] | None = None,
|
|
28
|
+
schema_overrides: dict[str, type] | SchemaFactory,
|
|
29
|
+
skip_track_headers: bool = False,
|
|
30
|
+
) -> BedlikeTable:
|
|
31
|
+
"""Read a tab-separated table, inferring header presence and width.
|
|
32
|
+
|
|
33
|
+
Leading blank, comment, and (optionally) track/browser lines are skipped.
|
|
34
|
+
Callers provide the schema, either directly or as a function of whether a
|
|
35
|
+
header was found and how many columns the first line has.
|
|
36
|
+
"""
|
|
37
|
+
source = Path(path)
|
|
38
|
+
with source.open("r", encoding="utf-8", newline="") as handle:
|
|
39
|
+
sample, skip_lines = _sample_lines(handle, skip_track_headers)
|
|
40
|
+
if not sample:
|
|
41
|
+
raise ValueError(f"empty table: {source}")
|
|
42
|
+
|
|
43
|
+
first_line = sample[0]
|
|
44
|
+
width = len(first_line.split("\t"))
|
|
45
|
+
tokens = {token.strip() for token in first_line.split("\t")}
|
|
46
|
+
has_header = header_names is not None and set(header_names).issubset(tokens)
|
|
47
|
+
|
|
48
|
+
handle.seek(0)
|
|
49
|
+
schema = (
|
|
50
|
+
schema_overrides(has_header, width)
|
|
51
|
+
if callable(schema_overrides)
|
|
52
|
+
else schema_overrides
|
|
53
|
+
)
|
|
54
|
+
table = pl.read_csv(
|
|
55
|
+
handle,
|
|
56
|
+
separator="\t",
|
|
57
|
+
has_header=has_header,
|
|
58
|
+
comment_prefix="#",
|
|
59
|
+
infer_schema_length=0,
|
|
60
|
+
schema_overrides=schema,
|
|
61
|
+
skip_lines=skip_lines,
|
|
62
|
+
truncate_ragged_lines=False,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
return BedlikeTable(
|
|
66
|
+
table=table,
|
|
67
|
+
has_header=has_header,
|
|
68
|
+
width=width,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _sample_lines(
|
|
73
|
+
handle: TextIO,
|
|
74
|
+
skip_track_headers: bool,
|
|
75
|
+
limit: int = 20,
|
|
76
|
+
) -> tuple[list[str], int]:
|
|
77
|
+
sample: list[str] = []
|
|
78
|
+
skipped = 0
|
|
79
|
+
started = False
|
|
80
|
+
|
|
81
|
+
for line in handle:
|
|
82
|
+
stripped = line.strip()
|
|
83
|
+
if not stripped or stripped.startswith("#"):
|
|
84
|
+
if not started:
|
|
85
|
+
skipped += 1
|
|
86
|
+
continue
|
|
87
|
+
if skip_track_headers and _is_track_header(stripped):
|
|
88
|
+
if not started:
|
|
89
|
+
skipped += 1
|
|
90
|
+
continue
|
|
91
|
+
|
|
92
|
+
sample.append(stripped)
|
|
93
|
+
started = True
|
|
94
|
+
if len(sample) >= limit:
|
|
95
|
+
break
|
|
96
|
+
|
|
97
|
+
return sample, skipped
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _is_track_header(line: str) -> bool:
|
|
101
|
+
lower = line.lower()
|
|
102
|
+
return lower in {"track", "browser"} or lower.startswith(("track ", "browser "))
|