# Path: cameo/io.py
# def _apply_sanitize_rules(id, rules):
#     for rule in rules:
#         id = id.replace(*rule)
#     return id
# 
# ID_SANITIZE_RULES_TAB_COMPLETION = [('_DASH_', '_dsh_'), ('_FSLASH_', '_fsh_'), ('_BSLASH_', "_bsh_"),
#                                     ('_LPAREN_', '_lp_'), ('_LSQBKT_', '_lb_'),
#                                     ('_RSQBKT_', '_rb_'), ('_RPAREN_', '_rp_'), ('_COMMA_', '_cm_'),
#                                     ('_PERIOD_', '_prd_'), ('_APOS_', "_apo_"),
#                                     ('&amp;', '_amp_'), ('&lt;', '_lt_'), ('&gt;', '_gt_'), ('&quot;', '_qot_')]
# 
# ID_SANITIZE_RULES_SIMPHENY = [('_DASH_', '-'), ('_FSLASH_', '/'), ('_BSLASH_', "\\"), ('_LPAREN_', '('),
#                               ('_LSQBKT_', '['),
#                               ('_RSQBKT_', ']'), ('_RPAREN_', ')'), ('_COMMA_', ','), ('_PERIOD_', '.'),
#                               ('_APOS_', "'"),
#                               ('&amp;', '&'), ('&lt;', '<'), ('&gt;', '>'), ('&quot;', '"')]


import sys
import logging
import gzip
import json
import sys
import requests
    import logging
from cobra.core.formula import Formula
from cobra.io.json import save_json_model
from pandas import read_table, notnull
from cobra import Reaction, Metabolite, Model
from cameo.io import _apply_sanitize_rules, ID_SANITIZE_RULES_TAB_COMPLETION, ID_SANITIZE_RULES_SIMPHENY
        from cobra.io.json import _REQUIRED_REACTION_ATTRIBUTES
    for metabolite in model.metabolites:
        model.add_boundary(metabolite, type='demand')
    return model


def load_metanetx_files():
    """"Update metanetx data."""
    BASE_URL = 'http://www.metanetx.org/cgi-bin/mnxget/mnxref/{}.tsv'
    for filename in ['chem_prop', 'chem_xref', 'reac_prop', 'reac_xref', 'comp_prop', 'comp_xref']:
        response = requests.get(BASE_URL.format(filename))
        filepath = '../data/metanetx/{}.tsv.gz'.format(filename)
        compress_by_lines(response, filepath)


def compress_by_lines(response, filepath):
    prev_line = next(response.iter_lines())
    with gzip.open(filepath, 'wb') as f:
        for line in response.iter_lines(decode_unicode=response.encoding):
            if line.startswith('#'):
                prev_line = line
                continue
            if prev_line:
                f.write(str.encode(prev_line + '\n'))
                prev_line = None
            f.write(str.encode(line + '\n'))


def add_to_all_mapping(dataframe, mapping):
    for other_id, mnx_id in dataframe[['XREF', 'MNX_ID']].values:
        cleaned_key = _apply_sanitize_rules(

# Path: cameo/io.py
# def _apply_sanitize_rules(id, rules):
#     for rule in rules:
#         id = id.replace(*rule)
#     return id
# 
# ID_SANITIZE_RULES_TAB_COMPLETION = [('_DASH_', '_dsh_'), ('_FSLASH_', '_fsh_'), ('_BSLASH_', "_bsh_"),
#                                     ('_LPAREN_', '_lp_'), ('_LSQBKT_', '_lb_'),
#                                     ('_RSQBKT_', '_rb_'), ('_RPAREN_', '_rp_'), ('_COMMA_', '_cm_'),
#                                     ('_PERIOD_', '_prd_'), ('_APOS_', "_apo_"),
#                                     ('&amp;', '_amp_'), ('&lt;', '_lt_'), ('&gt;', '_gt_'), ('&quot;', '_qot_')]
# 
# ID_SANITIZE_RULES_SIMPHENY = [('_DASH_', '-'), ('_FSLASH_', '/'), ('_BSLASH_', "\\"), ('_LPAREN_', '('),
#                               ('_LSQBKT_', '['),
#                               ('_RSQBKT_', ']'), ('_RPAREN_', ')'), ('_COMMA_', ','), ('_PERIOD_', '.'),
#                               ('_APOS_', "'"),
#                               ('&amp;', '&'), ('&lt;', '<'), ('&gt;', '>'), ('&quot;', '"')]


# Path: scripts/parse_metanetx.py
import sys
import logging
import gzip
import json
import sys
import requests
    import logging
from cobra.core.formula import Formula
from cobra.io.json import save_json_model
from pandas import read_table, notnull
from cobra import Reaction, Metabolite, Model
from cameo.io import _apply_sanitize_rules, ID_SANITIZE_RULES_TAB_COMPLETION, ID_SANITIZE_RULES_SIMPHENY
        from cobra.io.json import _REQUIRED_REACTION_ATTRIBUTES

    for metabolite in model.metabolites:
        model.add_boundary(metabolite, type='demand')
    return model


def load_metanetx_files():
    """"Update metanetx data."""
    BASE_URL = 'http://www.metanetx.org/cgi-bin/mnxget/mnxref/{}.tsv'
    for filename in ['chem_prop', 'chem_xref', 'reac_prop', 'reac_xref', 'comp_prop', 'comp_xref']:
        response = requests.get(BASE_URL.format(filename))
        filepath = '../data/metanetx/{}.tsv.gz'.format(filename)
        compress_by_lines(response, filepath)


def compress_by_lines(response, filepath):
    prev_line = next(response.iter_lines())
    with gzip.open(filepath, 'wb') as f:
        for line in response.iter_lines(decode_unicode=response.encoding):
            if line.startswith('#'):
                prev_line = line
                continue
            if prev_line:
                f.write(str.encode(prev_line + '\n'))
                prev_line = None
            f.write(str.encode(line + '\n'))


def add_to_all_mapping(dataframe, mapping):
    for other_id, mnx_id in dataframe[['XREF', 'MNX_ID']].values:
        cleaned_key = _apply_sanitize_rules(

            _apply_sanitize_rules(other_id, REVERSE_ID_SANITIZE_RULES_SIMPHENY),