Jack Dean
Jack Dean

Reputation: 163

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)

Upvotes: 1

Views: 600

Answers (2)

Maurits Evers
Maurits Evers

Reputation: 50718

First off, you don't provide a reproducible, minimal example; people are much more likely to help if you provide self-contained minimal code, state what you've tried, what failed, and what the expected outcome is.

That aside, below is a minimal example based on the data you provide for genes.

# Load the necessary library
library(biomaRt);

# Your vector of query gene symbols
genes <- c("FGF1", "BRCA1", "FOXP2");

# The relevant BioMart database and dataset
mart <- useMart(
    biomart = "ENSEMBL_MART_ENSEMBL",
    dataset = "hsapiens_gene_ensembl");

# Extract attributes from mart for every entry in values
getBM(
    attributes = c("external_gene_name", "hsapiens_paralog_associated_gene_name"),
    filters = "external_gene_name",
    values =  genes,
    mart = mart);
#    external_gene_name hsapiens_paralog_associated_gene_name
# 1                FGF1                                  FGF2
# 2               BRCA1
# 3               FOXP2                                 FOXP4
# 4               FOXP2                                 FOXP1
# 5               FOXP2                                 FOXP3
# 6               FOXP2                                 FOXO4
# 7               FOXP2                                 FOXO6
# 8               FOXP2                                 FOXO1
# 9               FOXP2                                 FOXO3
# 10              FOXP2                                 FOXM1

Upvotes: 1

denis
denis

Reputation: 5673

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

Upvotes: 3

Related Questions