1'''
2Created on Feb 5, 2012
3
4@author: marat
5'''
6import sys
7from ResAtom import *
8
9class Residue:
10    '''
11    classdocs
12    '''
13
14
15    def __init__(self,params={}):
16        '''
17        Default constructor for Residue class
18        atoms list of atoms in the residue
19        name residue name
20        '''
21        if params.has_key("atoms"):
22            self.atoms = params["coords"]
23        else:
24            self.atoms=[]
25
26        if params.has_key("name"):
27            self.name = params["name"]
28        else:
29            self.name = ""
30
31    def __str__(self):
32        output = ""
33        for a in self.atoms:
34            output=output + str(a)+"\n"
35        return output
36
37    def toPDBrecord(self,id_atom=1,id_res=1):
38        output = ""
39        i=id_atom-1
40        for a in self.atoms:
41            i = i + 1
42            output=output + a.toPDBrecord(i) +"\n"
43        return output
44
45
46    def AddAtom(self,a):
47        if self.name =="" :
48            self.name = a.resname
49        else:
50            if a.resname != self.name:
51                print("different names for the same residue index")
52                sys.exit(1)
53        self.atoms.append(a)
54
55if __name__ == '__main__':
56    aline1 = "ATOM      3  O2  IO3     1      -1.182   1.410   0.573       -0.80     O"
57    aline2 = "ATOM      1  I1  IO3     1      -1.555  -0.350   0.333        1.39     I"
58
59    res0 = Residue()
60    a = ResAtom.fromPDBrecord(aline2)
61    b = ResAtom.fromPDBrecord(aline1)
62    res0.AddAtom(a)
63    res0.AddAtom(b)
64    print(res0.toPDBrecord(id_atom=4))
65