-
Notifications
You must be signed in to change notification settings - Fork 1
/
RevigoPythonExample.py
373 lines (298 loc) · 11.8 KB
/
RevigoPythonExample.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# An example of using a REVIGO core library for your own projects.
# To Run this example you need RevigoCore library and a
# set of database files available at: http://revigo.irb.hr/RevigoDatabases.zip
#
# Authors:
# Rajko Horvat (https://github.com/rajko-horvat)
#
# License:
# MIT License
# Copyright(c) 2011-2023 Ruðer Boškoviæ Institute
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import time
import math
from clr_loader import get_coreclr
from pythonnet import set_runtime
set_runtime(get_coreclr(runtime_config="PythonRuntimeConfig.json"))
import clr
clr.AddReference("RevigoCore")
from IRB.Revigo.Core.Worker import RevigoWorker, ValueTypeEnum, RequestSourceEnum
from IRB.Revigo.Core import SemanticSimilarityTypeEnum, RevigoTerm, RevigoTermCollection, Utilities
from IRB.Revigo.Core.Databases import GeneOntology, SpeciesAnnotationList
clr.AddReference("mscorlib")
from System import TimeSpan
from System.IO import StreamWriter
def main():
dCutoff = 0.7
eValueType = ValueTypeEnum.PValue
iSpeciesTaxon = 0
eMeasure = SemanticSimilarityTypeEnum.SIMREL
bRemoveObsolete = True
print("Loading Ontology")
oOntology = GeneOntology.Deserialize("C:\\Revigo\\Databases\\Current\\GeneOntology.xml.gz")
print("Loading Species Annotations")
oAnnotations = SpeciesAnnotationList.Deserialize("C:\\Revigo\\Databases\\Current\\SpeciesAnnotations.xml.gz")
sExample1 = None
sExample2 = None
sExample3 = None
with open('Example1.csv', 'r') as file:
sExample1 = file.read()
with open('Example2.csv', 'r') as file:
sExample2 = file.read()
with open('Example3.csv', 'r') as file:
sExample3 = file.read()
# Create worker 1
oWorker1 = RevigoWorker(
# JobID
1,
# Ontology
oOntology,
# Annotations for a given dataset
oAnnotations.GetByID(iSpeciesTaxon),
# Timeout
TimeSpan(0, 15, 0),
# Job source
RequestSourceEnum.JobSubmitting,
# Dataset
sExample1,
# Job parameters
dCutoff, eValueType, eMeasure, bRemoveObsolete)
# Create worker 2
oWorker2 = RevigoWorker(2, oOntology, oAnnotations.GetByID(9606), TimeSpan(0, 15, 0), RequestSourceEnum.JobSubmitting,
sExample2, 0.9, eValueType, SemanticSimilarityTypeEnum.LIN, bRemoveObsolete);
# Create worker 3
oWorker3 = RevigoWorker(3, oOntology, oAnnotations.GetByID(iSpeciesTaxon), TimeSpan(0, 15, 0), RequestSourceEnum.JobSubmitting,
sExample3, 0.4, eValueType, eMeasure, bRemoveObsolete);
# Workers will notify when the are finished processing the data
oWorker1.OnFinish += OWorker_OnFinish
oWorker2.OnFinish += OWorker_OnFinish
oWorker3.OnFinish += OWorker_OnFinish
# Start Workers and wait for their completion
# They will automatically be assigned to different CPU core, if available
print("Starting Workers...")
oWorker1.Start()
oWorker2.Start()
oWorker3.Start()
while oWorker1.IsFinished != True or oWorker2.IsFinished != True or oWorker3.IsFinished != True:
time.sleep(0.1)
print("All Workers have finished processing.")
# export our results
print("Exporting data.")
ExportTable(oOntology, oWorker1, oWorker1.BPVisualizer, "..\\Example1_BPTable.tsv")
ExportScatterplot(oOntology, oWorker2, oWorker2.CCVisualizer, "..\\Example2_CCScatterplot.tsv")
ExportTreeMap(oOntology, oWorker3, oWorker3.MFVisualizer, "..\\Example3_MFTreeMap.tsv")
ExportCytoscapeXGMML(oWorker1.BPVisualizer, "..\\Example1_BPCytoscape.xgmml")
ExportSimMat(oWorker1.BPVisualizer, "..\\Example1_BPSimilarityMatrix.tsv")
ExportWordClouds(oWorker1, "..\\Example1_WordClouds.json")
# We are finished
def ExportTable(ontology, worker, visualizer, fileName):
if visualizer.IsEmpty != True:
with open(fileName, 'w') as oWriter:
oTerms = visualizer.Terms.FindClustersAndSortByThem(ontology, worker.CutOff)
oWriter.write("TermID\tName\tValue\t")
c = 1
while c < worker.MinNumColsPerGoTerm:
oWriter.write("UserValue_{0}\t", c - 1)
c += 1
oWriter.write("LogSize\tFrequency\tUniqueness\tDispensability\tRepresentative\n")
# print the data
i = 0
while i < oTerms.Count:
term = oTerms[i]
oWriter.write("\"{}\"\t".format(term.GOTerm.FormattedID))
oWriter.write("\"{}\"\t".format(term.GOTerm.Name))
oWriter.write("{}\t".format(term.Value))
c = 1
while c < worker.MinNumColsPerGoTerm:
oWriter.write("{}\t".format(term.UserValues[c - 1]))
c += 1
oWriter.write("{}\t".format(term.LogAnnotationSize))
oWriter.write("{}\t".format(term.AnnotationFrequency * 100.0))
oWriter.write("{}\t".format(term.Uniqueness))
oWriter.write("{}\t".format(term.Dispensability))
if term.RepresentativeID > 0:
oWriter.write("{}".format(term.RepresentativeID));
else:
oWriter.write("null");
oWriter.write("\n")
i += 1
def ExportScatterplot(ontology, worker, visualizer, fileName):
if visualizer.IsEmpty != True:
with open(fileName, 'w') as oWriter:
oTerms = visualizer.Terms.FindClustersAndSortByThem(ontology, worker.CutOff)
oWriter.write("TermID\tName\tValue\tLogSize\tFrequency\tUniqueness\tDispensability\tPC_0\tPC_1\tRepresentative\n")
# print the data
i = 0
while i < oTerms.Count:
term = oTerms[i]
oWriter.write("\"{}\"\t".format(term.GOTerm.FormattedID))
oWriter.write("\"{}\"\t".format(term.GOTerm.Name))
oWriter.write("{}\t".format(term.Value))
oWriter.write("{}\t".format(term.LogAnnotationSize))
oWriter.write("{}\t".format(term.AnnotationFrequency * 100.0))
oWriter.write("{}\t".format(term.Uniqueness))
oWriter.write("{}\t".format(term.Dispensability))
# 2D
oWriter.write("{}\t".format(term.PC[0] if (term.PC.Count > 0) else "null"))
oWriter.write("{}\t".format(term.PC[1] if (term.PC.Count > 1) else "null"))
oWriter.write("{}".format(term.RepresentativeID if (term.RepresentativeID > 0) else "null"))
oWriter.write("\n")
i += 1
def ExportTreeMap(ontology, worker, visualizer, fileName):
if visualizer.IsEmpty != True:
with open(fileName, 'w') as oWriter:
terms = visualizer.Terms.FindClustersAndSortByThem(ontology, 0.1)
oWriter.write("# WARNING - This exported Revigo data is only useful for the specific purpose of constructing a TreeMap visualization.\n")
oWriter.write("# Do not use this table as a general list of non-redundant GO categories, as it sets an extremely permissive \n")
oWriter.write("# threshold to detect redundancies (c=0.10) and fill the 'representative' column, while normally c>=0.4 is recommended.\n")
oWriter.write("# To export a reduced-redundancy set of GO terms, go to the Scatterplot or Table tab, and export from there.\n")
oWriter.write("TermID\tName\tFrequency\tValue\t")
c = 1
while c < worker.MinNumColsPerGoTerm:
oWriter.write("UserValue_{}\t".format(c - 1))
c += 1
oWriter.write("Uniqueness\tDispensability\tRepresentative\n")
# print the data
i = 0
while i < terms.Count:
term = terms[i]
isTermEliminated = term.Dispensability > worker.CutOff
if (isTermEliminated):
i += 1
continue # will not output terms below the dispensability threshold at all
oWriter.write("\"{}\"\t".format(term.GOTerm.FormattedID))
oWriter.write("\"{}\"\t".format(term.GOTerm.Name))
oWriter.write("{}\t".format(term.AnnotationFrequency * 100.0))
oWriter.write("{}\t".format(term.Value))
c = 1
while c < worker.MinNumColsPerGoTerm:
oWriter.write("{}\t".format(term.UserValues[c - 1]))
c += 1
oWriter.write("{}\t".format(term.Uniqueness))
oWriter.write("{}\t".format(term.Dispensability))
if term.RepresentativeID > 0:
oWriter.write("\"{}\"".format(ontology.Terms.GetValueByKey(term.RepresentativeID).Name))
else:
oWriter.write("null")
oWriter.write("\n")
i += 1
def ExportCytoscapeXGMML(visualizer, fileName):
if visualizer.IsEmpty != True:
oWriter = StreamWriter(fileName)
visualizer.SimpleOntologram.GraphToXGMML(oWriter)
oWriter.Close()
def ExportSimMat(visualizer, fileName):
if visualizer.IsEmpty != True:
with open(fileName, 'w') as oWriter:
i = 0
while i < visualizer.Terms.Count:
oWriter.write("\t{}".format(visualizer.Terms[i].GOTerm.FormattedID))
i += 1
oWriter.write("\n")
i = 0
while i < visualizer.Terms.Count:
oWriter.write(visualizer.Terms[i].GOTerm.FormattedID)
j = 0
while j < visualizer.Terms.Count:
oWriter.write("\t{}".format(visualizer.Matrix.GetSimilarity(i, j)))
j += 1
oWriter.write("\n")
i += 1
def ExportWordClouds(worker, fileName):
with open(fileName, 'w') as oWriter:
oWriter.write("{")
if worker.Enrichments.Count > 0:
oWriter.write("\"Enrichments\":[")
MIN_UNIT_SIZE = 1.0
MAX_UNIT_SIZE = 9.0
RANGE_UNIT_SIZE = MAX_UNIT_SIZE - MIN_UNIT_SIZE
minFreq = 999999.0
maxFreq = 0.0
i = 0
while i < worker.Enrichments.Count:
dFrequency = math.sqrt(worker.Enrichments[i].Value)
if dFrequency > 0.0:
minFreq = min(minFreq, dFrequency)
maxFreq = max(maxFreq, dFrequency)
i += 1
if minFreq > maxFreq:
dTemp = minFreq
minFreq = maxFreq
maxFreq = dTemp
if minFreq == maxFreq:
maxFreq += 1
range = maxFreq - minFreq
bFirst = True
i = 0
while i < worker.Enrichments.Count:
sWord = worker.Enrichments[i].Key.replace("'", "")
dFrequency = math.sqrt(worker.Enrichments[i].Value)
if dFrequency > 0.0:
if bFirst == False:
oWriter.write(",")
size = math.ceil(MIN_UNIT_SIZE + round(((dFrequency - minFreq) * RANGE_UNIT_SIZE) / range))
oWriter.write("{{\"Word\":\"{0}\",\"Size\":{1}}}".format(Utilities.StringToJSON(sWord), size))
bFirst = False
i += 1
oWriter.write("]")
if worker.Correlations.Count > 0:
if worker.Enrichments.Count > 0:
oWriter.write(",")
oWriter.write("\"Correlations\":[")
MIN_UNIT_SIZE = 1.0
MAX_UNIT_SIZE = 9.0
RANGE_UNIT_SIZE = MAX_UNIT_SIZE - MIN_UNIT_SIZE
minFreq = 999999.0
maxFreq = 0.0
i = 0
while i < worker.Correlations.Count:
dFrequency = worker.Correlations[i].Value;
if dFrequency > 0.0:
minFreq = min(minFreq, dFrequency)
maxFreq = max(maxFreq, dFrequency)
i += 1
if minFreq > maxFreq:
dTemp = minFreq
minFreq = maxFreq
maxFreq = dTemp
if minFreq == maxFreq:
maxFreq += 1
range = maxFreq - minFreq
bFirst = True
i = 0
while i < worker.Correlations.Count:
sWord = worker.Correlations[i].Key.replace("'", "")
dFrequency = worker.Correlations[i].Value
if dFrequency > 0.0:
if bFirst == False:
oWriter.write(",")
size = math.ceil(MIN_UNIT_SIZE + round(((dFrequency - minFreq) * RANGE_UNIT_SIZE) / range))
oWriter.write("{{\"Word\":\"{0}\",\"Size\":{1}}}".format(Utilities.StringToJSON(sWord), size))
bFirst = False
i += 1
oWriter.write("]");
oWriter.write("}");
def OWorker_OnFinish(sender, e):
if sender != None:
print("Worker {} has finished processing the data in {} seconds.".format(sender.JobID, sender.ExecutingTime.TotalSeconds))
if __name__ == '__main__':
main()