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

from CoordCalc import distance, torsion, v_angle
from DSSPExtparm import extparm
import math

eparm = extparm()

syn_dist_sigma = 0.227;
syn_dist_mean = 4.81;

anti_dist_sigma = 0.261;
anti_dist_mean_lo = 4.463;
anti_dist_mean_hi = 5.245;
    
syn_bond_sigma = 2.5;
syn_bond_mean = 87.0;

anti_bond_sigma = 3.7;
anti_bond_mean = 82.2;
    
syn_torsion_arrow_sigma = 15.6;
syn_torsion_arrow_mean = -20.9;

anti_torsion_arrow_sigma = 25.6;
anti_torsion_arrow_mean = -27.9;

class DSSPQuad:
    def score (self,
               res1, res2,
               res3, res4,
               res5, res6,
               res7, res8, orient, Qtype):
        score = 0
        dists=[0,0]
        dists[0]=distance(res3.coord, res4.coord)
        dists[1]=distance(res5.coord, res6.coord)
        dists.sort()
        for i in range(2):
            if not (3.5 < dists[i] < 7.5): return 'Err'
        if orient == 'Syn':
            dists[0] = (dists[0]-syn_dist_mean)/syn_dist_sigma
            dists[1] = (dists[1]-syn_dist_mean)/syn_dist_sigma
        else:
            dists[0] = (dists[0]-anti_dist_mean_lo)/anti_dist_sigma
            dists[1] = (dists[1]-anti_dist_mean_hi)/anti_dist_sigma
        for i in range(2):
            #if (Qtype == 'BadSyn'):
            #    print 'dist: ', dists[i]
            if not (-6.0 < dists[i] < 6.0):
                dists[i] = 6.0
            dists[i] = eparm.get_val('lpg', dists[i])
            score += dists[i]
        
        bondangles=[0,0,0,0]
        bondangles[0]=v_angle((res1.coord-res5.coord),
                              (res3.coord-res4.coord), 90)
        bondangles[1]=v_angle((res2.coord-res6.coord),
                              (res4.coord-res3.coord), 90)
        bondangles[2]=v_angle((res3.coord-res7.coord),
                              (res5.coord-res6.coord), 90)
        bondangles[3]=v_angle((res4.coord-res8.coord),
                              (res6.coord-res5.coord), 90)
        if orient == 'Syn':
            mean = syn_bond_mean
            sigma = syn_bond_sigma
            lp = 'sblp'
        else:
            mean = anti_bond_mean
            sigma = anti_bond_sigma
            lp = 'ablp'
        for i in range(4):
            #if (Qtype == 'BadSyn'):
            #    print 'bangle: ', bondangles[i]
            bondangles[i] = (bondangles[i]-mean)/sigma
            if not (-6.0 < bondangles[i] < 6.0):
                bondangles[i] = 6.0
            bondangles[i] = eparm.get_val(lp, bondangles[i])
            bondangles[i] = bondangles[i]/2.0
            score += bondangles[i]
            
        torangles=[0,0]
        torangles[0]=torsion(res3.coord,
                             (res1.coord+res5.coord)/2,
                             (res2.coord+res6.coord)/2, res4.coord)
        torangles[1]=torsion(res5.coord,
                             (res3.coord+res7.coord)/2,
                             (res4.coord+res8.coord)/2, res6.coord)
        if orient == 'Syn':
            mean = syn_torsion_arrow_mean
            sigma = syn_torsion_arrow_sigma
            lp = 'stlp'
        else:
            mean = anti_torsion_arrow_mean
            sigma = anti_torsion_arrow_sigma
            lp = 'atlp'
        for i in range(2):
            #if (Qtype == 'BadSyn'):
            #    print 'tangle: ', torangles[i]
            torangles[i] = (torangles[i]-mean)/sigma
            if not (-6.0 < torangles[i] < 6.0):
                torangles[i] = 6.0
            torangles[i] = eparm.get_val(lp, torangles[i])
            score += torangles[i]

        #print score
        return score
