forked from ssbio/E_dermatitidis_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertModel.py
221 lines (192 loc) · 9.74 KB
/
convertModel.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
# converts reaction list and descrition in a text file to GAMS input format
__author__ = "Mohammad Mazharul Islam and Wheaton Schroeder"
#version 1.0: Mohammad
#version 1.1: Wheaton
#updates associated with version 1.1:
#*updated the output to write all files necessary for fba.gms (added exchange_reactions.txt'
# irreversible_reactions.txt, and reversible_reactions_no_exchange such that this programs
# output is the input to fba.gms.
# This program is dependent on the nomenclature in kathKeys.txt, and was originally written
# to streamline the time to FBA using personal notation
#*minor streamlining of some lines of code, variable addition associated with previous bullet update
#*added command line output so that the user knows the program ran successfully
#importing the string package
import string
#import the regular expression package
import re
#this is the input file, 'r' denotes read operation
input1=open('Model.txt','r')
#these are the output file names, 'w' denotes write operation
output1=open('mets.txt','w')
output2=open('rxntype.txt','w')
output3=open('Sij.txt','w')
output4=open('rxns.txt','w')
output5=open('ex_rxns.txt','w')
output6=open('irrev_rxns_f.txt','w')
output7=open('rev_rxns_no_ex.txt','w')
output8=open('reg_rxns.txt','w')
output9=open('irrev_rxns_b.txt','w')
output10=open('medium.txt','w')
#reading the input file
alllines1=input1.readlines()
#declaring empty lists for metabolites and reactions (where each metabolite and reaction will be added one by one after reading the input file)
#complete metabolite list
met_list=[]
#complete reaction list
rxn_list=[]
#list of irreversible reactions
irr_rxn_list=[]
#list of reactions that are reversible, but not exchange
rev_rxn_no_ex_list=[]
#list of exchange reactions
ex_rxn_list=[]
#list of regulated reactions
reg_rxn_list=[]
#list of sink reactions
sink_rxn_list=[]
#objective species
objective=""
output1.write('/\n') #writing a '/' mark at the beginning of the output file 1, then new line (metabolites.txt)
output2.write('/\n') #writing a '/' mark at the beginning of the output file 2, then new line (rxntype.txt)
output3.write('/\n') #writing a '/' mark at the beginning of the output file 3, then new line (S_matrix.txt)
output4.write('/\n') #writing a '/' mark at the beginning of the output file 4, then new line (reactions.txt)
output5.write('/\n') #writing a '/' mark at the beginning of the output file 5, then new line (exchange_reactions.txt)
output6.write('/\n') #writing a '/' mark at the beginning of the output file 6, then new line (irreversible_reactions_f.txt)
output7.write('/\n') #writing a '/' mark at the beginning of the output file 7, then new line (reversible_reactions_no_exchange.txt)
output8.write('/\n') #writing a '/' mark at the beginning of the output file 8, then new line (regulated_reactions.txt)
output9.write('/\n') #writing a '/' mark at the beginning of the output file 8, then new line (regulated_reactions_b.txt)
output10.write('/\n') #writing a '/' mark at the beginning of the output file 8, then new line (regulated_reactions_b.txt)
for line in alllines1: #for each line in the input file
#this should allow comments lines that are deliniated by # at the start off the line
if bool(re.search('^#',line)):
#ignore this line, this line is a comment
print('comment line: \"'+line+'\"')
else:
line=line.rstrip() # stripping off any empty characters at the end of the line
rxn,des=line.split('\t') #splitting the remaining line with a tab separator, in this case rxn will give reaction name, and des will give you the rest
print(rxn)
#Quick key:
#rxn - reaction name/number/id/etc
#des - reaction descrition, the written out chemical reaction, possibly with stoichiometry
#block of text to handle regulated reactions before going into if/else scenarios
if bool(re.search('reg$',rxn)): #if has reg tag, note that regular expression search converted to boolean
if rxn not in reg_rxn_list: #if not already it regulated reactions list
reg_rxn_list.append(rxn) #append it to list
output8.write("'"+rxn+"'\n") #write it to appropriate file
if '->' in line and '<->' not in line: #if a forward reaction
reactant=des.split('->')[0] #splitting the "des" at '->' sign, [0] gives the first portion
product=des.split('->')[1] #[1] gives the second
dir='->' #directionality
state='1'
#find out if objective function, save objective species
if bool(re.search("biomass_ch",rxn)):
objective=reactant
if rxn not in irr_rxn_list:
irr_rxn_list.append(rxn) #add this reaction to irreversible list
output6.write("'"+rxn+"'\n") #writes reaction to irreversible reaction list
#find out if exchange or not
if bool(re.search('ex$',rxn)): #if exchange reaction, not that it is converted to a boolean of a regular expression search
if rxn not in ex_rxn_list:
ex_rxn_list.append(rxn) #add to exchange reaction list
output5.write("'"+rxn+"'\n") #write reaction to exchange reaction list
if bool(re.search('si$',rxn)):
if rxn not in sink_rxn_list:
sink_rxn_list.append(rxn)
elif '<-' in line and '<->' not in line: #if a reverse reaction
reactant=des.split('<-')[0]
product=des.split('<-')[1]
dir='<-' #directionality
state='-1'
if rxn not in irr_rxn_list:
irr_rxn_list.append(rxn) #add this reaction to irreversible list
output9.write("'"+rxn+"'\n") #writes reaction to irreversible reaction list
#find out if exchange or not
if bool(re.search('ex$',rxn)): #if exchange reaction, not that it is converted to a boolean of a regular expression search
if rxn not in ex_rxn_list:
ex_rxn_list.append(rxn) #add to exchange reaction list
output5.write("'"+rxn+"'\n") #write reaction to exchange reaction list
output10.write("'"+rxn+"'\n") #write reaction to exchange reaction list
if bool(re.search('si$',rxn)): #find out if sink reaction
if rxn not in sink_rxn_list:
sink_rxn_list.append(rxn)
else: #if a reversible reaction
reactant=des.split('<->')[0]
product=des.split('<->')[1]
dir='<->' #directionality
state='0'
#find out if exchange or not
if bool(re.search('ex$',rxn)): #if exchange reaction, not that it is converted to a boolean of a regular expression search
if rxn not in ex_rxn_list:
ex_rxn_list.append(rxn) #add to exchange reaction list
output5.write("'"+rxn+"'\n") #write reaction to exchange reaction list
output10.write("'"+rxn+"'\n") #write reaction to exchange reaction list
else: #otherwise, it is not an exchange reaction
if rxn not in rev_rxn_no_ex_list:
rev_rxn_no_ex_list.append(rxn) #append reversible but not exchange list
output7.write("'"+rxn+"'\n") #writes reaction to list of reversible reactions that are not exchange reactions
if bool(re.search('si$',rxn)): #find out if sink reaction
if rxn not in sink_rxn_list:
sink_rxn_list.append(rxn)
if rxn not in rxn_list:
rxn_list.append(rxn) #if the reaction name was not already in the reaction list, we are adding it
output2.write( "'"+ rxn + "' " + state) #writing the output in the rxntype.txt file
output2.write('\n') #newline
output4.write("'"+ rxn + "'")
output4.write('\n') #newline
dummy_list=reactant.split(' + ') #splitting the reactant side and getting the individual reactants
k=0
while k<len(dummy_list): #for every reactant, splitting the stochiometric coefficients
ind_react=dummy_list[k]
ind_react=ind_react.strip()
if ' ' in ind_react:
st, react_compt = ind_react.split()
else:
st = 1
react_compt = ind_react
if react_compt not in met_list:
met_list.append(react_compt) #if the metabolite name was not already in the metabolite list, we are adding it
output1.write("'"+react_compt+ "'")
output1.write('\n') #newline
#joining the reactant name and rxn name and stoichiometric coefficinet to write into output3 (Sij.txt)
stoich_line = "'" + react_compt + "'" +'.'+"'" + rxn +"'" +' '+'-'+str(st)
output3.write(stoich_line)
output3.write('\n') #newline
k=k+1
if product != '':
dummy_list2=product.split(' + ') #splitting the product side and getting the individual products
kk=0
while kk<len(dummy_list2): #for every product, splitting the stochiometric coefficients
ind_prod=dummy_list2[kk]
ind_prod=ind_prod.strip()
if ' ' in ind_prod:
st,prod_compt=ind_prod.split()
else:
st = 1
prod_compt = ind_prod
if prod_compt not in met_list:
met_list.append(prod_compt)
output1.write("'"+prod_compt+"'")
output1.write('\n') #newline
# joining the product name and rsn name and stoichiometric coefficinet to write into output3 (Sij.txt)
stoich_line = "'" + prod_compt+ "'" +'.'+"'" +rxn+"'" +' '+str(st)
output3.write(stoich_line)
output3.write('\n') #newline
kk=kk+1
#add the ending '/' mark to note the end of the file
output1.write('/')
output2.write('/')
output3.write('/')
output4.write('/')
output5.write('/')
output6.write('/')
output7.write('/')
output8.write('/')
output9.write('/')
output10.write('/')
#outputs to let the user know the stats, and show that something has happened
print("number metabolites: "+str(len(met_list))) #printing the total number of metabolites
print("number reactions: "+str(len(rxn_list))) #printing the total number of reactions
print("irreversible reactions: "+str(len(irr_rxn_list))) #print number of irreversible reactions
print("exchange reactions: "+str(len(ex_rxn_list))) #print number of exchange reactions
print("sink reactions: "+str(len(sink_rxn_list))) #print number of exchange reactions
print("objective: "+objective) #print ojective