# Path: scripts/trim_to_coding.py
# def get_regions(ref_seq):
# 
#     """
#     ref_seq is a string containing gaps '-'
#     This function returns a list of lists (legnth two)
#     with the start and stop (python) of the non gapped regions.
#     """
#     gap=True
#     gene = []
#     region = []
#     for i in range(0,len(ref_seq)):
#         if ref_seq[i]!='-' and gap==True:
#             region.append(i)
#             if i ==len(ref_seq)-1: # There is a trailing base at the end
#                 region.append(i+1)
#                 gene.append(region)
#             gap=False
# 
#         elif ref_seq[i]=='-' and gap ==False:
#             region.append(i)
#             gene.append(region)
#             region=[]
#             gap=True 
#             #print "setting gap to true"   
#         elif ref_seq[i]!='-' and gap==False and i ==len(ref_seq)-1: # The last character is not a gap and should be included in this frame
#             region.append(i+1)
#             gene.append(region)
#     return gene
# 
# def trim_sequences(aligned_header_seqs):
#     """
#     The first sequence in this alignment is taken to correspond to the reference sequence.
#     The returned variable is a list similar to aligned_headers_seqs, but with
#     all positions corresponding to gaps in this reference sequence stripped away.
#     In the sample sequence, every character at
#     the same position as a gap in the reference sequence is removed. The headers are
#     unchanged.  The order of sequences in this stripped alignment is also
#     unchanged.
#     """
#     
#     if not (isinstance(aligned_header_seqs, list) and len(aligned_header_seqs) >= 2):
#         raise ValueError, "Input does not specify at least two aligned sequences."
#     ref_seq = aligned_header_seqs[0]# str yields the sequence the reference here is shorter so we count the '-' in this sequence.
# 
#     regions= get_regions(ref_seq)
# 
# 
#     samp_seq=aligned_header_seqs[1]
#    
#     sample_sequence = trim_to_regions(samp_seq,regions)
#     return([sample_sequence,regions]) 


import unittest
from scripts.trim_to_coding import get_regions, trim_sequences


class test_loci(unittest.TestCase):
    def setUp(self):
        pass

    def test_trimming_ends(self):
        solution = ["AAAAA",[[2,7]]]
        seqs = ["--AAAAA--","TTAAAAATT"]#TTAATTAAA-AGC"
        output = trim_sequences(seqs)
        self.assertEqual(output,solution)
    
    def test_trimming_middles(self):
        solution = ["AAAAAAA",[[2,5],[6,8],[9,11]]]
        seqs = ["--AAA-AA-AA-","TTAAATAATAAT"]#TTAATTAAA-AGC"
        output = trim_sequences(seqs)
        self.assertEqual(output,solution)

    def test_trimming_middles_noend(self):
        solution = ["AAAAAAA",[[2,5],[6,8],[9,11]]]
        seqs = ["--AAA-AA-AA","TTAAATAATAA"]#TTAATTAAA-AGC"

# Path: scripts/trim_to_coding.py
# def get_regions(ref_seq):
# 
#     """
#     ref_seq is a string containing gaps '-'
#     This function returns a list of lists (legnth two)
#     with the start and stop (python) of the non gapped regions.
#     """
#     gap=True
#     gene = []
#     region = []
#     for i in range(0,len(ref_seq)):
#         if ref_seq[i]!='-' and gap==True:
#             region.append(i)
#             if i ==len(ref_seq)-1: # There is a trailing base at the end
#                 region.append(i+1)
#                 gene.append(region)
#             gap=False
# 
#         elif ref_seq[i]=='-' and gap ==False:
#             region.append(i)
#             gene.append(region)
#             region=[]
#             gap=True 
#             #print "setting gap to true"   
#         elif ref_seq[i]!='-' and gap==False and i ==len(ref_seq)-1: # The last character is not a gap and should be included in this frame
#             region.append(i+1)
#             gene.append(region)
#     return gene
# 
# def trim_sequences(aligned_header_seqs):
#     """
#     The first sequence in this alignment is taken to correspond to the reference sequence.
#     The returned variable is a list similar to aligned_headers_seqs, but with
#     all positions corresponding to gaps in this reference sequence stripped away.
#     In the sample sequence, every character at
#     the same position as a gap in the reference sequence is removed. The headers are
#     unchanged.  The order of sequences in this stripped alignment is also
#     unchanged.
#     """
#     
#     if not (isinstance(aligned_header_seqs, list) and len(aligned_header_seqs) >= 2):
#         raise ValueError, "Input does not specify at least two aligned sequences."
#     ref_seq = aligned_header_seqs[0]# str yields the sequence the reference here is shorter so we count the '-' in this sequence.
# 
#     regions= get_regions(ref_seq)
# 
# 
#     samp_seq=aligned_header_seqs[1]
#    
#     sample_sequence = trim_to_regions(samp_seq,regions)
#     return([sample_sequence,regions]) 


# Path: test/trim_to_coding_test.py
import unittest
from scripts.trim_to_coding import get_regions, trim_sequences



class test_loci(unittest.TestCase):
    def setUp(self):
        pass

    def test_trimming_ends(self):
        solution = ["AAAAA",[[2,7]]]
        seqs = ["--AAAAA--","TTAAAAATT"]#TTAATTAAA-AGC"
        output = trim_sequences(seqs)
        self.assertEqual(output,solution)
    
    def test_trimming_middles(self):
        solution = ["AAAAAAA",[[2,5],[6,8],[9,11]]]
        seqs = ["--AAA-AA-AA-","TTAAATAATAAT"]#TTAATTAAA-AGC"
        output = trim_sequences(seqs)
        self.assertEqual(output,solution)

    def test_trimming_middles_noend(self):
        solution = ["AAAAAAA",[[2,5],[6,8],[9,11]]]
        seqs = ["--AAA-AA-AA","TTAAATAATAA"]#TTAATTAAA-AGC"

        output = trim_sequences(seqs)