python 用python 写了一个求空间俩点之间的距离的脚本
python">import math L1=['104','32','249','177'] L2=['191','538','317','795'] L3=['735','346','542','429'] L4=['434','277','659','132'] L5=['866','569','46','763'] def Search_data(store_pdb,List_data): f_pdb=open(store_pdb,'r') lines=f_pdb.readlines() f_pdb.close() space_coordinate_1=[] space_coordinate_2=[] for line in lines: line_data=line.split() if List_data[0] in line_data[4:] and 'P' in line_data[2:]: space_coordinate_1.append(line_data[-5:-2]) continue elif List_data[1] in line_data[4:] and 'P' in line_data[2:]: space_coordinate_1.append(line_data[-5:-2]) continue elif List_data[2] in line_data[4:] and 'P' in line_data[2:]: space_coordinate_2.append(line_data[-5:-2]) continue elif List_data[3] in line_data[4:] and 'P' in line_data[2:]: space_coordinate_2.append(line_data[-5:-2]) continue space_coordinate=(space_coordinate_1,space_coordinate_2) return space_coordinate def Get_distance(store_pdb,List_data): one_tuple_data=Search_data(store_pdb,List_data) point_list_one=one_tuple_data[0] point_list_two=one_tuple_data[1] x1=(float(point_list_one[0][0])+float(point_list_one[1][0]))/2 y1=(float(point_list_one[0][1])+float(point_list_one[1][1]))/2 z1=(float(point_list_one[0][2])+float(point_list_one[1][2]))/2 x2=(float(point_list_two[0][0])+float(point_list_two[1][0]))/2 y2=(float(point_list_two[0][1])+float(point_list_two[1][1]))/2 z2=(float(point_list_two[0][2])+float(point_list_two[1][2]))/2 d=math.sqrt((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)-20 return d def distance_files(store_pdb): L=[L1,L2,L3,L4,L5] for i in range(0,len(L)): distance_file=open('distance files %d'%(i+1),'a+') d=Get_distance(store_pdb,L[i]) distance_file.write(str(d)+'\n') distance_file.close() if __name__=="__main__": distance_files('1.pdb')
其中列表L为源文件中要找的第几行的数据,每一行中都有空间的一个点的坐标,srore_pdb为存放数据的pdb文件,list_data为上面的列表