-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2_simplify.py
More file actions
42 lines (37 loc) · 1.05 KB
/
Copy path2_simplify.py
File metadata and controls
42 lines (37 loc) · 1.05 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 1.获得最长密码子序列
# 2.在ID前加入物种名(取自文件名称)
import sys
import os
file = sys.argv[1]
path = os.path.abspath(file)
#path = ("/Users/byeamx/catfishes/pep_catfishes_kaks/Tachysurus_fulvidraco.faa")
name = path.replace(path.split("/")[-1], path.split("/")[-1].split('.')[0] + '.fna'
)
species = path.split(r'/')[-1].replace('.faa', '')
print(f"开始精简 {species} \n")
re = {}
with open(path) as f:
for line in f:
seq = []
if line.startswith('>'):
id = line.split('_')[:-1]
id = str.join('_', id).replace('>','')
id = '>' + species + '|'+ id
else:
seq.append(line)
if id not in re:
re[id] = seq
else:
re[id] += seq
maxseq = {}
for k,v in re.items():
seq = max(v, key=len)
maxseq[k] = seq
with open(name,'w') as f:
for k,v in maxseq.items():
f.write( k +'\n')
tem = v.__str__()
f.write(tem)
print(f"精简完成 {species} \n")