forked from brsynth/RetroPathRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathway_scoring.py
168 lines (145 loc) · 5.45 KB
/
pathway_scoring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
Defines the pathway scoring functions.
Can take as inputs both Pathway objects and json dictionnaries exported from Pathways.
"""
import random
import numpy as np
import json
import os
# RP3 - specific objects
from pathway import Pathway
def geo_mean(iterable):
a = np.array(iterable)
return a.prod()**(1.0/len(a))
# def geo_mean_overflow(iterable):
# a = np.log(iterable)
# return np.exp(a.sum()/len(a))
class PathwayScoring(object):
"""
Defines Pathway Scorer object.
"""
def __init__(self, scoring_function = None, scoring_json_function = None):
if scoring_function is None:
pass
else:
self.scoring_function = scoring_function
if scoring_json_function is None:
pass
else:
self.scoring_json_function = scoring_json_function
def __repr__(self):
"""
Name the used scorer.
Raises an error is the class is not properly instantiated
"""
return(self.name)
def calculate(self, pathway):
score = self.scoring_function(pathway)
return(score)
def calculate_json(self, pathway):
score = self.scoring_json_function(pathway)
return(score)
def pseudo_random(pathway):
score = random.uniform(0, 10)
return(score)
class ConstantPathwayScoring(PathwayScoring):
"""
Returns a constant reward, whichever the pathway.
"""
def __init__(self, reward = 10):
PathwayScoring.__init__(self)
self.reward = reward
self.scoring_function = self.scoring_function()
self.scoring_json_function = self.scoring_json_function()
self.name = "ConstantPathwayScoring of {}".format(reward)
def set_reward(self,reward):
# For changing the reward of the object
self.reward = reward
self.scoring_function = self.scoring_function()
self.scoring_json_function = self.scoring_json_function()
def scoring_function(self):
def pathway_scoring(pathway):
return(self.reward)
return(pathway_scoring)
def scoring_json_function(self):
def pathway_scoring(pathway):
return(self.reward)
return(pathway_scoring)
class BiologicalPathwayScoring(PathwayScoring):
"""
Returns the geometric mean of biological scores in the Pathway.
"""
def __init__(self):
PathwayScoring.__init__(self)
self.scoring_function = self.scoring_function()
self.scoring_json_function = self.scoring_json_function()
self.name = "BiologicalPathwayScoring"
def scoring_function(self):
def pathway_scoring(pathway):
scores = []
for move in pathway.nodes_transformations:
scores.append(move["data"]["Score"])
return(geo_mean(scores))
return(pathway_scoring)
def scoring_json_function(self):
def pathway_scoring(pathway):
scores = []
for move in pathway["elements"]["nodes"]:
if move["data"]["type"] == "reaction":
scores.append(move["data"]["Score"])
return(geo_mean(scores))
return(pathway_scoring)
class ChemicalPathwayScoring(PathwayScoring):
"""
Returns the geometric mean of chemical scores in the Pathway.
"""
def __init__(self):
PathwayScoring.__init__(self)
self.scoring_function = self.scoring_function()
self.scoring_json_function = self.scoring_json_function()
self.name = "ChemicalPathwayScoring"
def scoring_function(self):
def pathway_scoring(pathway):
scores = []
for move in pathway.nodes_transformations:
scores.append(move["data"]["ChemicalScore"])
return(geo_mean(scores))
return(pathway_scoring)
def scoring_json_function(self):
def pathway_scoring(pathway):
scores = []
for move in pathway["elements"]["nodes"]:
if move["data"]["type"] == "reaction":
scores.append(move["data"]["ChemicalScore"])
return(geo_mean(scores))
return(pathway_scoring)
class BiochemicalPathwayScoring(PathwayScoring):
"""
Returns the geometric mean of biochemical scores in the Pathway.
"""
def __init__(self):
PathwayScoring.__init__(self)
self.scoring_function = self.scoring_function()
self.scoring_json_function = self.scoring_json_function()
self.name = "ChemicalPathwayScoring"
def scoring_function(self):
def pathway_scoring(pathway):
scores = []
for move in pathway.nodes_transformations:
scores.append(move["data"]["ChemicalScore"] * move["data"]["Score"])
return(geo_mean(scores))
return(pathway_scoring)
def scoring_json_function(self):
def pathway_scoring(pathway):
scores = []
for move in pathway["elements"]["nodes"]:
if move["data"]["type"] == "reaction":
scores.append(move["data"]["Score"] * move["data"]["ChemicalScore"])
return(geo_mean(scores))
return(pathway_scoring)
RandomPathwayScorer = PathwayScoring(scoring_function = pseudo_random)
constant_pathway_scoring = ConstantPathwayScoring(reward = 10)
null_pathway_scoring = ConstantPathwayScoring(reward = 0)
biological_pathway_scoring = BiologicalPathwayScoring()
chemical_pathway_scoring = ChemicalPathwayScoring()
biochemical_pathway_scoring = BiochemicalPathwayScoring()