ó
Ñ%�Tc           @   s«  d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z m	 Z	 m
 Z
 m Z m Z m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z m Z d d
 l m Z d d l m Z d d l m Z d d l m  Z  d d l! Z! d d l" m# Z# m$ Z$ m% Z% m& Z& d Z' d a) d a* e+ a, d a- e+ d „ Z. e+ d „ Z/ d „  Z0 e+ e+ d „ Z1 d „  Z2 d „  Z3 d „  Z4 d „  Z5 d „  Z6 d S(   sD   Recalculate coordinates for an already-built stretch of nucleotides.iÿÿÿÿ(   t   deepcopyN(   t   add_status_bar_textt   new_generic_object_numbert   set_display_generic_objectt   to_generic_object_add_linet   graphics_drawt   clear_pending_pickst   imol_refinement_map(   t   user_defined_click_py(   t   PseudoMoleculet   PseudoMoleculeError(   t   determineRotamerSeq(   t   recalcCoords(   t   createRCraneWindowObjectt   createRCraneErrorDialog(   t   ReviewSuitesGui(   t   BOND_LIST_FULL(   t   selectMapDialog(   t   createCitationPopup(   t   reportPDB2Errort   reportInsCodeErrort   STANDARD_BASESt   reportModifiedNucErrort
   yellowtinti   c            sœ   t  ƒ  t j ƒ  t r= d GHt d ƒ t ƒ  t ƒ  t a n[ ˆ  rf t ƒ  d k rf d GHt	 t
 ƒ d Sd GHt d ƒ t ƒ  t a t d ‡  f d †  ƒ d S(	   s  Prompt the user to pick two atoms to rotamerize a stretch of nucleotides.  Alternatively, if
    we're already waiting for the user to pick atoms, then cancel the pending picks.
    
    ARGUMENTS:
        None
    OPTIONAL ARGUMENTS:
        ignoreDensity - ignore the density when performing the minimization
                        defaults to False
    RETURNS:
        None
    s   Rotamerize cancellediÿÿÿÿs+   No refinement map set for RCrane rotamerizeNs'   Click on 2 atoms (in the same molecule)s2   Pick 2 atoms [Ctrl Left-mouse rotates the view]...i   c            s   t  |  | ˆ  ƒ S(   N(   t   rotamerizeFromAtomSpecs(   t	   atomSpec1t	   atomSpec2(   t   ignoreDensity(    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyt   <lambda>c   s    (   R   t   extendChaint   clearPendingExtendChaint   waitingForClicksR   R   t   __setMenuToRotamerizet   FalseR   R   t	   pickAtomst   __setMenuToCancelt   Truet   user_defined_click(   R   (    (   R   sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyR"   9   s"    

	

c   '      C   sa  t  ƒ  t a |  d \ } } } } } } | d \ }	 }
 } } } } | |	 k rd d GHt d ƒ t S| |
 k rƒ d GHt d ƒ t S| } | } t | ƒ t | ƒ } t | ƒ t | ƒ } y t d | d | d t ƒ } Wn- t k
 r	} | GHt t | ƒ d ƒ t SX| j | ƒ } | j | ƒ } | | k r^| | } } | | } } | | } } n  | j	 | ƒ r{t
 | ƒ t S| d k rÙ| j ƒ  t d	 d
 d d d g ƒ k rÙ| j | ƒ rÙ| d 8} | j | ƒ } n  | d | j ƒ  k  r,| j ƒ  d k r,| j | ƒ r,| d 7} | j | ƒ } n  | | k rKd GHt d ƒ t Sx… t | | ƒ D]t } | j | ƒ s[d | j | ƒ d | j | d ƒ d GHt d | j | ƒ d | j | d ƒ d ƒ t Sq[Wx° t | | d ƒ D]› } | j | ƒ } | t k r"t | j | ƒ | ƒ t S| j | ƒ } | d& k	 rçd | j | ƒ d | d GHt d | j | ƒ d | d ƒ t SqçW| d | j ƒ  k s¬| j | ƒ rç| d 8} | j | ƒ } | | k rçd GHt d ƒ t Sn  xK t | | d ƒ D]6 } | j | ƒ d d k rût | j | ƒ ƒ t SqûW| | d } t d k r´| t k r´d GHd G| Gd Gt GHd GHd t | ƒ d GHd GHt d  t | ƒ d! t t ƒ ƒ t S| j | | t ƒ \ } } } t | ƒ \ } } d" d j | ƒ GHd& }  | d& k rd& g | }! n | j | ƒ }  |  g | }! d& }" | d& k rV|! d& g }! n | j | ƒ }" |! |" g }! | j | | ƒ | j  | p‘| | pš| ƒ t! | ƒ }# t" ƒ  }$ | j# | | ƒ | j$ | pÒ| | pÛ| ƒ t% | | |! | | |$ | ƒ \ }% }& | d& k	 rd& g |% }% n  | j& ƒ  t' | | | | |% |& |$ t |# d# | d$ |  d% |" ƒ	d& S('   s¸  Rotamerize a stretch of nucleotides
    
    ARGUMENTS:
        atomSpec1 - an atom spec specifying the start of the stretch
        atomSpec2 - an atom spec specifying the end of the stretch
    OPTIONAL ARGUMENTS:
        ignoreDensity - ignore the density when performing the minimization
                        defaults to False
    RETURNS:
        False if there is a problem with the selected nucleotides
        None otherwise
    i   s-   Rotamerize atoms are not in the same molecules*   Rotamerize atoms are not in the same chaint   createFromMoleculet   chaint   batonss   

Rotamerization canceled.i    t   Pt   OP1t   OP2s   O5's   C5's   O3's@   You must select at least one suite (two residues) to rotamerize.s   Nucleotides s    and s'    are not connected.  Cannot rotamerize.s   Nucleotide s    is missing sL    atom.  Cannot rotamerize without phosphate and glycosidic bond coordinates.s4   Cannot rotamerize this suite without a 3' phosphate.i   t    s-   Warning:  Too many nucleotides to rotamerize.s      Trying to rotamerizes'   nucleotides and your current maximum issB      To increase the maximum, go to Calculate -> Scripting -> Pythons/      and type rcrane.setRotamerizeMaxNucleotides(t   )sF      or type rcrane.setRotamerizeMaxNucleotides(-1) to remove the limit.s<   Warning:  Too many nts to rotamerize.  Trying to rotamerize s    nts and your current max is s   Suite string: R   t   prevTorsionst   nextTorsionsN((   R    R!   R   R   t   strR	   R
   R   t   resIndext   checkPDB2FromIndexR   t   stript	   frozensett   connectedToPrevFromIndext
   resNumFullt	   getNumNtst   connectedToNextFromIndext   ranget   resTypeFromIndexR   R   t   checkPhosAndGlycosidicFromIndext   Nonet   resNumR   t   MAX_NUCLEOTIDES_TO_ROTAMERIZEt   createPartialChainObjectR$   R   t   joint   calcSuiteTorsionst	   saveBfacst   saveCoordinatest   __drawOrigCoordsR   t	   resetNucst   setExtraBondRangeR   t   restoreSavedBfacsR   ('   R   R   R   t   mol1t   chain1t   startResNumt   startInsCodet	   startAtomt   alt1t   mol2t   chain2t	   endResNumt
   endInsCodet   endAtomt   alt2t   molNumR'   t   startResFullt
   endResFullt	   pseudoMolt   errt
   startIndext   endIndext   curIndext
   curResTypet   missingAtomt   numNucsToRotamerizet   chainObjt   resNum5pt   resNum3pt   bestPatht   predictedProbsR.   t   rebuildRotsR/   t   oldCoordsObjt   windowt   intermediateAtomLocst   minimizationScores(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyR   f   sÐ    


'


+0#&

"
	$
c   
      C   s5  t  d ƒ } t | d ƒ g  } d } xÞ |  j D]Ó } xo t d t | j D]X \ } } | j | ƒ rK | j | ƒ rK | j | } | j | } | j | | ƒ qK qK W| d k	 rÝ | j d ƒ rÝ | j | | j d ƒ n  | j d ƒ rü | j d } q/ d } q/ Wx! | D] }	 t	 | t
 d |	 Œ qWt ƒ  | S(   s  Display the original pre-rotamerize coordinates as a generic object
    
    ARGUMENTS:
        origCoords - a chain object containing the atoms to be drawn
    RETURNS:
        drawNum - the generic object number
    EFFECTS:
        creates and displays a new Coot generic object
    s   Pre-rotamerize coordinatesi   t   backboneR)   s   O3'i   N(   R   R   R<   t   nucsR   t   typet   hasAtomt   atomst   appendR   t   ORIG_COORDS_COLORR   (
   t
   origCoordst   drawNumt   lineListt   prevO3t   curNuct   atom1t   atom2t   atom1Coordst   atom2Coordst   curLine(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyRD   K  s&    "
c         C   s6   | r |  a  n |  a | r2 t a t ƒ  t ƒ  n  d S(   sæ  Store the "Rotamerize existing structure..." and "Rotamerize without density..." menu entries so that we can later change them to "Cancel current rotamerize..."
    
    ARGUMENTS:
        menuItem   - the gtk MenuItem to store
    OPTIONAL ARGUMENTS:
        resetLabel    - whether we should reset the menu label to "Rotamerize existing structure..."
                        (and also clear all pending atom picks in Coot)
                        This is intended for use when we're reloading RCrane
                        Defaults to False
        ignoreDensity - whether this is the menu entry for "Rotamerize without density..."
    RETURNS:
        None
    EFFECTS:
        stores menuItem in the module-level variable __menuItem
    N(   t   __menuItemNoDensityt
   __menuItemR!   R   R    R   (   t   menuItemt
   resetLabelR   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyt   storeMenuItemy  s    	c           C   sH   t  d k	 r" t  j ƒ  j d ƒ n  t d k	 rD t j ƒ  j d ƒ n  d S(   s£   Set the menu entry labels to "Rotamerize existing structure..." and "Rotamerize without density..."
    
    ARGUMENTS:
        None
    RETURNS:
        None
    s    Rotamerize existing structure...s   Rotamerize without density...N(   R{   R<   t	   get_childt   set_textRz   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyR    š  s    	c           C   sH   t  d k	 r" t  j ƒ  j d ƒ n  t d k	 rD t j ƒ  j d ƒ n  d S(   sz   Set the menu entry label to "Cancel current rotamerize..."
    
    ARGUMENTS:
        None
    RETURNS:
        None
    s   Cancel current rotamerize...N(   R{   R<   R   R€   Rz   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyR#   ª  s    
c         C   s   t  |  ƒ a d S(   s	  Set the maximum number of nucleotides that can be rotamerized at once
    
    ARGUMENTS:
        newVal - the new maximum number of nucleotides to rotamerize at once
                 if this value is <= 0, then there will be no limit
    RETURNS:
        None
    N(   t   intR>   (   t   newVal(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyt   setRotamerizeMaxNucleotides»  s    c           C   s   t  S(   sá   Get the maximum number of nucleotides that can be rotamerized at once
    
    ARGUMENTS:
        None
    RETURNS
        MAX_NUCLEOTIDES_TO_ROTAMERIZE - the maximum number of nucleotides that can be rotamerized at once
    (   R>   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyt   getRotamerizeMaxNucleotidesÉ  s    	c           C   s+   t  r# d GHt ƒ  t ƒ  t a  t St Sd S(   sÈ   Cancel any pending rotamerize call (intended to be called by extendChain)
    
    ARGUMENTS:
        None
    RETURNS:
        True if there was a pending rotamerize call
        False otherwise
    s   Rotamerize cancelledN(   R   R   R    R!   R$   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyt   clearPendingRotamerizeÕ  s    (7   t   __doc__t   copyR    t   gtkt   os.patht   ost   cootR   R   R   R   R   R   R   R   R%   t   pseudoMoleculeR	   R
   t
   rotamerSeqR   t
   calcCoordsR   t   guiUtilsR   R   t   reviewSuitesGuiR   t
   bondLengthR   R   t   citationPopupR   R   t   reportInputErrorsR   R   R   R   Ro   R<   R{   Rz   R!   R   R>   R"   R   RD   R~   R    R#   Rƒ   R„   R…   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/rotamerize.pyt   <module>   s:   4"-å	.!				