def test_matches_ile(self): """Test of isoleucine match should work correctly""" index = '012345678901234567890123456789012345' seq_good = 'AAACCCCUACUUUUUCCCAAAAAUAUUGGGGGGGAA' seq_bad = 'AAACCCCUACUUUUUCCCAAAAAUAUUGGGCGGGAA' st_good = '...(((((..(((((...)))))......)))))..' st_bad = '((((((((..(((((...)))))...))))))))..' st_good = ViennaStructure(st_good) st_bad = ViennaStructure(st_bad) for first_pos in range(len(seq_good) - len(self.ile_mod_0) + 1): for second_pos in range(len(seq_good) - len(self.ile_mod_1) + 1): #seq_good and struct_good should match at one location match=self.ile.matches(seq_good,st_good,[first_pos,second_pos]) if (first_pos == 3) and (second_pos == 18): self.assertEqual(match, True) else: self.assertEqual(match, False) self.assertEqual(self.ile.matches(seq_good, st_bad, \ [first_pos, second_pos]), False) self.assertEqual(self.ile.matches(seq_bad, st_good, \ [first_pos, second_pos]), False) self.assertEqual(self.ile.matches(seq_bad, st_bad, \ [first_pos, second_pos]), False)
def test_breakBadPairs(self): """StructureNode breakBadPairs should eliminate mispaired bases.""" oh_str = ViennaStructure(self.OneHelixStr) #no change if all pairs valid oh = oh_str.toTree() oh.breakBadPairs(Rna('CCCCCGGGGG')) self.assertEqual(str(oh), str(oh_str)) #break everything if all pairs invalid oh.breakBadPairs(Rna('CCCCCAAAAA')) self.assertEqual(str(oh), '..........') #break a single pair oh = oh_str.toTree() oh.breakBadPairs(Rna('GCCCCGGGGG')) self.assertEqual(str(oh), '.(((()))).') #break two pairs oh = oh_str.toTree() oh.breakBadPairs(Rna('GCCCCCGGGG')) self.assertEqual(str(oh), '.(((..))).') #break internal pairs oh = oh_str.toTree() oh.breakBadPairs(Rna('GCCGCGGGGG')) self.assertEqual(str(oh), '.((.().)).') #repeat with multiple independent helices th_str = ViennaStructure('((.)).((.))') th = th_str.toTree() th.breakBadPairs(Rna('CCUGGCUUCGG')) self.assertEqual(str(th), th_str) th.breakBadPairs(Rna('CGUAGCAGUUU')) self.assertEqual(str(th), '(...).((.))') th = th_str.toTree() th.breakBadPairs(Rna('UUUUUUUUUUU')) self.assertEqual(str(th), '...........')
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), '...(((....)))..')
def test_matches_simple(self): """Test of simple match should work correctly""" index = '01234567890123456789012345678901' seq = 'AAACCCCCUUUGGGGGAAACCCCCUUUGGGGG' struct = ViennaStructure('((..((..))....))...(((.......)))') struct_2 = ViennaStructure('((((((..((())))))))).....(((.)))') #substring right, not pair self.assertEqual(self.simple.matches(seq, struct, [19, 27]), True) self.assertEqual(self.simple.matches(seq, struct_2, [19,27]), False) for first_pos in range(len(seq) - len(self.simple_0) + 1): for second_pos in range(len(seq) - len(self.simple_1) + 1): #should match struct only at one location match=self.simple.matches(seq, struct, [first_pos, second_pos]) if (first_pos == 19) and (second_pos == 27): self.assertEqual(match, True) else: self.assertEqual(match, False) #should never match in struct_2 self.assertEqual(self.simple.matches(seq, struct_2, \ [first_pos, second_pos]), False) #check that it doesn't fail if there are _two_ matches index = '01234567890123456789' seq = 'CCCCCGGGGGCCCCCGGGGG' struct = '(((....)))(((....)))' struct = ViennaStructure(struct) self.assertEqual(self.simple.matches(seq, struct, [0, 5]), True) self.assertEqual(self.simple.matches(seq, struct, [10,15]), True) #not allowed to cross-pair self.assertEqual(self.simple.matches(seq, struct, [0, 15]), False)
def to_Pairs(struct=None): """ Converts a vienna structure into a pairs object Returns pairs object """ struct = ViennaStructure(struct) pairs = struct.toPairs() return pairs
def test_fitSeq(self): """StructureNode fitSeq should adjust structure to match sequence""" #this is just a minimal test, since we know that both breakBadPairs() #and extendHelices() work fine with more extensive tests. s = ViennaStructure('..(((.....)))......(((.....)))...') r = Rna( 'UCCCCACUGAGGGGUUUGGGGGGUUUUCGCCCU') t = s.toTree() t.fitSeq(r) self.assertEqual(str(t), '.((((.....))))...(((.((...)).))).')
def test_fitSeq(self): """StructureNode fitSeq should adjust structure to match sequence""" #this is just a minimal test, since we know that both breakBadPairs() #and extendHelices() work fine with more extensive tests. s = ViennaStructure('..(((.....)))......(((.....)))...') r = Rna('UCCCCACUGAGGGGUUUGGGGGGUUUUCGCCCU') t = s.toTree() t.fitSeq(r) self.assertEqual(str(t), '.((((.....))))...(((.((...)).))).')
def to_pairs(struct): """ Takes a structure in dot-bracket notation and converts it to pairs notation functions tested in rna2d.py """ struct = ViennaStructure(struct) struct = struct.toPairs() return struct
def to_pairs(struct=None): """ Converts a vienna structure into a pairs object Returns pairs object pairs functions tested in test for rna2d.py """ struct = ViennaStructure(struct) pairs = struct.toPairs() return pairs
def convert_to_vienna(data): """ Converts into vienna dot bracket format, >< to () """ try: return toPairs(ViennaStructure(data.translate(cove_to_vienna_table))) except IndexError: return ''
def test_structureMatches_hh(self): """Test of hammerhead structureMatch should work correctly""" index = '0123456789012345678901234567890123456' seq_good = 'CCCCUAGGGGCCCCCUGAAGAGAAAUUUCGAAAGGGG' seq_bad ='CCCCCAGGGGCCCCCUGAAGAGAAAUUUCGAAGGGGG' structure ='(((((.(((()))).......(((())))...)))))' struct = ViennaStructure(structure) self.assertEqual(self.hh.structureMatches(struct, [0, 10, 25]), True) self.assertEqual(self.hh.structureMatches(struct, [0, 10, 25]), True)
def convert_to_pairs(data): """ Converts format >< to () format, viennaformat. """ try: vienna = ViennaStructure(data.translate(to_vienna_table)) return toPairs(vienna) except IndexError: return ''
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), '...(((....)))..')
def test_collapse(self): """StructureNode collapse should collapse consecutive pairs from self""" one = ViennaStructure('(.)').toTree() self.assertEqual(one.collapse(), False) self.assertEqual(str(one), '(.)') two = ViennaStructure('((.))').toTree() #can't collapse root node self.assertEqual(two.collapse(), False) #should be able to collapse next node self.assertEqual(two[0].collapse(), True) self.assertEqual((two[0].Start, two[0].End, two[0].Length), (0,4,2)) self.assertEqual(str(two), '((.))') three = ViennaStructure('(((...)))..').toTree() self.assertEqual(three[0].collapse(), True) self.assertEqual((three[0].Start, three[0].End, three[0].Length), \ (0,8,3)) self.assertEqual(str(three), '(((...)))..') self.assertEqual(three[0].collapse(), False) self.assertEqual(three[-1].collapse(), False) oh = self.OneHelix self.assertEqual(oh[0].collapse(), True) self.assertEqual(str(oh), '((((()))))')
def find_struct(lines): """Finds structures in output data""" struct = '' name1 = '' name2 = '' seq1 = '' seq2 = '' result = [] for line in lines: if line.startswith('; ========'): break if line.startswith('; ALIGNING'): line = line.split() name1 = line[2] name2 = line[4] continue if line.startswith('; ALIGN %s' % name1): line = line.split()[3:] line = ''.join(line) seq1 = ''.join([seq1, line]) continue if line.startswith('; ALIGN %s' % name2): line = line.split()[3:] line = ''.join(line) seq2 = ''.join([seq2, line]) continue if line.startswith('; ALIGN Structure'): line = line.split()[3:] line = ''.join(line) struct = ''.join([struct, line]) continue struct = ViennaStructure(struct).toPairs() struct.sort() result.append([struct, seq1, seq2]) return result
def find_struct(lines): """Finds structures in output data""" struct = '' name1 = '' name2 = '' seq1 = '' seq2 = '' result = [] for line in lines: if line.startswith('; ========'): break if line.startswith('; ALIGNING'): line = line.split() name1 = line[2] name2 = line[4] continue if line.startswith('; ALIGN %s' % name1): line = line.split()[3:] line = ''.join(line) seq1 = ''.join([seq1,line]) continue if line.startswith('; ALIGN %s' % name2): line = line.split()[3:] line = ''.join(line) seq2 = ''.join([seq2,line]) continue if line.startswith('; ALIGN Structure'): line = line.split()[3:] line = ''.join(line) struct = ''.join([struct,line]) continue struct = ViennaStructure(struct).toPairs() struct.sort() result.append([struct,seq1,seq2]) return result
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), '.(((((.)))))..')
def test_classify(self): """classify() should classify valid structures correctly""" Empty = '' NoPairs = '.....' OneHelix = '((((()))))' ManyHelices = '(..(((...)).((.(((((..))).)))..((((..))))))...)' Ends = '..(.)..' FirstEnd = '..((()))' LastEnd = '((..((.))))...' Internal = '(((...)))..((.)).' #following structure is from p 25 of Eddy's WUSS description manual Eddy = '..((((.(((...)))...((.((....))..)).)).))' structs = [Empty, NoPairs, OneHelix, ManyHelices, Ends, \ FirstEnd, LastEnd, Internal, Eddy] EmptyResult = '' NoPairsResult = 'EEEEE' OneHelixResult = 'SSSSSSSSSS' ManyHelicesResult = 'SBBSSSLLLSSJSSBSSSSSLLSSSBSSSJJSSSSLLSSSSSSBBBS' EndsResult = 'EESLSEE' FirstEndResult = 'EESSSSSS' LastEndResult = 'SSBBSSLSSSSEEE' InternalResult = 'SSSLLLSSSFFSSLSSE' #following structure is from p 25 of Eddy's WUSS description manual Eddy = 'EESSSSJSSSLLLSSSJJJSSBSSLLLLSSBBSSJSSBSS' results = [ EmptyResult, NoPairsResult, OneHelixResult, ManyHelicesResult, EndsResult, FirstEndResult, LastEndResult, InternalResult, Eddy ] for s, r in zip(structs, results): c = classify(s) self.assertEqual(classify(s), r) long_struct = ".((((((((((((((.((((((..((((.....)))))))))).))..))))))))))))....(((.((((.((((((((......((((((.((..(((((((....)))).)))..))))))))...))))))))...........(((((.(..(((((((((......((((((((((((.........))))))))))))....))))).))))..)..)))))..(((((((((((((((((((......(((((((((((((((((((((((((((((((...(((.......((((((((........)))))))).......)))...))))))))))))))))))))))))))))))).((((........(((((((((((((((((((...))))))))))))))))))).......)))).....((((((((((((((((((((((((((((((.(((...))).)))))))))))))))))))))))...........))))))).))))))))))))))))))).....)))).)))......" #compare standalone method with classification from tree c = classify(long_struct) d = ViennaStructure(long_struct).toTree().classify() self.assertEqual(c, d) #Error is raised when trying to classify invalid structures invalid_structure = '(((..)).))))(...)(...' self.assertRaises(IndexError, classify, invalid_structure)
def setUp(self): """Instantiate some standard ViennaNodes.""" self.EmptyStr = '' self.NoPairsStr = '.....' self.OneHelixStr = '((((()))))' self.ManyHelicesStr = '(..(((...)).((.(((((..))).)))..((((..))))))...)' self.EndsStr = '..(.)..' self.FirstEndStr = '..((()))' self.LastEndStr = '((..((.))))...' self.InternalStr = '(((...)))..((.)).' #following structure is from p 25 of Eddy's WUSS description manual self.EddyStr = '..((((.(((...)))...((.((....))..)).)).))' #add in the tree versions by deleting trailing 'Str' for s in list(self.__dict__.keys()): if s.endswith('Str'): self.__dict__[s[:-3]] = \ ViennaStructure(self.__dict__[s]).toTree()
def plot_from_seq_and_struct(seq, struct,seqname=None, params=None): """Returns plot of structure given seq and struct. - seq: sequence corresponding to struct. - Can be anything that behaves as a string same length as struct. - struct: Vienna structure corresponding to seq. Must be a valid Vienna structure. """ seq = str(seq) #Construct ViennaStructure. Object will handle errors in struct string. struct = ViennaStructure(struct) if len(seq) != len(struct): raise DataError, 'Sequence and structure are not same length!' app = RNAplot(WorkingDir='/tmp',\ params=params) if seqname is None: seqname = app._get_tmp_seqname() res = app(['>'+seqname,seq,struct]) plot = res[seqname+'_ss'].read() res.cleanUp() return plot
def test_extendHelices(self): """StructureNode extendHelices should extend all helices""" e = ViennaStructure('........') t = e.toTree() t.extendHelices(Rna('CCCCCCCCCC')) self.assertEqual(str(t), e) #no pairs if sequence can't form them s = ViennaStructure('(.....(...)..)...((.....))...') r = Rna('AAAAAAAAAAAAAAAAAAAAAAAAAAAAA') t = s.toTree() t.extendHelices(r) self.assertEqual(str(t), s) #should be able to extend a single helix s = ViennaStructure('(.....(...)..)...((.....))...') r = Rna('CAAAAACAAAGAAGCCCCCCCAGGGGGGG') t = s.toTree() t.extendHelices(r) self.assertEqual(str(t), '(.....(...)..)((((((...))))))') #should be able to extend multiple helices s = ViennaStructure('(.....(...)..)...((.....))...') r = Rna('AAAAACCCAGGGUUCCCCCAUAAAGGGAA') t = s.toTree() t.extendHelices(r) self.assertEqual(str(t), '((...((...))))..(((.....)))..')
def test_collapse(self): """StructureNode collapse should collapse consecutive pairs from self""" one = ViennaStructure('(.)').toTree() self.assertEqual(one.collapse(), False) self.assertEqual(str(one), '(.)') two = ViennaStructure('((.))').toTree() #can't collapse root node self.assertEqual(two.collapse(), False) #should be able to collapse next node self.assertEqual(two[0].collapse(), True) self.assertEqual((two[0].Start, two[0].End, two[0].Length), (0, 4, 2)) self.assertEqual(str(two), '((.))') three = ViennaStructure('(((...)))..').toTree() self.assertEqual(three[0].collapse(), True) self.assertEqual((three[0].Start, three[0].End, three[0].Length), \ (0,8,3)) self.assertEqual(str(three), '(((...)))..') self.assertEqual(three[0].collapse(), False) self.assertEqual(three[-1].collapse(), False) oh = self.OneHelix self.assertEqual(oh[0].collapse(), True) self.assertEqual(str(oh), '((((()))))')
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), '((..)).')
def to_pairs(list=None,pseudo=True): """ Converts nupack structure string into pairs object pseudoknotted and not pseudoknotted. """ tmp = list[1] pairs = [] if list.__contains__('pseudoknotted!'): #since pseudoknotted is denoted by {} it divides into {} and () string #they are then turned into pairs lists seperatly and then the lists are #joined to form the complete set of pairs first = second = tmp first = ViennaStructure(first.translate(curly_to_dots_table)) second = second.translate(bracket_to_dots_table) second = ViennaStructure(second.translate(curly_to_bracket_table)) pairs = first.toPairs() pairs.extend(second.toPairs()) pairs.sort() if not pseudo: pairs = opt_single_random(pairs) pairs.sort() else: structure = ViennaStructure(tmp.translate(curly_to_bracket_table)) pairs = structure.toPairs() pairs.sort() return pairs
def to_pairs(struct=None): """ Converts structure string in to a pairs object. Starts by checking for pseudoknots if pseudoknotted it translates each steam in to vienna notation and from there makes a pairs object. Each pairs object is then joined to form the final pairs object of the entire structure Returns a tuple of the pairs object and the energy """ primary = first = second = struct.split(None, 2)[0] energy = atof(struct.split(None, 2)[1].strip('()')) if struct.__contains__('['): #Checks for first pseudoknot steam primary = ViennaStructure(primary.translate(primary_table)) first = ViennaStructure(first.translate(first_table)) pairs = primary.toPairs() pairs.extend(first.toPairs()) #Adds the first steam to pairs object if struct.__contains__('{'): #Checks for second pseudo steam second = ViennaStructure(second.translate(second_table)) pairs.extend(second.toPairs()) else: primary = ViennaStructure(primary.translate(primary_table)) pairs = primary.toPairs() return pairs, energy
def to_pairs(struct=None): """ Converts structure string in to a pairs object. Starts by checking for pseudoknots if pseudoknotted it translates each steam in to vienna notation and from there makes a pairs object. Each pairs object is then joined to form the final pairs object of the entire structure Returns a tuple of the pairs object and the energy """ primary = first = second = struct.split(None,2)[0] energy = atof(struct.split(None,2)[1].strip('()')) if struct.__contains__('['): #Checks for first pseudoknot steam primary = ViennaStructure(primary.translate(primary_table)) first = ViennaStructure(first.translate(first_table)) pairs = primary.toPairs() pairs.extend(first.toPairs()) #Adds the first steam to pairs object if struct.__contains__('{'): #Checks for second pseudo steam second = ViennaStructure(second.translate(second_table)) pairs.extend(second.toPairs()) else: primary = ViennaStructure(primary.translate(primary_table)) pairs = primary.toPairs() return pairs,energy
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), '((..)).')