Ejemplo n.º 1
0
    def test1bPoint2D(self):
        pt = geom.Point2D()
        self.assertTrue(feq(pt.x, 0.0))
        self.assertTrue(feq(pt.y, 0.0))

        pt = geom.Point2D(3., 4.)
        self.assertTrue(feq(pt.x, 3.0))
        self.assertTrue(feq(pt.y, 4.0))

        self.assertTrue(feq(pt.x, 3.0))
        self.assertTrue(feq(pt.y, 4.0))
        self.assertTrue(feq(pt[0], 3.0))
        self.assertTrue(feq(pt[1], 4.0))
        self.assertTrue(feq(pt[-2], 3.0))
        self.assertTrue(feq(pt[-1], 4.0))
        lst = list(pt)
        self.assertTrue(feq(lst[0], 3.0))
        self.assertTrue(feq(lst[1], 4.0))

        pt2 = geom.Point2D(1., 1.)

        pt3 = pt + pt2
        self.assertTrue(feq(pt3.x, 4.0))
        self.assertTrue(feq(pt3.y, 5.0))

        pt += pt2
        self.assertTrue(feq(pt.x, 4.0))
        self.assertTrue(feq(pt.y, 5.0))

        pt3 = pt - pt2
        self.assertTrue(feq(pt3.x, 3.0))
        self.assertTrue(feq(pt3.y, 4.0))

        pt -= pt2
        self.assertTrue(feq(pt.x, 3.0))
        self.assertTrue(feq(pt.y, 4.0))

        pt *= 2.0
        self.assertTrue(feq(pt.x, 6.0))
        self.assertTrue(feq(pt.y, 8.0))

        pt /= 2
        self.assertTrue(feq(pt.x, 3.0))
        self.assertTrue(feq(pt.y, 4.0))
        self.assertTrue(feq(pt.Length(), 5.0))
        self.assertTrue(feq(pt.LengthSq(), 25.0))
        pt.Normalize()
        self.assertTrue(feq(pt.Length(), 1.0))

        pt1 = geom.Point2D(1.0, 0.0)
        pt2 = geom.Point2D(2.0 * math.cos(math.pi / 6),
                           2.0 * math.sin(math.pi / 6))
        ang = pt1.AngleTo(pt2)
        self.assertTrue(feq(ang, math.pi / 6))

        prod = pt1.DotProduct(pt2)
        self.assertTrue(feq(prod, 2.0 * math.cos(math.pi / 6)))
Ejemplo n.º 2
0
def GenerateDepictionMatching2DStructure(mol,
                                         reference,
                                         confId=-1,
                                         referencePattern=None,
                                         acceptFailure=False,
                                         **kwargs):
    """ 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

  """
    if reference and referencePattern:
        if not reference.GetNumAtoms(
                onlyExplicit=True) == referencePattern.GetNumAtoms(
                    onlyExplicit=True):
            raise ValueError(
                'When a pattern is provided, it must have the same number of atoms as the reference'
            )
        referenceMatch = reference.GetSubstructMatch(referencePattern)
        if not referenceMatch:
            raise ValueError("Reference does not map to itself")
    else:
        referenceMatch = range(reference.GetNumAtoms(onlyExplicit=True))
    if referencePattern:
        match = mol.GetSubstructMatch(referencePattern)
    else:
        match = mol.GetSubstructMatch(reference)

    if not match:
        if not acceptFailure:
            raise ValueError('Substructure match with reference not found.')
        else:
            coordMap = {}
    else:
        conf = reference.GetConformer()
        coordMap = {}
        for i, idx in enumerate(match):
            pt3 = conf.GetAtomPosition(referenceMatch[i])
            pt2 = rdGeometry.Point2D(pt3.x, pt3.y)
            coordMap[idx] = pt2
    Compute2DCoords(mol, clearConfs=True, coordMap=coordMap, canonOrient=False)
Ejemplo n.º 3
0
    def testPointPickles(self):
        pt = geom.Point3D(2.0, -3.0, 1.0)
        pt2 = pickle.loads(pickle.dumps(pt))
        self.assertTrue(feq(pt.x, pt2.x, 1e-6))
        self.assertTrue(feq(pt.y, pt2.y, 1e-6))
        self.assertTrue(feq(pt.z, pt2.z, 1e-6))

        pt = geom.Point2D(2.0, -4.0)
        pt2 = pickle.loads(pickle.dumps(pt))
        self.assertTrue(feq(pt.x, pt2.x, 1e-6))
        self.assertTrue(feq(pt.y, pt2.y, 1e-6))
Ejemplo n.º 4
0
    def testPointPickles(self):
        pt = geom.Point3D(2.0, -3.0, 1.0)
        pt2 = cPickle.loads(cPickle.dumps(pt))
        self.failUnless(feq(pt.x, pt2.x, 1e-6))
        self.failUnless(feq(pt.y, pt2.y, 1e-6))
        self.failUnless(feq(pt.z, pt2.z, 1e-6))

        pt = geom.Point2D(2.0, -4.0)
        pt2 = cPickle.loads(cPickle.dumps(pt))
        self.failUnless(feq(pt.x, pt2.x, 1e-6))
        self.failUnless(feq(pt.y, pt2.y, 1e-6))
Ejemplo n.º 5
0
def demo(old_mol):
    """
    Utility function for illustrating the application of rules.

    See also companion module rules_demo.py
    """

    new_mol = None

    for rule in rule_set:

        products = rule["rxn"].RunReactants((old_mol, ))

        if len(products):

            new_mol = products[0][0]

            logger.debug("rule {n} '{name}' applied".format(n=rule['n'],
                                                            name=rule['name']))

            break

    if not new_mol:

        logger.warn("No hits for mol!")

        return None, None, None, None

    Chem.SanitizeMol(new_mol)

    AllChem.Compute2DCoords(old_mol)

    conf = old_mol.GetConformer()

    old_pat, new_pat = rule["SMARTS"].split(">>")

    old_match = old_mol.GetSubstructMatch(Chem.MolFromSmarts(old_pat))
    new_match = new_mol.GetSubstructMatch(Chem.MolFromSmarts(new_pat))

    coord_map = {
        new_idx: rdGeometry.Point2D(
            conf.GetAtomPosition(old_idx).x,
            conf.GetAtomPosition(old_idx).y)
        for old_idx, new_idx in zip(old_match, new_match)
    }

    AllChem.Compute2DCoords(new_mol,
                            clearConfs=True,
                            coordMap=coord_map,
                            canonOrient=False)

    return old_mol, old_match, new_mol, new_match
Ejemplo n.º 6
0
 def test8InitPoint2DFromPoint3D(self):
     p3 = geom.Point3D(1., 2., 3.)
     p2 = geom.Point2D(p3)
     self.assertEqual(p2.x, p3.x)
     self.assertEqual(p2.y, p3.y)