-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync.py
217 lines (171 loc) · 6.66 KB
/
sync.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
# This script is reponsible for reading, validating and translating
# models in the /sources directory and writing the final results
# into the /models directory. As part of this process, it generates
# basic metadata about the model's structure.
import re
import sys
import json
from pathlib import Path
from os import listdir, mkdir
from os.path import isfile, isdir, join
from biodivine_aeon import *
if not isdir("sources"):
print("ERROR: Missing input `sources` directory.")
sys.exit(128)
if not isdir("models"):
print("ERROR: Missing output `models` directory.")
sys.exit(128)
def read_dir_name(dir_name):
result = re.search("(\\d+)_([A-Z0-9-]+)", dir_name)
if result == None:
print("ERROR: Invalid source directory:", dir_name)
return (int(result.group(1)), result.group(2))
def check_metadata(id, name, metadata, bib):
if (not 'id' in metadata) or metadata['id'] != id:
print("ERROR: Invalid id in metadata of", name)
sys.exit(128)
if (not 'name' in metadata) or metadata['name'] != name:
print("ERROR: Invalid name in metadata of", name)
sys.exit(128)
if(not 'url-publication' in metadata) or metadata['url-publication'] == None:
print("ERROR: Missing publication url in", name)
sys.exit(128)
if (not 'url-model' in metadata) or metadata['url-model'] == None:
print("ERROR: Missing model url in", name)
sys.exit(128)
if (not 'keywords' in metadata) or (metadata['keywords'] == None):
print("ERROR: Missing keywords attribute in", name)
sys.exit(128)
if (not f'bbm-{int(id):03d}' in bib):
print(f"ERROR: Missing a `bbm-{int(id):03d}` key in the `citation.bib` file.")
sys.exit(128)
def check_unused_variables(model: BooleanNetwork):
for var in model.variables():
name = model.get_variable_name(var)
if len(model.predecessors(var)) == 0 and len(model.successors(var)) == 0:
print("ERROR: Variable", name, "is unused.")
sys.exit(128)
function = model.get_update_function(var)
if function is not None and function.as_var() == var and len(model.predecessors(var)) <= 1:
print("ERROR: Variable", name, "is effectively an input.")
sys.exit(128)
def erase_inputs(model: BooleanNetwork):
for var in model.variables():
if len(model.predecessors(var)) == 0:
model.set_update_function(var, None)
return model
def check_integrity(model: BooleanNetwork):
# This will throw an error if the model is invalid.
async_graph = AsynchronousGraph(model)
assert async_graph.network_variable_count() == model.variable_count()
def model_stats(model: BooleanNetwork):
variables = 0
inputs = 0
for var in model.variables():
if len(model.predecessors(var)) == 0:
inputs += 1
else:
variables += 1
return (variables, inputs, len(model.regulations()))
def fix_variable_names(model: BooleanNetwork):
# Ensures that every variable starts with v_,
# otherwise bnet may have problems with invalid names.
for var in model.variables():
name = model.get_variable_name(var)
model.set_variable_name(var, "v_"+name)
return model
source_directories = list(listdir("sources"))
source_directories.sort()
if len(sys.argv) > 1:
test_id = sys.argv[1]
else:
test_id = None
meta_csv_summary = "ID, name, variables, inputs, regulations\n"
for model_dir in source_directories:
if model_dir.startswith("."):
# Skip hidden files.
continue
if test_id and not model_dir.startswith(test_id):
continue
(model_id, model_name) = read_dir_name(model_dir)
if model_name == "TEMPLATE":
# Skip template directories
continue
print("Start processing ", model_dir)
# Check that the model has all metadata prepared.
metadata_file = "sources/" + model_dir + "/metadata.json"
notes_file = "sources/" + model_dir + "/notes.md"
bib_file = "sources/" + model_dir + "/citation.bib"
if not isfile(metadata_file):
print("ERROR: Missing metadata.json in", model_dir)
sys.exit(128)
if not isfile(notes_file):
print("ERROR: Missing notes.md in", model_dir)
sys.exit(128)
if not isfile(bib_file):
print("ERROR: Missing citation.bib in", model_dir)
sys.exit(128)
notes = Path(notes_file).read_text()
bib = Path(bib_file).read_text()
metadata = {}
with open(metadata_file) as file:
metadata = json.load(file)
check_metadata(model_id, model_name, metadata, bib)
# Load the model (including syntactic check)
model = None
if isfile(f'sources/{model_dir}/source.sbml'):
source = Path(f'sources/{model_dir}/source.sbml').read_text()
model = BooleanNetwork.from_sbml(source)
if isfile(f'sources/{model_dir}/source.aeon'):
source = Path(f'sources/{model_dir}/source.aeon').read_text()
model = BooleanNetwork.from_aeon(source)
if isfile(f'sources/{model_dir}/source.bnet'):
source = Path(f'sources/{model_dir}/source.bnet').read_text()
model = BooleanNetwork.from_bnet(source)
if model == None:
print("ERROR: Missing source.sbml/.bnet/.aeon in", model_dir)
sys.exit(128)
# Static analysis (unused variables/regulations, etc.)
check_unused_variables(model)
model = erase_inputs(model)
check_integrity(model)
model = fix_variable_names(model)
print("\t - Model is OK.")
(variables, inputs, regulations) = model_stats(model)
print( "\t - Model has",
variables, "variables,",
inputs, "inputs, and",
regulations, "regulations.")
metadata['variables'] = variables
metadata['inputs'] = inputs
metadata['regulations'] = regulations
metadata['notes'] = notes
metadata['bib'] = bib
# Add a row to the metadata summary csv:
meta_csv_summary += f"{model_id:03d}, {model_name}, {variables}, {inputs}, {regulations}\n"
output_directory = f'[id-{model_id:03d}]__[var-{variables}]__[in-{inputs}]__[{model_name}]'
if not isdir('models/'+output_directory):
mkdir('models/'+output_directory)
# Write model outputs
Path(f'models/{output_directory}/model.aeon').write_text(model.to_aeon())
Path(f'models/{output_directory}/model.sbml').write_text(model.to_sbml())
Path(f'models/{output_directory}/model.bnet').write_text(model.to_bnet())
with open(f'models/{output_directory}/metadata.json', "w") as file:
json.dump(metadata, file, indent=4)
readme = f'# \\[{model_id:03d}\\] {model_name}\n\n'
readme += f' - Variables: {variables}\n'
readme += f' - Inputs: {inputs}\n'
readme += f' - Regulations: {regulations}\n'
readme += f' - Publication: {metadata["url-publication"]}\n'
readme += f' - Source: {metadata["url-model"]}\n'
readme += f' - Keywords: {", ".join(metadata["keywords"])}\n'
readme += "\n\n"
readme += notes
readme += "\n\n### Model citation"
readme += "\n\n```\n"
readme += bib
readme += "\n```\n\n"
Path(f'models/{output_directory}/README.md').write_text(readme)
print("\t - Model exported into", output_directory)
Path(f'models/summary.csv').write_text(meta_csv_summary)
print("Dataset summary written to `models/summary.csv`.")