Create Resnik distances and save them as ftsv.
This commit is contained in:
parent
08b7d8dd85
commit
ae3aa7e4aa
|
@ -32,6 +32,7 @@ class GeneOntology(networkx.XDiGraph):
|
|||
def __init__(self):
|
||||
networkx.XDiGraph.__init__(self)
|
||||
self.by_id = {}
|
||||
self.undirected = None
|
||||
|
||||
def add_term(self, term):
|
||||
self.add_node(term)
|
||||
|
@ -62,12 +63,12 @@ class GeneOntology(networkx.XDiGraph):
|
|||
|
||||
def subsumer(self, t1, t2):
|
||||
if t1 == t2:
|
||||
# print "t1 == t2"
|
||||
return t1
|
||||
|
||||
go_undir = self.to_undirected()
|
||||
# print go_undir.nodes()
|
||||
path = networkx.shortest_path(go_undir, t1, t2)
|
||||
if self.undirected == None:
|
||||
self.undirected = self.to_undirected()
|
||||
|
||||
path = networkx.shortest_path(self.undirected, t1, t2)
|
||||
if not path:
|
||||
print "Woah, path not found."
|
||||
return None
|
||||
|
@ -76,12 +77,9 @@ class GeneOntology(networkx.XDiGraph):
|
|||
print "This shouldn't happen"
|
||||
return t1
|
||||
|
||||
# print t1['id'], t2['id'], path
|
||||
# print "path:", path
|
||||
for t in path:
|
||||
if networkx.shortest_path(self, t, t1) and \
|
||||
networkx.shortest_path(self, t, t2):
|
||||
# print " ", t1, t2, t
|
||||
return t
|
||||
|
||||
print "GeneOntology.subsumer: should not reach this point"
|
||||
|
|
|
@ -120,6 +120,7 @@ class GoWorkflow (workflow.Workflow):
|
|||
go = workflow.Stage('go', 'Gene Ontology')
|
||||
go.add_function(SelectGoTermsFunction(self))
|
||||
go.add_function(GoDistanceFunction())
|
||||
go.add_function(SaveDistancesFunction())
|
||||
self.add_stage(go)
|
||||
|
||||
|
||||
|
@ -277,7 +278,7 @@ class SelectGoTermsFunction(workflow.Function):
|
|||
self.wf = wf
|
||||
|
||||
def run(self):
|
||||
self.wf.project.set_selection('go-terms', set(['GO:0007582', 'GO:0008150', 'GO:0051704']))
|
||||
self.wf.project.set_selection('go-terms', set(['GO:0007582', 'GO:0008150', 'GO:0051704', 'GO:0044419']))
|
||||
|
||||
|
||||
class GoDistanceFunction(workflow.Function):
|
||||
|
@ -296,7 +297,7 @@ class GoDistanceFunction(workflow.Function):
|
|||
term2 = go.by_id[t2]
|
||||
subsumer = go.subsumer(term1, term2)
|
||||
print "%s - %s - %s" % (t1, subsumer['id'], t2)
|
||||
m[i, j] = ic[subsumer['id']] - ic[t1] + ic[subsumer['id']] - ic[t2]
|
||||
m[i, j] = ic[t1] + ic[t2] - 2.0 * ic[subsumer['id']]
|
||||
ds = dataset.Dataset(m, (('go-terms', ids), ('_go-terms', ids)), 'Resnik')
|
||||
return ds
|
||||
|
||||
|
@ -322,11 +323,10 @@ class GoDistanceFunction(workflow.Function):
|
|||
for term in reversed(networkx.topological_sort(go)):
|
||||
for parent in go.in_neighbors(term):
|
||||
annotations[parent['id']] += annotations[term['id']]
|
||||
# print "%s -> %s (%s)" % (term['id'], parent['id'], annotations[parent['id']])
|
||||
|
||||
# Create information content dictionary
|
||||
for i, v in enumerate(evidence.get_identifiers('go-terms')):
|
||||
ic[v] = -log(ann_count_matrix[i] / total_ann)
|
||||
for term, count in annotations.items():
|
||||
ic[term] = -log(count / total_ann)
|
||||
|
||||
return [self.resnik_distance_matrix(selection, ic)]
|
||||
|
||||
|
@ -343,6 +343,17 @@ class GoDistanceFunction(workflow.Function):
|
|||
return options
|
||||
|
||||
|
||||
class SaveDistancesFunction(workflow.Function):
|
||||
def __init__(self):
|
||||
workflow.Function.__init__(self, 'save-matrix', 'Save Matrix')
|
||||
|
||||
def run(self, ds):
|
||||
filename = '/home/einarr/data/output.ftsv'
|
||||
fd = open(filename, 'w')
|
||||
dataset.write_ftsv(fd, ds)
|
||||
fd.close()
|
||||
|
||||
|
||||
class Options(dict):
|
||||
def __init__(self):
|
||||
dict.__init__(self)
|
||||
|
|
Reference in New Issue