-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder.py
More file actions
executable file
·360 lines (316 loc) · 10.9 KB
/
folder.py
File metadata and controls
executable file
·360 lines (316 loc) · 10.9 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/env python3
from configparser import ConfigParser
import getopt, sys
import cProfile
import copy
import pyximport; pyximport.install()
orients = ['N','S','E','W']
folded = 0
def score(protein):
h_amino = set()
all_amino = set()
score = 0
for i in range(1, len(protein)):
amino = protein[i]
cardinal = (amino[2][0], amino[2][1])
all_amino.add(cardinal)
if amino[0] == "H":
x = cardinal[0]
y = cardinal[1]
all_neighbors = [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]
for neighbor in all_neighbors:
if neighbor in h_amino:
score += 1
h_amino.add(cardinal)
for i in range(1, len(protein)):
amino = protein[i]
cardinal = (amino[2][0], amino[2][1])
x = cardinal[0]
y = cardinal[1]
all_neighbors = [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]
for neighbor in all_neighbors:
if neighbor in all_amino:
score += 1
return score
def score1(protein):
set_h = set()
score = 0
for i in range(1, len(protein)):
amino = protein[i]
if amino[0] == "H":
cardinal = (amino[2][0], amino[2][1])
set_h.add(cardinal)
for i in range(1, len(protein)):
amino = protein[i]
if amino[0] == "H":
cardinal = (amino[2][0], amino[2][1])
x = cardinal[0]
y = cardinal[1]
all_neighbors = [(x+1,y), (x-1,y), (x,y+1), (x,y-1)]
for neighbor in all_neighbors:
if neighbor in set_h:
score += 1
return score
def parse_protein(protein_string):
protein_struct = []
protein_struct.append(['X','X',['x','x']])
protein_struct.append([protein_string[0],'',[0,0]])
for i in range(1,len(protein_string)-1):
protein_struct.append([protein_string[i],'',['x','x']])
protein_struct.append([protein_string[i+1],'X',['x','x']])
return protein_struct
def cardinalize(protein):
cardinals = []
cardinals.append((0,0))
cardinals.append((0,0))
for i in range(2, len(protein)):
orientation = protein[i-1][1]
if orientation == 'N':
new_cards = (cardinals[i-1][0],cardinals[i-1][1]+1)
elif orientation == 'S':
new_cards = (cardinals[i-1][0],cardinals[i-1][1]-1)
elif orientation == 'E':
new_cards = (cardinals[i-1][0]+1,cardinals[i-1][1])
else:
new_cards = (cardinals[i-1][0]-1,cardinals[i-1][1])
cardinals.append(new_cards)
return cardinals
def is_protein_valid(protein):
seen = set()
for i in range(1, len(protein)):
x = protein[i][2][0]
y = protein[i][2][1]
cardinal = (x,y)
if cardinal in seen:
return False
seen.add(cardinal)
return True
def check_new_fold(new_fold, best_score, best_protein):
if new_fold[0] > best_score:
best_score = new_fold[0]
best_protein = new_fold[1]
return best_score, best_protein
def complete_protein(protein, hide, best_score):
global folded
if is_protein_valid(protein):
if hide == 0:
draw_protein(protein)
folded += 1
current_score = score(protein)
if current_score > best_score:
if hide == 1:
draw_protein(protein)
print("Score:",str(current_score)+"\n")
return current_score, copy.deepcopy(protein)
return 0,[]
def oposite(orientation):
if orientation == "N":
return "S"
if orientation == "S":
return "N"
if orientation == "E":
return "W"
if orientation == "W":
return "E"
def symbol_preprocess_check(protein, symbol, depth):
symbol_oposite = oposite(symbol)
if symbol_oposite == protein[depth-1][1]:
return False
if depth > 3:
a = protein[depth-1][1]
b = protein[depth-2][1]
c = protein[depth-3][1]
if symbol_oposite == b and oposite(a) == c:
return False
return True
def partial_protein(protein, depth, length, hide, max_folds,\
best_score, best_protein):
global orients
for symbol in orients:
if symbol_preprocess_check(protein, symbol, depth):
protein[depth][1] = symbol
offset = card_offset(symbol) # Improvement here
protein[depth+1][2][0] = protein[depth][2][0]+offset[0]
protein[depth+1][2][1] = protein[depth][2][1]+offset[1]
if is_protein_valid(protein[:depth+1]):
new_fold = fold_rec(protein,depth+1,\
length,hide,max_folds, best_score)
best_score, best_protein = check_new_fold(new_fold,best_score,\
best_protein)
return best_score, best_protein
def fold_rec(protein, depth, length, hide, max_folds, best_score=-1):
if max_folds != 0 and folded >= max_folds:
return 0,[]
if depth == length-1:
score,new_protein = complete_protein(protein, hide, best_score)
return score, new_protein
best_protein = None
best_score, best_protein = partial_protein(protein, depth, \
length, hide, max_folds, best_score, best_protein)
return best_score, best_protein
def fold(protein, max_folds, hide):
best_fold = fold_rec(protein, 1, len(protein), hide, max_folds)
return best_fold
def print_protein(protein):
protein = protein[1:]
for i, aminoacid in enumerate(protein):
print(i, aminoacid[0], aminoacid[1],
"("+str(aminoacid[2][0])+","+str(aminoacid[2][1])+")")
print()
def normalize_cardinals(cardinals):
minimum_1 = len(cardinals)*2
minimum_2 = len(cardinals)*2
for cardinal in cardinals:
if cardinal[0] < minimum_1:
minimum_1 = cardinal[0]
if cardinal[1] < minimum_2:
minimum_2 = cardinal[1]
if minimum_1 < 0 and minimum_2 < 0:
for i in range(len(cardinals)):
new_card = (cardinals[i][0]+minimum_1*-1,
cardinals[i][1]+minimum_2*-1)
cardinals[i] = new_card
elif minimum_1 < 0:
for i in range(len(cardinals)):
new_card = (cardinals[i][0]+minimum_1*-1,
cardinals[i][1])
cardinals[i] = new_card
elif minimum_2 < 0:
for i in range(len(cardinals)):
new_card = (cardinals[i][0],
cardinals[i][1]+minimum_2*-1)
cardinals[i] = new_card
return cardinals
def print_grid(grid):
for row in grid:
for col in row:
#print(col, end="")
sys.stdout.write(col)
print()
def create_base_grid(cardinals, length):
grid = []
for i in range(length):
row = []
for j in range(length):
row.append(" ")
grid.append(row)
return grid
def card_offset(orientation):
if orientation == 'N':
offset = (0,1)
elif orientation == 'S':
offset = (0,-1)
elif orientation == 'E':
offset = (1,0)
else:
offset = (-1,0)
return offset
def card_offset_f_orientation(orientation):
if orientation == 'N':
offset = (0,1)
padding_char = '|'
elif orientation == 'S':
offset = (0,-1)
padding_char = '|'
elif orientation == 'E':
offset = (1,0)
padding_char = '-'
else:
offset = (-1,0)
padding_char = '-'
return offset, padding_char
def insert_protein_in_grid(grid, protein, cardinals):
for i in range(1, len(protein)):
bond_orientation = protein[i][1]
x = cardinals[i][0]*2
y = cardinals[i][1]*2
grid[y][x] = protein[i][0]
if bond_orientation != 'X':
offset, padding_char =\
card_offset_f_orientation(bond_orientation)
grid[y+offset[1]][x+offset[0]] = padding_char
return grid
def draw_protein(protein):
cardinals = cardinalize(protein)
normalized_card = normalize_cardinals(cardinals)
grid = create_base_grid(normalized_card, len(protein)*2)
grid = insert_protein_in_grid(grid, protein, normalized_card)
print_grid(grid)
def parse_arguments():
argument_list = sys.argv[1:]
short_options = "hbf:p:n"
long_options = ["help","bench","max_folds=",\
"protein=", "best"]
help_text = "You asked for help?"
bench = False
max_folds = 0
protein_string = ""
hide = 0
# 0 show all
# 1 show if one better pops up
# 2 show better of all
try:
arguments, value = getopt.getopt(argument_list, short_options,\
long_options)
except getopt.error as err:
print(help_text)
sys.exit(1)
for current_arg, current_val in arguments:
if current_arg in ("-h","--help"):
print(help_text)
sys.exit(1)
if current_arg in ("-b","--bench"):
bench = True
if current_arg in ("-f","-max_folds"):
max_folds = int(current_val[1:])
if current_arg in ("-p","--protein"):
protein_string = current_val[1:]
if current_arg in ("-n","--not_show","--best"):
hide = 2
if current_arg == "--best":
hide = 1
return bench, hide, max_folds, protein_string
def handle_parameters():
bench, hide, max_folds, protein_string = parse_arguments()
max_folds_conf, seed, protein_string_conf = read_config_file("params.conf")
if max_folds == 0:
max_folds = max_folds_conf
if protein_string == "":
protein_string = protein_string_conf
if len(protein_string) < 3:
print("Protein must have at least 3 aminoacids")
sys.exit(-1)
protein = parse_protein(protein_string)
return bench, hide, seed, max_folds, protein
def read_config_file(file_path):
config_object = ConfigParser()
config_object.read(file_path)
program_params = config_object["PROGRAM_PARAMS"]
max_folds = program_params["max_folds"]
seed = program_params["seed"]
protein = config_object["PROTEIN"]
protein_string = protein["string"]
return int(max_folds), seed, protein_string
def profile(hide):
max_folds = 1000000
protein_string_complex = "HPHPHHPHPHHPPHHPHPH"
protein_string_simple = "HPHPHH"
protein = parse_protein(protein_string_complex)
fold(protein, max_folds, hide)
def main():
bench, hide, seed, max_folds, protein = handle_parameters()
if bench == True:
if hide == 0:
cProfile.run("profile( 0 )")
elif hide == 1:
cProfile.run("profile( 1 )")
else:
cProfile.run("profile( 2 )")
sys.exit(0)
best_fold = fold(protein, max_folds, hide)
protein = best_fold[1]
score = best_fold[0]
draw_protein(protein)
print("Score",score)
if __name__=="__main__":
main()