Skip to content

Commit

Permalink
Merge branch 'fix_conversion_fuction' of https://github.com/omnidecon…
Browse files Browse the repository at this point in the history
…v/immunedeconv into fix_conversion_fuction
  • Loading branch information
Merotto committed Nov 3, 2023
2 parents 9504bf1 + e9af770 commit a0135fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
47 changes: 24 additions & 23 deletions R/mouse_deconvolution_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ deconvolute_mouse <- function(gene_expression_matrix,
#'
#' @export
convert_human_mouse_genes <- function(gene_expression_matrix, mirror = "www",
other_annot = TRUE, convert_to = c('human', 'mouse')) {
other_annot = TRUE, convert_to = c("human", "mouse")) {
gene.names <- rownames(gene_expression_matrix)
gene_expression_matrix$gene_name <- gene.names

Expand All @@ -270,25 +270,25 @@ convert_human_mouse_genes <- function(gene_expression_matrix, mirror = "www",
genes.retrieved <- NULL
tryCatch(
expr = {

if(convert_to == 'human'){
mart.use = mouse
mart.link = human
attr = 'mgi_symbol'
attr.link = 'hgnc_symbol'
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'
mart.use <- human
mart.link <- mouse
attr <- "hgnc_symbol"
attr.link <- "mgi_symbol"
}

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

if(convert_to == 'human'){
if (convert_to == "human") {
newGenes.counts <<- gene_expression_matrix %>%
left_join(., genes.retrieved, by = c("gene_name" = "MGI.symbol")) %>%
select(., -c("gene_name")) %>%
Expand All @@ -301,21 +301,19 @@ convert_human_mouse_genes <- function(gene_expression_matrix, mirror = "www",
}
},
error = function(e) {

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, convert_to = c('human', 'mouse')) {

if(convert_to == 'human'){
orgn.name <- 'mouse, laboratory'
new.orgn <- 'human'
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'
orgn.name <- "human"
new.orgn <- "mouse, laboratory"
}

class_key <- (mouse_human_genes_df %>%
Expand All @@ -330,8 +328,11 @@ convert_human_mouse_genes <- function(gene_expression_matrix, mirror = "www",
}

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

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_deconvolution_mouse.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ test_that("generic deconvolution works for all methods", {
})

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')
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_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')
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))
})

0 comments on commit a0135fb

Please sign in to comment.