Exemplo n.º 1
0
  def testCondense(self):
    m = Chem.MolFromSmiles('FC(F)(F)CC(=O)O')
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.defaultAbbrevs, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), '*C* |$CF3;;CO2H$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (1, 4, 5))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (3, 4))

    m = Chem.MolFromSmiles('CCC(F)(F)F')
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.defaultAbbrevs)
    self.assertEqual(Chem.MolToCXSmiles(nm), '*C(F)(F)F |$Et;;;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (1, 2, 3, 4, 5))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (1, 2, 3, 4))

    # make sure we don't mess up chirality
    m = Chem.MolFromSmiles('FC(F)(F)[C@](Cl)(F)I')
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.defaultAbbrevs, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), '*[C@@](F)(Cl)I |$CF3;;;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (1, 4, 5, 6, 7))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (3, 4, 5, 6))
Exemplo n.º 2
0
  def testAbbreviationsAndLinkers(self):
    m = Chem.MolFromSmiles('COC1CCC(C)CC1')
    # wouldn't normally do this in this order:
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.defaultAbbrevs, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), '*C1CCC(C)CC1 |$OMe;;;;;;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (1, 2, 3, 4, 5, 6, 7, 8))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (1, 2, 3, 4, 5, 6, 7, 8))
    nm = rdAbbreviations.CondenseMolAbbreviations(nm, self.customLinkers, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), '**C |$OMe;Cy;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (1, 2, 6))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (1, 5))

    # This is a more logical order
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.customLinkers, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), 'C*OC |$;Cy;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (0, 1, 2, 6))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (0, 1, 5))
    nm = rdAbbreviations.CondenseMolAbbreviations(nm, self.defaultAbbrevs, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), 'C*OC |$;Cy;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (0, 1, 2, 6))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (0, 1, 5))
Exemplo n.º 3
0
    def testCondenseLinkers(self):
        m = Chem.MolFromSmiles('FCOCCOCCOCCCCCCCCCCl')
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      self.defaultLinkers,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), 'FC**Cl |$;;PEG3;Hept;$|')

        m = Chem.MolFromSmiles('COC1CCC(C)CC1')
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      self.customLinkers,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), 'C*OC |$;Cy;;$|')
Exemplo n.º 4
0
    def testCondense(self):
        m = Chem.MolFromSmiles('FC(F)(F)CC(=O)O')
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      self.defaultAbbrevs,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), '*C* |$CF3;;CO2H$|')
        m = Chem.MolFromSmiles('CCC(F)(F)F')
        nm = rdAbbreviations.CondenseMolAbbreviations(m, self.defaultAbbrevs)
        self.assertEqual(Chem.MolToCXSmiles(nm), '*C(F)(F)F |$Et;;;;$|')

        # make sure we don't mess up chirality
        m = Chem.MolFromSmiles('FC(F)(F)[C@](Cl)(F)I')
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      self.defaultAbbrevs,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), '*[C@@](F)(Cl)I |$CF3;;;;$|')
Exemplo n.º 5
0
  def testCondenseLinkers(self):
    m = Chem.MolFromSmiles('FCOCCOCCOCCCCCCCCCCl')
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.defaultLinkers, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), 'FC**Cl |$;;PEG3;Hept;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (0, 1, 2, 11, 18))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (0, 1, 10, 17))

    m = Chem.MolFromSmiles('COC1CCC(C)CC1')
    nm = rdAbbreviations.CondenseMolAbbreviations(m, self.customLinkers, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), 'C*OC |$;Cy;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (0, 1, 2, 6))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (0, 1, 5))
Exemplo n.º 6
0
    def testParsingAbbrevs(self):
        defn = '''CO2Et    C(=O)OCC
COOEt    C(=O)OCC
OiBu     OCC(C)C
tBu      C(C)(C)C'''
        abbrevs = rdAbbreviations.ParseAbbreviations(defn)
        m = Chem.MolFromSmiles('CCC(=O)OCC')
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      abbrevs,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), '*CC |$CO2Et;;$|')
Exemplo n.º 7
0
    def testAbbreviationsAndLinkers(self):
        m = Chem.MolFromSmiles('COC1CCC(C)CC1')
        # wouldn't normally do this in this order:
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      self.defaultAbbrevs,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), '*C1CCC(C)CC1 |$OMe;;;;;;;$|')
        nm = rdAbbreviations.CondenseMolAbbreviations(nm,
                                                      self.customLinkers,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), '**C |$OMe;Cy;$|')

        # This is a more logical order
        nm = rdAbbreviations.CondenseMolAbbreviations(m,
                                                      self.customLinkers,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), 'C*OC |$;Cy;;$|')
        nm = rdAbbreviations.CondenseMolAbbreviations(nm,
                                                      self.defaultAbbrevs,
                                                      maxCoverage=1.0)
        self.assertEqual(Chem.MolToCXSmiles(nm), 'C*OC |$;Cy;;$|')
Exemplo n.º 8
0
  def testParsingAbbrevs(self):
    defn = '''CO2Et    C(=O)OCC
COOEt    C(=O)OCC
OiBu     OCC(C)C
tBu      C(C)(C)C'''
    abbrevs = rdAbbreviations.ParseAbbreviations(defn)
    m = Chem.MolFromSmiles('CCC(=O)OCC')
    nm = rdAbbreviations.CondenseMolAbbreviations(m, abbrevs, maxCoverage=1.0)
    self.assertEqual(Chem.MolToCXSmiles(nm), '*CC |$CO2Et;;$|')
    self.assertTrue(nm.HasProp('_origAtomMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origAtomMapping')), (0, 1, 2))
    self.assertTrue(nm.HasProp('_origBondMapping'))
    self.assertEqual(self.parseMapping(nm.GetProp('_origBondMapping')), (0, 1))
Exemplo n.º 9
0
def _drawHelper(mol, drawer, **kwargs):
    tgt = request.get_json()
    if tgt is None:
        tgt = request.values
    sanit = _stringtobool(tgt.get('sanitize', True))
    kekulize = _stringtobool(tgt.get('kekulize', True))

    for arg in ('highlightAtomColors', 'highlightBondColors'):
        if arg not in kwargs:
            tmp = _loadJSONParam(tgt.get(arg, None))
            if tmp:
                for k in list(tmp):
                    tmp[int(k)] = tuple(tmp[k])
                    del tmp[k]
                kwargs[arg] = tmp
    for arg in ('highlightAtoms', 'highlightBonds'):
        if arg not in kwargs:
            tmp = _loadJSONParam(tgt.get(arg, None))
            if tmp:
                kwargs[arg] = tmp
    if 'legend' not in kwargs:
        kwargs['legend'] = tgt.get('legend', '')
    # if 'lw' not in kwargs and 'lw' in request.values:
    #     kwargs['lw'] = int(request.values['lw'])
    if 'highlightSubstruct' not in kwargs:
        if 'highlightColor' not in kwargs:
            hcolor = _loadJSONParam(tgt.get('highlightColor', None))
            if hcolor:
                highlightColor = tuple(hcolor)
            else:
                highlightColor = None
        else:
            highlightColor = kwargs['highlightColor']
            del kwargs['higlightColor']
        if _stringtobool(tgt.get('highlightSubstruct', False)) or \
          'smiles_highlight' in tgt or \
          'smarts_highlight' in tgt or \
          'mol_highlight' in tgt:
            qmol = _queryfromrequest(suffix='_highlight')
            if qmol is not None:
                qMatches = mol.GetSubstructMatches(qmol)
                highlights = kwargs.get('highlightAtoms', [])
                if highlightColor is not None:
                    highlightBonds = kwargs.get('highlightBonds', [])
                else:
                    highlightBonds = None

                for entry in qMatches:
                    if highlightColor is not None:
                        had = kwargs.get('highlightAtomColors', {})
                        for v in entry:
                            had[v] = highlightColor
                        kwargs['highlightAtomColors'] = had

                        hbd = kwargs.get('highlightBondColors', {})
                        emap = dict(enumerate(entry))
                        for bond in qmol.GetBonds():
                            mbond = mol.GetBondBetweenAtoms(
                                emap[bond.GetBeginAtomIdx()],
                                emap[bond.GetEndAtomIdx()])
                            highlightBonds.append(mbond.GetIdx())
                            hbd[mbond.GetIdx()] = highlightColor
                        kwargs['highlightBondColors'] = hbd
                    highlights.extend(list(entry))
                if highlights:
                    kwargs['highlightAtoms'] = highlights
                    if highlightBonds is not None:
                        kwargs['highlightBonds'] = highlightBonds
    from rdkit.Chem.Draw import rdDepictor
    rdDepictor.SetPreferCoordGen(True)
    # print(Chem.MolToMolBlock(mc))
    drawo = drawer.drawOptions()
    for opt, default in (('dummiesAreAttachments', False),
                         ('comicMode', False), ('addAtomIndices', False),
                         ('addBondIndices', False), ('isotopeLabels', True),
                         ('addStereoAnnotation',
                          False), ('explicitMethyl',
                                   False), ('includeChiralFlagLabel', False),
                         ('simplifiedStereoGroupLabel', False)):
        setattr(drawo, opt, _stringtobool(tgt.get(opt, default)))
    if drawo.comicMode:
        drawo.fontFile = os.path.join(RDConfig.RDDataDir, "Fonts",
                                      "ComicNeue-Regular.ttf")
    if drawo.addStereoAnnotation:
        Chem.FindMolChiralCenters(mol,
                                  force=True,
                                  useLegacyImplementation=False)

    if 'backgroundColor' in tgt:
        tmp = _loadJSONParam(tgt.get('backgroundColor', None))

        drawo.SetBackgroundColor(tuple)

    if _stringtobool(tgt.get('atomIndices', False)):
        drawo.addAtomIndices = True
    if _stringtobool(tgt.get('bondIndices', False)):
        drawo.addBondIndices = True
    if _stringtobool(tgt.get('useBW', False)):
        drawo.useBWAtomPalette()

    if _stringtobool(tgt.get('useGrid', '0')):
        frags = []
        for frag in Chem.GetMolFrags(mol, asMols=True):
            frags.append(
                rdMolDraw2D.PrepareMolForDrawing(frag,
                                                 kekulize=kekulize & sanit,
                                                 addChiralHs=sanit))

        drawer.DrawMolecules(frags)
    elif _stringtobool(tgt.get('asRxn', False)):
        drawer.DrawReaction(mol)
    else:
        mc = rdMolDraw2D.PrepareMolForDrawing(mol,
                                              kekulize=kekulize & sanit,
                                              addChiralHs=sanit)
        mc.SetProp("_Name", "temp")
        if _stringtobool(tgt.get('collapseAbbreviations',False)) and \
            len(Chem.GetMolSubstanceGroups(mc)):
            mc = rdAbbreviations.CondenseAbbreviationSubstanceGroups(mc)
        elif _stringtobool(tgt.get('findAbbreviations', False)):
            mc = rdAbbreviations.CondenseMolAbbreviations(
                mc,
                rdAbbreviations.GetDefaultAbbreviations(),
                maxCoverage=float(tgt.get('maxAbbreviationCoverage', 0.4)))
        drawer.DrawMolecule(mc, **kwargs)
    drawer.FinishDrawing()