Exemplo n.º 1
0
def _hydrogenize(block, hydro):
    mol = Chem.MolFromMolBlock(block, sanitize=False)
    _call([mol], 'UpdatePropertyCache', strict=False)
    _apply([mol], ct._sssr)
    res = Chem.AddHs(mol, addCoords=True) if hydro else Chem.RemoveHs(
        mol, sanitize=False)
    return MolToMarvin(Chem.MolToMolBlock(res))
Exemplo n.º 2
0
def _mols2svg(mols,
              size,
              kekulize=False,
              atomMapNumber=False,
              computeCoords=False,
              highlightAtomLists=None):

    if not mols:
        return ''
    else:
        # process only one molecule
        mols = mols[0:1]

    _call(mols, 'UpdatePropertyCache', strict=False)
    _apply(mols, ct._sssr)

    if computeCoords:
        _apply(mols, _computeCoords, True)
    if atomMapNumber:
        _apply(mols, _atomMapNumber)

    if kekulize:
        _apply(mols, _kekulize)

    if highlightAtomLists:
        highlightAtomLists = highlightAtomLists[0]

    drawer = rdMolDraw2D.MolDraw2DSVG(size, size)
    opts = drawer.drawOptions()
    opts.clearBackground = False
    drawer.DrawMolecule(mols[0], highlightAtoms=highlightAtomLists)
    drawer.FinishDrawing()
    return drawer.GetDrawingText()
Exemplo n.º 3
0
def _similarityMapSVG(ms,
                      width=500,
                      height=500,
                      radius=2,
                      fingerprint='morgan',
                      format='svg'):
    if matplotlib is None:
        raise ValueError('matplotlib not useable')

    _call(ms, 'UpdatePropertyCache', strict=False)
    _apply(ms, ct._sssr)

    fn = None
    if fingerprint == 'morgan':
        fn = lambda x, i: SimilarityMaps.GetMorganFingerprint(
            x, i, radius=radius)
    elif fingerprint == 'tt':
        fn = SimilarityMaps.GetAPFingerprint
    elif fingerprint == 'ap':
        fn = SimilarityMaps.GetTTFingerprint

    SimilarityMaps.GetSimilarityMapForFingerprint(ms[0],
                                                  ms[1],
                                                  fn,
                                                  size=(width, height))
    sio = io.StringIO()
    pyplot.savefig(sio, format=format, bbox_inches='tight', dpi=100)

    return sio.getvalue()
Exemplo n.º 4
0
def MolToMarvin(mol):
    if mol.endswith('.mol') and os.path.exists(mol):
        mol = Chem.MolFromMolFile(mol, False, False, False)
    else:
        mol = Chem.MolFromMolBlock(mol, False, False, False)
    _call([mol], 'UpdatePropertyCache', strict=False)
    _apply([mol], ct._sssr)

    rdmolops.SetAromaticity(mol)
    js = _molsToJson([mol], MOL_MARVIN_SCALE)
    return _dataToXml(js)
Exemplo n.º 5
0
def _parseSMILESData(data, computeCoords=False, delimiter=' ', smilesColumn=0, nameColumn=1, titleLine=True,
                     sanitize=True):
    fd, fpath = tempfile.mkstemp(text=True)
    os.write(fd, data)
    os.close(fd)
    suppl = Chem.SmilesMolSupplier(fpath, delimiter=delimiter, smilesColumn=smilesColumn, nameColumn=nameColumn,
                                   titleLine=titleLine, sanitize=sanitize)
    mols = [x for x in suppl if x]
    if computeCoords:
        _apply(mols, ct._computeCoords, True)
        _apply(mols, ct._wedgeMolBonds)
    _call(mols, 'SetProp', '_Name', '')
    os.remove(fpath)
    return mols
Exemplo n.º 6
0
def _clean(mrv, dim=2, cgen=False):
    block = MarvinToMol(mrv)
    mol = Chem.MolFromMolBlock(block, sanitize=False)
    _call([mol], 'UpdatePropertyCache', strict=False)
    _apply([mol], ct._sssr)
    if not mol:
        print("No mol for block:\n %s" % block)
        return mrv

    if cgen:
        Chem.rdDepictor.SetPreferCoordGen(True)
    else:
        Chem.rdDepictor.SetPreferCoordGen(False)
    Chem.rdDepictor.Compute2DCoords(mol, bondLength=0.8)
    return MolToMarvin(Chem.MolToMolBlock(mol))
Exemplo n.º 7
0
def get_matches(mol):
    _call([mol], 'UpdatePropertyCache', strict=False)
    _apply([mol], ct._sssr)

    matches = []
    for alert in alerts:
        try:
            match = mol.HasSubstructMatch(alert['rdmol'])
            if match:
                malert = copy.deepcopy(alert)
                del malert['rdmol']
                matches.append(malert)
        except Exception as e:
            print(e)
    return matches
Exemplo n.º 8
0
def _molExport(structure, **kwargs):
    input_f = kwargs.get('input', None)
    output_f = kwargs.get('output', None)

    if not input_f:
        mol = _autoDetect(str(structure))
    elif input_f == 'mrv':
        mol = Chem.MolFromMolBlock(MarvinToMol(structure), sanitize=False)
    elif input_f == 'smiles':
        mol = Chem.MolFromSmiles(str(structure), sanitize=False)
    elif input_f == 'inchi':
        mol = Chem.MolFromInchi(str(structure))
    else:
        mol = Chem.MolFromMolBlock(structure, sanitize=False)

    _call([mol], 'UpdatePropertyCache', strict=False)
    _apply([mol], ct._sssr)

    if not mol.GetNumConformers() or mol.GetConformer().Is3D():
        Chem.rdDepictor.SetPreferCoordGen(True)
        Chem.rdDepictor.Compute2DCoords(mol, bondLength=0.8)

    if output_f == 'smiles':
        out_structure = Chem.MolToSmiles(mol)

    elif output_f == 'inchi':
        out_structure = Chem.MolToInchi(mol)

    elif output_f == 'inchikey':
        out_structure = Chem.InchiToInchiKey(Chem.MolToInchi(mol))

    elif output_f == 'mol':
        out_structure = Chem.MolToMolBlock(mol)

    elif output_f == 'sdf':
        out_structure = Chem.MolToMolBlock(mol) + '\n$$$$\n'

    elif output_f == 'mrv':
        out_structure = MolToMarvin(Chem.MolToMolBlock(mol))

    return {
        "structure": out_structure,
        "format": output_f,
        "contentUrl": "",
        "contentBaseUrl": ""
    }
Exemplo n.º 9
0
def _autoDetect(structure):
    mol = None

    if Chem.MolFromSmiles(structure.strip(), sanitize=False):
        mol = Chem.MolFromSmiles(structure.strip(), sanitize=False)

    elif Chem.MolFromMolBlock(structure, sanitize=False):
        return Chem.MolFromMolBlock(structure, sanitize=False)

    elif Chem.INCHI_AVAILABLE and Chem.inchi.MolFromInchi(
            structure.strip(), sanitize=True, removeHs=True):
        mol = Chem.inchi.MolFromInchi(structure.strip(),
                                      sanitize=True,
                                      removeHs=True)

    elif hasattr(Chem, 'MolFromSmarts') and Chem.MolFromSmarts(structure):
        mol = Chem.MolFromSmarts(structure)

    elif hasattr(Chem,
                 'MolFromMol2Block') and Chem.MolFromMol2Block(structure):
        mol = Chem.MolFromMol2Block(structure)

    elif hasattr(Chem, 'MolFromPDBBlock') and Chem.MolFromPDBBlock(structure):
        mol = Chem.MolFromPDBBlock(structure)

    elif hasattr(Chem, 'MolFromTPLBlock') and Chem.MolFromTPLBlock(structure):
        mol = Chem.MolFromTPLBlock(structure)

    else:
        try:
            mol = Chem.MolFromMolBlock(MarvinToMol(structure))
        except:
            pass

    _call([mol], 'UpdatePropertyCache', strict=False)
    _apply([mol], ct._sssr)

    return mol
Exemplo n.º 10
0
def _align(mols, pattern, force=False):

    if all(mol.HasSubstructMatch(pattern) for mol in mols):
        _apply(mols, GenerateDepictionMatching2DStructure, pattern)
        return mols

    if not force:
        return

    pattern = _adjustQuery(pattern)

    matches = _call(mols, 'HasSubstructMatch', pattern)

    if all(matches):
        _apply(mols, GenerateDepictionMatching2DStructure, pattern)
        return mols

    if any(matches):
        _apply(mols,
               GenerateDepictionMatching2DStructure,
               pattern,
               acceptFailure=True)
        return compress(mols, matches)
Exemplo n.º 11
0
def _getMatches(mols, smarts, force=False):
    _call(mols, 'UpdatePropertyCache', strict=False)
    _apply(mols, ct._sssr)
    return _apply(mols, ct._getSubstructMatch, _molFromSmarts(smarts), force)