ó
Ñ%�Tc           @   s:  d  Z  d d l Z d d l m Z d d l m Z d d l m Z m Z m	 Z	 d d l
 m Z m Z e j j e j j e ƒ ƒ Z e j j e d ƒ Z e d e j j e d	 ƒ d
 e j j e d ƒ d e j j e d ƒ d e j j e d ƒ d e j j e d ƒ ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   sN   Determine the sequence of rotamers for a structure using a hidden Markov modeliÿÿÿÿN(   t   log(   t   PseudoPredictor(   t
   puckerListt   rotListt   rotsByPucker(   t   lnt   negInft   datat   thetaEtas   thetaEtaClusts.csvt   puckers   smoothedPuckerDist.csvt	   sugarDists   sugarDists.csvt   startPhosDists   startingPDists.csvt   endPhosDists   endingPDists.csvc         C   s    g  } x� |  j  ƒ  D]s } t j d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d | j	 ƒ  ƒ } | j
 | ƒ q Wt | ƒ } | | f S(   s´  Determine the best sequence of rotamers for a structure.
    
    ARGUMENTS:
        builtChain      - a Chain object containing phosphate and base coordinates
    RETURNS:
        bestPath        - a list of the most likely rotamer for each suite
        predictedProbs  - the probability for each suite for each rotamer formatted as a list of dictionaries
                          predictedProbs[suiteNum][rotamer] = probability
    t   thetat   etat
   startPperpt   endPperpR
   R   R   (   t   suitest   pseudoPredict   calcProbR   R   t   startingPperpt   endingPperpR
   t   startingPhosDistt   endingPhosDistt   appendt
   rotamerHMM(   t
   builtChaint   predictedProbst   curSuitet   curProbst   bestPath(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyt   determineRotamerSeq$   s    c            sŽ  t  |  ƒ } g  t | ƒ D] } i  ^ q ‰ g  t | ƒ D] } i  ^ q8 } x* t D]" ‰ t |  d ˆ ƒ ˆ d ˆ <qQ Wx˜ t d | ƒ D]‡ ‰  x~ t D]v ‰ t ˆ ˆ  d d ‡  ‡ ‡ f d †  ƒ} | | ˆ  ˆ <ˆ ˆ  d | t | ˆ ƒ t |  ˆ  ˆ ƒ ˆ ˆ  ˆ <q” Wq‡ Wd g | } | d ‰  t ˆ ˆ  d ‡  ‡ f d †  ƒ| ˆ  <x8 t | d d d ƒ D]  ‰  | ˆ  | ˆ  | ˆ  d <qfW| S(   s•  Given rotamer likelihoods for all suites, predict the most likely rotamer string using a Hidden Markov Model
    
    ARGUMENTS:
        predictedProbs  - the probability for each suite for each rotamer formatted as a list of dictionaries
                          predictedProbs[suiteNum][rotamer] = probability
    RETURNS:
        bestPath        - a list of the most likely rotamer for each suite
    i    i   t   keyc            s   ˆ ˆ  d |  t  |  ˆ ƒ S(   Ni   (   t   __transitionProb(   t   prevRot(   t   curPost   curRott	   pathProbs(    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyt   <lambda>W   s    c            s   ˆ ˆ  |  S(   N(    (   R$   (   R#   R%   (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyR&   `   s    iÿÿÿÿN(   t   lent   xrangeR   R   t   maxR!   t   None(   R   t	   numSuitest   it   patht   bestPrevRotR   (    (   R#   R$   R%   sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyR   A   s     
 )A
&c         C   s(   t  |  d t  | d k r  d St Sd S(   sf  Calculate the log of the transition probability between two rotamers
    
    ARGUMENTS:
        startingRot - the rotamer to transition from
        endingRot   - the rotamer to transition to
    RETURNS:
        0 if the ending pucker of the starting rotamer is the same as the starting pucker of the ending rotamer
        negative infinity otherwise
    i   i    N(   R   R   (   t   startingRott	   endingRot(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyR!   j   s    c         C   s   t  j |  ƒ S(   s  Predict only the pucker (used when the user has only built a single nucleotide)
    
    ARGUMENTS:
        pperp - the base-phosphate perpendicular (P-perp) distance
    RETURNS:
        3 if a C3'-endo sugar pucker is more likely, 2 otherwise
    NOTE:
        This function simply calls the calcPucker() function from the pseudoPredictor module.  The
        determinePucker() function exists only so the traceGui module doesn't have to separately import
        and initialize the pseudoPredictor module.
    (   R   t
   calcPucker(   t   pperp(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyt   determinePucker{   s    c            s$   t  t |  | d ‡  ‡ f d †  ƒS(   sï  Determine the best conformer for a suite given required starting and ending sugar puckers
    
    ARGUMENTS:
        leadingPucker   - the pucker of the starting sugar of the suite (either 2 or 3)
        endingPucker    - the pucker of the ending sugar of the suite (either 2 or 3)
        suiteNum        - the number of the suite
        predictedProbs  - the probability for each suite for each rotamer, as returned by determineRotamerSeq
    RETURNS:
        the most likely conformer
    R    c            s   ˆ  ˆ |  S(   N(    (   t   rot(   R   t   suiteNum(    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyR&   –   s    (   R)   R   (   t   leadingPuckert   endingPuckerR5   R   (    (   R   R5   sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyt   determineAlternateConfŠ   s    (   t   __doc__t   os.patht   ost   mathR    t   pseudoPredictorR   R   R   R   t   safelogR   R   R-   t   dirnamet   abspatht   __file__t   dataPatht   joinR   R   R   R!   R3   R8   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerSeq.pyt   <module>   s"   		)		