Exemple #1
0
def remove_table_from_database(config_file, table_name, alias, silent,
                               integrity_cancel):
    """ Function removes a given table from a database

    :param silent:
    :param config_file:
    :param table_name:
    :param alias:
    :return:
    """
    _cfg, _tbl, _sil, _al = config_file, table_name, silent, alias
    config, config_file = ConfigManager.confirm_config_set(config_file)
    if alias != "None":
        table_name = ConfigManager.get_name_by_alias(alias, config)
        assert table_name is not None, TableNameAssertString.TABLE_NOT_FOUND
    if not silent:
        _remove_table_display_message_prelude(
            config[ConfigKeys.DATABASES][ConfigKeys.db_name],
            config[ConfigKeys.DATABASES][ConfigKeys.rel_work_dir], table_name,
            alias)

    cfg = ConfigManager(config, table_name)
    engine = BaseData.get_engine(cfg.db_dir, cfg.db_name + ".db")
    sess = BaseData.get_session_from_engine(engine)
    TableClass = ClassManager.get_class_orm(table_name, engine)
    UserClass = type(table_name, (Record, ), {})
    mapper(UserClass, TableClass)
    all_records = sess.query(UserClass).all()
    for record in all_records:
        if record:
            print_if_not_silent(silent, " ..Removing record %s" % record._id)
            try:
                os.remove(record.full_path())
            except OSError:
                continue
    TableClass.drop(engine)
    cfg.remove_table_from_config_file(table_name)
    os.remove(cfg.classes_file)
    shutil.rmtree(cfg.table_dir)
    if not silent:
        _remove_columns_display_message_epilogue()
    if not integrity_cancel:
        integrity_check(_cfg, _tbl, _al, _sil)
Exemple #2
0
def get_table(config_path, table_name=None, alias=None):
    """ Primary method for users importing

    :param config_path:
    :param table_name:
    :param alias:
    :return:
    """
    # Load config data from file
    cfg, config_path = ConfigManager.confirm_config_set(config_path)
    if alias:
        table_name = ConfigManager.get_name_by_alias(alias, cfg)
    config = ConfigManager(cfg, table_name)
    # Get session to return
    engine = BaseData.get_engine(config.db_dir, config.db_name + ".db")
    sess = BaseData.get_session_from_engine(engine)
    # Generate table class and name based on presence of alias
    TableClass = ClassManager.get_class_orm(table_name, engine)
    UserClass = type(table_name, (Record, ), {})
    # Map to SQL orm
    mapper(UserClass, TableClass)
    return RecordList(sess, UserClass, config)
Exemple #3
0
def summarize_database(config_file, view, query, table_name, alias, write,
                       write_tsv, unique):
    """ Function will query all tables listed in the config file, outputting simple
    metrics to the screen

    :param config_file:
    :param view:
    :param query:
    :param table_name:
    :param alias:
    :param write:
    :param write_tsv:
    :return:
    """
    if not view:
        if query != "None":
            assert (query != "None"
                    and (table_name != "None" or alias != "None")
                    ), SummarizeDBAssertString.QUERY_AND_TABLE_SET
        assert not (table_name != "None" and alias != "None"
                    ), SummarizeDBAssertString.ALIAS_OR_TABLE_ONLY
    config, config_file = ConfigManager.confirm_config_set(config_file)
    if not view.lower()[0] == "t":
        _summarize_display_message_prelude(
            config[ConfigKeys.DATABASES][ConfigKeys.db_name])
    if alias != "None":
        table_name = ConfigManager.get_name_by_alias(alias, config)
    if table_name != "None" or alias != "None":
        tables_in_database = (table_name, )
    else:
        tables_in_database = config[ConfigKeys.TABLES_TO_DB].keys()
    if ("~>" in query) and ("->" in query):
        assert table_name == 'None', "Query cannot contain '~>/->' statement with a table name"
        matching_records = []
        eval_sess, EvalClass, eval_cfg = load_table_metadata(
            config, "evaluation")
        eval_rl = RecordList(eval_sess,
                             EvalClass,
                             eval_cfg,
                             compute_metadata=False)
        fxn_sess, FxnClass, fxn_cfg = load_table_metadata(config, "functions")
        fxn_rl = RecordList(fxn_sess,
                            FxnClass,
                            fxn_cfg,
                            compute_metadata=False)
        evaluation_query, block_1 = query.split("~>")
        function_query, annotation_query = block_1.split("->")
        if evaluation_query.replace(" ", "") != '':
            eval_rl.query(evaluation_query)
        else:
            eval_rl.query()
        if function_query.replace(" ", "") != '':
            fxn_rl.query(function_query)
        else:
            fxn_rl.query()
        for record in eval_rl:
            if record in fxn_rl:
                record_id = record._id.split(".")[0]
                sess, UserClass, cfg = load_table_metadata(config, record_id)
                annot_rl = RecordList(sess,
                                      UserClass,
                                      cfg,
                                      compute_metadata=False)
                _handle_query(annot_rl, annotation_query)
                if write_tsv != 'None':
                    annot_rl.write_tsv(record_id + "." +
                                       write_tsv.replace(".tsv", "") + ".tsv")
                for record_2 in annot_rl:
                    matching_records.append(record_2)
                if write_tsv == 'None' and write == 'None':
                    print(annot_rl.summarize())
        if matching_records and write != "None":
            rl = RecordList(compute_metadata=False,
                            records_list=matching_records)
            rl.write_records(write)
        return
    if "~>" in query:
        assert table_name == 'None', "Query cannot contain a '~>' statement with a table name"
        matching_records = []
        eval_sess, EvalClass, eval_cfg = load_table_metadata(
            config, "evaluation")
        eval_rl = RecordList(eval_sess,
                             EvalClass,
                             eval_cfg,
                             compute_metadata=False)
        evaluation_query, annotation_query = query.split("~>")
        if evaluation_query.replace(" ", "") != '':
            eval_rl.query(evaluation_query)
        else:
            eval_rl.query()
        for record in eval_rl:
            record_id = record._id.split(".")[0]
            sess, UserClass, cfg = load_table_metadata(config, record_id)
            annot_rl = RecordList(sess, UserClass, cfg, compute_metadata=False)
            _handle_query(annot_rl, annotation_query)
            if write_tsv != 'None':
                annot_rl.write_tsv(record_id + "." +
                                   write_tsv.replace(".tsv", "") + ".tsv")
            for record_2 in annot_rl:
                matching_records.append(record_2)
            if write_tsv == 'None' and write == 'None':
                print(annot_rl.summarize())
        if matching_records and write != "None":
            rl = RecordList(compute_metadata=False,
                            records_list=matching_records)
            rl.write_records(write)
        return
    if "->" in query:
        assert table_name == 'None', "Query cannot contain a '->' statement with a table name"
        matching_records = []
        eval_sess, EvalClass, eval_cfg = load_table_metadata(
            config, "functions")
        eval_rl = RecordList(eval_sess,
                             EvalClass,
                             eval_cfg,
                             compute_metadata=False)
        evaluation_query, annotation_query = query.split("->")
        if evaluation_query.replace(" ", "") != '':
            eval_rl.query(evaluation_query)
        else:
            eval_rl.query()
        for record in eval_rl:
            record_id = record._id.split(".")[0]
            sess, UserClass, cfg = load_table_metadata(config, record_id)
            annot_rl = RecordList(sess, UserClass, cfg, compute_metadata=False)
            _handle_query(annot_rl, annotation_query)
            if write_tsv != 'None':
                annot_rl.write_tsv(record_id + "." +
                                   write_tsv.replace(".tsv", "") + ".tsv")
            for record_2 in annot_rl:
                matching_records.append(record_2)
            if write_tsv == 'None' and write == 'None':
                print(annot_rl.summarize())
        if matching_records and write != "None":
            rl = RecordList(compute_metadata=False,
                            records_list=matching_records)
            rl.write_records(write)
        return
    if ">>" in query:
        assert table_name == 'None', "Query cannot contain a '>>' statement with a table name"
        eval_sess, EvalClass, eval_cfg = load_table_metadata(
            config, "evaluation")
        eval_rl = RecordList(eval_sess,
                             EvalClass,
                             eval_cfg,
                             compute_metadata=False)
        evaluation_query, annotation_query = query.split(">>")
        if evaluation_query.replace(" ", "") != '':
            eval_rl.query(evaluation_query)
        else:
            eval_rl.query()
        sess, UserClass, cfg = load_table_metadata(config, "functions")
        if len(eval_rl) > 0:
            in_query = ""
            for record in eval_rl:
                in_query += "_id == '%s' OR " % record._id
            annot_rl = RecordList(sess, UserClass, cfg, compute_metadata=False)
            if annotation_query.replace(" ", "") != '':
                annot_rl.query(annotation_query + " AND " + in_query[:-4])
            else:
                annot_rl.query(in_query[:-4])
            if annot_rl is not None:
                if write != "None":
                    annot_rl.write_records(write)
                if write_tsv != "None":
                    annot_rl.write_tsv(write_tsv.replace(".tsv", "") + ".tsv")
                if write_tsv == 'None' and write == 'None':
                    print(annot_rl.summarize())
        else:
            print(eval_rl.summarize())
        return
    if view == "None" and unique == 'None':
        for tbl_name in tables_in_database:
            # Display queried info for single table and break
            if table_name == 'None' or table_name == tbl_name:
                sess, UserClass, cfg = load_table_metadata(config, tbl_name)
                annot_rl = RecordList(sess,
                                      UserClass,
                                      cfg,
                                      compute_metadata=True)
                _handle_query(annot_rl, query)
                if len(annot_rl) > 1:
                    print(annot_rl.summarize())
                elif len(annot_rl) == 1:
                    print(annot_rl[0])
    for tbl_name in tables_in_database:
        sess, UserClass, cfg = load_table_metadata(config, tbl_name)
        annot_rl = RecordList(sess, UserClass, cfg)
        # Display column info for table
        if view.lower()[0] == "c":
            # Display queried info for single table and break
            # Do not need to query since only displaying columns
            print(annot_rl.columns_summary())
        elif view.lower()[0] == "t":
            # Display queried info for single table and break
            # Do not need to query since only displaying columns
            print(annot_rl.table_name_summary())
        if write != "None" and table_name == tbl_name:
            _handle_query(annot_rl, query)
            annot_rl.write_records(write)
        if write_tsv != "None" and table_name == tbl_name:
            _handle_query(annot_rl, query)
            annot_rl.write_tsv(write_tsv)
        if unique != 'None' and table_name == tbl_name:
            _handle_query(annot_rl)
            col_vals = set()
            for record in annot_rl:
                val = getattr(record, unique, "None")
                if val:
                    col_vals.add(val)
            col_vals = sorted(col_vals)
            for val in col_vals:
                print(val)