def get_hydrogen_bonding(self):
    """Gets hydrogen bonding character for all atoms.

    Returns:
      A dict mapping RDKit Atom indices to a HydrogenBonding object. Atom
      indices not in the dict are neither acceptors nor donors.

    Raises:
      TypeError: if more than one atom index is associated with the same
        acceptor or donor.
    """
    self.check_indices()
    factory = ChemicalFeatures.BuildFeatureFactoryFromString(_HBOND_FEATURE_DEF)
    features = factory.GetFeaturesForMol(self.mol)
    hb = collections.defaultdict(lambda: HydrogenBonding(False, False))
    for feat in features:
      family = feat.GetFamily().lower()
      if family in ['acceptor', 'donor']:
        if len(feat.GetAtomIds()) != 1:
          raise TypeError('More than one atom index for %s.' % family)
        idx = feat.GetAtomIds()[0]
        # pylint:disable=protected-access
        if family == 'acceptor':
          hb[idx] = hb[idx]._replace(acceptor=True)
        elif family == 'donor':
          hb[idx] = hb[idx]._replace(donor=True)
          # pylint:enable=protected-access
    return hb
    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)
    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)
  def testAtomMatch(self):
    fdefBlock = \
"""
DefineFeature HAcceptor1 [#7,#8]
    Family HBondAcceptor
    Weights 1.0
EndFeature
DefineFeature Arom1 a1aaaaa1
    Family Aromatic
    Weights 1.0,1.0,1.0,1.0,1.0,1.0
EndFeature
"""
    cfac = ChemicalFeatures.BuildFeatureFactoryFromString(fdefBlock)
    self.failUnless(cfac.GetNumFeatureDefs() == 2)
    mol = Chem.MolFromSmiles('n1ccccc1')
    feats = cfac.GetFeaturesForMol(mol)
    self.failUnless(len(feats)==2)
    m = ChemicalFeatures.GetAtomMatch(feats)
    self.failIf(m)

    mol = Chem.MolFromSmiles('c1ccccc1N')
    feats = cfac.GetFeaturesForMol(mol)
    self.failUnless(len(feats)==2)
    m = ChemicalFeatures.GetAtomMatch(feats)
    self.failUnless(len(m)==2)
Beispiel #5
0
  def testParseErrorHandling(self):
    fdefBlock = \
"""DefineFeature HDonor1 [N,O;!HQ]
    Family HBondDonor
    Weights 1.0
EndFeature
"""
    self.failUnlessRaises(ValueError,
                          lambda: ChemicalFeatures.BuildFeatureFactoryFromString(fdefBlock))
    fdefBlock = \
"""DefineFeature HDonor1 [N,O;!H0]
    Family HBondDonor
    Weights 1.0
"""
    self.failUnlessRaises(ValueError,
                          lambda: ChemicalFeatures.BuildFeatureFactoryFromString(fdefBlock))

    self.failUnlessRaises(IOError, lambda: ChemicalFeatures.BuildFeatureFactory('noSuchFile.txt'))
  def testStringParse(self):
    fdefBlock = \
"""DefineFeature HDonor1 [N,O;!H0]
    Family HBondDonor
    Weights 1.0
EndFeature
DefineFeature HAcceptor1 [N,O;H0]
    Family HBondAcceptor
    Weights 1.0
EndFeature
"""
    cfac = ChemicalFeatures.BuildFeatureFactoryFromString(fdefBlock)
    self.failUnless(cfac.GetNumFeatureDefs() == 2)
Beispiel #7
0
def get_cats_factory(features='cats', names=False):
    """ Get the feature combinations paired to all possible distances

    :param features: {str} which pharmacophore features to consider; available: ["cats", "rdkit"]
    :param names: {bool} whether to return an array describing the bits with names of features and distances
    :return: RDKit signature factory to be used for 2D pharmacophore fingerprint calculation
    """
    if features == 'cats':
        fdef = fdef_cats
    else:
        fdef = fdef_rdkit
    factory = ChemicalFeatures.BuildFeatureFactoryFromString(fdef)
    sigfactory = SigFactory.SigFactory(factory, useCounts=True, minPointCount=2, maxPointCount=2)
    sigfactory.SetBins([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)])
    sigfactory.Init()
    if names:
        descs = [sigfactory.GetBitDescription(i) for i in range(sigfactory.GetSigSize())]
        return sigfactory, descs
    else:
        return sigfactory
  def testIssue231(self):
    fdefs = """
DefineFeature HDonor1 [N,O;!H0]
  Family HBondDonor
  Weights 1.0
EndFeature
DefineFeature HAcceptor1 [N,O;H0]
  Family HBondAcceptor
  Weights 1.0
EndFeature
"""
    cfac = ChemicalFeatures.BuildFeatureFactoryFromString(fdefs)

    m = Chem.MolFromSmiles('O=CCCN')
    rdDistGeom.EmbedMolecule(m)
    feats = cfac.GetFeaturesForMol(m)
    for feat in feats:
      feat.GetPos()
    m = None
    for feat in feats:
      feat.GetPos()
Beispiel #9
0
  Weights 1.0,1.0,1.0,1.0,1.0
EndFeature
DefineFeature Arom6 a1aaaaa1
  Family Aromatic
  Weights 1.0,1.0,1.0,1.0,1.0,1.0
EndFeature
DefineFeature Arom7 a1aaaaaa1
  Family Aromatic
  Weights 1.0,1.0,1.0,1.0,1.0,1.0,1.0
EndFeature
DefineFeature Arom8 a1aaaaaaa1
  Family Aromatic
  Weights 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
EndFeature
"""
featFactory = ChemicalFeatures.BuildFeatureFactoryFromString(fdefstr)


def CalculatePharm2D2pointFingerprint(mol, featFactory=featFactory):
    """
    Calculate Pharm2D2point Fingerprints
    """
    sigFactory_2point = SigFactory(featFactory,
                                   minPointCount=2,
                                   maxPointCount=2)
    sigFactory_2point.SetBins([(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                               (6, 7), (7, 8), (8, 9)])
    sigFactory_2point.Init()
    res = Generate.Gen2DFingerprint(mol, sigFactory_2point)

    res_keys = tuple(res.GetOnBits())
Beispiel #10
0
def get_factory():
    featFactory = ChemicalFeatures.BuildFeatureFactoryFromString(fdef)
    factory = SigFactory(featFactory, minPointCount=2, maxPointCount=3, useCounts=True, trianglePruneBins=False)
    factory.SetBins(defaultBins)
    factory.Init()
    return factory
Beispiel #11
0
def _init():
    global labels, patts, factory
    featFactory = ChemicalFeatures.BuildFeatureFactoryFromString(fdef)
    factory = SigFactory(featFactory, minPointCount=2, maxPointCount=3)
    factory.SetBins(defaultBins)
    factory.Init()
Beispiel #12
0
def make_pharma_probes(target_id,
                       opt=None,
                       clash=-1.0,
                       rmsd=0.5,
                       shape_dist=0.2,
                       react_id=None):
    """Function to make a protein pharamcophore"""
    target = Target.objects.get(pk=target_id)
    tot_res = Residue.objects.filter(target_id=target_id)
    if not opt:
        # Do the proteins - but only a limited distance
        # Make the limited description for these proteins
        if react_id:
            react_id.proc_stage = "MAKING PROTEIN PROBES"
            react_id.stage_completion = 0
            react_id.save()
        factory = ChemicalFeatures.BuildFeatureFactoryFromString(prot_feats)
        prots = Protein.objects.filter(
            target_id=target,
            pdb_info__isnull=False).exclude(code__contains=target.title)
        tot_prots = len(prots)
        # Loop through the mols
        prot_old = -1
        #        for counter, p in enumerate(prots):
        #            # Trim the coords to X A from the ligand
        #            delta = 10.0
        #            prot_res = []
        #            if react_id:
        #                my_prg = int((float(counter) / float(tot_prots)) * 100)
        #                if my_prg != prot_old:
        #                    react_id.stage_completion = my_prg
        #                    prot_old = my_prg
        #                    react_id.save()
        #            for mol in Molecule.objects.filter(prot_id=p):
        #                rdmol = Chem.MolFromMolBlock(str(mol.sdf_info))
        ##                x_coords, y_coords, z_coords = get_all_coords(rdmol)
        ##                x_max = max(x_coords)
        ##                y_max = max(y_coords)
        ##                z_max = max(z_coords)
        ##                x_min = min(x_coords)
        ##                y_min = min(y_coords)
        ##                z_min = min(z_coords)
        ##                near_res = tot_res.filter(prot_id=p).exclude(x_max__gte=x_max + delta, x_min__gte=x_min + delta).exclude(x_max__lte=x_max - delta, x_min__lte=x_min - delta)
        ##                near_res = near_res.exclude(y_max__gte=y_max + delta, y_min__gte=y_min + delta).exclude(y_max__lte=y_max - delta, y_min__lte=y_min - delta)
        ##                near_res = near_res.exclude(z_max__gte=z_max + delta, z_min__gte=z_min + delta).exclude(z_max__lte=z_max - delta, z_min__lte=z_min - delta)
        ##                prot_res.extend(near_res)
        #            old = -1
        #            tot = len(prot_res)
        #            #for res_count, res in enumerate(prot_res):
        for p in prots:
            print p.code
            ## Mol2 required for hydrogens currently
            if PlifProtein.objects.filter(prot_id=p):
                rdmol = Chem.MolFromMol2Block(str(
                    PlifProtein.objects.filter(prot_id=p)[0].mol2_data),
                                              removeHs=False)
            else:
                rdmol = Chem.MolFromPDBBlock(str(p.pdb_info))
                # Add the Hs => but this is pretty ruddy dodgy
                rdmol = AllChem.AddHs(rdmol, addCoords=True)
            if rdmol is None:
                print "ERROR WITH PROTEIN ", p.pk
                continue

            conf = rdmol.GetConformer()
            feats = factory.GetFeaturesForMol(rdmol)
            for i, feat in enumerate(feats):
                # Assign the residue
                info = rdmol.GetAtomWithIdx(
                    feat.GetAtomIds()[0]).GetPDBResidueInfo()
                if info:
                    res_num = info.GetResidueNumber()
                    res_name = info.GetResidueName()
                else:
                    res_num = i
                    res_name = "MOL2"
                my_res = Residue.objects.get_or_create(target_id=target,
                                                       prot_id=p,
                                                       res_name=res_name,
                                                       res_num=res_num)[0]
                new_fragment = init_probe(feat,
                                          target,
                                          mol_id=None,
                                          res_id=my_res,
                                          prot_id=p)
                new_fragment.res_id = my_res
                new_fragment.unique_id = "RES" + str(my_res.pk)
                ### Now we've set x_com
                # Now validate this is in 1) a new place 2) a new feature 3) on a unique residue
                new_fragment = assign_extents(new_fragment, conf, feat, rdmol)
                if not new_fragment:
                    continue
                # Now assign the atom_ids
                new_fragment.atom_ids = str(feat.GetAtomIds())
                try:
                    new_fragment.validate_unique()
                except ValidationError:
                    new_fragment = PlifProbe.objects.get(
                        x_com=new_fragment.x_com,
                        y_com=new_fragment.y_com,
                        z_com=new_fragment.z_com,
                        unique_id=new_fragment.unique_id,
                        type=new_fragment.type)
                    new_fragment.atom_ids = str(feat.GetAtomIds())
                    new_fragment.save(update_fields=["atom_ids"])
                    continue
                new_fragment.save()
        sys.stdout.write("\rMaking Protein probes for protein complete")
        sys.stdout.flush()
        print "Made protein PLIFS"
    # Different ph4s for these molecules
    fdefName = os.path.join(
        os.path.join(os.path.split(sys.argv[0])[0], 'data/media'),
        'BaseFeatures.fdef')
    factory = ChemicalFeatures.BuildFeatureFactory(fdefName)
    # Get the molecules that relate to this
    if not opt:
        if react_id:
            react_id.proc_stage = "MAKING MOLECULE PROBES"
            react_id.stage_completion = 0
            react_id.save()
        mols = Molecule.objects.filter(prot_id__in=prots)
    elif opt == "LLOOMMPPAA":
        # Take  the molecules from LLOOMMPPAA for testing
        if react_id:
            ll_confs = LLConf.objects.filter(target_id=target,
                                             clash__gte=clash,
                                             rmsd__gte=rmsd,
                                             shape_dist__lte=shape_dist,
                                             reactionprocess=react_id)
            react_id.proc_stage = "MAKING LLOOMMPPAA PROBES"
            react_id.stage_completion = 0
            react_id.save()
        else:
            ll_confs = LLConf.objects.filter(target_id=target,
                                             clash__gte=clash,
                                             rmsd__gte=rmsd,
                                             shape_dist__lte=shape_dist)
        mols = [ll.mol_id for ll in ll_confs]
    old = -1
    tot = len(mols)
    # List and counter to trigger the bulk create
    new_counter = 0
    new_fragments = []
    done_list = []
    mol_old = -1
    print "Making probes for " + str(tot) + " molecules"
    for counter, m in enumerate(mols):
        if m.pk in done_list:
            continue
        done_list.append(m.pk)
        sys.stdout.write("\rMaking molecule probes %d of %d complete..." %
                         (counter, tot))
        sys.stdout.flush()
        if react_id:
            my_prg = int((float(counter) / float(tot)) * 100)
            if my_prg != mol_old:
                react_id.stage_completion = my_prg
                mol_old = my_prg
                react_id.save()
        # Get the molecule
        orig_mol = Chem.MolFromMolBlock(str(m.sdf_info))
        # Add the Hydrogens
        rdmol = AllChem.AddHs(orig_mol, addCoords=True)
        # Get the conformer
        conf = rdmol.GetConformer()
        for feat in factory.GetFeaturesForMol(rdmol):
            # Initialise this new object
            new_fragment = init_probe(feat, target, mol_id=m)
            new_fragment = assign_extents(new_fragment, conf, feat, rdmol,
                                          "MOL")
            if not new_fragment:
                continue
            # Now assign the atom_ids
            new_fragment.atom_ids = str(feat.GetAtomIds())
            try:
                new_fragment.validate_unique()
                new_fragment.save()
            except ValidationError:
                continue

            new_fragments.append(new_fragment)
            new_counter += 1
            if new_counter == 5000:
                print "COMMITING"
                #PlifProbe.objects.bulk_create(new_fragments)
                new_fragments = []
                done_list = []
                new_counter = 0
    #PlifProbe.objects.bulk_create(new_fragments)
    sys.stdout.write("\rMaking molecule probes %d%% complete..." % 100)
    sys.stdout.flush()
    print "Made molecule probes"
    if not opt and react_id:
        react_id.proc_stage = "MAKING LLOOMMPPAA PROBES"
        react_id.stage_completion = -1
        react_id.save()
    elif react_id:
        react_id.proc_stage = "FINDING INTERACTIONS"
        react_id.stage_completion = -1
        react_id.save()