# Path: crumbs/utils/segments_utils.py
# def get_longest_segment(segments):
#     'It returns the longest segment'
#     longest = None
#     longest_size = None
#     for segment in segments:
#         size = segment[1] - segment[0]
#         if longest is None:
#             longest = [segment]
#             longest_size = size
#         elif size > longest_size:
#             longest = [segment]
#             longest_size = size
#         elif size == longest_size:
#             longest.append(segment)
# 
#     if longest is None:
#         return None
#     elif len(longest) == 1:
#         return longest[0]
#     else:
#         return random.choice(longest)
# 
# def get_all_segments(segments, seq_len):
#     '''Given a set of some non overlapping regions it returns all regions.
# 
#     input:  --- ----       ----
#     output: ---+----+++++++----
# 
#     '''
#     segments_ = copy.deepcopy(segments)
#     if not segments_:
#         return [((0, seq_len - 1), False)]
# 
#     all_segments = []
#     # If the first segment doesn't start at zero we create a new one
#     if segments_[0][0] == 0:
#         start = segments_[0][1] + 1
#         all_segments.append((segments_.pop(0), True))
#     else:
#         start = 0
# 
#     for loc in segments_:
#         all_segments.append(((start, loc[0] - 1), False))
#         all_segments.append((loc, True))
#         start = loc[1] + 1
#     else:
#         # if the last segment does not ends at the end of the sequence we add
#         # an extra one
#         end = seq_len - 1
#         if start <= end:
#             all_segments.append(((start, end), False))
#     return all_segments
# 
# def get_complementary_segments(segments, seq_len):
#     'Given a set of regions in a seq it returns the complementary ones'
#     non_matched = []
#     for segment in get_all_segments(segments, seq_len):
#         if not segment[1]:
#             non_matched.append(segment[0])
#     return non_matched
# 
# def get_longest_complementary_segment(segments, seq_len):
#     'Given a seq and the locations it returns the longest region not covered'
# 
#     segments = merge_overlaping_segments(segments)
# 
#     # we want the non-matched locations
#     segments = get_complementary_segments(segments, seq_len)
# 
#     # now we return the longest one
#     return get_longest_segment(segments)


import unittest
from crumbs.utils.segments_utils import (get_longest_segment, get_all_segments,
                                         get_complementary_segments,
                                         get_longest_complementary_segment)

        segments = [(0, 3), (10, 34)]
        assert (10, 34) == get_longest_segment(segments)

        segments = [(0, 3), (10, 13)]
        segment = get_longest_segment(segments)
        assert segment == (0, 3) or segment == (10, 13)

    @staticmethod
    def test_get_all_segments():
        'Give a list of discontinious segments we get all segments'
        segments = get_all_segments([(0, 10), (15, 20)], 30)
        assert segments == [((0, 10), True), ((11, 14), False),
                            ((15, 20), True), ((21, 29), False)]
        segments = get_all_segments([(15, 20)], 30)
        assert segments == [((0, 14), False),
                            ((15, 20), True), ((21, 29), False)]
        segments = get_all_segments([(15, 29)], 30)
        assert segments == [((0, 14), False), ((15, 29), True)]

    @staticmethod
    def test_non_matched():
        'Given a list of segments we get the complementary matches'
        segments = get_complementary_segments([(0, 10), (15, 20)], 30)
        assert segments == [(11, 14), (21, 29)]

    @staticmethod
    def test_get_longest_complementary():
        'It test that we can get the longest complementary segment'
        segments = [(0, 200)]

# Path: crumbs/utils/segments_utils.py
# def get_longest_segment(segments):
#     'It returns the longest segment'
#     longest = None
#     longest_size = None
#     for segment in segments:
#         size = segment[1] - segment[0]
#         if longest is None:
#             longest = [segment]
#             longest_size = size
#         elif size > longest_size:
#             longest = [segment]
#             longest_size = size
#         elif size == longest_size:
#             longest.append(segment)
# 
#     if longest is None:
#         return None
#     elif len(longest) == 1:
#         return longest[0]
#     else:
#         return random.choice(longest)
# 
# def get_all_segments(segments, seq_len):
#     '''Given a set of some non overlapping regions it returns all regions.
# 
#     input:  --- ----       ----
#     output: ---+----+++++++----
# 
#     '''
#     segments_ = copy.deepcopy(segments)
#     if not segments_:
#         return [((0, seq_len - 1), False)]
# 
#     all_segments = []
#     # If the first segment doesn't start at zero we create a new one
#     if segments_[0][0] == 0:
#         start = segments_[0][1] + 1
#         all_segments.append((segments_.pop(0), True))
#     else:
#         start = 0
# 
#     for loc in segments_:
#         all_segments.append(((start, loc[0] - 1), False))
#         all_segments.append((loc, True))
#         start = loc[1] + 1
#     else:
#         # if the last segment does not ends at the end of the sequence we add
#         # an extra one
#         end = seq_len - 1
#         if start <= end:
#             all_segments.append(((start, end), False))
#     return all_segments
# 
# def get_complementary_segments(segments, seq_len):
#     'Given a set of regions in a seq it returns the complementary ones'
#     non_matched = []
#     for segment in get_all_segments(segments, seq_len):
#         if not segment[1]:
#             non_matched.append(segment[0])
#     return non_matched
# 
# def get_longest_complementary_segment(segments, seq_len):
#     'Given a seq and the locations it returns the longest region not covered'
# 
#     segments = merge_overlaping_segments(segments)
# 
#     # we want the non-matched locations
#     segments = get_complementary_segments(segments, seq_len)
# 
#     # now we return the longest one
#     return get_longest_segment(segments)


# Path: test/test_segment_utils.py
import unittest
from crumbs.utils.segments_utils import (get_longest_segment, get_all_segments,
                                         get_complementary_segments,
                                         get_longest_complementary_segment)


        segments = [(0, 3), (10, 34)]
        assert (10, 34) == get_longest_segment(segments)

        segments = [(0, 3), (10, 13)]
        segment = get_longest_segment(segments)
        assert segment == (0, 3) or segment == (10, 13)

    @staticmethod
    def test_get_all_segments():
        'Give a list of discontinious segments we get all segments'
        segments = get_all_segments([(0, 10), (15, 20)], 30)
        assert segments == [((0, 10), True), ((11, 14), False),
                            ((15, 20), True), ((21, 29), False)]
        segments = get_all_segments([(15, 20)], 30)
        assert segments == [((0, 14), False),
                            ((15, 20), True), ((21, 29), False)]
        segments = get_all_segments([(15, 29)], 30)
        assert segments == [((0, 14), False), ((15, 29), True)]

    @staticmethod
    def test_non_matched():
        'Given a list of segments we get the complementary matches'
        segments = get_complementary_segments([(0, 10), (15, 20)], 30)
        assert segments == [(11, 14), (21, 29)]

    @staticmethod
    def test_get_longest_complementary():
        'It test that we can get the longest complementary segment'
        segments = [(0, 200)]

        result = get_longest_complementary_segment(segments, seq_len=200)