def reactions_to_files(model, dest, writer, split_subsystem):
def safe_file_name(origin_name):<EOL><INDENT>safe_name = re.sub(<EOL>r'<STR_LIT>', '<STR_LIT:_>', origin_name, flags=re.UNICODE)<EOL>safe_name = re.sub(<EOL>r'<STR_LIT>', '<STR_LIT:_>', safe_name.lower(), flags=re.UNICODE)<EOL>safe_name = safe_name.strip('<STR_LIT:_>')<EOL>return safe_name<EOL><DEDENT>common_reactions = []<EOL>reaction_files = []<EOL>if not split_subsystem:<EOL><INDENT>common_reactions = sorted(model.reactions, key=lambda r: r.id)<EOL>if len(common_reactions) > <NUM_LIT:0>:<EOL><INDENT>reaction_file = '<STR_LIT>'<EOL>with open(os.path.join(dest, reaction_file), '<STR_LIT:w>') as f:<EOL><INDENT>writer.write_reactions(f, common_reactions)<EOL><DEDENT>reaction_files.append(reaction_file)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>subsystems = {}<EOL>for reaction in sorted(model.reactions, key=lambda r: r.id):<EOL><INDENT>if '<STR_LIT>' in reaction.properties:<EOL><INDENT>subsystem_file = safe_file_name(<EOL>reaction.properties['<STR_LIT>'])<EOL>subsystems.setdefault(subsystem_file, []).append(reaction)<EOL><DEDENT>else:<EOL><INDENT>common_reactions.append(reaction)<EOL><DEDENT><DEDENT>subsystem_folder = '<STR_LIT>'<EOL>sub_existance = False<EOL>for subsystem_file, reactions in iteritems(subsystems):<EOL><INDENT>if len(reactions) < _MAX_REACTION_COUNT:<EOL><INDENT>for reaction in reactions:<EOL><INDENT>common_reactions.append(reaction)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if len(reactions) > <NUM_LIT:0>:<EOL><INDENT>mkdir_p(os.path.join(dest, subsystem_folder))<EOL>subsystem_file = os.path.join(<EOL>subsystem_folder, '<STR_LIT>'.format(subsystem_file))<EOL>with open(os.path.join(dest, subsystem_file), '<STR_LIT:w>') as f:<EOL><INDENT>writer.write_reactions(f, reactions)<EOL><DEDENT>reaction_files.append(subsystem_file)<EOL>sub_existance = True<EOL><DEDENT><DEDENT><DEDENT>reaction_files.sort()<EOL>if sub_existance:<EOL><INDENT>reaction_file = os.path.join(<EOL>subsystem_folder, '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>reaction_file = '<STR_LIT>'<EOL><DEDENT>if len(common_reactions) > <NUM_LIT:0>:<EOL><INDENT>with open(os.path.join(dest, reaction_file), '<STR_LIT:w>') as f:<EOL><INDENT>writer.write_reactions(f, common_reactions)<EOL><DEDENT>reaction_files.append(reaction_file)<EOL><DEDENT><DEDENT>return reaction_files<EOL>
Turn the reaction subsystems into their own files.

    If a subsystem has a number of reactions over the threshold, it gets its
    own YAML file. All other reactions, those that don't have a subsystem or
    are in a subsystem that falls below the threshold, get added to a common
    reaction file.

    Args:
        model: :class:`psamm_import.model.MetabolicModel`.
        dest: output path for model files.
        writer: :class:`psamm.datasource.native.ModelWriter`.
        split_subsystem: Divide reactions into multiple files by subsystem.