import pytest
import hashlib
from utils.search_result_parsing import ProteomicSearchResultParser


def md5sum(filename):
    data = open(filename, 'rb').read()
    return hashlib.md5(data).hexdigest()


test_data = [('tests/integration_tests/mztab_parser/job/pd_mztab_example.mztab',
              'pd_example_protein_groups.yaml',
              'tests/integration_tests/mztab_parser/expected_results/correct_protein_groups.yaml',
              r'REV_$'),
             ('tests/resources/test_parse_mztab/job_dir/maxquant_mzTab_example.mztab',
              'maxquant_pg.yaml',
              'tests/resources/test_parse_mztab/expected-results/protein_groups.yaml',
              '^REV_|^CON_')]
style = 'styles/mztab_1_0_0.yaml'
required_sample_descriptors = []


@pytest.mark.parametrize("mztab, output_yaml, correct_pg_yaml, decoy_regex", test_data)
def test_pg_yaml_from_mztab(tmpdir, mztab, output_yaml, correct_pg_yaml, decoy_regex):
    test_result = tmpdir / output_yaml
    protein_parser = ProteomicSearchResultParser(mztab, style, test_result, decoy_regex,
                                                 required_sample_descriptors)
    protein_parser.write_protein_groups_yaml()
    assert md5sum(test_result) == md5sum(correct_pg_yaml)
