forked from sys-bio/temp-biomodels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_unused_sedml_elements.py
218 lines (186 loc) · 6.75 KB
/
remove_unused_sedml_elements.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
import libsedml
import os
def hasTime(plot):
doc = plot.getSedDocument()
for curve in plot.getListOfCurves():
xdg = doc.getDataGenerator(curve.getXDataReference())
var = xdg.getVariable(0)
if var.isSetSymbol():
if var.getSymbol() == "urn:sedml:symbol:time":
return True
return False
def checkRepeats(plot):
doc = plot.getSedDocument()
for curve in plot.getListOfCurves():
ydg = doc.getDataGenerator(curve.getYDataReference())
var = ydg.getVariable(0)
if var.isSetTaskReference():
task = doc.getTask(var.getTaskReference())
if isinstance(task, libsedml.SedRepeatedTask):
range = task.getRange(0)
if isinstance(range, libsedml.SedUniformRange):
return (True, range.getNumberOfPoints())
if isinstance(range, libsedml.SedVectorRange):
return (True, range.getNumValues())
raise("Unimplemented Range type:\n" + range.toSed())
return (False, 1)
def choose_between(plotlist):
unlikely = []
possible = []
good = []
for plot in plotlist:
if not isinstance(plot, libsedml.SedPlot2D):
raise("Unable to parse surface plots.")
hastime = hasTime(plot)
isrepeat, reps = checkRepeats(plot)
if hastime:
if isrepeat:
if reps <= 30:
good.append(plot)
else:
unlikely.append(plot)
else:
possible.append(plot)
else:
if isrepeat:
good.append(plot)
else:
possible.append(plot)
# print(plot.toSed())
if len(good) > 0:
# If there was a better option, don't use the worse one
unlikely.extend(possible)
return unlikely
if len(possible) > 0:
return unlikely
return []
def remove_duplicate_plots(doc):
""" Remove any duplicate plots
Args:
doc (:obj:`libsedml.SedMLDocument`): Document
"""
changed = False
# Collect plot names
names = {}
for plot in doc.getListOfOutputs():
if isinstance(plot, libsedml.SedPlot):
if plot.isSetName():
name = plot.getName()
if name not in names:
names[name] = []
names[name].append(plot)
for name in names:
if len(names[name]) > 1:
for plot in choose_between(names[name]):
doc.removeOutput(plot.getId())
changed = True
# write corrected SED-ML
return changed
def remove_unused_datagens(doc):
""" Remove any duplicate data generators
Args:
doc (:obj:`libsedml.SedMLDocument`): Document
"""
changed = False
# Collect referenced data generators
used_datagens = set()
for output in doc.getListOfOutputs():
if isinstance(output, libsedml.SedReport):
for dataset in output.getListOfDataSets():
used_datagens.add(dataset.getDataReference())
elif isinstance(output, libsedml.SedPlot2D):
for curve in output.getListOfCurves():
used_datagens.add(curve.getXDataReference())
used_datagens.add(curve.getYDataReference())
elif isinstance(output, libsedml.SedSurface):
for surface in output.getListOfSurfaces():
used_datagens.add(surface.getXDataReference())
used_datagens.add(surface.getYDataReference())
used_datagens.add(surface.getZDataReference())
unused_datagens = []
dgs = doc.getListOfDataGenerators()
for datagen in dgs:
dgid = datagen.getId()
if dgid not in used_datagens:
unused_datagens.append(dgid)
for unused_datagen in unused_datagens:
dgs.remove(unused_datagen)
changed = True
# write corrected SED-ML
return changed
def remove_unused_tasks(doc):
changed = False
# get tasks used by data generators
used_tasks = set()
for datagen in doc.getListOfDataGenerators():
for variable in datagen.getListOfVariables():
if variable.isSetTaskReference():
used_tasks.add(variable.getTaskReference())
# add used subtasks
used_parent_tasks = list(used_tasks)
while used_parent_tasks:
parent_task = doc.getTask(used_parent_tasks.pop())
if isinstance(parent_task, libsedml.SedRepeatedTask):
for subtask in parent_task.getListOfSubTasks():
used_parent_tasks.append(subtask.getTask())
used_tasks.add(subtask.getTask())
# determine unused tasks
unused_tasks = set()
for task in doc.getListOfTasks():
task_id = task.getId()
if task_id not in used_tasks:
unused_tasks.add(task_id)
# remove unused tasks
for task_id in unused_tasks:
doc.removeTask(task_id)
changed = True
# return whether at least one task was removed
return changed
def remove_unused_sims_and_mods(doc):
changed = False
used_sims = set()
used_mods = set()
for task in doc.getListOfTasks():
try:
used_mods.add(task.getModelReference())
used_sims.add(task.getSimulationReference())
except Exception:
pass
unused_sims = set()
unused_mods = set()
for mod in doc.getListOfModels():
mid = mod.getId()
if mid not in used_mods:
unused_mods.add(mid)
for sim in doc.getListOfSimulations():
sid = sim.getId()
if sid not in used_sims:
unused_sims.add(sid)
for sid in unused_sims:
doc.removeSimulation(sid)
changed = True
for mid in unused_mods:
doc.removeModel(mid)
changed = True
return changed
def run(id, sedml_filenames):
removed_sedml_files = []
for sedml_filename in sedml_filenames:
if "Parmar2017_Deficient_Rich_tracer" in sedml_filename or "Parmar2017_Adequate_tracer" in sedml_filename:
continue
doc = libsedml.readSedMLFromFile(sedml_filename)
changed = False
changed = remove_duplicate_plots(doc) or changed
changed = remove_unused_datagens(doc) or changed
changed = remove_unused_tasks(doc) or changed
changed = remove_unused_sims_and_mods(doc) or changed
if (changed):
# print("Modified", id, os.path.basename(sedml_filename))
if doc.getNumModels() == 0:
# Remove the SED-ML file entirely
os.remove(sedml_filename)
removed_sedml_files.append(sedml_filename)
else:
libsedml.writeSedMLToFile(doc, sedml_filename)
for sedml_filename in removed_sedml_files:
sedml_filenames.remove(sedml_filename)