所以我有下面的脚本:
def single_to_tripple(res):
aa = {'R':'ARG','H':'HIS','K':'LYS','D':'ASP','E':'GLU','S':'SER','T':'THR','N':'ASN','Q':'GLN','C':'CYS','U':'SEC','G':'GLY','P':'PRO','A':'ALA','I':'ILE','L':'LEU','M':'MET','F':'PHE','W':'TRP','Y':'TYR','V':'VAL'}
return(aa[res])
seq = 'ASALKDYYAIMGVKPTDDLKTIKTAYRRLARKYHPDVSKEPDAEARFKEVAEAWEVLSDEQRRAEYDQMWQHRNDPQFNRQFHHGDGQSFNAEDFDDIFSSIFGQHARQSRQRPATRGHDIEIEVAVFLEETLTEHKRTISYNLPVYNAFGMIEQEIPKTLNVKIPAGVGNGQRIRLKGQGTPGENGGPNGDLWLVIHIAPHPLFDIVGQDLEIVVPVSPWEAALGAKVTVPTLKESILLTIPPGSQAGQRLRVKGKGLVSKKQTGDLYAVLKIVMPPKPDENTAALWQQLADAQSSFDPRKDWGKA'
length = len(seq)
for i,v in enumerate(xrange(0,len(seq),13)):
line = seq[v:v+13]
out_line = ('{:<3} '*13).format(single_to_tripple(line[0]),single_to_tripple(line[1]),single_to_tripple(line[2]),single_to_tripple(line[3]),single_to_tripple(line[4]),single_to_tripple(line[5]),single_to_tripple(line[6]),single_to_tripple(line[7]),single_to_tripple(line[8]),single_to_tripple(line[9]),single_to_tripple(line[10]),single_to_tripple(line[11]),single_to_tripple(line[12]))
print out_line
我正在使用脚本seq
每 13 个元素拼接一次字符串,然后将拼接字符串中的每个元素从其单字母代码转换为single_to_tripple
. 我的数据输出需要包含由空格分隔的 13 列。如果接头不包含 13 个元素,则会在最后一个接头处出现问题。我怎样才能捕捉到这个并像往常一样格式化字符串?
我enumerate
在 for 循环中使用,因为稍后我需要添加行号。
我当前的代码输出:
ALA SER ALA LEU LYS ASP TYR TYR ALA ILE MET GLY VAL
LYS PRO THR ASP ASP LEU LYS THR ILE LYS THR ALA TYR
ARG ARG LEU ALA ARG LYS TYR HIS PRO ASP VAL SER LYS
GLU PRO ASP ALA GLU ALA ARG PHE LYS GLU VAL ALA GLU
ALA TRP GLU VAL LEU SER ASP GLU GLN ARG ARG ALA GLU
TYR ASP GLN MET TRP GLN HIS ARG ASN ASP PRO GLN PHE
ASN ARG GLN PHE HIS HIS GLY ASP GLY GLN SER PHE ASN
ALA GLU ASP PHE ASP ASP ILE PHE SER SER ILE PHE GLY
GLN HIS ALA ARG GLN SER ARG GLN ARG PRO ALA THR ARG
GLY HIS ASP ILE GLU ILE GLU VAL ALA VAL PHE LEU GLU
GLU THR LEU THR GLU HIS LYS ARG THR ILE SER TYR ASN
LEU PRO VAL TYR ASN ALA PHE GLY MET ILE GLU GLN GLU
ILE PRO LYS THR LEU ASN VAL LYS ILE PRO ALA GLY VAL
GLY ASN GLY GLN ARG ILE ARG LEU LYS GLY GLN GLY THR
PRO GLY GLU ASN GLY GLY PRO ASN GLY ASP LEU TRP LEU
VAL ILE HIS ILE ALA PRO HIS PRO LEU PHE ASP ILE VAL
GLY GLN ASP LEU GLU ILE VAL VAL PRO VAL SER PRO TRP
GLU ALA ALA LEU GLY ALA LYS VAL THR VAL PRO THR LEU
LYS GLU SER ILE LEU LEU THR ILE PRO PRO GLY SER GLN
ALA GLY GLN ARG LEU ARG VAL LYS GLY LYS GLY LEU VAL
SER LYS LYS GLN THR GLY ASP LEU TYR ALA VAL LEU LYS
ILE VAL MET PRO PRO LYS PRO ASP GLU ASN THR ALA ALA
LEU TRP GLN GLN LEU ALA ASP ALA GLN SER SER PHE ASP
Traceback (most recent call last):
File "make_seq_res.py", line 10, in <module>
out_line = ('{:<3} '*13).format(single_to_tripple(line[0]),single_to_tripple(line[1]),single_to_tripple(line[2]),single_to_tripple(line[3]),single_to_tripple(line[4]),single_to_tripple(line[5]),single_to_tripple(line[6]),single_to_tripple(line[7]),single_to_tripple(line[8]),single_to_tripple(line[9]),single_to_tripple(line[10]),single_to_tripple(line[11]),single_to_tripple(line[12]))
IndexError: string index out of range