Search code examples
rloopsbioinformaticsbiomart

Run a loop using values from a list in R


I am new to R programming and I am using the BioMart package to extract gene paralogues for a list of genes.

Using the 'genes' vector below is it possible to loop each value individually into the values part of the 'getBM' function then add the output of this into a data frame?

genes <- C("FGF1", "BRCA1", "FOXP2")

getBM(attributes = c("external_gene_name", "hsapiens_paralog_associated_gene_name"), 
                                  filters = "external_gene_name", 
                                  values =  , mart = ensembl_hsapiens)

Below is how I've been doing it, using the genes vector as the values but it gives me the wrong numbers which I know the reason as to why. When I try the genes individually the values are correct which is why I want to loop these values instead.

 genes <- C("FGF1", "BRCA1", "FOXP2")

getBM(attributes = c("external_gene_name", "hsapiens_paralog_associated_gene_name"), 
                                  filters = "external_gene_name", 
                                  values = c(genes), mart = ensembl_hsapiens)

Solution

  • try something like

    result <- lapply(genes,function(x){getBM(attributes = c("external_gene_name", "hsapiens_paralog_associated_gene_name"), 
                                      filters = "ensembl_gene_id", 
                                      values = x, mart = ensembl_hsapiens)})
    

    to loop over your vector of gene. Result will be a list of the result for each value