maxcombat 0.2.2__tar.gz → 0.2.5__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.
Potentially problematic release.
This version of maxcombat might be problematic. Click here for more details.
- {maxcombat-0.2.2/src/maxcombat.egg-info → maxcombat-0.2.5}/PKG-INFO +63 -49
- {maxcombat-0.2.2 → maxcombat-0.2.5}/README.md +62 -48
- {maxcombat-0.2.2 → maxcombat-0.2.5}/pyproject.toml +7 -2
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat/__init__.py +1 -1
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat/cli.py +114 -63
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat/core.py +245 -106
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat/utils.py +19 -18
- {maxcombat-0.2.2 → maxcombat-0.2.5/src/maxcombat.egg-info}/PKG-INFO +63 -49
- {maxcombat-0.2.2 → maxcombat-0.2.5}/tests/test_cli.py +115 -62
- {maxcombat-0.2.2 → maxcombat-0.2.5}/tests/test_tutorial_data.py +23 -9
- {maxcombat-0.2.2 → maxcombat-0.2.5}/LICENSE +0 -0
- {maxcombat-0.2.2 → maxcombat-0.2.5}/setup.cfg +0 -0
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat.egg-info/SOURCES.txt +0 -0
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat.egg-info/dependency_links.txt +0 -0
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat.egg-info/entry_points.txt +0 -0
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat.egg-info/requires.txt +0 -0
- {maxcombat-0.2.2 → maxcombat-0.2.5}/src/maxcombat.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: maxcombat
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.5
|
|
4
4
|
Summary: Scalable batch-effect correction based on reComBat
|
|
5
5
|
Author: Olivier Coen
|
|
6
6
|
License: MIT
|
|
@@ -36,9 +36,9 @@ Dynamic: license-file
|
|
|
36
36
|
|
|
37
37
|
Scalable batch-effect correction based on [reComBat](https://github.com/BorgwardtLab/reComBat) (Michael F. Adamer, BSD-3-Clause).
|
|
38
38
|
|
|
39
|
-
This package is
|
|
39
|
+
This package is intended as a replacement for `reComBat`, whenever users require a high level of scalability.
|
|
40
40
|
|
|
41
|
-
It was originally developed for [nf-core/stableexpression](https://nf-co.re/stableexpression/).
|
|
41
|
+
It was originally developed for [nf-core/stableexpression](https://nf-co.re/stableexpression/) to correct batch effect among a potentially very large number of expression datasets.
|
|
42
42
|
|
|
43
43
|
## Citation
|
|
44
44
|
|
|
@@ -48,53 +48,18 @@ If you use `maxcombat` for your analysis, please cite [the original reComBat pap
|
|
|
48
48
|
## Installation
|
|
49
49
|
|
|
50
50
|
```bash
|
|
51
|
-
pip install maxcombat
|
|
52
|
-
# or
|
|
53
51
|
conda install -c bioconda maxcombat
|
|
52
|
+
# or
|
|
53
|
+
uv add maxcombat
|
|
54
|
+
# or
|
|
55
|
+
pip install maxcombat
|
|
54
56
|
```
|
|
55
57
|
|
|
56
58
|
## Usage
|
|
57
59
|
|
|
58
|
-
### From Python
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
```python
|
|
62
|
-
from maxcombat import MaxCombat
|
|
63
|
-
|
|
64
|
-
data = ... # (n_genes, n_samples) array
|
|
65
|
-
batches = ... # (n_samples,) array of batch labels
|
|
66
|
-
|
|
67
|
-
model = MaxCombat() # default model: elastic_net
|
|
68
|
-
model.fit(data,batches)
|
|
69
|
-
model.transform(data,batches)
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
or:
|
|
73
|
-
|
|
74
|
-
```python
|
|
75
|
-
corrected = model.fit_transform(data, batches)
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Optional: with covariates and / or a reference batch:
|
|
79
|
-
```python
|
|
80
|
-
preserved_covariates = ... # (n_samples, k) covariates to be considered during batch effect correction: the biological signal that must be preserved
|
|
81
|
-
dropped_covariates = ... # (n_samples, k) covariates to be ignored during batch effect correction (batch-related confounders)
|
|
82
|
-
|
|
83
|
-
model = MaxCombat(model="elastic_net", reference_batch="batch_1")
|
|
84
|
-
corrected = model.fit_transform(data, batches, preserved_covariates, dropped_covariates)
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
Categorical covariates must be one-hot encoded beforehand
|
|
88
|
-
(e.g. sklearn.preprocessing.OneHotEncoder(drop="first")); numerical
|
|
89
|
-
covariates are passed as is.
|
|
90
|
-
|
|
91
|
-
Non-parametric empirical Bayes (leave-one-out, more expensive):
|
|
92
|
-
```python
|
|
93
|
-
model = MaxCombat(parametric=False, model="linear", n_jobs=8)
|
|
94
|
-
```
|
|
95
|
-
|
|
96
60
|
### From the command line
|
|
97
61
|
|
|
62
|
+
#### Basic usage
|
|
98
63
|
|
|
99
64
|
```bash
|
|
100
65
|
maxcombat \
|
|
@@ -128,7 +93,18 @@ sample_E,batch_2
|
|
|
128
93
|
> - the design dataframe may contain other columns, but only the `sample` and `batch` columns will be parsed
|
|
129
94
|
> - batches containing only one sample are removed from the analysis
|
|
130
95
|
|
|
131
|
-
|
|
96
|
+
#### Add preserved and dropped covariates
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
maxcombat \
|
|
100
|
+
--counts counts.csv \
|
|
101
|
+
--design design.csv \
|
|
102
|
+
--out corrected_counts.csv \
|
|
103
|
+
--preserved-covariates preserved_covariate.csv \
|
|
104
|
+
--dropped-covariates dropped_covariate.csv
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Example of preserved (desired) covariates file:
|
|
132
108
|
|
|
133
109
|
```csv title=preserved_covariate.csv
|
|
134
110
|
sample,condition,age
|
|
@@ -139,7 +115,7 @@ sample_D,control,29
|
|
|
139
115
|
sample_E,control,63
|
|
140
116
|
```
|
|
141
117
|
|
|
142
|
-
Example of
|
|
118
|
+
Example of unwanted (dropped) covariates file:
|
|
143
119
|
|
|
144
120
|
```csv title=dropped_covariate.csv
|
|
145
121
|
sample,rin
|
|
@@ -187,7 +163,46 @@ sample_E,6.9
|
|
|
187
163
|
|
|
188
164
|
`--clip-negative-values`: clip negative values to 0 after correction (useful for specific downstream analyses requiring only positive values)
|
|
189
165
|
|
|
190
|
-
`--out-
|
|
166
|
+
`--out-float32`: convert corrected data to float32 before export (useful for very large datasets)
|
|
167
|
+
|
|
168
|
+
`--out-ndigits`: round corrected data to `n` decimal digits before export (useful for very large datasets)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
### Import as a Python package
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from maxcombat import MaxCombat
|
|
175
|
+
|
|
176
|
+
data = ... # (n_genes, n_samples) array
|
|
177
|
+
batches = ... # (n_samples,) array of batch labels
|
|
178
|
+
|
|
179
|
+
model = MaxCombat() # default model: elastic_net
|
|
180
|
+
model.fit(data, batches)
|
|
181
|
+
model.transform(data, batches)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
or:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
corrected = model.fit_transform(data, batches)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Optional: with covariates and / or a reference batch:
|
|
191
|
+
```python
|
|
192
|
+
preserved_covariates = ... # (n_samples, k) covariates to be considered during batch effect correction: the biological signal that must be preserved
|
|
193
|
+
dropped_covariates = ... # (n_samples, k) covariates to be ignored during batch effect correction (batch-related confounders)
|
|
194
|
+
|
|
195
|
+
model = MaxCombat(model="elastic_net", reference_batch="batch_1")
|
|
196
|
+
corrected = model.fit_transform(data, batches, preserved_covariates, dropped_covariates)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
> [!NOTE]
|
|
200
|
+
> Categorical covariates must be one-hot encoded beforehand (e.g. sklearn.preprocessing.OneHotEncoder(drop="first")); numerical covariates are passed as is.
|
|
201
|
+
|
|
202
|
+
Non-parametric empirical Bayes (leave-one-out, more expensive):
|
|
203
|
+
```python
|
|
204
|
+
model = MaxCombat(parametric=False, model="linear", n_jobs=8)
|
|
205
|
+
```
|
|
191
206
|
|
|
192
207
|
## Feature improvement over `reComBat`
|
|
193
208
|
|
|
@@ -195,9 +210,8 @@ sample_E,6.9
|
|
|
195
210
|
- Genes as rows, samples as columns (the usual omics convention, unlike reComBat)
|
|
196
211
|
- Automatic removal of orphan samples (samples alone in their batch)
|
|
197
212
|
- Optional clipping of negative output values (safety net for count data)
|
|
198
|
-
-
|
|
199
|
-
-
|
|
200
|
-
- Possibility to export data in `float32`, to reduce to memory footprint when dealing with a very large dataset
|
|
213
|
+
- [CLI]: Automatic detection of input and output file extension (`parquet` / `csv` / `tsv`)
|
|
214
|
+
- [CLI]: Possibility to export data in `float32` and / or to reduce the number of decimal digits, to reduce to memory footprint when dealing with a very large dataset
|
|
201
215
|
|
|
202
216
|
## License
|
|
203
217
|
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
|
|
6
6
|
Scalable batch-effect correction based on [reComBat](https://github.com/BorgwardtLab/reComBat) (Michael F. Adamer, BSD-3-Clause).
|
|
7
7
|
|
|
8
|
-
This package is
|
|
8
|
+
This package is intended as a replacement for `reComBat`, whenever users require a high level of scalability.
|
|
9
9
|
|
|
10
|
-
It was originally developed for [nf-core/stableexpression](https://nf-co.re/stableexpression/).
|
|
10
|
+
It was originally developed for [nf-core/stableexpression](https://nf-co.re/stableexpression/) to correct batch effect among a potentially very large number of expression datasets.
|
|
11
11
|
|
|
12
12
|
## Citation
|
|
13
13
|
|
|
@@ -17,53 +17,18 @@ If you use `maxcombat` for your analysis, please cite [the original reComBat pap
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
pip install maxcombat
|
|
21
|
-
# or
|
|
22
20
|
conda install -c bioconda maxcombat
|
|
21
|
+
# or
|
|
22
|
+
uv add maxcombat
|
|
23
|
+
# or
|
|
24
|
+
pip install maxcombat
|
|
23
25
|
```
|
|
24
26
|
|
|
25
27
|
## Usage
|
|
26
28
|
|
|
27
|
-
### From Python
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
```python
|
|
31
|
-
from maxcombat import MaxCombat
|
|
32
|
-
|
|
33
|
-
data = ... # (n_genes, n_samples) array
|
|
34
|
-
batches = ... # (n_samples,) array of batch labels
|
|
35
|
-
|
|
36
|
-
model = MaxCombat() # default model: elastic_net
|
|
37
|
-
model.fit(data,batches)
|
|
38
|
-
model.transform(data,batches)
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
or:
|
|
42
|
-
|
|
43
|
-
```python
|
|
44
|
-
corrected = model.fit_transform(data, batches)
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Optional: with covariates and / or a reference batch:
|
|
48
|
-
```python
|
|
49
|
-
preserved_covariates = ... # (n_samples, k) covariates to be considered during batch effect correction: the biological signal that must be preserved
|
|
50
|
-
dropped_covariates = ... # (n_samples, k) covariates to be ignored during batch effect correction (batch-related confounders)
|
|
51
|
-
|
|
52
|
-
model = MaxCombat(model="elastic_net", reference_batch="batch_1")
|
|
53
|
-
corrected = model.fit_transform(data, batches, preserved_covariates, dropped_covariates)
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Categorical covariates must be one-hot encoded beforehand
|
|
57
|
-
(e.g. sklearn.preprocessing.OneHotEncoder(drop="first")); numerical
|
|
58
|
-
covariates are passed as is.
|
|
59
|
-
|
|
60
|
-
Non-parametric empirical Bayes (leave-one-out, more expensive):
|
|
61
|
-
```python
|
|
62
|
-
model = MaxCombat(parametric=False, model="linear", n_jobs=8)
|
|
63
|
-
```
|
|
64
|
-
|
|
65
29
|
### From the command line
|
|
66
30
|
|
|
31
|
+
#### Basic usage
|
|
67
32
|
|
|
68
33
|
```bash
|
|
69
34
|
maxcombat \
|
|
@@ -97,7 +62,18 @@ sample_E,batch_2
|
|
|
97
62
|
> - the design dataframe may contain other columns, but only the `sample` and `batch` columns will be parsed
|
|
98
63
|
> - batches containing only one sample are removed from the analysis
|
|
99
64
|
|
|
100
|
-
|
|
65
|
+
#### Add preserved and dropped covariates
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
maxcombat \
|
|
69
|
+
--counts counts.csv \
|
|
70
|
+
--design design.csv \
|
|
71
|
+
--out corrected_counts.csv \
|
|
72
|
+
--preserved-covariates preserved_covariate.csv \
|
|
73
|
+
--dropped-covariates dropped_covariate.csv
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Example of preserved (desired) covariates file:
|
|
101
77
|
|
|
102
78
|
```csv title=preserved_covariate.csv
|
|
103
79
|
sample,condition,age
|
|
@@ -108,7 +84,7 @@ sample_D,control,29
|
|
|
108
84
|
sample_E,control,63
|
|
109
85
|
```
|
|
110
86
|
|
|
111
|
-
Example of
|
|
87
|
+
Example of unwanted (dropped) covariates file:
|
|
112
88
|
|
|
113
89
|
```csv title=dropped_covariate.csv
|
|
114
90
|
sample,rin
|
|
@@ -156,7 +132,46 @@ sample_E,6.9
|
|
|
156
132
|
|
|
157
133
|
`--clip-negative-values`: clip negative values to 0 after correction (useful for specific downstream analyses requiring only positive values)
|
|
158
134
|
|
|
159
|
-
`--out-
|
|
135
|
+
`--out-float32`: convert corrected data to float32 before export (useful for very large datasets)
|
|
136
|
+
|
|
137
|
+
`--out-ndigits`: round corrected data to `n` decimal digits before export (useful for very large datasets)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
### Import as a Python package
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from maxcombat import MaxCombat
|
|
144
|
+
|
|
145
|
+
data = ... # (n_genes, n_samples) array
|
|
146
|
+
batches = ... # (n_samples,) array of batch labels
|
|
147
|
+
|
|
148
|
+
model = MaxCombat() # default model: elastic_net
|
|
149
|
+
model.fit(data, batches)
|
|
150
|
+
model.transform(data, batches)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
or:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
corrected = model.fit_transform(data, batches)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Optional: with covariates and / or a reference batch:
|
|
160
|
+
```python
|
|
161
|
+
preserved_covariates = ... # (n_samples, k) covariates to be considered during batch effect correction: the biological signal that must be preserved
|
|
162
|
+
dropped_covariates = ... # (n_samples, k) covariates to be ignored during batch effect correction (batch-related confounders)
|
|
163
|
+
|
|
164
|
+
model = MaxCombat(model="elastic_net", reference_batch="batch_1")
|
|
165
|
+
corrected = model.fit_transform(data, batches, preserved_covariates, dropped_covariates)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
> [!NOTE]
|
|
169
|
+
> Categorical covariates must be one-hot encoded beforehand (e.g. sklearn.preprocessing.OneHotEncoder(drop="first")); numerical covariates are passed as is.
|
|
170
|
+
|
|
171
|
+
Non-parametric empirical Bayes (leave-one-out, more expensive):
|
|
172
|
+
```python
|
|
173
|
+
model = MaxCombat(parametric=False, model="linear", n_jobs=8)
|
|
174
|
+
```
|
|
160
175
|
|
|
161
176
|
## Feature improvement over `reComBat`
|
|
162
177
|
|
|
@@ -164,9 +179,8 @@ sample_E,6.9
|
|
|
164
179
|
- Genes as rows, samples as columns (the usual omics convention, unlike reComBat)
|
|
165
180
|
- Automatic removal of orphan samples (samples alone in their batch)
|
|
166
181
|
- Optional clipping of negative output values (safety net for count data)
|
|
167
|
-
-
|
|
168
|
-
-
|
|
169
|
-
- Possibility to export data in `float32`, to reduce to memory footprint when dealing with a very large dataset
|
|
182
|
+
- [CLI]: Automatic detection of input and output file extension (`parquet` / `csv` / `tsv`)
|
|
183
|
+
- [CLI]: Possibility to export data in `float32` and / or to reduce the number of decimal digits, to reduce to memory footprint when dealing with a very large dataset
|
|
170
184
|
|
|
171
185
|
## License
|
|
172
186
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "maxcombat"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.5"
|
|
8
8
|
description = "Scalable batch-effect correction based on reComBat"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { text = "MIT" }
|
|
@@ -27,7 +27,7 @@ dependencies = [
|
|
|
27
27
|
"scikit-learn>=1.2",
|
|
28
28
|
"joblib>=1.2",
|
|
29
29
|
"tqdm>=4.64",
|
|
30
|
-
"polars>=0.21"
|
|
30
|
+
"polars>=0.21",
|
|
31
31
|
]
|
|
32
32
|
|
|
33
33
|
[project.optional-dependencies]
|
|
@@ -52,3 +52,8 @@ markers = [
|
|
|
52
52
|
"cli: cli tests, used in bioconda CI",
|
|
53
53
|
"tutorial: tests aiming at reproducing results from the original reComBat, using tutorial data"
|
|
54
54
|
]
|
|
55
|
+
|
|
56
|
+
[dependency-groups]
|
|
57
|
+
dev = [
|
|
58
|
+
"pytest>=9.2.0",
|
|
59
|
+
]
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
1
|
# Written by Olivier Coen. Released under the MIT license.
|
|
3
2
|
|
|
4
3
|
"""cli.py: thin command-line interface around maxcombat."""
|
|
@@ -7,8 +6,8 @@ from __future__ import annotations
|
|
|
7
6
|
|
|
8
7
|
import argparse
|
|
9
8
|
import logging
|
|
10
|
-
import sys
|
|
11
9
|
import os
|
|
10
|
+
import sys
|
|
12
11
|
from multiprocessing import cpu_count
|
|
13
12
|
from pathlib import Path
|
|
14
13
|
|
|
@@ -22,75 +21,114 @@ DEFAULT_NJOBS = 8
|
|
|
22
21
|
# PARSE ARGUMENTS
|
|
23
22
|
# --------------------------------------------------------------
|
|
24
23
|
|
|
24
|
+
|
|
25
25
|
def parse_args() -> argparse.Namespace:
|
|
26
|
-
parser = argparse.ArgumentParser(
|
|
27
|
-
|
|
28
|
-
"--counts", type=Path, dest="count_file", required=True,
|
|
29
|
-
help="Parquet gathering all count data (genes as rows, samples as columns). The column containing gene IDs should be the first column, or should be specified with --gene-id-colname"
|
|
26
|
+
parser = argparse.ArgumentParser(
|
|
27
|
+
description="maxcombat: scalable batch-effect correction based on reComBat"
|
|
30
28
|
)
|
|
31
29
|
parser.add_argument(
|
|
32
|
-
"--
|
|
33
|
-
|
|
30
|
+
"--counts",
|
|
31
|
+
type=Path,
|
|
32
|
+
dest="count_file",
|
|
33
|
+
required=True,
|
|
34
|
+
help="Parquet gathering all count data (genes as rows, samples as columns). The column containing gene IDs should be the first column, or should be specified with --gene-id-colname",
|
|
34
35
|
)
|
|
35
36
|
parser.add_argument(
|
|
36
|
-
"--
|
|
37
|
-
|
|
37
|
+
"--design",
|
|
38
|
+
type=Path,
|
|
39
|
+
dest="design_file",
|
|
40
|
+
required=True,
|
|
41
|
+
help="CSV design file with 'sample' and 'batch' columns",
|
|
38
42
|
)
|
|
39
43
|
parser.add_argument(
|
|
40
|
-
"--
|
|
44
|
+
"--gene-id-colname",
|
|
45
|
+
type=str,
|
|
46
|
+
dest="gene_id_col",
|
|
47
|
+
help="Name of the gene ID column in the counts file (default: first column)",
|
|
41
48
|
)
|
|
49
|
+
parser.add_argument("--model", help="Regression model used to standardize the data")
|
|
42
50
|
parser.add_argument(
|
|
43
|
-
"--parametric",
|
|
44
|
-
|
|
51
|
+
"--parametric",
|
|
52
|
+
action=argparse.BooleanOptionalAction,
|
|
53
|
+
default=True,
|
|
54
|
+
help="Parametric (default) or --no-parametric empirical Bayes optimization",
|
|
45
55
|
)
|
|
46
56
|
parser.add_argument(
|
|
47
|
-
"--config",
|
|
48
|
-
|
|
57
|
+
"--config",
|
|
58
|
+
type=Path,
|
|
59
|
+
dest="config_file",
|
|
60
|
+
help="JSON file with keyword arguments for the scikit-learn regressor",
|
|
49
61
|
)
|
|
50
62
|
parser.add_argument(
|
|
51
|
-
"--conv-criterion",
|
|
52
|
-
|
|
63
|
+
"--conv-criterion",
|
|
64
|
+
dest="conv_criterion",
|
|
65
|
+
type=float,
|
|
66
|
+
help="Relative convergence criterion (default 1e-4)",
|
|
53
67
|
)
|
|
54
68
|
parser.add_argument(
|
|
55
|
-
"--max-iter",
|
|
56
|
-
|
|
69
|
+
"--max-iter",
|
|
70
|
+
dest="max_iter",
|
|
71
|
+
type=int,
|
|
72
|
+
help="Maximum number of iterations (default 1000)",
|
|
57
73
|
)
|
|
58
74
|
parser.add_argument(
|
|
59
|
-
"--mean-only",
|
|
60
|
-
|
|
75
|
+
"--mean-only",
|
|
76
|
+
dest="mean_only",
|
|
77
|
+
action="store_true",
|
|
78
|
+
help="Adjust the mean only, no scaling",
|
|
61
79
|
)
|
|
62
80
|
parser.add_argument(
|
|
63
|
-
"--clip-negative-values",
|
|
64
|
-
|
|
81
|
+
"--clip-negative-values",
|
|
82
|
+
dest="clip_negative_values",
|
|
83
|
+
action="store_true",
|
|
84
|
+
help="Clip negative output values to 0",
|
|
65
85
|
)
|
|
66
86
|
parser.add_argument(
|
|
67
|
-
"--reference-batch",
|
|
87
|
+
"--reference-batch",
|
|
88
|
+
dest="reference_batch",
|
|
89
|
+
default=None,
|
|
68
90
|
help="Batch label left unadjusted. Defaults to None (all batches adjusted)",
|
|
69
91
|
)
|
|
70
92
|
parser.add_argument(
|
|
71
|
-
"--optimize-params",
|
|
72
|
-
|
|
93
|
+
"--optimize-params",
|
|
94
|
+
dest="optimize_params",
|
|
95
|
+
action=argparse.BooleanOptionalAction,
|
|
96
|
+
default=True,
|
|
73
97
|
help="Perform empirical Bayes optimization (default). Use --no-optimize-params to skip it",
|
|
74
98
|
)
|
|
75
99
|
parser.add_argument(
|
|
76
|
-
"--preserved-covariates",
|
|
100
|
+
"--preserved-covariates",
|
|
101
|
+
dest="preserved_covariates_file",
|
|
102
|
+
type=Path,
|
|
103
|
+
default=None,
|
|
77
104
|
help="CSV with a 'sample' column plus covariates of desired variation (ie. conditions that should be considered when computing the batch effect correction).",
|
|
78
105
|
)
|
|
79
106
|
parser.add_argument(
|
|
80
|
-
"--dropped-covariates",
|
|
107
|
+
"--dropped-covariates",
|
|
108
|
+
dest="dropped_covariates_file",
|
|
109
|
+
type=Path,
|
|
110
|
+
default=None,
|
|
81
111
|
help="CSV with a 'sample' column plus covariates of unwanted variation (e.g. RIN, sex) other than batch (e.g. batch-related confounders).",
|
|
82
112
|
)
|
|
83
113
|
parser.add_argument(
|
|
84
|
-
"--out
|
|
114
|
+
"--out", type=Path, dest="outfile", required=True, help="Output Parquet file"
|
|
115
|
+
)
|
|
116
|
+
parser.add_argument(
|
|
117
|
+
"--out-float32",
|
|
118
|
+
action="store_true",
|
|
119
|
+
dest="cast_output_to_float32",
|
|
85
120
|
help="Cast corrected data to float32 before export to save memory",
|
|
86
121
|
)
|
|
87
122
|
parser.add_argument(
|
|
88
|
-
"--out",
|
|
89
|
-
|
|
123
|
+
"--out-ndigits",
|
|
124
|
+
dest="nb_digits_output",
|
|
125
|
+
type=int,
|
|
126
|
+
help="Number of digits to keep in the output. Defaults to all digits",
|
|
90
127
|
)
|
|
91
128
|
parser.add_argument(
|
|
92
|
-
"--cpus",
|
|
93
|
-
|
|
129
|
+
"--cpus",
|
|
130
|
+
type=int,
|
|
131
|
+
help="Number of threads for numpy/polars. Defaults to min(8, <number of available cores>",
|
|
94
132
|
)
|
|
95
133
|
return parser.parse_args()
|
|
96
134
|
|
|
@@ -103,31 +141,31 @@ args = parse_args()
|
|
|
103
141
|
|
|
104
142
|
# Limit thread usage; must be set before importing numpy-heavy libs.
|
|
105
143
|
n_cpus = args.cpus or min(DEFAULT_NJOBS, cpu_count())
|
|
106
|
-
os.environ.update(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
144
|
+
os.environ.update(
|
|
145
|
+
{
|
|
146
|
+
"OMP_NUM_THREADS": str(n_cpus),
|
|
147
|
+
"OPENBLAS_NUM_THREADS": str(n_cpus),
|
|
148
|
+
"MKL_NUM_THREADS": str(n_cpus),
|
|
149
|
+
"NUMEXPR_NUM_THREADS": str(n_cpus),
|
|
150
|
+
"POLARS_MAX_THREADS": str(n_cpus),
|
|
151
|
+
}
|
|
152
|
+
)
|
|
113
153
|
|
|
114
|
-
import polars as pl # noqa:
|
|
154
|
+
import polars as pl # noqa: I001
|
|
115
155
|
|
|
116
|
-
from maxcombat.core import MaxCombat, SingleBatchException # noqa: E402
|
|
117
|
-
from maxcombat.utils import parse_count_table, build_covariates, parse_config, export
|
|
118
156
|
|
|
157
|
+
from maxcombat.core import MaxCombat, SingleBatchException
|
|
158
|
+
from maxcombat.utils import build_covariates, export, parse_config, parse_count_table
|
|
119
159
|
|
|
120
160
|
# --------------------------------------------------------------
|
|
121
161
|
# MAIN
|
|
122
162
|
# --------------------------------------------------------------
|
|
123
163
|
|
|
164
|
+
|
|
124
165
|
def main() -> None:
|
|
125
|
-
|
|
126
166
|
|
|
127
167
|
logging.basicConfig(
|
|
128
|
-
level=logging.INFO,
|
|
129
|
-
stream=sys.stdout,
|
|
130
|
-
format="[%(levelname)s] %(message)s"
|
|
168
|
+
level=logging.INFO, stream=sys.stdout, format="[%(levelname)s] %(message)s"
|
|
131
169
|
)
|
|
132
170
|
logger = logging.getLogger("maxcombat-cli")
|
|
133
171
|
|
|
@@ -145,21 +183,29 @@ def main() -> None:
|
|
|
145
183
|
counts = counts.drop(gene_id_col)
|
|
146
184
|
|
|
147
185
|
# taking only the subpart of the design that corresponds to the count data
|
|
148
|
-
design = design.filter(pl.col(
|
|
149
|
-
|
|
150
|
-
data = counts.to_numpy()
|
|
186
|
+
design = design.filter(pl.col("sample").is_in(samples))
|
|
187
|
+
|
|
188
|
+
data = counts.to_numpy() # (n_genes, n_samples)
|
|
151
189
|
batches = design["batch"].to_numpy()
|
|
152
190
|
|
|
153
191
|
# parsing covariates if provided
|
|
154
|
-
preserved_covariates =
|
|
155
|
-
|
|
156
|
-
|
|
192
|
+
preserved_covariates = (
|
|
193
|
+
build_covariates(args.preserved_covariates_file, samples)
|
|
194
|
+
if args.preserved_covariates_file
|
|
195
|
+
else None
|
|
196
|
+
)
|
|
197
|
+
dropped_covariates = (
|
|
198
|
+
build_covariates(args.dropped_covariates_file, samples)
|
|
199
|
+
if args.dropped_covariates_file
|
|
200
|
+
else None
|
|
201
|
+
)
|
|
202
|
+
|
|
157
203
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
158
204
|
# BATCH CORRECTION
|
|
159
205
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
160
206
|
|
|
161
207
|
model = MaxCombat(
|
|
162
|
-
parametric=args.parametric,
|
|
208
|
+
parametric=args.parametric, # False if --no-parametric is provided
|
|
163
209
|
model=args.model,
|
|
164
210
|
config=parse_config(args.config_file),
|
|
165
211
|
conv_criterion=args.conv_criterion,
|
|
@@ -168,16 +214,16 @@ def main() -> None:
|
|
|
168
214
|
optimize_params=args.optimize_params,
|
|
169
215
|
reference_batch=args.reference_batch,
|
|
170
216
|
clip_negative_values=args.clip_negative_values,
|
|
171
|
-
n_jobs=n_cpus
|
|
217
|
+
n_jobs=n_cpus,
|
|
172
218
|
)
|
|
173
219
|
|
|
174
220
|
try:
|
|
175
221
|
corrected = model.fit_transform(
|
|
176
|
-
data,
|
|
177
|
-
batches,
|
|
222
|
+
data,
|
|
223
|
+
batches,
|
|
178
224
|
preserved_covariates,
|
|
179
225
|
dropped_covariates,
|
|
180
|
-
sample_names=samples
|
|
226
|
+
sample_names=samples,
|
|
181
227
|
) # (n_genes, n_samples)
|
|
182
228
|
# update new samples, removing orphans that were dropped
|
|
183
229
|
samples = [s for s, k in zip(samples, model.keep_mask_) if k]
|
|
@@ -186,25 +232,30 @@ def main() -> None:
|
|
|
186
232
|
corrected = data
|
|
187
233
|
|
|
188
234
|
assert corrected.shape[1] == len(samples)
|
|
189
|
-
|
|
235
|
+
|
|
190
236
|
# post-processing (clipping negative values if needed)
|
|
191
237
|
corrected = model.post_process(corrected)
|
|
192
238
|
|
|
193
239
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
194
240
|
# POSTPROCESSING AND EXPORT
|
|
195
241
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
196
|
-
|
|
242
|
+
|
|
197
243
|
out_df = pl.DataFrame(corrected, schema=samples)
|
|
244
|
+
|
|
245
|
+
if args.nb_digits_output is not None:
|
|
246
|
+
logger.info(f"Rounding output to {args.nb_digits_output} digits")
|
|
247
|
+
out_df = out_df.select(pl.all().round(args.nb_digits_output))
|
|
248
|
+
|
|
198
249
|
if args.cast_output_to_float32:
|
|
250
|
+
logger.info("Casting output to float32")
|
|
199
251
|
out_df = out_df.select(pl.all().cast(pl.Float32))
|
|
200
|
-
|
|
252
|
+
|
|
201
253
|
out_df = pl.concat([gene_ids, out_df], how="horizontal")
|
|
202
254
|
|
|
203
255
|
logger.info(f"Exporting corrected counts to {args.outfile}")
|
|
204
256
|
export(out_df, args.outfile)
|
|
205
|
-
|
|
206
|
-
logger.info("Done")
|
|
207
257
|
|
|
258
|
+
logger.info("Done")
|
|
208
259
|
|
|
209
260
|
|
|
210
261
|
if __name__ == "__main__":
|