Skip to content

Commit

Permalink
parallelize deseq2 via config yaml.
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoester committed Oct 13, 2017
1 parent a2982ed commit 2f2bc35
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ params:
star: ""
cutadapt-se: ""
cutadapt-pe: ""
deseq2:
# if you have many samples or conditions, you can use more than 1 thread here.
threads: 1
2 changes: 2 additions & 0 deletions rules/diffexp.smk
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rule deseq2_init:
"../envs/deseq2.yaml"
log:
"logs/deseq2/init.log"
threads: config["params"]["deseq2"]["threads"]
script:
"../scripts/deseq2-init.R"

Expand Down Expand Up @@ -54,5 +55,6 @@ rule deseq2:
"../envs/deseq2.yaml"
log:
"logs/deseq2/{contrast}.diffexp.log"
threads: config["params"]["deseq2"]["threads"]
script:
"../scripts/deseq2.R"
12 changes: 8 additions & 4 deletions scripts/deseq2-init.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ sink(log)
sink(log, type="message")

library("DESeq2")
library("BiocParallel")

# setup parallelization
register(MulticoreParam(snakemake@threads))
parallel <- FALSE
if (snakemake@threads > 1) {
library("BiocParallel")
# setup parallelization
register(MulticoreParam(snakemake@threads))
parallel <- TRUE
}

# colData and countData must have the same sample order, but this is ensured
# by the way we create the count matrix
Expand All @@ -20,6 +24,6 @@ dds <- DESeqDataSetFromMatrix(countData=cts,
# remove uninformative columns
dds <- dds[ rowSums(counts(dds)) > 1, ]
# TODO optionally allow to collapse technical replicates
dds <- DESeq(dds, parallel=TRUE)
dds <- DESeq(dds, parallel=parallel)

saveRDS(dds, file=snakemake@output[[1]])
12 changes: 8 additions & 4 deletions scripts/deseq2.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ sink(log)
sink(log, type="message")

library("DESeq2")
library("BiocParallel")

# setup parallelization
register(MulticoreParam(snakemake@threads))
parallel <- FALSE
if (snakemake@threads > 1) {
library("BiocParallel")
# setup parallelization
register(MulticoreParam(snakemake@threads))
parallel <- TRUE
}

dds <- readRDS(snakemake@input[[1]])

contrast <- c("condition", snakemake@params[["contrast"]])
res <- results(dds, contrast=contrast, parallel=TRUE)
res <- results(dds, contrast=contrast, parallel=parallel)
# shrink fold changes for lowly expressed genes
res <- lfcShrink(dds, contrast=contrast, res=res)
# sort by p-value
Expand Down

0 comments on commit 2f2bc35

Please sign in to comment.