如何使用lapply和biomart更改两个变量

时间:2018-04-29 13:08:41

标签: r loops bioinformatics lapply biomart

我使用了lapply和biomart来提取3种不同物种的同源物。我还需要提取所有同源物的目标ID,我希望也使用lapply作为目标ID,以使我的代码更有效。我到目前为止的代码如下:

加载Biomart:

library(biomaRt)

设置物种载体

species <- c("hsapiens", "mmusculus", "ggallus")

连接所有物种的ensembl

ensembl_hsapiens <- useMart("ensembl", 
                            dataset = "hsapiens_gene_ensembl")
ensembl_mmusculus <- useMart("ensembl", 
                         dataset = "mmusculus_gene_ensembl")
ensembl_ggallus <- useMart("ensembl",
                           dataset = "ggallus_gene_ensembl")  

获取人类基因

hsapien_PC_genes <- getBM(attributes = c("ensembl_gene_id", "external_gene_name"), 
                          filters = "biotype", 
                          values = "protein_coding", 
                          mart = ensembl_hsapiens)

ensembl_gene_ID <- hsapien_PC_genes$ensembl_gene_id

获取同源物但排除人类,因为已经通过使用物种[2:9]

检索了这些物种
all_homologues <- list()



all_homologues <- lapply(species[2:9], function(s) getBM(attributes = c("ensembl_gene_id", 
                                                                   "external_gene_name", 
                                                                   paste0(s, c("_homolog_ensembl_gene",
                                                                               "_homolog_associated_gene_name"))),
                                                    filters = "ensembl_gene_id",
                                                    values = c(ensembl_gene_ID),
                                                    mart = ensembl_hsapiens))

这是我遇到问题的地方,我不知道如何为每个物种分配ensembl_gene_id并使用lapply来运行它。我到目前为止所做的尝试如下:

target_id <- list()

target_id <- lapply(species, function(s) getBM(attributes = c("ensembl_gene_id", 
                                             "external_gene_name", 
                                             "hsapiens_homolog_associated_gene_name", 
                                             "hsapiens_homolog_perc_id"), 
                              filters = "ensembl_gene_id", 
                              values = c(all_homologues[[]][["ensembl_gene_id"]]), 
                              mart = get(paste0("ensembl_", s))))

我可以按照这样的正常方式工作:

target_id[["mmusculus"]] <- getBM(attributes = c("ensembl_gene_id", 
                                             "external_gene_name", 
                                             "hsapiens_homolog_associated_gene_name", 
                                             "hsapiens_homolog_perc_id"), 
                              filters = "ensembl_gene_id", 
                              values = c(all_homologues[["mmusculus"]]$ensembl_gene_id), 
                              mart = ensembl_mmusulus)

target_id[["ggallus"]] <- getBM(attributes = c("ensembl_gene_id", 
                                                 "external_gene_name", 
                                                 "hsapiens_homolog_associated_gene_name", 
                                                 "hsapiens_homolog_perc_id"), 
                                  filters = "ensembl_gene_id", 
                                  values = c(all_homologues[["ggallus"]]$ensembl_gene_id), 
                                  mart = ensembl_ggallus)

但这并不像让我自动改变物种那样有效

1 个答案:

答案 0 :(得分:2)

我找到了一个解决方案:

target_id <- lapply(species[-1], function(s) getBM(attributes = c("ensembl_gene_id", 
                                             "external_gene_name", 
                                             "hsapiens_homolog_associated_gene_name", 
                                             "hsapiens_homolog_perc_id"), 
                              filters = "ensembl_gene_id", 
                              values = all_homologues[[paste0(s)]][paste0(s, "_homolog_ensembl_gene")], 
                              mart = ensembl[[paste0(s)]]))