brentp / combat.py

python / numpy / pandas / patsy version of ComBat for removing batch effects.
MIT License
101 stars 44 forks source link

ComBat is an R package for removing batch effects from data. This is a python version that matches the output from the ComBat function in SVA (http://www.bioconductor.org/packages/release/bioc/html/sva.html). This code is completely copied from the ComBat function in that package.

Compare

To test, run this R code (requires sva and bladderbatch from bioconductor):


Rscript R-combat.R

Then, from the same directory, run


python combat.py

you can then run this python code to see the differences:

import pandas as pa
p = pa.read_table('py-batch.txt', index_col=0)
r = pa.read_table('r-batch.txt', index_col=0)

print (p - r).max().max()

This outputs 3.9423421307e-05 on my machine. Indicating that that is the largest difference between the 22,283*57 values generated by the R version and those generated in this version.

Performance

In the example above, the combat function runs in < 1 second in python and about 15 seconds in R.

On an identical dataset, of 30K rows 190 samples, this python version finishes in 10.008s as measured by unix time. The R version takes 4m0.681s with output identical to 3 decimal places. This is a speed-up of about 24x.*

The speed improvement seems to be larger for larger datasets.

Function

The python version is usable as a module, the function has the signature:


   combat(dat, batch, mod, numCovs=None)

which is the same as the R function except the non-parametric version is not supported.

Read

Johnson WE, Rabinovic A, Li C (2007). Adjusting batch effects in microarray
expression data using Empirical Bayes methods. Biostatistics 8:118-127.  

Jeffrey T. Leek, W. Evan Johnson, Hilary S. Parker, Andrew E. Jaffe
and John D. Storey (). sva: Surrogate Variable Analysis. R package
version 3.4.0.