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

# extracts phi psi angles from dssp for each type of sec structure
# and writes to separate files

import os
import string

class Parm:
    dsspdir = '../../dat/cull100.dssp/'
    outdir = './'
    outfilebase = 'dsspphipsi_'

class DSSPPhiPsi:
    def __init__(self, parm):
        self.parm = parm
        self.PP = {}
        
    def getLines(self):
        lines = self.lines
        for (i,line) in zip(range(len(lines)+1),lines):
            if line[2] == '#':
                break
        lines = lines[i+1:]
        return lines

    def extractPhiPsi(self, fname):
        fh = file(fname)
        self.lines = fh.readlines()
        fh.close()
        fid = os.path.basename(fname)
        lines = self.getLines()
        for line in lines:
            if line[13] == '!': continue
            SS = line[16]
            resid=int(line[6:10].lstrip())
            phi = float(line[103:109])
            psi = float(line[109:115])
            if not self.PP.has_key(SS):
                self.PP[SS] = []
            self.PP[SS].append([fid, resid, phi, psi])

    def writeOut(self):
        for (key, values) in self.PP.items():
            typ = key
            if typ == ' ': typ = 'blank'
            lines = []
            for [fid, resid, phi, psi] in values:
                lines.append(str(phi)+'\t'+str(psi)+'\n')
            fh = file(self.parm.outdir+self.parm.outfilebase+typ, 'a')
            fh.writelines(lines)
            fh.close()

class Runner:
    def __init__(self, parm):
        self.parm = parm
        
    def runfile(self, fname):
        dsspphipsi = DSSPPhiPsi(self.parm)
        dsspphipsi.extractPhiPsi(fname)
        dsspphipsi.writeOut()

    def runall(self):
        for fname in os.listdir(self.parm.dsspdir):
            fName = self.parm.dsspdir+fname
            self.runfile(fName)

if __name__ == '__main__':
    parm = Parm()
    runner = Runner(parm)
    runner.runall()
    
