# Load required libraries
source("https://bioconductor.org/biocLite.R")
biocLite("BgeeDB")
library(BgeeDB)
biocLite("biomaRt")
library(biomaRt)

# Connection to Biomart for mouse data in Ensembl release 87
mouse_mart <- useMart("ENSEMBL_MART_ENSEMBL", dataset="mmusculus_gene_ensembl",
    host="Dec2016.archive.ensembl.org");

# Retrieve mouse genes annotated to GO term GO:0060537 "muscle tissue development",
mouse_muscle_genes <- getBM(attributes=c("ensembl_gene_id"),
    filters="go_parent_term", values=c("GO:0060537"), mart=mouse_mart);
# Retrieve all mouse genes with any GO annotation for proper definition of gene universe
mouse_GO_genes <- getBM(attributes=c("ensembl_gene_id"),
    filters="with_go_id", values=c(T), mart=mouse_mart);

# Prepare the gene list vector
gene_list <- factor(as.integer(unique(mouse_GO_genes$ensembl_gene_id) %in%
    mouse_muscle_genes$ensembl_gene_id));
names(gene_list) <- unique(mouse_GO_genes$ensembl_gene_id);

# Load TopAnat object with mouse expression data in embryo
bgee_topanat <- Bgee$new(species="Mus_musculus");
topanat_data <- loadTopAnatData(bgee_topanat, stage="UBERON:0000068");
topanat_obj  <- topAnat(topanat_data, gene_list);
# Run test
mouse_embryo_results <- runTest(topanat_obj, algorithm='weight', statistic='fisher');
mouse_embryo_table_over <- makeTable(topanat_data, topanat_obj, mouse_embryo_results, cutoff=0.2);

# Load TopAnat object with mouse expression data in adult
bgee_topanat <- Bgee$new(species="Mus_musculus");
topanat_data <- loadTopAnatData(bgee_topanat, stage="UBERON:0000092");
topanat_obj  <- topAnat(topanat_data, gene_list);
# Run test
mouse_adult_results <- runTest(topanat_obj, algorithm='weight', statistic='fisher');
mouse_adult_table_over <- makeTable(topanat_data, topanat_obj, mouse_adult_results, cutoff=0.2);
