Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/nf-core/gprofiler2/gconvert/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/nf-core/modules/master/modules/environment-schema.json
channels:
- conda-forge
- bioconda
dependencies:
- conda-forge::r-gprofiler2=0.2.4
37 changes: 37 additions & 0 deletions modules/nf-core/gprofiler2/gconvert/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
process GPROFILER2_GCONVERT {
tag "$meta.id"
label 'process_single'

conda "${moduleDir}/environment.yml"
container "${ workflow.containerEngine in ['singularity', 'apptainer'] && !task.ext.singularity_pull_docker_container ?
'oras://community.wave.seqera.io/library/r-gprofiler2:0.2.4--ada5695b0be8ddbc':
'community.wave.seqera.io/library/r-gprofiler2:0.2.4--c87811ab729aa1b3' }"

input:
tuple val(meta), path(ids_tsv), val(target)

output:
tuple val(meta), path("*.gprofiler2.gconvert.tsv"), emit: converted_ids
tuple val(meta), path("*R_sessionInfo.log") , emit: session_info
path "versions.yml" , emit: versions, topic: versions

when:
task.ext.when == null || task.ext.when

script:
template 'gprofiler2_gconvert.R'

stub:
def prefix = task.ext.prefix ?: meta.id
"""
touch ${prefix}.gprofiler2.gconvert.tsv
touch ${prefix}.R_sessionInfo.log

cat <<-END_VERSIONS > versions.yml
"${task.process}":
r-base: \$(Rscript -e "cat(strsplit(R.version[['version.string']], ' ')[[1]][3])")
r-gprofiler2: \$(Rscript -e "cat(as.character(packageVersion('gprofiler2')))")
gprofiler-data: \$(Rscript -e "cat(gprofiler2::get_version_info()[['gprofiler_version']])")
END_VERSIONS
"""
}
73 changes: 73 additions & 0 deletions modules/nf-core/gprofiler2/gconvert/meta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: "gprofiler2_gconvert"
description: Convert gene and feature identifiers with gProfiler g:Convert
keywords:
- gprofiler2
- gconvert
- identifier conversion
- gene ids
tools:
- "gprofiler2":
description: "An R package for gene list functional enrichment analysis and namespace conversion with g:Profiler."
homepage: "https://biit.cs.ut.ee/gprofiler/page/r"
documentation: "https://cran.r-project.org/package=gprofiler2"
tool_dev_url: "https://github.com/gprofiler/gprofiler2"
doi: "10.1093/bioinformatics/btab213"
licence:
- "GPL-3.0-or-later"
identifier: biotools:gprofiler
input:
- - meta:
type: map
description: |
Groovy Map containing sample information
e.g. `[ id:'sample1' ]`
- ids_tsv:
type: file
description: TSV, TXT, or CSV table containing input IDs in the first column.
pattern: "*.{tsv,txt,csv}"
ontologies:
- edam: http://edamontology.org/format_3475 # TSV
- edam: http://edamontology.org/format_3752 # CSV
- target:
type: string
description: Target namespace to pass to gprofiler2::gconvert().
output:
converted_ids:
- - meta:
type: map
description: |
Groovy Map containing sample information
e.g. `[ id:'sample1' ]`
- "*.gprofiler2.gconvert.tsv":
type: file
description: TSV table returned by gprofiler2::gconvert().
pattern: "*.gprofiler2.gconvert.tsv"
ontologies:
- edam: http://edamontology.org/format_3475 # TSV
session_info:
- - meta:
type: map
description: |
Groovy Map containing sample information
e.g. `[ id:'sample1' ]`
- "*R_sessionInfo.log":
type: file
description: R session information.
pattern: "*R_sessionInfo.log"
ontologies: []
versions:
- versions.yml:
type: file
description: File containing software versions
pattern: "versions.yml"
ontologies:
- edam: http://edamontology.org/format_3750 # YAML
topics:
versions:
- versions.yml:
type: string
description: File containing software versions
authors:
- "@Schansiate"
maintainers:
- "@Schansiate"
209 changes: 209 additions & 0 deletions modules/nf-core/gprofiler2/gconvert/templates/gprofiler2_gconvert.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#!/usr/bin/env Rscript

#written by Mo Tan (https://github.com/Schansiate) modified from original script by Oliver Wacker (https://github.com/WackerO)

# MIT License

# Copyright (c) QBiC

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

################################################
################################################
## Functions ##
################################################
################################################

#' Parse out options from a string without recourse to optparse
#'
#' @param x Long-form argument list like --opt1 val1 --opt2 val2
#'
#' @return named list of options and values similar to optparse

parse_args <- function(x) {
args_list <- unlist(strsplit(x, ' ?--')[[1]])[-1]
args_vals <- lapply(args_list, function(x) scan(text=x, what='character', quiet = TRUE))

# Ensure the option vectors are length 2 (key/ value) to catch empty ones
args_vals <- lapply(args_vals, function(z) { length(z) <- 2; z})

parsed_args <- structure(lapply(args_vals, function(x) x[2]), names = lapply(args_vals, function(x) x[1]))
parsed_args[! is.na(parsed_args)]
}

#' Flexibly read CSV or TSV files
#'
#' @param file Input file
#' @param header Passed to read.delim()
#' @param row.names Passed to read.delim()
#'
#' @return output Data frame

read_delim_flexible <- function(file, header = TRUE, row.names = NULL, check.names = FALSE) {

ext <- tolower(tail(strsplit(basename(file), split = "\\\\.")[[1]], 1))

if (ext == "tsv" || ext == "txt") {
separator <- "\\t"
} else if (ext == "csv") {
separator <- ","
} else {
stop(paste("Unknown separator for", ext))
}

read.delim(
file,
sep = separator,
header = header,
row.names = row.names,
check.names = check.names
)
}

################################################
################################################
## PARSE PARAMETERS FROM NEXTFLOW ##
################################################
################################################

opt <- list(
ids_tsv = '$ids_tsv',
target = '$target',
organism = 'hsapiens',
numeric_ns = '',
mthreshold = Inf,
filter_na = TRUE,
output_prefix = ifelse('$task.ext.prefix' == 'null', '$meta.id', '$task.ext.prefix')
)

opt_types <- lapply(opt, class)

args_opt <- parse_args('$task.ext.args')
allowed_args <- c('organism', 'numeric_ns', 'mthreshold', 'filter_na')
for (ao in names(args_opt)) {
if (! ao %in% allowed_args) {
stop(paste("Invalid option:", ao))
} else {
if (! is.null(opt[[ao]])) {
args_opt[[ao]] <- as(args_opt[[ao]], opt_types[[ao]])
}
opt[[ao]] <- args_opt[[ao]]
}
}

required_opts <- c('ids_tsv', 'target', 'output_prefix')
missing <- required_opts[unlist(lapply(opt[required_opts], is.null)) | ! required_opts %in% names(opt)]

if (length(missing) > 0) {
stop(paste("Missing required options:", paste(missing, collapse=', ')))
}

if (opt\$target == "") {
stop("Please provide target.")
}

if (! file.exists(opt\$ids_tsv)) {
stop(paste0('Value of ids_tsv: ', opt\$ids_tsv, ' is not a valid file'))
}

################################################
################################################
## Finish loading libraries ##
################################################
################################################

library(gprofiler2)

################################################
################################################
## READ IDS AND RUN GCONVERT ##
################################################
################################################

ids_table <- read_delim_flexible(
file = opt\$ids_tsv
)

if (ncol(ids_table) < 1) {
stop("The input ID table must contain at least one column.")
}

output_prefix <- paste0(opt\$output_prefix, ".gprofiler2")
output_file <- paste(output_prefix, 'gconvert', 'tsv', sep = '.')

query <- as.character(ids_table[[1]])
query <- query[!is.na(query) & query != ""]

if (length(query) > 0) {
gconvert_results <- gconvert(
query = query,
organism = opt\$organism,
target = opt\$target,
numeric_ns = opt\$numeric_ns,
mthreshold = opt\$mthreshold,
filter_na = opt\$filter_na
)

write.table(
gconvert_results,
file = output_file,
col.names = TRUE,
row.names = FALSE,
sep = '\t',
quote = FALSE
)
} else {
print("No input IDs found, gProfiler2 g:Convert will be skipped.")
file.create(output_file)
}

################################################
################################################
## R SESSION INFO ##
################################################
################################################

sink("R_sessionInfo.log")
print(sessionInfo())
sink()

################################################
################################################
## VERSIONS FILE ##
################################################
################################################

r.version <- strsplit(version[['version.string']], ' ')[[1]][3]
gprofiler2.version <- as.character(packageVersion('gprofiler2'))
gprofiler_data.version <- gprofiler2::get_version_info(opt[["organism"]])[["gprofiler_version"]]

writeLines(
c(
'"$task.process":',
paste0(' r-base: ', r.version),
paste0(' r-gprofiler2: ', gprofiler2.version),
paste0(' gprofiler-data: ', gprofiler_data.version)
),
con = 'versions.yml'
)

################################################
################################################
################################################
################################################
5 changes: 5 additions & 0 deletions modules/nf-core/gprofiler2/gconvert/tests/input_ids.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id
Klf4
Pax5
Sox2
Nanog
Loading
Loading