# Path: graftm/sequence_extractor.py
# class SequenceExtractor:
#     def extract(self, reads_to_extract, database_fasta_file, output_file):
#         '''Extract the reads_to_extract from the database_fasta_file and put them in
#         output_file.
# 
#         Parameters
#         ----------
#         reads_to_extract: Iterable of str
#             IDs of reads to be extracted
#         database_fasta_file: str
#             path the fasta file that containing the reads
#         output_file: str
#             path to the file where they are put
# 
#         Returns
#         -------
#         Nothing'''
#         cmd = "mfqe --fasta-read-name-lists /dev/stdin --input-fasta '{}' --output-fasta-files '{}' --output-uncompressed".format(
#             database_fasta_file, output_file)
#         extern.run(cmd, stdin='\n'.join(reads_to_extract))
# 
#     def extract_forward_and_reverse_complement(
#             self, forward_reads_to_extract, reverse_reads_to_extract, database_fasta_file,
#             output_file):
#         '''As per extract except also reverse complement the sequences.'''
#         self.extract(forward_reads_to_extract, database_fasta_file, output_file)
#         cmd_rev = "mfqe --fasta-read-name-lists /dev/stdin --input-fasta '{}' --output-fasta-files /dev/stdout --output-uncompressed".format(
#             database_fasta_file)
# 
#         output = extern.run(cmd_rev, stdin='\n'.join(reverse_reads_to_extract))
# 
#         with open(output_file, 'a') as f:
#             for record in SeqIO.parse(StringIO(output), 'fasta'):
#                 record.seq = record.reverse_complement().seq
#                 SeqIO.write(record, f, 'fasta')


import sys, os, unittest, logging, tempfile
from graftm.sequence_extractor import SequenceExtractor
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License.
# If not, see <http://www.gnu.org/licenses/>.
#=======================================================================


sys.path = [os.path.join(os.path.dirname(os.path.realpath(__file__)),'..')]+sys.path


class Tests(unittest.TestCase):
    def test_extract(self):
        fasta = '''>1
ATG
>2 comment
AAAAA
'''
        with tempfile.NamedTemporaryFile() as f:
            f.write(fasta.encode())
            f.flush()
            with tempfile.NamedTemporaryFile() as g:

# Path: graftm/sequence_extractor.py
# class SequenceExtractor:
#     def extract(self, reads_to_extract, database_fasta_file, output_file):
#         '''Extract the reads_to_extract from the database_fasta_file and put them in
#         output_file.
# 
#         Parameters
#         ----------
#         reads_to_extract: Iterable of str
#             IDs of reads to be extracted
#         database_fasta_file: str
#             path the fasta file that containing the reads
#         output_file: str
#             path to the file where they are put
# 
#         Returns
#         -------
#         Nothing'''
#         cmd = "mfqe --fasta-read-name-lists /dev/stdin --input-fasta '{}' --output-fasta-files '{}' --output-uncompressed".format(
#             database_fasta_file, output_file)
#         extern.run(cmd, stdin='\n'.join(reads_to_extract))
# 
#     def extract_forward_and_reverse_complement(
#             self, forward_reads_to_extract, reverse_reads_to_extract, database_fasta_file,
#             output_file):
#         '''As per extract except also reverse complement the sequences.'''
#         self.extract(forward_reads_to_extract, database_fasta_file, output_file)
#         cmd_rev = "mfqe --fasta-read-name-lists /dev/stdin --input-fasta '{}' --output-fasta-files /dev/stdout --output-uncompressed".format(
#             database_fasta_file)
# 
#         output = extern.run(cmd_rev, stdin='\n'.join(reverse_reads_to_extract))
# 
#         with open(output_file, 'a') as f:
#             for record in SeqIO.parse(StringIO(output), 'fasta'):
#                 record.seq = record.reverse_complement().seq
#                 SeqIO.write(record, f, 'fasta')


# Path: test/test_sequence_extractor.py
import sys, os, unittest, logging, tempfile
from graftm.sequence_extractor import SequenceExtractor

#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License.
# If not, see <http://www.gnu.org/licenses/>.
#=======================================================================


sys.path = [os.path.join(os.path.dirname(os.path.realpath(__file__)),'..')]+sys.path


class Tests(unittest.TestCase):
    def test_extract(self):
        fasta = '''>1
ATG
>2 comment
AAAAA
'''
        with tempfile.NamedTemporaryFile() as f:
            f.write(fasta.encode())
            f.flush()
            with tempfile.NamedTemporaryFile() as g:

                SequenceExtractor().extract(['1'], f.name, g.name)