# Path: crumbs/utils/bin_utils.py
# BIN_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..',
#                                        'bin'))
# 
# Path: crumbs/seq/seq.py
# def assing_kind_to_seqs(kind, seqs, file_format):
#     'It puts each seq into a NamedTuple named Seq'
#     return (SeqWrapper(kind, seq, file_format) for seq in seqs)
# 
# def get_str_seq(seq):
#     seq_class = seq.kind
#     if seq_class == SEQITEM:
#         seq = seq.object.lines[1].strip()
#     elif seq_class == SEQRECORD:
#         seq = str(seq.object.seq)
#     return seq.strip()
# 
# Path: crumbs/seq/utils/seq_utils.py
# def uppercase_length(string):
#     'It returns the number of uppercase characters found in the string'
#     return len(re.findall("[A-Z]", string))
# 
# class ChangeCase(object):
#     'It changes the sequence case.'
# 
#     def __init__(self, action):
#         'The initiator'
#         if action not in (UPPERCASE, LOWERCASE, SWAPCASE):
#             msg = 'Action should be: uppercase, lowercase or invertcase'
#             raise ValueError(msg)
#         self.action = action
# 
#     def __call__(self, seqs):
#         'It changes the case of the seqrecords.'
#         action = self.action
#         processed_seqs = []
#         for seq in seqs:
#             str_seq = get_str_seq(seq)
#             if action == UPPERCASE:
#                 str_seq = str_seq.upper()
#             elif action == LOWERCASE:
#                 str_seq = str_seq.lower()
#             elif action == SWAPCASE:
#                 str_seq = str_seq.swapcase()
#             else:
#                 raise NotImplementedError()
#             seq = copy_seq(seq, seq=str_seq)
#             processed_seqs.append(seq)
#         return processed_seqs
# 
# def get_uppercase_segments(string):
#     '''It detects the unmasked regions of a sequence
# 
#     It returns a list of (start, end) tuples'''
#     start = 0
#     for is_upper, group in itertools.groupby(string, lambda x: x.isupper()):
#         group = list(group)
#         end = start + len(group) - 1
#         if is_upper:
#             yield start, end
#         start = end + 1
# 
# Path: crumbs/utils/tags.py
# SWAPCASE = 'swap'
# 
# UPPERCASE = 'upper'
# 
# LOWERCASE = 'lower'
# 
# SEQRECORD = 'seqrecord'


import unittest
import os.path
from cStringIO import StringIO
from tempfile import NamedTemporaryFile
from subprocess import check_output
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from crumbs.utils.bin_utils import BIN_DIR
from crumbs.seq.seq import assing_kind_to_seqs, get_str_seq
from crumbs.seq.utils.seq_utils import (uppercase_length, ChangeCase,
                                        get_uppercase_segments)
from crumbs.utils.tags import SWAPCASE, UPPERCASE, LOWERCASE, SEQRECORD
    return fhand


class MaskedSegmentsTest(unittest.TestCase):
    'It tests the lower case segments location functions'

    @staticmethod
    def test_masked_locations():
        'It test the masked locations function'

        assert list(get_uppercase_segments('aaATTTTTTaa')) == [(2, 8)]

        assert list(get_uppercase_segments('aaATTTaTTaa')) == [(2, 5), (7, 8)]

        assert list(get_uppercase_segments('AAATaaa')) == [(0, 3)]

        assert list(get_uppercase_segments('aaaaAAAA')) == [(4, 7)]

        seq = 'AATTaaTTaaTTT'
        assert list(get_uppercase_segments(seq)) == [(0, 3), (6, 7), (10, 12)]

        assert list(get_uppercase_segments('AATT')) == [(0, 3)]
        assert not list(get_uppercase_segments('aatt'))


class ChangeCaseTest(unittest.TestCase):
    'It tests the case change'
    def test_case_change(self):
        'It changes the case of the sequences'
        seqs = [SeqRecord(Seq('aCCg'), letter_annotations={'dummy': 'dddd'})]

# Path: crumbs/utils/bin_utils.py
# BIN_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..',
#                                        'bin'))
# 
# Path: crumbs/seq/seq.py
# def assing_kind_to_seqs(kind, seqs, file_format):
#     'It puts each seq into a NamedTuple named Seq'
#     return (SeqWrapper(kind, seq, file_format) for seq in seqs)
# 
# def get_str_seq(seq):
#     seq_class = seq.kind
#     if seq_class == SEQITEM:
#         seq = seq.object.lines[1].strip()
#     elif seq_class == SEQRECORD:
#         seq = str(seq.object.seq)
#     return seq.strip()
# 
# Path: crumbs/seq/utils/seq_utils.py
# def uppercase_length(string):
#     'It returns the number of uppercase characters found in the string'
#     return len(re.findall("[A-Z]", string))
# 
# class ChangeCase(object):
#     'It changes the sequence case.'
# 
#     def __init__(self, action):
#         'The initiator'
#         if action not in (UPPERCASE, LOWERCASE, SWAPCASE):
#             msg = 'Action should be: uppercase, lowercase or invertcase'
#             raise ValueError(msg)
#         self.action = action
# 
#     def __call__(self, seqs):
#         'It changes the case of the seqrecords.'
#         action = self.action
#         processed_seqs = []
#         for seq in seqs:
#             str_seq = get_str_seq(seq)
#             if action == UPPERCASE:
#                 str_seq = str_seq.upper()
#             elif action == LOWERCASE:
#                 str_seq = str_seq.lower()
#             elif action == SWAPCASE:
#                 str_seq = str_seq.swapcase()
#             else:
#                 raise NotImplementedError()
#             seq = copy_seq(seq, seq=str_seq)
#             processed_seqs.append(seq)
#         return processed_seqs
# 
# def get_uppercase_segments(string):
#     '''It detects the unmasked regions of a sequence
# 
#     It returns a list of (start, end) tuples'''
#     start = 0
#     for is_upper, group in itertools.groupby(string, lambda x: x.isupper()):
#         group = list(group)
#         end = start + len(group) - 1
#         if is_upper:
#             yield start, end
#         start = end + 1
# 
# Path: crumbs/utils/tags.py
# SWAPCASE = 'swap'
# 
# UPPERCASE = 'upper'
# 
# LOWERCASE = 'lower'
# 
# SEQRECORD = 'seqrecord'


# Path: test/seq/test_seq_utils.py
import unittest
import os.path
from cStringIO import StringIO
from tempfile import NamedTemporaryFile
from subprocess import check_output
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from crumbs.utils.bin_utils import BIN_DIR
from crumbs.seq.seq import assing_kind_to_seqs, get_str_seq
from crumbs.seq.utils.seq_utils import (uppercase_length, ChangeCase,
                                        get_uppercase_segments)
from crumbs.utils.tags import SWAPCASE, UPPERCASE, LOWERCASE, SEQRECORD

    return fhand


class MaskedSegmentsTest(unittest.TestCase):
    'It tests the lower case segments location functions'

    @staticmethod
    def test_masked_locations():
        'It test the masked locations function'

        assert list(get_uppercase_segments('aaATTTTTTaa')) == [(2, 8)]

        assert list(get_uppercase_segments('aaATTTaTTaa')) == [(2, 5), (7, 8)]

        assert list(get_uppercase_segments('AAATaaa')) == [(0, 3)]

        assert list(get_uppercase_segments('aaaaAAAA')) == [(4, 7)]

        seq = 'AATTaaTTaaTTT'
        assert list(get_uppercase_segments(seq)) == [(0, 3), (6, 7), (10, 12)]

        assert list(get_uppercase_segments('AATT')) == [(0, 3)]
        assert not list(get_uppercase_segments('aatt'))


class ChangeCaseTest(unittest.TestCase):
    'It tests the case change'
    def test_case_change(self):
        'It changes the case of the sequences'
        seqs = [SeqRecord(Seq('aCCg'), letter_annotations={'dummy': 'dddd'})]

        seqs = assing_kind_to_seqs(SEQRECORD, seqs, None)