Whilst a little late, I've been looking for something similar and found a way to do it in R. This is an adaptation from the code I found on the bioconductor support forums. This script gets all children of a given GO term, but it wouldn't be difficult to adapt to get ancestral terms instead.
To get all GO terms at level 4 that are child terms to the second level term GO:0002376 (Immune System Process), you would execute this;
library(GO.db)
library(org.Dr.eg.db) ## zebrafish annotation package from bioconductor
getAllBPChildren <- function(goids)
{
ans <- unique(unlist(mget(goids, GOBPCHILDREN), use.names=FALSE))
ans <- ans[!is.na(ans)]
}
level3_terms <- getAllBPChildren("GO:0002376")
level4_terms <- getAllBPChildren(level3_terms)
level4_genes <- mget(intersect(level4_terms, keys(org.Dr.eg.db)), org.Dr.eg.db)
Additionally, I'll add this because levels of GO terms can be misleading. Due to the nature of GO terms, "levels" can be fluid, where one term is a child to a level 3 and a level 4 terms, for example. So, to check the number of terms in both level 3 and 4, you can run this;
length(intersect(level3_terms, level4_terms))
If you wanted to get Molecular Function, just change the BP to MF.
Hi all,
Thank you, Jean-Karim Heriche and Pierre Lindenbaum, for all the answers...
I have tried the solutions you guys told me to. But unfortunatelly I haven't been able to figure it out those solutions (I'm a beginner in programming). On the other hand I have found the API page in quickGO website (https://www.ebi.ac.uk/QuickGO/api/index.html#!/gene_ontology/findTermsCoreAttrUsingGET_1). I'm using jupyter notebook and python to retrieve the ancestor GO that I am interested (instead of through an GO term, I decided to use a GO_ID) from a specific GO_ID.
from a code like this:
I got this response:
I wonder now how in python to access the last item of the ancestor key (GO:0032501) through a code. It seems that the reponse generates a unicode output instead of dictionary type. But I'm thinking creating a function, so I can find all the ancestors from my input list of GO_IDs. I've tried some things like make the unicode into a python dictionary, but I still can't access the 'ancestor' key.