def push(data): indigo = Indigo() compound = indigo.loadMolecule(data) indigo_record = IndigoRecordMolecule(indigo_object=compound) elasticRepository = elastic_repository_molecule() elasticRepository.index_record(record=indigo_record)
def search(data): indigo = Indigo() compound = indigo.loadMolecule(data) indigo_record = IndigoRecordMolecule(indigo_object=compound) elasticRepository = elastic_repository_molecule() return elasticRepository.filter(exact=indigo_record)
def postprocess( self, record: IndigoRecord, indigo: Indigo ) -> Optional[IndigoRecord]: if indigo.substructureMatcher(record.as_indigo_object(indigo)).match( indigo.loadQueryMolecule( self._value.as_indigo_object(indigo).canonicalSmiles() ) ): return record
def postprocess(self, record: IndigoRecord, indigo: Indigo) -> Optional[IndigoRecord]: # postprocess only on molecule search if not isinstance(record, IndigoRecordMolecule): return record if indigo.substructureMatcher(record.as_indigo_object(indigo)).match( indigo.loadQueryMolecule( self._target.as_indigo_object(indigo).canonicalSmiles())): return record
def get_molfile_v3000(compound: str) -> str: """Computes the molfile v3000 from a compound string. Args: compound: A molfile (either v2000 or v3000), MRV file, etc. Returns: The molfile v3000. """ indigo = Indigo() indigo.setOption("molfile-saving-mode", "3000") molecule = indigo.loadMolecule(compound) return molecule.molfile()
def wrapper(): indigo = Indigo() opts = request.json.get('options', {}) for key, value in opts.items(): try: indigo.setOption(key, value) except IndigoException as e: pass mol_text = request.json.get("struct") try: mol = indigo.loadMolecule(mol_text) except IndigoException as e: return jsonify({'msg': str(e)}) return func(mol)
def get_inchikey(compound: str) -> str: """Computes the InChIKey from a compound string. Args: compound: A molfile (either v2000 or v3000), MRV file, SMILES, etc. Returns: The InChIKey for the compound. """ indigo = Indigo() indigo_inchi = IndigoInchi(indigo) molecule = indigo.loadMolecule(compound) inchi = indigo_inchi.getInchi(molecule) return indigo_inchi.getInchiKey(inchi)
def iterate_file(file: Path, iterator: str = None) -> Generator[IndigoRecord, None, None]: """ :param file: :param iterator: supported iterators sdf, smiles, smi, cml. If iterator is not set, trying to determine iterator by file extension :type iterator: str :return: """ iterators = { "sdf": "iterateSDFile", "smiles": "iterateSmilesFile", "smi": "iterateSmilesFile", "cml": "iterateCMLFile", } if not iterator: iterator = file.suffix[1:] iterator_fn = iterators.get(iterator) if not iterator_fn: raise AttributeError(f"Unsupported iterator {iterator}") indigo_object: IndigoObject for indigo_object in getattr(Indigo(), iterator_fn)(str(file)): yield IndigoRecord(indigo_object=indigo_object)
def filter( self, similarity: Union[BaseMatch] = None, exact: IndigoRecord = None, substructure: IndigoRecord = None, limit=10, **kwargs, ) -> Generator[IndigoRecord, None, None]: # actions needed to be called on elastic_search result postprocess_actions: PostprocessType = [] query = self.__compile( similarity=similarity, exact=exact, substructure=substructure, limit=limit, postprocess_actions=postprocess_actions, **kwargs, ) res = self.el_client.search(index=self.index_name, body=query) indigo_session = Indigo() for el_response in res.get("hits", {}).get("hits", []): record = get_record_by_index(el_response, self.index_name) for action_fn in postprocess_actions: record = action_fn(record, indigo_session) if not record: continue yield record
class CompoundFaker(BaseProvider): compounds = COMPOUNDS indigo = Indigo() def molecule(self): smiles_str = self.random_element(self.compounds) return self.indigo.loadMolecule(smiles_str) def cid(self): return build_cid(self.random_int(2000000, 3000000)) def molfile(self, v2000=False): if v2000: self.indigo.setOption("molfile-saving-mode", "2000") else: self.indigo.setOption("molfile-saving-mode", "3000") return self.molecule().molfile() def mrvfile(self): return format_mrvfile(self.molecule().cml()) def molfile_v2000(self): smiles_str = self.random_element(self.compounds) self.indigo.setOption("molfile-saving-mode", "2000") return self.indigo.loadMolecule(smiles_str).molfile() def smile(self): return self.random_element(self.compounds)
def iterate_file( file: Path, iterator: str = None, error_handler: Optional[Callable[[object, BaseException], None]] = None, ) -> Generator[IndigoRecordMolecule, None, None]: """ :param file: :param iterator: supported iterators sdf, smiles, smi, cml. If iterator is not set, trying to determine iterator by file extension :type iterator: str :param error_handler: lambda for catching exceptions :type error_handler: Optional[Callable[[object, BaseException], None]] :return: """ iterators = { "sdf": "iterateSDFile", "smiles": "iterateSmilesFile", "smi": "iterateSmilesFile", "cml": "iterateCMLFile", } if not iterator: iterator = file.suffix[1:] iterator_fn = iterators.get(iterator) if not iterator_fn: raise AttributeError(f"Unsupported iterator {iterator}") indigo_object: IndigoObject for indigo_object in getattr(Indigo(), iterator_fn)(str(file)): yield IndigoRecordMolecule( indigo_object=indigo_object, error_handler=error_handler )
def test_filter_by_name( elastic_repository_molecule: ElasticRepository, indigo_fixture: Indigo, loaded_sdf: IndigoRecordMolecule, resource_loader, ): mol = indigo_fixture.loadMoleculeFromFile( resource_loader("molecules/composition1.mol")) elastic_repository_molecule.index_record( IndigoRecordMolecule(indigo_object=mol)) time.sleep(1) result = elastic_repository_molecule.filter(name="Composition1") for item in result: assert item.name == "Composition1" result = elastic_repository_molecule.filter( similarity=TanimotoSimilarityMatch( IndigoRecordMolecule(indigo_object=mol), 0.1)) i = 0 for _ in result: i += 1 assert i == 10 result = elastic_repository_molecule.filter( similarity=TanimotoSimilarityMatch( IndigoRecordMolecule(indigo_object=mol), 0.1), name="Composition1", ) for item in result: assert item.name == "Composition1"
def load_molecule( file_: Union[str, Path], session: Indigo ) -> IndigoRecordMolecule: """ Helper for loading molecules from file into IndigoRecordMolecule object """ molecule = session.loadMoleculeFromFile(file_) return IndigoRecordMolecule(indigo_object=molecule)
def load_reaction( file_: Union[str, Path], session: Indigo ) -> IndigoRecordReaction: """ Helper for loading reactions into IndigoRecordReaction object """ reaction = session.loadReactionFromFile(str(file_)) return IndigoRecordReaction(indigo_object=reaction)
def test_search_empty_fingerprint( elastic_repository: ElasticRepository, indigo_fixture: Indigo, resource_loader: Callable[[str], str], ): for smile in ["[H][H]", "[H][F]"]: rec = IndigoRecord(indigo_object=indigo_fixture.loadMolecule(smile), skip_errors=True) elastic_repository.index_record(rec) time.sleep(5) result = elastic_repository.filter(exact=IndigoRecord( indigo_object=indigo_fixture.loadMolecule("[H][H]"), skip_errors=True)) assert ("[H][H]" == next(result).as_indigo_object( indigo_fixture).canonicalSmiles()) with pytest.raises(StopIteration): next(result).as_indigo_object(indigo_fixture).canonicalSmiles()
def parseMolecule(self): """ turn the input into a toolkit molecule according to user settings indigo is supposed to read transparently, so we can do away with the format setting, basically. If it's numeric, we ask pubchem, if it isn't, we consider it a molecule. """ rawinput = self.data_string try: pubchemId = int(rawinput) except ValueError: pubchemId = None if pubchemId is not None: try: url = common.pubchem_url % pubchemId pubchemContent = urllib.urlopen(url).read() except IOError: raise common.MCFError, "No connection to PubChem" self.data_string = pubchemContent # common.debug('rpc: %s' % self.rpc) # common.debug('data ---\n%s\n---' % self.data_string) try: tkmol = Indigo().loadMolecule(self.data_string) except IndigoException: raise common.MCFError, "Invalid input data" hydrogens = self.options["hydrogens"] if hydrogens == "add": tkmol.unfoldHydrogens() tkmol.layout() # needed to give coordinates to added Hs elif hydrogens == "delete": tkmol.foldHydrogens() if not tkmol.hasCoord() or self.options["recalculate_coordinates"]: tkmol.layout() return tkmol
def depict_smile(genecluster, structuresfolder): indigo = Indigo() renderer = IndigoRenderer(indigo) query = indigo.loadMoleculeFromFile("genecluster" + str(genecluster) + ".smi") indigo.setOption("render-coloring", True) renderer.renderToFile(query, "genecluster" + str(genecluster) + ".png") indigo.setOption("render-image-size", 200, 150) renderer.renderToFile(query, "genecluster" + str(genecluster) + "_icon.png") dircontents = os.listdir(os.getcwd()) geneclusterstring = "genecluster" + str(genecluster) + ".png" if geneclusterstring in dircontents: shutil.copy("genecluster" + str(genecluster) + ".png", structuresfolder) shutil.copy("genecluster" + str(genecluster) + "_icon.png", structuresfolder) shutil.copy("genecluster" + str(genecluster) + ".smi", structuresfolder) os.remove("genecluster" + str(genecluster) + ".png") os.remove("genecluster" + str(genecluster) + "_icon.png") os.remove("genecluster" + str(genecluster) + ".smi") smiles_input = path.join('SMILES', 'input') if path.exists(smiles_input): os.remove(smiles_input) return "success" else: return "failed"
def get_compound_id(smiles): """ returns kegg id for compund with given smiles """ indigo = Indigo() # convert smiles to standard format mol = indigo.loadMolecule(smiles) mol.aromatize() moi_smiles = mol.canonicalSmiles() # Get list of possible kegg IDs url = "http://rest.genome.jp/subcomp/?smiles=%s&cutoff=1.0" % smiles http_client = HTTPClient() try: response = http_client.fetch(url).body except HTTPError as e: raise RuntimeError("Error:", str(e)) http_client.close() subcomp_results = response.split("\n") subcomp_results.pop() subcomp_results = ([i.split('\t')[0] for i in subcomp_results]) # get smiles for all compound IDs found all_smiles = [] uni = UniChem() mapping = uni.get_mapping("kegg_ligand", "chebi") ch = ChEBI() all_smiles = [ch.getCompleteEntity(mapping[x]).smiles for x in subcomp_results] # convert smiles to a standard format for pos, mol in enumerate(all_smiles): m = indigo.loadMolecule(mol) m.aromatize() all_smiles[pos] = m.canonicalSmiles() # check if smiles matches given and, if so, use that compound ID # if not, errors out try: index = all_smiles.index(moi_smiles) except: raise RuntimeError("SMILES unmatchable to: %s" % str(all_smiles)) return subcomp_results[index]
def __init__(self, path=None): Indigo.__init__(self, path) if isJython() or isIronPython(): IndigoObject = IndigoObjectCoverageWrapper # TODO: Change standard IndigoObject to IndigoObjectCoverageWrapper else: self.IndigoObject = IndigoObjectCoverageWrapper self._indigoObjectCoverageDict = dict() self._indigoObjectCoverageByTypeDict = dict() m = self.createMolecule() for item in getmembers(m): if type(item[1]) in (BuiltinFunctionType, BuiltinMethodType, MethodType, FunctionType) and not item[0].startswith('_'): self._indigoObjectCoverageDict[item[0]] = 0 self._indigoCoverageDict = dict() for item in getmembers(self): if type(item[1]) in (BuiltinFunctionType, BuiltinMethodType, MethodType, FunctionType) and not item[0].startswith('_'): self._indigoCoverageDict[item[0]] = 0
def test_wildcard_search( elastic_repository: ElasticRepository, indigo_fixture: Indigo, loaded_sdf: IndigoRecord, resource_loader: Callable[[str], str], ): mol = indigo_fixture.loadMoleculeFromFile( resource_loader("resources/composition1.mol")) elastic_repository.index_record(IndigoRecord(indigo_object=mol)) time.sleep(1) result = elastic_repository.filter(name=WildcardQuery("Comp*")) for item in result: assert item.name == "Composition1"
def indigo_atom_map_reaction(rxn_smiles: str, timeout_period: int, existing_mapping="discard", verbose=False): try: # Instantiate the Indigo class object and set the timeout period. indigo_mapper = Indigo() indigo_mapper.setOption("aam-timeout", timeout_period) # Return the atom mapping of the reaction SMILES string. rxn = indigo_mapper.loadReaction(rxn_smiles) rxn.automap(existing_mapping) return rxn.smiles() # If an exception occurs for any reason, print the message if indicated, and return None. except Exception as exception: if verbose: print( "Exception occured during atom mapping of the reaction SMILES. Detailed message: {}" .format(exception)) return None
def indigo_init(options={}): try: tls.indigo = Indigo() tls.indigo.inchi = IndigoInchi(tls.indigo) tls.indigo.renderer = IndigoRenderer(tls.indigo) for option, value in indigo_api.indigo_defaults.items(): tls.indigo.setOption(option, value) for option, value in options.items(): # TODO: Remove this when Indigo API supports smiles type option if option in {'smiles', }: continue tls.indigo.setOption(option, value) return tls.indigo except Exception as e: indigo_api_logger.error('indigo-init: {0}'.format(e)) return None
def test_custom_fields( elastic_repository: ElasticRepository, indigo_fixture: Indigo, loaded_sdf: IndigoRecord, resource_loader: Callable[[str], str], ): mol = indigo_fixture.loadMoleculeFromFile( resource_loader("resources/composition1.mol")) rec = IndigoRecord(indigo_object=mol, PUBCHEM_IUPAC_INCHIKEY="RDHQFKQIGNGIED-UHFFFAOYSA-N") elastic_repository.index_record(rec) time.sleep(1) result = elastic_repository.filter( PUBCHEM_IUPAC_INCHIKEY="RDHQFKQIGNGIED-UHFFFAOYSA-N") for item in result: assert item.PUBCHEM_IUPAC_INCHIKEY == "RDHQFKQIGNGIED-UHFFFAOYSA-N"
def wrapper(): indigo = Indigo() opts = request.json.get('options', {}) for key, value in opts.items(): indigo.setOption(key, value) return func()
def resetIndigo(): global indigo, indigoRenderer, indigoInchi indigo = Indigo() indigoRenderer = IndigoRenderer(indigo) indigoInchi = IndigoInchi(indigo) # Set default options indigo.setOption('render-bond-length', '30') indigo.setOption('render-relative-thickness', '1.3') indigo.setOption('render-coloring', False) indigo.setOption('render-comment-font-size', 14.0) indigo.setOption('render-comment-offset', '10')
""" Categorize flavonoids according to the input molecule. """ import itertools from indigo import Indigo from itertools import combinations, product from chalcone_class import Chalcone idg = Indigo() CHAIN = idg.CHAIN RING = idg.RING def getskidx(ringidx, sk): """ get the indices of atoms in identified skeletons """ skix = [] for ski in sk: tix = [] for i in ski[0]: tix += ringidx[i] tix += ski[1:-1] skix.append(tix) return skix # TO DO def _checkXanthSK(molobj): """ Get skeleton of Xanthones.
def test_init(self): indigo = Indigo() indigo_render = IndigoRenderer(indigo) self.assertIsNotNone(indigo_render)
import time from pathlib import Path from bingo_elastic.model.record import IndigoRecord from bingo_elastic.queries import SimilarityMatch from bingo_elastic.elastic import ElasticRepository from bingo_elastic.model import helpers from indigo import Indigo if __name__ == '__main__': indigo = Indigo() start_time = time.time() repository = ElasticRepository(host="127.0.0.1", port=9200) # try: # sdf = helpers.iterate_sdf("../data/pubchem/Compound_000000001_000500000.sdf") # repository.index_records(sdf) # except ValueError as e: # print(e) # # # time.sleep(300) print( "--- {} seconds for reading and indexing data ---".format(time.time() - start_time)) mol = indigo.loadMolecule( "Cc1ccc2nc(-c3ccc(NC(C4N(C(c5cccs5)=O)CCC4)=O)cc3)sc2c1") target = IndigoRecord(indigo_object=mol)
def as_indigo_object(self, session: Indigo): return session.deserialize(list(map(int, self.cmf.split(" "))))
# Indigo code acquired from http://www.ggasoftware.com/ from indigo import Indigo from indigo_renderer import IndigoRenderer import uuid import rsvg import gtk BORDER_WIDTH = 0 # for more rendering options visit: # http://www.ggasoftware.com/opensource/indigo/api/options#rendering _indigo = Indigo() _renderer = IndigoRenderer(_indigo) _indigo.setOption('render-output-format', 'svg') _indigo.setOption('render-margins', 10, 10) _indigo.setOption('render-stereo-style', 'none') _indigo.setOption('render-implicit-hydrogens-visible', False) _indigo.setOption('render-coloring', True) _indigo.setOption('render-bond-length', 20.0) _indigo.setOption('render-label-mode', 'hetero') def smiles2svg(smiles, comment=''): _indigo.setOption('render-comment', comment) m = _indigo.loadMolecule(smiles) m.aromatize() m.layout() s = _renderer.renderToBuffer(m).tostring() id = str(uuid.uuid4()) i = 0 while True:
def setUp(self) -> None: self.indigo = Indigo()
def indigo_fixture() -> Indigo: return Indigo()
def smiles(smiles_string): # Render SMILES to PNG indigo = Indigo() renderer = IndigoRenderer(indigo) mol = indigo.loadMolecule(smiles_string) mol.layout() # if not called, will be done automatically by the renderer indigo.setOption("render-output-format", "png") indigo.setOption("render-image-size", 250, 250) indigo.setOption("render-background-color", 1.0, 1.0, 1.0) indigo.setOption("render-coloring", True) indigo.setOption("aromaticity-model", "generic") mol.dearomatize() buf = renderer.renderToBuffer(mol) buf = io.BytesIO(buf) return send_file( buf, mimetype='image/png', as_attachment=False, )
from __future__ import absolute_import raise NotImplementedError("This module has not been updated to the current chemfp API") # WARNING! This is a first attempt at Indigo support. There are known problems import warnings warnings.warn("Indigo support is incomplete, experimental, and not to be trusted!") from . import io from . import types from indigo import Indigo _indigo = Indigo() SOFTWARE = "Indigo/" + _indigo.version() # There is no way to get the sizes from the SDK API. While it is # possible to change the fingerprint sizes, I decided to not support # that for now since it appears that things will change. # All fingerprints are currently stored in a single mega-fingerprint # object, broken down into parts: # 3 bytes for "extra" bits # 25*8 bytes for "ordinary" part # 8*8 bytes for "similarity" part # 10*8 bytes for "tautomer" part # 15*8 bytes for "resonance" part _ORD = 3 _SIM = 3+(25)*8 _TAU = 3+(25+8)*8 _RES = 3+(25+8+10)*8