goatools.semantic module

Compute semantic similarities between GO terms. Borrowed from book chapter from Alex Warwick Vesztrocy and Christophe Dessimoz (thanks). For details, please check out: notebooks/semantic_similarity.ipynb

class goatools.semantic.TermCounts(go2obj, annots, relationships=None, **kws)[source]

Bases: object

TermCounts counts the term counts for each

get_annotations_reversed()[source]

Return go2geneset for all GO IDs explicitly annotated to a gene

get_count(go_id)[source]

Returns the count of that GO term observed in the annotations.

get_total_count(aspect)[source]

Gets the total count that’s been precomputed.

get_term_freq(go_id)[source]

Returns the frequency at which a particular GO term has been observed in the annotations.

get_gosubdag_all(prt=sys.stdout)[source]

Get GO DAG subset include descendants which are not included in the annotations

prt_objdesc(prt=sys.stdout)[source]

Print TermCount object description

goatools.semantic.get_info_content(go_id, termcounts)[source]

Retrieve the information content of a GO term.

goatools.semantic.resnik_sim(go_id1, go_id2, godag, termcounts)[source]

Computes Resnik’s similarity measure: the information content of the most informative common ancestor (MICA).

goatools.semantic.lin_sim(goid1, goid2, godag, termcnts, dfltval=None)[source]

Computes Lin’s similarity measure.

goatools.semantic.lin_sim_calc(goid1, goid2, sim_r, termcnts, dfltval=None)[source]

Computes Lin’s similarity measure using pre-calculated Resnik’s similarities.

goatools.semantic.get_freq_msca(go_id1, go_id2, godag, termcounts)[source]

Retrieve the frequency of the MSCA of two GO terms.

goatools.semantic.schlicker_sim(goid1, goid2, godag, termcnts, dfltval=None)[source]

Computes Schlicker’s similarity measure.

goatools.semantic.schlicker_sim_calc(goid1, goid2, sim_r, tfreq, termcnts, dfltval=None)[source]

Computes Schlicker’s similarity measure using pre-calculated Resnik’s similarities.

goatools.semantic.common_parent_go_ids(goids, godag)[source]

This function finds the common ancestors in the GO tree of the list of goids in the input.

goatools.semantic.deepest_common_ancestor(goterms, godag)[source]

This function gets the nearest common ancestor using the above function. Returns a single term; ties at the maximum depth are broken by GO ID. Use get_deepest_common_ancestors() to get all of them.

goatools.semantic.most_informative_common_ancestor(goids, godag, termcounts)[source]

Returns the most informative common ancestor (MICA): the common ancestor with the highest information content (Resnik 1995). This need not be the deepest one, since a deep term can be heavily annotated. Ties are broken by depth, then GO ID.

goatools.semantic.get_deepest_common_ancestors(goids, godag)[source]

Returns the set of all deepest (most recent) common ancestors for the given GO terms.

Unlike deepest_common_ancestor(), which returns a single term and may behave non-deterministically when multiple common ancestors share the same maximum depth, this function returns all of them.

Parameters:
  • goids (list of str) – List of GO IDs (e.g. [’GO:0048364’, ‘GO:0044707’]).

  • godag (GODag) – The GO DAG object (e.g. loaded via GODag(‘go-basic.obo’)).

Returns:

Set of GO IDs that are the deepest common ancestors of all input terms. Returns an empty set if no common ancestor exists.

Return type:

set of str

Example

>>> from goatools.obo_parser import GODag
>>> from goatools.semantic import get_deepest_common_ancestors
>>> godag = GODag('go-basic.obo')
>>> mrca_set = get_deepest_common_ancestors(['GO:0048364', 'GO:0044707'], godag)
goatools.semantic.min_branch_length(go_id1, go_id2, godag, branch_dist)[source]

Finds the minimum branch length between two terms in the GO DAG.

goatools.semantic.shortest_path(go_id1, go_id2, godag)[source]

Finds the shortest path length between two GO terms in the same namespace by performing a BFS upward through the DAG from each term and finding the minimum total distance through any common ancestor.

Unlike min_branch_length() / semantic_distance(), which compute distance using the depth attribute (longest path from root), this function uses a proper BFS traversal and therefore gives correct results in DAGs where a term is reachable via multiple paths of different lengths.

Parameters:
  • go_id1 (str) – First GO ID (e.g. ‘GO:0048364’).

  • go_id2 (str) – Second GO ID (e.g. ‘GO:0044707’).

  • godag (GODag) – The GO DAG object (e.g. loaded via GODag(‘go-basic.obo’)).

Returns:

The shortest path length (number of edges) between the two GO terms through their common ancestors. Returns None if the terms are in different namespaces (and therefore not connected in the DAG). Returns 0 if both GO IDs resolve to the same term.

Return type:

int or None

Example

>>> from goatools.obo_parser import GODag
>>> from goatools.semantic import shortest_path
>>> godag = GODag('go-basic.obo')
>>> dist = shortest_path('GO:0048364', 'GO:0044707', godag)
goatools.semantic.semantic_distance(go_id1, go_id2, godag, branch_dist=None)[source]

Finds the semantic distance (minimum number of connecting branches) between two GO terms.

goatools.semantic.semantic_similarity(go_id1, go_id2, godag, branch_dist=None)[source]

Finds the semantic similarity (inverse of the semantic distance) between two GO terms.