`getFunctionalCategories` <-
function(functionalCategories, species=c("Hs", "Rn", "Mm")) {
	if(missing(species)|is.null(species)) {
		species <- "Hs"
		warning("Argument `species` should be one of `Hs`, `Mm`, `Rn`). Using the default `Hs`.")
	}
	species <- tryCatch(match.arg(species), 
		error = function(e) {warning("Argument `species` should be one of `Hs`, `Mm`, `Rn`). Using the default `Hs`.");"Hs"})
	require(AnnotationDbi)
    fCategories <- list()
    for(i in 1:length(functionalCategories)) {
        if(toupper(functionalCategories[i]) == "GO") {
            require("GO.db", character.only = TRUE)
            require(paste("org", species, "eg.db", sep = "."), character.only = TRUE)
            GOID2GeneID <- as.list(get(paste("org", species, "egGO2ALLEGS", sep = ".")))
            k <- keys(get("GOTERM"))
            l <- toTable(get("GOTERM"))
            GOID2Desc <- cbind(GOID = k, GOTerm = l[match(k, l[,1]), "Term"])
            fCategories <- c(fCategories, list(GO = list(GOID2GeneID, GOID2Desc)))
        } else if(toupper(functionalCategories[i]) == "KEGG") {
            require("KEGG.db", character.only = TRUE)
            KEGGID2GeneID <- as.list(get("KEGGPATHID2EXTID"))
            spec <- unique(substr(names(KEGGID2GeneID), 1, 3))
            l <- as.list(get("KEGGPATHID2NAME"))
            KEGGID2Desc <- cbind(ID = paste(rep(spec, each = length(l)), rep(names(l), times = length(spec)), sep = ""),
                Name = rep(unlist(l), times = length(spec)))
            fCategories <- c(fCategories, list(KEGG = list(KEGGID2GeneID, KEGGID2Desc)))
        } else {
			require(paste("CLEAN", species, sep = "."), character.only = TRUE)
            e1 <- new.env()
            data(list = paste(functionalCategories[i], species, sep = "."), envir = e1)
            if(length(ls(e1)) > 0) {
                fCategories <- c(fCategories, list(get(paste(functionalCategories[i], species, sep = "."), envir = e1)))
                names(fCategories)[length(fCategories)] <- functionalCategories[i]
            }
        }
    }
    fCategories
}

