<commit_before># MRA stands for mechanistic reasoning agent. 
# Its task is to use INDRA to construct mechanistic models of 
# biochemical systems from natural language, publications
# and databases.

from indra.pysb_assembler import PysbAssembler
from indra.statements import Agent
from indra.biopax import biopax_api
from indra.trips import trips_api
import copy

class MRA:
    def __init__(self):
        self.statements = []
        self.model = None
    
    def statement_exists(self, stmt):
        for s in self.statements:
            if stmt == s:
                return True
        return False

    def add_statements(self, stmts):
        for stmt in stmts:
            if not self.statement_exists(stmt):
                self.statements.append(stmt)

    def find_mechanism(self, source, target, force_contains):
        bp = biopax_api.process_pc_pathsfromto(source, target, neighbor_limit=1)
        return bp

    def build_model_from_text(self, model_txt):
        '''
        Build a model using INDRA from natural language.
        '''
        pa = PysbAssembler()
        tp = trips_api.process_text(model_txt)
        pa.add_statements(tp.statements)
        self.add_statements(tp.statements)
        self.model = pa.make_model()
        return self.model

    def replace_agent(self, agent_name, agent_replacement_names):
        '''
        Replace an agent in the stored statements with one or more 
        other agents. This is used, for instance, to expand a protein family
        to multiple specific proteins.
        '''
        for stmt in self.statements:
            agent = [k for k, v in stmt.__dict__.iteritems() if isinstance(v, Agent) and v.name == agent_name]
            if agent:
                self.statements.remove(stmt)
                for p in agent_replacement_names:
                    s = copy.deepcopy(stmt)
                    s.__dict__[agent[0]].name = p
                    self.add_statements([s])
        pa = PysbAssembler()
        pa.add_statements(self.statements)
        self.model = pa.make_model()
        return self.model

if __name__ == '__main__':
    mra = MRA()
    model = mra.build_model('EGF stimulation leads to the activity of HRAS')
     
    <commit_msg>Implement agent replacement for complexes in MRA<commit_after># MRA stands for mechanistic reasoning agent. 
# Its task is to use INDRA to construct mechanistic models of 
# biochemical systems from natural language, publications
# and databases.

from indra.pysb_assembler import PysbAssembler
from indra.statements import Agent, Complex
from indra.biopax import biopax_api
from indra.trips import trips_api
import copy

class MRA:
    def __init__(self):
        self.statements = []
        self.model = None
    
    def statement_exists(self, stmt):
        for s in self.statements:
            if stmt == s:
                return True
        return False

    def add_statements(self, stmts):
        for stmt in stmts:
            if not self.statement_exists(stmt):
                self.statements.append(stmt)

    def find_mechanism(self, source, target, force_contains):
        bp = biopax_api.process_pc_pathsfromto(source, target, neighbor_limit=1)
        return bp

    def build_model_from_text(self, model_txt):
        '''
        Build a model using INDRA from natural language.
        '''
        pa = PysbAssembler()
        tp = trips_api.process_text(model_txt)
        pa.add_statements(tp.statements)
        self.add_statements(tp.statements)
        self.model = pa.make_model()
        return self.model

    def replace_agent(self, agent_name, agent_replacement_names):
        '''
        Replace an agent in the stored statements with one or more 
        other agents. This is used, for instance, to expand a protein family
        to multiple specific proteins.
        '''
        for stmt in self.statements: 
            if isinstance(stmt, Complex):
                agent_key = [i for i, m in enumerate(stmt.members) if m.name == agent_name]
            else:
                agent_key = [k for k, v in stmt.__dict__.iteritems() if isinstance(v, Agent) and v.name == agent_name]
            if agent_key:
                self.statements.remove(stmt)
                for p in agent_replacement_names:
                    s = copy.deepcopy(stmt)
                    if isinstance(stmt, Complex):
                        s.members[agent_key[0]].name = p
                    else:
                        s.__dict__[agent_key[0]].name = p
                    self.add_statements([s])
        pa = PysbAssembler()
        pa.add_statements(self.statements)
        self.model = pa.make_model()
        return self.model

if __name__ == '__main__':
    mra = MRA()
    model = mra.build_model('EGF stimulation leads to the activity of HRAS')
     
    
{'fraction_non_alphanumeric': 0.07666804807293826, 'fraction_numerical': 0.0010360547036883548, 'mean_word_length': 2.1590314136125652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7667065', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1346, 'n_words': 405}