# Path: crumbs/utils/bin_utils.py
# SEQ_BIN_DIR = os.path.join(BIN_DIR, 'seq')
# 
# 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 tempfile import NamedTemporaryFile
from subprocess import check_output
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from crumbs.utils.bin_utils import SEQ_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

class UppercaseLengthTest(unittest.TestCase):
    'It tests the uppercase character count'
    def test_uppercase_length(self):
        'It counts the number of uppercase letters in a string'
        assert uppercase_length('aCTaGGt') == 4
        assert uppercase_length('acagt') == 0


def _make_fhand(content=''):
    'It makes temporary fhands'
    fhand = NamedTemporaryFile()
    fhand.write(content)
    fhand.flush()
    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)]


# Path: crumbs/utils/bin_utils.py
# SEQ_BIN_DIR = os.path.join(BIN_DIR, 'seq')
# 
# 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 tempfile import NamedTemporaryFile
from subprocess import check_output
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from crumbs.utils.bin_utils import SEQ_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


class UppercaseLengthTest(unittest.TestCase):
    'It tests the uppercase character count'
    def test_uppercase_length(self):
        'It counts the number of uppercase letters in a string'
        assert uppercase_length('aCTaGGt') == 4
        assert uppercase_length('acagt') == 0


def _make_fhand(content=''):
    'It makes temporary fhands'
    fhand = NamedTemporaryFile()
    fhand.write(content)
    fhand.flush()
    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)]