def generate_reactions(self, species_list): ''' Generates the reactions by applying all reaction rules to given species and generated species. ''' result_list = [] rules = self.reaction_rules.copy() sp_list = list(species_list) for rule in rules.itervalues(): reactions = rule.generate_reactions(sp_list) if len(reactions): result = ReactionResult(rule) for r in reactions: result.add_reaction(r) result_list.append(result) return result_list
def generate_reaction_network(self, species_list, max_iteration=1): ''' Generates the reaction network by applying all reaction rules to the species given and generated in iterations. species_list: The list of seed species. max_iteration: The maximum number of the iteration. ''' assert max_iteration > 0 # Reactions for each reaction ruls. reaction_list_map = {} for (rule_id, rule) in self.reaction_rules.iteritems(): reaction_list_map[rule_id] = [] # Initializes the list of species with given seed species. sp_list = list(species_list) # Loops until the list of species does not vary or up to the # max iteration number. for i in range(max_iteration): # The counter for new species. new_species_cnt = 0 # Finds the reaction rules and reactions. results = self.generate_reactions(sp_list) # Loops for the results. for result in results: rule = result.reaction_rule # Loops for the reactions. for reaction in result.reactions: # Checks whether created reaction exists in the list # of reactions. exists = False reaction_list = reaction_list_map[rule.id] for r in reaction_list: if r.equals(reaction): exists = True break # If the created reaction is a new one, add it # to the list of reactions. if not exists: reaction_list.append(reaction) # Loops for the reactant species. for p in reaction.products: # Checks whether created species exists # in the list of species. exists = False for sp in sp_list: if p.equals(sp): exists = True break # If the created species is a new one, add it # to the species list. if not exists: sp_list.append(p) new_species_cnt += 1 # If new species are not generated, breaks the loop. if not new_species_cnt: break # Creates the returned value. result_list = [] for (rule_id, reactions) in reaction_list_map.iteritems(): rule = self.reaction_rules[rule_id] result = ReactionResult(rule) for r in reactions: result.add_reaction(r) result_list.append(result) return result_list