forked from sys-bio/temp-biomodels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_empty_containers_from_sedml_doc.py
75 lines (60 loc) · 2.31 KB
/
remove_empty_containers_from_sedml_doc.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
import libsedml
def remove_empty_containers(filename):
""" Remove empty containers
* Repeated tasks with no subtasks
* Reports with no data sets
* Plots with no curves or surfaces
Args:
filename (:obj:`str`): file name
"""
doc = libsedml.readSedMLFromFile(filename)
changed = False
# remove repeated tasks with no sub-tasks
repeated_task_ids = []
for task in doc.getListOfTasks():
if isinstance(task, libsedml.SedRepeatedTask):
repeated_task_ids.append(task.getId())
while True:
removed_task_ids = []
for task_id in list(repeated_task_ids):
task = doc.getTask(task_id)
if not task.getNumSubTasks():
doc.removeTask(task_id)
repeated_task_ids.remove(task_id)
removed_task_ids.append(task_id)
changed = True
if task.getNumRanges() == 0:
doc.removeTask(task_id)
repeated_task_ids.remove(task_id)
removed_task_ids.append(task_id)
changed = True
removed_sub_task = False
for task_id in repeated_task_ids:
task = doc.getTask(task_id)
for i_sub_task in range(task.getNumSubTasks()):
sub_task = task.getSubTask(i_sub_task)
if sub_task.getTask() in removed_task_ids:
task.removeSubTask(i_sub_task)
removed_sub_task = True
if not removed_sub_task:
break
# remove outputs with no children
for output in list(doc.getListOfOutputs()):
if isinstance(output, libsedml.SedReport):
if not output.getNumDataSets():
doc.removeOutput(output.getId())
changed = True
elif isinstance(output, libsedml.SedPlot2D):
if not output.getNumCurves():
doc.removeOutput(output.getId())
changed = True
elif isinstance(output, libsedml.SedPlot3D):
if not output.getNumSurfaces():
doc.removeOutput(output.getId())
changed = True
# write corrected SED-ML
if changed:
libsedml.writeSedML(doc, filename)
def run(sedml_filenames):
for sedml_filename in sedml_filenames:
remove_empty_containers(sedml_filename)