-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaitd_c.py
583 lines (521 loc) · 27.5 KB
/
aitd_c.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import os, sys
import aitd
import json
import pickle
import yaml
import zipfile
from colorama import Fore, Back, Style
argv = sys.argv
programdict = os.path.dirname(os.path.abspath(__file__))
####################
# I'M HERE!!! #
####################
# 以下为所需的接口
def treeModel():
pass
def getName(name): # 获得程序生成的唯一名称
pass
SpeciesList = [...] # 用于存储已经创建的物种
SeqMap = {"":[...]} # 用于将序列和物种一一对应
def saveSetting(projectDict):
with open(projectDict + "setting.json") as setting:
settingData = json.load(setting)
for species in SpeciesList:
for seq in SeqMap[species]:
settingData["sequence_list"]["seq::" + seq] = species
def getNamespace(namespace): # 用于获取命名空间名所在位置
pass
def getObject(str):
with open(programdict + "setting.json") as js:
contents = json.read(js)
#endif // 乱入
helps = """
+-----------------------------------------------------------------+
| AITD System |
| A systematic phylogenetic tree drawing and species evolution |
| path inference system based on |
| specific macromolecular sequence alignment and |
| automatic sequence screening techniques. |
+=================================================================+
| Commands """ + Style.BRIGHT + "(Normal Mode)" + Style.RESET_ALL + """ |
+-----------------+-----------------------------------------------+
| setting | setting <option> <value> |
| new | new <type> <name> [<parameter>] |
| open | open [<name>] |
| debug | debug [on|off] |
| exit | |
+=================+===============================================+
| Commands """ + Fore.CYAN + "(Debug mode)" + Style.RESET_ALL + """ |
+-----------------------------------------------------------------+
| (You can directly run a single line of Python code) |
+=================================================================+
| Commands """ + Fore.MAGENTA + "(Project Mode)" + Style.RESET_ALL + """ |
+-----------------+-----------------------------------------------+
| | |
+=================+===============================================+
| Welcome to AITD System! |
+-----------------------------------------------------------------+
"""
def Fatal(message):
print(
Style.BRIGHT + Fore.WHITE + Back.RED + "[Fatal] %s" % message + Style.RESET_ALL
)
def Error(message):
print(Fore.RED + "[Error] %s" % message + Style.RESET_ALL)
def Warning(message):
print(Fore.YELLOW + "[Warning] %s" % message + Style.RESET_ALL)
def Note(message):
print(Fore.CYAN + "[Note] %s" % message + Style.RESET_ALL)
def reNote(message, end):
print(" %s" % message, end=end)
with open(os.path.join(programdict, "data", "setting.dat"), "rb") as f:
try:
config = pickle.load(f)
except EOFError:
config = {"language": "en"}
with open(os.path.join(programdict, "data", "setting.dat"), "wb") as f:
pickle.dump(config, f)
with open(os.path.join(programdict, "data", "en.yml"), "r", encoding="utf-8") as f:
EnData = yaml.safe_load(f.read())
langData = EnData
def getWord(word):
if word in langData:
return langData[word]
elif word in EnData:
return EnData[word]
else:
Fatal("Unable to find corresponding language information: %s" % word)
exit(-1)
def refresh_setting():
global langData
try:
with open(
os.path.join(programdict, "data", "%s.yml" % config["language"]),
"r",
encoding="utf-8",
) as f:
langData = yaml.safe_load(f.read())
except Exception as e:
Fatal("Unable to load language file: %s.yml" % (config["language"]))
langData = EnData
return False
return True
refresh_setting()
def mkdir(path):
reNote("%s %s..." % (getWord("createdict"), path), end=" ")
try:
os.makedirs(path)
print("done.")
return True
except Exception:
print()
Error("%s : %s" % (getWord("dictnotcreate"), path))
return False
def mkfile(filename, mode="w"):
reNote("%s %s..." % (getWord("createfile"), filename), end=" ")
try:
open(filename, "w").close()
print(getWord("done"))
return True
except Exception:
print()
Error("%s : %s" % (getWord("filenotcreate"), filename))
return False
print(helps)
nowProject = ""
debug = False
while True:
if nowProject == "":
ouInput = 'AITD "%s"> ' % os.getcwd()
else:
ouInput = (
'AITD "%s" ' % os.getcwd()
+ Fore.MAGENTA
+ 'with the project "%s"' % nowProject
+ Style.RESET_ALL
+ ">"
)
if debug:
ouInput = Fore.CYAN + "[DEBUG] " + Style.RESET_ALL + ouInput
oriInput = input(ouInput)
command = oriInput.split()
if len(command) == 0:
continue
elif command[0] == "clear":
os.system("cls")
elif command[0] == "setting":
if len(command) < 3:
Error(getWord("synerr"))
Note("%s : setting <option> <value>" % getWord("usage"))
continue
if command[1] == "language":
config["language"] = command[2]
with open("data/setting.dat", "wb") as f:
pickle.dump(config, f)
if refresh_setting():
Note("%s %s" % (getWord("langset"), config["language"]))
else:
Error(getWord("notsetting"))
elif command[0] == "new":
if len(command) < 3:
Error(getWord("synerr"))
Note("%s : new <type> <name> [<parameter>]" % getWord("usage"))
continue
if command[1] == "project":
Note(getWord("mkpj"))
mkdir(command[2])
mkdir(os.path.join(command[2], "input"))
mkdir(os.path.join(command[2], "plugins"))
mkfile(os.path.join(command[2], "plugins", "plugin_list.dat"))
mkdir(os.path.join(command[2], "data"))
mkdir(os.path.join(command[2], "data", "sequence"))
mkdir(os.path.join(command[2], "data", "alignment"))
mkdir(os.path.join(command[2], "data", "tree"))
mkdir(os.path.join(command[2], "data", "correction"))
mkdir(os.path.join(command[2], "training"))
mkdir(os.path.join(command[2], "output"))
mkdir(os.path.join(command[2], "cache"))
mkdir(os.path.join(command[2], "cache", "sketch"))
mkdir(os.path.join(command[2], "cache", "log"))
mkfile(os.path.join(command[2], "setting.json"))
with open(os.path.join(command[2], "setting.json"), "w") as f:
json.dump({}, f)
mkfile(os.path.join(command[2], "setting.dat"))
Note(getWord("pjcreated"))
else:
Error(getWord("synerr"))
Note("%s : new <type> <name> [<parameter>]" % getWord("usage"))
continue
elif command[0] == "open":
if len(command) == 2:
try:
os.chdir(os.path.join(os.getcwd(), command[1]))
nowProject = command[1]
except FileNotFoundError:
Error(getWord("notpjdict"))
nowProject = ""
continue
else:
nowProject = os.path.split(os.getcwd())[1]
# try:
# nowProject = command[1]
# except IndexError:
# Error(getWord("synerr"))
# Note("%s : open <name>" % getWord("usage"))
# continue
try:
with open(os.path.join("setting.json"), "r") as f:
if json.load(f) == {}:
Warning(getWord("warnbasic"))
except Exception:
Error(getWord("wpj"))
nowProject = ""
continue
elif command[0] == "debug":
if len(command) == 1 or command[1] == "on":
debug = True
elif command[1] == "off":
debug = False
else:
Error(getWord("synerr"))
Note("%s : debug [on|off]" % getWord("usage"))
continue
elif command[0] == "exit":
if nowProject != "":
Note(getWord("exitpj"))
os.chdir(os.path.join(os.getcwd(), ".."))
nowProject = ""
else:
Note(getWord("exit"))
print(
"""
+-----------------------------------------------------------------+
| AITD System |
| Welcome to use this software again! |
+=================================================================+
| Staff |
+------------------------------+----------------------------------+
| Zhu Jingrui (distjr_) | who built the overall project |
| | and wrote most of the code |
+------------------------------+----------------------------------+
| Hu Gaoyuan (hzzzzzzzzzzzzz) | who proposed several |
| | key algorithms |
+------------------------------+----------------------------------+
| Deng Yutong (dyt_dirt) | who conducted AI training |
| | and wrote some code |
+==============================+==================================+
| Credits |
+------------------------------+----------------------------------+
| Bai Zhiyuan (Qemu_Android) | who provided the necessary |
| | computing power for training AI |
+==============================+==================================+
| Copyright 2024 Zhu jingrui, et al |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License.|
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| either express or implied. |
| See the License for the specific language governing permissions |
| and limitations under the License. |
+-----------------------------------------------------------------+
"""
)
exit(0)
elif command[0] == "help":
print(helps)
else:
if debug:
try:
exec(" ".join(command))
except Exception as e:
Error(e)
continue
if nowProject != "":
if command[0] == "import":
fileCommand = oriInput.split("\"")
if len(fileCommand == 3):
files = fileCommand[2].split(" ")
seqName = fileCommand[1]
name = getName(seqName)
seqs = [...]
DefaultParser = getattr(aitd.xerlist.ParserList, "aitd-fasta")
if len(files) == 2:
Par = files[1]
DefaultParser = getattr(aitd.xerlist.ParserList, Par)
aitd.readFile(files[0], DefaultParser, seqs)
for seq in seqs:
if os.path.exists(os.path.join(programdict, "data", "sequence", name + ".seq")):
with open(programdict + name + ".seq", 'w') as f:
f.write(seq.sequence)
else:
with open(programdict + name + ".seq", 'x') as f:
f.write(seq.sequence)
if os.path.exists(os.path.join(programdict, "data", "sequence", name + ".metadata")):
with open(programdict + name + ".metadata", 'w') as f:
f.write(seq.metadata)
else:
with open(programdict + name + ".seq", 'x') as f:
f.write(seq.sequence)
else:
Error(getWord("synerr"))
Note("%s : import <name> <file> [<parser>]" % getWord("usage"))
elif command[0] == "species":
fullCommand = oriInput.split("\"")
if len(fullCommand) == 2:
speciesName = getName(fullCommand[1])
SpeciesList.extend(speciesName)
else:
Error(getWord("synerr"))
Note("%s : species <name>" % getWord("usage"))
elif command[0] == "add":
if len(command) == 3:
if command[1] in SeqMap.keys():
SeqMap[command[1]].extend(command[2])
else:
SeqMap[command[1]] = [...]
SeqMap[command[1]].extend(command[2])
else:
Error(getWord("synerr"))
Note("%s : add <species> <name>" % getWord("usage"))
elif command[0] == "del":
if len(command) == 2:
while True:
c = input(getWord("confirmSpeciesDel"))
if c == 'y':
del SeqMap[command[1]]
break
if c == 'n':
break
if len(command) == 3:
while True:
c = input(getWord("confirmSeqDel"))
if c == 'y':
del SeqMap[command[1]][c]
break
if c == 'n':
break
elif command[0] == "parameter":
if command[1] == "get" and len(command) == 4:
with open(programdict + "setting.json") as js:
output = ""
dicts = json.load(js.read())
if command[2].split("::")[0] == "seq":
output = dicts["sequence_list"][command[2]][command[3]]
elif command[2].split("::")[0] == "ali":
output = dicts["alignment_list"][command[2]][command[3]]
elif command[2].split("::")[0] == "tree":
output = dicts["tree_list"][command[2]][command[3]]
elif command[2].split("::")[0] == "sktch":
output = dicts["sketch_list"][command[2]][command[3]]
print(output)
elif command[1] == "set" and len(command) == 5:
with open(programdict + "setting.json") as js:
dicts = json.load(js.read())
if command[2].split("::")[0] == "seq":
dicts["sequence_list"][command[2]][command[3]] = command[4]
elif command[2].split("::")[0] == "ali":
dicts["alignment_list"][command[2]][command[3]] = command[4]
elif command[2].split("::")[0] == "tree":
dicts["tree_list"][command[2]][command[3]] = command[4]
elif command[2].split("::")[0] == "sktch":
dicts["sketch_list"][command[2]][command[3]] = command[4]
json.dump(dicts, js)
else:
Error(getWord("synerr"))
Note("%s : parameter set <object> <key> <value>" % getWord("usage"))
Note("%s : parameter get <object> <key>" % getWord("usage"))
elif command[0] == "align":
if command[1] == "seq":
defaultComparator = "ComparatorList::needleman-wunsch"
defaultMatrix = "God know what it is"
if len(command) > 4:
for i in range(4, len(command) + 1):
if command[i].split("=")[0] == "comparator":
# comparator
cmp = command[i].split("=")[1]
defaultComparator = cmp
elif command[i].split("=")[0] == "matrix":
# matrix
mat = command[5].split("=")[1]
namespace = getNamespace(cmp.split("::")[0])
defaultMatrix = getattr(namespace, mat)
print("align seq1 and seq2 with cmp and mat")
with open(os.path.join(programdict, "setting.json"), "r") as js:
strSeq1 = command[2].split("::")[1]
strSeq2 = command[3].split("::")[1]
data = json.load(js.read())
file1 = data["sequence_list"][command[2]]["file"]
file2 = data["sequence_list"][command[3]]["file"]
seq1 = seq2 = ""
with open(file1, "r") as f:
seq1 = f.read()
with open(file2, "r") as f:
seq2 = f.read()
score, alignment, distant = getattr(getNamespace(defaultComparator.split("::")[0]), defaultComparator)(seq1, seq2, defaultMatrix)
if os.path.exists(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali")):
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali"), 'w') as ali:
ali.write(alignment[0] + "\n")
ali.write(alignment[1])
else:
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali"), 'x') as ali:
ali.write(alignment[0] + "\n")
ali.write(alignment[1])
if os.path.exists(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali.dat")):
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali.dat"), 'w') as ali:
ali.write(score + "\n" + distant)
else:
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali.dat"), 'x') as ali:
ali.write(score + "\n" + distant)
data["alignment_list"][command[2] + "." + command[3]] = {
"file": repr(os.path.join("data","alignment",strSeq1 + "." + strSeq2 + ".ali")),
"opposing": [
command[2],command[3]
],
"data": repr(os.path.join("data", "alignment", strSeq1 + "." + strSeq2 + ".ali.dat")),
"algorithm": defaultComparator
}
elif command[1] == "species":
pass
# To be continued...
elif command[0] == "tree":
try:
n = int(command[1])
if len(command) > n + 1:
defaultPlanter = getattr(aitd.xerlist.TreePlanterList, "TreePlanterList::UPGMA")
defaultComparator = getattr(aitd.xerlist.ComparatorList, "ComparatorList::needleman-wunsch")
defaultMatrix = "God it"
isSave = True
if len(command) > n + 2:
for i in range(n + 2, len(command) + 1):
if command[i].split("=")[0] == "planter":
planter = command[i].split("=")[1]
namespace = planter.split("::")[0]
defaultPlanter = getattr(getNamespace(namespace), planter)
elif command[i].split("=")[0] == "comparator":
cmp = command[i].split("=")[1]
namespace = getNamespace(cmp.split("::")[0])
defaultComparator = getattr(namespace, cmp)
elif command[i].split("=")[0] == "matrix":
mat = command[5].split("=")[1]
namespace = getNamespace(cmp.split("::")[0])
defaultMatrix = getattr(namespace, mat)
elif command[i].split("=")[0] == "savealign":
isSave = (command[i].split("=")[1].lower() == "true")
sequenceList = [aitd.Sequence("", "", "")]
disMatrix = [[...],...]
name = ""
with open(os.path.join(programdict, "setting.json")) as js:
data = json.load(js.read())
for i in range(2, n + 2):
seqFile = data["sequence_list"][command[i]]["file"]
name += command[i] + "."
with open(seqFile,'r') as file:
sequenceList.append(aitd.Sequence("gene", command[i], file.read()))
for j in range(i, n + 2):
file1 = data["sequence_list"][command[i]]["file"]
file2 = data["sequence_list"][command[j]]["file"]
seq1 = seq2 = ""
with open(file1, 'r') as file:
seq1 = file.read()
with open(file2, 'r') as file:
seq2 = file.read()
score, alignment, distant = defaultComparator(seq1, seq2, defaultMatrix)
disMatrix[i - 1][j - 1] = distant
if isSave:
if os.path.exists(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali")):
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali"), 'w') as ali:
ali.write(alignment[0] + "\n")
ali.write(alignment[1])
else:
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali"), 'x') as ali:
ali.write(alignment[0] + "\n")
ali.write(alignment[1])
if os.path.exists(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali.dat")):
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali.dat"), 'w') as ali:
ali.write(score + "\n" + distant)
else:
with open(os.path.join(programdict, "data", "alignment", command[2] + "." + command[3] + ".ali.dat"), 'x') as ali:
ali.write(score + "\n" + distant)
data["alignment_list"][command[2] + "." + command[3]] = {
"file": repr(os.path.join("data","alignment",strSeq1 + "." + strSeq2 + ".ali")),
"opposing": [
command[2],command[3]
],
"data": repr(os.path.join("data", "alignment", strSeq1 + "." + strSeq2 + ".ali.dat")),
"algorithm": defaultComparator
}
lis1, lis2 = aitd.UPGMA(sequenceList, disMatrix)
with open(os.path.join(programdict, "data", "tree", name + "tree"), 'wb') as file:
pickle.dump((lis1,lis2), file)
except:
Error(getWord("synerr"))
Note("%s : tree <seqNum> <seq1> <seq2> <seq3>... [<parameter>]" % getWord("usage"))
elif command[0] == "correct":
try:
namespace = getNamespace(command[1].split("::")[0])
model = getattr(namespace, command[1])
n = int(command[3])
pass
except:
pass
elif command[0] == "list":
with open(os.path.join(programdict, "setting.json"), 'r') as js:
data = json.load(js.read())
if command[1] == "seq":
for key, val in data["sequence_list"].items():
print(key, end=": ")
print(val["name"])
elif command[1] == "ali":
pass
################################################################
# HERE!!!!!!!! #
################################################################
continue
Error(getWord("invcmd"))
continue