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 = Function("var", version=version) new_var.add_argument(StrArg(new_var_arg, new_var)) return new_var
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 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
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 process_ast(edges, ast, spec): if isinstance(ast, BELAst): pass # TODO composite is not being addressed right now # TODO elif isinstance(ast, Function): if ast.name in ("complex", "complexAbundance"): for arg in ast.args: edges.append(BELAst(ast, "hasComponent", arg, spec)) elif ast.name in ("composite", "compositeAbundance"): for arg in ast.args: edges.append(BELAst(ast, "hasAssociation", arg, spec)) elif ast.name in ("act", "activity"): subject = ast.args[0] edge = BELAst(subject, "hasActivity", ast, spec) edges.append(edge) elif ast.name in ("pmod", "proteinModification"): parent = ast.parent_function src_abundance = Function(parent.name, spec) src_abundance.add_argument(parent.args[0]) edges.append(BELAst(src_abundance, "hasModification", parent, spec)) elif ast.name in ("var", "variant"): parent = ast.parent_function src_abundance = Function(parent.name, spec) src_abundance.add_argument(parent.args[0]) edges.append(BELAst(src_abundance, "hasVariant", parent, spec)) elif ast.name in ("frag", "fragment"): parent = ast.parent_function src_abundance = Function(parent.name, spec) src_abundance.add_argument(parent.args[0]) edges.append(BELAst(src_abundance, "hasFragment", parent, spec)) elif ast.name in ("loc", "location"): parent = ast.parent_function src_abundance = Function(parent.name, spec) src_abundance.add_argument(parent.args[0]) edges.append(BELAst(src_abundance, "hasLocation", parent, spec)) elif ast.name in ("tloc", "translocation"): src_abundance = ast.args[0] from_abundance = copy.deepcopy(src_abundance) from_loc = Function("loc", spec) from_loc.add_argument(ast.args[1].args[0]) from_abundance.add_argument(from_loc) to_abundance = copy.deepcopy(src_abundance) to_loc = Function("loc", spec) to_loc.add_argument(ast.args[2].args[0]) to_abundance.add_argument(to_loc) edges.append(BELAst(ast, "decreases", from_abundance, spec)) edges.append(BELAst(ast, "increases", to_abundance, spec)) edges.append( BELAst(src_abundance, "hasLocation", from_abundance, spec)) edges.append( BELAst(src_abundance, "hasLocation", to_abundance, spec)) elif ast.name in ("sec", "cellSecretion", "surf", "cellSurfaceExpression"): target_loc = locations["extracellular"] if ast.name in ("surf", "cellSurfaceExpression"): target_loc = locations["cellsurface"] src_abundance = ast.args[0] to_abundance = copy.deepcopy(src_abundance) to_loc = Function("loc", spec) to_loc.add_argument(target_loc) to_abundance.add_argument(to_loc) edges.append(BELAst(ast, "increases", to_abundance, spec)) edges.append( BELAst(src_abundance, "hasLocation", to_abundance, spec)) elif ast.name in ("deg", "degradation"): edges.append(BELAst(ast, "directlyDecreases", ast.args[0], spec)) elif ast.name in ("fus", "fusion"): parent = ast.parent_function src_abundance = Function(parent.name, spec) src_abundance.add_argument(ast.args[0]) edges.append(BELAst(src_abundance, "hasFusion", parent, spec)) src_abundance = Function(parent.name, spec) src_abundance.add_argument(ast.args[2]) edges.append(BELAst(src_abundance, "hasFusion", parent, spec)) elif ast.name in ("reactants", "products"): parent = ast.parent_function relation = "hasProduct" if ast.name == "reactants": relation = "hasReactant" for arg in ast.args: edges.append(BELAst(parent, relation, arg, spec)) elif ast.name in ("product", "fusion"): parent = ast.parent_function src_abundance = Function(parent.name, spec) src_abundance.add_argument(ast.args[0]) edges.append(BELAst(src_abundance, "hasFusion", parent, spec)) src_abundance = Function(parent.name, spec) src_abundance.add_argument(ast.args[2]) edges.append(BELAst(src_abundance, "hasFusion", parent, spec)) # Recursively process every element by processing BELAst and Functions if hasattr(ast, "args"): for arg in ast.args: process_ast(edges, arg, spec)