我写了一个代码,它读取多个配体文件和一个受体文件,并逐个计算受体和配体文件的XYZ坐标之间的距离。然后想分别为每个特定的配体文件写输出文件。但是我的代码只写了一个输出文件,并将所有配体文件的结果写在一个文件中。 以下是我的代码:
import re
import math
import glob
class Atom:
def __init__(self, resname='', resnum='', atomid=0, chainId='',
x=0.0, y=0.0, z=0.0, atomname = '', element = ''):
self.x=x
self.y = y
self.z = z
self.resname = resname
self.resnum = resnum
self.atomid = atomid
self.chainid = chainId
self.atomname = atomname
self.element = element
self.resnum = resnum
def __init__(self, line):
self.x = line[28: 38].strip()
self.y = line[39: 46].strip()
self.z = line[47: 56].strip()
self.resname = line[17: 21].strip()
self.resnum = line[22: 28].strip()
self.atomid = line[7: 12].strip()
self.chainid = line[22]
self.atomname = line[13: 16].strip()
self.element = line[77:78]
def caluDistence(proteinatom, ligandatoms, cutoff = 4.0):
matchedAtom = []
for atom in proteinatom:
atomX = float(atom.x)
atomY = float(atom.y)
atomZ = float(atom.z)
for hetatom in ligandatoms:
hetatomX = float(hetatom.x)
hetatomY = float(hetatom.y)
hetatomZ = float(hetatom.z)
d = math.sqrt((atomX - hetatomX)**2 + (atomY - hetatomY)**2 +
(atomZ - hetatomZ)**2 )
#print (str(d))
if d <= cutoff:
if atom in matchedAtom :
continue
matchedAtom.append(atom)
return matchedAtom
return d
pdb = "receptor.pdb"
lig = "D:\\python\\workspace\\practise\\exhau15\\frag1\\out_ligand_*.pdbqt"
files=glob.glob(lig)
proteinatom=[]
for line in open(pdb, 'r').readlines():
if line[0:6] == 'ATOM ':
atom = Atom(line)
proteinatom.append(atom)
ligandatoms = []
for file in files:
print("current file :" +file)
with open(file) as f:
for line in f:
if line[0:6] == 'ATOM ':
atom = Atom(line)
ligandatoms.append(atom)
matchedAtomList = caluDistence(proteinatom, ligandatoms)
outfile = "out2.pdb"
output = open(outfile, 'w')
for atom in matchedAtomList:
output.write("current file name: " + file + " " + atom.resname + "
" + atom.resnum + " " + atom.element + '\n' )
output.flush()
output.close()