1

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)
Jack Dean
  • 163
  • 1
  • 7

2 Answers2

3

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

denis
  • 5,580
  • 1
  • 13
  • 40
  • Thanks that has done the trick, I just need to find a way to merge these separate lists into a single table. Sorry I am new to using R so there are many things I still need to learn – Jack Dean Jan 28 '18 at 21:42
  • @denis `getBM` is already vectorised in that it accepts a vector of character strings as `values`. So in this case there is no need for `lapply`; just use `getBM(..., values = genes, ...)`. – Maurits Evers Jan 29 '18 at 06:58
  • @JackDean to merge the list, or use `sapply` instead of lapply, or if the output is a dataframe, you can rbind or something similar – denis Jan 29 '18 at 07:43
  • @MauritsEvers I didn't even know if the function was home made or not, so I proposed the simpliest solution that would work in any case given the information in the question (the function works for one gene). But indeed if the funciton is vectorised, it is event simpler – denis Jan 29 '18 at 07:46
  • No worries @denis. This wasn't meant as a criticism, merely as an addition; `getBM` uses the BioMart API to query and extract gene information, so using the vectorised version will definitely be faster. – Maurits Evers Jan 29 '18 at 11:40
1

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
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • My apologies for not making myself clearer, I am new to using the forum and will provide more info if I post again, thanks for the advice. – Jack Dean Jan 28 '18 at 21:39
  • No problem @JackDean; you can close a question by accepting an answer (setting the tick mark) if this solves your problem. Welcome to SO! – Maurits Evers Jan 28 '18 at 21:43