Example #1
0
 def test_expandAll(self):
     """StructureNode expandAll should act recursively"""
     r = StructureNode()
     r.append(StructureNode(Data=Stem(0, 6, 4)))
     r.append(StructureNode(Data=Stem(0, 6, 3)))
     r.append(StructureNode())
     r[0].append(StructureNode())
     r[0].append(StructureNode(Data=Stem(0,6,2)))
     r[0][-1].append(StructureNode())
     r.renumber()
     self.assertEqual(str(r), '((((.((.))))))((())).')
     r.expandAll()
     self.assertEqual(str(r), '((((.((.))))))((())).')
     expected_nodes = [
         (None, None, 0),
         (0, 13, 1),
         (1, 12, 1),
         (2, 11, 1),
         (3, 10, 1),
         (4, None, 0),
         (5, 9, 1),
         (6, 8, 1),
         (7, None, 0),
         (14, 19, 1),
         (15, 18, 1),
         (16, 17, 1),
         (20, None, 0),
     ]
     for obs, exp in zip(r.traverse(), expected_nodes):
         self.assertEqual((obs.Start, obs.End, obs.Length), exp)
Example #2
0
 def test_unpair(self):
     """StructureNode unpair should break a base pair and add correct nodes"""
     i = self.Internal
     self.assertEqual(i[0].unpair(), True)
     self.assertEqual(str(i), '.((...))...((.)).')
     e = self.Ends
     self.assertEqual(e[0].unpair(), False)
     self.assertEqual(str(e), self.EndsStr)
     o = self.OneHelix
     self.assertEqual(o[0].unpair(), True)
     self.assertEqual(str(o), '.(((()))).')
     self.assertEqual(o[1][0][0].unpair(), True)
     self.assertEqual(str(o), '.((.().)).')
     self.assertEqual(o[1].unpair(), True)
     self.assertEqual(str(o), '..(.().)..')
     self.assertEqual(o[2][1].unpair(), True)
     self.assertEqual(str(o), '..(....)..')
     self.assertEqual(o[2].unpair(), True)
     self.assertEqual(str(o), '..........')
     #test with multiple bases in helix
     r = StructureNode()
     r.append(StructureNode(Data=Stem(0,0, 5)))
     r.renumber()
     self.assertEqual(str(r), '((((()))))')
     self.assertEqual(r[0].unpair(), True)
     self.assertEqual(str(r), '.(((()))).')
Example #3
0
 def test_extendHelix(self):
     """StructureNode extendHelix should extend the helix as far as possible
     """
     #single paired node is root[4]
     op_str = ViennaStructure('....(......)...')
     op = op_str.toTree()
     #can't extend if base pairs not allowed
     op[4].extendHelix(Rna('AAAAAAAAAAAAAAA'))
     self.assertEqual(str(op), op_str)
     #should extend a pair 5'
     op[4].extendHelix(Rna('AAACCAAAAAAGGAA'))
     self.assertEqual(str(op), '...((......))..')
     #should extend multiple pairs 5'
     op = op_str.toTree()
     op[4].extendHelix(Rna('CCCCCUUUUUUGGGG'))
     self.assertEqual(str(op), '.((((......))))')
     #should extend a pair 3', but must leave > 2-base loop
     op = op_str.toTree()
     op[4].extendHelix(Rna('AAAACCCCGGGGAAA'))
     self.assertEqual(str(op), '....((....))...')
     op[4][0].insert(1, StructureNode(Data=Stem(Start=1, End=1, Length=1)))
     op.renumber()
     self.assertEqual(str(op), '....((.()...))...')
     op[4][0].extendHelix(Rna('AAAACCCUACGGGGAAA'))
     self.assertEqual(str(op), '....(((()..)))...')
     #should extend a pair in both directions if possible
     op = op_str.toTree()
     op[4].extendHelix(Rna('AAACCCAAAAGGGAA'))
     self.assertEqual(str(op), '...(((....)))..')
Example #4
0
 def test_unpair(self):
     """StructureNode unpair should break a base pair and add correct nodes"""
     i = self.Internal
     self.assertEqual(i[0].unpair(), True)
     self.assertEqual(str(i), '.((...))...((.)).')
     e = self.Ends
     self.assertEqual(e[0].unpair(), False)
     self.assertEqual(str(e), self.EndsStr)
     o = self.OneHelix
     self.assertEqual(o[0].unpair(), True)
     self.assertEqual(str(o), '.(((()))).')
     self.assertEqual(o[1][0][0].unpair(), True)
     self.assertEqual(str(o), '.((.().)).')
     self.assertEqual(o[1].unpair(), True)
     self.assertEqual(str(o), '..(.().)..')
     self.assertEqual(o[2][1].unpair(), True)
     self.assertEqual(str(o), '..(....)..')
     self.assertEqual(o[2].unpair(), True)
     self.assertEqual(str(o), '..........')
     #test with multiple bases in helix
     r = StructureNode()
     r.append(StructureNode(Data=Stem(0, 0, 5)))
     r.renumber()
     self.assertEqual(str(r), '((((()))))')
     self.assertEqual(r[0].unpair(), True)
     self.assertEqual(str(r), '.(((()))).')
Example #5
0
    def test_str(self):
        """ViennaNode str should return Vienna-format string"""
        for s in [self.EmptyStr, self.NoPairsStr, self.OneHelixStr,
            self.ManyHelicesStr, self.EndsStr, self.InternalStr]:
            self.assertEqual(str(ViennaStructure(s).toTree()), s)

        #test with multiple-base helix in a node
        r = StructureNode()
        r.append(StructureNode())
        r.append(StructureNode(Data=Stem(1,7,5)))
        r[1].append(StructureNode())
        r.append(StructureNode())
        r.append(StructureNode())
        r.renumber()
        self.assertEqual(str(r), '.(((((.)))))..')
Example #6
0
 def test_renumber(self):
     """ViennaNode renumber should assign correct numbers to nodes"""
     #should have no effect on empty structure
     se = self.Empty
     self.assertEqual(se.renumber(5), 5)
     self.assertEqual((se.Start, se.End, se.Length), (None, None, 0))
     #with no pairs, should number consecutively
     sn = self.NoPairs
     self.assertEqual(sn.renumber(5), 10)
     self.assertEqual([i.Start for i in sn], [5, 6, 7, 8, 9])
     self.assertEqual([i.End for i in sn], [None]*5)
     self.assertEqual([i.Length for i in sn], [0]*5)
     #spot checks on a complex structure
     sm = self.ManyHelices
     self.assertEqual(sm.renumber(5), 52)
     s0 = sm[0]
     self.assertEqual((s0.Start, s0.End, s0.Length), (5, 51, 1))
     s5 = sm[0][2][2][0]
     self.assertEqual(len(s5), 2)
     self.assertEqual((s5.Start, s5.End, s5.Length), (18, 33, 1))
     s6 = s5[0]
     self.assertEqual((s6.Start, s6.End, s6.Length), (19,None,0))
     #test with some helices of different lengths
     root = StructureNode()
     root.extend([StructureNode() for i in range(3)])
     root.insert(1, StructureNode(Data=Stem(3, 7, 5)))
     root.insert(3, StructureNode(Data=Stem(6,2,2)))
     root.append(StructureNode())
     self.assertEqual(root.renumber(0), 18)
     self.assertEqual(len(root), 6)
     curr = root[0]
     self.assertEqual((curr.Start,curr.End,curr.Length), (0, None, 0))
     curr = root[1]
     self.assertEqual((curr.Start, curr.End, curr.Length), (1, 10, 5))
     curr = root[2]
     self.assertEqual((curr.Start, curr.End, curr.Length), (11, None, 0))
     curr = root[3]
     self.assertEqual((curr.Start, curr.End, curr.Length), (12, 15, 2))
     curr = root[4]
     self.assertEqual((curr.Start, curr.End, curr.Length), (16, None, 0))
     curr = root[5]
     self.assertEqual((curr.Start, curr.End, curr.Length), (17, None, 0))
Example #7
0
 def test_pairChildren(self):
     """StructureNode PairChildren should make the correct pairs"""
     n = ViennaStructure('.....').toTree()  #same as self.NoPairs
     self.assertEqual(n.pairChildren(0, 4), True)
     self.assertEqual(str(n), '(...)')
     n = ViennaStructure('.....').toTree()  #same as self.NoPairs
     self.assertEqual(n.pairChildren(1, 4), True)
     self.assertEqual(str(n), '.(..)')
     n = ViennaStructure('.....').toTree()  #same as self.NoPairs
     #can't pair same object
     self.assertEqual(n.pairChildren(1, 1), False)
     self.assertEqual(str(n), '.....')
     self.assertEqual(n.pairChildren(1, -1), True)
     self.assertEqual(str(n), '.(..)')
     #can't pair something already paired
     self.assertEqual(n.pairChildren(0, 1), False)
     #IndexError if out of range
     self.assertRaises(IndexError, n.pairChildren, 0, 5)
     n.append(StructureNode())
     n.append(StructureNode())
     n.renumber()
     self.assertEqual(str(n), '.(..)..')
     self.assertEqual(n.pairChildren(0, -2), True)
     self.assertEqual(str(n), '((..)).')
Example #8
0
 def test_expandAll(self):
     """StructureNode expandAll should act recursively"""
     r = StructureNode()
     r.append(StructureNode(Data=Stem(0, 6, 4)))
     r.append(StructureNode(Data=Stem(0, 6, 3)))
     r.append(StructureNode())
     r[0].append(StructureNode())
     r[0].append(StructureNode(Data=Stem(0, 6, 2)))
     r[0][-1].append(StructureNode())
     r.renumber()
     self.assertEqual(str(r), '((((.((.))))))((())).')
     r.expandAll()
     self.assertEqual(str(r), '((((.((.))))))((())).')
     expected_nodes = [
         (None, None, 0),
         (0, 13, 1),
         (1, 12, 1),
         (2, 11, 1),
         (3, 10, 1),
         (4, None, 0),
         (5, 9, 1),
         (6, 8, 1),
         (7, None, 0),
         (14, 19, 1),
         (15, 18, 1),
         (16, 17, 1),
         (20, None, 0),
     ]
     for obs, exp in zip(r.traverse(), expected_nodes):
         self.assertEqual((obs.Start, obs.End, obs.Length), exp)
Example #9
0
 def test_expand(self):
     """StructureNode expand should extend helices."""
     s = StructureNode(Data=(Stem(1, 10, 3)))
     s.append(StructureNode())
     #need to make a root node for consistency
     r = StructureNode()
     r.append(s)
     self.assertEqual(str(s), '(((.)))')
     s.expand()
     self.assertEqual(str(s), '(((.)))')
     self.assertEqual((s.Start, s.End, s.Length), (1, 10, 1))
     n = s[0]
     self.assertEqual((n.Start, n.End, n.Length), (2, 9, 1))
     n = s[0][0]
     self.assertEqual((n.Start, n.End, n.Length), (3, 8, 1))
     n = s[0][0][0]
     self.assertEqual((n.Start, n.End, n.Length), (None, None, 0))
     s.renumber()
     self.assertEqual((s.Start, s.End, s.Length), (0, 6, 1))
     n = s[0]
     self.assertEqual((n.Start, n.End, n.Length), (1, 5, 1))
     n = s[0][0]
     self.assertEqual((n.Start, n.End, n.Length), (2, 4, 1))
     n = s[0][0][0]
     self.assertEqual((n.Start, n.End, n.Length), (3, None, 0))
     #check that it's not recursive
     s[0][0].append(StructureNode(Data=Stem(20, 24, 2)))
     s.expand()
     n = s[0][0][-1]
     self.assertEqual((n.Start, n.End, n.Length), (20, 24, 2))
     n.expand()
     self.assertEqual((n.Start, n.End, n.Length), (20, 24, 1))
     n = n[0]
     self.assertEqual((n.Start, n.End, n.Length), (21, 23, 1))
Example #10
0
 def test_renumber(self):
     """ViennaNode renumber should assign correct numbers to nodes"""
     #should have no effect on empty structure
     se = self.Empty
     self.assertEqual(se.renumber(5), 5)
     self.assertEqual((se.Start, se.End, se.Length), (None, None, 0))
     #with no pairs, should number consecutively
     sn = self.NoPairs
     self.assertEqual(sn.renumber(5), 10)
     self.assertEqual([i.Start for i in sn], [5, 6, 7, 8, 9])
     self.assertEqual([i.End for i in sn], [None] * 5)
     self.assertEqual([i.Length for i in sn], [0] * 5)
     #spot checks on a complex structure
     sm = self.ManyHelices
     self.assertEqual(sm.renumber(5), 52)
     s0 = sm[0]
     self.assertEqual((s0.Start, s0.End, s0.Length), (5, 51, 1))
     s5 = sm[0][2][2][0]
     self.assertEqual(len(s5), 2)
     self.assertEqual((s5.Start, s5.End, s5.Length), (18, 33, 1))
     s6 = s5[0]
     self.assertEqual((s6.Start, s6.End, s6.Length), (19, None, 0))
     #test with some helices of different lengths
     root = StructureNode()
     root.extend([StructureNode() for i in range(3)])
     root.insert(1, StructureNode(Data=Stem(3, 7, 5)))
     root.insert(3, StructureNode(Data=Stem(6, 2, 2)))
     root.append(StructureNode())
     self.assertEqual(root.renumber(0), 18)
     self.assertEqual(len(root), 6)
     curr = root[0]
     self.assertEqual((curr.Start, curr.End, curr.Length), (0, None, 0))
     curr = root[1]
     self.assertEqual((curr.Start, curr.End, curr.Length), (1, 10, 5))
     curr = root[2]
     self.assertEqual((curr.Start, curr.End, curr.Length), (11, None, 0))
     curr = root[3]
     self.assertEqual((curr.Start, curr.End, curr.Length), (12, 15, 2))
     curr = root[4]
     self.assertEqual((curr.Start, curr.End, curr.Length), (16, None, 0))
     curr = root[5]
     self.assertEqual((curr.Start, curr.End, curr.Length), (17, None, 0))
Example #11
0
    def test_str(self):
        """ViennaNode str should return Vienna-format string"""
        for s in [
                self.EmptyStr, self.NoPairsStr, self.OneHelixStr,
                self.ManyHelicesStr, self.EndsStr, self.InternalStr
        ]:
            self.assertEqual(str(ViennaStructure(s).toTree()), s)

        #test with multiple-base helix in a node
        r = StructureNode()
        r.append(StructureNode())
        r.append(StructureNode(Data=Stem(1, 7, 5)))
        r[1].append(StructureNode())
        r.append(StructureNode())
        r.append(StructureNode())
        r.renumber()
        self.assertEqual(str(r), '.(((((.)))))..')
Example #12
0
 def test_expand(self):
     """StructureNode expand should extend helices."""
     s = StructureNode(Data=(Stem(1, 10, 3)))
     s.append(StructureNode())
     #need to make a root node for consistency
     r = StructureNode()
     r.append(s)
     self.assertEqual(str(s), '(((.)))')
     s.expand()
     self.assertEqual(str(s), '(((.)))')
     self.assertEqual((s.Start, s.End, s.Length), (1, 10, 1))
     n = s[0]
     self.assertEqual((n.Start, n.End, n.Length), (2, 9, 1))
     n = s[0][0]
     self.assertEqual((n.Start, n.End, n.Length), (3, 8, 1))
     n = s[0][0][0]
     self.assertEqual((n.Start, n.End, n.Length), (None, None, 0))
     s.renumber()
     self.assertEqual((s.Start, s.End, s.Length), (0, 6, 1))
     n = s[0]
     self.assertEqual((n.Start, n.End, n.Length), (1, 5, 1))
     n = s[0][0]
     self.assertEqual((n.Start, n.End, n.Length), (2, 4, 1))
     n = s[0][0][0]
     self.assertEqual((n.Start, n.End, n.Length), (3, None, 0))
     #check that it's not recursive
     s[0][0].append(StructureNode(Data=Stem(20, 24, 2)))
     s.expand()
     n = s[0][0][-1]
     self.assertEqual((n.Start, n.End, n.Length), (20, 24, 2))
     n.expand()
     self.assertEqual((n.Start, n.End, n.Length), (20, 24, 1))
     n = n[0]
     self.assertEqual((n.Start, n.End, n.Length), (21, 23, 1))