python:浮点数的格式

2024-06-16 09:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写python脚本,该脚本将一个点的坐标(表示三维结构的几何中心)保存在多模型文件(result.pdb)中,该文件包含240个相同原子的模型,其中240个不同的坐标(XYZ)由ENDMDL项分隔。以下是我的python脚本片段:

# this function calculates XYZ of the given atomic coordinates
def lig_center(nested_array_list):
    a = numpy.array(nested_array_list)
    mean = numpy.mean(a, axis=0)
    return mean[0], mean[1], mean[2]  # x, y, z


# make the list of the centres for each of the conformation
conf_xyz=[]
i=1
# loop each of the 240 conformations
for conf in d.ch.conformations:
    number=i
    # get XYZ of all atoms of each conformations
    xyz=conf.getCoords()
    # get XYZ of the geometrical center of these coordinates
    col=lig_center(xyz)
    # store geometrical center as in a list
    col1 = list(col)
    # append it to the existing list called conf_xyz
    conf_xyz.append(col1)
    # store coordinates of the centers in the output pdb file:
    with open(outputfilename+".pdb", "a") as f:
    # for each of the model first print its index
        print >>f, 'MODEL'+str(i)
    # then print the name of the atom (always C and its XYZ taken from the list col:
        print >>f, 'ATOM      1  C   COM     1      '+'%.2f  %.2f  %.2f' %(col[0],col[1],col[2])+' 1.00 0.00'
        print >>f, 'ENDMDL'
    i=i+1

此方法在输出格式中的问题。以下是result.pdb输出文件中的内容:

MODEL79
ATOM      1  C   COM     1      22.06  -14.69  6.02 1.00 0.00
ENDMDL
MODEL80
ATOM      1  C   COM     1      20.01  -17.91  4.69 1.00 0.00
ENDMDL
MODEL81
ATOM      1  C   COM     1      04.38  -9.05  -7.02 1.00 0.00
ENDMDL
MODEL82
ATOM      1  C   COM     1      3.12  -3.63  -9.41 1.00 0.00
ENDMDL
MODEL83
ATOM      1  C   COM     1      4.13  -6.49  -7.51 1.00 0.00
ENDMDL
MODEL84
ATOM      1  C   COM     1      3.22  -3.67  -9.70 1.00 0.00
ENDMDL
MODEL85
ATOM      1  C   COM     1      4.22  -4.74  -8.54 1.00 0.00
ENDMDL
MODEL86
ATOM      1  C   COM     1      1.70  -2.19  -12.49 1.00 0.00
ENDMDL
MODEL87
ATOM      1  C   COM     1      3.32  -3.98  -10.74 1.00 0.00
ENDMDL

以下是我应该做的:

MODEL79
ATOM      1  C   COM     1      22.06  -14.69  06.02 1.00 0.00
ENDMDL
MODEL80
ATOM      1  C   COM     1      20.01  -17.91  04.69 1.00 0.00
ENDMDL
MODEL81
ATOM      1  C   COM     1      04.38  -09.05  -07.02 1.00 0.00
ENDMDL
MODEL82
ATOM      1  C   COM     1      03.12  -03.63  -09.41 1.00 0.00
ENDMDL
MODEL83
ATOM      1  C   COM     1      04.13  -06.49  -07.51 1.00 0.00
ENDMDL
MODEL84
ATOM      1  C   COM     1      03.22  -03.67  -09.70 1.00 0.00
ENDMDL
MODEL85
ATOM      1  C   COM     1      04.22  -04.74  -08.54 1.00 0.00
ENDMDL
MODEL86
ATOM      1  C   COM     1      01.70  -02.19  -12.49 1.00 0.00
ENDMDL
MODEL87
ATOM      1  C   COM     1      03.32  -03.98  -10.74 1.00 0.00

注:在第二种情况下,XYZ(6-8)列完全对齐,因为每个值的xx.xx格式(我将零添加到x.xx,如4.22-4.74-8.54到04.22-04.74-08.54的转换等)

假设输出文件的格式在python脚本的末尾定义,通过

print >>f, 'ATOM      1  C   COM     1      '+'%.2f  %.2f  %.2f' %(col[0],col[1],col[2])+' 1.00 0.00' 

解决方案之一是用

 print >>f, 'ATOM      1  C   COM     1      '+'%05.2f  %05.2f  %05.2f' %(col_B[0],col_B[1],col_B[2])+' 1.00 0.00'

哪个函数对正数正确,但对负数不正确:

MODEL7 
ATOM 1 C COM 1 06.09 -19.59 -0.90 1.00 0.00 
ENDMDL 
MODEL8 
ATOM 1 C COM 1 06.51 -19.86 -1.27 1.00 0.00
ENDMDL 

Tags: 文件ofthe脚本comconfcolmean