# $CVSHeader: pal8e/src/GenericElement.py,v 1.2 2005/02/15 01:10:26 indraneel Rel $


from CoordCalc import torsion

class GenericElement(list):
    def __init__(self, logger, type, seq=None):
        self.logger = logger
        self.type = type
        if seq:
            list.__init__(self, seq)

    def __repr__(self):
        line = ''
        for residue in self:
            line += str(residue)+'\n'
        return line

    def pairBP(self):
        for res in self:
            res.pairBP()

    def getPhiPsi(self, handle=''):
        # start, end and len for debugging
        start = 0
        end = 0
        if len(self) < 0 : return []
        
        PhiPsi = []
        for (j, res) in enumerate(self[start:-2-end]):
            i = j+start
            if not res.has_key('C'): continue
            C0 = res['C'].coord
            if not self[i+1].has_key('N'): continue
            N1 = self[i+1]['N'].coord
            CA1 = self[i+1]['CA'].coord
            if not self[i+1].has_key('C'): continue
            C1 = self[i+1]['C'].coord
            if not self[i+2].has_key('N'): continue
            N2 = self[i+2]['N'].coord
            phi = torsion(C0, N1, CA1, C1)
            psi = torsion(N1, CA1, C1, N2)
            PhiPsi.append([phi, psi])
            #if (self.type == 'H') and (phi>0) and (-50<psi<50):
            #    self.logger.log(handle+'\t'+str(self[i+1].resid)+'\t'+ \
            #                    str(phi)+'\t'+str(psi))
        return PhiPsi

class GenericElementList(list):
    def __init__(self, logger, seq=None):
        self.logger = logger
        if seq:
            list.__init__(self, seq)

    def __repr__(self):
            line = ''
            for element in self:
                line += str(element)+'\n'
            return line

    def pairBP(self):
        for element in self:
            element.pairBP()
            
