# Path: cameo/network_analysis/util.py
# def distance_based_on_molecular_formula(metabolite1, metabolite2, normalize=True):
#     """Calculate the distance of two metabolites bases on the molecular formula
# 
#     Parameters
#     ----------
#     metabolite1 : Metabolite
#         The first metabolite.
#     metabolite2 : Metabolite
#         The second metabolite.
#     normalize : bool, optional
#         If the distance should be normalized by the total number of elements in both metabolites (defaults to True).
# 
#     Returns
#     -------
#     float
#         The distance between metabolite1 and metabolite2.
# 
#     """
#     if len(metabolite1.elements) == 0 or len(metabolite2.elements) == 0:
#         raise ValueError('Cannot calculate distance between metabolites %s and %s' % (metabolite1, metabolite2))
#     elements = set(list(metabolite1.elements.keys()) + list(metabolite2.elements.keys()))
#     distance = 0.
#     for element in elements:
#         distance += abs(metabolite1.elements.get(element, 0) - metabolite2.elements.get(element, 0))
#     if normalize:
#         return distance / sum(list(metabolite1.elements.values()) + list(metabolite2.elements.values()))
#     else:
#         return distance


from cameo.network_analysis import (model_to_network, reactions_to_network,
                                    remove_highly_connected_nodes)
from cameo.network_analysis.util import \
    distance_based_on_molecular_formula as dbmf
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



def test_distance_based_on_molecular_formula(salmonella):
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[0], normalize=False) == 0
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[0], normalize=True) == 0
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[1], normalize=False) == 58.0
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[1], normalize=True) == 0.6590909090909091


def test_model_to_network(salmonella):
    assert model_to_network(salmonella).edges == reactions_to_network(salmonella.reactions).edges
    network = model_to_network(salmonella)
    assert len(network.nodes) == 1761
    assert len(network.edges) == 4924
    network = model_to_network(salmonella, max_distance=1.)
    # nodes = network.nodes()
    # print(set(nodes).difference(set(core_model_one.metabolites)))
    # print(set(core_model_one.metabolites).difference(set(nodes)))
    assert len(network.nodes) == 1800

# Path: cameo/network_analysis/util.py
# def distance_based_on_molecular_formula(metabolite1, metabolite2, normalize=True):
#     """Calculate the distance of two metabolites bases on the molecular formula
# 
#     Parameters
#     ----------
#     metabolite1 : Metabolite
#         The first metabolite.
#     metabolite2 : Metabolite
#         The second metabolite.
#     normalize : bool, optional
#         If the distance should be normalized by the total number of elements in both metabolites (defaults to True).
# 
#     Returns
#     -------
#     float
#         The distance between metabolite1 and metabolite2.
# 
#     """
#     if len(metabolite1.elements) == 0 or len(metabolite2.elements) == 0:
#         raise ValueError('Cannot calculate distance between metabolites %s and %s' % (metabolite1, metabolite2))
#     elements = set(list(metabolite1.elements.keys()) + list(metabolite2.elements.keys()))
#     distance = 0.
#     for element in elements:
#         distance += abs(metabolite1.elements.get(element, 0) - metabolite2.elements.get(element, 0))
#     if normalize:
#         return distance / sum(list(metabolite1.elements.values()) + list(metabolite2.elements.values()))
#     else:
#         return distance


# Path: tests/test_network_analysis.py
from cameo.network_analysis import (model_to_network, reactions_to_network,
                                    remove_highly_connected_nodes)
from cameo.network_analysis.util import \
    distance_based_on_molecular_formula as dbmf

# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



def test_distance_based_on_molecular_formula(salmonella):
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[0], normalize=False) == 0
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[0], normalize=True) == 0
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[1], normalize=False) == 58.0
    assert dbmf(salmonella.metabolites[0], salmonella.metabolites[1], normalize=True) == 0.6590909090909091


def test_model_to_network(salmonella):
    assert model_to_network(salmonella).edges == reactions_to_network(salmonella.reactions).edges
    network = model_to_network(salmonella)
    assert len(network.nodes) == 1761
    assert len(network.edges) == 4924
    network = model_to_network(salmonella, max_distance=1.)
    # nodes = network.nodes()
    # print(set(nodes).difference(set(core_model_one.metabolites)))
    # print(set(core_model_one.metabolites).difference(set(nodes)))
    assert len(network.nodes) == 1800

    assert len(network.edges) == 12853