文件中同一轨迹上at1的参考原子到所有其他原子之间的距离

时间:2019-03-01 17:01:51

标签: python python-2.7 coordinates distance tinker

当前,我的代码在TINKER输入坐标文件中读取我的原子。

定义了一个计算距离的函数,如下所示:

def getdist(at1,at2):
''' Calculate distance between two particles
'''
dist_at=sqrt((at2[0]-at1[0])**2+(at2[1]-at1[1])**2+(at2[2]-at1[2])**2)
return dist_at

这将传递到主脚本,该主脚本根据在命令行中传递的两个参数执行计算:

def parsecmd():
description="Calculate distances, angles, torsions and quota in periodic or\
 non-periodic molecular structures.\n\
See documentation for further information."
usage="usage: %prog [options] input_file"
#updtate this when new formats are added

list_of_formats="XYZ/XMOL, TINKER XYZ/ARC"
#parse command line
parser=OP(version='%prog 1.1',description=description,usage=usage)

parser.add_option('-c','--credits',dest='credits',action='store_true',
                 default=False,help='display credits')
parser.add_option('-f','--format',dest='fformat', default="arc",
                  help='specify the format of the input structure, [default: %default]')
parser.add_option('-e','--filedata',dest='filedata',nargs=1,
                  help='specify that input file is setup file')
parser.add_option('-d','--distance',dest='distance',nargs=2,type='int',
                 help='require a distance calculation on the specified couple of atoms')
parser.add_option('-a','--angle',dest='angle',nargs=3,type='int',
                 help='require angle calculation on the specified three atoms')
parser.add_option('-i','--dih',dest='dih',nargs=4,type='int',
                 help='dihedral angle four atoms')
parser.add_option('-t','--torsion',dest='torsion',nargs=5,type='int',
                 help='require torsions calculation on the specified dihedral angle combination pair and periodicity (i.e. 360 deg)')
parser.add_option('-T','--torsion1',dest='torsion1',nargs=5,type='int',
                 help='require torsions calculation on the specified four atoms and periodicity (i.e. 360 deg)')
parser.add_option('-u','--unitcell',dest='periodicity',nargs=6,type='float',
                 help='define periodic unit cell (a,b,c in angstrom; alpha,beta,gamma in degrees)')
parser.add_option('-l','--listformats',dest='listformats',action='store_true',
                 default=False, help='list the available formats')
(options, args) = parser.parse_args(argv[1:])

    while 1:
    #get format file structure
    if lower(fformat) == "arc":
        retdata=read_arc(structfile,offset)
    elif lower(fformat) == "xyz":
        retdata=read_xyz(structfile,offset)
    else:
        pass

    if retdata=="EOF":
        break
    else:
        coordinates=retdata[0]  #always in 1st position if not EOF
        offset=retdata[1]       #always in 2nd position if not EOF
    for index,task in enumerate(tasklist):
        if task != None:
            if index == 0: #get distances
                tmplist=[]
                for element in task: #scan over the lists of distances
                    for i in range(3):
                        at1[i]=coordinates[element[0]-1][i]   #1st atom
                        at2[i]=coordinates[element[1]-1][i]   #2nd atom
                    if pbc == None:
                        dist=getdist(at1,at2)
                    else:
                        dist=getpbcdist(at1,at2,pbc)
                    tmplist.append('%12.4f' % dist)
                F_DISTS.write(join(tmplist,'')+"\n")  #write out the list of 
distances as string
def main():
'''
#===============================================================================
#                               MAIN MAIN MAIN MAIN
#===============================================================================
'''

(options,args)=parsecmd()
print('test')
infile=args[0]
outfiles=[]
#tasklist contain the list of task.the list in the 1st element is
#for the distances, that in the 2nd is for angle and in the 3rd for torsions and 4th is torsion combination
tasklist=[]

fformat=options.fformat     #get format input file

if options.periodicity:   #get periodicity
    pbc=options.periodicity
else:
    pbc=None

if options.filedata: #read data from file
    filedata=safeopen(options.filedata,'r')
    if filedata:
        (tasklist,outfiles)=read_filedata(filedata)
    else:
        print ("Cannot find "+options.filedata)
else:
    if options.distance: #distance
        junk=[]
        junk.append(list(options.distance)) #convert the tuple from the cml to a list
        tasklist.append(junk)
        tasklist.append(None) #no angle to calculate
        tasklist.append(None) #no torsion to calculate
        #create list outfiles in the form [['file'],None,None]
        outfiles.append('distcml.dat')
        outfiles.append(None)
        outfiles.append(None)

我想让at2遍历文件中的多个原子,而不是通过命令行at1和at2传递两个参数,而我想用这些原子数的列来计算距离at1的距离。

我在寻找什么

at1 = 26

at2 = 26,27,28,29,30,31,32,33,34,45(这些值保存在一个名为atoms.dat的文件中,所有内容都由新行分隔开)

我想要at1和at2中所有元素之间的距离。

我尝试过的事情:

我尝试在代码内使用for循环打开文件,因此at2被迭代了文件中的每一行,但发生了此错误:

文件“ distances_reference_atom_to_other_atoms.py”,第839行,位于主目录中     junk.append(list(options.distance))#将元组从cml转换为列表 TypeError:“ int”对象不可迭代

然后,我尝试通过命令行传递参数,并且发生了类似的错误。

Script

Atoms.dat Coordinate Trajectory File

0 个答案:

没有答案