ó
Ñ%�Tc           @   s  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z 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 m Z d d
 l m Z y d d l m  Z! Wn e" k
 rn Xy d d l m# Z# Wn e" k
 rJd „  Z# n Xd d l$ 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. m/ Z/ d d l0 Z0 e j1 j2 e j1 j3 e4 ƒ ƒ Z5 e j1 j6 e5 d ƒ Z7 e% d e j1 j6 e7 d ƒ d e j1 j6 e7 d ƒ ƒ Z8 e+ e j1 j6 e7 d ƒ ƒ Z* d Z9 d Z: d Z; d Z< d Z= d Z> d Z? d Z@ d  ZA d! ZB d" ZC d# ZD d$ ZE d% ZF d& ZG d' ZH d( ZI eJ ZK eL d) g ƒ ZM eL d* jN d+ ƒ ƒ ZO eL d, jN d+ ƒ ƒ ZP eJ aQ eL d- jN d+ ƒ ƒ ZR d. „  ZS eJ d/ „ ZT eJ eJ eU eU eJ d0 „ ZV d1 „  ZW d2 „  ZX eJ d3 „ ZY eJ d4 „ ZZ d5 „  Z[ e j\ d6 ƒ Z] d7 „  Z^ d8 „  Z_ d9 „  Z` d: „  Za d; „  Zb eJ eU d< „ Zc d= „  Zd d> „  Ze d? „  Zf d@ „  Zg d& dA „ Zh dB f  dC „  ƒ  YZi d S(D   s0   Functions for calculating nucleotide coordinatesiÿÿÿÿN(   t   deepcopy(   t&   refinement_immediate_replacement_statet$   set_refinement_immediate_replacementt   accept_regularizementt   clear_all_fixed_atomst   add_extra_torsion_restraintt"   set_refine_with_torsion_restraintst$   refine_with_torsion_restraints_statet   matrix_statet
   set_matrixt   delete_all_extra_restraintst   add_extra_start_pos_restraintt   refine_zonet2   set_use_only_extra_torsion_restraints_for_torsions(   t   svn_revision(   t   monomer_restraints_py(   t   set_monomer_restraints_py(   t   refine_residues_py(   t   refine_zone_with_score_py(   t   mark_multiple_atoms_as_fixed_py(   t   regularize_zone_with_score_py(   t4   use_only_extra_torsion_restraints_for_torsions_statec           C   s   d S(   Ni    (    (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR   2   s    (   t   BuildInitSugart   rotateSugar(   t
   puckerList(   t   buildPhosOxyt   buildInitOrTerminalPhosOxy(   t   RotDataLoader(   t   HBOX_SPACINGt   VBOX_SPACINGt   createRCraneWindowObjectt   datat   c3pStrucs   c3p.pdbt   c2pStrucs   c2p.pdbs   dihedData.csvgš™™™™™¹?i   g      Ð?g      @g     ðt@g      b@g     0u@g     €A@g     À^@g     À^ÀgÍÌÌÌÌL8@i   i
   i   i   iŸ† s   O3's!   P C2' O2' C3' O3' C4' O4' C5' O5't    s   P O5' OP1 OP2s3   N1 N9 C1' C2' O2' C3' O3' C4' O4' C5' O5' P OP1 OP2c         C   sÈ  t  | |  j ƒ  d ƒ } t ƒ  } d } d } zc|  j ƒ  d k rG d S|  j ƒ  d k r® t | d d d d d d d |  j d j d |  j d j d | d |  j d j ƒnâd g } g  } t | d d d d d | d d |  j d j |  j d j |  j d j ƒ \ } }	 | \ |  j d _ |  j d _ | j	 |  j d j d |  j d j d |  j d j d	 g ƒ | j	 |	 ƒ | j
 ƒ  t }
 x7t d t | ƒ ƒ D] } t | | d p»d | | d | d | | d | | |  j | d j |  j | j |  j | d j |  j | j d
 |
 ƒ\ } }	 | \ |  j | d _ |  j | _ |  j | d _ | j	 |  j | d j d |  j | j d |  j | j d	 g ƒ | j	 |	 ƒ | j
 ƒ  t }
 q¢Wt | ƒ } t | | d påd | | d | d | | d d |  j | d j |  j | j d |  j | j d
 |
 ƒ\ } }	 | \ |  j | d _ |  j | _ |  j | d _ | j	 |	 ƒ | j
 ƒ  Wd t | ƒ X|  j ƒ  d k r¾| j ƒ  n  | | f S(   s\  Calculate coordinates for a chain of nucleotides
    
    ARGUMENTS:
        builtChain - a chain object containing phosphate and base coordinates for all nucleotides to be built
        bestPath   - a list of the desired conformers to be built
        pseudoMol  - a pseudoMolecule object currently being used to display the chain
        window     - the window contianing the GUI
    RETURNS:
        intermediateAtomLocs - phosphate and O3' coordinates for each nucleotide immediately before minimization of that nucleotide was started
        minimizationScores   - a list of the minimization scores for each nucleotide
    NOTE:
        If only a single nucleotide is being built, then we don't have a full suite, so there are no confmers
        In this case, bestPath should be a scalar containing the intended pucker of the nucleotide to be built
    i   Ni   i    t   puckert   nextResAtomst   Ps   O3's   C3't   fixPrevPhosOxy(   t   ProgressDialogObjectt   numNucst   __changeCootSettingst   Nonet   __minCoordst   nucleotidest   typet   atomst   nucst   appendt   progresst   Truet   xranget   lent   Falset   __restoreCootSettingst   drawExtraBonds(   t
   builtChaint   bestPatht	   pseudoMolt   windowt   progressDialogt   origCootSettingst   intermediateAtomLocst   minimizationScorest	   newCoordst   scoreR&   t   resNum(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt
   calcCoords|   sh    		
 @
/5D

!5c         C   ss  | j  |  ƒ } | j  | ƒ } t | | | d ƒ }	 x t j ƒ  rT t j t ƒ q8 Wt ƒ  }
 t | ƒ } d G|  Gd G| GHd g } g  } z½| d k rŠd GHt	 | d d |  | j
 d j d | d d | j d j | j d j | j d j d | ƒ\ } } | \ | j d _ | j d _ | j | j
 d j d | j
 d j d | j
 d j d	 g ƒ | j | ƒ | d 7} d
 } |	 j ƒ  n d } t } x{t | | ƒ D]j} t	 | | d k rÒ| j
 | d
 j n d | j
 | d j | j
 | j | j
 | d j | | d | | | j
 | d j | j
 | j | j
 | d j | j
 | j d | d | ƒ\ } } | \ | j | d _ | j | _ | j | d _ | j | j
 | d j d | j
 | j d | j
 | j d	 g ƒ | j | ƒ |	 j ƒ  | d 7} t } t } q¦W| } | d t | j
 ƒ k  r<t	 | | d k rY| j
 | d
 j n d | j
 | d j | j
 | j | j
 | d j | | d | | | j
 | d j | j
 | j | j
 | d j | j
 | j d t d | d | ƒ\ } } | \ | j | d _ | j | _ | j | d _ | j | ƒ n  |	 j ƒ  Wd t |
 ƒ |	 j ƒ  X| j ƒ  | | f S(   sœ  Recalculate coordinates (using different rotamers) for part of a chain of nucleotides starting with a chain built by calcCoords
    
    ARGUMENTS:
        startingRes             - the first residue to rebuild
        endingRes               - the last residue to rebuilt
        rots                    - what rotamers to use for the rebuild
        origCoords              - a chain object containing the current coordinates
        pseudoMol               - a pseudoMolecule object currently being used to display the chain
        window                  - the window contianing the GUI
    OPTIONAL ARGUMENTS:
        ignoreDensity           - ignore the density when performing the minimization
                                  defaults to False
    RETURNS:
        intermediateAtomLocs - phosphate and O3' coordinates for each nucleotide immediately before minimization of that nucleotide was started
        minimizationScores   - a list of the minimization scores for each nucleotide
    NOTE:
        Currently, the return values from this function are only used for initial rotamerize minimization.  They are ignored for all
        other calls to this function
    i   s   Recalculating t   -i    s   Rebuilding initial nucleotidet   ignoreDensityR%   s   O3's   C3'i   R&   t   nextResAlreadyBuiltN(   t   resIndexR'   t   gtkt   events_pendingt   main_iterationR5   R)   R    R*   R+   R/   RB   R,   R-   R.   R0   R1   R2   R3   R4   R6   t   restoreWindowR7   (   t   startingRest	   endingRest   rotst
   origCoordsR:   R;   RE   t   startingResIndext   endingResIndexR<   R=   R8   R>   R?   R@   RA   t	   rotsIndexR&   RG   t   firstRes(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   recalcCoords÷   sr     		
 @
R/5D


R/5

c   '      C   s~  |  j  } d } d } d } d } t s0 d } n  t | t ƒ rP t | d } n# | d k	 rm t | d } n | } t j |
 | ƒ } |  j | | ƒ | d k	 rû t	 |
 j
 ƒ  | j
 ƒ  ƒ } t | ƒ } |  j | | ƒ t | | ƒ } |  j | | ƒ n  |  j | p
| | ƒ } |  j ƒ  } t |  | | | | ƒ t | | | | | | | | | ƒ	 t r¶t | | | | | | | | | | ƒ
 | d k	 r¶t | | | |  j | d t ƒƒ q¶n  t | | | pÎ| pÎ| | | ƒ } | } d } | t k rÓ|  j | p| | ƒ } |  j | p| | ƒ } |  j | p1| | | t ƒ t | |
 d ƒ } |  j | | ƒ t | | | px| px| | | ƒ } | | k r¬|  j | pŸ| | | ƒ që|  j | p»| | ƒ } | } d } n |  j | pâ| | ƒ } | t k rÀ|  j | p| | ƒ }  |  j | p| | | t ƒ t | |
 d ƒ }! |  j | |! ƒ t | | | pe| pe| | | ƒ } | | k r™|  j | pŒ| | |  ƒ qÀ|  j | p¨| | ƒ } | } d } n  t r.d GHd GHd GHd	 t | ƒ GHd
 t | ƒ GHd t | ƒ GHd GHd | d d t | ƒ d GHd GHn  | d k rj|  j | ƒ }" | d k r�| \ }# }$ t |# ƒ }% |% d k	 rg|  j | |% ƒ qgqj|  j  |" d ƒ d k r*|  j! |" d ƒ r*| \ }& }# }$ t" |# |& ƒ } | d k	 rù|  j | | ƒ n  t |$ |# ƒ } | d k	 rg|  j | | ƒ qgqj| \ }& }# }$ t" |# |& ƒ }% |% d k	 rj|  j | |% ƒ qjn  t# | ƒ | | f S(   sæ	  Build a single nucleotide and minimize its coordinates.
    
    ARGUMENTS:
        pseudoMol           - the pseudoMolecule object currently being used to display the chain
        prevPrevResNum      - the residue number of residue i-2
                              only used if we're using Phenix's pucker-specific restraints, in which case it's needed
                              to restrain the non-bridging oxygens of residue prevResNum
        prevResNum          - the residue number of the previous residue using Coot numbering (i.e. starting at 1, not 0)
        curResNum           - the residue number to build using Coot numbering (i.e. starting at 1, not 0)
        nextResNum          - the residue number of the next residue using Coot numbering (i.e. starting at 1, not 0)
        curRot              - the rotamer to use when building the current residue
        nextRot             - the rotamer to use when building the next residue
        prevResType         - the residue type (i.e. A, G, C, or U) of the previous residue
        curResType          - the residue type (i.e. A, G, C, or U) of the current residue
        nextResType         - the residue type (i.e. A, G, C, or U) of the next residue
        curResAtoms         - a hash of atom coordinates for the current residue
    OPTIONAL ARGUMENTS:
        nextResAlreadyBuilt - should be True if the next residue is already built (i.e. if we are being
                              called from recalcCoords instead of calcCoords)
                              Defaults to False.
        fixPrevPhosOxy      - whether to fix the phosphoryl oxygens of the previous nucleotide during minimization
        pucker              - the sugar pucker for this nucleotide.  Only necessary when both curRot
                              and nextRot are None (i.e. when we're building the only nucleotide of the chain)
                              Defaults to None
        nextResAtoms        - a hash of atoms containing coordinates for the next phosphate.
                              Should only be provided if we are building the only nucleotide of the chain
                              Defaults to None
        ignoreDensity       - ignore the density when performing the minimization
                              defaults to False
    RETURNS:
        newCoords           - new atomic coordinates calculated by the minimization procedure, formatted as a list of dictionaries
        score               - the score received from Coot's minimization
    i   i    t   stript   antit   syns	   high-antis    ********************************s    *  Starting struc  *   Score   *s   * Anti             *  %s  *s   * Syn              *  %s  *s   * High-anti        *  %s  *s   * Using %s minimizationR"   i	   s    *N($   t   chainR*   t   USE_PHENIX_RESTRAINTSt
   isinstancet   strR   t   sugarBuildert
   buildSugart   addSugart   dictt   itemsR   t
   addPhosOxyt   getCootNucst   molNumt
   __fixAtomst   __setTorsionRestraintst   __addPhenixRestraintst   __fixEntireResiduet   getAtomNamesR5   t   __runRefinementt   SYN_REBUILDING_CUTOFFt	   updateRest   setCootNucsR   t   replaceSugart   HIGH_ANTI_REBUILDING_CUTOFFt   PRINT_SUMMARY_TABLEt   __stringifyScoreR4   RG   t   numAtomsFromIndext   connectedToNextFromIndexR   t   __clearResRestraints('   R:   t   prevPrevResNumt
   prevResNumt	   curResNumt
   nextResNumt   curRott   nextRott   prevResTypet
   curResTypet   nextResTypet   curResAtomsRF   R&   R#   R$   RE   RX   t   antiRefinementScoret   synRefinementScoret   highAntiRefinementScoret   selectedStartingStruct	   curPuckert   initSugarCoordst	   sugarAndPt   phosOxyCoords5t   phosOxyCoords3t   preMinimizationStrucRc   RA   R@   t   antiRefinementStruct   synSugarCoordst   prevRefinementStruct   highAntiSugarCoordst   curResIndext   newCurResAtomst   newNextResAtomst   phosOxyCoordst   newPrevResAtoms(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR+   }  s¬    +		"%($
$	$	-
c         C   s,   |  t  k r d S|  d k	 r$ d |  Sd Sd S(   sÊ   Format a refinement score for printing in the summary table
    ARGUMENTS:
        score - the refinement
    RETURNS:
        score formatted as a fixed length string or "Not run" if score is None
    s    Faileds   %7.3fs   Not runN(   t   REFINEMENT_FAIL_SCORER*   (   RA   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRp   \  s
    c      	   C   sÒ  |  j  ƒ  } |  j } t | ƒ \ } } t | ƒ \ }	 }
 t | ƒ \ } } g  } | d k	 rÕ | rl t } n t j d d g ƒ } xQ |  j | d t ƒD]7 } | j ƒ  | k r— | j	 | | | | d g ƒ q— q— Wn  t
 | | |	 |
 d d t ƒ t
 | | | | d d t ƒ xN |  j |	 d t ƒD]7 } | j ƒ  t k r#t
 | | |	 |
 | d t ƒ q#q#W| d k	 r¾xQ |  j | d t ƒD]7 } | j ƒ  t k r€| j	 | | | | d g ƒ q€q€Wn  t | | d ƒ d S(   s$  Fix and restrain the appropriate atoms for minimization
    
    ARGUMENTS:
        pseudoMol           - the PseudoMolecule object representing the molecule to fix atoms in
        prevResNumFull      - the residue number of the previous residue using Coot numbering (i.e. starting at 1, not 0) including insertion code
        curResNumFull       - the residue number using Coot numbering (i.e. starting at 1, not 0) including insertion code
        nextResNumFull      - the residue number of the next residue using Coot numbering (i.e. starting at 1, not 0) including insertion code
    OPTIONAL ARGUMENTS:
        fixPrevPhosOxy      - whether to fix the phosphoryl oxygens of the previous nucleotide during minimization
                              Defaults to False
    RETURNS:
        None
    t   OP1t   OP2RU   t    s    P  i   N(   Rc   RX   t   __splitResNumR*   t   PREV_RES_MOBILE_ATOMSt   unionRh   R5   RU   R0   R   t   HARMONIC_STD_DEV_PHOSPHATEt   CUR_RES_MOBILE_ATOMSt   HARMONIC_STD_DEV_BASEt   NEXT_RES_MOBILE_ATOMSt   mark_multiple_atoms_as_fixed(   R:   t   prevResNumFullt   curResNumFullt   nextResNumFullR&   Rc   RX   Ru   t   prevResInsCodeRv   t   curResInsCodeRw   t   nextResInsCodet   fixListt   prevResMobileAtomst   curAtom(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRd   l  s.    		&#&c	   #      C   sf	  t  | ƒ \ }	 }
 t  | ƒ \ } } t  | ƒ \ } } | d k	 r·t | t ƒ r t j | ƒ } t j | ƒ } t j | ƒ } t j | ƒ } t j	 | ƒ } t j
 | ƒ } t j | ƒ t } t j | ƒ t } t j | ƒ t } t j | ƒ t } t j | ƒ t } t j | ƒ t } n` | d } | d } | d } | d } | d } | d } t } t } t } t } t } t } | d k	 rÞt |  | |	 |
 d d | |	 |
 d	 d | |	 |
 d
 d | |	 |
 d d | | d ƒ n  | d k	 r<t |  | |	 |
 d	 d | |	 |
 d
 d | |	 |
 d d | | | d d | | d ƒ n  | d k	 ršt |  | |	 |
 d
 d | |	 |
 d d | | |
 d d | | | d d | | d ƒ n  | d k	 røt |  | | | d d | | | d d | | | d d | |	 |
 d d | | d ƒ n  | d k	 rVt |  | | | d d | | | d d | | | d d | | | d	 d | | d ƒ n  | d k	 r·t |  | | | d d | | | d d | | | d	 d | | | d
 d | | d ƒ q·n  d } d } t | t ƒ rt | d } t j | ƒ } t j | ƒ t } nB t | t ƒ rGt | d } t j | ƒ } t j | ƒ t } n  | d k	 r¥t |  | | | d d | | | d	 d | | | d
 d | | | d d | | d ƒ n  | d k	 rÔ| d k rÒt } t } t }  n t } t } t }  t |  | | | d	 d | | | d d | | | d d | | | d d | t d ƒ t |  | | | d d | | | d d | | | d d | | | d
 d | t d ƒ t |  | | | d d | | | d	 d | | | d d | | | d d |  t d ƒ n  | d k sì| d k rûd }! d }" n d }! d }" t |  | | | d d | | | d d | | | |! d | | | |" d t t d ƒ | d k	 rb	t | t ƒ r't j | ƒ } t j | ƒ t } | r€t j | ƒ } t j | ƒ } t j	 | ƒ } t j
 | ƒ } t j | ƒ t } t j | ƒ t } t j | ƒ t } t j | ƒ t } q€nY | d } t } | r€| d } | d } | d } | d } t } t } t } t } n  | d k	 rÞt |  | | | d	 d | | | d
 d | | | d d | | | d d | | d ƒ n  | rb	| d k	 rBt |  | | | d
 d | | | d d | | | d d | | | d d | | d ƒ n  | d k	 r t |  | | | d d | | | d d | | | d d | | | d d | | d ƒ n  | d k	 rþt |  | | | d d | | | d d | | | d d | | | d	 d | | d ƒ n  | d k	 r_	t |  | | | d d | | | d d | | | d	 d | | | d
 d | | d ƒ q_	qb	n  d S(   sç  Set the appropriate torsional restraints for the minimization
    
    ARGUMENTS:
        molNum              - the Coot molecule number
        chain               - the chain name
        prevResNumFull      - the residue number of the previous residue including insertion codes
        curResNumFull       - the residue number to build including insertion codes
        nextResNumFull      - the residue number of the next residue including insertion codes
        curRot              - the rotamer to use when building the current residue
        nextRot             - the rotamer to use when building the next residue
        curResType          - the residue type (i.e. A, G, C, or U) of the current residue
    OPTIONAL ARGUMENTS:
        nextResAlreadyBuilt - should be True if the next residue is already built (i.e. if we are being
                              called from recalcCoords instead of calcCoords)
                              Defaults to False.
    RETURNS:
        None
    i    i   i   i   i   i   s    C5'R”   s    C4's    C3's    O3's    P  s    O5's    O4's    C1's    C2't   Gt   As    N9 s    C4 s    N1 s    C2 N(    R•   R*   RZ   R[   t   rotDatat   prevDeltaMeant   epMeant   zetaMeant	   alphaMeant   betaMeant	   gammaMeant   prevDeltaSDt   TORSION_STD_DEV_MODt   epSDt   zetaSDt   alphaSDt   betaSDt   gammaSDt   MANUAL_TORSION_STD_DEVR   R   t   curDeltaMeant
   curDeltaSDt   NU0_C2t   NU1_C2t   NU4_C2t   NU0_C3t   NU1_C3t   NU4_C3t
   NU_STD_DEVt   CHI_MEANt   CHI_STD_DEV(#   Rc   RX   R�   Rž   RŸ   Rx   Ry   R{   RF   Ru   R    Rv   R¡   Rw   R¢   t	   prevDeltat   ept   zetat   alphat   betat   gammaR¯   R±   R²   R³   R´   Rµ   R‚   t   deltat   deltaSDt   curNu0t   curNu1t   curNu4t	   baseAtom1t	   baseAtom2(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRe   °  sR   





		




	c   
      C   s‰   d GHd G| Gd G| GHd GHt  | ƒ \ } } t  | ƒ \ } } | r` t |  | | | d ƒ }	 n t |  | | | d ƒ }	 t ƒ  t |	 ƒ S(   sO  Run the minimization using Coot's built-in refinement functions
    
    ARGUMENTS:
        molNum              - the Coot molecule number
        chain               - the chain name
        startingResNumFull  - the residue number (including insertion code) of the first nucleotide to refine
        endingResNumFull    - the residue number (including insertion code) of the last nucleotide to refine
    OPTIONAL ARGUMENTS:
        ignoreDensity - ignore the density when performing the minimization
                        defaults to False
    RETURNS:
        the refinement score
    s   *************************s   About to refine residuesRD   R”   (   R•   t   regularize_zone_with_scoret   refine_zone_with_scoreR   t   __calcRefinementScore(
   Rc   RX   t   startingResNumFullt   endingResNumFullRE   t   startingResNumt   startingResInsCodet   endingResNumt   endingResInsCodet   refinementResults(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRi   ¯  s    c         C   s   t  |  ƒ t |  ƒ d S(   sÁ   Clear the torsional restraints and fixed atoms that applied to the residues we were just minimizing
    
    ARGUMENTS:
        molNum  - the Coot molecule number
    RETURNS:
        None
    N(   R   R
   (   Rc   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRs   Ò  s    
s   (-?\d+)([A-Za-z]?)$c         C   sK   |  d k r d St j t |  ƒ ƒ j ƒ  \ } } t | ƒ } | | f Sd S(   s!  Split a residue number into number and insertion code
    
    ARGUMENTS:
        resNumFull - a residue number potentially contianing an insertion code
    RETURNS:
        resNum     - the residue number without the insertion code (as an int)
        insCode    - the insertion code
    N(   NN(   R*   t   __splitResNumREt   matchR[   t   groupst   int(   t
   resNumFullRB   t   insCode(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR•   á  s
    
!c          C   sì  i  }  xßd# D]×} t  | ƒ |  | <t  | ƒ } g  | d <x | d D] } d | d <qD Wt t k r×g  | d	 D]N } | d
 j ƒ  t k o´ | d j ƒ  t k o´ | d j ƒ  t k so | ^ qo | d	 <| d	 j d d d d d g d d d d d g d d d d d g d d d d d g d d d d d g g ƒ g  | d D]8 } | d
 j ƒ  t k ol| d j ƒ  t k s=| ^ q=| d <| d j d d d d d g d d d d d g d d d d d g g ƒ d  | d! d" <n  t | | ƒ q W|  S($   s¹   Change the monomer restraints for RNA residues
    
    ARGUMENTS:
        None
    RETURNS:
        origRestraints - the original monomer restraints (so we can restore them later)
    R§   R¦   t   Ct   Ut   _chem_comp_tort   _chem_comp_plane_atomgü©ñÒMbP?i   t   _chem_comp_anglei    i   s    C2's    C3's    C4'gfffff¦Y@g      ð?s    C5's    O5's    P  gš™™™™9^@s    OP1gÍÌÌÌÌ¬[@s    OP2gfffffæ]@t   _chem_comp_bondt   singleg°rh‘í|ù?g¸…ëQ¸Ž?t   delocgÃõ(\�Â÷?g{®Gáz”?R”   t
   _chem_compi   (   R§   R¦   Rß   Rà   (   t   monomer_restraintsRY   R2   RU   t   PHENIX_NEW_RESTRAINT_ATOMSt   extendt   set_monomer_restraints(   t   origRestraintst   curNuct   newRestraintst   curPlanet   curAnglet   curBond(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   __changeMonomerRestraintsó  s*    	
cMc          C   sh   t  ƒ  }  t ƒ  } t ƒ  } t ƒ  } t d ƒ t d ƒ t t ƒ t d ƒ t	 ƒ  } |  | | | | f S(   sƒ  Change Coot's minimization settings
    
    ARUGMENTS:
        None
    RETURNS:
        these values are returned so the minimization settings can be restored later
        origImmediateReplaceValue    - the original refinement_immediate_replacement_state() value
        origRefineWithTorsionsValue  - the original refine_with_torsion_restraints_state()
        origWeightValue              - the original matrix_state() value
        origMonomerRestraints        - the original monomer restraints (i.e. torsion restraints)
        origUseOnlyExtraTorsionValue - the original settings for use_only_extra_torsion_restraints_for_torsions
    i   i    (
   R   R   R   R   R   R   R	   t   REFINE_MAP_WEIGHTR   Rò   (   t   origImmediateReplaceValuet   origRefineWithTorsionsValuet   origWeightValuet   origUseOnlyExtraTorsionValuet   origMonomerRestraints(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR)   +  s    				



	c         C   sc   |  \ } } } } } t  | ƒ t | ƒ t | ƒ t | ƒ x d D] } t | | | ƒ qD Wd S(   sµ   Restore Coot's minimization settings
    
    ARGUMENTS:
        origCootSettings - Coot settings to be restored (as returned by __changeCootSettings)
    RETURNS:
        None
    R§   R¦   Rß   Rà   N(   R§   R¦   Rß   Rà   (   R   R   R	   R   Rë   (   R=   Rô   Rõ   Rö   Rø   R÷   Rí   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR6   V  s    	



c         C   sP   |  d } | s t  Sd } x/ | D]' } | d d k r! | | d 7} q! q! W| S(   s  Calculate an overall score for the quality of the refinement results
    
    ARGUMENTS:
        refinementResults - the output of refine_residues()
    RETURNS:
        overallScore      - an overall refinement score based on the bond length, angles, and torsions
    i   i    t   Bondst   Anglest   Torsions(   Rù   Rú   Rû   (   R‘   (   RØ   t	   scoreListt   overallScoret   curScore(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRÑ   i  s    
c         C   sþ  | d k s | d k r! d } n d } d
 } d
 } d
 } t | t ƒ r[ t | \ } } nb t | t ƒ r½ | d d k  rƒ d } n d } t | t ƒ s½ | d	 d k  r± d } qº d } q½ n  t | t ƒ rß t | \ } } n_ t | t ƒ r>| d
 k r| d d k  rd } qd } n  | d	 d k  r5d } q>d } n  |
 d
 k	 r_| d
 k r_|
 } n  t | ƒ \ } } t | ƒ \ } } t | ƒ \ } } t | ƒ \ } } t j |  | | | | | | | | | |	 ƒ t j |  | | | | | | | | |	 ƒ
 d
 S(   sÙ  Add angle and bond restraints for the specified nucleotides using the Phenix pucker-specific restraint values
    
    ARGUMENTS
        molNum              - the Coot molecule number
        chain               - the chain name
        prevPrevResNumFull  - the residue number of residue i-2 (needed to restrain the non-bridging oxygens of residue prevResNum)
        prevResNumFull      - the residue number of the previous residue using Coot numbering (i.e. starting at 1, not 0)
        curResNumFull       - the residue number to build using Coot numbering (i.e. starting at 1, not 0)
        nextResNumFull      - the residue number of the next residue using Coot numbering (i.e. starting at 1, not 0)
        curRot              - the rotamer to use when building the current residue
        nextRot             - the rotamer to use when building the next residue
        curResType          - the residue type (i.e. A, G, C, or U) of the current residue
        OPTIONAL ARGUMENTS:
        nextResAlreadyBuilt - should be True if the next residue is already built (i.e. if we are being
                              called from recalcCoords instead of calcCoords)
                              Defaults to False.
        onlyPucker          - should only be provided if curRot and nextRot are None.  This is used when we're only building
                              a single nucleotide, so we don't have a full suite and therefore can't predict a conformer
    RETURNS:
        None
    R§   R¦   s    N9 s    N1 i    g      \@i   i   i   N(	   R*   RZ   t
   basestringR   t   listR•   t   phenixRestraintst   setAngleRestraintst   setBondRestraints(   Rc   RX   t   prevPrevResNumFullR�   Rž   RŸ   Rx   Ry   R{   RF   t
   onlyPuckert   glycosidicBaseAtomt
   prevPuckerR‚   t
   nextPuckerRt   t   prevPrevResInsCodeRu   R    Rv   R¡   Rw   R¢   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRf   ‡  sD    								+c           C   s+   t  ƒ  d k r t a d GHt Sd GHt Sd S(   s  Enable Phenix's pucker-specific restraints for minimization
    
    ARGUMENTS:
        None
    RETURNS:
        True if enabling the restraints succceeded, False otherwise
    NOTE:
        Phenix restraints are only available in versions of Coot newer than 3926
    iV  s   Using Phenix restraintss\   Coot must be newer than 0.7-pre r3926 to use Phenix restraints.  Using Coot/CCP4 restraints.N(   R   R2   RY   R5   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   enablePhenixRestraintsá  s    c           C   s   t  a d GHt S(   sÕ   Disable Phenix's pucker-specific restraints for minimization and use the Coot/CCP4 restraints instead
    
    ARGUMENTS:
        None
    RETURNS:
        True (for consistancy with enablePhenixRestraints())
    s   Using Coot/CCP4 restraints(   R5   RY   R2   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   disablePhenixRestraintsø  s    
c           C   s   t  S(   sß   Check if we are using Phenix's pucker-specific restraints for minimization
    
    ARGUMENTS:
        None
    RETURNS:
        True if we are using Phenix restraints
        False if we are using Coot/CCP4 restraints
    (   RY   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   usingPhenixRestraints  s    
c         C   sN   t  | ƒ \ } } g  | D] } | | d | d g ^ q } t |  | d ƒ d S(   s^  Fix all atoms in a residue
    
    ARGUMENTS:
        molNum      - the molecule number of the residue to fix
        chain       - the chain of the residue to fix
        resNumFull  - the residue number of the residue to fix including insertion code
        atomList    - a list of all atoms in the specified residue
    RETURNS:
        None
    R”   i   N(   R•   Rœ   (   Rc   RX   RÝ   t   atomListRB   RÞ   R¥   R£   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRg     s    (c         C   s�   | d k rE |  j  | d | ƒ } | j d j } | j d j } n( |  j  | | ƒ } | j d j } d } t | | ƒ } |  j | | ƒ d S(   s  Build the non-bridging oxygens onto a terminal nucleotide
    
    ARGUMENTS:
        resIndex - the index of the residue to build the phosphates onto
    OPTIONAL ARGUMENTS:
        direction - which side of the residue to add the phosphoryl oxygens to
    RETURNS:
        NONE
    i   i   i    N(   t!   createPartialChainObjectFromIndexR,   R.   R*   R   t   addPhosOxyFromIndex(   R:   RG   t	   directionRX   t   prevResAtomsR}   R�   (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   buildOnlyPhosOxy%  s    R'   c           B   s5   e  Z d  Z d „  Z d „  Z d d „ Z d „  Z RS(   sZ   A class for displaying a progress dialog while calculating/minimizing backbone coordinatesc         C   sº   | |  _  d |  _ d |  _ d |  _ d |  _ d |  _ | |  _ d |  _ |  j  d k	 r  |  j  j	 ƒ  } | d k	 r¬ | j
 ƒ  |  _ | |  _ |  j  j | ƒ q¬ n t ƒ  |  _  |  j ƒ  d S(   sb  Initialize a ProgressDialogObject object
        
        ARGUMENTS:
            window   - the window object to display the progress dialog in
                       if None, a new window will be created
            totalNum - the total number of steps for the progress bar
                       akin to the deprecated set_discrete_blocks functionality
        RETURNS:
            an initialized ProgressDialogObject object
        EFFECTS:
            If provided, the contents of window are replaced by a progress bar
            The contents of window can later be restored using restoreWindow()
        i    N(   t   _ProgressDialogObject__windowR*   t$   _ProgressDialogObject__oldWindowSizet(   _ProgressDialogObject__oldWindowContentst,   _ProgressDialogObject__progressFracIncrementt&   _ProgressDialogObject__curProgressFract%   _ProgressDialogObject__curProgressIntt   _ProgressDialogObject__totalNumt"   _ProgressDialogObject__progressbart	   get_childt   get_sizet   removeR   t+   _ProgressDialogObject__createProgressDialog(   t   selfR;   t   totalNumt   windowChild(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   __init__H  s     									c         C   sù   t  j t t ƒ } |  j j | ƒ |  j j d d ƒ t  j d ƒ } | j t  j	 ƒ | j
 | t t t ƒ t  j ƒ  |  _ |  j j d t |  j ƒ d ƒ t  j d d d d ƒ } | j |  j ƒ | j
 | t t t ƒ |  j j ƒ  d	 |  j |  _ d
 S(   s|   Add the ProgressBar to self.__window
        
        ARGUMENTS:
            None
        RETURNS:
            None
        i   s=   
  Calculating backbone coordinates...  

  Please wait...  
s   Built 0 of s    nucleotidest   xscalegÍÌÌÌÌÌì?t   xaligng      à?g      ð?N(   RH   t   VBoxR5   R   R  t   addt   resizet   Labelt   set_justifyt   JUSTIFY_CENTERt
   pack_startR   t   ProgressBarR  t   set_textR[   R  t	   Alignmentt   show_allR  (   R  t	   windowBoxt   calculatingLabelt   progressAlign(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   __createProgressDialogl  s    	!i   c         C   s‰   |  j  | |  j 7_  |  j | 7_ |  j  d k r@ d |  _  n  |  j j |  j  ƒ |  j j d t |  j ƒ d t |  j ƒ d ƒ d S(   s¼   Increment the progress bar
        
        OPTIONAL ARGUMENTS:
            step - how many steps to increment the progress bar by, defaults to 1
        RETURNS:
            None
        i   s   Built s    of s    nucleotidesN(   R  R  R  R  t   set_fractionR-  R[   R  (   R  t   step(    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR1   ˆ  s    	c         C   ss   |  j  j ƒ  } |  j  j | ƒ |  j d k	 rD |  j  j |  j ƒ n  |  j d k	 ri |  j  j |  j Œ  n  d }  d S(   sP  Restore the original contents of the window and destroy the ProgressDialogObject object
        
        ARGUMENTS:
            None
        RETURNS:
            None
        EFFECTS:
            the original contents of the window are destroyed
            the ProgressDialogObject is set to None, since it is no longer useful
        N(   R  R  R  R  R*   R&  R  R'  (   R  R!  (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyRK   œ  s    (   t   __name__t
   __module__t   __doc__R"  R  R1   RK   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyR'   E  s
   	$	(j   R8  t   os.patht   ost   reRH   t   copyR    t   cootR   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   Rè   R   Rë   R   t   refine_residuesR   RÐ   R   Rœ   R   RÏ   t   ImportErrorR   t   buildInitSugarR   R   R   R   R   R¨   R   t   guiUtilsR   R   R   R  t   patht   dirnamet   abspatht   __file__t
   rcranePatht   joint   dataPathR\   R°   R¶   R˜   Rš   R¼   R½   R¾   R¹   Rº   R»   RÀ   RÁ   R¿   Ró   Rj   Rn   R‘   R5   Ro   t	   frozensetR–   t   splitR™   R›   RY   Ré   RC   RT   R*   R+   Rp   Rd   Re   Ri   Rs   t   compileRÙ   R•   Rò   R)   R6   RÑ   Rf   R
  R  R  Rg   R  R'   (    (    (    sX   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/calcCoords.pyt   <module>   s–   X	{†ß		Dÿ#			8	+		Z				 