TheUndecided
TheUndecided

Reputation: 47

Excel: Ensembl continue, how to save to CSV file after change R

Related to Unable to use biomaRt package to get Gene Symbols from Entrez IDs

The code is working like a charm, but I would like to save it to my current CSV file and not only "viewing" it (adding an additional column will do the work as well, I got over 65k ids)

library(biomaRt)
marts <- listMarts()
ensembl <- useMart("ensembl")
datasets <- listDatasets(ensembl)
ensembl=useDataset("hsapiens_gene_ensembl",mart=ensembl)
attributes <- listAttributes(ensembl)
my_ids <- read.csv("LUADhtseq.csv")
ensembl = useEnsembl(biomart="ensembl", dataset="hsapiens_gene_ensembl")
results_end_1 <- getBM(attributes = c("ensembl_gene_id","external_gene_name"), values = my_ids, mart = ensembl )
View(results_end_1)

Upvotes: 0

Views: 162

Answers (1)

StupidWolf
StupidWolf

Reputation: 46978

You are getting back all the entries in ensembl, and you need to merge:

my_ids = data.frame(ids=c("ENSG00000130203","ENSG00000136717","ENSG00000186868"))

df = merge(my_ids,results_end_1,by.x="ids",by.y="ensembl_gene_id")
df
              ids external_gene_name
1 ENSG00000130203               APOE
2 ENSG00000136717               BIN1
3 ENSG00000186868               MAPT

Or you need to set the "filters" option to get the entries in my_ids, :

out <- getBM(attributes = c("ensembl_gene_id","external_gene_name"), 
values = my_ids, mart = ensembl,filters="ensembl_gene_id")

  ensembl_gene_id external_gene_name
1 ENSG00000130203               APOE
2 ENSG00000136717               BIN1
3 ENSG00000186868               MAPT

And you can write:

write.csv(out,"out.csv",row.names=FALSE,quote=FALSE)

Upvotes: 1

Related Questions