maxcombat 0.2.3__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maxcombat
3
- Version: 0.2.3
3
+ Version: 0.2.5
4
4
  Summary: Scalable batch-effect correction based on reComBat
5
5
  Author: Olivier Coen
6
6
  License: MIT
@@ -59,6 +59,8 @@ pip install maxcombat
59
59
 
60
60
  ### From the command line
61
61
 
62
+ #### Basic usage
63
+
62
64
  ```bash
63
65
  maxcombat \
64
66
  --counts counts.parquet \
@@ -91,7 +93,18 @@ sample_E,batch_2
91
93
  > - the design dataframe may contain other columns, but only the `sample` and `batch` columns will be parsed
92
94
  > - batches containing only one sample are removed from the analysis
93
95
 
94
- Example of preserved covariates file:
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:
95
108
 
96
109
  ```csv title=preserved_covariate.csv
97
110
  sample,condition,age
@@ -102,7 +115,7 @@ sample_D,control,29
102
115
  sample_E,control,63
103
116
  ```
104
117
 
105
- Example of unwated (dropped) covariates file:
118
+ Example of unwanted (dropped) covariates file:
106
119
 
107
120
  ```csv title=dropped_covariate.csv
108
121
  sample,rin
@@ -160,12 +173,12 @@ sample_E,6.9
160
173
  ```python
161
174
  from maxcombat import MaxCombat
162
175
 
163
- data = ... # (n_genes, n_samples) array
164
- batches = ... # (n_samples,) array of batch labels
176
+ data = ... # (n_genes, n_samples) array
177
+ batches = ... # (n_samples,) array of batch labels
165
178
 
166
- model = MaxCombat() # default model: elastic_net
167
- model.fit(data,batches)
168
- model.transform(data,batches)
179
+ model = MaxCombat() # default model: elastic_net
180
+ model.fit(data, batches)
181
+ model.transform(data, batches)
169
182
  ```
170
183
 
171
184
  or:
@@ -28,6 +28,8 @@ pip install maxcombat
28
28
 
29
29
  ### From the command line
30
30
 
31
+ #### Basic usage
32
+
31
33
  ```bash
32
34
  maxcombat \
33
35
  --counts counts.parquet \
@@ -60,7 +62,18 @@ sample_E,batch_2
60
62
  > - the design dataframe may contain other columns, but only the `sample` and `batch` columns will be parsed
61
63
  > - batches containing only one sample are removed from the analysis
62
64
 
63
- Example of preserved covariates file:
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:
64
77
 
65
78
  ```csv title=preserved_covariate.csv
66
79
  sample,condition,age
@@ -71,7 +84,7 @@ sample_D,control,29
71
84
  sample_E,control,63
72
85
  ```
73
86
 
74
- Example of unwated (dropped) covariates file:
87
+ Example of unwanted (dropped) covariates file:
75
88
 
76
89
  ```csv title=dropped_covariate.csv
77
90
  sample,rin
@@ -129,12 +142,12 @@ sample_E,6.9
129
142
  ```python
130
143
  from maxcombat import MaxCombat
131
144
 
132
- data = ... # (n_genes, n_samples) array
133
- batches = ... # (n_samples,) array of batch labels
145
+ data = ... # (n_genes, n_samples) array
146
+ batches = ... # (n_samples,) array of batch labels
134
147
 
135
- model = MaxCombat() # default model: elastic_net
136
- model.fit(data,batches)
137
- model.transform(data,batches)
148
+ model = MaxCombat() # default model: elastic_net
149
+ model.fit(data, batches)
150
+ model.transform(data, batches)
138
151
  ```
139
152
 
140
153
  or:
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "maxcombat"
7
- version = "0.2.3"
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]
@@ -1,3 +1,3 @@
1
1
  """maxcombat: scalable version of reComBat (batch effect correction)."""
2
2
 
3
- __version__ = "0.2.3"
3
+ __version__ = "0.2.5"
@@ -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,79 +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(description="maxcombat: scalable batch-effect correction based on reComBat")
27
- parser.add_argument(
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
- "--design", type=Path, dest="design_file", required=True,
33
- help="CSV design file with 'sample' and 'batch' columns"
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
- "--gene-id-colname", type=str, dest="gene_id_col",
37
- help="Name of the gene ID column in the counts file (default: first column)",
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
- "--model", help="Regression model used to standardize the data"
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", action=argparse.BooleanOptionalAction, default=True,
44
- help="Parametric (default) or --no-parametric empirical Bayes optimization"
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", type=Path, dest="config_file",
48
- help="JSON file with keyword arguments for the scikit-learn regressor"
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", dest="conv_criterion", type=float,
52
- help="Relative convergence criterion (default 1e-4)"
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", dest="max_iter", type=int,
56
- help="Maximum number of iterations (default 1000)"
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", dest="mean_only", action="store_true",
60
- help="Adjust the mean only, no scaling"
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", dest="clip_negative_values", action="store_true",
64
- help="Clip negative output values to 0"
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", dest="reference_batch", default=None,
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", dest="optimize_params",
72
- action=argparse.BooleanOptionalAction, default=True,
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", dest="preserved_covariates_file", type=Path, default=None,
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", dest="dropped_covariates_file", type=Path, default=None,
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", type=Path, dest="outfile", required=True,
85
- help="Output Parquet file"
114
+ "--out", type=Path, dest="outfile", required=True, help="Output Parquet file"
86
115
  )
87
116
  parser.add_argument(
88
- "--out-float32", action="store_true", dest="cast_output_to_float32",
117
+ "--out-float32",
118
+ action="store_true",
119
+ dest="cast_output_to_float32",
89
120
  help="Cast corrected data to float32 before export to save memory",
90
121
  )
91
122
  parser.add_argument(
92
- "--out-ndigits", dest="nb_digits_output", type=int,
123
+ "--out-ndigits",
124
+ dest="nb_digits_output",
125
+ type=int,
93
126
  help="Number of digits to keep in the output. Defaults to all digits",
94
127
  )
95
128
  parser.add_argument(
96
- "--cpus", type=int,
97
- help="Number of threads for numpy/polars. Defaults to min(8, <number of available cores>"
129
+ "--cpus",
130
+ type=int,
131
+ help="Number of threads for numpy/polars. Defaults to min(8, <number of available cores>",
98
132
  )
99
133
  return parser.parse_args()
100
134
 
@@ -107,31 +141,31 @@ args = parse_args()
107
141
 
108
142
  # Limit thread usage; must be set before importing numpy-heavy libs.
109
143
  n_cpus = args.cpus or min(DEFAULT_NJOBS, cpu_count())
110
- os.environ.update({
111
- "OMP_NUM_THREADS": str(n_cpus),
112
- "OPENBLAS_NUM_THREADS": str(n_cpus),
113
- "MKL_NUM_THREADS": str(n_cpus),
114
- "NUMEXPR_NUM_THREADS": str(n_cpus),
115
- "POLARS_MAX_THREADS": str(n_cpus),
116
- })
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
+ )
117
153
 
118
- import polars as pl # noqa: E402
154
+ import polars as pl # noqa: I001
119
155
 
120
- from maxcombat.core import MaxCombat, SingleBatchException # noqa: E402
121
- from maxcombat.utils import parse_count_table, build_covariates, parse_config, export
122
156
 
157
+ from maxcombat.core import MaxCombat, SingleBatchException
158
+ from maxcombat.utils import build_covariates, export, parse_config, parse_count_table
123
159
 
124
160
  # --------------------------------------------------------------
125
161
  # MAIN
126
162
  # --------------------------------------------------------------
127
163
 
164
+
128
165
  def main() -> None:
129
-
130
166
 
131
167
  logging.basicConfig(
132
- level=logging.INFO,
133
- stream=sys.stdout,
134
- format="[%(levelname)s] %(message)s"
168
+ level=logging.INFO, stream=sys.stdout, format="[%(levelname)s] %(message)s"
135
169
  )
136
170
  logger = logging.getLogger("maxcombat-cli")
137
171
 
@@ -149,21 +183,29 @@ def main() -> None:
149
183
  counts = counts.drop(gene_id_col)
150
184
 
151
185
  # taking only the subpart of the design that corresponds to the count data
152
- design = design.filter(pl.col('sample').is_in(samples))
153
-
154
- data = counts.to_numpy() # (n_genes, n_samples)
186
+ design = design.filter(pl.col("sample").is_in(samples))
187
+
188
+ data = counts.to_numpy() # (n_genes, n_samples)
155
189
  batches = design["batch"].to_numpy()
156
190
 
157
191
  # parsing covariates if provided
158
- preserved_covariates = build_covariates(args.preserved_covariates_file, samples) if args.preserved_covariates_file else None
159
- dropped_covariates = build_covariates(args.dropped_covariates_file, samples) if args.dropped_covariates_file else None
160
-
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
+
161
203
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162
204
  # BATCH CORRECTION
163
205
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164
206
 
165
207
  model = MaxCombat(
166
- parametric=args.parametric, # False if --no-parametric is provided
208
+ parametric=args.parametric, # False if --no-parametric is provided
167
209
  model=args.model,
168
210
  config=parse_config(args.config_file),
169
211
  conv_criterion=args.conv_criterion,
@@ -172,16 +214,16 @@ def main() -> None:
172
214
  optimize_params=args.optimize_params,
173
215
  reference_batch=args.reference_batch,
174
216
  clip_negative_values=args.clip_negative_values,
175
- n_jobs=n_cpus
217
+ n_jobs=n_cpus,
176
218
  )
177
219
 
178
220
  try:
179
221
  corrected = model.fit_transform(
180
- data,
181
- batches,
222
+ data,
223
+ batches,
182
224
  preserved_covariates,
183
225
  dropped_covariates,
184
- sample_names=samples
226
+ sample_names=samples,
185
227
  ) # (n_genes, n_samples)
186
228
  # update new samples, removing orphans that were dropped
187
229
  samples = [s for s, k in zip(samples, model.keep_mask_) if k]
@@ -190,20 +232,20 @@ def main() -> None:
190
232
  corrected = data
191
233
 
192
234
  assert corrected.shape[1] == len(samples)
193
-
235
+
194
236
  # post-processing (clipping negative values if needed)
195
237
  corrected = model.post_process(corrected)
196
238
 
197
239
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198
240
  # POSTPROCESSING AND EXPORT
199
241
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
-
242
+
201
243
  out_df = pl.DataFrame(corrected, schema=samples)
202
244
 
203
245
  if args.nb_digits_output is not None:
204
246
  logger.info(f"Rounding output to {args.nb_digits_output} digits")
205
247
  out_df = out_df.select(pl.all().round(args.nb_digits_output))
206
-
248
+
207
249
  if args.cast_output_to_float32:
208
250
  logger.info("Casting output to float32")
209
251
  out_df = out_df.select(pl.all().cast(pl.Float32))
@@ -212,9 +254,8 @@ def main() -> None:
212
254
 
213
255
  logger.info(f"Exporting corrected counts to {args.outfile}")
214
256
  export(out_df, args.outfile)
215
-
216
- logger.info("Done")
217
257
 
258
+ logger.info("Done")
218
259
 
219
260
 
220
261
  if __name__ == "__main__":