def clear_databases(): EnzymeType.drop_collection() Sequence.drop_collection() Paper.drop_collection() Reaction.drop_collection() Activity.drop_collection() Molecule.drop_collection()
def submission_main_page(paper_id): user = user_datastore.get_user(current_user.id) paper_query = Paper.objects(id=paper_id).select_related() if len(paper_query) == 0: flash('Paper has not been added yet, please add to the database first', 'fail') return redirect(url_for("biocatdb.launch_add_paper")) paper = paper_query[0] if not check_permission.check_paper_permission(current_user.id, paper): flash('No access to edit this entry', 'fail') return redirect(url_for("biocatdb.launch_add_paper")) paper_data = get_paper_data(paper, user) activity_data = get_activity_data(paper) reactions = list(Reaction.objects().distinct('name')) enzyme_names = list(Sequence.objects(papers=paper).distinct('enzyme_name')) enzyme_types = list(EnzymeType.objects().distinct('enzyme_type')) enzyme_data = sequence_table.get_enzyme_data(db.Q(papers=paper)) enzyme_types_in_paper = list(Sequence.objects(papers=paper).distinct('enzyme_type')) reactions_in_paper = list(Reaction.objects(enzyme_types__in=enzyme_types_in_paper).distinct('name')) reactions_in_activity = list(Activity.objects(paper=paper).distinct('reaction')) status_dict = get_status(paper, user) comments = get_comments(paper, user) paper_molecules = get_paper_molecules(paper) admin_panel = False admin_dict = {} if current_user.has_role('admin'): admin_panel = True admin_dict = get_admin_dict(paper) reactions_ordered = reactions_in_activity + [r for r in reactions_in_paper if r not in reactions_in_activity] reactions_ordered += [r for r in reactions_in_paper if r not in reactions_ordered] reactions_ordered += [r for r in reactions if r not in reactions_ordered] return render_template('data_submission/submission_main_page.html', paper=paper_data, activity_data=activity_data, seq_data=enzyme_data, seq_button_columns=['edit', 'remove', 'papers'], status=status_dict, seq_table_height='60vh', enzyme_types=enzyme_types, show_header_filters=False, include_owner=True, lock_enz_type='false', reactions=reactions_ordered, enzyme_names=enzyme_names+['Chemical'], doi=paper.doi, comments=comments, paper_molecules=paper_molecules, admin_panel=admin_panel, admin_dict=admin_dict, enzyme_reactions=reactions_in_paper)
def load_into_mongo(yaml_dict): for rxn_name in yaml_dict: enzymes = [] for enz in list(yaml_dict[rxn_name]['enzymes'].keys()): enzymes.append(enz) if len(EnzymeType.objects(enzyme_type=enz)) == 0: new_type = EnzymeType(enzyme_type=enz, description='') new_type.save() if len(Reaction.objects(name=rxn_name)) == 0: reaction = Reaction(name=rxn_name, smarts=yaml_dict[rxn_name]['smarts'], enzyme_types=enzymes, cofactors=yaml_dict[rxn_name]['enzymes'], positive_tests=yaml_dict[rxn_name]['positive_tests'], negative_tests=yaml_dict[rxn_name]['negative_tests'], type=yaml_dict[rxn_name]['type']) if 'experimental' in yaml_dict[rxn_name]: reaction.experimental = bool(yaml_dict[rxn_name]['experimental']) if 'two_step' in yaml_dict[rxn_name]: reaction.two_step = bool(yaml_dict[rxn_name]['two_step']) reaction.save()
def _test_name(self, rxn_name, selection): if rxn_name == '' or rxn_name == ' ' or rxn_name == 'Empty template': self.issues.append('Reaction must have a name') self.state = 'danger' if rxn_name != selection: if len(Reaction.objects(name=rxn_name)) != 0: self.issues.append('Reaction name already exists') self.state = 'danger'
def submit_reaction_issue(): substrates = json.loads(request.form['parents']) products = json.loads(request.form['children']) reaction_name = request.form['reaction'] comment = request.form['comment'] public = bool(strtobool(request.form['public'])) if len(Reaction.objects(name=reaction_name)) == 0: result = { 'status': 'danger', 'msg': 'Can not raise an issue for a non retrobiocat reaction', 'issues': [] } return jsonify(result=result) reaction_smiles = f"{substrates[0]}" if len(substrates) > 1: reaction_smiles += f".{substrates[1]}" reaction_smiles += f">>{products[0]}" query_reaction_svg = smiles_rxn_to_svg(reaction_smiles, rxnSize=(300, 70)) user = user_datastore.get_user(current_user.id) reaction = Reaction.objects(name=reaction_name)[0] comment_obj = Comment(owner=user, text=comment) comment_obj.save() issue = Issue(reaction=reaction, issue_reaction_smiles=reaction_smiles, issue_reaction_svg=query_reaction_svg, raised_by=user, status='Open', comments=[comment_obj], public=public) issue.save() print(f"Issue saved for {issue.reaction}") result = {'status': 'success', 'msg': 'Issue raised', 'issues': []} return jsonify(result=result)
def download_rxn_yaml(): all_reactions = Reaction.objects() yaml_dict = {} for reaction in all_reactions: print(reaction) yaml_dict = yaml_conversion.reaction_to_yaml_dict(yaml_dict, reaction) print(yaml_dict) yaml_json = json.dumps(yaml_dict) yaml_dict = json.loads(yaml_json) resp = make_response(yaml.dump(yaml_dict)) resp.headers["Content-Disposition"] = "attachment; filename=rxns_yaml.yaml" return resp
def set_choices(self): self.enzyme_type.choices = [ (c, c) for c in ['All'] + (list(Sequence.objects().distinct('enzyme_type'))) ] self.enzyme_name.choices = [ (c, c) for c in ['All'] + (list(Sequence.objects().distinct('enzyme_name'))) ] self.reaction.choices = [ (c, c) for c in ['All'] + (list(Reaction.objects().distinct('name'))) ]
def change_enzyme_type_name(enz_type, new_name): print("Updating enzyme types..") old_name = enz_type.enzyme_type for seq in Sequence.objects(enzyme_type=old_name): seq.enzyme_type = new_name seq.save() for reaction in Reaction.objects(enzyme_types=old_name): reaction.enzyme_types.remove(old_name) reaction.enzyme_types.append(new_name) reaction.cofactors[new_name] = reaction.cofactors.pop(old_name) reaction.save() for activity in Activity.objects(enzyme_type=old_name): activity.enzyme_type = new_name activity.save() print('..done')
def get_reactions(include_experimental=False, include_two_step=False, include_requires_absence_of_water=False): if include_experimental == True: q_exp = db.Q() else: q_exp = db.Q(experimental__ne=True) if include_two_step == True: q_two = db.Q() else: q_two = db.Q(two_step__ne=True) if include_requires_absence_of_water == True: q_water = db.Q() else: q_water = db.Q(requires_absence_of_water__ne=True) return Reaction.objects(q_exp & q_two & q_water)
def delete_enzyme_types(): to_delete = request.form['to_delete'] enz_type = EnzymeType.objects(enzyme_type=to_delete)[0] seqs = Sequence.objects(enzyme_type=to_delete) reacs = Reaction.objects(enzyme_types=to_delete) acts = Activity.objects(enzyme_type=to_delete) status = 'success' msg = 'Enzyme type deleted' issues = [] if len(seqs) != 0: status = 'danger' msg = 'Could not delete' for seq in seqs: issues.append( f'Enzyme type is present in sequence: {seq.enzyme_name}') if len(reacs) != 0: status = 'danger' msg = 'Could not delete' for reac in reacs: issues.append(f'Enzyme type is present in reaction: {reac.name}') if len(acts) != 0: status = 'danger' msg = 'Could not delete' papers = [] for act in acts: if act.short_citation not in papers: papers.append(act.short_citation) for paper in papers: issues.append( f"Enzyme type is recorded in activity data for {paper}") if status == 'success': enz_type.delete() result = {'status': status, 'msg': msg, 'issues': issues} return jsonify(result=result)
def edit_enzyme_types(): headings = ['Name', 'Full name', 'Other abbreviations', 'Description', 'Num rules'] enzyme_types = EnzymeType.objects().distinct("enzyme_type") enzyme_types.sort() enz_type_dict_list = EnzymeType.objects().order_by('enzyme_type').as_pymongo() renamed_enz_type_dict_list = [] for enz_type_dict in enz_type_dict_list: new_enz_type_dict = {} enz_type = enz_type_dict.get('enzyme_type') new_enz_type_dict['Name'] = enz_type new_enz_type_dict['Full name'] = enz_type_dict.get('full_name', '') new_enz_type_dict['Other abbreviations'] = enz_type_dict.get('other_abbreviations', '') new_enz_type_dict['Description'] = enz_type_dict.get('description', '') new_enz_type_dict['Num rules'] = len(Reaction.objects(enzyme_types=enz_type)) renamed_enz_type_dict_list.append(new_enz_type_dict) return render_template('enzyme_type/edit_enzyme_types.html', headings=headings, rows=renamed_enz_type_dict_list, enzyme_types=enzyme_types)
def load_rule(): reaction_name = request.form['selected_rule'] if reaction_name == 'Empty template': rxn_name = "" rxn_smarts = "" rxn_enzyme_cofactor = "" positive_tests = "" negative_tests = "" reaction_type = "" experimental = True two_step = False requires_absence_of_water = False else: yaml_dict = get_reaction_yaml_dict(reaction_name) rxn_name = reaction_name rxn_smarts = get_rxn_smarts_yaml(yaml_dict[rxn_name]) rxn_enzyme_cofactor = get_enzyme_cofactor_yaml(yaml_dict[rxn_name]) positive_tests = get_positive_tests(yaml_dict[rxn_name]) negative_tests = get_negative_tests(yaml_dict[rxn_name]) reaction_type = yaml_dict[rxn_name]['type'] rxn = Reaction.objects(name=reaction_name)[0] experimental = rxn.experimental two_step = rxn.two_step requires_absence_of_water = rxn.requires_absence_of_water result = { "rxn_name": rxn_name, "rxn_smarts": rxn_smarts, "rxn_enzyme_cofactor": rxn_enzyme_cofactor, "positive_tests": positive_tests, "negative_tests": negative_tests, "reaction_type": reaction_type, "experimental": experimental, "two_step": two_step, "requires_absence_of_water": requires_absence_of_water } return jsonify(result=result)
def init_db(): form = InitDB() if form.validate_on_submit() == True: if form.rxns.data != None: rxns_data = form.rxns.data filename = secure_filename(rxns_data.filename) try: rxns_data.save(filename) yaml_dict = rxn_rules_to_db.load_yaml_dict(filename) Reaction.drop_collection() current_app.db_queue.enqueue(rxn_rules_to_db.load_into_mongo, yaml_dict) flash("Yaml loaded ok, reactions_to_db in queue", 'success') except: flash("Failed to load reactions", 'fail') os.remove(filename) if form.biocatdb.data != None: filename = secure_filename(form.biocatdb.data.filename) form.biocatdb.data.save(filename) try: df = biocatdb_excel_to_db.load_df(filename) Paper.drop_collection() Activity.drop_collection() Molecule.drop_collection() Sequence.drop_collection() current_app.db_queue.enqueue(biocatdb_excel_to_db.df_to_db, df) current_app.db_queue.enqueue(make_molecule_db.make_fp_db) flash("Biocatdb excel loaded ok, added to queue", "success") except: flash("Problem loading biocatdb_2 excel", "fail") os.remove(filename) if form.sequences.data != None: filename = secure_filename(form.sequences.data.filename) form.sequences.data.save(filename) try: if '.xlsx' in filename: df = pd.read_excel(filename) elif '.csv' in filename: df = pd.read_csv(filename) else: df = None Exception('File must be .csv or .xlsx') current_app.db_queue.enqueue(task_add_sequence_data, df) flash("Sequences excel loaded ok, added to queue", "success") except Exception as e: flash(f"Problem loading sequences excel - {e}", "fail") os.remove(filename) if form.mongo_dump.data != None: filename = secure_filename(form.mongo_dump.data.filename) form.mongo_dump.data.save(filename) try: if 'mongo_dump.gz' in filename: execute_mongo_restore(filename) else: Exception('File must be mongo_dump.gz') flash("mongo dump loaded ok", "success") except Exception as e: flash(f"Problem loading mongo dump - {e}", "fail") return render_template('init_db/init_db.html', form=form)
def get_reaction_yaml_dict(reaction_name): reaction = Reaction.objects(name=reaction_name)[0] yaml_dict = yaml_conversion.reaction_to_yaml_dict({}, reaction) return yaml_dict
def get_reactions(): reactions = list(Reaction.objects().distinct('name')) reactions.sort() return reactions
def save_reaction(): rxn_selection = request.form['rxn_selection'] rxn_name = request.form['rxn_name'] smarts_yaml = request.form['smarts_yaml'] cofactors = request.form['cofactors'] positive_tests = request.form['positive_tests'] negative_tests = request.form['negative_tests'] rxn_type = request.form['rxn_type'] experimental = request.form['experimental'] two_step = request.form['two_step'] requires_absence_of_water = request.form['requires_absence_of_water'] if experimental == 'false': experimental = False elif experimental == 'true': experimental = True if two_step == 'false': two_step = False elif two_step == 'true': two_step = True if requires_absence_of_water == 'false': requires_absence_of_water = False elif requires_absence_of_water == 'true': requires_absence_of_water = True if rxn_selection == 'Empty template': if len(Reaction.objects(name=rxn_name)) == 0 and rxn_name != '': reaction = Reaction(name=rxn_name) else: result = { 'status': 'failed', 'msg': "Could not create new reaction - name already exists", 'issues': [] } return jsonify(result=result) else: reaction = Reaction.objects(name=rxn_selection)[0] if len(Reaction.objects(name=rxn_name)) == 0: reaction.name = rxn_name reaction.smarts = yaml.load(smarts_yaml, Loader=yaml.FullLoader) reaction.enzyme_types = list( yaml.load(cofactors, Loader=yaml.FullLoader).keys()) reaction.cofactors = yaml.load(cofactors, Loader=yaml.FullLoader) reaction.positive_tests = yaml.load(positive_tests, Loader=yaml.FullLoader) reaction.negative_tests = yaml.load(negative_tests, Loader=yaml.FullLoader) reaction.type = rxn_type reaction.experimental = experimental reaction.two_step = two_step reaction.requires_absence_of_water = requires_absence_of_water reaction.save() refresh = 'False' if rxn_selection != reaction.name: refresh = 'True' activities = Activity.objects(reaction=rxn_selection) for act in activities: act.reaction = reaction.name act.save() print("reaction saved") result = { 'status': 'info', 'msg': "Reaction saved", 'issues': [], 'refresh': refresh } return jsonify(result=result)
def apply_reaction_rev(): p = request.form['p'] reaction_q = Reaction.objects(name=request.form['reaction']) if len(reaction_q) == 0: print(f"{request.form['reaction']} was not found in RetroBioCat") result = {'status': 'danger', 'msg': 'No reaction on RetroBioCat with this name', 'issues': [f"{request.form['reaction']} was not found in RetroBioCat"]} return jsonify(result=result) rdkit_rxns = {'rdkit_rxns': []} rdchiral_rxns = {'rd_chiral_rxns': []} try: for smarts in reaction_q[0].smarts: rdkit_rxns['rdkit_rxns'].append(rdChemReactions.ReactionFromSmarts(smarts)) try: rdchiral_rxns['rd_chiral_rxns'].append(rdchiralReaction(smarts)) except: pass except Exception as e: print('Error parsing reaction SMARTS') print(e) result = {'status': 'danger', 'msg': 'Error parsing reaction SMARTS', 'issues': []} return jsonify(result=result) rule_applicator = RuleApplicator(None) smi = p try: products = rule_applicator.apply_rules(smi, rdchiral_rxns)['rd_chiral_rxns'] except: products = [] if products == []: try: products = rule_applicator.apply_rules_rdkit(smi, rdkit_rxns)['rdkit_rxns'] except: products = [] no_dupl = [] for prod in products: if prod not in no_dupl: no_dupl.append(prod) products = no_dupl svg_dict = make_svg_dict(products) reaction_svgs = [] for smi_list in products: if len(smi_list) == 1: reaction_svgs.append(get_reaction_svg(smi_list[0], '', p)) elif len(smi_list) == 2: reaction_svgs.append(get_reaction_svg(smi_list[0], smi_list[1], p)) else: reaction_svgs.append(get_reaction_svg('', '', p)) if len(products) == 0: result = {'status': 'danger', 'msg': 'No products', 'issues': []} return jsonify(result=result) result = {'status': 'success', 'msg': '', 'products': products, 'reaction_svgs': reaction_svgs, 'svg_dict': svg_dict, 'issues': []} return jsonify(result=result)
enzyme_types=enzymes, cofactors=yaml_dict[rxn_name]['enzymes'], positive_tests=yaml_dict[rxn_name]['positive_tests'], negative_tests=yaml_dict[rxn_name]['negative_tests'], type=yaml_dict[rxn_name]['type']) if 'experimental' in yaml_dict[rxn_name]: reaction.experimental = bool(yaml_dict[rxn_name]['experimental']) if 'two_step' in yaml_dict[rxn_name]: reaction.two_step = bool(yaml_dict[rxn_name]['two_step']) reaction.save() def load_reaction_rules_into_mongo(yaml_file=YAML_PATH): yaml_dict = load_yaml_dict(yaml_file) load_into_mongo(yaml_dict) if __name__ == '__main__': from retrobiocat_web.mongo.default_connection import make_default_connection make_default_connection() Reaction.drop_collection() load_reaction_rules_into_mongo() test_reaction = Reaction.objects.get(name='Primary alcohol oxidation') print(test_reaction.smarts)