0
#!/usr/bin/env python2.7 
##-*- mode:python;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t;python-indent:2 -*-'

import noesy
import argparse
import library

parser =argparse.ArgumentParser(description="read pdb file",
                                add_help=True)

parser.add_argument("file",help="protein pdb file")

library.add_standard_args( parser )
args = parser.parse_args()

def read_structure(pdbfile):
        struct=[]
        for line in pdbfile:
                if len(line):
                        struct.append(PDBAttributes.read_from_line(line))
        return struct

pdb=read_structure(open(args.file,'r'))

class PDBAttributes:
    def __init__(self, atomindex=1, atom=noesy.Atom(), atomx=1, atomy=1, atomz=1):
        self._atomindex=atomindex
        self._atom=atom
        self._atomx=atomx
        self._atomy=atomy
        self._atomz=atomz

        def __str__(self):
        s='ATOM %(_atomindex)d %(_atom)s at %(_atomx)8.3f %(_atomy)8.3f %(_atomz)8.3f'%self.__dict__
        return s

    def atom(self):
        return self._atom

    def atomindex(self):
        return self._atomindex

    def atomx(self):
        return self._atomx

    def atomy(self):
        return self._atomy

    def atomz(self):
        return self._atomz

    @classmethod
    def read_from_line(obj,line):
        tags=line.split()
        atomindex=int(tags[1])
        atom=noesy.Atom(tags[2],int(tags[5]))
        atomx=float(tags[6])
        atomy=float(tags[7])
        atomz=float(tags[8])
        obj=PDBAttributes(atomindex, atom, atomx, atomy, atomz)
        print obj

class AtomDistance(PDBAttributes):

    def distance(self, atom1,atom2):
        pass
4

2 回答 2

1

移动您的调用read_structure以遵循PDBAttributes类的定义。

此外,在重新格式化您的帖子的过程中,我看到您的缩进混合了制表符和空格。尝试重新格式化您的代码以使用所有空格进行缩进,推荐的形式是 4 空格缩进。

您对所有这些 getter 函数的定义看起来就像用 Python 编写的 Java - 这是在 Python 中通常不需要的大量额外代码。推荐的方法是省略这些 all-they-do-is-assign-a-value-to-an-attribute-with-the-same-name-but-with-a-leading-underscore 方法,只使用属性公开名称。请参阅Python 不是 Java

于 2012-06-14T09:39:39.783 回答
1

The NameError you are getting is due to the order you have placed the code in your file.

When you call read_structure to create a value for the pdb variable, it tries to look for PDBAttributes, but it has not been defined yet. If you move that line lower down in the file (below the class definition) you'll avoid that error. Note that it is OK to have the declaration of read_structure above the PDBAttributes class definition, though you might want to move it lower too to make the code easier to understand.

Here's a very simple bit of code that demonstrates the same error:

def foo():
    print(foo_text)

foo() # raises a NameError

foo_text = "foo"

Here's a fixed version:

def foo():
    print(foo_text)

foo_text = "foo"

foo() # no error, prints "foo"
于 2012-06-14T09:50:01.097 回答