Ejemplo n.º 1
0
 def validate(self):
     is_valid = super(Form, self).validate()
     analysis = Analysis.query(Analysis.title == self.title.data).first()
     if analysis is None:
         return is_valid
     return False
Ejemplo n.º 2
0
def launch_fba(username):
    data = request.get_json(force=True)

    logging.debug(data)

    model_name = data['model']
    project_id = data['project_id']
    title = data['title']

    model = Biomodel.query.filter(Biomodel.name == model_name).first_or_404()

    # have often exception here du to encoding issues
    sbml_model = model.get_cobra_model()
    if sbml_model is None:
        return render_template('errors/server_error.html', form=LoginForm())

    solution = _launch_fba(sbml_model,
                           data['objective_functions'],
                           data['constraints'])

    # create analysis object
    a = Analysis(title=data['title'],
                 kind=Analysis.KIND[0],
                 model_id=model.id,
                 project_id=project_id)
    a.results_content = solution.status
    a.serialized_properties = json.dumps(data)
    # in order to populate its id field
    a.save()

    # building several path local and s3 path
    filename = "{}-{}-{}".format(username, project_id, a.id)
    results_url = filename.replace('-', '/')

    # dump solution to file
    d = {'status': solution.status, 'objective_value': solution.f,
         'x_dict': solution.x_dict, 'y_dict': solution.y_dict}

    with open(filename, 'w') as f:
        f.write(json.dumps(d).encode('utf-8'))

    # upload to s3
    logging.info('uploading to s3')
    s3_upload_from_server(filename, destination_filename=results_url)

    # finally save analysis object
    logging.info('saving analysis')
    a.results_url = results_url
    a.save()

    # remove created file
    logging.info('removing created file...')
    try:
        os.remove(filename)
        logging.warn('Done')
    except OSError:
        logging.warn('Enable to remove "{}"'.format(filename))
        logging.warn('Failed !')

    flash('FBA analysis {} saved...'.format(title), 'success')

    return json.dumps({'redirect': url_for('user.profile', username=username)})