# 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)
# ngs_crumbs is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# ngs_crumbs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with ngs_crumbs. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=R0201
# pylint: disable=R0904




class SegmentsTest(unittest.TestCase):
    'It tests the segments functions'

    @staticmethod
    def test_get_longest_section():
        'It gets the longest section from a list of sections'

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

        segments = [(0, 3), (10, 13)]

# 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)

# ngs_crumbs is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# ngs_crumbs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with ngs_crumbs. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=R0201
# pylint: disable=R0904




class SegmentsTest(unittest.TestCase):
    'It tests the segments functions'

    @staticmethod
    def test_get_longest_section():
        'It gets the longest section from a list of sections'

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

        segments = [(0, 3), (10, 13)]

        segment = get_longest_segment(segments)