-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1118 lines (936 loc) · 43.7 KB
/
app.py
File metadata and controls
1118 lines (936 loc) · 43.7 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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import queue
import re
import subprocess
import threading
import time
import traceback
import uuid
import yaml
import zipfile
from datetime import datetime
from flask import Flask, render_template, send_file, Response, request, jsonify, stream_with_context
from flask_cors import CORS
from src.config import config_manager
from src.envCheck.checkRequirement import check_requirement
from src.utils.batch_status_manager import batch_status_manager, BatchStatus
from src.llm.llm_pipeline import LLMAnalysisPipeline
app = Flask(__name__)
CORS(app)
app.debug = True # set debug flag to True
# 為了遷移 WebSocket 到 SSE,我們需要儲存活躍的進程和它們的輸出隊列
# 格式: {batch_id: {'process': process, 'queue': queue, 'last_activity': timestamp}}
active_processes = {}
# 設置 batch_status_manager 的 active_processes 參考以支援 SSE 串流
batch_status_manager.set_active_processes(active_processes)
# 清理閒置進程的時間閾值(30分鐘)
IDLE_THRESHOLD = 30 * 60 # 秒
def enqueue_output(proc, output_queue):
"""
從子進程的輸出流讀取數據並放入隊列
"""
try:
for line in iter(proc.stdout.readline, ''):
if line: # 避免處理空行
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output_queue.put(f"[{timestamp}] {line.rstrip()}")
except (BrokenPipeError, IOError) as e:
# 處理管道錯誤,通常發生在進程被終止時
output_queue.put(f"[警告] 輸出流中斷: {str(e)}")
finally:
# 確保在退出前關閉流
try:
if proc.stdout:
proc.stdout.close()
except:
pass
def clean_idle_processes():
"""
清理長時間閒置的進程
"""
current_time = time.time()
to_remove = []
for batch_id, process_info in active_processes.items():
if current_time - process_info['last_activity'] > IDLE_THRESHOLD:
to_remove.append(batch_id)
for batch_id in to_remove:
del active_processes[batch_id]
def remove_ansi_escape_sequences(text):
"""
@brief Remove ANSI escape sequences from the given text (used by tqdm).
@param text: The text containing ANSI escape sequences.
@return: The text with ANSI escape sequences removed.
"""
ansi_escape = re.compile(r'(?:\x1B[@-_][0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)
@app.route('/stream/<batch_id>')
def stream(batch_id):
"""
SSE端點,用於串流程序輸出
"""
# 檢查批次是否存在
if batch_id not in active_processes:
return jsonify({
'status': 'error',
'message': f'批次 {batch_id} 不存在或已完成'
}), 404
def generate():
process_info = active_processes[batch_id]
q = process_info['queue']
process_info['last_activity'] = time.time() # 更新最後活動時間
# 發送初始連接確認
yield f"data: [INFO] 已連接到批次 {batch_id} 的數據流\n\n"
# 持續監控隊列,直到進程結束
while True:
try:
# 非阻塞獲取,最多等待0.5秒
try:
line = q.get(timeout=0.5)
clean_line = remove_ansi_escape_sequences(line)
# 直接發送訊息,使用正確的 SSE 格式
yield f"data: {clean_line}\n\n"
process_info['last_activity'] = time.time() # 更新最後活動時間
except queue.Empty:
# 檢查進程是否已結束
if process_info['process'].poll() is not None:
exit_code = process_info['process'].poll()
# 檢查批次狀態以提供更精確的訊息
batch_status = batch_status_manager.get_batch_status(
batch_id)
if batch_status and batch_status['status'] == BatchStatus.FAILED.value:
# 如果批次執行失敗
yield f"data: [ERROR] 批次 {batch_id} 處理失敗\n\n"
elif exit_code == 0:
yield f"data: [INFO] 處理完成!批次 {batch_id}\n\n"
else:
yield f"data: [ERROR] 處理過程發生錯誤,退出碼:{exit_code}\n\n"
yield f"data: [INFO] 結果保存在 {process_info.get('result_path', 'data/result/')} 目錄\n\n"
yield f"data: [END] 串流結束\n\n"
break
except Exception as e:
# 發生錯誤,發送錯誤訊息並繼續
error_msg = f"[ERROR] 串流處理時出現錯誤: {str(e)}"
print(error_msg)
yield f"data: {error_msg}\n\n"
time.sleep(1) # 防止錯誤循環過快
# 確保正確設置 SSE 響應頭
headers = {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no', # 對 Nginx 代理很重要
'Retry-After': '30000' # 告訴客戶端連接斷開後等待30秒再重連
}
return Response(stream_with_context(generate()), mimetype="text/event-stream", headers=headers)
@app.route('/run-procedure', methods=['POST'])
def run_procedure_http():
"""
HTTP端點,用於啟動PowerBarcoder流程
"""
# 清理閒置進程
clean_idle_processes()
# 獲取表單數據
form_data = request.json
# 檢查是否指定了batch_id
if form_data is not None and 'batch_id' in form_data and form_data['batch_id']:
batch_id = form_data['batch_id']
override_config = form_data.get('override_config', False)
path_manager = config_manager.PathManager(batch_id)
if path_manager.result_dir is None:
return jsonify({
'status': 'error',
'message': f'找不到 result_dir,請確認 batch_id: {batch_id} 的路徑設定'
}), 500
config_file_path = os.path.join(path_manager.result_dir, "config.yml")
if not os.path.exists(config_file_path) or override_config:
# 若不存在config或有勾選強制覆蓋,則寫入新config
config_manager.save_config(
batch_id, form_data, override=override_config)
print(
f"[INFO] 寫入/覆蓋 config.yml (override_config={override_config}) for batch_id: {batch_id}", flush=True)
else:
print(f"使用指定的批次ID: {batch_id},讀取現有配置文件 (未覆蓋)", flush=True)
else:
# 使用者未指定batch_id,按原有流程生成
# 生成批次ID(使用時間戳)
batch_id = datetime.now().strftime("%Y%m%d%H%M")
# 檢查是否已有同名批次運行
if batch_id in active_processes:
# 使用UUID創建唯一的批次ID
temp_uuid = str(uuid.uuid4())[:8]
batch_id = f"{batch_id}_{temp_uuid}"
# 保存配置文件
config_manager.save_config(batch_id, form_data)
# 創建輸出隊列
output_queue = queue.Queue()
# 準備執行命令
path_manager = config_manager.PathManager(batch_id)
# 設置環境變數以禁用輸出緩衝
env = os.environ.copy()
env['PYTHONUNBUFFERED'] = '1'
if path_manager.src_dir is None:
error_message = "找不到 src_dir,請確認 batch_id: {} 的路徑設定".format(batch_id)
print(f"[ERROR] {error_message}")
return jsonify({
'status': 'error',
'message': error_message
}), 500
if os.name == 'nt': # Windows
python_cmd = 'python'
script_path = os.path.join(
path_manager.src_dir, 'src', 'powerBarcode.py')
cmd = [python_cmd, '-u', script_path, batch_id] # 添加 -u 參數強制無緩衝
use_shell = False
else: # Unix
cmd = f'cd {os.path.join(path_manager.src_dir, "src")} && PYTHONUNBUFFERED=1 python3 -u powerBarcode.py {batch_id} 2>&1'
use_shell = True
try:
# 啟動子進程,設置 bufsize=0 強制無緩衝
proc = subprocess.Popen(cmd, shell=use_shell, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
bufsize=0, universal_newlines=True, encoding='utf-8', errors='replace', env=env)
# 創建並啟動讀取線程
read_thread = threading.Thread(
target=enqueue_output, args=(proc, output_queue))
read_thread.daemon = True # 守護線程,確保主程序退出時終止
read_thread.start()
# 計算結果路徑
if path_manager.data_dir is None:
error_message = "找不到 data_dir,請確認 batch_id: {} 的路徑設定".format(
batch_id)
print(f"[ERROR] {error_message}")
return jsonify({
'status': 'error',
'message': error_message
}), 500
result_path = os.path.join(path_manager.data_dir, "result", batch_id)
# 存儲進程信息
active_processes[batch_id] = {
'process': proc,
'queue': output_queue,
'thread': read_thread,
'last_activity': time.time(),
'result_path': result_path,
'start_time': datetime.now().isoformat()
}
# 初始化批次狀態 (狀態管理器內部會處理重複初始化的情況)
batch_status_manager.initialize_batch(batch_id)
# 返回批次ID
return jsonify({
'status': 'success',
'message': f'處理已啟動,批次ID: {batch_id}',
'batch_id': batch_id
})
except Exception as e:
error_message = f"啟動進程時出錯: {str(e)}"
print(f"[ERROR] {error_message}")
return jsonify({
'status': 'error',
'message': error_message
}), 500
@app.route('/batch-status/<batch_id>')
def get_batch_status_http(batch_id):
"""
獲取批次處理的狀態信息
"""
# 從狀態管理器獲取批次狀態
status = batch_status_manager.get_batch_status(batch_id)
if not status:
return jsonify({
'status': 'error',
'message': f'找不到批次 {batch_id} 的狀態信息'
}), 404
# 檢查進程是否仍在運行 (僅當批次仍在活躍列表中)
is_process_running = False
if batch_id in active_processes:
proc = active_processes[batch_id]['process']
is_process_running = proc.poll() is None
# 增加進程狀態信息
status['process_running'] = is_process_running
return jsonify(status)
@app.route('/')
def home():
"""
@brief Render the home page with default values for the form.
@return: The rendered home page template.
"""
# Get path manager with environment variables
path_manager = config_manager.PathManager()
return render_template('index.html',
# Path
default_mycutadapt_path="/venv/cutadapt-venv/bin/",
default_myfastp_path="/usr/local/bin/",
default_localblast_tool_dir="/usr/local/bin/",
default_data_dir=os.path.join(
path_manager.data_dir or ""),
default_result_data_path=os.path.join(
path_manager.data_dir, "result") if path_manager.data_dir else "",
default_miss_list=os.path.join(
path_manager.data_dir or "", "missingList.txt"),
default_r1_fastq_gz="amplicon_data/RUN_pool_R1.fastq.gz",
default_r2_fastq_gz="amplicon_data/RUN_pool_R2.fastq.gz",
default_summary_json_file_name="summary.json",
default_summary_html_file_name="summary.html",
default_dada2_learn_error_file=os.path.join(
"dada2LearnErrorFile"),
default_barcode_matrix_file="amplicon_data/multiplex_cpDNAbarcode_clean_raw.txt",
default_denoise_mode=0,
dev_mode=0,
amplicon_minimum_length=1,
# LLM
llm_analysis_enabled=False,
llm_model='mistralai/magistral-small-2509',
llm_api_key=path_manager.llm_api_key or '',
llm_base_url='http://100.64.86.127:1234/v1',
llm_batch_size=1,
# 序列萃取
enable_debug_sequence_extraction=False,
top_k_sequences=3,
# Locus (rbcL demo)
rbcl_name_of_loci="rbcLN",
rbcl_error_rate_cutadapt=0.125,
rbcl_minimum_length_cutadapt=70,
rbcl_primer_f="GAGACTAAAGCAGGTGTTGGATTCA",
rbcl_primer_f_name="fVGF",
rbcl_primer_r="TCAAGTCCACCRCGAAGRCATTC",
rbcl_primer_r_name="rECL",
rbcl_barcodes_file1="amplicon_data/barcodes_rbcL_start_0.fasta",
rbcl_barcodes_file2="amplicon_data/barcodes_rbcLN_start2_0.fasta",
rbcl_sseqid_file_name="amplicon_data/fermalies_rbcL.fasta",
rbcl_minimum_length_cutadapt_in_loop=150,
rbcl_customized_core_number=30,
rbcl_blast_read_choosing_mode=1,
rbcl_blast_parsing_mode=2,
rbcl_blast_top_k_abundance=100, # 進階設定:只保留前 k 名豐度的 ASV,決定 merger 要拿多少 ASVs 來比
# rbcL is a conserved gene, so we can use a smaller overlap
rbcl_minimum_overlap_base_pair=4,
# rbcL is a conserved gene, so no mismatch is allowed
rbcl_maximum_mismatch_base_pair=0,
rbcl_top_k_identical_dada2_merge=3, # 進階設定:QC 模組的 identical_to_topK_dada2_merge 的 K 值,決定 dada2 要拿多少 ASVs 來比
# Merger ablation study options
rbcl_enable_4way_alignment=True,
rbcl_enable_overlap_realign=True,
rbcl_enable_fake_gap_correction=True,
rbcl_enable_sliding_extension=True,
# Locus (trnLF demo)
trnlf_name_of_loci="trnLF",
trnlf_error_rate_cutadapt=0.125,
trnlf_minimum_length_cutadapt=70,
trnlf_primer_f="TGAGGGTTCGANTCCCTCTA",
trnlf_primer_f_name="L5675",
trnlf_primer_r="GGATTTTCAGTCCYCTGCTCT",
trnlf_primer_r_name="F4121",
trnlf_barcodes_file1="amplicon_data/barcodes_trnL_3exonSTART_0.fasta",
trnlf_barcodes_file2="amplicon_data/barcodes_trnF_0.fasta",
trnlf_sseqid_file_name="amplicon_data/ftol_sanger_alignment_trnLLF_full_f.fasta",
trnlf_minimum_length_cutadapt_in_loop=150,
trnlf_customized_core_number=30,
trnlf_blast_read_choosing_mode=1, # 0: r1 r2 cat後blast, 1: r1 r2分開blast
trnlf_blast_parsing_mode=2,
trnlf_blast_top_k_abundance=100, # 進階設定:只保留前 k 名豐度的 ASV,決定 merger 要拿多少 ASVs 來比
# trnLF is a non-coding region, so we can use a larger overlap
trnlf_minimum_overlap_base_pair=12,
# trnLF is a non-coding region, 0 might be too strict, adjust as needed
trnlf_maximum_mismatch_base_pair=0,
trnlf_top_k_identical_dada2_merge=3, # 進階設定:QC 模組的 identical_to_topK_dada2_merge 的 K 值,決定 dada2 要拿多少 ASVs 來比
# Merger ablation study options
trnlf_enable_4way_alignment=True,
trnlf_enable_overlap_realign=True,
trnlf_enable_fake_gap_correction=True,
trnlf_enable_sliding_extension=True
)
@app.route('/download/<room_name>', methods=['GET'])
def download_result(room_name):
"""
@brief Download result from a specific room by loci.
本功能將下載每個 locus 的 {locus}_qcReport.csv 及 extracted_sequences 目錄。
同時也會包含 llm_analysis 目錄內的所有 LLM 分析結果。
@param room_name The name of the room.
@return The ZIP file containing the result folders.
@exception Exception If an error occurs during the process.
"""
# List all result folders in room_name folder
path_manager = config_manager.PathManager()
if path_manager.data_dir is None:
return jsonify({
'status': 'error',
'message': '找不到 data_dir,請確認環境設定'
}), 500
room_name_folder = os.path.join(path_manager.data_dir, "result", room_name)
result_folders = os.listdir(room_name_folder)
# Get only the folders, not files
result_folders = [f for f in result_folders if os.path.isdir(
os.path.join(room_name_folder, f))]
# Create a zip file
zip_file_name = f'{room_name}_best_seq.zip'
zip_file_path = os.path.join(room_name_folder, zip_file_name)
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
for folder in result_folders:
locus_name = folder.split('_')[0] # 文件夾名稱格式為 {locus}_result
# Include extracted_sequences folder contents (取代 validator)
extracted_seq_folder = os.path.join(
room_name_folder, folder, 'qcResult', 'extracted_sequences')
for root, dirs, files in os.walk(extracted_seq_folder):
for file in files:
src_file = os.path.join(root, file)
rel_path = os.path.relpath(src_file, extracted_seq_folder)
zipf.write(src_file, os.path.join(
folder, 'extracted_sequences', rel_path))
# Include '{locus}_qcReport.csv' file
qc_report_file = os.path.join(
room_name_folder, folder, 'qcResult', f'{locus_name}_qcReport.csv')
if os.path.exists(qc_report_file):
zipf.write(qc_report_file, os.path.join(
folder, f'{locus_name}_qcReport.csv'))
# Include recommended sequences file
recommended_sequences_file = os.path.join(
room_name_folder, folder, 'qcResult', f'{locus_name}_recommended_sequences.fas')
if os.path.exists(recommended_sequences_file):
zipf.write(recommended_sequences_file, os.path.join(
folder, f'{locus_name}_recommended_sequences.fas'))
# Include llm_analysis folder contents
llm_analysis_folder = os.path.join(
room_name_folder, folder, 'llm_analysis')
if os.path.exists(llm_analysis_folder):
for root, dirs, files in os.walk(llm_analysis_folder):
for file in files:
src_file = os.path.join(root, file)
rel_path = os.path.relpath(src_file, llm_analysis_folder)
zipf.write(src_file, os.path.join(
folder, 'llm_analysis', rel_path))
# Download the zip file
return send_file(zip_file_path, as_attachment=True)
@app.route('/analyze-sequence-stream', methods=['GET'])
def analyze_sequence_stream():
"""
提供 streaming 回應的序列分析 API
使用 Server-Sent Events (SSE) 提供即時分析進度
"""
import json
try:
# 獲取請求參數
batch_id = request.args.get('batch_id')
locus = request.args.get('locus')
identifier_type = request.args.get('identifier_type')
identifier_value = request.args.get('identifier_value')
# 參數驗證
if not all([batch_id, locus, identifier_type, identifier_value]):
def error_generator():
yield f"data: {json.dumps({'type': 'error', 'message': 'Missing required parameters'})}\n\n"
return Response(error_generator(), mimetype='text/event-stream')
def generate_analysis_stream():
try:
# 進度報告
yield f"data: {json.dumps({'type': 'status', 'message': 'Initializing analysis...', 'progress': 10})}\n\n"
# 設置路徑管理器
from src.config.config_manager import PathManager, load_config
# 驗證必要參數
if not batch_id or not locus or not identifier_value:
yield f"data: {json.dumps({'type': 'error', 'message': 'Missing required parameters'})}\n\n"
return
path_manager = PathManager(batch_id)
if not path_manager.data_dir:
yield f"data: {json.dumps({'type': 'error', 'message': f'Data directory not found for batch {batch_id}'})}\n\n"
return
result_data_path = os.path.join(path_manager.data_dir, "result", batch_id)
if not os.path.exists(result_data_path):
yield f"data: {json.dumps({'type': 'error', 'message': f'Result directory does not exist for batch {batch_id}'})}\n\n"
return
yield f"data: {json.dumps({'type': 'status', 'message': 'Loading sequence data...', 'progress': 30})}\n\n"
# 處理 barcode_id 轉 sample_id
if identifier_type == 'barcode_id':
from src.qc.qc_data_manager import QCDataManager
qc_manager = QCDataManager(locus, result_data_path)
sample_mappings = qc_manager.get_sample_mappings()
sample_id = sample_mappings.get(identifier_value)
if not sample_id:
yield f"data: {json.dumps({'type': 'error', 'message': f'Sample not found for barcode {identifier_value}'})}\n\n"
return
else:
sample_id = identifier_value
# 載入配置
config = load_config(batch_id)
config.resultDataPath = result_data_path
llm_model = config.llm_model or "未設定"
llm_base_url = config.llm_base_url or "未設定"
api_key_length = len(config.llm_api_key or "")
api_key_preview = (
f"{config.llm_api_key[:4]}***{config.llm_api_key[-4:]}"
if config.llm_api_key and len(config.llm_api_key) >= 8
else (config.llm_api_key[:2] + "***" if config.llm_api_key else "<空>")
)
print(
f"[LLM][App] llm_analysis_enabled={config.llm_analysis_enabled}, 模型={llm_model}, 基礎網址={llm_base_url}"
)
print(
f"[LLM][App] LLM API Key 長度={api_key_length}, 預覽={api_key_preview}"
)
if not config.llm_analysis_enabled:
print("[LLM][App] LLM 分析功能未啟用,停止分析流程。")
yield f"data: {json.dumps({'type': 'error', 'message': 'LLM 分析功能未啟用,請於設定中開啟'})}\n\n"
return
yield f"data: {json.dumps({'type': 'status', 'message': 'Sending LLM request...', 'progress': 50})}\n\n"
# 使用 streaming 分析
yield f"data: {json.dumps({'type': 'status', 'message': 'Starting streaming analysis...', 'progress': 60})}\n\n"
# 嘗試使用真正的 streaming 分析
if not locus or not sample_id:
yield f"data: {json.dumps({'type': 'error', 'message': 'Missing locus or sample_id parameter'})}\n\n"
return
try:
# 初始化 LLMAnalysisPipeline
pipeline = LLMAnalysisPipeline(config)
# 使用 pipeline 的 streaming 分析方法
print(f"[LLM][App] 開始 streaming 分析: locus={locus}, sample_id={sample_id}")
# 調用 pipeline 的 streaming 方法並逐事件轉發
for event in pipeline.run_single_locus_analysis_streaming(locus, sample_id):
yield f"data: {json.dumps(event)}\n\n"
except Exception as analysis_error:
yield f"data: {json.dumps({'type': 'error', 'message': f'Streaming analysis failed: {str(analysis_error)}'})}\n\n"
return
except Exception as e:
yield f"data: {json.dumps({'type': 'error', 'message': f'Error occurred during analysis: {str(e)}'})}\n\n"
return Response(generate_analysis_stream(), mimetype='text/event-stream', headers={
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*'
})
except Exception as e:
def error_generator():
yield f"data: {json.dumps({'type': 'error', 'message': f'Initialization error: {str(e)}'})}\n\n"
return Response(error_generator(), mimetype='text/event-stream')
@app.route('/download-analysis-report', methods=['POST'])
def download_analysis_report():
"""
統一的報告下載 API,從持久目錄讀取 majority_vote_summary.json 並生成報告
Request JSON:
{
"batch_id": "202508091806",
"locus": "trnLF",
"sample_id": "Christella_jaculosa_Wade3964_KTHU2097"
}
Returns:
報告檔案(Markdown 或 JSON)
"""
import json
try:
data = request.get_json()
batch_id = data.get('batch_id')
locus = data.get('locus')
sample_id = data.get('sample_id')
if not all([batch_id, locus, sample_id]):
return jsonify({
'status': 'error',
'message': '缺少必要參數 (batch_id, locus, sample_id)'
}), 400
# 使用持久目錄讀取 majority_vote_summary.json
from src.path.path_manager import PowerBarcoderResultPathManager
from src.config.config_manager import load_config
# 載入配置以獲取 resultDataPath
config = load_config(batch_id)
# 設置路徑管理器
result_path_manager = PowerBarcoderResultPathManager(
result_data_path=config.resultDataPath,
locus=locus
)
llm_analysis_dir = result_path_manager.get_llm_analysis_root_path()
# 讀取 majority_vote_summary.json
summary_path = os.path.join(llm_analysis_dir, f"{sample_id}_majority_vote_summary.json")
if not os.path.exists(summary_path):
return jsonify({
'status': 'error',
'message': f'找不到分析結果文件: {sample_id}_majority_vote_summary.json'
}), 404
try:
with open(summary_path, 'r', encoding='utf-8') as f:
summary_data = json.load(f)
except Exception as e:
return jsonify({
'status': 'error',
'message': f'讀取 majority_vote_summary.json 失敗: {str(e)}'
}), 500
# 下載端改為唯讀:優先提供已存在的 Markdown 報告,其次回傳 JSON 摘要內容
print(f"[報告下載] 讀取現有報告,批次: {batch_id}, 基因座: {locus}, 樣本: {sample_id}")
# 1) 嘗試尋找已存在的 Markdown 報告(按照慣例命名 llm_single_analysis_*)
md_candidates = []
try:
for fname in os.listdir(llm_analysis_dir):
if fname.startswith(f"llm_single_analysis_{sample_id}_") and fname.endswith(".md"):
md_candidates.append(os.path.join(llm_analysis_dir, fname))
except Exception:
md_candidates = []
if md_candidates:
# 選擇最新一份(依檔名時間戳排序)
md_candidates.sort(reverse=True)
latest_md = md_candidates[0]
with open(latest_md, 'r', encoding='utf-8-sig') as f:
content = f.read()
filename = os.path.basename(latest_md)
return Response(
content,
mimetype='text/markdown',
headers={
'Content-Disposition': f'attachment; filename="{filename}"',
'Content-Type': 'text/markdown; charset=utf-8'
}
)
# 2) 若無 Markdown,直接回傳 majority_vote_summary.json 的內容(唯讀,不生成新檔)
return Response(
json.dumps(summary_data, ensure_ascii=False, indent=2),
mimetype='application/json',
headers={
'Content-Disposition': f'attachment; filename="{sample_id}_majority_vote_summary.json"',
'Content-Type': 'application/json; charset=utf-8'
}
)
except Exception as e:
print(f"[報告下載] 錯誤: {e}")
import traceback
traceback.print_exc()
return jsonify({
'status': 'error',
'message': f'報告生成失敗: {str(e)}'
}), 500
@app.route('/get-available-samples/<batch_id>/<locus>', methods=['GET'])
def get_available_samples(batch_id, locus):
"""
獲取指定批次和基因座的可用樣本列表
回傳格式:
{
"status": "success",
"data": {
"samples": [
{"sample_id": "sample_001", "barcode_id": "A01"},
{"sample_id": "sample_002", "barcode_id": "A02"}
]
}
}
"""
try:
# 設置路徑管理器
path_manager = config_manager.PathManager(batch_id)
if not path_manager.data_dir:
return jsonify({
'status': 'error',
'message': f'找不到批次 {batch_id} 的資料目錄'
}), 404
result_data_path = os.path.join(path_manager.data_dir, "result", batch_id)
if not os.path.exists(result_data_path):
return jsonify({
'status': 'error',
'message': f'批次 {batch_id} 的結果目錄不存在'
}), 404
# 獲取樣本列表
from src.qc.qc_data_manager import QCDataManager
qc_manager = QCDataManager(locus, result_data_path)
# 獲取所有樣本的 barcode 和 sample_id 對應關係
sample_mappings = qc_manager.get_sample_mappings()
samples = [
{
"barcode_id": barcode_id,
"sample_id": sample_id
}
for barcode_id, sample_id in sample_mappings.items()
]
return jsonify({
'status': 'success',
'data': {
'samples': samples,
'count': len(samples)
}
})
except Exception as e:
return jsonify({
'status': 'error',
'message': f'獲取樣本列表時發生錯誤: {str(e)}'
}), 500
# health check
@app.route('/health')
def health():
"""
@brief Health check endpoint.
@return: 'OK' if the server is running.
"""
return 'OK'
@app.route('/config-uploader')
def config_uploader():
"""
提供配置上傳器頁面
"""
import os
static_file_path = os.path.join(app.root_path, 'static', 'config_uploader.html')
return send_file(static_file_path)
@app.route('/upload-yaml-config', methods=['POST'])
def upload_yaml_config():
"""
上傳 YAML 配置並啟動分析流程
只支援批次隊列模式
"""
try:
print(f"[DEBUG] 收到 YAML 配置上傳請求")
data = request.json
if not data:
return jsonify({
'status': 'error',
'message': '未提供請求數據'
}), 400
# 獲取配置文件列表
configs = data.get('configs', []) # 批次模式的配置列表
if not configs:
return jsonify({
'status': 'error',
'message': '未提供 YAML 配置'
}), 400
# 提取 YAML 內容
yaml_contents = [config['content'] for config in configs]
print(f"[DEBUG] 批次模式, 配置數量: {len(yaml_contents)}")
# 提取額外參數
batch_interval = data.get('batch_interval', 0)
execution_order = data.get('execution_order', 'sequential')
return handle_batch_yaml_upload(yaml_contents, batch_interval, execution_order, configs)
except yaml.YAMLError as e:
return jsonify({
'status': 'error',
'message': f'YAML 解析錯誤: {str(e)}'
}), 400
except Exception as e:
return jsonify({
'status': 'error',
'message': f'處理配置時出錯: {str(e)}'
}), 500
def handle_batch_yaml_upload(yaml_contents, batch_interval=0, execution_order='sequential', configs=None):
"""處理多檔 YAML 上傳,加入批次隊列"""
try:
# 準備配置列表
config_dicts = []
priorities = []
original_filenames = []
# 根據執行順序排序
indexed_contents = list(enumerate(yaml_contents))
if execution_order == 'alphabetical' and configs:
# 按檔名排序
indexed_contents = sorted(indexed_contents, key=lambda x: configs[x[0]]['filename'])
elif execution_order == 'size' and configs:
# 按內容長度排序
indexed_contents = sorted(indexed_contents, key=lambda x: len(x[1]))
# 'sequential' 保持原順序
for order_index, (original_index, yaml_content) in enumerate(indexed_contents):
# 解析配置
config_dict = yaml.safe_load(yaml_content)
# 獲取原始檔名(使用 original_index 來訪問 configs,因為 indexed_contents 可能被重新排序)
original_filename = configs[original_index]['filename'] if configs else f"config_{original_index}"
# 添加元數據到配置
config_dict['_metadata'] = {
'original_filename': original_filename,
'batch_interval': batch_interval,
'execution_order': execution_order
}
config_dicts.append(config_dict)
priorities.append(order_index)
original_filenames.append(original_filename)
# 使用整合的批次狀態管理器添加到隊列
queue_ids = batch_status_manager.add_configs_to_queue(config_dicts, priorities, original_filenames)
# 生成基礎時間戳作為 queue_id
base_timestamp = datetime.now().strftime("%Y%m%d%H%M")
# 從 queue_ids 提取 batch_ids
batch_ids = []
for queue_id in queue_ids:
# 假設 queue_id 格式為 "queue_YYYYMMDD_HHMMSS_XXX"
# 對應的 batch_id 格式為 "batch_YYYYMMDD_HHMMSS_XXX"
batch_id = queue_id.replace('queue_', 'batch_')
batch_ids.append(batch_id)
return jsonify({
'status': 'success',
'message': f'成功添加 {len(config_dicts)} 個批次到執行隊列',
'batch_ids': batch_ids,
'queue_id': base_timestamp
})
except Exception as e:
print(f"[ERROR] 處理批次隊列時出錯: {e}")
return jsonify({
'status': 'error',
'message': f'處理批次隊列時出錯: {str(e)}'
}), 500
@app.route('/batch-queue-status', methods=['GET'])
def get_batch_queue_status_api():
"""獲取批次佇列狀態 - 直接從SQLite讀取以確保狀態一致性"""
try:
queue_status = batch_status_manager.get_queue_status()
return jsonify({
'status': 'success',
'queue_status': queue_status
})
except Exception as e:
return jsonify({
'status': 'error',
'message': f'獲取佇列狀態時出錯: {str(e)}'
}), 500
@app.route('/reload-queue-state', methods=['POST'])
def reload_queue_state_api():
"""重新載入隊列狀態資料庫"""
try:
# 獲取當前狀態(直接從SQLite讀取)
queue_status = batch_status_manager.get_queue_status()
return jsonify({
'status': 'success',
'message': '隊列狀態已重新載入',
'queue_status': queue_status
})
except Exception as e:
return jsonify({
'status': 'error',
'message': f'重新載入失敗: {str(e)}'
}), 500
@app.route('/delete-batch-item', methods=['POST'])
def delete_batch_item_api():
"""刪除指定的批次項目"""
try:
data = request.get_json()
queue_id = data.get('queue_id')
print(f"[DEBUG] 收到刪除請求: queue_id={queue_id}")
if not queue_id:
print("[ERROR] 缺少 queue_id 參數")
return jsonify({
'status': 'error',
'message': '缺少 queue_id 參數'
}), 400
# 刪除隊列項目
success = batch_status_manager.delete_queue_item(queue_id)
if success:
print(f"[INFO] 批次項目刪除成功: {queue_id}")
# 刪除成功後,立即從SQLite重新讀取最新的狀態
updated_status = batch_status_manager.get_queue_status()
return jsonify({
'status': 'success',
'message': '批次項目已刪除',
'queue_status': updated_status # 返回最新的狀態
})
else:
print(f"[WARNING] 找不到要刪除的批次項目: {queue_id}")
return jsonify({
'status': 'error',
'message': '找不到指定的批次項目'
}), 404
except Exception as e:
print(f"[ERROR] 刪除批次項目時發生錯誤: {str(e)}")
return jsonify({
'status': 'error',
'message': f'刪除失敗: {str(e)}'
}), 500
@app.route('/start-processing', methods=['POST'])
def start_processing_api():
"""開始處理下一個隊列項目"""
try:
# 處理下一個隊列項目
batch_id = batch_status_manager.start_processing()
if batch_id:
print(f"[INFO] 已開始處理隊列項目: {batch_id}")
return jsonify({
'status': 'success',
'message': f'已開始處理批次: {batch_id}',
'batch_id': batch_id
})
else:
print("[INFO] 沒有待處理的隊列項目")
return jsonify({
'status': 'no_items',
'message': '沒有待處理的隊列項目'
})
except Exception as e:
print(f"[ERROR] 處理隊列項目時發生錯誤: {str(e)}")