Exemple #1
0
def match_pairs(reads, out_fhand, orphan_out_fhand, out_format, ordered=True,
                check_order_buffer_size=0, max_reads_memory=None,
                temp_dir=None):
    '''It matches the seq pairs in an iterator and splits the orphan seqs.'''
    counts = 0
    check_order_buffer = KeyedSet()
    for pair in _get_paired_and_orphan(reads, ordered, max_reads_memory,
                                       temp_dir):
        if len(pair) == 1:
            write_seqs(pair, orphan_out_fhand, out_format)
            try:
                name = _parse_pair_direction_and_name(pair[0])[0]
            except PairDirectionError:
                name = get_name(pair[0])
            if ordered and counts < check_order_buffer_size:
                counts += 1
                if not check_order_buffer.check_add(name):
                    msg = 'Reads are not ordered by pairs.Use unordered option'
                    raise ItemsNotSortedError(msg)
            elif ordered and counts >= check_order_buffer_size:
                if name in check_order_buffer:
                    msg = 'Reads are not ordered by pairs.Use unordered option'
                    raise ItemsNotSortedError(msg)
        elif len(pair) == 2:
            write_seqs(pair, out_fhand, out_format)
    flush_fhand(orphan_out_fhand)
    flush_fhand(out_fhand)
Exemple #2
0
def match_pairs(reads,
                out_fhand,
                orphan_out_fhand,
                out_format,
                ordered=True,
                check_order_buffer_size=0,
                max_reads_memory=None,
                temp_dir=None):
    '''It matches the seq pairs in an iterator and splits the orphan seqs.'''
    counts = 0
    check_order_buffer = KeyedSet()
    for pair in _get_paired_and_orphan(reads, ordered, max_reads_memory,
                                       temp_dir):
        if len(pair) == 1:
            write_seqs(pair, orphan_out_fhand, out_format)
            try:
                name = _parse_pair_direction_and_name(pair[0])[0]
            except PairDirectionError:
                name = get_name(pair[0])
            if ordered and counts < check_order_buffer_size:
                counts += 1
                if not check_order_buffer.check_add(name):
                    msg = 'Reads are not ordered by pairs.Use unordered option'
                    raise ItemsNotSortedError(msg)
            elif ordered and counts >= check_order_buffer_size:
                if name in check_order_buffer:
                    msg = 'Reads are not ordered by pairs.Use unordered option'
                    raise ItemsNotSortedError(msg)
        elif len(pair) == 2:
            write_seqs(pair, out_fhand, out_format)
    flush_fhand(orphan_out_fhand)
    flush_fhand(out_fhand)
    def test_unordered_set(self):
        in_set = [1, 2, 3, 4, 5, 8, 10]
        not_in_set = [6, 9, 11, 13]
        keyed_set = KeyedSet(in_set)
        for item in in_set:
            assert item in keyed_set
        assert keyed_set.check_add(7)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert not keyed_set.check_add(2)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert keyed_set.check_add(0)
        assert keyed_set._items == set([0, 1, 2, 3, 4, 5, 7, 8, 10])

        for item in not_in_set:
            assert item not in keyed_set

        #with key
        a = 'a'
        in_set = [(1, a), (2, a), (3, a), (4, a), (5, a),  (8, a), (10, a)]
        not_in_set = [(6, a), (9, a), (11, a), (13, a)]

        def key(item):
            return item[0]
        keyed_set = KeyedSet(in_set, key=key)
        for item in in_set:
            assert item in keyed_set
        assert keyed_set.check_add((7, a))
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert not keyed_set.check_add((2, a))
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert keyed_set.check_add((0, a))
        assert keyed_set._items == set([0, 1, 2, 3, 4, 5, 7, 8, 10])
        for item in not_in_set:
            assert item not in keyed_set
Exemple #4
0
    def test_unordered_set(self):
        in_set = [1, 2, 3, 4, 5, 8, 10]
        not_in_set = [6, 9, 11, 13]
        keyed_set = KeyedSet(in_set)
        for item in in_set:
            assert item in keyed_set
        assert keyed_set.check_add(7)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert not keyed_set.check_add(2)
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert keyed_set.check_add(0)
        assert keyed_set._items == set([0, 1, 2, 3, 4, 5, 7, 8, 10])

        for item in not_in_set:
            assert item not in keyed_set

        #with key
        a = 'a'
        in_set = [(1, a), (2, a), (3, a), (4, a), (5, a), (8, a), (10, a)]
        not_in_set = [(6, a), (9, a), (11, a), (13, a)]

        def key(item):
            return item[0]

        keyed_set = KeyedSet(in_set, key=key)
        for item in in_set:
            assert item in keyed_set
        assert keyed_set.check_add((7, a))
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert not keyed_set.check_add((2, a))
        assert keyed_set._items == set([1, 2, 3, 4, 5, 7, 8, 10])
        assert keyed_set.check_add((0, a))
        assert keyed_set._items == set([0, 1, 2, 3, 4, 5, 7, 8, 10])
        for item in not_in_set:
            assert item not in keyed_set