Ejemplo n.º 1
0
    def setUp(self):
        self.defaultAbbrevs = rdAbbreviations.GetDefaultAbbreviations()
        self.defaultLinkers = rdAbbreviations.GetDefaultLinkers()
        self.customLinkers = rdAbbreviations.ParseLinkers(
            '''PEG3  *OCCOCCOCC* PEG3
Pent  *CCCCC*
Cy   *C1CCC(*)CC1  Cy''')
Ejemplo n.º 2
0
  def setUp(self):
    self.defaultAbbrevs = rdAbbreviations.GetDefaultAbbreviations()
    self.defaultLinkers = rdAbbreviations.GetDefaultLinkers()
    self.customLinkers = rdAbbreviations.ParseLinkers('''PEG3  *OCCOCCOCC* PEG3
Pent  *CCCCC*
Cy   *C1CCC(*)CC1  Cy''')
    self.parseMapping = lambda mapping: tuple(map(int, mapping.strip('[],').split(',')))
Ejemplo n.º 3
0
 def testGithub3692(self):
     defaults = rdAbbreviations.GetDefaultAbbreviations()
     self.assertIsNotNone(defaults[0].mol)
     lbls = [x.label for x in defaults]
     self.assertIn('CO2Et', lbls)
     idx = lbls.index('CO2Et')
     self.assertEqual(Chem.MolToSmiles(defaults[idx].mol), '*C(=O)OCC')
Ejemplo n.º 4
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()