计算之道

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2024.1.28更新:主要对红色部分进行了更新。以避免当xyz坐标的数值连在一起时,使用split函数会导致index的数量不确定,进而造成后续提取元素超出index。

#python3  for add ACENME
#ruichaomao,2022.1.4
pro_list = ['ALA','ARG','ASN','ASP','GLN','GLU','GLY','HIE','HID','HIP','ILE','LEU','LYS','MET','PHE','PRO','SER','THR','TRP','TYR','VAL','CYS']
lines = open('template3.pdb','r').readlines()
#for PDB which output by tleap
lines.insert(0,'###zhan hang fu \n')
#first_num = 0
new_lines = []
len_files = len(lines)
#for i in range(0,len_files):
i = 0
while i < len_files:
    #print (i)
    #The first line must not be an ATOM
    if lines[i].startswith('TER') or i==0:
        try:
            lines[i+3]
        except:
            new_lines.append(lines[i])
            i+=1
        else:
            if lines[i+1].split()[3] in pro_list:
                #print (lines[i])
                #aa=lines[i+3].split()
                aa=lines[i+3]
                #aa[2]='C '
                #aa[3]='ACE'
                #print (aa) 
                newline = aa[0:12]+' '+'C  '+' '+'ACE '+aa[21:]                
                #print (newline)
                #newline = aa[0]+'{:>7}'.format(aa[1])+'{:>5}'.format(aa[2])+'{:>4}'.format(aa[3])+'{:>6}'.format(aa[4])+'{:>12}'.format(aa[5])+'{:>8}'.format(aa[6])+'{:>8}'.format(aa[7])+'{:>6}'.format(aa[8])+'{:>6}'.format(aa[9])+'\n'
                if i == 0:
                    ace_trans=[newline,lines[i+1]]
                else:
                    ace_trans=['TER\n',newline,lines[i+1]]
                for s in ace_trans:
                    new_lines.append(s)
                i = i+5
            else:
                new_lines.append(lines[i])
                i+=1
    elif lines[i].startswith('ATOM'):
        if lines[i].split()[2]=='OXT':
            aa = lines[i].split()
            aa[2] = 'N  '
            aa[3] = 'NME'
            newline = aa[0]+'{:>7}'.format(aa[1])+'{:>5}'.format(aa[2])+'{:>4}'.format(aa[3])+'{:>6}'.format(aa[4])+'{:>12}'.format(aa[5])+'{:>8}'.format(aa[6])+'{:>8}'.format(aa[7])+'{:>6}'.format(aa[8])+'{:>6}'.format(aa[9])+'\n'
            new_lines.append(newline)
            i+=1
        else:
            new_lines.append(lines[i])
            i+=1
    else:
        i+=1
#print (new_lines)
new_file = open('template3_addACENME.pdb','w')
for i in new_lines:
    #print (type(i))
    new_file.write(i)
new_file.close()



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

旧版本:

#python3
#ruichaomao,2022.1.4,处理经过tleap加氢的蛋白体系,为第二次tleap增加ACE+NME做准备
pro_list = ['ALA','ARG','ASN','ASP','GLN','GLU','GLY','HIE','HID','HIP','ILE','LEU','LYS','MET','PHE','PRO','SER','THR','TRP','TYR','VAL','CYS']
lines = open('WT+h.pdb','r').readlines()
#first_num = 0
new_lines = []
len_files = len(lines)
#for i in range(0,len_files):
i = 0
while i < len_files:
    print (i)
    if lines[i].startswith('TER') or i==0:
        try:
            lines[i+3]
        except:
            new_lines.append(lines[i])
            i+=1
        else:
            if lines[i+1].split()[3] in pro_list:
                print (lines[i])
                aa=lines[i+3].split()
                aa[2]='C '
                aa[3]='ACE'
                newline = aa[0]+'{:>7}'.format(aa[1])+'{:>5}'.format(aa[2])+'{:>4}'.format(aa[3])+'{:>6}'.format(aa[4])+'{:>12}'.format(aa[5])+'{:>8}'.format(aa[6])+'{:>8}'.format(aa[7])+'{:>6}'.format(aa[8])+'{:>6}'.format(aa[9])+'\n'
                if i == 0:
                    ace_trans=[newline,lines[i+1]]
                else:
                    ace_trans=['TER\n',newline,lines[i+1]]
                for s in ace_trans:
                    new_lines.append(s)
                i = i+5
            else:
                new_lines.append(lines[i])
                i+=1
    elif lines[i].startswith('ATOM'):
        if lines[i].split()[2]=='OXT':
            aa = lines[i].split()
            aa[2] = 'N  '
            aa[3] = 'NME'
            newline = aa[0]+'{:>7}'.format(aa[1])+'{:>5}'.format(aa[2])+'{:>4}'.format(aa[3])+'{:>6}'.format(aa[4])+'{:>12}'.format(aa[5])+'{:>8}'.format(aa[6])+'{:>8}'.format(aa[7])+'{:>6}'.format(aa[8])+'{:>6}'.format(aa[9])+'\n'
            new_lines.append(newline)
            i+=1
        else:
            new_lines.append(lines[i])
            i+=1
    else:
        i+=1
#print (new_lines)
new_file = open('WT+h-acenme.pdb','w')
for i in new_lines:
    print (type(i))
    new_file.write(i)
new_file.close()

posted on 2022-01-04 23:12  计算之道  阅读(319)  评论(0编辑  收藏  举报