Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit f71d165

Browse files
committed
...
1 parent aec17fd commit f71d165

File tree

2 files changed

+260
-0
lines changed

2 files changed

+260
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ install:
88
# !!! It will take 14 minutes, so response is needed. But the full output is too long !!!
99
# - pip install -v scipy | grep ^.*building
1010
- pip install -r requirements.txt
11+
- apt-get install pypy
1112
- cd BioBLESS/biocircuit
1213
- make
1314
- python biogate_gen.py
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#!/usr/bin/env python
2+
"""
3+
This is a module to describe BioSystem.
4+
5+
There is a function named bio_system in this module.
6+
The format to input is in doc/api-for-front/gates_specification.txt
7+
8+
Examples
9+
--------------
10+
Create some biosys to calculate their simulation
11+
12+
>>> test = bio_system(data)
13+
>>> test.simulation()
14+
15+
"""
16+
__author__ = 'Trumpet'
17+
18+
import os
19+
try:
20+
import simplejson
21+
except:
22+
import json as simplejson
23+
24+
from reaction_system import ReactionSystem
25+
26+
27+
GATE_FILE = None
28+
real_path = os.path.split(os.path.realpath(__file__))[0]+"/"
29+
try:
30+
GATE_FILE = open(real_path+"../../../doc/devices/gates_lizhi.json", "r")
31+
except IOError:
32+
GATE_FILE = open(real_path+"../doc/devices/gates_lizhi.json", "r")
33+
gate_data_source = GATE_FILE.read()
34+
gates_data = simplejson.loads(gate_data_source)
35+
gates_data = list(gates_data)
36+
gates_data.append({
37+
"id": "INPUT",
38+
"input": [],
39+
"output": ["d1"],
40+
"parts": {
41+
"id": [["d1"]],
42+
"type": {"d1": "Input"}
43+
},
44+
"map": [],
45+
"type": {}
46+
})
47+
48+
49+
def dev_system(gates, data, nodes_id, input_sub, output_sub):
50+
"""
51+
To build a reaction system from the data of a device.
52+
53+
Input gates data(from doc/devices/gates_lizi.json),
54+
and data(format is the same with the system data),
55+
nodes_id,input_sub and output_sub(str)
56+
Then return a reaction system.
57+
This reaction system will be sumerize in bio_system
58+
"""
59+
if gates["id"] == "INPUT":
60+
species = [[gates["parts"]["id"][0][0], data["device_parameter"]["initial"][0]]]
61+
reaction = []
62+
else:
63+
def coding_find_last(tmp, string):
64+
while parts_type[tmp] != string:
65+
tmp -= 1
66+
return tmp + 1
67+
68+
def select(list_from, string):
69+
ans = []
70+
tmp = 0
71+
flag = True
72+
while flag:
73+
try:
74+
ans.append(list_from.index(string, tmp) + 1)
75+
tmp = ans[-1]
76+
except ValueError:
77+
flag = False
78+
return ans
79+
80+
def get_species(list_from, str_list):
81+
list_from = map(str, list_from)
82+
return [i + j for i in str_list for j in list_from]
83+
84+
def find_in_map(tmp=0, **kw):
85+
for single_map in range(tmp, len(maps)):
86+
try:
87+
if maps[single_map][kw.keys()[0]] == kw.values()[0]:
88+
return single_map#
89+
except KeyError:
90+
pass
91+
return -1
92+
93+
parts_type = [gates["parts"]["type"]["d" + str(i + 1)] for i in range(len(gates["parts"]["type"]))]
94+
maps = gates["map"]
95+
protein = select(parts_type, "Coding")
96+
s_rna = select(parts_type, "sRNA")
97+
species = get_species(protein, ["d", "m", "r", "l", "p"]) + get_species(s_rna, ["d", "m", "r"])
98+
initial = data["device_parameter"]["initial"]
99+
for i in range(len(initial)):
100+
for j in gates["parts"]["id"][i]:
101+
if j[0] == "d":
102+
try:
103+
species[species.index(j)] = [j, initial[i]]
104+
except ValueError:
105+
pass
106+
species += gates["input"]
107+
108+
reaction = []
109+
for single in protein + s_rna:
110+
part_type = parts_type[single - 1]
111+
part_id = str(single)
112+
maper = find_in_map(id1="d" + part_id)
113+
single_data = data[maps[maper]["id"]]
114+
single_reaction = [
115+
[["d" + part_id], ["m" + part_id], single_data["trans1"]],
116+
[["m" + part_id], ["d" + part_id, "r" + part_id], single_data["trans1"]],
117+
[["r" + part_id], [], single_data["decay1"]],
118+
[["r" + part_id], ["l" + part_id], single_data["trans2"]],
119+
[["l" + part_id], ["r" + part_id, "p" + part_id], single_data["trans2"]],
120+
[["p" + part_id], [], single_data["decay2"]],
121+
] if part_type == "Coding" else [
122+
[["d" + part_id], ["m" + part_id], single_data["trans1"]],
123+
[["m" + part_id], ["d" + part_id, "r" + part_id], single_data["trans1"]],
124+
[["r" + part_id], [], single_data["decay1"]],
125+
]
126+
for reg_sub in ["Promoter", "RBS"]:
127+
#print reg_sub+","+str(single)#
128+
if reg_sub == "Promoter":
129+
pro = coding_find_last(single, reg_sub)
130+
tmp = find_in_map(id2="d" + str(pro))
131+
else:
132+
rbs = coding_find_last(single, reg_sub)
133+
if rbs > pro:
134+
pro = rbs
135+
tmp = find_in_map(id2="d" + str(pro))
136+
else:
137+
tmp = -1
138+
while tmp != -1:
139+
reg_data = data[maps[tmp]["id"]]
140+
reg_pro = maps[tmp]["id1"]
141+
142+
#print str(maps[tmp]["id"])+","+str(reg_data)#
143+
"""
144+
d -> r -> p
145+
m l
146+
n i
147+
"""
148+
149+
if maps[tmp]["type"] == "inh":
150+
single_reaction.append(
151+
[[reg_pro, "m" + part_id], ["n" + part_id], reg_data["reg"] * single_data["trans1"]]
152+
)
153+
single_reaction.append(
154+
[["n" + part_id], [reg_pro, "d" + part_id], reg_data["reg"] * single_data["trans1"]]
155+
)
156+
if not "n" + part_id in species:
157+
species.append("n" + part_id)
158+
temp = find_in_map(id2="e" + str(tmp+1))
159+
while temp != -1:
160+
rep_data = data[maps[temp]["id"]]
161+
rep_pro = maps[temp]["id1"]
162+
single_reaction.append(
163+
[[rep_pro, "n" + part_id], ["m" + part_id, reg_pro, rep_pro],
164+
rep_data["reg"] * reg_data["reg"] * single_data["trans1"]]
165+
)
166+
temp = find_in_map(temp + 1, id2="e" + str(tmp+1))
167+
168+
if maps[tmp]["type"] == "lock":
169+
single_reaction.append(
170+
[[reg_pro, "l" + part_id], ["i" + part_id], reg_data["reg"] * single_data["trans2"]]
171+
)
172+
single_reaction.append(
173+
[["i" + part_id], [reg_pro, "r" + part_id], reg_data["reg"] * single_data["trans2"]]
174+
)
175+
if not "i" + part_id in species:
176+
species.append("i" + part_id)
177+
178+
if maps[tmp]["type"] == "act":
179+
single_reaction.append(
180+
[[reg_pro, "d" + part_id], [reg_pro, "d" + part_id, "r" + part_id],
181+
reg_data["reg"] * single_data["trans1"]]
182+
)
183+
184+
if maps[tmp]["type"] == "key":
185+
single_reaction.append(
186+
[[reg_pro, "r" + part_id], [reg_pro, "p" + part_id, "r" + part_id],
187+
reg_data["reg"] * single_data["trans2"]]
188+
)
189+
190+
tmp = find_in_map(tmp + 1, id2="d" + str(pro))
191+
reaction += single_reaction
192+
193+
def add_str(list_from, nodes_id):
194+
for sig in list_from:
195+
if isinstance(sig, list):
196+
add_str(sig, nodes_id)
197+
for sig in range(len(list_from)):
198+
if isinstance(list_from[sig], str) or isinstance(list_from[sig], unicode):
199+
list_from[sig] = nodes_id + list_from[sig]
200+
201+
def replace_str(list_from, st1, st2):
202+
for sig in list_from:
203+
if isinstance(sig, list):
204+
replace_str(sig, st1, st2)
205+
for sig in range(len(list_from)):
206+
if list_from[sig] == st1:
207+
list_from[sig] = st2
208+
209+
#"""
210+
add_str(species, "S"+nodes_id)
211+
add_str(reaction, "S"+nodes_id)
212+
tmp = [replace_str(species, "S"+nodes_id + gates["input"][i], "S"+input_sub[i]) for i in range(len(input_sub))]
213+
tmp = [replace_str(species, "S"+nodes_id + gates["output"][i], "S"+output_sub[i]) for i in range(len(output_sub))]
214+
tmp = [replace_str(reaction, "S"+nodes_id + gates["input"][i], "S"+input_sub[i]) for i in range(len(input_sub))]
215+
tmp = [replace_str(reaction, "S"+nodes_id + gates["output"][i], "S"+output_sub[i]) for i in range(len(output_sub))]
216+
"""
217+
add_str(species, nodes_id)
218+
add_str(reaction, nodes_id)
219+
tmp = [replace_str(species, nodes_id + gates["input"][i], input_sub[i]) for i in range(len(input_sub))]
220+
tmp = [replace_str(species, nodes_id + gates["output"][i], output_sub[i]) for i in range(len(output_sub))]
221+
tmp = [replace_str(reaction, nodes_id + gates["input"][i], input_sub[i]) for i in range(len(input_sub))]
222+
tmp = [replace_str(reaction, nodes_id + gates["output"][i], output_sub[i]) for i in range(len(output_sub))]
223+
"""
224+
return ReactionSystem(reaction, species)
225+
226+
227+
def bio_system(system_data):
228+
"""
229+
Input a BioSystem, and output the reaction system with simulation.
230+
231+
The format to input is in doc/api-for-front/gates_specification.txt
232+
Input it,and return a reaction system which you can use simulation() and record_list
233+
"""
234+
time = system_data["system_parameter"]["time"]
235+
gates = {single_gate["id"]: single_gate for single_gate in gates_data}
236+
nodes = system_data["nodes"]
237+
devices = [dev_system(
238+
gates[nodes[i]],
239+
system_data["simulation_parameters"][i],
240+
str(i),
241+
[str(j["from"]) for j in system_data["arcs"] if j["to"] == i],
242+
[str(i)]
243+
) for i in range(len(nodes))]
244+
reaction = devices[0]
245+
for single_nodes in range(1, len(nodes)):
246+
reaction += devices[single_nodes]
247+
248+
reaction.time = time
249+
reaction.nodes = nodes
250+
return reaction
251+
252+
def json_to_json(string):
253+
system = bio_system(simplejson.loads(string))
254+
system.simulation()
255+
return simplejson.dumps(system.record)
256+
257+
if __name__ == "__main__":
258+
reader = raw_input()
259+
print json_to_json(reader)

0 commit comments

Comments
 (0)