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
def get_client(host=None, port=None, username=None, password=None, enable_logging=True): """Get arango client and edgestore db handle""" host = utils.first_true( [host, config["bel_api"]["servers"]["arangodb_host"], "localhost"]) port = utils.first_true( [port, config["bel_api"]["servers"]["arangodb_port"], 8529]) username = utils.first_true( [username, config["bel_api"]["servers"]["arangodb_username"], ""]) password = utils.first_true([ password, config.get( "secrets", config["secrets"]["bel_api"]["servers"].get("arangodb_password")), "", ]) client = arango.client.ArangoClient( protocol=config["bel_api"]["servers"]["arangodb_protocol"], host=host, port=port) return client
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
def get_user_creds(username, password): """Get username/password Use provided username and password OR in config OR blank in that order """ username = utils.first_true( [username, config["bel_api"]["servers"]["arangodb_username"]], default="" ) password = utils.first_true( [password, config["secrets"]["bel_api"]["servers"].get("arangodb_password")], default="" ) return username, password
def test_first_true(): test1 = [False, 1, "2", None] test2 = [None, "", "2", None] test3 = [None, False, ""] result = utils.first_true(test1) assert result == 1 result = utils.first_true(test2) assert result == "2" # Result is the default value '3' result = utils.first_true(test3, "3") assert result == "3"
def test_first_true(): test1 = [False, 1, '2', None] test2 = [None, '', '2', None] test3 = [None, False, ''] result = utils.first_true(test1) assert result == 1 result = utils.first_true(test2) assert result == '2' # Result is the default value '3' result = utils.first_true(test3, '3') assert result == '3'
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
def nanopub_validate(ctx, input_fn, output_fn, api, config_fn): """Validate nanopubs""" if config_fn: config = bel.db.Config.merge_config(ctx.config, override_config_fn=config_fn) else: config = ctx.config api = utils.first_true([api, config["bel_api"]["servers"].get("api_url", None)], None) print(f"Running validate nanopubs using {api}")
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
def get_client(host=None, port=None, username=None, password=None, enable_logging=True): """Get arango client and edgestore db handle""" host = utils.first_true([host, config["bel_api"]["servers"]["arangodb_host"], "localhost"]) port = utils.first_true([port, config["bel_api"]["servers"]["arangodb_port"], 8529]) username = utils.first_true([username, config["bel_api"]["servers"]["arangodb_username"], ""]) password = utils.first_true( [ password, config.get("secrets", config["secrets"]["bel_api"]["servers"].get("arangodb_password")), "", ] ) arango_url = f"http://{host}:{port}" try: client = arango.ArangoClient(hosts=arango_url) client.db(verify=True) return client except Exception as e: log.error(f"Cannot access arangodb at {arango_url}") return None
def pipeline( ctx, input_fn, db_save, db_delete, output_fn, rules, species, namespace_targets, version, api, config_fn, ): """BEL Pipeline - BEL Nanopubs into BEL Edges This will process BEL Nanopubs into BEL Edges by validating, orthologizing (if requested), canonicalizing, and then computing the BEL Edges based on the given rule_set. \b input_fn: If input fn has *.gz, will read as a gzip file If input fn has *.jsonl*, will parsed as a JSONLines file IF input fn has *.json*, will be parsed as a JSON file If input fn has *.yaml* or *.yml*, will be parsed as a YAML file \b output_fn: If output fn has *.gz, will written as a gzip file If output fn has *.jsonl*, will written as a JSONLines file IF output fn has *.json*, will be written as a JSON file If output fn has *.yaml* or *.yml*, will be written as a YAML file If output fn has *.jgf, will be written as JSON Graph Formatted file """ 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 ) rules = utils.first_true( [rules, config["bel"]["nanopub"].get("pipeline_edge_rules", False)], False ) 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 ) n = bnn.Nanopub() try: json_flag, jsonl_flag, yaml_flag, jgf_flag = False, False, False, False all_bel_edges = [] fout = None if db_save or db_delete: if db_delete: arango_client = bel.db.arangodb.get_client() bel.db.arangodb.delete_database(arango_client, "edgestore") else: arango_client = bel.db.arangodb.get_client() edgestore_handle = bel.db.arangodb.get_edgestore_handle(arango_client) elif re.search("ya?ml", output_fn): yaml_flag = True elif "jsonl" in output_fn: jsonl_flag = True elif "json" in output_fn: json_flag = True elif "jgf" in output_fn: jgf_flag = True if db_save: pass elif "gz" in output_fn: fout = gzip.open(output_fn, "wt") else: fout = open(output_fn, "wt") nanopub_cnt = 0 with timy.Timer() as timer: for np in bnf.read_nanopubs(input_fn): # print('Nanopub:\n', json.dumps(np, indent=4)) nanopub_cnt += 1 if nanopub_cnt % 100 == 0: timer.track(f"{nanopub_cnt} Nanopubs processed into Edges") bel_edges = n.bel_edges( np, namespace_targets=namespace_targets, orthologize_target=species, rules=rules, ) if db_save: bel.edge.edges.load_edges_into_db(edgestore_handle, edges=bel_edges) elif jsonl_flag: fout.write("{}\n".format(json.dumps(bel_edges))) else: all_bel_edges.extend(bel_edges) if db_save: pass elif yaml_flag: fout.write("{}\n".format(yaml.dumps(all_bel_edges))) elif json_flag: fout.write("{}\n".format(json.dumps(all_bel_edges))) elif jgf_flag: bnf.edges_to_jgf(output_fn, all_bel_edges) finally: if fout: fout.close()