Ejemplo n.º 1
0
def _get_resolver(
    name_data: Union[None, str, pd.DataFrame] = None,
    alts_data: Union[None, str, pd.DataFrame] = None,
    defs_data: Union[None, str, pd.DataFrame] = None,
    lazy: bool = False,
    sql: bool = False,
    uri: Optional[str] = None,
    refs_table: Optional[str] = None,
    alts_table: Optional[str] = None,
    defs_table: Optional[str] = None,
) -> Backend:
    if sql:
        logger.info("using raw SQL backend")
        return RawSQLBackend(
            engine=uri,
            refs_table=refs_table,
            alts_table=alts_table,
            defs_table=defs_table,
        )

    if lazy:
        name_lookup = None
    elif name_data is None:
        name_lookup = _get_lookup_from_path(ensure_ooh_na_na())
    elif isinstance(name_data, str):
        name_lookup = _get_lookup_from_path(name_data)
    elif isinstance(name_data, pd.DataFrame):
        name_lookup = _get_lookup_from_df(name_data)
    else:
        raise TypeError(f"invalid type for `name_data`: {name_data}")

    if lazy:
        alts_lookup = None
    elif alts_data is None and not lazy:
        alts_lookup = _get_lookup_from_path(ensure_alts())
    elif isinstance(alts_data, str):
        alts_lookup = _get_lookup_from_path(alts_data)
    elif isinstance(alts_data, pd.DataFrame):
        alts_lookup = _get_lookup_from_df(alts_data)
    else:
        raise TypeError(f"invalid type for `alt_data`: {alts_data}")

    if lazy:
        defs_lookup = None
    elif defs_data is None and not lazy:
        defs_lookup = _get_lookup_from_path(ensure_definitions())
    elif isinstance(defs_data, str):
        defs_lookup = _get_lookup_from_path(defs_data)
    elif isinstance(defs_data, pd.DataFrame):
        defs_lookup = _get_lookup_from_df(defs_data)
    else:
        raise TypeError(f"invalid type for `defs_data`: {defs_data}")

    return _prepare_backend_with_lookup(
        name_lookup=name_lookup,
        alts_lookup=alts_lookup,
        defs_lookup=defs_lookup,
    )
Ejemplo n.º 2
0
def get_app(
    name_data: Union[None, str, pd.DataFrame] = None,
    alts_data: Union[None, str, pd.DataFrame] = None,
    lazy: bool = False,
    sql: bool = False,
    uri: Optional[str] = None,
    refs_table: Optional[str] = None,
    alts_table: Optional[str] = None,
) -> Flask:
    """Build a flask app.

    :param name_data: If none, uses the internal PyOBO loader. If a string, assumes is a gzip and reads a
     dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
     identifier, and name and is a TSV.
    :param alts_data: If none, uses the internal PyOBO loader. If a string, assumes is a gzip and reads a
     dataframe from there. If a dataframe, uses it directly. Assumes data frame has 3 columns - prefix,
     alt identifier, and identifier and is a TSV.
    :param lazy: don't load the full cache into memory to run
    :param sql_table: Use SQL-based backend
    """
    app = Flask(__name__)
    Swagger(app)
    Bootstrap(app)

    if sql:
        app.config['resolver_backend'] = RawSQLBackend(
            engine=uri,
            refs_table=refs_table,
            alts_table=alts_table,
        )

    else:
        if lazy:
            name_lookup = None
        elif name_data is None:
            name_lookup = _get_lookup_from_path(ensure_ooh_na_na())
        elif isinstance(name_data, str):
            name_lookup = _get_lookup_from_path(name_data)
        elif isinstance(name_data, pd.DataFrame):
            name_lookup = _get_lookup_from_df(name_data)
        else:
            raise TypeError(f'invalid type for `name_data`: {name_data}')

        if lazy:
            alts_lookup = None
        elif alts_data is None and not lazy:
            alts_lookup = _get_lookup_from_path(ensure_alts())
        elif isinstance(alts_data, str):
            alts_lookup = _get_lookup_from_path(alts_data)
        elif isinstance(alts_data, pd.DataFrame):
            alts_lookup = _get_lookup_from_df(alts_data)
        else:
            raise TypeError(f'invalid type for `alt_data`: {alts_data}')

        app.config['resolver_backend'] = _prepare_backend_with_lookup(name_lookup=name_lookup, alts_lookup=alts_lookup)

    app.register_blueprint(resolve_blueprint)
    return app
Ejemplo n.º 3
0
def _load_name(
    *,
    engine: Union[None, str, Engine] = None,
    table: Optional[str] = None,
    path: Optional[str] = None,
    test: bool = False,
):
    engine = _ensure_engine(engine)
    _load_table(
        engine=engine,
        table=table or REFS_TABLE_NAME,
        path=path if path else ensure_ooh_na_na(),
        test=test,
        target_col='name',
        target_col_size=4096,
    )