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

##Calculates all statistics from DSSP files in single run.
##Statistics are stored in files with comments in first 2 lines.
##These are:
##    d3
##    dih
##    quad dist:
##    quad bond:
##    quad torsion:
##    good quad
##    best bad quad
##    angle between neighboring quads (120 degree rule)

from Bin import Bin
from Sigma import Sigma
from Log import LogFH
from DSSPRunner import DSSPRunner
from DSSPStat import DSSPStat
from DSSPPlot import DSSPPlot
import Numeric
import copy

class Config:
    dsspdir = '../../dat/nrchn2787.dssp/'
    #dsspdir = '../../dat/x/'
    outdir = '../dat/stat/'
    Hd3 = 'Hd3'
    Ed3 = 'Ed3'
    Hdih = 'Hdih'
    Edih = 'Edih'
    QdistSyn = 'QdistSyn'
    QdistAnti = 'QdistAnti'
    QbondSyn = 'QbondSyn'
    QbondAnti = 'QbondAnti'
    QtorSyn = 'QtorSyn'
    QtorAnti = 'QtorAnti'
    QbondSynTS = 'QbondSynTS'
    QbondAntiTS = 'QbondAntiTS'
    QtorSynTS = 'QtorSynTS'
    QtorAntiTS = 'QtorAntiTS'
    Qscore = 'Qscore'
    i2Angle = 'i2Angle'
    i4pseudoAngle = 'i4pseudoAngle'
    Q120Angle = 'Q120Angle'
    QgoodSyn = 'QgoodSyn'
    QbadSyn = 'QbadSyn'
    QgoodAnti = 'QgoodAnti'
    QbadAnti = 'QbadAnti'
    QSyn = 'QSyn'
    QAnti = 'QAnti'

class DSSPAllStat:
    def getData(self, bin_size, start, end, etype, fn):
        logger = self.logger
        conf = self.conf
        runner = DSSPRunner()
        typedef={etype:etype}
        etype = typedef.values()[0]
        data_array = []
        dsspdir = conf.dsspdir
        runner.rundir(logger, dsspdir, typedef, etype, data_array, fn)
        bin = Bin(data_array)
        bin.bin(bin_size, start, end)
        bin.normalise()
        data = bin.getNumarray()
        sigma = Sigma(data_array)
        s = sigma.sigma()
        mean = sum(data_array)/len(data_array)
        return (data, s, mean, data_array)

    def getTimesSigmaData(self, bin_size, start, end, dataArray, sigma, mean):
        data = copy.copy(dataArray)
        def ts(d):
            return (d-mean)/sigma
        data = map(ts, data)
        bin = Bin(data)
        bin.bin(bin_size, start, end)
        bin.normalise()
        data = bin.getNumarray()
        return data

    def storeData(self, fname, data, sigma, mean):
        logger = self.logger
        conf = self.conf
        outfname = conf.outdir+getattr(conf, fname)
        outfh = file(outfname, 'w')
        outfh.write('# '+str(sigma)+'\t'+str(mean)+'\n')
        for d in data:
            outfh.write(str(d[0])+'\t'+str(d[1])+'\n')
        outfh.close()
    
    def getQData(self, bin_size, start, end, etype, fn):
        logger = self.logger
        conf = self.conf
        runner = DSSPRunner()
        typedef={etype:etype}
        etype = typedef.values()[0]
        data_array = []
        dsspdir = conf.dsspdir
        runner.rundir(logger, dsspdir, typedef, etype, data_array, fn)
        data = [[],[]]
        dat = [0,0]
        s = [0,0]
        m = [0,0]
        for d in data_array:
            data[0].append(d[0])
            data[1].append(d[1])
        for i in range(2):
            bin = Bin(data[i])
            bin.bin(bin_size, start, end)
            bin.normalise()
            dat[i] = bin.getNumarray()
            sigma = Sigma(data[i])
            s[i] = sigma.sigma()
            m[i] = sum(data[i])/len(data[i])
        return (dat[0], dat[1], s[0], s[1], m[0], m[1], data_array)

    def run(self):
        self.logger = LogFH('DSSPAllStat-testlog')
        self.conf = Config()
        stat = DSSPStat()
        #(data, sigma, mean, da) = self.getData(0.2, 3.8, 14.0, 'H', stat.d3)
        #self.storeData('Hd3', data, sigma, mean)
        
        #(data, sigma, mean, da) = self.getData(0.2, 3.8, 14.0, 'E', stat.d3)
        #self.storeData('Ed3', data, sigma, mean)
        
        #(data, sigma, mean, da) = self.getData(5, -180, 180, 'H', stat.dih)
        #self.storeData('Hdih', data, sigma, mean)
        
        #(data, sigma, mean, da) = self.getData(5, -180, 180, 'E', stat.dih)
        #self.storeData('Edih', data, sigma, mean)
        
        #(data, sigma, mean, da)=self.getData(0.1, 3.0, 8.5, 'E', stat.synDist)
        #self.storeData('QdistSyn', data, sigma, mean)
        
        #(data, sigma, mean, da)=self.getData(0.1, 3, 8.5, 'E', stat.antiDist)
        #self.storeData('QdistAnti', data, sigma, mean)
        
        #(data, sigma, mean, da)=self.getData(1, 0, 90, 'E', stat.synBond)
        #self.storeData('QbondSyn', data, sigma, mean)
        #data=self.getTimesSigmaData(0.1, -6.55, 6.55, da, sigma, mean)
        #self.storeData('QbondSynTS', data, 0, 0)
        #
        #(data, sigma, mean, da)=self.getData(1, 0, 90, 'E', stat.antiBond)
        #self.storeData('QbondAnti', data, sigma, mean)
        #data=self.getTimesSigmaData(0.1, -6.55, 6.55, da, sigma, mean)
        #self.storeData('QbondAntiTS', data, 0, 0)
        #
        #(data, sigma, mean, da)=self.getData(1, -180, 180, 'E', stat.synTor)
        #self.storeData('QtorSyn', data, sigma, mean)
        #data=self.getTimesSigmaData(0.1, -6.55, 6.55, da, sigma, mean)
        #self.storeData('QtorSynTS', data, 0, 0)
        #
        #(data, sigma, mean, da)=self.getData(1, -180, 180, 'E', stat.antiTor)
        #self.storeData('QtorAnti', data, sigma, mean)
        #data=self.getTimesSigmaData(0.1, -6.55, 6.55, da, sigma, mean)
        #self.storeData('QtorAntiTS', data, 0, 0)
        
        #(SQdata, AQdata, Ssigma, Asigma, Smean, Amean, da) = \
        #         self.getQData(1,0,160,'E',stat.synQuad)
        #self.storeData('QgoodSyn', SQdata, Ssigma, Smean)
        #self.storeData('QbadSyn', AQdata, Asigma, Amean)
        #self.storeData('QSyn', da, 0, 0)
        #
        #(SQdata, AQdata, Ssigma, Asigma, Smean, Amean, da) = \
        #         self.getQData(1,0,160,'E',stat.antiQuad)
        #self.storeData('QgoodAnti', SQdata, Ssigma, Smean)
        #self.storeData('QbadAnti', AQdata, Asigma, Amean)
        #self.storeData('QAnti', da, 0, 0)
        
        #(data, sigma, mean, da)=self.getData(1, -180, 180, 'E', stat.i2Angle)
        #self.storeData('i2Angle', data, sigma, mean)
        
        #(data,sigma,mean,da)=self.getData(1,-180,180,'E',stat.i4pseudoAngle)
        #self.storeData('i4pseudoAngle', data, sigma, mean)
        
        #(data, sigma, mean, da)=self.getData(1,-180, 180, 'E', stat.Q120Angle)
        #self.storeData('Q120Angle', data, sigma, mean)

if __name__ == '__main__':
    allStat = DSSPAllStat()
    allStat.run()
