-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_paml_multiline.py
More file actions
82 lines (63 loc) · 2.72 KB
/
sort_paml_multiline.py
File metadata and controls
82 lines (63 loc) · 2.72 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
def sort_paml_multiline(input_file, output_file=None):
"""
读取 '顺序格式 + 多行序列' 的 PAML/PHYLIP 文件,
将每个物种序列拼成一行,然后按物种名排序,写出到 output_file(顺序格式)。
"""
if output_file is None:
output_file = input_file + ".sorted"
with open(input_file, 'r') as f:
lines = [line.rstrip('\n') for line in f]
# 第1行: N(物种数), L(序列长度)
first_line = lines[0].strip()
N, L = map(int, first_line.split())
# 准备一个字典存放 {species_name: sequence_str}
seq_dict = {}
current_line_index = 1 # 从第2行开始
for i in range(N):
# 1) 读物种名称(跳过空行等)
while not lines[current_line_index].strip():
current_line_index += 1
species_name = lines[current_line_index].strip()
current_line_index += 1
# 2) 读物种序列,多行拼接
seq_fragments = []
current_seq_length = 0
while current_seq_length < L:
if current_line_index >= len(lines):
# 文件不够,报错或警告
raise ValueError(f"文件数据不足,{species_name} 的序列尚未达到长度 {L}")
line_seq = lines[current_line_index].strip()
current_line_index += 1
# 如果行是空行,可能需要跳过继续读下一行
if not line_seq:
continue
# 拼接
seq_fragments.append(line_seq)
current_seq_length += len(line_seq)
full_sequence = "".join(seq_fragments)
# 如果多读了长度 > L,可以在此做截断 full_sequence = full_sequence[:L]
if len(full_sequence) > L:
full_sequence = full_sequence[:L]
seq_dict[species_name] = full_sequence
# 对物种名称进行字母顺序排序
sorted_items = sorted(seq_dict.items(), key=lambda x: x[0])
# 写出结果:顺序格式(sequential),每条序列只占 2 行(名称 + 序列)
with open(output_file, 'w') as out:
out.write(f"{N} {L}\n")
for name, seq in sorted_items:
out.write(f"{name}\n{seq}\n")
print(f"[完成] 已处理多行序列并按名称排序写至:{output_file}")
def main():
if len(sys.argv) < 2:
print("用法:python sort_paml_multiline.py input.phy [output.phy]")
sys.exit(1)
input_file = sys.argv[1]
output_file = None
if len(sys.argv) > 2:
output_file = sys.argv[2]
sort_paml_multiline(input_file, output_file)
if __name__ == "__main__":
main()