My data are genes in a list of list structure, like this:
>listoflists <- list(samp1 = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457"),
samp2 = c("ENSG00000002834", "ENSG00000002919", "ENSG00000002933"),
samp3 = c("ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167"))
I'm trying to convert gene identifiers. When working with similar data in a dataframe structure, I've successfully used code like this:
>library(org.Hs.eg.db)
>gene_df$symbol <- mapIds(org.Hs.eg.db,keys=rownames(gene_df),column="SYMBOL",keytype="ENSEMBL",multiVals="first")
But now I'm working with a list of lists. I would like to keep the same structure, and I think the answer provided here should give me insight, but when I try to use a nested apply command like this:
>convertedLoL <- lapply(listoflists, function(x) lapply(listoflists[x], function(i)mapIds(org.Hs.eg.db,keys=listoflists[i],column="SYMBOL",keytype="ENSEMBL",multiVals="first")))
Error in listoflists[[i]] :
attempt to select less than one element in get1index
>convertedLoL <- lapply(listoflists, function(x) lapply(listoflists[x], function(i)mapIds(org.Hs.eg.db,keys=listoflists[[x]][[i]],column="SYMBOL",keytype="ENSEMBL",multiVals="first")))
Error in listoflists[[x]] : no such index at level 1
I keep getting errors. I think my issues stem from the fact that I don't fully comprehend how apply works and how to reference lists. Could someone help me?
EDIT
I thought I'd figured it out, but it still isn't quite right.
>convertedLoL <- lapply(listoflists, function(x) sapply(x, function(i)mapIds(org.Hs.eg.db,keys=i,column="SYMBOL",keytype="ENSEMBL",multiVals="first")))
will give me what might be a list of a list of a list. It's also REALLY slow. So I still need help...