ó
a#nSc           @   s÷  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l Td d l Td d l	 Td d l
 Td d l Td d l Td d l Td d l Td d l Td d l Ty d d l TWn n Xd d l Td d l Td d l m Z e e _ e e _ d d l Z d d l Z d d	 l m Z e ƒ  Z d e d
 „ Z d d d d „ Z d d d d „ Z d d e d „ Z! d d „ Z" d d d d „ Z# d „  Z$ e% d d e& d „ Z' d „  Z( d „  Z) e* d k ród d l+ Z+ e) ƒ  \ Z, Z- e+ j. e, ƒ n  d S(   s%    Import all RDKit chemistry modules

iÿÿÿÿ(   t   rdBase(   t   RDConfig(   t   DataStructs(   t
   rdGeometry(   t   *(   t
   ForceFieldN(   t   loggerc         C   s¦   |  j  | ƒ } t | | ƒ | s¢ | d k r7 d } n  g  |  j ƒ  D] } | j ƒ  ^ qD } x* | D]" } | | k sc |  j | ƒ qc qc W|  j  | ƒ j d ƒ n  d S(   sŒ     Applies the transformation (usually a 4x4 double matrix) to a molecule
  if keepConfs is False then all but that conformer are removed

  iÿÿÿÿi    N(   t   GetConformert   TransformConformert   GetConformerst   GetIdt   RemoveConformert   SetId(   t   molt   tformt   confIdt	   keepConfst   refConft   ct
   allConfIdst   id(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   TransformMol)   s     	% i   g      à?c         K   s>   t  j | d | d | d d | ƒ} t |  | | | � | S(   s9    returns a grid representation of the molecule's shape
  i    i   i   t   spacing(   R   t   UniformGrid3Dt   EncodeShape(   R   R   t   boxDimR   t   kwargst   res(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   ComputeMolShape8   s    'gš™™™™™É?g       @c         C   s#  t  j |  ƒ }  |  j | ƒ } t | ƒ t | ƒ } | d j | d j d | | d j | d j d | | d j | d j d | f } t j	 | d | d | d d | ƒ} t
 |  | | d t d d ƒ| d } | j ƒ  }	 g  |	 D] }
 |
 d k rñ d ^ qñ } | t | ƒ } | S(	   su    Calculates the volume of a particular conformer of a molecule
  based on a grid-encoding of the molecular shape.

  i   i    i   R   t   ignoreHst   vdwScaleg      ð?i   (   t   rdchemt   MolR   t   CanonicalizeConformert   ComputeConfBoxt   xt   yt   zR   R   R   t   Falset   GetOccupancyVectt   len(   R   R   t   gridSpacingt	   boxMargint   conft   boxt   sideLent   shapet   voxelVolt   occVectR#   t   voxelst   vol(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   ComputeMolVolume?   s    
#	
%c         K   sD  | r` | r` | j  d t ƒ | j  d t ƒ k s< t d ‚ n  | j | ƒ } | sx t d ‚ qx n t | j  d t ƒ ƒ } | r� |  j | ƒ } n |  j | ƒ } | sÀ | s· t d ‚ q$i  } nd | j ƒ  }	 i  } xO t | ƒ D]A \ }
 } |	 j | |
 ƒ } t j	 | j
 | j ƒ } | | | <qß Wt |  d t d | d t ƒd S(	   s   Generates a depiction for a molecule where a piece of the molecule
     is constrained to have the same coordinates as a reference.

     This is useful for, for example, generating depictions of SAR data
     sets so that the cores of the molecules are all oriented the same
     way.

  Arguments:
    - mol:          the molecule to be aligned, this will come back
                    with a single conformer.
    - reference:    a molecule with the reference atoms to align to;
                    this should have a depiction.
    - confId:       (optional) the id of the reference conformation to use
    - referencePattern:  (optional) an optional molecule to be used to
                         generate the atom mapping between the molecule
                         and the reference.
    - acceptFailure: (optional) if True, standard depictions will be generated
                     for molecules that don't have a substructure match to the
                     reference; if False, a ValueError will be raised

  t   onlyExplicitsR   When a pattern is provided, it must have the same number of atoms as the references    Reference does not map to itselfs,   Substructure match with reference not found.t
   clearConfst   coordMapt   canonOrientN(   t   GetNumAtomst   Truet
   ValueErrort   GetSubstructMatcht   rangeR   t	   enumeratet   GetAtomPositionR   t   Point2DR#   R$   t   Compute2DCoordsR&   (   R   t	   referenceR   t   referencePatternt   acceptFailureR   t   referenceMatcht   matchR6   R+   t   it   idxt   pt3t   pt2(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt$   GenerateDepictionMatching2DStructureT   s*    $	c         K   s®   |  j  ƒ  } g  } | j | ƒ } xg t | ƒ D]Y } | j | ƒ } xA t | d | ƒ D], }	 | j |	 ƒ }
 | j | |
 j ƒ  ƒ qW Wq. Wt j | ƒ } t |  | | � d S(   sã   Generates a depiction for a molecule where a piece of the molecule
     is constrained to have coordinates similar to those of a 3D reference
     structure.

  Arguments:
    - mol:          the molecule to be aligned, this will come back
                    with a single conformer.
    - reference:    a molecule with the reference atoms to align to;
                    this should have a depiction.
    - confId:       (optional) the id of the reference conformation to use

  i   N(	   R8   R   R<   R>   t   appendt   Lengtht   numpyt   arrayt   Compute2DCoordsMimicDistmat(   R   RA   R   R   t   nAtst   dmR+   RF   t   pit   jt   pj(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt$   GenerateDepictionMatching3DStructureˆ   s    c         C   sê   | sq |  j  | d t ƒ} | sI t d |  j d ƒ | j d ƒ f ‚ n  g  | D] } t t | ƒ ƒ ^ qP } n  d } xD | D]< } t | |  | | d | ƒ}	 |	 | k  r~ |	 } | }
 q~ q~ W|
 | k ræ t | |  | | d |
 ƒn  | S(   sK   Returns the optimal RMS for aligning two molecules, taking
  symmetry into account. As a side-effect, the probe molecule is
  left in the aligned state.

  Arguments:
    - ref: the reference molecule
    - probe: the molecule to be aligned to the reference
    - refConfId: (optional) reference conformation to use
    - probeConfId: (optional) probe conformation to use
    - maps: (optional) a list of lists of (probeAtomId,refAtomId)
      tuples with the atom-atom mappings of the two molecules.
      If not provided, these will be generated using a substructure
      search.

  t   uniquifys   mol %s does not match mol %st   _Nameg     @�@t   atomMap(   t   GetSubstructMatchesR&   R:   t   GetPropt   listR=   t   AlignMol(   t   reft   probet	   refConfIdt   probeConfIdt   mapst   matchesRE   t   bestRMSt   amapt   rmst   bestMap(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt
   GetBestRMS£   s    (c         #   sŒ   t  | ƒ |  j ƒ  k r: t d t  | ƒ |  j ƒ  f ‚ n  d ‡  f d † ‰  x9 ˆ  | ƒ D]+ } |  j | ƒ } x | D] } | Vqu WqY Wd S(   s�   Returns a generator for the virtual library defined by
   a reaction and a sequence of sidechain sets

  >>> from rdkit import Chem
  >>> from rdkit.Chem import AllChem
  >>> s1=[Chem.MolFromSmiles(x) for x in ('NC','NCC')]
  >>> s2=[Chem.MolFromSmiles(x) for x in ('OC=O','OC(=O)C')]
  >>> rxn = AllChem.ReactionFromSmarts('[O:2]=[C:1][OH].[N:3]>>[O:2]=[C:1][N:3]')
  >>> r = AllChem.EnumerateLibraryFromReaction(rxn,[s2,s1])
  >>> [Chem.MolToSmiles(x[0]) for x in list(r)]
  ['CNC=O', 'CCNC=O', 'CNC(C)=O', 'CCNC(C)=O']

  Note that this is all done in a lazy manner, so "infinitely" large libraries can
  be done without worrying about running out of memory. Your patience will run out first:

  Define a set of 10000 amines:
  >>> amines = (Chem.MolFromSmiles('N'+'C'*x) for x in range(10000))

  ... a set of 10000 acids
  >>> acids = (Chem.MolFromSmiles('OC(=O)'+'C'*x) for x in range(10000))

  ... now the virtual library (1e8 compounds in principle):
  >>> r = AllChem.EnumerateLibraryFromReaction(rxn,[acids,amines])

  ... look at the first 4 compounds:
  >>> [Chem.MolToSmiles(r.next()[0]) for x in range(4)]
  ['NC=O', 'CNC=O', 'CCNC=O', 'CCCNC=O']


  s#   %d sidechains provided, %d requiredi    c         3   sy   xr |  | D]f } | d t  |  ƒ k  ri ˆ  |  | d ƒ } x4 | D]! } | g } | j | ƒ | VqA Wq | g Vq Wd  S(   Ni   (   R(   t   extend(   t   itemst   deptht   itemt   vt   entryt   l(   t   _combiEnumerator(    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyRo   ç   s    	N(   R(   t   GetNumReactantTemplatesR:   t   RunReactants(   t   reactiont   sidechainSetst   chainst   prodSetst   prods(    (   Ro   sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   EnumerateLibraryFromReactionÅ   s    "
i&	  c         K   sê  |  j  | ƒ } | s! t d ‚ n  i  } | j | ƒ }	 x3 t | ƒ D]% \ }
 } |	 j |
 ƒ } | | | <qC Wt |  d | d | | �} | d k  rŸ t d ‚ n  g  t | ƒ D] \ }
 } | |
 f ^ q¬ } | sº| |  d d ƒ} xx t | ƒ D]j \ }
 } x[ t |
 d t | ƒ ƒ D]@ } | | } | | j | | ƒ } | j	 | | | | d ƒ qWqï W| j
 ƒ  d	 } | j ƒ  } x& | r¡| r¡| j ƒ  } | d 8} q|Wt |  | d
 | ƒ} nt |  | d
 | ƒ} | |  d d ƒ} | j ƒ  } xn t | j ƒ  ƒ D]Z }
 | j |
 ƒ } | j | j | j | j d t ƒd } | j	 | | |
 d d d ƒ q W| j
 ƒ  d	 } | j d d d d ƒ } x2 | rº| rº| j d d d d ƒ } | d 8} q‰Wt |  | d
 | ƒ} |  j d t | ƒ ƒ |  S(   s½   generates an embedding of a molecule where part of the molecule
  is constrained to have particular coordinates

  Arguments
    - mol: the molecule to embed
    - core: the molecule to use as a source of constraints
    - useTethers: (optional) if True, the final conformation will be
        optimized subject to a series of extra forces that pull the
        matching atoms to the positions of the core atoms. Otherwise
        simple distance constraints based on the core atoms will be
        used in the optimization.
    - coreConfId: (optional) id of the core conformation to use
    - randomSeed: (optional) seed for the random number generator


    An example, start by generating a template with a 3D structure:
    >>> from rdkit.Chem import AllChem
    >>> template = AllChem.MolFromSmiles("c1nn(Cc2ccccc2)cc1")
    >>> AllChem.EmbedMolecule(template)
    0
    >>> AllChem.UFFOptimizeMolecule(template)
    0

    Here's a molecule:
    >>> mol = AllChem.MolFromSmiles("c1nn(Cc2ccccc2)cc1-c3ccccc3")

    Now do the constrained embedding
    >>> newmol=AllChem.ConstrainedEmbed(mol, template)

    Demonstrate that the positions are the same:
    >>> newp=newmol.GetConformer().GetAtomPosition(0)
    >>> molp=mol.GetConformer().GetAtomPosition(0)
    >>> list(newp-molp)==[0.0,0.0,0.0]
    True
    >>> newp=newmol.GetConformer().GetAtomPosition(1)
    >>> molp=mol.GetConformer().GetAtomPosition(1)
    >>> list(newp-molp)==[0.0,0.0,0.0]
    True

  s   molecule doesn't match the coreR6   t
   randomSeedi    s   Could not embed molecule.R   i   g      Y@i   RX   t   fixedt	   energyTolg-Cëâ6?t   forceTolgü©ñÒMbP?t   EmbedRMS(   R;   R:   R   R=   R>   t   EmbedMoleculeR<   R(   t   Distancet   AddDistanceConstraintt
   Initializet   MinimizeR\   R8   t   AddExtraPointR#   R$   R%   R9   t   SetPropt   str(   R   t   coret
   useTetherst
   coreConfIdt
   randomseedt   getForceFieldR   RE   R6   t   coreConfRF   t   idxIt   corePtIt   ciRS   t   algMapt   fft   idxJt   dt   nt   moreRe   R+   t   pt   pIdx(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   ConstrainedEmbedö   sT    *+ 
!
(!
c         C   sh  t  j |  ƒ } t  j | ƒ } | j | ƒ } | sdxI | j ƒ  D]; } | j ƒ  t j k r@ | j t j ƒ | j t	 ƒ q@ q@ Wx1 | j ƒ  D]# } | j t j ƒ | j t	 ƒ qŒ Wx! | j
 ƒ  D] } | j d ƒ qÀ Wx! | j
 ƒ  D] } | j d ƒ qä W| j | d t	 ƒ} | rUt | ƒ d k r8t j d ƒ n  | d } xl |  j ƒ  D]^ } | | j ƒ  } | | j ƒ  } | j | | ƒ }	 |	 j | j ƒ  ƒ |	 j | j ƒ  ƒ qOWxy |  j
 ƒ  D]k } | j | | j ƒ  ƒ }
 |
 j | j ƒ  ƒ |
 j | j ƒ  ƒ |
 j | j ƒ  ƒ |
 j | j ƒ  ƒ q¾Wt | ƒ t | d ƒ rad | _ qaqdt d ƒ ‚ n  | S(   sF   assigns bond orders to a molecule based on the
      bond orders in a template molecule

  Arguments
    - refmol: the template molecule
    - mol: the molecule to assign bond orders to

    An example, start by generating a template from a SMILES
    and read in the PDB structure of the molecule
    >>> from rdkit.Chem import AllChem
    >>> template = AllChem.MolFromSmiles("CN1C(=NC(C1=O)(c2ccccc2)c3ccccc3)N")
    >>> mol = AllChem.MolFromPDBFile(os.path.join(RDConfig.RDCodeDir, 'Chem', 'test_data', '4DJU_lig.pdb'))
    >>> print len([1 for b in template.GetBonds() if b.GetBondTypeAsDouble() == 1.0])
    8
    >>> print len([1 for b in mol.GetBonds() if b.GetBondTypeAsDouble() == 1.0])
    22

    Now assign the bond orders based on the template molecule
    >>> newMol = AllChem.AssignBondOrdersFromTemplate(template, mol)
    >>> print len([1 for b in newMol.GetBonds() if b.GetBondTypeAsDouble() == 1.0])
    8

    Note that the template molecule should have no explicit hydrogens
    else the algorithm will fail.

    It also works if there are different formal charges (this was github issue 235):
    >>> template=AllChem.MolFromSmiles('CN(C)C(=O)Cc1ccc2c(c1)NC(=O)c3ccc(cc3N2)c4ccc(c(c4)OC)[N+](=O)[O-]')
    >>> mol = AllChem.MolFromMolFile(os.path.join(RDConfig.RDCodeDir, 'Chem', 'test_data', '4FTR_lig.mol'))
    >>> AllChem.MolToSmiles(mol)
    'COC1CC(C2CCC3C(C2)NC2CCC(CC(O)N(C)C)CC2NC3O)CCC1N(O)O'
    >>> newMol = AllChem.AssignBondOrdersFromTemplate(template, mol)
    >>> AllChem.MolToSmiles(newMol)
    'COc1cc(-c2ccc3c(c2)Nc2ccc(CC(=O)N(C)C)cc2NC3=O)ccc1[N+](=O)[O-]'

  i    RV   i   s2   More than one matching pattern found - picking onet
   __sssAtomss   No matching foundN(    R   R    R;   t   GetBondst   GetBondTypet   BondTypet   SINGLEt   SetBondTypet   SetIsAromaticR&   t   GetAtomst   SetFormalChargeRY   R(   R   t   warningt   GetBeginAtomIdxt   GetEndAtomIdxt   GetBondBetweenAtomst   GetIsAromatict   GetAtomWithIdxt   GetIdxt   SetHybridizationt   GetHybridizationt   SetNumExplicitHst   GetNumExplicitHst   GetFormalCharget   SanitizeMolt   hasattrt   NoneR—   R:   (   t   refmolR   t   refmol2t   mol2t   matchingt   bt   at   atom1t   atom2t   b2t   a2(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   AssignBondOrdersFromTemplateS  sJ    $

c          C   s,   d d  l  }  d d  l } |  j | j d ƒ S(   Niÿÿÿÿt   __main__(   t   doctestt   syst   testmodt   modules(   R»   R¼   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   _test«  s    Rº   (   i   i   i   (/   t   __doc__t   rdkitR    R   R   t   rdkit.GeometryR   t
   rdkit.Chemt   rdkit.Chem.rdPartialChargest   rdkit.Chem.rdDepictort   rdkit.Chem.rdForceFieldHelperst   rdkit.Chem.ChemicalFeaturest   rdkit.Chem.rdDistGeomt   rdkit.Chem.rdMolAlignt   rdkit.Chem.rdMolTransformst   rdkit.Chem.rdShapeHelperst   rdkit.Chem.rdChemReactionst   rdkit.Chem.rdSLNParset   rdkit.Chem.rdMolDescriptorst   rdkit.Chem.rdqueriesR   R@   R    t   ComputeGasteigerChargesRM   t   ost   rdkit.RDLoggerR   R&   R   R   R3   R®   RJ   RU   Rg   Rw   R9   t   UFFGetMoleculeForceFieldR–   R¹   R¿   t   __name__R¼   t   failedt   triedt   exit(    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/rdkit/Chem/AllChem.pyt   <module>   sV   











			2"	1\	X	