def similarity_search_reactions_core(**kwargs):
    outputdata = RDFwrite(kwargs['output'])
    reactions = RDFread(kwargs['input'])
    num = kwargs['number']
    rebuild = kwargs['rebuild']
    with db_session():
        x = TreeIndex(Reactions, reindex=rebuild)
        for reaction_container in reactions:
            print(reaction_container)
            a,b = TreeIndex.get_similar(x, reaction_container, num)
            print(a)
            print(b)
            for i in b:
                react_cont = i.structure
                react_cont.__class__ = ReactionContainer
                outputdata.write(react_cont)
def structure_reaction_search_core(**kwargs):
    inputdata = RDFread(kwargs['input'])
    outputdata = RDFwrite(kwargs['output'])
    search_type = kwargs['enclosure']
    if search_type == False:
        print("We are going to use simple hash-search now:")
    else:
        print("We are going to use advanced enclosure-search now:")
Пример #3
0
def similarity_search_reactions_core(**kwargs):
    init()
    Molecule, Reaction, Conditions = load_databases()[
        kwargs['']]  # придумать аргумент команд лайн
    outputdata = RDFwrite(kwargs['output'])
    reactions = RDFread(kwargs['input'])
    num = kwargs['number']
    rebuild = kwargs['rebuild']
    with db_session():
        x = TreeIndex(Reactions, reindex=rebuild)
        for reaction_container in reactions:
            print(reaction_container)
            a, b = TreeIndex.get_similar(x, reaction_container, num)
            print(a)
            print(b)
            for i in b:
                react_cont = i.structure
                react_cont.__class__ = ReactionContainer
                outputdata.write(react_cont)
Пример #4
0
def structure_molecule_search_core(**kwargs):
    molecules = SDFread(kwargs['input'])
    outputdata = RDFwrite(kwargs['output'])
    product = kwargs['product']
    if kwargs['product'] == False and kwargs['reagent'] == False:
        product = None
    elif kwargs['product'] == True and kwargs['reagent'] == True:
        print('No,No,No')
    elif kwargs['product'] == True:
        product = True
    elif kwargs['reagent'] == True:
        product = False
    with db_session():
        for molecule in molecules:
            required_reacts = Reactions.get_reactions_by_molecule(molecule,product)
            print(required_reacts)
            for reaction in required_reacts:
                react_cont = reaction.structure
                print(react_cont)
                outputdata.write(react_cont)
def structure_molecule_search_core(**kwargs):
    molecules = SDFread(kwargs['input'])
    outputdata = RDFwrite(kwargs['output'])
    product = kwargs['product']
    if kwargs['product'] == False and kwargs['reagent'] == False:
        product = None
    elif kwargs['product'] == True and kwargs['reagent'] == True:
        print('No,No,No')
    elif kwargs['product'] == True:
        product = True
    elif kwargs['reagent'] == True:
        product = False
    with db_session():
        for molecule in molecules:
            required_reacts = Reactions.get_reactions_by_molecule(
                molecule, product)
            print(required_reacts)
            for reaction in required_reacts:
                react_cont = reaction.structure
                print(react_cont)
                outputdata.write(react_cont)
Пример #6
0
    def __parse_structure(self, structure):
        chemaxed = chemax_post(
            'calculate/molExport',
            dict(structure=structure['data'],
                 parameters="rdf",
                 filterChain=self.__pre_filter_chain))
        if not chemaxed:
            return False

        with StringIO(
                chemaxed['structure']) as in_file, StringIO() as out_file:
            rdf = RDFwrite(out_file)
            r = next(RDFread(in_file))
            if isinstance(
                    r, ReactionContainer
            ) and r.products and r.substrats:  # ONLY FULL REACTIONS
                # todo: preparation for queries!!!
                g = self.getCGR(r)
                _type = StructureType.REACTION
                _report = g.graph.get('CGR_REPORT', [])
                _status = StructureStatus.HAS_ERROR if any(
                    'ERROR:' in x for x in _report) else StructureStatus.CLEAR
                rdf.write(self.dissCGR(g))
            elif isinstance(r, MoleculeContainer):  # MOLECULES AND MIXTURES
                _type = StructureType.MOLECULE
                _report = []
                _status = StructureStatus.CLEAR
                rdf.write(r)  # todo: molecules checks.
            else:
                return False

            prepared = out_file.getvalue()

        chemaxed = chemax_post(
            'calculate/molExport',
            dict(structure=prepared,
                 parameters="mrv",
                 filterChain=self.__post_filter_chain))
        if not chemaxed:
            return False

        return dict(data=chemaxed['structure'].split('\n')[1],
                    status=_status,
                    type=_type,
                    results=[
                        dict(key='Processed', value=x, type=ResultType.TEXT)
                        for x in _report
                    ])
Пример #7
0
def to_rdf(structures):
    with StringIO() as f:
        for x in structures if isinstance(structures, list) else [structures]:
            RDFwrite(f).write(x)
    return f.getvalue()
def substructure_search_core(**kwargs):
    inputdata = RDFread(kwargs['input'])
    outputdata = RDFwrite(kwargs['output'],
                          extralabels=kwargs['save_extralabels'])

#  worker = ?
Пример #9
0
    def get_results(self, structures):
        structure_file = join(self.__workpath, 'structures')
        results_file = join(self.__workpath, 'results.csv')
        # prepare input file
        if len(structures) == 1:
            chemaxed = chemax_post(
                'calculate/molExport',
                dict(structure=structures[0]['data'], parameters="rdf"))
            if not chemaxed:
                return False
            data = chemaxed['structure']
        else:
            with Popen([MOLCONVERT, 'rdf'],
                       stdin=PIPE,
                       stdout=PIPE,
                       stderr=STDOUT,
                       cwd=self.__workpath) as convert_mol:
                data = convert_mol.communicate(input=''.join(
                    s['data'] for s in structures).encode())[0].decode()
                if convert_mol.returncode != 0:
                    return False

        counter = count()
        with StringIO(data) as in_file, open(structure_file, 'w') as out_file:
            rdf = RDFwrite(out_file)
            sdf = SDFwrite(out_file)
            for r, meta in zip(RDFread(in_file), structures):
                next(counter)
                r.meta.update(pressure=meta['pressure'],
                              temperature=meta['temperature'])
                for n, a in enumerate(meta['additives'], start=1):
                    r.meta['additive.%d' % n] = a['name']
                    r.meta['amount.%d' % n] = '%f' % a['amount']

                if self.get_type(
                ) == ModelType.REACTION_MODELING and isinstance(
                        r, ReactionContainer):
                    rdf.write(r)
                elif self.get_type(
                ) == ModelType.MOLECULE_MODELING and isinstance(
                        r, MoleculeContainer):
                    sdf.write(r)

        if len(structures) != next(counter):
            return False

        if call(self.__starter, cwd=self.__workpath) == 0:
            results = []
            with open(results_file) as f:
                header = [
                    dict(key=k, type=ResultType[t])
                    for t, k in (x.split(':')
                                 for x in next(f).rstrip().split(','))
                ]
                for l in f:
                    rep = []
                    for h, v in zip(header, l.rstrip().split(',')):
                        if v:
                            tmp = dict(value=v)
                            tmp.update(h)
                            rep.append(tmp)
                    results.append(dict(results=rep))
            if len(structures) == len(results):
                return results

        return False