# Path: graftm/hmmsearcher.py
# class NoInputSequencesException(Exception):
#     def __init__(self, command):
#         """Instantiate with the command used that went amiss"""
#         self.command = command


import unittest
import tempfile
import os
import re
import sys
import graftm.hmmsearcher
from graftm.hmmsearcher import NoInputSequencesException
        queue = [['hmm1','out1'],['hmm2','out2']]
        pairs_to_run = searcher._munch_off_batch(queue)
        self.assertEqual([[['hmm1','out1'],3], [['hmm2','out2'],2]], pairs_to_run)

    def test_actually_runs(self):
        searcher = graftm.hmmsearcher.HmmSearcher(5)
        faa_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA_1.1.faa')
        hmm_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA.hmm')
        with tempfile.NamedTemporaryFile(suffix='.fa') as output:
            searcher.hmmsearch('cat %s' % faa_file,
                               [hmm_file],
                               [output.name])
            expected = '''#                                                                             --- full sequence --- -------------- this domain -------------   hmm coord   ali coord   env coord
# target name         accession   tlen query name           accession   qlen   E-value  score  bias   #  of  c-Evalue  i-Evalue  score  bias  from    to  from    to  from    to  acc description of target
# ------------------- ---------- ----- -------------------- ---------- ----- --------- ------ ----- --- --- --------- --------- ------ ----- ----- ----- ----- ----- ----- ----- ---- ---------------------
example_partial_mcra8 -            162 mcrA.fasta           -            557     2e-88  286.6   5.0   1   1   2.5e-89   2.2e-88  286.4   5.0   332   487     1   162     1   162 0.99 -
#'''.split('\n')
            observed = re.sub(r'\t', ' ', open(output.name).read()).split("\n")
            self.assertEqual(expected, observed[:5])

    def test_nhmmer_searcher(self):
        searcher = graftm.hmmsearcher.NhmmerSearcher(20)
        cmd = searcher._hmm_command('cat some', [(['hmm1','out1'],10)])
        self.assertEqual('cat some | nhmmer  --cpu 10 -o /dev/null --noali --tblout out1 hmm1 -', cmd)

    def test_no_input_exception(self):
        searcher = graftm.hmmsearcher.HmmSearcher(2)
        fna_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA_1.1.fna')
        hmm_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA.hmm')
        with tempfile.NamedTemporaryFile(suffix='.fa') as output:

# Path: graftm/hmmsearcher.py
# class NoInputSequencesException(Exception):
#     def __init__(self, command):
#         """Instantiate with the command used that went amiss"""
#         self.command = command


# Path: test/test_hmmsearcher.py
import unittest
import tempfile
import os
import re
import sys
import graftm.hmmsearcher
from graftm.hmmsearcher import NoInputSequencesException

        queue = [['hmm1','out1'],['hmm2','out2']]
        pairs_to_run = searcher._munch_off_batch(queue)
        self.assertEqual([[['hmm1','out1'],3], [['hmm2','out2'],2]], pairs_to_run)

    def test_actually_runs(self):
        searcher = graftm.hmmsearcher.HmmSearcher(5)
        faa_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA_1.1.faa')
        hmm_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA.hmm')
        with tempfile.NamedTemporaryFile(suffix='.fa') as output:
            searcher.hmmsearch('cat %s' % faa_file,
                               [hmm_file],
                               [output.name])
            expected = '''#                                                                             --- full sequence --- -------------- this domain -------------   hmm coord   ali coord   env coord
# target name         accession   tlen query name           accession   qlen   E-value  score  bias   #  of  c-Evalue  i-Evalue  score  bias  from    to  from    to  from    to  acc description of target
# ------------------- ---------- ----- -------------------- ---------- ----- --------- ------ ----- --- --- --------- --------- ------ ----- ----- ----- ----- ----- ----- ----- ---- ---------------------
example_partial_mcra8 -            162 mcrA.fasta           -            557     2e-88  286.6   5.0   1   1   2.5e-89   2.2e-88  286.4   5.0   332   487     1   162     1   162 0.99 -
#'''.split('\n')
            observed = re.sub(r'\t', ' ', open(output.name).read()).split("\n")
            self.assertEqual(expected, observed[:5])

    def test_nhmmer_searcher(self):
        searcher = graftm.hmmsearcher.NhmmerSearcher(20)
        cmd = searcher._hmm_command('cat some', [(['hmm1','out1'],10)])
        self.assertEqual('cat some | nhmmer  --cpu 10 -o /dev/null --noali --tblout out1 hmm1 -', cmd)

    def test_no_input_exception(self):
        searcher = graftm.hmmsearcher.HmmSearcher(2)
        fna_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA_1.1.fna')
        hmm_file = os.path.join(self.path_to_data, 'mcrA.gpkg/mcrA.hmm')
        with tempfile.NamedTemporaryFile(suffix='.fa') as output:

            with self.assertRaises(NoInputSequencesException):