Skip to content

Commit

Permalink
Begin test for issue 256: How to get most recent common ancesters and…
Browse files Browse the repository at this point in the history
… shortest path distance between two terms?

#256
  • Loading branch information
dvklopfenstein committed Jan 4, 2023
1 parent 8e09829 commit a80acb2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 20 deletions.
36 changes: 21 additions & 15 deletions tests/i148_semsim_lin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,35 @@
"""Test for issue 148, Lin Similarity if a term has no annotations"""


import os
import sys
from os.path import join
from os.path import dirname
from os.path import abspath
from sys import stdout

from itertools import combinations_with_replacement as combo_w_rplc

from goatools.base import get_godag
from goatools.associations import dnld_annofile
from goatools.anno.gpad_reader import GpadReader
#### from goatools.semantic import semantic_similarity
from goatools.semantic import TermCounts
#### from goatools.semantic import get_info_content
#### from goatools.semantic import deepest_common_ancestor
from goatools.semantic import resnik_sim
#### from goatools.semantic import resnik_sim
from goatools.semantic import lin_sim
#### from goatools.godag.consts import NS2GO

REPO = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
REPO = join(dirname(abspath(__file__)), "..")

def test_i148_semsim_lin(prt=sys.stdout):
def test_i148_semsim_lin(prt=stdout):
"""Test for issue 148, Lin Similarity if a term has no annotations"""
fin_gpad = os.path.join(REPO, 'goa_human.gpad')
fin_gpad = join(REPO, 'goa_human.gpad')
dnld_annofile(fin_gpad, 'gpad')

godag = get_godag(os.path.join(REPO, "go-basic.obo"), loading_bar=None)
godag = get_godag(join(REPO, "go-basic.obo"), loading_bar=None)
annoobj = GpadReader(fin_gpad, godag=godag)

# Researcher-provided GO IDs
goids = [
'GO:0042581',
'GO:0101002',
Expand All @@ -40,19 +45,20 @@ def test_i148_semsim_lin(prt=sys.stdout):

# Calculate Lin values
p2v = {frozenset([a, b]): lin_sim(a, b, godag, termcounts) for a, b in combo_w_rplc(goids, 2)}
_prt_values(goids, p2v, prt=sys.stdout)
_prt_lins_similarity(goids, p2v, prt)


def _prt_values(goids, p2v, prt=sys.stdout):
"""Print values"""
prt.write(' {HDR}\n'.format(HDR=' '.join(goids)))
def _prt_lins_similarity(goids, p2v, prt):
"""Print Lin's semantic similarity between all researcher provided GO IDs (goids)"""
prt.write("\nLin's semantic similarity between researcher-provided GO IDs:\n")
prt.write(f' {" ".join(goids)}\n')
none = 'None '
for go_row in goids:
prt.write('{GO} '.format(GO=go_row))
prt.write(f'{go_row} ')
for go_col in goids:
val = p2v[frozenset([go_row, go_col])]
txt = '{L:<9.6} '.format(L=val) if val is not None else none
prt.write('{T:10} '.format(T=txt))
lins_val = p2v[frozenset([go_row, go_col])]
lins_str = f'{lins_val:<9.6} ' if lins_val is not None else none
prt.write(f'{lins_str:10} ')
prt.write('\n')


Expand Down
57 changes: 57 additions & 0 deletions tests/test_i256_mca_dist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python
"""Test Yang's RWC measure added to other semantic similariy measures"""


from os.path import join
from os.path import dirname
from os.path import abspath
from os.path import exists
import collections as cx
from goatools.obo_parser import GODag
from goatools.utils import get_b2aset
from goatools.anno.idtogos_reader import IdToGosReader
from goatools.semantic import TermCounts


REPO = join(dirname(abspath(__file__)), "..")

def test_semantic_similarity():
"""Test faster version of sematic similarity"""
godag = GODag(join(REPO, 'tests/data/yangRWC/fig1b.obo'))
name2go = {o.name: o.item_id for o in godag.values()}
assoc = _get_id2gos(join(REPO, 'tests/data/yangRWC/fig1b.anno'), godag, name2go)
tcntobj = TermCounts(godag, assoc)
assert tcntobj.gocnts[name2go['I']] == 20
assert tcntobj.gocnts[name2go['L']] == 21
assert tcntobj.gocnts[name2go['M']] == 20
assert tcntobj.gocnts[name2go['N']] == 20

def _get_id2gos(file_id2gos, godag, name2go):
"""Get annotations"""
if exists(file_id2gos):
return IdToGosReader(file_id2gos, godag=godag).get_id2gos('CC')
id2num = {
name2go['A']: 1,
name2go['B']: 1,
name2go['C']: 10,
name2go['D']: 10,
name2go['E']: 10,
name2go['F']: 10,
name2go['G']: 10,
name2go['H']: 10,
name2go['I']: 18,
}
go2genes = cx.defaultdict(set)
genenum = 0
for goid, qty in id2num.items():
for _ in range(qty):
go2genes[goid].add(genenum)
genenum += 1
id2gos = get_b2aset(go2genes)
IdToGosReader.wr_id2gos(file_id2gos, id2gos)
return id2gos



if __name__ == '__main__':
test_semantic_similarity()
13 changes: 8 additions & 5 deletions tests/test_yang_fig1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
"""Test Yang's RWC measure added to other semantic similariy measures"""


import os
from os.path import join
from os.path import dirname
from os.path import abspath
from os.path import exists
import collections as cx
from goatools.obo_parser import GODag
from goatools.utils import get_b2aset
from goatools.anno.idtogos_reader import IdToGosReader
from goatools.semantic import TermCounts


REPO = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
REPO = join(dirname(abspath(__file__)), "..")

def test_semantic_similarity():
"""Test faster version of sematic similarity"""
godag = GODag(os.path.join(REPO, 'tests/data/yangRWC/fig1b.obo'))
godag = GODag(join(REPO, 'tests/data/yangRWC/fig1b.obo'))
name2go = {o.name: o.item_id for o in godag.values()}
assoc = _get_id2gos(os.path.join(REPO, 'tests/data/yangRWC/fig1b.anno'), godag, name2go)
assoc = _get_id2gos(join(REPO, 'tests/data/yangRWC/fig1b.anno'), godag, name2go)
tcntobj = TermCounts(godag, assoc)
assert tcntobj.gocnts[name2go['I']] == 20
assert tcntobj.gocnts[name2go['L']] == 21
Expand All @@ -25,7 +28,7 @@ def test_semantic_similarity():

def _get_id2gos(file_id2gos, godag, name2go):
"""Get annotations"""
if os.path.exists(file_id2gos):
if exists(file_id2gos):
return IdToGosReader(file_id2gos, godag=godag).get_id2gos('CC')
id2num = {
name2go['A']: 1,
Expand Down

0 comments on commit a80acb2

Please sign in to comment.