def test_make_loop_index_00(self):
        struct = '.'
        pt = make_pair_table(struct)
        exp1 = [[0]]
        exp2 = set([0])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)

        struct = '(((...)))'
        pt = make_pair_table(struct)
        exp1 = [[1, 2, 3, 3, 3, 3, 3, 2, 1]]
        exp2 = set([0])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)

        struct = '..(((...)))..'
        pt = make_pair_table(struct)
        exp1 = [[0, 0, 1, 2, 3, 3, 3, 3, 3, 2, 1, 0, 0]]
        exp2 = set([0])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)

        struct = '.((.((...))..((...).))).'
        pt = make_pair_table(struct)
        exp1 = [[
            0, 1, 2, 2, 3, 4, 4, 4, 4, 4, 3, 2, 2, 5, 6, 6, 6, 6, 6, 5, 5, 2,
            1, 0
        ]]
        exp2 = set([0])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)
    def test_make_loop_index_01(self):
        struct = '.((.((...)).+.((...).))).'
        pt = make_pair_table(struct)
        exp1 = [[0, 1, 2, 2, 3, 4, 4, 4, 4, 4, 3, 2],
                [2, 5, 6, 6, 6, 6, 6, 5, 5, 2, 1, 0]]
        exp2 = set([0, 2])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)

        struct = '.((.((...))+..((...).))).'
        pt = make_pair_table(struct)
        exp1 = [[0, 1, 2, 2, 3, 4, 4, 4, 4, 4, 3],
                [2, 2, 5, 6, 6, 6, 6, 6, 5, 5, 2, 1, 0]]
        exp2 = set([0, 2])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)

        struct = '.((.((...))+((...).))).'
        pt = make_pair_table(struct)
        exp1 = [[0, 1, 2, 2, 3, 4, 4, 4, 4, 4, 3],
                [5, 6, 6, 6, 6, 6, 5, 5, 2, 1, 0]]
        exp2 = set([0, 2])
        out1, out2 = make_loop_index(pt)
        self.assertEqual(out1, exp1)
        self.assertSetEqual(out2, exp2)
    def test_make_loop_index_02(self):
        with self.assertRaises(SecondaryStructureError):
            struct = '..+..'
            pt = make_pair_table(struct)
            out1, out2 = make_loop_index(pt)

        with self.assertRaises(SecondaryStructureError):
            struct = '(.)+.(..)'
            pt = make_pair_table(struct)
            out1, out2 = make_loop_index(pt)

        with self.assertRaises(SecondaryStructureError):
            struct = '((..((.))+.(..).+((...))))'
            pt = make_pair_table(struct)
            out1, out2 = make_loop_index(pt)
    def test_split_complex_01(self):
        se = 'CCCT+AAA+TTGGG'
        ss = '(((.+...+..)))'
        ps = make_strand_table(se)
        pt = make_pair_table(ss)
        out = list(split_complex_pt(ps, pt))
        assert len(out) == 2
        outdb = list(split_complex_db(se, ss, join=True))
        assert ('CCCT+TTGGG', '(((.+..)))') in outdb
        assert ('AAA', '...') in outdb

        se = 'CCCT+AAA+TTTT+TTGGG'
        ss = '(((.+...+....+..)))'
        ps = make_strand_table(se)
        pt = make_pair_table(ss)
        out = list(split_complex_pt(ps, pt))
        assert len(out) == 3
        outdb = list(split_complex_db(se, ss, join=True))
        assert ('CCCT+TTGGG', '(((.+..)))') in outdb
        assert ('AAA', '...') in outdb
        assert ('TTTT', '....') in outdb
    def test_pair_tables(self):
        inp = '(((...)))'
        res = make_pair_table(inp)
        exp = [[(0, 8), (0, 7), (0, 6), None, None, None, (0, 2), (0, 1),
                (0, 0)]]
        rev = pair_table_to_dot_bracket(exp, join=True)
        self.assertEqual(res, exp)
        self.assertEqual(inp, rev)

        inp = '(((+...)))'
        res = make_pair_table(inp)
        exp = [[(1, 5), (1, 4), (1, 3)],
               [None, None, None, (0, 2), (0, 1), (0, 0)]]
        rev = pair_table_to_dot_bracket(exp, join=True)
        self.assertEqual(res, exp)
        self.assertEqual(inp, rev)

        res = make_pair_table('((((+))).)')
        exp = [[(1, 4), (1, 2), (1, 1), (1, 0)],
               [(0, 3), (0, 2), (0, 1), None, (0, 0)]]
        self.assertEqual(res, exp)

        inp = '((((&))).)'
        res = make_pair_table(inp, strand_break='&')
        exp = [[(1, 4), (1, 2), (1, 1), (1, 0)],
               [(0, 3), (0, 2), (0, 1), None, (0, 0)]]
        rev = pair_table_to_dot_bracket(exp, strand_break='&', join=True)
        self.assertEqual(res, exp)
        self.assertEqual(inp, rev)

        with self.assertRaises(SecondaryStructureError):
            res = make_pair_table('((((+)).)')

        with self.assertRaises(SecondaryStructureError):
            res = make_pair_table('((((&))).)')
Ejemplo n.º 6
0
def join_complexes_21(complex1, loc1, complex2, loc2):
    """ Joins two complexes into one disconnected, base-pair-compatible complex.

    Returns the disconnected complex and the loci for pairing.
    """
    # a value larger than any possible location on the pair_table.
    maxlen = complex1.size + complex2.size + 1

    for e, (st, pt) in enumerate(complex1.rotate_pt()):
        l1 = complex1.rotate_pairtable_loc(loc1, e)
        li, ext = make_loop_index(pt)
        if li[l1[0]][l1[1]] == 0:
            seq1 = strand_table_to_sequence(st)
            pt[l1[0]][l1[1]] = (maxlen, maxlen) # add an additional '(' 
            ptb1 = pair_table_to_dot_bracket(pt)
            break
    for e, (st, pt) in enumerate(complex2.rotate_pt()):
        l2 = complex2.rotate_pairtable_loc(loc2, e)
        li, ext = make_loop_index(pt)
        if li[l2[0]][l2[1]] == 0:
            seq2 = strand_table_to_sequence(st)
            pt[l2[0]][l2[1]] = (-1, -1) # add an additional ')' 
            ptb2 = pair_table_to_dot_bracket(pt)
            break

    # build the new sequence and structure *including* the new pair
    newseq = seq1 + ['+'] + seq2
    newstr = ptb1 + ['+'] + ptb2
    # update l2 from the new structure
    combined = make_pair_table(newstr)
    l2 = combined[l1[0]][l1[1]]
    # remove the new pair again ...
    combined[l1[0]][l1[1]] = None
    combined[l2[0]][l2[1]] = None
    # update the structure to the unpaired (disconnected) version.
    newstr = pair_table_to_dot_bracket(combined)
    try:
        new_complex = PepperComplex(newseq, newstr)
    except SingletonError as err:
        new_complex = err.existing
    # strands may be rotated in the new complex ...
    for e, (st, pt) in enumerate(new_complex.rotate()):
        if st == newseq and pt == newstr:
            rotate = e
            break
    else:
        raise ValueError(f'Joining of complexes {complex1} and {complex2} failed.')
    loc1 = new_complex.rotate_pairtable_loc(l1, -rotate)
    loc2 = new_complex.rotate_pairtable_loc(l2, -rotate)
    if loc1 > loc2:
        (loc1, loc2) = (loc2, loc1)
    return new_complex, loc1, loc2
 def test_split_complex_02(self):
     se = 'CCCT+AAA+GCGC+TTTT+TTGGG'
     ss = '(((.+.(.+)()(+.)..+..)))'
     ps = make_strand_table(se)
     pt = make_pair_table(ss)
     bs, bt = ps[:], pt[:]
     out = list(split_complex_pt(ps, pt))
     assert len(out) == 2
     assert bs == ps
     assert bt == pt
     outdb = list(split_complex_db(se, ss, join=True))
     assert len(outdb) == 2
     assert ('CCCT+TTGGG', '(((.+..)))') in outdb
     assert ('AAA+GCGC+TTTT', '.(.+)()(+.)..') in outdb
    def test_split_complex_00(self):
        se = list('CCCTTTGGG')
        ss = list('(((...)))')
        ps = make_strand_table(se)
        pt = make_pair_table(ss)
        out = list(split_complex_pt(ps, pt))
        assert len(out) == 1
        c1 = ([['C', 'C', 'C', 'T', 'T', 'T', 'G', 'G',
                'G']], [[(0, 8), (0, 7), (0, 6), None, None, None, (0, 2),
                         (0, 1), (0, 0)]])
        assert c1 in out

        outdb = list(split_complex_db(se, ss))
        assert (se, ss) in outdb

        se = list('CCCT+TTGGG')
        ss = list('(((.+..)))')
        ps = make_strand_table(se)
        pt = make_pair_table(ss)
        out = list(split_complex_pt(ps, pt))
        assert len(out) == 1
        outdb = list(split_complex_db(se, ss))
        assert (se, ss) in outdb
    def test_make_loop_index_03(self):
        struct = '..+..'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[0, 0], [0, 0]], [[0, 0], [0, 0]])

        struct = '(.)+.(..)'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[1, 1, 1], [0, 2, 2, 2, 2]], [[0, 0], [0, 0]])

        struct = '(.)(+(+(+(.)+)+)+).(..)'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[1, 1, 1, 2], [3], [4], [5, 5, 5], [4], [3],
                        [2, 0, 6, 6, 6, 6]], [[0, 2], [2, 3], [3, 4], [4, 4],
                                              [4, 3], [3, 2], [2, 0]])

        struct = '(.)(+(+(+(+))+(.)+)+).(..)'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[1, 1, 1, 2], [3], [4], [5], [5, 4], [6, 6, 6], [3],
                        [2, 0, 7, 7, 7, 7]], [[0, 2], [2, 3], [3, 4], [4, 5],
                                              [5, 3], [3, 3], [3, 2], [2, 0]])

        struct = '((..((.))+.(..).+((...))))'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[1, 2, 2, 2, 3, 4, 4, 4, 3], [2, 5, 5, 5, 5, 2],
                        [6, 7, 7, 7, 7, 7, 6, 2, 1]], [[0, 2], [2, 2], [2, 0]])

        struct = '((..((.))+.(..).+(+(...)+)))'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[1, 2, 2, 2, 3, 4, 4, 4, 3], [2, 5, 5, 5, 5, 2], [6],
                        [7, 7, 7, 7, 7], [6, 2, 1]], [[0, 2], [2, 2], [2, 6],
                                                      [6, 6], [6, 0]])

        struct = '((..((.))+(+.(..)+).+(+(...)+)))'
        pt = make_pair_table(struct)
        out = make_loop_index(pt, components=True)
        assert out == ([[1, 2, 2, 2, 3, 4, 4, 4, 3], [5], [5, 6, 6, 6, 6],
                        [5, 2], [7], [8, 8, 8, 8, 8], [7, 2, 1]], [[0, 2],
                                                                   [2, 5],
                                                                   [5, 5],
                                                                   [5, 2],
                                                                   [2, 7],
                                                                   [7, 7],
                                                                   [7, 0]])
    def test_split_complex_03(self):
        se = 'CCCT+TC+GT+TG+C+GG'
        ss = '(((.+.(+).+.)+.+))'
        ps = make_strand_table(se)
        pt = make_pair_table(ss)
        out = list(split_complex_pt(ps, pt))
        assert len(out) == 3
        c1 = ([['T', 'C'], ['G', 'T']], [[None, (1, 0)], [(0, 1), None]])
        c2 = ([['C']], [[None]])
        c3 = ([['C', 'C', 'C', 'T'], ['T', 'G'], ['G', 'G']], [[(2, 1), (2, 0),
                                                                (1, 1), None],
                                                               [None, (0, 2)],
                                                               [(0, 1),
                                                                (0, 0)]])
        assert c1 in out
        assert c2 in out
        assert c3 in out

        outdb = list(split_complex_db(se, ss, join=True))
        assert ('TC+GT', '.(+).') in outdb
        assert ('C', '.') in outdb
        assert ('CCCT+TG+GG', '(((.+.)+))') in outdb