def make_all_static_models():
    """Write static models for all models in the database."""
    # delete static model dir
    try:
        shutil.rmtree(static_dir)
    except OSError:
        pass

    # make the directories
    try:
        makedirs(join(static_dir, 'raw'))
    except OSError:
        pass

    failed_models = []
    polisher_path = autodetect_model_polisher()
    session = Session()
    bigg_ids = [i[0] for i in session.query(Model.bigg_id)]
    for bigg_id in bigg_ids:
        if DEBUG and bigg_id != 'e_coli_core':
            continue
        # keep track of which models failed
        print('------------------------------------------------------------\n'
              'Dumping model %s' % bigg_id)
        if not write_static_model(bigg_id, polisher_path):
            failed_models.append(bigg_id)
    session.close()
    if len(failed_models) > 0:
        return "Failed for models " + " ".join(failed_models)
Beispiel #2
0
def session(request):
    """Make a session"""
    def teardown():
        Session.close_all()

    request.addfinalizer(teardown)

    return Session()
    def get(self, compartment_bigg_id):
        session = Session()
        result_db = (session
                     .query(Compartment)
                     .filter(Compartment.bigg_id == compartment_bigg_id)
                     .first())
        session.close()

        result = {'bigg_id': result_db.bigg_id, 'name': result_db.name}
        self.return_result(result)
 def get(self):
     metabolite_dict = {k: float(v[0]) for k, v in
                        self.request.query_arguments.iteritems()}
     hash = hash_metabolite_dictionary(metabolite_dict)
     session = Session()
     try:
         results = {'results': [queries.reaction_with_hash(hash, session)],
                    'results_count': 1}
     except NotFoundError:
         results = {'results': [],
                    'results_count': 0}
     session.close()
     self.write(results)
     self.finish()
def safe_query(func, *args, **kwargs):
    """Run the given function, and raise a 404 if it fails.

    Arguments
    ---------

    func: The function to run. *args and **kwargs are passed to this function.

    """
    session = Session()
    kwargs["session"] = session
    try:
        return func(*args, **kwargs)
    except queries.NotFoundError as e:
        raise HTTPError(status_code=404, reason=e.message)
    except ValueError as e:
        raise HTTPError(status_code=400, reason=e.message)
    finally:
        session.close()
    def post(self):
        query_strings = [x.strip() for x in
                         self.get_argument('query', '').split(',')
                         if x != '']
        # run the queries
        session = Session()
        def checkbox_arg(name):
            return self.get_argument(name, None) == 'on'


        all_models = queries.get_model_list(session)
        model_list = [m for m in all_models if checkbox_arg(m)]
        include_metabolites = checkbox_arg('include_metabolites')
        include_reactions = checkbox_arg('include_reactions')
        include_genes = checkbox_arg('include_genes')
        metabolite_results = []
        reaction_results = []
        gene_results = []

        # genes
        for query_string in query_strings:
            if include_genes:
                gene_results += queries.search_for_genes(query_string, session,
                                                         limit_models=model_list)
            if include_reactions:
                reaction_results += queries.search_for_reactions(query_string, session,
                                                                 limit_models=model_list)
            if include_metabolites:
                metabolite_results += queries.search_for_metabolites(query_string, session,
                                                                     limit_models=model_list)
        result = {'results': {'reactions': reaction_results,
                              'metabolites': metabolite_results,
                              'genes': gene_results},
                  'no_pager': True}

        session.close()
        template = env.get_template("list_display.html")
        self.write(template.render(result))
        self.finish()
    def post(self):
        query_string = self.get_argument('query', '')
        database_source = self.get_argument('database_source', '')
        session = Session()
        metabolites = queries.get_metabolites_for_database_id(session,
                                                              query_string,
                                                              database_source)
        reactions = queries.get_reactions_for_database_id(session,
                                                          query_string,
                                                          database_source)
        genes = queries.get_genes_for_database_id(session,
                                                  query_string,
                                                  database_source)
        session.close()
        dictionary = {'results': {'metabolites': metabolites,
                                  'reactions': reactions,
                                  'genes': genes},
                      'no_pager': True,
                      'hide_organism': True}

        template = env.get_template("list_display.html")
        self.write(template.render(dictionary))
        self.finish()
    def get(self):
        # get arguments
        query_string = self.get_argument("query")
        page = self.get_argument('page', None)
        size = self.get_argument('size', None)
        search_type = self.get_argument('search_type', None)
        include_link_urls = "include_link_urls" in self.request.query_arguments

        # defaults
        sort_column = None
        sort_direction = 'ascending'

        # get the sorting column
        columns = _parse_col_arg(self.get_argument('columns', None))
        sort_column, sort_direction = _get_col_name(self.request.query_arguments, columns,
                                                    sort_column, sort_direction)

        # run the queries
        session = Session()
        result = None

        if search_type == 'reactions':
            # reactions
            raw_results = queries.search_for_universal_reactions(query_string, session, page,
                                                                 size, sort_column, sort_direction)
            if include_link_urls:
                raw_results = [dict(x, link_urls={'bigg_id': '/universal/reactions/{bigg_id}'.format(**x)})
                               for x in raw_results]
            result = {'results': [dict(x, model_bigg_id='Universal', organism='') for x in raw_results],
                      'results_count': queries.search_for_universal_reactions_count(query_string,
                                                                                    session)}

        elif search_type == 'metabolites':
            raw_results = queries.search_for_universal_metabolites(query_string, session,
                                                                    page, size, sort_column,
                                                                    sort_direction)
            if include_link_urls:
                raw_results = [dict(x, link_urls={'bigg_id': '/universal/metabolites/{bigg_id}'.format(**x)})
                            for x in raw_results]

            result = {'results': [dict(x, model_bigg_id='Universal', organism='') for x in raw_results],
                      'results_count': queries.search_for_universal_metabolites_count(query_string, session)}

        elif search_type == 'genes':
            raw_results = queries.search_for_genes(query_string, session, page,
                                                   size, sort_column,
                                                   sort_direction)
            if include_link_urls:
                raw_results = [dict(x, link_urls={'bigg_id': '/models/{model_bigg_id}/genes/{bigg_id}'.format(**x)})
                               for x in raw_results]

            result = {'results': raw_results,
                      'results_count': queries.search_for_genes_count(query_string, session)}

        elif search_type == 'models':
            raw_results = queries.search_for_models(query_string, session, page,
                                                    size, sort_column, sort_direction)
            if include_link_urls:
                raw_results = [dict(x, link_urls={'bigg_id': '/models/{bigg_id}'.format(**x),
                                                'metabolite_count': '/models/{bigg_id}/metabolites'.format(**x),
                                                'reaction_count': '/models/{bigg_id}/reactions'.format(**x),
                                                'gene_count': '/models/{bigg_id}/genes'.format(**x)})
                               for x in raw_results]

            result = {'results': raw_results,
                      'results_count': queries.search_for_models_count(query_string, session)}

        else:
            raise HTTPError(400, 'Bad search_type %s' % search_type)

        session.close()
        self.write(result)
        self.finish()
 def get(self):
     session = Session()
     results = [{'bigg_id': x[0], 'name': x[1]}
                for x in session.query(Compartment.bigg_id, Compartment.name)]
     session.close()
     self.return_result(results)