Skip to content

Commit

Permalink
feat: Immunedeconv (#1741)
Browse files Browse the repository at this point in the history
<!-- Ensure that the PR title follows conventional commit style (<type>:
<description>)-->
<!-- Possible types are here:
https://github.com/commitizen/conventional-commit-types/blob/master/index.json
-->

### Description

This PR provides
[immunedeconv](https://github.com/omnideconv/immunedeconv/tree/master/data)
as a new available wrapper. This wrapper opens all deconvolution methods
in a single script (just like `xsv` and `seqkit` wrappers) since IO
files are (almost) identical between every immunedeconv functions.

### QC
<!-- Make sure that you can tick the boxes below. -->

* [X] I confirm that:

For all wrappers added by this PR, 

* there is a test case which covers any introduced changes,
* `input:` and `output:` file paths in the resulting rule can be changed
arbitrarily,
* either the wrapper can only use a single core, or the example rule
contains a `threads: x` statement with `x` being a reasonable default,
* rule names in the test case are in
[snake_case](https://en.wikipedia.org/wiki/Snake_case) and somehow tell
what the rule is about or match the tools purpose or name (e.g.,
`map_reads` for a step that maps reads),
* all `environment.yaml` specifications follow [the respective best
practices](https://stackoverflow.com/a/64594513/2352071),
* wherever possible, command line arguments are inferred and set
automatically (e.g. based on file extensions in `input:` or `output:`),
* all fields of the example rules in the `Snakefile`s and their entries
are explained via comments (`input:`/`output:`/`params:` etc.),
* `stderr` and/or `stdout` are logged correctly (`log:`), depending on
the wrapped tool,
* temporary files are either written to a unique hidden folder in the
working directory, or (better) stored where the Python function
`tempfile.gettempdir()` points to (see
[here](https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir);
this also means that using any Python `tempfile` default behavior
works),
* the `meta.yaml` contains a link to the documentation of the respective
tool or command,
* `Snakefile`s pass the linting (`snakemake --lint`),
* `Snakefile`s are formatted with
[snakefmt](https://github.com/snakemake/snakefmt),
* Python wrapper scripts are formatted with
[black](https://black.readthedocs.io).
* Conda environments use a minimal amount of channels, in recommended
ordering. E.g. for bioconda, use (conda-forge, bioconda, nodefaults, as
conda-forge should have highest priority and defaults channels are
usually not needed because most packages are in conda-forge nowadays).

---------

Co-authored-by: tdayris <tdayris@gustaveroussy.fr>
Co-authored-by: tdayris <thibault.dayris@gustaveroussy.fr>
Co-authored-by: Johannes Köster <johannes.koester@uni-due.de>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: snakedeploy-bot[bot] <115615832+snakedeploy-bot[bot]@users.noreply.github.com>
Co-authored-by: Felix Mölder <felix.moelder@uni-due.de>
Co-authored-by: Christopher Schröder <christopher.schroeder@tu-dortmund.de>
  • Loading branch information
8 people committed Aug 25, 2023
1 parent 0bd316d commit 97b5bde
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bio/immunedeconv/environment.yaml
@@ -0,0 +1,6 @@
channels:
- conda-forge
- bioconda
- nodefaults
dependencies:
- r-immunedeconv =2.1.2
20 changes: 20 additions & 0 deletions bio/immunedeconv/meta.yaml
@@ -0,0 +1,20 @@
name: immunedeconv
description: |
Perform immune deconvolution from human or mouse gene expression with immunedeconv
url: https://github.com/omnideconv/immunedeconv/tree/master
authors:
- Thibault Dayris
input:
- expr: Path to gene expression matrix (RDS, CSV, or TSV formatted).
- signature: Optional path to custom cell-type signatures.
- cibersort_bin: Path to CIBERSORT binary, required for CIBERSORT deconvolution.
- cibersort_mat: Path to CIBERSORT signatures matrix, required for CIBERSORT deconvolution.
output:
- Path to deconvolution result (RDS, CSV, or TSV formatted).
params:
- method: Optional immunedeconv method to use, default = "deconvolute"
- extra: Optional parameters to provide to immunedeconv, besides `gene_expression_matrix`, and `signature_matrix`.
note: |
* Input and output matrices cas be either `RDS`, `csv` or `tsv` formatted.
* Since CIBERSORT is not available through `conda`, binary must be provided.
* Warning: The `method` parameter is case sensitive.
16 changes: 16 additions & 0 deletions bio/immunedeconv/test/Snakefile
@@ -0,0 +1,16 @@
rule test_immunedeconv:
input:
expr="dataset.RDS",
# signature="", # Path to custom signatures.
# cibersort_bin="", # Path to CIBERSORT binary.
# cibersort_mat="", # Path to CIBERSORT matrix.
output:
"deconv.csv", # Available formats are: RDS, csv or tsv
threads: 1
params:
method="deconvolute",
extra="method='epic'",
log:
"logs/immunedeconv/csv.log",
wrapper:
"master/bio/immunedeconv"
Binary file added bio/immunedeconv/test/dataset.RDS
Binary file not shown.
138 changes: 138 additions & 0 deletions bio/immunedeconv/wrapper.R
@@ -0,0 +1,138 @@
# This script takes a gene expression matrix
# and run ImmuneDeconv to estimate immune cell
# population(s).

# __author__ = "Thibault Dayris"
# __copyright__ = "Copyright 2023, Thibault Dayris"
# __email__ = "thibault.dayris@gustaveroussy.fr"
# __license__ = "MIT"

# Sink the stderr and stdout to the snakemake log file
# https://stackoverflow.com/a/48173272
log_file <- base::file(description = snakemake@log[[1]], open = "wt")
base::sink(file = log_file)
base::sink(file = log_file, type = "message")

# Loading libary
base::library("immunedeconv", character.only = TRUE)
base::message("Library loaded")


# Function used to load matrices depending on their format
load_input_file <- function(path) {
data_matrix <- NULL
if (base::endsWith(x = path, suffix = "RDS")) {
data_matrix <- base::readRDS(file = path)
} else if (base::endsWith(x = path, suffix = "tsv")) {
data_matrix <- utils::read.table(
file = path,
sep = "\t",
header = TRUE,
row.names = TRUE
)
} else if (base::endsWith(x = path, suffix = "csv")) {
data_matrix <- utils::read.table(
file = path,
sep = ",",
header = TRUE,
row.names = TRUE
)
}
base::return(data_matrix)
}

# Setting-up threading (only xCell uses this)
base::Sys.setenv(
MAX_CORES = base::as.numeric(x = snakemake@threads)
)

# Loading input file, using its extension
# to detect file format
gene_expression_matrix <- load_input_file(
path = base::as.character(x = snakemake@input[["expr"]])
)
base::message("Gene expression loaded")

method <- "deconvolute"
if ("method" %in% base::names(x = snakemake@params)) {
method <- base::as.character(x = snakemake@params[["method"]])
}

# Handling special case of CIBERSORT
if (base::grepl(pattern = "cibersort", x = method, ignore.case = TRUE)) {
cibersort_binary <- base::as.character(
x = snakemake@input[["cibersort_binary"]]
)
immunedeconv::set_cibersort_binary(path = cibersort_binary)

cibersort_mat <- base::as.character(
x = snakemake@input[["cibersort_mat"]]
)
immunedeconv::set_cibersort_mat(path = cibersort_mat)
base::message("CIBERSORT binary and matrix linked")
}

# Build command line
extra <- "gene_expression = gene_expression_matrix"
if ("extra" %in% base::names(x = snakemake@params)) {
param_extra <- base::as.character(x = snakemake@params[["extra"]])
if (param_extra != "") {
extra <- base::paste(
extra,
param_extra,
sep = ", "
)
}
}

signature_matrix <- NULL
if ("signature" %in% base::names(x = snakemake@input)) {
signature_matrix <- load_input_file(
path = base::as.character(x = snakemake@input[["signature"]])
)
extra <- base::paste(
extra,
"signature_matrix = signature_matrix",
sep = ", "
)
base::message("Custom signatures loaded")
}

cmd <- base::paste0("immunedeconv::", method, "(", extra, ")")
base::message("Command line used:")
base::message(cmd)

# Running ImmuneDeconv
deconvolution_results <- base::eval(base::parse(text = cmd))
base::message("Immunedeconv done")

# Saving results
output_file <- base::as.character(x = snakemake@output[[1]])
if (base::endsWith(x = output_file, suffix = "RDS")) {
base::message("Saving results as RDS")
base::saveRDS(
object = deconvolution_results,
file = output_file
)
} else if (base::endsWith(x = output_file, suffix = "csv")) {
base::message("Saving results as CSV")
utils::write.table(
x = deconvolution_results,
file = output_file,
sep = ","
)
} else if (base::endsWith(x = output_file, suffix = "tsv")) {
base::message("Saving results as TSV")
utils::write.table(
x = deconvolution_results,
file = output_file,
sep = "\t"
)
}
base::message("Process over.")


# Proper syntax to close the connection for the log file
# but could be optional for Snakemake wrapper
base::sink(type = "message")
base::sink()
8 changes: 8 additions & 0 deletions test.py
Expand Up @@ -2949,6 +2949,14 @@ def test_homer_makeTagDirectory():
)


@skip_if_not_modified
def test_immunedeconv():
run(
"bio/immunedeconv",
["snakemake", "--cores", "1", "--use-conda", "-F", "deconv.csv"]
)


@skip_if_not_modified
def test_jellyfish_count():
run(
Expand Down

0 comments on commit 97b5bde

Please sign in to comment.