# Path: pyPaSWAS/Core/SWSeqRecord.py
# class SWSeqRecord(SeqRecord):
#     '''
#     TODO: Docstring
#     '''
# 
#     start_position = 0
#     distance = 0
#     original_length = 0
# 
#     def __init__(self, seq, identifier, start_position=0, original_length = None, distance = 0, refID = None):
#         super(SWSeqRecord, self).__init__(seq.upper(), identifier)
#         self.start_position = start_position
#         if original_length ==None:
#             self.original_length = len(seq)
#         else:
#             self.original_length = original_length
#         self.distance = distance 
#         self.refID = refID
# 
#     def _compare(self, other):
#         '''
#         TODO: Docstring
#         :param other:
#         '''
#         self.distance = math.sqrt((self.count[0] - other.count[0]) ** 2 + (self.count[1] - other.count[1]) ** 2 +
#                                   (self.count[2] - other.count[2]) ** 2 + (self.count[3] - other.count[3]) ** 2 +
#                                   (self.count[4] - other.count[4]) ** 2)
#         other.distance = self.distance
#         return self.distance
# 
# Path: pyPaSWAS/Core/Exceptions.py
# class InvalidOptionException(Exception):
#     '''
#     This class is used to give a consistent user interface to exceptions that are specific for pyPaSWAS.
#     '''
# 
# Path: pyPaSWAS/Core/Exceptions.py
# class ReaderException(Exception):
#     ''' 
#     This class is used to indicate no sequences left in file.
#     '''


import os.path
from itertools import islice
from Bio import  SeqIO
from Bio.Seq import Seq
from pyPaSWAS.Core.SWSeqRecord import SWSeqRecord
from pyPaSWAS.Core.Exceptions import InvalidOptionException
from pyPaSWAS.Core.Exceptions import ReaderException
        #self.logger.debug('Creating complement sequences...')
        seqIO = lambda seqIO: SWSeqRecord(Seq(str(seqIO.seq.reverse_complement()), seqIO.seq.alphabet),
                                          identifier=(str(seqIO.id) + self.rc_string))
        self.records.extend([seqIO(record) for record in self.records])

    def complement_records_only(self):
        '''Creates the reverse complements to the parsed records '''
        seqIO = lambda seqIO: SWSeqRecord(Seq(str(seqIO.seq.reverse_complement()), seqIO.seq.alphabet),
                                          identifier=(str(seqIO.id) + self.rc_string))
        self.records = [seqIO(record) for record in self.records]

    def reverse_records(self):
        '''Appends the reverse complements to the parsed records '''
        #self.logger.debug('Creating complement sequences...')
        seqIO = lambda seqIO: SWSeqRecord(Seq(str(seqIO.seq[::-1]), seqIO.seq.alphabet),
                                          identifier=(str(seqIO.id) + self.rc_string))
        self.records.extend([seqIO(record) for record in self.records])



class BioPythonReader(Reader):
    '''This class parses input files'''

    def read_records(self, start=0, end=None):
        '''Parses the records from a supported input file'''
        self.logger.debug('Reading from {} file...'.format(self.filetype))
        file_elements = open(self.path, "rU")
        self.records = list(islice(SeqIO.parse(file_elements, self.filetype), start, end))
        file_elements.close()
        if len(self.records) == 0:

# Path: pyPaSWAS/Core/SWSeqRecord.py
# class SWSeqRecord(SeqRecord):
#     '''
#     TODO: Docstring
#     '''
# 
#     start_position = 0
#     distance = 0
#     original_length = 0
# 
#     def __init__(self, seq, identifier, start_position=0, original_length = None, distance = 0, refID = None):
#         super(SWSeqRecord, self).__init__(seq.upper(), identifier)
#         self.start_position = start_position
#         if original_length ==None:
#             self.original_length = len(seq)
#         else:
#             self.original_length = original_length
#         self.distance = distance 
#         self.refID = refID
# 
#     def _compare(self, other):
#         '''
#         TODO: Docstring
#         :param other:
#         '''
#         self.distance = math.sqrt((self.count[0] - other.count[0]) ** 2 + (self.count[1] - other.count[1]) ** 2 +
#                                   (self.count[2] - other.count[2]) ** 2 + (self.count[3] - other.count[3]) ** 2 +
#                                   (self.count[4] - other.count[4]) ** 2)
#         other.distance = self.distance
#         return self.distance
# 
# Path: pyPaSWAS/Core/Exceptions.py
# class InvalidOptionException(Exception):
#     '''
#     This class is used to give a consistent user interface to exceptions that are specific for pyPaSWAS.
#     '''
# 
# Path: pyPaSWAS/Core/Exceptions.py
# class ReaderException(Exception):
#     ''' 
#     This class is used to indicate no sequences left in file.
#     '''


# Path: pyPaSWAS/Core/Readers.py
import os.path
from itertools import islice
from Bio import  SeqIO
from Bio.Seq import Seq
from pyPaSWAS.Core.SWSeqRecord import SWSeqRecord
from pyPaSWAS.Core.Exceptions import InvalidOptionException
from pyPaSWAS.Core.Exceptions import ReaderException

        #self.logger.debug('Creating complement sequences...')
        seqIO = lambda seqIO: SWSeqRecord(Seq(str(seqIO.seq.reverse_complement()), seqIO.seq.alphabet),
                                          identifier=(str(seqIO.id) + self.rc_string))
        self.records.extend([seqIO(record) for record in self.records])

    def complement_records_only(self):
        '''Creates the reverse complements to the parsed records '''
        seqIO = lambda seqIO: SWSeqRecord(Seq(str(seqIO.seq.reverse_complement()), seqIO.seq.alphabet),
                                          identifier=(str(seqIO.id) + self.rc_string))
        self.records = [seqIO(record) for record in self.records]

    def reverse_records(self):
        '''Appends the reverse complements to the parsed records '''
        #self.logger.debug('Creating complement sequences...')
        seqIO = lambda seqIO: SWSeqRecord(Seq(str(seqIO.seq[::-1]), seqIO.seq.alphabet),
                                          identifier=(str(seqIO.id) + self.rc_string))
        self.records.extend([seqIO(record) for record in self.records])



class BioPythonReader(Reader):
    '''This class parses input files'''

    def read_records(self, start=0, end=None):
        '''Parses the records from a supported input file'''
        self.logger.debug('Reading from {} file...'.format(self.filetype))
        file_elements = open(self.path, "rU")
        self.records = list(islice(SeqIO.parse(file_elements, self.filetype), start, end))
        file_elements.close()
        if len(self.records) == 0:

            raise ReaderException('No (more) sequence data found in input file ({}), of file type {}.'.format(self.path, self.filetype))