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 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 convert_trunc(trunc): """Convert BEL1 trunc() to BEL2 var()""" parent_fn_name = trunc.parent_function.name_short prefix_list = {'p': 'p.', 'r': 'r.', 'g': 'c.'} prefix = prefix_list[parent_fn_name] new_var_arg = f'"truncated at {trunc.args[0].value}"' new_var = bel.lang.ast.Function('var', bo.spec) new_var.add_argument(StrArg(new_var_arg, new_var)) return new_var
def convert_trunc(trunc): """Convert BEL1 trunc() to BEL2 var()""" parent_fn_name = trunc.parent_function.name_short prefix_list = {"p": "p.", "r": "r.", "g": "c."} prefix = prefix_list[parent_fn_name] new_var_arg = f'"truncated at {trunc.args[0].value}"' new_var = bel.lang.ast.Function("var", bo.spec) new_var.add_argument(StrArg(new_var_arg, new_var)) return new_var
def convert_sub(sub): """Convert BEL1 sub() to BEL2 var()""" args = sub.args (ref_aa, pos, new_aa) = args parent_fn_name = sub.parent_function.name_short prefix_list = {'p': 'p.', 'r': 'r.', 'g': 'c.'} prefix = prefix_list[parent_fn_name] new_var_arg = f'"{prefix}{spec["namespaces"]["AminoAcid"]["to_short"][ref_aa.value]}{pos.value}{spec["namespaces"]["AminoAcid"]["to_short"][new_aa.value]}"' new_var = bel.lang.ast.Function('var', bo.spec) new_var.add_argument(StrArg(new_var_arg, new_var)) return new_var
def convert_activity(ast): """Convert BEL1 activities to BEL2 act()""" if len(ast.args) > 1: log.error(f"Activity should not have more than 1 argument {ast.to_string()}") p_arg = ast.args[0] # protein argument print("p_arg", p_arg) ma_arg = Function("ma", bo.spec) ma_arg.add_argument(StrArg(ast.name, ma_arg)) p_arg.change_parent_fn(ma_arg) ast = Function("activity", bo.spec) p_arg.change_parent_fn(ast) ast.add_argument(p_arg) ast.add_argument(ma_arg) return ast
def convert_sub(sub): """Convert BEL1 sub() to BEL2 var()""" args = sub.args (ref_aa, pos, new_aa) = args parent_fn_name = sub.parent_function.name_short prefix_list = {"p": "p.", "r": "r.", "g": "c."} prefix = prefix_list[parent_fn_name] new_var_arg = f'"{prefix}{belspec["namespaces"]["AminoAcid"]["to_short"][ref_aa.value]}{pos.value}{belspec["namespaces"]["AminoAcid"]["to_short"][new_aa.value]}"' new_var = Function("var", version=version) new_var.add_argument(StrArg(new_var_arg, new_var)) return new_var
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