Ejemplo n.º 1
0
def orthologize(ctx, assertion_str, species, version):
    """Orthologize statement

    species ID needs to be the NCBI Taxonomy ID in this format: TAX:<tax_id_number>
    You can use the following common names for species id: human, mouse, rat
      (basically whatever is supported at the api orthologs endpoint)
    """

    print("------------------------------")
    print(f"BEL version: {version}")
    print("------------------------------")

    bo = BEL(assertion_str, version=version)
    bo.orthologize(species)

    if bo.ast is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print("ORIGINAL     ", bo.original_bel_stmt)
        print("ORTHOLOGIZED ", bo.ast)
        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
Ejemplo n.º 2
0
def stmt_validate(ctx, statement, version, api, config_fn):
    """Parse statement and validate """

    if config_fn:
        config = bel.db.Config.merge_config(ctx.config, override_config_fn=config_fn)
    else:
        config = ctx.config

    # Configuration - will return the first truthy result in list else the default option
    api = utils.first_true([api, config["bel_api"]["servers"].get("api_url", None)], None)
    version = utils.first_true(
        [version, config["bel"]["lang"].get("default_bel_version", None)], None
    )

    print("------------------------------")
    print("BEL version: {}".format(version))
    print("API Endpoint: {}".format(api))
    print("------------------------------")

    bo = BEL(version=version, endpoint=api)
    bo.parse(statement)

    if bo.ast is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print(bo.ast.to_triple())
        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
    return
Ejemplo n.º 3
0
def canonicalize(ctx, assertion_str, namespace_targets, version):
    """Canonicalize statement

    Target namespaces can be provided in the following manner:

        bel stmt canonicalize "<BELStmt>" --namespace_targets '{"HGNC": ["EG", "SP"], "CHEMBL": ["CHEBI"]}'
            the value of target_namespaces must be JSON and embedded in single quotes
            reserving double quotes for the dictionary elements
    """

    version = bel.belspec.crud.check_version(version)

    print("------------------------------")
    print(f"BEL version: {version}")
    print("------------------------------")

    bo = BEL(assertion_str, version=version)

    namespace_targets = [a for a in namespace_targets.split(",") if a]
    bo.canonicalize(namespace_targets=namespace_targets)

    if bo.ast is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print("ORIGINAL ", bo.original_bel_stmt)
        print("CANONICAL", bo.ast)
        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
Ejemplo n.º 4
0
    def on_get(self, req, resp, version, belstr=''):

        api_url = config['bel_api']['servers']['api_url']
        bel_obj = BEL(version=version, api_url=api_url)

        decanon_belstr = bel_obj.parse(belstr).decanonicalize().to_string()

        resp.media = {'decanonicalized': decanon_belstr, 'original': belstr}
        resp.status = falcon.HTTP_200
Ejemplo n.º 5
0
    def on_get(self, req, resp, version, belstr=""):

        api_url = config["bel_api"]["servers"]["api_url"]
        bel_obj = BEL(version=version, api_url=api_url)

        decanon_belstr = bel_obj.parse(belstr).decanonicalize().to_string()

        resp.media = {"decanonicalized": decanon_belstr, "original": belstr}
        resp.status = falcon.HTTP_200
Ejemplo n.º 6
0
def edges(ctx, statement, rules, species, namespace_targets, version, api, config_fn):
    """Create BEL Edges from BEL Statement"""

    if config_fn:
        config = bel.db.Config.merge_config(ctx.config, override_config_fn=config_fn)
    else:
        config = ctx.config

    # Configuration - will return the first truthy result in list else the default option
    if namespace_targets:
        namespace_targets = json.loads(namespace_targets)
    if rules:
        rules = rules.replace(" ", "").split(",")

    namespace_targets = utils.first_true(
        [namespace_targets, config["bel"]["lang"].get("canonical")], None
    )
    api_url = utils.first_true(
        [api, config["bel_api"]["servers"].get("api_url", None)], None
    )
    version = utils.first_true(
        [version, config["bel"]["lang"].get("default_bel_version", None)], None
    )

    print("------------------------------")
    print("BEL version: {}".format(version))
    print("API Endpoint: {}".format(api))
    print("------------------------------")

    bo = BEL(version=version, endpoint=api_url)
    if species:
        edges = (
            bo.parse(statement)
            .orthologize(species)
            .canonicalize(namespace_targets=namespace_targets)
            .compute_edges(rules=rules)
        )
    else:
        edges = (
            bo.parse(statement)
            .canonicalize(namespace_targets=namespace_targets)
            .compute_edges(rules=rules)
        )

    if edges is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print(json.dumps(edges, indent=4))

        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
    return
Ejemplo n.º 7
0
def canonicalize(ctx, statement, namespace_targets, version, api, config_fn):
    """Canonicalize statement

    Target namespaces can be provided in the following manner:

        bel stmt canonicalize "<BELStmt>" --namespace_targets '{"HGNC": ["EG", "SP"], "CHEMBL": ["CHEBI"]}'
            the value of target_namespaces must be JSON and embedded in single quotes
            reserving double quotes for the dictionary elements
    """

    if config_fn:
        config = bel.db.Config.merge_config(ctx.config, override_config_fn=config_fn)
    else:
        config = ctx.config

    # Configuration - will return the first truthy result in list else the default option
    if namespace_targets:
        namespace_targets = json.loads(namespace_targets)

    namespace_targets = utils.first_true(
        [namespace_targets, config.get("canonical")], None
    )
    api = utils.first_true([api, config.get("api", None)], None)
    version = utils.first_true([version, config.get("bel_version", None)], None)

    print("------------------------------")
    print("BEL version: {}".format(version))
    print("API Endpoint: {}".format(api))
    print("------------------------------")

    bo = BEL(version=version, endpoint=api)
    bo.parse(statement).canonicalize(namespace_targets=namespace_targets)

    if bo.ast is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print("ORIGINAL ", bo.original_bel_stmt)
        print("CANONICAL", bo.ast)
        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
    return
Ejemplo n.º 8
0
    def on_get(self, req, resp, version, belstr=""):

        namespace_targets = req.get_param("namespace_targets", default=None)
        api_url = config["bel_api"]["servers"]["api_url"]

        log.info(f"Api_url {api_url}")

        bel_obj = BEL(version=version, api_url=api_url)

        canon_belstr = (bel_obj.parse(belstr).canonicalize(
            namespace_targets=namespace_targets).to_string())

        # TODO figure out how to handle naked namespace:val better
        if not canon_belstr:
            canon_belstr = terms.canonicalize(belstr)

        resp.media = {"canonicalized": canon_belstr, "original": belstr}
        resp.status = falcon.HTTP_200
Ejemplo n.º 9
0
    def on_get(self, req, resp, version, belstr=''):

        namespace_targets = req.get_param('namespace_targets', default=None)
        api_url = config['bel_api']['servers']['api_url']

        log.info(f'Api_url {api_url}')

        bel_obj = BEL(version=version, api_url=api_url)

        canon_belstr = bel_obj.parse(belstr).canonicalize(
            namespace_targets=namespace_targets).to_string()

        # TODO figure out how to handle naked namespace:val better
        log.info(f'Canonicalize BEL string {canon_belstr}')
        if not canon_belstr:
            canon_belstr = terms.canonicalize(belstr)

        resp.media = {'canonicalized': canon_belstr, 'original': belstr}
        resp.status = falcon.HTTP_200
Ejemplo n.º 10
0
def orthologize(ctx, statement, species, version, api, config_fn):
    """Orthologize statement

    species ID needs to be the NCBI Taxonomy ID in this format: TAX:<tax_id_number>
    You can use the following common names for species id: human, mouse, rat
      (basically whatever is supported at the api orthologs endpoint)
    """

    if config_fn:
        config = bel.db.Config.merge_config(ctx.config, override_config_fn=config_fn)
    else:
        config = ctx.config

    # Configuration - will return the first truthy result in list else the default option
    api_url = utils.first_true(
        [api, config["bel_api"]["servers"].get("api_url", None)], None
    )
    version = utils.first_true(
        [version, config["bel"]["lang"].get("default_bel_version", None)], None
    )

    print("------------------------------")
    print("BEL version: {}".format(version))
    print("API Endpoint: {}".format(api))
    print("------------------------------")

    bo = BEL(version=version, endpoint=api_url)
    bo.parse(statement).orthologize(species)

    if bo.ast is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print("ORIGINAL     ", bo.original_bel_stmt)
        print("ORTHOLOGIZED ", bo.ast)
        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
    return
Ejemplo n.º 11
0
def stmt_validate(ctx, assertion_str, version):
    """Parse statement and validate """

    version = bel.belspec.crud.check_version(version)

    print("------------------------------")
    print(f"BEL version: {version}")
    print("------------------------------")

    bo = BEL(assertion_str, version=version)

    if bo.ast is None:
        print(bo.original_bel_stmt)
        print(bo.parse_visualize_error)
        print(bo.validation_messages)
    else:
        print(bo.ast.to_triple())
        if bo.validation_messages:
            print(bo.validation_messages)
        else:
            print("No problems found")
Ejemplo n.º 12
0
# Standard Library
import json

# Third Party
from loguru import logger

# Local
import bel.belspec.crud
import bel.core.settings as settings
from bel.belspec.crud import get_enhanced_belspec
from bel.lang.ast import BELAst, Function, NSArg, StrArg
from bel.lang.belobj import BEL

version = bel.belspec.crud.get_latest_version()
bo = BEL("", version=version)
belspec = get_enhanced_belspec(bo.version)


def migrate(belstr: str) -> str:
    """Migrate BEL 1 to 2.0.0

    Args:
        bel: BEL 1

    Returns:
        bel: BEL 2
    """

    bo.parse(belstr)