maxcombat 0.2.0__tar.gz → 0.2.2__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.

@@ -0,0 +1,204 @@
1
+ Metadata-Version: 2.4
2
+ Name: maxcombat
3
+ Version: 0.2.2
4
+ Summary: Scalable batch-effect correction based on reComBat
5
+ Author: Olivier Coen
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/OlivierCoen/maxcombat
8
+ Project-URL: Repository, https://github.com/OlivierCoen/maxcombat
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Classifier: Programming Language :: Python :: 3.15
18
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: numpy>=1.23
23
+ Requires-Dist: scikit-learn>=1.2
24
+ Requires-Dist: joblib>=1.2
25
+ Requires-Dist: tqdm>=4.64
26
+ Requires-Dist: polars>=0.21
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=7; extra == "dev"
29
+ Requires-Dist: ruff>=0.4; extra == "dev"
30
+ Dynamic: license-file
31
+
32
+ # maxcombat
33
+
34
+ [![PyPI Latest Release](https://img.shields.io/pypi/v/maxcombat.svg)](https://pypi.org/project/maxcombat/) [![Conda Latest Release](https://anaconda.org/bioconda/maxcombat/badges/version.svg)](https://anaconda.org/bioconda/maxcombat/) [![PythonVersion](https://img.shields.io/pypi/pyversions/maxcombat)](https://pypi.org/project/maxcombat/) [![CI - Test](https://github.com/OlivierCoen/maxcombat/actions/workflows/ci.yml/badge.svg)](https://github.com/OlivierCoen/maxcombat/actions/workflows/ci.yml)
35
+
36
+
37
+ Scalable batch-effect correction based on [reComBat](https://github.com/BorgwardtLab/reComBat) (Michael F. Adamer, BSD-3-Clause).
38
+
39
+ This package is meant as a drop-in replacement for `reComBat`, whenever users need a high level of scalability.
40
+
41
+ It was originally developed for [nf-core/stableexpression](https://nf-co.re/stableexpression/).
42
+
43
+ ## Citation
44
+
45
+ If you use `maxcombat` for your analysis, please cite [the original reComBat paper](https://academic.oup.com/bioinformaticsadvances/article/2/1/vbac071/6728137?login=false).
46
+
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ pip install maxcombat
52
+ # or
53
+ conda install -c bioconda maxcombat
54
+ ```
55
+
56
+ ## Usage
57
+
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
+ ### From the command line
97
+
98
+
99
+ ```bash
100
+ maxcombat \
101
+ --counts counts.parquet \
102
+ --design design.csv \
103
+ --out corrected_counts.parquet
104
+ ```
105
+
106
+ The count dataframe should have the following structure:
107
+
108
+ ```csv
109
+ gene_id,sample_A,sample_B,sample_C
110
+ gene_1,1,2,3
111
+ gene_2,1,2,3
112
+ ```
113
+
114
+ The design should look like:
115
+
116
+ ```csv title=design.csv
117
+ sample,batch
118
+ sample_A,batch_1
119
+ sample_B,batch_1
120
+ sample_C,batch_1
121
+ sample_D,batch_2
122
+ sample_E,batch_2
123
+ ```
124
+
125
+ > [!NOTE]
126
+ > Prior to running `maxcombat`:
127
+ > - the design dataframe is filtered to keep only samples that are present in the count dataframe
128
+ > - the design dataframe may contain other columns, but only the `sample` and `batch` columns will be parsed
129
+ > - batches containing only one sample are removed from the analysis
130
+
131
+ Example of preserved covariates file:
132
+
133
+ ```csv title=preserved_covariate.csv
134
+ sample,condition,age
135
+ sample_A,treated,34
136
+ sample_B,treated,52
137
+ sample_C,control,41
138
+ sample_D,control,29
139
+ sample_E,control,63
140
+ ```
141
+
142
+ Example of unwated (dropped) covariates file:
143
+
144
+ ```csv title=dropped_covariate.csv
145
+ sample,rin
146
+ sample_A,8.1
147
+ sample_B,7.4
148
+ sample_C,9.0
149
+ sample_D,8.8
150
+ sample_E,6.9
151
+ ```
152
+
153
+ > [!NOTE]
154
+ > - only sample and the covariate columns may be present (every extra column silently becomes a covariate)
155
+ > - sample names must match those in the counts / design files exactly.
156
+
157
+
158
+ #### Mandatory arguments:
159
+
160
+ `--counts`: file in `parquet`, or `csv` / `tsv` (possibly gzipped) formats. The format is automatically detected. Genes must be provided as rows, samples as columns.
161
+
162
+ `--design`: CSV with sample and batch columns.
163
+
164
+ `--out`: path to output file; the format is parsed from the output file extension. Supported extensions: `.parquet`, `.csv`, `.tsv`.
165
+
166
+ #### Optional arguments:
167
+
168
+ `--model`: sklearn model to use. Choose between: `linear`, `ridge`, `lasso`, `elastic_net`. Default: `elastic_net`.
169
+
170
+ `--no-parametric`: switch to the non-parametric empirical Bayes optimization (default: false, ie. use the parametric mode).
171
+
172
+ `--gene-id-col`: name of the column containing gene IDs (default: first column).
173
+
174
+ `--conv-criterion`: Relative convergence criterion (default 1e-4).
175
+
176
+ `--max-iter`: Maximum number of iterations (default 1000).
177
+
178
+ `--mean-only`: Adjust the mean only, no scaling.
179
+
180
+ `--preserved-covariates` / `--dropped-covariates`: CSVs with a sample column plus covariate columns; categorical columns are one-hot encoded automatically (first level dropped).
181
+
182
+ `--reference-batch`: batch label left unadjusted.
183
+
184
+ `--no-optimize-params`: skip the empirical Bayes optimization.
185
+
186
+ `--cpus`: parallelism for the numerical backends / joblib (controls the `n_jobs` argument in `MaxCombat`)
187
+
188
+ `--clip-negative-values`: clip negative values to 0 after correction (useful for specific downstream analyses requiring only positive values)
189
+
190
+ `--out-f32`: convert corrected data to float32 before export
191
+
192
+ ## Feature improvement over `reComBat`
193
+
194
+ - NumPy-only: no pandas dependency, arrays in, arrays out in the `MaxCombat` class. Polars is used to parse and export dataframes when using the CLI.
195
+ - Genes as rows, samples as columns (the usual omics convention, unlike reComBat)
196
+ - Automatic removal of orphan samples (samples alone in their batch)
197
+ - Optional clipping of negative output values (safety net for count data)
198
+ - Sensible default parallelism: min(8, number of CPUs) jobs
199
+ - Automatic detection of input and output file extension (`parquet` / `csv` / `tsv`)
200
+ - Possibility to export data in `float32`, to reduce to memory footprint when dealing with a very large dataset
201
+
202
+ ## License
203
+
204
+ MIT (see [LICENSE](LICENSE)). This package incorporates code from reComBat (BSD-3-Clause), see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
@@ -0,0 +1,173 @@
1
+ # maxcombat
2
+
3
+ [![PyPI Latest Release](https://img.shields.io/pypi/v/maxcombat.svg)](https://pypi.org/project/maxcombat/) [![Conda Latest Release](https://anaconda.org/bioconda/maxcombat/badges/version.svg)](https://anaconda.org/bioconda/maxcombat/) [![PythonVersion](https://img.shields.io/pypi/pyversions/maxcombat)](https://pypi.org/project/maxcombat/) [![CI - Test](https://github.com/OlivierCoen/maxcombat/actions/workflows/ci.yml/badge.svg)](https://github.com/OlivierCoen/maxcombat/actions/workflows/ci.yml)
4
+
5
+
6
+ Scalable batch-effect correction based on [reComBat](https://github.com/BorgwardtLab/reComBat) (Michael F. Adamer, BSD-3-Clause).
7
+
8
+ This package is meant as a drop-in replacement for `reComBat`, whenever users need a high level of scalability.
9
+
10
+ It was originally developed for [nf-core/stableexpression](https://nf-co.re/stableexpression/).
11
+
12
+ ## Citation
13
+
14
+ If you use `maxcombat` for your analysis, please cite [the original reComBat paper](https://academic.oup.com/bioinformaticsadvances/article/2/1/vbac071/6728137?login=false).
15
+
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ pip install maxcombat
21
+ # or
22
+ conda install -c bioconda maxcombat
23
+ ```
24
+
25
+ ## Usage
26
+
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
+ ### From the command line
66
+
67
+
68
+ ```bash
69
+ maxcombat \
70
+ --counts counts.parquet \
71
+ --design design.csv \
72
+ --out corrected_counts.parquet
73
+ ```
74
+
75
+ The count dataframe should have the following structure:
76
+
77
+ ```csv
78
+ gene_id,sample_A,sample_B,sample_C
79
+ gene_1,1,2,3
80
+ gene_2,1,2,3
81
+ ```
82
+
83
+ The design should look like:
84
+
85
+ ```csv title=design.csv
86
+ sample,batch
87
+ sample_A,batch_1
88
+ sample_B,batch_1
89
+ sample_C,batch_1
90
+ sample_D,batch_2
91
+ sample_E,batch_2
92
+ ```
93
+
94
+ > [!NOTE]
95
+ > Prior to running `maxcombat`:
96
+ > - the design dataframe is filtered to keep only samples that are present in the count dataframe
97
+ > - the design dataframe may contain other columns, but only the `sample` and `batch` columns will be parsed
98
+ > - batches containing only one sample are removed from the analysis
99
+
100
+ Example of preserved covariates file:
101
+
102
+ ```csv title=preserved_covariate.csv
103
+ sample,condition,age
104
+ sample_A,treated,34
105
+ sample_B,treated,52
106
+ sample_C,control,41
107
+ sample_D,control,29
108
+ sample_E,control,63
109
+ ```
110
+
111
+ Example of unwated (dropped) covariates file:
112
+
113
+ ```csv title=dropped_covariate.csv
114
+ sample,rin
115
+ sample_A,8.1
116
+ sample_B,7.4
117
+ sample_C,9.0
118
+ sample_D,8.8
119
+ sample_E,6.9
120
+ ```
121
+
122
+ > [!NOTE]
123
+ > - only sample and the covariate columns may be present (every extra column silently becomes a covariate)
124
+ > - sample names must match those in the counts / design files exactly.
125
+
126
+
127
+ #### Mandatory arguments:
128
+
129
+ `--counts`: file in `parquet`, or `csv` / `tsv` (possibly gzipped) formats. The format is automatically detected. Genes must be provided as rows, samples as columns.
130
+
131
+ `--design`: CSV with sample and batch columns.
132
+
133
+ `--out`: path to output file; the format is parsed from the output file extension. Supported extensions: `.parquet`, `.csv`, `.tsv`.
134
+
135
+ #### Optional arguments:
136
+
137
+ `--model`: sklearn model to use. Choose between: `linear`, `ridge`, `lasso`, `elastic_net`. Default: `elastic_net`.
138
+
139
+ `--no-parametric`: switch to the non-parametric empirical Bayes optimization (default: false, ie. use the parametric mode).
140
+
141
+ `--gene-id-col`: name of the column containing gene IDs (default: first column).
142
+
143
+ `--conv-criterion`: Relative convergence criterion (default 1e-4).
144
+
145
+ `--max-iter`: Maximum number of iterations (default 1000).
146
+
147
+ `--mean-only`: Adjust the mean only, no scaling.
148
+
149
+ `--preserved-covariates` / `--dropped-covariates`: CSVs with a sample column plus covariate columns; categorical columns are one-hot encoded automatically (first level dropped).
150
+
151
+ `--reference-batch`: batch label left unadjusted.
152
+
153
+ `--no-optimize-params`: skip the empirical Bayes optimization.
154
+
155
+ `--cpus`: parallelism for the numerical backends / joblib (controls the `n_jobs` argument in `MaxCombat`)
156
+
157
+ `--clip-negative-values`: clip negative values to 0 after correction (useful for specific downstream analyses requiring only positive values)
158
+
159
+ `--out-f32`: convert corrected data to float32 before export
160
+
161
+ ## Feature improvement over `reComBat`
162
+
163
+ - NumPy-only: no pandas dependency, arrays in, arrays out in the `MaxCombat` class. Polars is used to parse and export dataframes when using the CLI.
164
+ - Genes as rows, samples as columns (the usual omics convention, unlike reComBat)
165
+ - Automatic removal of orphan samples (samples alone in their batch)
166
+ - Optional clipping of negative output values (safety net for count data)
167
+ - Sensible default parallelism: min(8, number of CPUs) jobs
168
+ - Automatic detection of input and output file extension (`parquet` / `csv` / `tsv`)
169
+ - Possibility to export data in `float32`, to reduce to memory footprint when dealing with a very large dataset
170
+
171
+ ## License
172
+
173
+ MIT (see [LICENSE](LICENSE)). This package incorporates code from reComBat (BSD-3-Clause), see [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "maxcombat"
7
- version = "0.2.0"
7
+ version = "0.2.2"
8
8
  description = "Scalable batch-effect correction based on reComBat"
9
9
  readme = "README.md"
10
10
  license = { text = "MIT" }
@@ -1,3 +1,3 @@
1
1
  """maxcombat: scalable version of reComBat (batch effect correction)."""
2
2
 
3
- __version__ = "0.2.0"
3
+ __version__ = "0.2.2"
@@ -26,7 +26,7 @@ def parse_args() -> argparse.Namespace:
26
26
  parser = argparse.ArgumentParser(description="maxcombat: scalable batch-effect correction based on reComBat")
27
27
  parser.add_argument(
28
28
  "--counts", type=Path, dest="count_file", required=True,
29
- help="Parquet gathering all count data (genes as rows, samples as columns)"
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"
30
30
  )
31
31
  parser.add_argument(
32
32
  "--design", type=Path, dest="design_file", required=True,
@@ -34,7 +34,7 @@ def parse_args() -> argparse.Namespace:
34
34
  )
35
35
  parser.add_argument(
36
36
  "--gene-id-colname", type=str, dest="gene_id_col",
37
- help="Name of the gene ID column in the counts file",
37
+ help="Name of the gene ID column in the counts file (default: first column)",
38
38
  )
39
39
  parser.add_argument(
40
40
  "--model", help="Regression model used to standardize the data"
@@ -73,12 +73,12 @@ def parse_args() -> argparse.Namespace:
73
73
  help="Perform empirical Bayes optimization (default). Use --no-optimize-params to skip it",
74
74
  )
75
75
  parser.add_argument(
76
- "--x-covariates", dest="x_covariates_file", type=Path, default=None,
77
- help="CSV with a 'sample' column plus covariates of desired variation (e.g. condition). Categorical columns are one-hot encoded (first level dropped)",
76
+ "--preserved-covariates", dest="preserved_covariates_file", type=Path, default=None,
77
+ help="CSV with a 'sample' column plus covariates of desired variation (ie. conditions that should be considered when computing the batch effect correction).",
78
78
  )
79
79
  parser.add_argument(
80
- "--c-covariates", dest="c_covariates_file", type=Path, default=None,
81
- help="CSV with a 'sample' column plus covariates of unwanted variation (e.g. RIN, sex). Categorical columns are one-hot encoded (first level dropped)",
80
+ "--dropped-covariates", dest="dropped_covariates_file", type=Path, default=None,
81
+ help="CSV with a 'sample' column plus covariates of unwanted variation (e.g. RIN, sex) other than batch (e.g. batch-related confounders).",
82
82
  )
83
83
  parser.add_argument(
84
84
  "--out-f32", action="store_true", dest="cast_output_to_float32",
@@ -114,7 +114,7 @@ os.environ.update({
114
114
  import polars as pl # noqa: E402
115
115
 
116
116
  from maxcombat.core import MaxCombat, SingleBatchException # noqa: E402
117
- from maxcombat.utils import parse_table, build_covariates, parse_config, export
117
+ from maxcombat.utils import parse_count_table, build_covariates, parse_config, export
118
118
 
119
119
 
120
120
  # --------------------------------------------------------------
@@ -136,7 +136,7 @@ def main() -> None:
136
136
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137
137
 
138
138
  logger.info(f"Parsing counts from {args.count_file}")
139
- counts, gene_id_col = parse_table(args.count_file, args.gene_id_col)
139
+ counts, gene_id_col = parse_count_table(args.count_file, args.gene_id_col)
140
140
  logger.info(f"Getting design from {args.design_file}")
141
141
  design = pl.read_csv(args.design_file)
142
142
 
@@ -151,9 +151,9 @@ def main() -> None:
151
151
  batches = design["batch"].to_numpy()
152
152
 
153
153
  # parsing covariates if provided
154
- x_covariates = build_covariates(args.x_covariates_file, samples) if args.x_covariates_file else None
155
- c_covariates = build_covariates(args.c_covariates_file, samples) if args.c_covariates_file else None
156
-
154
+ preserved_covariates = build_covariates(args.preserved_covariates_file, samples) if args.preserved_covariates_file else None
155
+ dropped_covariates = build_covariates(args.dropped_covariates_file, samples) if args.dropped_covariates_file else None
156
+
157
157
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158
158
  # BATCH CORRECTION
159
159
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -175,8 +175,8 @@ def main() -> None:
175
175
  corrected = model.fit_transform(
176
176
  data,
177
177
  batches,
178
- X=x_covariates,
179
- C=c_covariates,
178
+ preserved_covariates,
179
+ dropped_covariates,
180
180
  sample_names=samples
181
181
  ) # (n_genes, n_samples)
182
182
  # update new samples, removing orphans that were dropped