I recently asked a question about lapply, but now I've changed my approach and ran into more problems I can't resolve.
This is the code I'm having issues with:
Load biomaRt
library(biomaRt)
Make the hsapiens mart
ensembl_hsapiens <- useMart("ensembl",
dataset = "hsapiens_gene_ensembl")
I then extract the human genes:
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
Set the species vector
species <- c("mmusculus", "ggallus")
This all works as expected, I run into problems when I use lapply
all_homologues <- lapply(species, function(s) getBM(attributes = c("ensembl_gene_id",
"external_gene_name",
get(paste0(s, "_homolog_ensembl_gene")),
get(paste0(s, "_homolog_associated_ensembl_gene")),
filters = "ensembl_gene_id",
values = c(ensembl_gene_ID),
mart = ensembl_hsapiens)))
I get the error message:
Error in martCheck(mart) :
You must provide a valid Mart object. To create a Mart object use the function: useMart. Check ?useMart for more information.
The code works perfect like this:
all_homologues <- getBM(attributes = c("ensembl_gene_id", "external_gene_name",
"mmusculus_homolog_ensembl_gene",
"mmusculus_homolog_associated_gene_name"),
filters = "ensembl_gene_id",
values = c(ensembl_gene_ID),
mart = ensembl_hsapiens)
But I have many species, so I want to be able to use a small bit of code to extract the homologues for the species, rather than have to rewrite it all for each species.
I think I'm doing something wrong with lapply as the mart works perfectly when not using lapply.
My previous question: Issue with lapply using biomart
After playing around with it, I've managed to get it to work by using:
all_homologues <- lapply(species, 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))
Although I'm not exactly sure why?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.