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

import os
import string

class Stride:
    def __init__(self, conf, logger):
        self.conf = conf
        self.logger = logger
        structureID = conf.get('Input', 'structureID')
        stridedir = conf.get('Compare', 'stridedir')
        stridetail = conf.get('Compare', 'stridetail')
        self.fname = stridedir + structureID + stridetail

    def _get_lines(self):
        if not os.path.isfile(self.fname): return []
        fh = file(self.fname)
        lines = fh.readlines()
        fh.close()
        return lines

    def _get_sec_str_lines(self):
        lines = self._get_lines()
        if not lines: return []
        for start in range(len(lines)):
            if lines[start][:3] == 'ASG':
                break
        lines = lines[start:]
        return lines

    def _extract_secstr(self):
        lines = self._get_sec_str_lines()
        secstrdef = {}
        for line in lines:
            chain = line[9]
            if chain == '-': chain = ' '
            if not secstrdef.has_key(chain):
                secstrdef[chain] = {}
            id = line[10:15]
            icode = id[-1]
            id = string.rstrip(id, string.letters)
            id = int(id)
            if not secstrdef[chain].has_key(id):
                secstrdef[chain][id] = {}
            icode = string.strip(icode, string.digits)
            if not icode: icode = ' '
            secstr = line[24]
            if not secstrdef[chain][id].has_key(icode):
                secstrdef[chain][id][icode] = secstr
        self.secstrdef = secstrdef

    def _report(self, peptides):
        peptides.initialise_tags(['stride_stride_only', 'stride_pssd_only' \
                                  'stride_pssd_only_H', 'stride_pssd_only_E'])
        total_residues = 0
        total_pssd_H = 0
        total_pssd_E = 0
        total_other_H = 0
        total_other_E = 0
        both_H = 0
        both_E = 0
        only_pssd_H = 0
        only_pssd_E = 0
        only_other_H = 0
        only_other_E = 0
        for peptide in peptides:
            for res in peptide:
                if res.compare_stride == None:
                    mess = 'no stride output for '+str(res)
                    self.logger.log('critical', mess)
                    continue                    
                total_residues += 1
                if res.H: total_pssd_H += 1
                if res.E: total_pssd_E += 1
                if res.compare_stride == 'H': total_other_H += 1
                if res.compare_stride == 'E': total_other_E += 1
                if res.H and res.compare_stride == 'H': both_H += 1
                if res.E and res.compare_stride == 'E': both_E += 1
                if res.H and res.compare_stride != 'H':
                    only_pssd_H += 1
                    res.stride_pssd_only = 1
                    res.stride_pssd_only_H = 1
                if res.E and res.compare_stride != 'E':
                    only_pssd_E += 1
                    res.stride_pssd_only = 1
                    res.stride_pssd_only_E = 1
                if res.compare_stride == 'H' and not res.H:
                    only_other_H += 1
                    res.stride_stride_only = 1
                if res.compare_stride == 'E' and not res.E:
                    only_other_E += 1
                    res.stride_stride_only = 1
        line='stride comparison report for '+str(total_residues)+' residues:\n'
        lines = [line]
        lines += ['stride total_pssd_H '+str(total_pssd_H)+'\n']
        lines += ['stride total_pssd_E '+str(total_pssd_E)+'\n']
        lines += ['stride total_other_H '+str(total_other_H)+'\n']
        lines += ['stride total_other_E '+str(total_other_E)+'\n']
        lines += ['stride both_H '+str(both_H)+'\n']
        lines += ['stride both_E '+str(both_E)+'\n']
        lines += ['stride only_pssd_H '+str(only_pssd_H)+'\n']
        lines += ['stride only_pssd_E '+str(only_pssd_E)+'\n']
        lines += ['stride only_other_H '+str(only_other_H)+'\n']
        lines += ['stride only_other_E '+str(only_other_E)+'\n']
        self.logger.log('compare', lines)

    ## Public Methods
    
    def compare(self, structure, peptides):
        self._extract_secstr()
        for peptide in peptides:
            chain = peptide[0].get_parent().id
            if self.secstrdef.has_key(chain):
                for res in peptide:
                    if self.secstrdef[chain].has_key(res.id[1]):
                        if self.secstrdef[chain][res.id[1]].has_key(res.id[2]):
                            v = self.secstrdef[chain][res.id[1]][res.id[2]]
                            res.compare_stride = v
        self._report(peptides)
