1import RNApath
2
3RNApath.addSwigInterfacePath(3)
4
5
6import RNA
7import unittest
8from struct import *
9import locale
10
11locale.setlocale(locale.LC_ALL, 'C')
12
13seq1      = "CGCAGGGAUACCCGCG"
14struct1   = "(((.(((...))))))"
15
16struct11  = "(((.((.....)))))"
17seq2      = "GCGCCCAUAGGGACGC"
18struct2   = "((((((...))).)))"
19seq3      = "GCGCACAUAGUGACGC"
20struct3   = "(..(((...)))...)"
21align     = [seq1,seq2,seq3]
22(struct, mfe) = RNA.fold(seq1)
23
24
25class GeneralTests(unittest.TestCase):
26    def test_version(self):
27        print("test version number")
28        self.assertEqual(RNA.__version__, RNApath.VERSION_NUMBER)
29
30    def test_hammingDistance(self):
31        print("test_hammingDistance \t calculate a hamming distance")
32        self.assertEqual(RNA.hamming(seq1,seq2),16)
33        self.assertEqual(RNA.bp_distance(struct1,struct2),6)
34
35
36    def test_temperature(self):
37        print("test_temperature")
38        self.assertEqual(RNA.cvar.temperature,37)
39
40
41    def test_foldASequence(self):
42        print("test_foldASequence")
43        # new better interface
44        (struct, mfe) = RNA.fold(seq1)
45        self.assertEqual(struct,struct1)
46        # check energy
47        self.assertTrue(abs(RNA.energy_of_struct(seq1, struct1) - mfe) < 0.0001)
48
49
50    def test_constrained_folding(self):
51        print("test_constrained_folding")
52        RNA.cvar.fold_constrained = 1
53        (struct,cmfe) = RNA.fold(seq1,"....xx....xx....")
54        self.assertEqual(struct,'(((..........)))')
55        self.assertTrue(abs(RNA.energy_of_struct(seq1, struct) - cmfe) < 0.0001)
56        RNA.cvar.fold_constrained = 0
57
58    def test_tree_distance(self):
59        print("test_tree_distance");
60        xstruc = RNA.expand_Full(struct1)
61        T1 = RNA.make_tree(xstruc)
62        xstruc = RNA.expand_Full(struct2)
63        T2 = RNA.make_tree(xstruc)
64        RNA.edit_backtrack = 1
65        tree_dist = RNA.tree_edit_distance(T1, T2)
66        # print RNA::get_aligned_line(0), RNA::get_aligned_line(1),"\n"
67        self.assertEqual(tree_dist,2)
68
69
70    def test_cofold_andMore(self):
71        print("test_cofold")
72        RNA.cvar.cut_point = len(seq1)+1
73        (costruct,comfe) = RNA.cofold(seq1 + seq2)
74        self.assertEqual(costruct,'(((.(((...))))))((((((...))).)))')
75        cmfe = RNA.energy_of_struct(seq1 + seq2, costruct)
76        self.assertTrue(abs(comfe - cmfe) < 1e-5)
77        (x,ac,bc,fcab,cf) = RNA.co_pf_fold(seq1 + seq2, struct)
78        self.assertTrue(cf<comfe)
79        self.assertTrue(comfe-cf<1.3)
80
81        (x,usel1, usel2, fcaa, usel3)= RNA.co_pf_fold(seq1 + seq1, struct)
82        RNA.cvar.cut_point = len(seq2)+1
83        (x,usel1, usel2, fcbb, usel3)= RNA.co_pf_fold(seq2 + seq2, struct)
84        (AB,AA,BB,A,B) = RNA.get_concentrations(fcab, fcaa, fcbb,ac, bc, 1e-5, 1e-5)
85        AB/=2e-5
86        AA/=2e-5
87        BB/=2e-5
88        A/=2e-5
89        B/=2e-5
90
91        ret = (abs(AB-0.0)+abs(AA-0.00578)+abs(BB-0.01100)+abs(A-0.48843)+abs(B-0.47801))
92        self.assertTrue(ret<0.0001)
93        RNA.cvar.cut_point=-1
94        #pf_fo ld
95        s,f = RNA.pf_fold(seq1,struct)
96        self.assertTrue(f < mfe)
97        self.assertTrue(mfe-f < 0.8)
98
99        print("test_bp_prop")
100
101        p1 = RNA.get_pr(2,15)
102        ii = RNA.intP_getitem(RNA.cvar.iindx, 2)
103
104        if(RNA.pf_float_precision() != 0) :
105             p2 = RNA.floatP_getitem(RNA.cvar.pr, ii-15)
106        else:
107             p2 = RNA.doubleP_getitem(RNA.cvar.pr, ii-15)
108        self.assertTrue(p1 < 0.999 and abs(p1-p2) < 1.2e-7)
109
110
111    def test_parse_structure(self):
112        print("test_parse_structure")
113        RNA.parse_structure(struct1)
114        self.assertEqual(RNA.cvar.loops,2)
115        self.assertEqual(RNA.cvar.pairs,6)
116        self.assertEqual(RNA.cvar.unpaired,4)
117        self.assertEqual(RNA.intP_getitem(RNA.cvar.loop_degree,1),2)
118
119    def test_rna_plots(self):
120        print("test_rna_plots")
121        RNA.PS_rna_plot(seq1, struct1, "test_ss.ps")
122        anote = "2 15 1 gmark\n" + "3 cmark\n"
123        RNA.PS_rna_plot_a(seq1, struct1, "test_ss_a.ps", None, anote)
124        RNA.PS_dot_plot(seq1, "test_dp.ps")
125        RNA.ssv_rna_plot(seq1, struct, "test.coord")
126        print("please check the two postscript files test_ss.ps and test_dp.ps")
127        RNA.write_parameter_file("test.par")
128
129
130    def test_different_symbol_set(self):
131        print("test_different_symbol_set")
132        RNA.cvar.symbolset = "GC"
133        start = RNA.random_string(len(struct1), "GC")
134        (sinv, cost) = RNA.inverse_fold(start, struct1)
135        (ss, en) = RNA.fold(sinv)
136        self.assertEqual(ss, struct1)
137
138
139    def test_eos_dimer(self):
140        print("test_eos_dimer")
141
142        RNA.cvar.cut_point = 3
143        e =  RNA.energy_of_struct("GCGC", "(())")
144        RNA.cvar.cut_point = -1
145        self.assertEqual(int(e*100+0.5), 70)
146
147
148    def test_duplexfold(self):
149        print("testing duplexfold()")
150        duplex = RNA.duplexfold(seq1, seq2)
151        self.assertEqual(duplex.structure, ".(((.....(((.&)))))).")
152
153
154    def test_alifold(self):
155        print("testing alifold()")
156        align = ["GCCAUCCGAGGGAAAGGUU", "GAUCGACAGCGUCU-AUCG", "CCGUCUUUAUGAGUCCGGC"]
157
158        (css, cen) = RNA.alifold(align)
159        self.assertEqual(css,"(((.(((...)))..))).")
160        self.assertEqual(RNA.consens_mis(align), "SMBHBHYDRBGDVWmVKBB")
161        RNA.free_alifold_arrays()
162
163
164    def test_moveSets(self):
165        print("test_moveSets")
166
167        RNA.cvar.cut_point=-1
168        struct1_move = "(..............)"
169        #move_standard( sequence, start structure, move_type(GRADIENT, FIRST, ADAPTIVE), verbosity, shifts, noLP)
170        (s,energy) = RNA.move_standard(seq1, struct1_move, 0, 1, 0, 0)
171        print("energy = ",energy," s = ", s);
172        self.assertEqual(s, "................")
173
174        struct1_move = "(..............)"
175        (s,energy) =  RNA.move_standard(seq1, struct1_move, 1, 1, 0, 0)
176        self.assertEqual(s, "(((.((....)).)))")
177
178
179    def test_simplexycoordinates(self):
180        print("test_simplexycoordinates")
181
182        coords = RNA.simple_xy_coordinates(struct1)
183
184        for c in (coords):
185            print(c.X, ",", c.Y)
186
187
188    def test_model_details_structure(self):
189        print("test_model_details_parameter_structure")
190
191        # check model details structure
192        md = RNA.md();
193        self.assertEqual(int(md.dangles), 2)
194        self.assertEqual(md.temperature, 37.0)
195
196        RNA.cvar.dangles     = 0
197        RNA.cvar.temperature = 40.1
198        md = RNA.md()
199        self.assertEqual(int(md.dangles), 0)
200        self.assertEqual(int(RNA.cvar.dangles), 0)
201        self.assertEqual(md.temperature, 40.1)
202
203        #reset globals to default
204        RNA.cvar.dangles = 2
205        RNA.cvar.temperature= 37.0
206
207        # check parameter structures
208        params = RNA.param()
209        self.assertEqual(params.temperature,37.0)
210        params = RNA.param(md)
211        self.assertEqual(params.temperature,40.1)
212
213        pf_params = RNA.exp_param()
214        self.assertEqual(pf_params.temperature,37.0)
215        pf_params = RNA.exp_param(md)
216        self.assertEqual(pf_params.temperature,40.1)
217        md = None
218
219
220class FoldCompoundTest(unittest.TestCase):
221
222    def test_create_fold_compound_Single(self):
223        print("test_create_fold_compound_Single")
224
225        fc = RNA.fold_compound(seq1)
226        self.assertEqual(fc.type, RNA.FC_TYPE_SINGLE)
227
228
229    def test_create_fold_compound_Align(self):
230        print("test_create_fold_compound_Align")
231        fc= RNA.fold_compound(align)
232        self.assertEqual(fc.type, RNA.FC_TYPE_COMPARATIVE)
233
234
235    def test_create_fold_compound_2D(self):
236        print("test_create_fold_compound_2D")
237        fc= RNA.fold_compound(seq1,seq2,seq3)
238        self.assertTrue(fc)
239
240
241    ###centroid.h
242    def test_centroid(self):
243        print("test_centroid")
244        fc=RNA.fold_compound(align)
245        fc.pf()
246        (sc,dist) = fc.centroid()
247        print(sc, "\tDistance of :  %6.2f" % dist)
248        self.assertTrue(sc and dist)
249
250
251    ## partition function from here
252    def test_pf(self):
253        print("test_pf")
254        fc= RNA.fold_compound(seq1)
255        (ss,gfe) = fc.pf()
256        print(ss, "[ %6.2f ]" % gfe)
257        self.assertTrue(ss)
258        bp_dis = fc.mean_bp_distance()
259        print(seq1, "\t meanBPDistance : ", bp_dis)
260        self.assertTrue(bp_dis)
261
262
263    def test_pf_dimer(self):
264        print("testing pf_dimer() method")
265        fc = RNA.fold_compound(seq1 + "&" + seq2)
266        (costruct, comfe) = fc.mfe_dimer()
267        self.assertEqual(costruct, "(((.(((...))))))((((((...))).)))")
268        cmfe = fc.eval_structure(costruct)
269        self.assertTrue(abs(comfe-cmfe) < 1e-5)
270
271        (x,ac,bc,fcab,cf) = fc.pf_dimer()
272        self.assertTrue((cf < comfe) and (comfe - cf < 1.3))
273
274
275if __name__ == '__main__':
276    unittest.main()
277
278