Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit of conversion fct #153

Merged
merged 8 commits into from
Nov 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(available_datasets)
export(cell_type_map)
export(cell_type_tree)
export(convert_human_mouse_genes)
export(custom_deconvolution_methods)
export(deconvolute)
export(deconvolute_abis)
Expand Down Expand Up @@ -32,7 +33,6 @@ export(make_bulk_eset)
export(make_random_bulk)
export(map_cell_types)
export(map_result_to_celltypes)
export(mouse_genes_to_human)
export(reduce_mouse_cell_types)
export(scale_to_million)
export(set_cibersort_binary)
Expand Down
120 changes: 73 additions & 47 deletions R/mouse_deconvolution_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ deconvolute_mouse <- function(gene_expression_matrix,
}


#' This function converts the mouse gene symbols into corresponding human ones.
#' This function converts the mouse gene symbols into corresponding human ones, and vice versa.
#'
#' This function relies on the `biomaRt`` package and connects to the ENSEMBL repository
#' to retrieve the gene symbols. If ENSEMBL cannot be reached, another solution will be
Expand All @@ -253,87 +253,113 @@ deconvolute_mouse <- function(gene_expression_matrix,
#' @param mirror the ensembl mirror to use. Possible choices are 'www' (default),
#' 'uswest', 'useast', 'asia'
#' @param other_annot boolean, wether to run the other conversion method (might be memory intensive)
#' @param convert_to one of 'human' or 'mouse'. Specifies the organism of the orthologs to look for
#' @return the same matrix, with the counts for the corresponding human genes.
#' This matrix can directly be used with the immunedeconv methods. A message
#' will display the ratio of original genes which were converted.
#'
#' @export
mouse_genes_to_human <- function(gene_expression_matrix, mirror = "www", other_annot = TRUE) {
gene.names.mouse <- rownames(gene_expression_matrix)
gene_expression_matrix$gene_name <- gene.names.mouse
convert_human_mouse_genes <- function(gene_expression_matrix, mirror = "www",
other_annot = TRUE, convert_to = c("human", "mouse")) {
gene.names <- rownames(gene_expression_matrix)
gene_expression_matrix$gene_name <- gene.names

human <- useEnsembl("ensembl", dataset = "hsapiens_gene_ensembl", mirror = mirror)
mouse <- useEnsembl("ensembl", dataset = "mmusculus_gene_ensembl", mirror = mirror)

genes.retrieved <- NULL
tryCatch(
expr = {
if (convert_to == "human") {
mart.use <- mouse
mart.link <- human
attr <- "mgi_symbol"
attr.link <- "hgnc_symbol"
} else {
mart.use <- human
mart.link <- mouse
attr <- "hgnc_symbol"
attr.link <- "mgi_symbol"
}

genes.retrieved <<- getLDS(
attributes = c("mgi_symbol"),
filters = "mgi_symbol", values = gene.names.mouse,
mart = mouse, attributesL = c("hgnc_symbol"), martL = human, uniqueRows = T
attributes = c(attr),
filters = attr, values = gene.names,
mart = mart.use, attributesL = c(attr.link), martL = mart.link, uniqueRows = T
)

newGenes.counts <- gene_expression_matrix %>%
left_join(., genes.retrieved, by = c("gene_name" = "MGI.symbol")) %>%
select(., -c("gene_name")) %>%
select(., c("HGNC.symbol", everything())) %>%
.[!(is.na(.$HGNC.symbol)), ]

colnames(newGenes.counts)[1] <- "gene_name"
newGenes.counts <- newGenes.counts[!(duplicated(newGenes.counts$gene_name)), ] %>%
as.data.frame(.)
rownames(newGenes.counts) <- newGenes.counts$gene_name
newGenes.counts <- select(newGenes.counts, -c("gene_name"))

fraction <- 100 * (nrow(newGenes.counts) / nrow(gene_expression_matrix)) %>%
round(., 1)

message(paste0("ATTENTION: Only the ", fraction, "% of genes was maintained"))
if (convert_to == "human") {
newGenes.counts <<- gene_expression_matrix %>%
left_join(., genes.retrieved, by = c("gene_name" = "MGI.symbol")) %>%
select(., -c("gene_name")) %>%
select(., c("HGNC.symbol", everything()))
} else {
newGenes.counts <<- gene_expression_matrix %>%
left_join(., genes.retrieved, by = c("gene_name" = "HGNC.symbol")) %>%
select(., -c("gene_name")) %>%
select(., c("MGI.symbol", everything()))
}
},
error = function(e) {
print("Cannot connect to ENSEMBL. Using alternative method. This will take some time.")

if (manual_annot) {
if (other_annot) {
print("Cannot connect to ENSEMBL. Using alternative method. This will take some time.")
# Code adapted from: https://support.bioconductor.org/p/129636/#9144606

mouse_human_genes <- read.csv("http://www.informatics.jax.org/downloads/reports/HOM_MouseHumanSequence.rpt", sep = "\t")

find_corr_gene <- function(gene, mouse_human_genes_df) {
find_corr_gene <- function(gene, mouse_human_genes_df, convert_to = c("human", "mouse")) {
if (convert_to == "human") {
orgn.name <- "mouse, laboratory"
new.orgn <- "human"
} else {
orgn.name <- "human"
new.orgn <- "mouse, laboratory"
}

class_key <- (mouse_human_genes_df %>%
filter(Symbol == gene & Common.Organism.Name == "mouse, laboratory"))[["DB.Class.Key"]]
filter(Symbol == gene & Common.Organism.Name == orgn.name))[["DB.Class.Key"]]

if (!identical(class_key, integer(0))) {
output <- NULL
human_genes <- (mouse_human_genes_df %>% filter(DB.Class.Key == class_key & Common.Organism.Name == "human"))[, "Symbol"]
for (human_gene in human_genes) {
output <- append(output, human_gene)
new_genes <- (mouse_human_genes_df %>% filter(DB.Class.Key == class_key & Common.Organism.Name == new.orgn))[, "Symbol"]

for (new_gene in new_genes) {
output <- append(output, new_gene)
}

if (!is.null(output)) {
return(
data.frame(
"human_gene" = output,
"mouse_gene" = gene
)
)
return(data.frame(
"new_gene" = output,
"old_gene" = gene
))
}
}
}

genes.retrieved <- map_dfr(gene.names.mouse, function(x) find_corr_gene(x, mouse_human_genes))
genes.retrieved <- map_dfr(gene.names, function(x) find_corr_gene(x, mouse_human_genes, convert_to))

newGenes.counts <- gene_expression_matrix %>%
left_join(., genes.retrieved, by = c("gene_name" = "mouse_gene")) %>%
newGenes.counts <<- gene_expression_matrix %>%
left_join(., genes.retrieved, by = c("gene_name" = "old_gene")) %>%
select(., -c("gene_name")) %>%
select(., c("human_gene", everything())) %>%
.[!(is.na(.$human_gene)), ]

fraction <- 100 * (nrow(newGenes.counts) / nrow(gene_expression_matrix)) %>%
round(., 1)

message(paste0("ATTENTION: Only the ", fraction, "% of genes was maintained"))
select(., c("new_gene", everything()))
}
}
)


colnames(newGenes.counts)[1] <- "gene_name"
newGenes.counts <- newGenes.counts[!(is.na(newGenes.counts$gene_name)), ] %>%
group_by(gene_name) %>%
summarise_all(median)

newGenes.counts <- as.data.frame(newGenes.counts)

rownames(newGenes.counts) <- newGenes.counts$gene_name
newGenes.counts <- select(newGenes.counts, -c("gene_name"))

fraction <- 100 * (nrow(newGenes.counts) / nrow(gene_expression_matrix)) %>%
round(., 1)

message(paste0("ATTENTION: Only the ", fraction, "% of genes was maintained"))
return(newGenes.counts)
}
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ reference:
- deconvolute_seqimmucc
- deconvolute_base_algorithm
- deconvolute_dcq
- mouse_genes_to_human
- convert_human_mouse_genes
- title: Deconvolution methods with custom signature
desc: Access to the individual deconvolution methods that allow the use of a custom signature (matrix or gene set)
contents:
Expand Down
Binary file modified inst/extdata/cell_type_mapping.xlsx
Binary file not shown.
13 changes: 8 additions & 5 deletions man/mouse_genes_to_human.Rd → man/convert_human_mouse_genes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions tests/testthat/test_deconvolution_mouse.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ test_that("generic deconvolution works for all methods", {
assert("sample names consistent with input", colnames(res)[-1] == colnames(test_mat))
})
})

# test_that("mouse gene names can be converted into their human orthologus, and vice versa", {
# test_mat_newGenes <- convert_human_mouse_genes(test_mat[1:1000, ], convert_to = "human")
# assert("matrix dimensions consistent", ncol(test_mat_newGenes) == ncol(test_mat))
#
#
# test_that("mouse gene names can be converted into their human orthologus", {
# test_mat_newGenes = mouse_genes_to_human(test_mat)
# test_mat_human <- read_tsv("bulk_mat.tsv") %>%
# as.data.frame() %>%
# tibble::column_to_rownames("gene_symbol")
# test_mat_newGenes <- convert_human_mouse_genes(test_mat_human[1:1000, ], convert_to = "mouse")
# assert("matrix dimensions consistent", ncol(test_mat_newGenes) == ncol(test_mat))
# })
2 changes: 1 addition & 1 deletion vignettes/detailed_example_mouse.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Human-based methods can still be used to deconvolve mouse data through the use o


```R
dataset_petitprez_humanGenes <- mouse_genes_to_human(dataset_petitprez$expr_mat, mirror = 'uswest')
dataset_petitprez_humanGenes <- convert_human_mouse_genes(dataset_petitprez$expr_mat, convert_to = 'human')
res_MCPcounter <- deconvolute(dataset_petitprez_humanGenes, 'mcp_counter')

```
Expand Down
Loading