def gapfill_model(app): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() if MODEL_ID not in request.args: return missing_argument(MODEL_ID) if OUTPUT_ID not in request.args: return missing_argument(OUTPUT_ID) fasta_id = request.args[FASTA_ID] if FASTA_ID in request.args else None if fasta_id is None or fasta_id == '': return missing_argument(FASTA_ID) model_id = request.args['model_id'] session_id = session_management.get_session_id() likelihoods = db.retrieve_probanno(session, fasta_id) if likelihoods is None: return not_found(fasta_id + " not found") addReactions = True # Could make an argument for more customization model = _retrieve_model(session_id, model_id) if model is None: return not_found(model_id + " not found") template = request.args['template'] if 'template' in request.args else MICROBIAL universal_model_file = app.config['UNIVERSAL_MODELS'] + template + '.json' job = Job(session_id, GAPFILL_MODEL_JOB, model_id) model_queue.enqueue(_async_gapfill_model, job, model_id, session_id, model, universal_model_file, likelihoods, addReactions, copy.copy(request.args), timeout=600, job_id=job.id, solver=app.config['SOLVER']) return jsonify(retrieve_job(job.id).to_dict_dto())
def get_job(): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() if JOB_ID not in request.args: return missing_argument(JOB_ID) job = retrieve_job(request.args[JOB_ID]) if job is None: return not_found("Job not found") return jsonify(job.to_dict_dto())
def get_model(app): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() if MODEL_ID not in request.args: return missing_argument(MODEL_ID) model = db.find_model(session, request.args[MODEL_ID]) if model is None: return not_found(request.args[MODEL_ID] + " not found") return Response(model[-1], mimetype='application/json')
def get_probanno(): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() fasta_id = request.args[FASTA_ID] if FASTA_ID in request.args else None if fasta_id is None or fasta_id == '': return missing_argument(FASTA_ID) likelihoods = db.retrieve_probanno(session, fasta_id) if likelihoods is None: return not_found(fasta_id + " not found") return Response(likelihoods[-2], mimetype='application/json')
def download_model(app): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() if MODEL_ID not in request.args: return missing_argument(MODEL_ID) model_id = request.args[MODEL_ID] model = db.find_model(session, model_id) if model is None: return not_found(model_id + " not found") filename = str(model_id) + '.json' with open(app.config['UPLOAD_FOLDER'] + filename, 'w') as f: f.write(json.dumps(model[-1])) return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
def download_probanno(app): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() fasta_id = request.args[FASTA_ID] if FASTA_ID in request.args else None if fasta_id is None or fasta_id == '': return missing_argument(FASTA_ID) likelihoods = db.retrieve_probanno(session, fasta_id) if likelihoods is None: abort(404) filename = str(fasta_id) + '.json' with open(app.config['UPLOAD_FOLDER'] + filename, 'w') as f: f.write(json.dumps(likelihoods[-2])) return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
def get_reaction_probabilities(app, fasta_id=None, fasta_file=None): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() template = None if request.method == GET: fasta_id = request.args[FASTA_ID] if fasta_id is None else fasta_id # check cache if fasta_id is not None and fasta_id != '': likelihoods = db.find_probanno(fasta_id) if likelihoods is not None: job = Job(session, CALCULATE_PROBANNO_JOB, fasta_id, status=COMPLETE) # add a record of us calculating this job unless we already have if db.retrieve_probanno(session, fasta_id) is None: db.insert_probanno_record(fasta_id, session, likelihoods[2]) return jsonify(job.to_dict_dto()) # download fasta file from Uniprot and continue try: fasta_file = get_fasta_by_id(app, fasta_id) if fasta_file is None else fasta_file except probanno.FastaNotFoundError: return not_found(fasta_id + " not found") template = request.args[TEMPLATE] if TEMPLATE in request.args else DEFAULT_TEMPLATE if request.method == PUT: fasta_id = request.form[FASTA_ID] if fasta_id is None else fasta_id if fasta_id is None: return missing_argument(FASTA_ID) if FASTA in request.files: fasta_file = app.config['UPLOAD_FOLDER'] + utils.upload_file(app, request.files[FASTA]) else: return missing_argument(FASTA_ID) template = request.form[TEMPLATE] if TEMPLATE in request.form else DEFAULT_TEMPLATE template_file = TEMPLATE_FILES[template] if template in TEMPLATE_FILES else TEMPLATE_FILES[DEFAULT_TEMPLATE] gen_id = request.args[FASTA_ID] if FASTA_ID in request.args and request.args[FASTA_ID] is not None else fasta_file fasta_name = get_name_from_fasta(fasta_file) template_model_file = app.config['MODEL_TEMPLATES'] + template_file job = Job(session, CALCULATE_PROBANNO_JOB, fasta_id) probanno_queue.enqueue(_async_get_reaction_probabilities, job, fasta_id, fasta_name, session, fasta_file, template_model_file, gen_id, job_id=job.id, timeout=600) return jsonify(retrieve_job(job.id).to_dict_dto())
def load_model(app): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() if 'file' not in request.files: return missing_argument('file') file = request.files['file'] filename = utils.upload_file(app, file, ALLOWED_EXTENSIONS) # load model w/ cobra model = None try: model = cobra_modeling.from_json_file(os.path.join(app.config['UPLOAD_FOLDER'], filename)) except BaseException as e: return bad_request("Invalid CobraPy Model. Not in proper cobra.io.json deserializable format") os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if MODEL_ID not in request.form: return missing_argument(MODEL_ID) model_id = request.form[MODEL_ID] if db.find_model(session, model_id) is not None: db.delete_model(session, model_id) db.insert_model(session, model_id, cobra_modeling.model_to_json(model)) return Response()
def list_models(): # session_id = request.args['sid'] session_id = session_management.get_session_id() if session_id is None: return session_management.bad_or_missing_session() return jsonify(db.list_models(session_id))
def list_probannos(): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() probs = [{'fasta_id': p[0], 'name': p[1]} for p in db.list_probannos(session)] return jsonify(probs)
def list_jobs(): session = session_management.get_session_id() if session is None: return session_management.bad_or_missing_session() jobs = [Job.from_db_tuple(job).to_dict_dto() for job in db.list_jobs(session)] return jsonify(jobs)