Ejemplo n.º 1
0
    def setUp(self):
        self.dataDir = os.path.join(RDConfig.RDCodeDir,
                                    'Chem/Pharm3D/test_data')
        self.fdefBlock = """
                   DefineFeature HAcceptor1 [N,O;H0]
                      Family HBondAcceptor
                      Weights 1.0
                   EndFeature
                   DefineFeature HDonor1 [N,O;!H0]
                      Family HBondDonor
                      Weights 1.0
                   EndFeature
                   DefineFeature Aromatic1 c1ccccc1
                      Family Aromatic
                      Weights 1.,1.,1.,1.,1.,1.
                   EndFeature\n"""

        self.featFactory = ChemicalFeatures.BuildFeatureFactoryFromString(
            self.fdefBlock)
        self.feats = [
            ChemicalFeatures.FreeChemicalFeature(
                'HBondAcceptor', 'HAcceptor1', Geometry.Point3D(0.0, 0.0,
                                                                0.0)),
            ChemicalFeatures.FreeChemicalFeature(
                'HBondDonor', 'HDonor1', Geometry.Point3D(2.65, 0.0, 0.0)),
            ChemicalFeatures.FreeChemicalFeature(
                'Aromatic', 'Aromatic1', Geometry.Point3D(5.12, 0.908, 0.0)),
        ]
        self.pcophore = Pharmacophore.Pharmacophore(self.feats)
        self.pcophore.setLowerBound(0, 1, 2.0)
        self.pcophore.setUpperBound(0, 1, 3.3)
        self.pcophore.setLowerBound(0, 2, 5.0)
        self.pcophore.setUpperBound(0, 2, 5.4)
        self.pcophore.setLowerBound(1, 2, 2.6)
        self.pcophore.setUpperBound(1, 2, 3.0)
Ejemplo n.º 2
0
    def setUp(self):
        self.fdefBlock = \
                       """DefineFeature HAcceptor1 [N,O;H0]
                      Family HBondAcceptor
                      Weights 1.0
                   EndFeature
                   DefineFeature HDonor1 [N,O;!H0]
                      Family HBondDonor
                      Weights 1.0
                   EndFeature
                   DefineFeature Aromatic1 c1ccccc1
                      Family Aromatic
                      Weights 1.0,1.0,1.0,1.0,1.0,1.0
                   EndFeature\n"""

        self.featFactory = ChemicalFeatures.BuildFeatureFactoryFromString(
            self.fdefBlock)
        self.feats = [
            ChemicalFeatures.FreeChemicalFeature(
                'HBondAcceptor', 'HAcceptor1', Geometry.Point3D(0.0, 0.0,
                                                                0.0)),
            ChemicalFeatures.FreeChemicalFeature(
                'HBondDonor', 'HDonor1', Geometry.Point3D(2.65, 0.0, 0.0)),
            ChemicalFeatures.FreeChemicalFeature(
                'Aromatic', 'Aromatic1', Geometry.Point3D(5.12, 0.908, 0.0)),
        ]
        self.pcophore = Pharmacophore.Pharmacophore(self.feats)
Ejemplo n.º 3
0
  def testBasic(self):
    ffeat = ChemicalFeatures.FreeChemicalFeature()
    ffeat.SetId(123)
    pos = ffeat.GetId()
    self.assertTrue(pos == 123)
    ffeat.SetFamily("HBondDonor")
    self.assertTrue(ffeat.GetFamily() == "HBondDonor")
    ffeat.SetPos(geom.Point3D(1.0, 2.0, 3.0))
    pos = ffeat.GetPos()
    self.assertTrue(ptFeq(pos, geom.Point3D(1.0, 2.0, 3.0)))
    ffeat.SetType("HBondDonor1")
    self.assertTrue(ffeat.GetType() == "HBondDonor1")

    ffeat = ChemicalFeatures.FreeChemicalFeature("HBondDonor", "HBondDonor1", geom.Point3D(1.0, 2.0,
                                                                                           3.0))
    self.assertTrue(ffeat.GetId() == -1)
    self.assertTrue(ffeat.GetFamily() == "HBondDonor")
    self.assertTrue(ffeat.GetType() == "HBondDonor1")

    ffeat = ChemicalFeatures.FreeChemicalFeature("HBondDonor", "HBondDonor1",
                                                 geom.Point3D(1.0, 2.0, 3.0), id=123)
    self.assertTrue(ffeat.GetId() == 123)
    self.assertTrue(ffeat.GetFamily() == "HBondDonor")
    self.assertTrue(ffeat.GetType() == "HBondDonor1")

    pos = ffeat.GetPos()
    self.assertTrue(ptFeq(pos, geom.Point3D(1.0, 2.0, 3.0)))

    ffeat = ChemicalFeatures.FreeChemicalFeature(id=123, type="HBondDonor1", family="HBondDonor",
                                                 loc=geom.Point3D(1.0, 2.0, 3.0))
    self.assertTrue(ffeat.GetId() == 123)
    self.assertTrue(ffeat.GetFamily() == "HBondDonor")
    self.assertTrue(ffeat.GetType() == "HBondDonor1")
    pos = ffeat.GetPos()
    self.assertTrue(ptFeq(pos, geom.Point3D(1.0, 2.0, 3.0)))
Ejemplo n.º 4
0
  def test4Search(self):
    featFactory = ChemicalFeatures.BuildFeatureFactory(os.path.join(self.dataDir,
                                                        'BaseFeatures.fdef'))

    activeFeats = [ChemicalFeatures.FreeChemicalFeature('Acceptor',
                                            Geometry.Point3D(0.0, 0.0, 0.0)),
                   ChemicalFeatures.FreeChemicalFeature('Donor',
                                            Geometry.Point3D(0.0, 0.0, 0.0)),
                   ChemicalFeatures.FreeChemicalFeature('Aromatic',
                                            Geometry.Point3D(0.0, 0.0, 0.0))]
    pcophore= Pharmacophore.Pharmacophore(activeFeats)
    pcophore.setLowerBound(0,1,2.251)
    pcophore.setUpperBound(0,1,2.451)
    pcophore.setUpperBound2D(0,1,3)

    pcophore.setLowerBound(0,2,4.970)
    pcophore.setUpperBound(0,2,5.170)
    pcophore.setUpperBound2D(0,2,6)

    pcophore.setLowerBound(1,2,2.681)
    pcophore.setUpperBound(1,2,2.881)
    pcophore.setUpperBound2D(1,2,6)

    inF = gzip.open(os.path.join(self.dataDir,'cdk2-syn-clip100.pkl.gz'),'rb')
    nDone = 0
    nMatches = 0
    nHits = 0

    while 1:
      try:
        name,molPkl,boundsMat = cPickle.load(inF, encoding='latin1')
        if PY3:
          molPkl = bytes(molPkl, encoding='latin1')
      except:
        break

      nDone += 1

      mol = Chem.Mol(molPkl)
      boundsMat = rdDistGeom.GetMoleculeBoundsMatrix(mol)
      DG.DoTriangleSmoothing(boundsMat)
    
      canMatch,matches = EmbedLib.MatchPharmacophoreToMol(mol,featFactory,
                                                          pcophore)
      if canMatch:
        nMatches+=1
        r = EmbedLib.MatchPharmacophore(matches,boundsMat,pcophore,
                                        useDownsampling=True,use2DLimits=True,
                                        mol=mol)
        failed,bm,match,details = r
        if not failed:
          nHits+=1

    self.assertEqual(nDone,100)
    self.assertEqual(nMatches,93)
    #print 'nhits:',nHits
    self.assertEqual(nHits,67)
Ejemplo n.º 5
0
    def initFromLines(self, lines):
        import re
        spaces = re.compile('[\ \t]+')

        feats = []
        rads = []
        for lineNum, line in enumerate(lines):
            txt = line.split('#')[0].strip()
            if txt:
                splitL = spaces.split(txt)
                if len(splitL) < 5:
                    logger.error(
                        'Input line %d only contains %d fields, 5 are required. Read failed.'
                        % (lineNum, len(splitL)))
                    return
                fName = splitL[0]
                try:
                    xP = float(splitL[1])
                    yP = float(splitL[2])
                    zP = float(splitL[3])
                    rad = float(splitL[4])
                except ValueError:
                    logger.error(
                        'Error parsing a number of line %d. Read failed.' %
                        (lineNum))
                    return
                feats.append(
                    ChemicalFeatures.FreeChemicalFeature(
                        fName, fName, Geometry.Point3D(xP, yP, zP)))
                rads.append(rad)
        self._initializeFeats(feats, rads)
Ejemplo n.º 6
0
    def testPickle(self):
        ffeat = ChemicalFeatures.FreeChemicalFeature(
            "HBondDonor", "HBondDonor1", geom.Point3D(1.0, 2.0, 3.0), 123)
        pkl = cPickle.dumps(ffeat)
        ffeat2 = cPickle.loads(pkl, encoding='bytes')
        self.assertTrue(ffeat2.GetId() == ffeat.GetId())
        self.assertTrue(ffeat2.GetFamily() == ffeat.GetFamily())
        self.assertTrue(ffeat2.GetType() == ffeat.GetType())
        self.assertTrue(ptFeq(ffeat2.GetPos(), ffeat.GetPos()))

        # Check that the old pickled versions have not been broken
        inF = open(
            os.path.join(RDConfig.RDBaseDir,
                         'Code/ChemicalFeatures/Wrap/testData/feat.pkl'), 'rb')
        ffeat2 = cPickle.load(inF, encoding='bytes')
        # this version (1.0) does not have an id in the byte stream
        self.assertTrue(ffeat2.GetFamily() == ffeat.GetFamily())
        self.assertTrue(ffeat2.GetType() == ffeat.GetType())
        self.assertTrue(ptFeq(ffeat2.GetPos(), ffeat.GetPos()))

        # Test the new version also has the id and works as expected

        # uncomment the following to generate (overrwrite) new version of pickled
        # data file
        #cPickle.dump(ffeat,file(os.path.join(RDConfig.RDBaseDir, 'Code/ChemicalFeatures/Wrap/testData/featv2.pkl'),'wb+'))
        inF = open(
            os.path.join(RDConfig.RDBaseDir,
                         'Code/ChemicalFeatures/Wrap/testData/featv2.pkl'),
            'rb')
        ffeat2 = cPickle.load(inF, encoding='bytes')
        self.assertTrue(ffeat2.GetId() == ffeat.GetId())
        self.assertTrue(ffeat2.GetFamily() == ffeat.GetFamily())
        self.assertTrue(ffeat2.GetType() == ffeat.GetType())
        self.assertTrue(ptFeq(ffeat2.GetPos(), ffeat.GetPos()))
Ejemplo n.º 7
0
 def _initializeFeats(self, feats):
   self._feats = []
   for feat in feats:
     if isinstance(feat, ChemicalFeatures.MolChemicalFeature):
       pos = feat.GetPos()
       newFeat = ChemicalFeatures.FreeChemicalFeature(feat.GetFamily(), feat.GetType(),
                                                      Geometry.Point3D(pos[0], pos[1], pos[2]))
       self._feats.append(newFeat)
     else:
       self._feats.append(feat)
Ejemplo n.º 8
0
 def _initializeFeats(self, feats, radii):
   if len(feats) != len(radii):
     raise ValueError('len(feats)!=len(radii)')
   self._feats = []
   self._radii = []
   for feat, rad in zip(feats, radii):
     if isinstance(feat, ChemicalFeatures.MolChemicalFeature):
       pos = feat.GetPos()
       newFeat = ChemicalFeatures.FreeChemicalFeature(feat.GetFamily(), feat.GetType(),
                                                      Geometry.Point3D(pos[0], pos[1], pos[2]))
     else:
       newFeat = feat
     self._feats.append(newFeat)
     self._radii.append(rad)
Ejemplo n.º 9
0
    def to_rdkit(self) -> Tuple[rdkitPharmacophore.Pharmacophore, List[float]]:
        """ Returns an rdkit pharmacophore with the pharmacophoric_points from the original pharmacophore. 
            
            rdkit pharmacophores do not store the pharmacophoric_points radii, so they are returned as well.

            Returns
            -------
            rdkit_pharmacophore : rdkit.Chem.Pharm3D.Pharmacophore
                The rdkit pharmacophore.

            radii : list of float
                List with the radius in angstroms of each pharmacophoric point.
        """
        rdkit_element_name = { # dictionary to map openpharmacophore feature names to rdkit feature names
        "aromatic ring": "Aromatic",
        "hydrophobicity": "Hydrophobe",
        "hb acceptor": "Acceptor",
        "hb donor": "Donor",
        "positive charge": "PosIonizable",
        "negative charge": "NegIonizable",
        }

        points = []
        radii = []

        for element in self._pharmacophoric_points:
            feat_name = rdkit_element_name[element.feature_name]
            center = puw.get_value(element.center, to_unit="angstroms")
            center = Geometry.Point3D(center[0], center[1], center[2])
            points.append(ChemicalFeatures.FreeChemicalFeature(
                feat_name,
                center
            ))
            radius = puw.get_value(element.radius, to_unit="angstroms")
            radii.append(radius)

        rdkit_pharmacophore = rdkitPharmacophore.Pharmacophore(points)
        return rdkit_pharmacophore, radii