ó
Ñ%�Tc           @   sx   d  Z  d d l m Z m Z m Z m Z m Z m Z d Z d Z	 d Z
 d Z d Z d Z d	 d d
 „  ƒ  YZ d „  Z d S(   s)   Predict rotamers from (theta, eta) valuesiÿÿÿÿ(   t   radianst   sint   cost   pit   sqrtt   expt   ,i    i   i   i   i   t   ThetaEtaPredictorc           B   s,   e  Z d  Z d „  Z d d „ Z d „  Z RS(   sQ  Predict rotamers from (theta, eta) values
    
    NOTE:
        This prediction is based in-part on the soft K-means clustering algorithm from:
        MacKay, D.J.C. 2002. Maximum Likelihood and Clustering. In Information Theory, Inference & Learning
            Algorithms, pp. 300-306,  Cambridge University Press, New York, NY.
    c         C   sÉ   t  | d ƒ } | j ƒ  i  } x� | D]ˆ } | j t ƒ } | d } | d } | d } t t | d d !ƒ }	 t t | d d !ƒ }
 t | d ƒ } | | |	 |
 | g | | <q& W| j ƒ  | |  _ d S(	   s  Create and initialize a ThetaEtaPredictor object
        
        ARGUMENTS:
            inputFile - a csv file containing training data for theta-eta Gaussian clusters
        RETURNS:
            a ThetaEtaPredictor object initialized with the cluster data from inputFile
        t   ri    i   i   i   i   i   N(   t   opent   readlinet   splitt   DELIMt   mapt   floatt   closet   _ThetaEtaPredictor__clusters(   t   selft   fileNamet   inputt   clusterst   curlinet   datat   rotNamet   startPuckert	   endPuckert   centert   variancet   rotation(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/thetaEtaPredictor.pyt   __init__&   s    	




c         C   sœ   i  } x0 |  j  j ƒ  D] } |  j | | | ƒ | | <q Wt | j ƒ  ƒ } t g  | j ƒ  D] \ } } | | | f ^ q[ ƒ } | d k r� | S| | Sd S(   s²  Calculate the probability of each rotamer for a given (theta, eta) value
        
        ARGUMENTS:
            theta  - the theta value of the point
            eta    - the eta value of the point
        OPTIONAL ARGUMENTS
            rot    - a specific rotamer
        RETURNS:
            if rot is not given, a dictionary of {rotamer => probability}
            if rot is given, the probability that the rotamer is rot
        N(   R   t   keyst   _ThetaEtaPredictor__calcRawProbt   sumt   valuest   dictt	   iteritemst   None(   R   t   thetat   etat   rott   probst   currott	   totalProbt   curprob(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/thetaEtaPredictor.pyt   calcProbE   s    5c         C   s'  |  j  | t } |  j  | t } |  j  | t } | | g } t | d | d ƒ } t | d | d ƒ }	 t | ƒ } | t | ƒ |	 t | ƒ | d <| t | ƒ |	 t | ƒ | d <d }
 d } xH d D]@ } |
 t d t	 | | ƒ 9}
 | | | d d | | 7} qÐ Wd |
 t
 | ƒ S(   sd  Calculate the responsibility of a given rotamer cluster for a given point based only on (theta, eta) values
        
        ARGUMENTS:
            theta  - the theta value of the point
            eta    - the eta value of the point
            rot    - the rotamer
        RETURNS:
            the unscaled responsibility of rot for (theta, eta)
        i    i   i   g      ð?(   i    i   (   R   t   CENTERt   VARIANCEt   ROTATIONt   subtractCoordsR    R   R   R   R   R   (   R   R%   R&   R'   R   R   t   anglet   pointt   xt   yt   productR    t   curaxis(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/thetaEtaPredictor.pyt   __calcRawProbc   s    """N(   t   __name__t
   __module__t   __doc__R   R$   R,   R   (    (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/thetaEtaPredictor.pyR      s   	c         C   s   |  | d d d S(   s  Calculate the minimum distance between two measures, keeping the appropriate sign
    
    ARGUMENTS:
        num1 - the first measurement
        num2 - the second measurement
    RETURNS:
        the distance between these two angles of no more than 180 degrees
    i´   ih  (    (   t   num1t   num2(    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/thetaEtaPredictor.pyR0   ‰   s    
N(    (   R:   t   mathR    R   R   R   R   R   R   t   STARTPUCKERt	   ENDPUCKERR-   R.   R/   R   R0   (    (    (    s_   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/thetaEtaPredictor.pyt   <module>   s   .l