def convert_fus(ast): """Convert BEL1 fus() to BEL2 fus()""" parent_fn_name = ast.name_short prefix_list = {'p': 'p.', 'r': 'r.', 'g': 'c.'} prefix = prefix_list[parent_fn_name] fus1_ns = ast.args[0].namespace fus1_val = ast.args[0].value arg_fus = ast.args[1] fus_args = [None, '?', '?'] for idx, arg in enumerate(arg_fus.args): fus_args[idx] = arg fus2_ns = fus_args[0].namespace fus2_val = fus_args[0].value if fus_args[1] == '?': fus1_range = fus_args[1] else: fus1_range = f'"{prefix}1_{fus_args[1].value}"' if fus_args[2] == '?': fus2_range = fus_args[2] else: fus2_range = f'"{prefix}{fus_args[2].value}_?"' fus = Function('fus', spec, parent_function=ast) fus.args = [ NSArg(fus1_ns, fus1_val, fus), StrArg(fus1_range, fus), NSArg(fus2_ns, fus2_val, fus), StrArg(fus2_range, fus), ] # Remove BEL ast_args = ast.args ast_args.pop(0) ast_args.pop(0) if ast_args == [None]: ast_args = [] ast.args = [] ast.add_argument(fus) if len(ast_args) > 0: ast.args.extend(ast_args) return ast
def convert_fus(ast): """Convert BEL1 fus() to BEL2 fus()""" parent_fn_name = ast.name_short prefix_list = {"p": "p.", "r": "r.", "g": "c."} prefix = prefix_list[parent_fn_name] fus1_ns = ast.args[0].namespace fus1_val = ast.args[0].value arg_fus = ast.args[1] fus_args = [None, "?", "?"] for idx, arg in enumerate(arg_fus.args): fus_args[idx] = arg fus2_ns = fus_args[0].namespace fus2_val = fus_args[0].value if fus_args[1] == "?": fus1_range = fus_args[1] else: fus1_range = f'"{prefix}1_{fus_args[1].value}"' if fus_args[2] == "?": fus2_range = fus_args[2] else: fus2_range = f'"{prefix}{fus_args[2].value}_?"' fus = Function("fus", version=version, parent=ast) fus.args = [ NSArg(fus1_ns, fus1_val, fus), StrArg(fus1_range, fus), NSArg(fus2_ns, fus2_val, fus), StrArg(fus2_range, fus), ] # Remove BEL ast_args = ast.args ast_args.pop(0) ast_args.pop(0) if ast_args == [None]: ast_args = [] ast.args = [] ast.add_argument(fus) if len(ast_args) > 0: ast.args.extend(ast_args) return ast
def convert_tloc(ast): """Convert BEL1 tloc() to BEL2""" from_loc_arg = ast.args[1] to_loc_arg = ast.args[2] from_loc = Function("fromLoc", version=version, parent=ast) from_loc.add_argument( NSArg(from_loc_arg.namespace, from_loc_arg.value, parent=from_loc)) to_loc = Function("toLoc", version=version, parent=ast) to_loc.add_argument( NSArg(to_loc_arg.namespace, to_loc_arg.value, parent=to_loc)) ast.args[1] = from_loc ast.args[2] = to_loc return ast
def add_ast_fn(d, spec, parent_function=None): """Convert dict AST to object AST Function Args: ast_fn: AST object Function d: AST as dictionary spec: BEL Specification Return: ast_fn """ if d["type"] == "Function": ast_fn = Function(d["function"]["name"], spec, parent_function=parent_function) for arg in d["args"]: if arg["type"] == "Function": ast_fn.add_argument( add_ast_fn(arg, spec, parent_function=ast_fn)) elif arg["type"] == "NSArg": ast_fn.add_argument( NSArg(arg["nsarg"]["ns"], arg["nsarg"]["ns_val"], ast_fn)) elif arg["type"] == "StrArg": ast_fn.add_argument(StrArg(arg["arg"], ast_fn)) return ast_fn
def add_ast_fn(d, spec, parent_function=None): """Convert dict AST to object AST Function Args: ast_fn: AST object Function d: AST as dictionary spec: BEL Specification Return: ast_fn """ if d['type'] == 'Function': ast_fn = Function(d['function']['name'], spec, parent_function=parent_function) for arg in d['args']: if arg['type'] == 'Function': ast_fn.add_argument(add_ast_fn(arg, spec, parent_function=ast_fn)) elif arg['type'] == 'NSArg': ast_fn.add_argument(NSArg(arg['nsarg']['ns'], arg['nsarg']['ns_val'], ast_fn)) elif arg['type'] == 'StrArg': ast_fn.add_argument(StrArg(arg['arg'], ast_fn)) return ast_fn
import copy from typing import List, Any, Mapping, MutableSequence from bel.lang.ast import BELAst, Function, NSArg from structlog import get_logger log = get_logger() # Typings Edges = MutableSequence[Mapping[str, Any]] BELSpec = Mapping[str, Any] ComputeRules = List[str] locations = { "extracellular": NSArg("GO", "extracellular space"), "cellsurface": NSArg("GO", "cell surface"), } def compute_edges(ast: BELAst, spec: BELSpec) -> Edges: """Compute edges""" edges = [] if ast.bel_object.__class__.__name__ == "BELAst": edges.append(ast.bel_object) process_ast(edges, ast, spec) return edges