Package: obogo
obogo: yet another GeneOntology reader/crawler !!!installationpip install obogoprerequistesYou will need to grab the latest Gene Ontology description in.oboformat. Download it from theofficial GO site.
This is a reasonable format, where each GO term is encoded as a paragraph of key:value lines:[Term]
id: GO:0000001
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton." [GOC:mcc, PMID:10873824, PMID:11389764]
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distributionObvisously,is_ais the main relationship of interest to build the DAG of GO terms. Butconsider,replaced_byandalias_toproperties are also handled by this package.Build the GO DAG structureSimply read from a flat.obofilefromobogoimportcreate_tree_from_oboobogo_tree=create_tree_from_obo('../data/go-basic.obo')You can query a go term by a name or its GO identifierobogo_tree.view_go_node('GO:1903507')obogo_tree.view_go_node('biological process')Build a protein collectionIn order to set the background population of proteins for each GO term, you will need to build a collection of uniprot data containers. In obogo, these are calledUniprotDatumand can be directly created from auniprot proteome xml reference file(eg:E.coli K12).
Supposed we downloaded the above mentioned E.Coli K12 proteome xml file nameduniprotkb_proteome_UP000000625.xml, the collection of uniprot containers can be buildt this way:fromuniprot_redis.store.mockupimportUniprotStoreDummyfromuniprot_redis.wrapperimportCollector#populate storestore=UniprotStoreDummy()load_data=store.load_uniprot_xml(file="path/to/uniprotkb_proteome_UP000000625.xml")store.save_collection('ecoli_K12',load_data)#retrieve collectionmy_collection=Collector(store,'ecoli_K12')This collection is iterable, slice-able or get-able via uniprot Accession numbers.print(my_collection['P19636'])foruniprot_datuminmy_collection:print(uniprot_datum.id,uniprot_datum.go)Assign whole proteome to the GO structureEach protein of the collection now has to be attached to the GO terms that are described in itsUniprotDatumgofield (see above).obogo_tree.load_proteins('background',my_collection)Theobogo_treerepresents each GO term as a straight newtworkx node. Theload_proteinscall will set the  for each node the value of their 'background' key to a list of UniprotDatum.In most ORA analysis the population of proteins attached to a given node is the union of the proteins attached to its descendant ("GO annotation goes up": "any specific GO term implicitly carries the meaning of a less specific"). Hence, an additional operation is required to propagate protein populations up the tree.obogo_tree.percolate(percol_type="background")This currently takes around 2mn for E.Coli proteome.NB: At this stage, serializing the obogo_tree could be handyimportpicklepik_fpath="obogo_ecoliK12.pik"withopen(pik_fpath,"wb")asfp:pickle.dump(obogo_tree,fp)Load the experimental protein setFor this tutorial, we will create a dummy collection of experimental proteins based on a slice of 1200 protein from the proteome and load it into obogo_tree. Note that this time, it is loaded using the'measured'argument. Then, we also propagate this additional protein population up the tree.obo_tree.load_proteins('measured',my_collection[1000:2200])obo_tree.percolate(percol_type='measured')Define sample protein setWe now define a subset ofmeasuredproteins as "of interest" (aka: over-abundant).sample=my_collection[1100:1180]Compute ORA of the GO terms within the sampleFor a particular GO termfromobogo.statisticsimportcompute_node_oraprint(compute_node_ora(obo_tree,sample,'GO:0006811'))print(compute_node_ora(obo_tree,sample,'metal ion transport'))print(compute_node_ora(obo_tree,sample,'GO:0006811',norm='measured'))The sample parameter can also be a straight Uniprot AC iterator (eg:['P02930', 'P03819', 'P0A910']).
The `norm`` parameter controls the reference population for the Fisher statistic:'background' : the whole proteome (default)'measured'   : the proteins of the experimentThe returned value is a tuple of the form:(GO_identfier, GO_name, total_sample protein carrying the GO term, Fisher test log_odd, Fisher test pvalue, contingency table)A similar operation can be applied to the entiere tree, where a generator ofscoretuples will be returnedfromobogo.statisticsimportscore_ora_treeforgo_scoreinscore_ora_tree(obo_tree,sample):print(go_score)