# Path: kleborate/__main__.py
# def get_chromosome_mlst_results(data_folder, contigs, kp_complex, args):
#     chromosome_mlst_header = get_chromosome_mlst_header()
# 
#     if kp_complex:
#         seqs = data_folder + '/Klebsiella_pneumoniae.fasta'
#         database = data_folder + '/kpneumoniae.txt'
#         chr_st, chr_st_detail, _, _ = \
#             mlst_blast(seqs, database, 'no', [contigs], min_cov=args.min_coverage,
#                        min_ident=args.min_identity, max_missing=3, allow_multiple=False)
#         if chr_st != '0':
#             chr_st = 'ST' + chr_st
#         chr_st_with_subsp = get_kp_subspecies_based_on_st(chr_st)
# 
#         assert len(chromosome_mlst_header) == len(chr_st_detail)
# 
#         results = {'ST': chr_st_with_subsp,
#                    'Chr_ST': chr_st}
# 
#     else:
#         results = {'ST': "NA",
#                    'Chr_ST': "NA"}
#         chr_st_detail = ['-'] * len(chromosome_mlst_header)
# 
#     results.update(dict(zip(get_chromosome_mlst_header(), chr_st_detail)))
#     return results
# 
# def gunzip_contigs_if_necessary(contigs, temp_dir):
#     if get_compression_type(contigs) == 'gz':
#         name = get_strain_name(contigs)
#         new_contigs = temp_dir + '/' + name + '.fasta'
#         decompress_file(contigs, new_contigs)
#         return new_contigs
#     else:
#         return contigs
# 
# def get_kp_subspecies_based_on_st(chr_st):
#     ozaenae_sts = {'ST90', 'ST91', 'ST92', 'ST93', 'ST95', 'ST96', 'ST97', 'ST381', 'ST777',
#                    'ST3193', 'ST3766', 'ST3768', 'ST3771', 'ST3781', 'ST3782', 'ST3784', 'ST3802',
#                    'ST3803'}
#     rhinoscleromatis_sts = {'ST67', 'ST68', 'ST69', 'ST3772', 'ST3819'}
#     chr_st_minus_1lv = chr_st.replace('-1LV', '')  # 1LV still results in a subspecies call
#     if chr_st_minus_1lv in ozaenae_sts:
#         return chr_st + ' (subsp. ozaenae)'
#     if chr_st_minus_1lv in rhinoscleromatis_sts:
#         return chr_st + ' (subsp. rhinoscleromatis)'
#     return chr_st


import collections
import tempfile
import unittest
from kleborate.__main__ import get_chromosome_mlst_results, gunzip_contigs_if_necessary, \
    get_kp_subspecies_based_on_st
            contigs = gunzip_contigs_if_necessary(contigs, tmp_dir)
            results = get_chromosome_mlst_results(self.data_dir, contigs, True, self.args)
            self.assertEqual(results['gapA'], '2')
            self.assertEqual(results['infB'], '1')
            self.assertEqual(results['mdh'], '1')
            self.assertEqual(results['pgi'], '1')
            self.assertEqual(results['phoE'], '9')
            self.assertEqual(results['rpoB'], '4')
            self.assertEqual(results['tonB'], '12')
            self.assertEqual(results['ST'], 'ST23')
            self.assertEqual(results['Chr_ST'], 'ST23')

    def test_ozaenae(self):
        contigs = 'test/test_mlst/GCF_900451425.1.fna.gz'
        with tempfile.TemporaryDirectory() as tmp_dir:
            contigs = gunzip_contigs_if_necessary(contigs, tmp_dir)
            results = get_chromosome_mlst_results(self.data_dir, contigs, True, self.args)
            self.assertEqual(results['ST'], 'ST90 (subsp. ozaenae)')
            self.assertEqual(results['Chr_ST'], 'ST90')

    def test_rhinoscleromatis(self):
        contigs = 'test/test_mlst/GCF_000163455.1.fna.gz'
        with tempfile.TemporaryDirectory() as tmp_dir:
            contigs = gunzip_contigs_if_necessary(contigs, tmp_dir)
            results = get_chromosome_mlst_results(self.data_dir, contigs, True, self.args)
            self.assertEqual(results['ST'], 'ST67 (subsp. rhinoscleromatis)')
            self.assertEqual(results['Chr_ST'], 'ST67')

    def test_kp_subspecies_1(self):
        # Just test some non-ozaenae non-rhinoscleromatis STs.

# Path: kleborate/__main__.py
# def get_chromosome_mlst_results(data_folder, contigs, kp_complex, args):
#     chromosome_mlst_header = get_chromosome_mlst_header()
# 
#     if kp_complex:
#         seqs = data_folder + '/Klebsiella_pneumoniae.fasta'
#         database = data_folder + '/kpneumoniae.txt'
#         chr_st, chr_st_detail, _, _ = \
#             mlst_blast(seqs, database, 'no', [contigs], min_cov=args.min_coverage,
#                        min_ident=args.min_identity, max_missing=3, allow_multiple=False)
#         if chr_st != '0':
#             chr_st = 'ST' + chr_st
#         chr_st_with_subsp = get_kp_subspecies_based_on_st(chr_st)
# 
#         assert len(chromosome_mlst_header) == len(chr_st_detail)
# 
#         results = {'ST': chr_st_with_subsp,
#                    'Chr_ST': chr_st}
# 
#     else:
#         results = {'ST': "NA",
#                    'Chr_ST': "NA"}
#         chr_st_detail = ['-'] * len(chromosome_mlst_header)
# 
#     results.update(dict(zip(get_chromosome_mlst_header(), chr_st_detail)))
#     return results
# 
# def gunzip_contigs_if_necessary(contigs, temp_dir):
#     if get_compression_type(contigs) == 'gz':
#         name = get_strain_name(contigs)
#         new_contigs = temp_dir + '/' + name + '.fasta'
#         decompress_file(contigs, new_contigs)
#         return new_contigs
#     else:
#         return contigs
# 
# def get_kp_subspecies_based_on_st(chr_st):
#     ozaenae_sts = {'ST90', 'ST91', 'ST92', 'ST93', 'ST95', 'ST96', 'ST97', 'ST381', 'ST777',
#                    'ST3193', 'ST3766', 'ST3768', 'ST3771', 'ST3781', 'ST3782', 'ST3784', 'ST3802',
#                    'ST3803'}
#     rhinoscleromatis_sts = {'ST67', 'ST68', 'ST69', 'ST3772', 'ST3819'}
#     chr_st_minus_1lv = chr_st.replace('-1LV', '')  # 1LV still results in a subspecies call
#     if chr_st_minus_1lv in ozaenae_sts:
#         return chr_st + ' (subsp. ozaenae)'
#     if chr_st_minus_1lv in rhinoscleromatis_sts:
#         return chr_st + ' (subsp. rhinoscleromatis)'
#     return chr_st


# Path: test/test_mlst.py
import collections
import tempfile
import unittest
from kleborate.__main__ import get_chromosome_mlst_results, gunzip_contigs_if_necessary, \
    get_kp_subspecies_based_on_st

            contigs = gunzip_contigs_if_necessary(contigs, tmp_dir)
            results = get_chromosome_mlst_results(self.data_dir, contigs, True, self.args)
            self.assertEqual(results['gapA'], '2')
            self.assertEqual(results['infB'], '1')
            self.assertEqual(results['mdh'], '1')
            self.assertEqual(results['pgi'], '1')
            self.assertEqual(results['phoE'], '9')
            self.assertEqual(results['rpoB'], '4')
            self.assertEqual(results['tonB'], '12')
            self.assertEqual(results['ST'], 'ST23')
            self.assertEqual(results['Chr_ST'], 'ST23')

    def test_ozaenae(self):
        contigs = 'test/test_mlst/GCF_900451425.1.fna.gz'
        with tempfile.TemporaryDirectory() as tmp_dir:
            contigs = gunzip_contigs_if_necessary(contigs, tmp_dir)
            results = get_chromosome_mlst_results(self.data_dir, contigs, True, self.args)
            self.assertEqual(results['ST'], 'ST90 (subsp. ozaenae)')
            self.assertEqual(results['Chr_ST'], 'ST90')

    def test_rhinoscleromatis(self):
        contigs = 'test/test_mlst/GCF_000163455.1.fna.gz'
        with tempfile.TemporaryDirectory() as tmp_dir:
            contigs = gunzip_contigs_if_necessary(contigs, tmp_dir)
            results = get_chromosome_mlst_results(self.data_dir, contigs, True, self.args)
            self.assertEqual(results['ST'], 'ST67 (subsp. rhinoscleromatis)')
            self.assertEqual(results['Chr_ST'], 'ST67')

    def test_kp_subspecies_1(self):
        # Just test some non-ozaenae non-rhinoscleromatis STs.

        self.assertEqual(get_kp_subspecies_based_on_st('ST1'), 'ST1')