use*_*152 11 python r bioinformatics biopython
我有一个fasta文件,如下所示.我想将三个字母的代码转换为一个字母代码.我怎么能用python或R做到这一点?
>2ppo
ARGHISLEULEULYS
>3oot
METHISARGARGMET
Run Code Online (Sandbox Code Playgroud)
期望的输出
>2ppo
RHLLK
>3oot
MHRRM
Run Code Online (Sandbox Code Playgroud)
你的建议将不胜感激!
小智 13
BioPython已经内置了词典来帮助完成这些翻译.以下命令将显示可用字典的完整列表:
import Bio
help(Bio.SeqUtils.IUPACData)
Run Code Online (Sandbox Code Playgroud)
您正在寻找的预定义词典:
Bio.SeqUtils.IUPACData.protein_letters_3to1['Ala']
Run Code Online (Sandbox Code Playgroud)
Jun*_*uxx 12
使用字典查找单字母代码:
d = {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N',
'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W',
'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M'}
Run Code Online (Sandbox Code Playgroud)
还有一个简单的函数可以匹配三个字母代码和整个字符串的一个字母代码:
def shorten(x):
if len(x) % 3 != 0:
raise ValueError('Input length should be a multiple of three')
y = ''
for i in range(len(x)/3):
y += d[x[3*i:3*i+3]]
return y
Run Code Online (Sandbox Code Playgroud)
测试你的例子:
>>> shorten('ARGHISLEULEULYS')
'RHLLK'
Run Code Online (Sandbox Code Playgroud)
以下是在R中执行此操作的方法:
# Variables:
foo <- c("ARGHISLEULEULYS","METHISARGARGMET")
# Code maps:
code3 <- c("Ala", "Arg", "Asn", "Asp", "Cys", "Glu", "Gln", "Gly", "His",
"Ile", "Leu", "Lys", "Met", "Phe", "Pro", "Ser", "Thr", "Trp",
"Tyr", "Val")
code1 <- c("A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K",
"M", "F", "P", "S", "T", "W", "Y", "V")
# For each code replace 3letter code by 1letter code:
for (i in 1:length(code3))
{
foo <- gsub(code3[i],code1[i],foo,ignore.case=TRUE)
}
Run Code Online (Sandbox Code Playgroud)
结果是 :
> foo
[1] "RHLLK" "MHRRM"
Run Code Online (Sandbox Code Playgroud)
请注意,我更改了变量名称,因为不允许变量名以R中的数字开头.
>>> src = "ARGHISLEULEULYS"
>>> trans = {'ARG':'R', 'HIS':'H', 'LEU':'L', 'LYS':'K'}
>>> "".join(trans[src[x:x+3]] for x in range(0, len(src), 3))
'RHLLK'
Run Code Online (Sandbox Code Playgroud)
您只需将其余条目添加到trans
dict即可.
编辑:
为了完成剩下的工作trans
,你可以做到这一点.档案table
:
Ala A
Arg R
Asn N
Asp D
Cys C
Glu E
Gln Q
Gly G
His H
Ile I
Leu L
Lys K
Met M
Phe F
Pro P
Ser S
Thr T
Trp W
Tyr Y
Val V
Run Code Online (Sandbox Code Playgroud)
阅读:
trans = dict((l.upper(), s) for l, s in
[row.strip().split() for row in open("table").readlines()])
Run Code Online (Sandbox Code Playgroud)
Biopython 有一个很好的解决方案
>>> from Bio.PDB.Polypeptide import *
>>> three_to_one('ALA')
'A'
Run Code Online (Sandbox Code Playgroud)
对于你的例子,我将通过这一行来解决它
>>> from Bio.PDB.Polypeptide import *
>>> str3aa = 'ARGHISLEULEULYS'
>>> "".join([three_to_one(aa3) for aa3 in [ "".join(g) for g in zip(*(iter(str3aa),) * 3)]])
>>> 'RHLLK'
Run Code Online (Sandbox Code Playgroud)
他们可能会批评我的这种说法:),但在我内心深处,我仍然热爱 PERL。
归档时间: |
|
查看次数: |
13341 次 |
最近记录: |