# $CVSHeader: pal8e/src/Compare_pross.py,v 1.3 2005/02/15 01:10:24 indraneel Rel $

import os
import string

class Pross:
    def __init__(self, conf, logger):
        self.conf = conf
        self.logger = logger
        structureID = conf.get('Input', 'structureID')
        prossdir = conf.get('Compare', 'prossdir')
        prosstail = conf.get('Compare', 'prosstail')
        self.fname = prossdir + structureID + prosstail

    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] == '>>>':
                break
        lines = lines[start:]
        return lines

    def _extract_secstr(self):
        lines = self._get_sec_str_lines()
        secstrdef = {}
        for line in lines:
            if line[:3] == '>>>': continue
            if line[:3] == '<<<': continue
            chain = line[18]
            if not secstrdef.has_key(chain):
                secstrdef[chain] = {}
            id = line[8:12]
            id = int(id)
            icode = line[16]
            if icode == '_': icode = ' '
            if not secstrdef[chain].has_key(id):
                secstrdef[chain][id] = {}
            secstr = line[43]
            if not secstrdef[chain][id].has_key(icode):
                secstrdef[chain][id][icode] = secstr
        self.secstrdef = secstrdef

    def _report(self, peptides):
        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:
                total_residues += 1
                if res.H: total_pssd_H += 1
                if res.E: total_pssd_E += 1
                if res.compare_pross == 'H': total_other_H += 1
                if res.compare_pross == 'E': total_other_E += 1
                if res.H and res.compare_pross == 'H': both_H += 1
                if res.E and res.compare_pross == 'E': both_E += 1
                if res.H and res.compare_pross != 'H': only_pssd_H += 1
                if res.E and res.compare_pross != 'E': only_pssd_E += 1
                if res.compare_pross == 'H' and not res.H: only_other_H += 1
                if res.compare_pross == 'E' and not res.E: only_other_E += 1
        line='pross comparison report for '+str(total_residues)+' residues:\n'
        lines = [line]
        lines += ['pross total_pssd_H '+str(total_pssd_H)+'\n']
        lines += ['pross total_pssd_E '+str(total_pssd_E)+'\n']
        lines += ['pross total_other_H '+str(total_other_H)+'\n']
        lines += ['pross total_other_E '+str(total_other_E)+'\n']
        lines += ['pross both_H '+str(both_H)+'\n']
        lines += ['pross both_E '+str(both_E)+'\n']
        lines += ['pross only_pssd_H '+str(only_pssd_H)+'\n']
        lines += ['pross only_pssd_E '+str(only_pssd_E)+'\n']
        lines += ['pross only_other_H '+str(only_other_H)+'\n']
        lines += ['pross 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_pross = v
        self._report(peptides)
