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

# calculates d3 from DSSP for making a plot

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

testfile = "../../dat/nrchn2787.dssp/1fiy.dssp"
DSSPDIR = "../../dat/nrchn2787.dssp/"

class DSSPd3:
    def getHdata(self, logger, bin_size, start, end):
        runner = DSSPRunner()
        typedef={'H':'H'}
        etype = typedef.values()[0]
        data_array = []
        stat = DSSPStat()
#        runner.runfile(logger, testfile, typedef, etype, data_array, stat.d3)
        runner.rundir(logger, DSSPDIR, typedef, etype, data_array, stat.d3)
        bin = Bin(data_array)
        bin.bin(bin_size, start, end)
        bin.normalise()
        data = bin.getNumarray()
        sigma = Sigma(data_array)
        s = sigma.sigma()
        return (data, s)

    def getEdata(self, logger, bin_size, start, end):
        runner = DSSPRunner()
        typedef={'E':'E'}
        etype = typedef.values()[0]
        data_array = []
        stat = DSSPStat()
#        runner.runfile(logger, testfile, typedef, etype, data_array, stat.d3)
        runner.rundir(logger, DSSPDIR, typedef, etype, data_array, stat.d3)
        bin = Bin(data_array)
        bin.bin(bin_size, start, end)
        bin.normalise()
        data = bin.getNumarray()
        sigma = Sigma(data_array)
        s = sigma.sigma()
        return (data, s)
    
    def run(self, bin_size, start, end):
        logger = LogFH('DSSPd3-testlog')
        (Hdata, Hsigma) = self.getHdata(logger, bin_size, start, end)
        (Edata, Esigma) = self.getEdata(logger, bin_size, start, end)
        data = Numeric.transpose((Hdata[:,0], Hdata[:,1],
                                  Edata[:,0], Edata[:,1]))
        plot = DSSPPlot()
        title = "i, i+3 CA-CA Distances for Helix and Strand"
        xlabel = "Distance (A)"
        ylabel = "Normalised Points"
        fname = 'd3.eps'
        cols = [[0,1], [2,3]]
        titles = ['Helix','Strand']
        #cols = [[0,1]]
        plot.plot(data, cols, titles, title, xlabel, ylabel, fname)
        return (data, Hsigma, Esigma)

if __name__ == '__main__':
    d3 = DSSPd3()
    (data, Hsigma, Esigma) = d3.run(0.2, 3.8, 14.0)
