-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First pass at semantic similarity #49
Open
cmungall
wants to merge
4
commits into
master
Choose a base branch
from
sim
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import math | ||
import pandas as pd | ||
|
||
class SimEngine(): | ||
|
||
def __init__(self, | ||
association_set=None, | ||
icmap=None): | ||
self.association_set = association_set | ||
self.icmap = icmap | ||
|
||
def _get_icmap(self): | ||
if self.icmap is None: | ||
icmap = {} | ||
aset = self.association_set | ||
num_subjs = len(asset.subjects) | ||
for n in aset.ontology.nodes(): | ||
num_anns = len(aset.query([n])) | ||
freq = num_anns / num_subjs | ||
ic = None | ||
if freq > 0: | ||
ic = -math.log(freq/num_subjs) / math.log(2) | ||
icmap[n] = ic | ||
self.icmap = icmap | ||
return self.icmap | ||
|
||
def information_content(self,nid): | ||
""" | ||
Returns information content for a node | ||
""" | ||
icmap = self._get_icmap() | ||
return icmap[nid] | ||
|
||
def entity_jaccard_similarity(self,s1,s2): | ||
""" | ||
Calculate jaccard index of inferred associations of two subjects | ||
|
||
|ancs(s1) /\ ancs(s2)| | ||
--- | ||
|ancs(s1) \/ ancs(s2)| | ||
|
||
""" | ||
a1 = self.association_set.inferred_types(s1) | ||
a2 = self.association_set.inferred_types(s2) | ||
num_union = len(a1.union(a2)) | ||
if num_union == 0: | ||
return 0.0 | ||
return len(a1.intersection(a2)) / num_union | ||
|
||
def class_jaccard_similarity(self,c1,c2): | ||
""" | ||
Calculate jaccard index of two classes | ||
|
||
|ancs(c1) /\ ancs(c2)| | ||
--- | ||
|ancs(c1) \/ ancs(c2)| | ||
|
||
""" | ||
ont = self.association_set.ontology | ||
a1 = ont.ancestors(c1,reflexive=True) | ||
a2 = ont.ancestors(c2,reflexive=True) | ||
num_union = len(a1.union(a2)) | ||
if num_union == 0: | ||
return 0.0 | ||
return len(a1.intersection(a2)) / num_union | ||
|
||
def class_resnik_similarity(self,c1,c2): | ||
""" | ||
Calculate resnik similarty of two classes | ||
|
||
Return | ||
------ | ||
(number,list) | ||
tuple of max_ic and list of MRCAs | ||
""" | ||
cas = self.common_ancestors(c1,c2) | ||
pairs = [(a, self.information_content(a)) for a in cas] | ||
max_ic = 0 | ||
mrcas = [] | ||
for a,ic in pairs: | ||
if ic > max_ic: | ||
max_ic = ic | ||
mrcas = [a] | ||
elif ic == max_ic: | ||
mrcas.append(a) | ||
return max_ic, mrcas | ||
|
||
def used_classes(self): | ||
aset = self.association_set | ||
cset = set() | ||
for s in aset.subjects: | ||
cset.update(aset.inferred_types(s)) | ||
return cset | ||
|
||
def dataframe(self): | ||
aset = self.association_set | ||
entries = [] | ||
subjs = aset.subjects | ||
for s in subjs: | ||
vmap = {} | ||
for c in aset.inferred_types(s): | ||
vmap[c] = 1 | ||
entries.append(vmap) | ||
df = pd.DataFrame(entries, index=subjs) | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from ontobio.ontol_factory import OntologyFactory | ||
from ontobio.assoc_factory import AssociationSetFactory | ||
from ontobio.assocmodel import AssociationSet | ||
from ontobio.io.gafparser import GafParser | ||
from ontobio.sim import SimEngine | ||
import logging | ||
import random | ||
|
||
|
||
|
||
POMBASE = "tests/resources/truncated-pombase.gaf" | ||
INTRACELLULAR='GO:0005622' | ||
G1 = 'PomBase:SPBC902.04' | ||
def test_sim(): | ||
""" | ||
Test loading from gaf | ||
""" | ||
ofactory = OntologyFactory() | ||
afactory = AssociationSetFactory() | ||
ont = ofactory.create('tests/resources/go-truncated-pombase.json') | ||
aset = afactory.create_from_gaf(open(POMBASE,"r"), | ||
ontology=ont) | ||
|
||
sim = SimEngine(aset) | ||
for g1 in aset.subjects: | ||
print("G1={} '{}'".format(g1, aset.label(g1))) | ||
for g2 in aset.subjects: | ||
print(" G2={} '{}'".format(g2, aset.label(g2))) | ||
jsim = sim.entity_jaccard_similarity(g1,g2) | ||
print(" SIM={}".format(jsim)) | ||
|
||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like this should just be a method on whatever is returned from
self.definition
. So if you want the string version, you would just saystr(definition(id))
instead of providing a separate method here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the most common operation is getting the value; but in any case this will be dropped as I accidentlly reimplemented this code on a separate PR that's already been merged, doh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh it's merged already? Where at? I was thinking we would release after this PR to get my logging changes in and this.
Oh, I just saw that it's just a dictionary that's being returned and not a real Object. I guess my suggestion doesn't make as much sense then.