Hello, I want to perform the Biomart request on several files in the same folder (which works). But I would like the output of the request for each file to be saved either: 1 file to its own output file (1 request = 1 file) or the output of requests for 500 files is in the same file (500 files = 1 output file with accumulation of the output of each request). Here is the code used but in the final file, only the last request is saved.
library(biomaRt)
files<-list.files(path = "/Users/amandinelecerfdefer/Desktop/poi/data/", pattern = (".txt$"))
files
myList2 <- list()
for (k in 1:length(files)) {
setwd("/Users/amandinelecerfdefer/Desktop/poi/data/")
myList2[[k]] <- read.delim(files[k])
snpmart <-
useMart(biomart = "ENSEMBL_MART_SNP", dataset="hsapiens_snp")
res <- getBM(
attributes = c(
"refsnp_id",
"ensembl_gene_stable_id",
"ensembl_transcript_stable_id"
),
filters = "snp_filter",
values = myList2[[k]]$rsID,
mart = snpmart,
uniqueRows = TRUE
)
setwd("/Users/amandinelecerfdefer/Desktop/poi/result/")
write.csv(res[[k]], file = "recovery_gene_trans.txt")
or
for(k in 1:length(files)){
setwd("/Users/amandinelecerfdefer/Desktop/poi/result/")
write.csv(res[[k]], file = "recovery_gene_trans.txt")
}
}
Always the same issue
How to do this?
Thank you for your answer. Excuse me, I made a mistake, there is no i in my code, it's a bad habit, it's a k instead of the i.
unfortunately, I just tried your proposals, which unfortunately don't work.
edit : I answer here because the site doesn't want me to comment on your answer: I want to retrieve the total output of each request and not just one item to be returned by BioMart
Which one? Does it give you an error message or it doesn't merge them properly?
The main problem I see is that you're getting
res
from biomart. So callingres[[k]]
doesn't seem to make sense since biomart doesn't know that you havek
files, that's why I assumed you were usingi
inres[[i]]
to access a specific element of the biomart output.Check if you want the whole
res
list or a specific element of it, but it seems unlikely that you'll want elementk
for each iteration.