File: test_rna_fold.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (167 lines) | stat: -rw-r--r-- 4,788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
"""Tests for the RNAfold dot plot parser
"""
from __future__ import division
from cogent.util.unit_test import TestCase, main
from cogent.parse.rna_fold import *

__author__ = "Jeremy Widmann"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Jeremy Widmann", "Rob Knight"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Jeremy Widmann"
__email__ = "jeremy.widmann@colorado.edu"
__status__ = "Production"

class RnaFoldParserTests(TestCase):
    """Tests for RnaFoldParser.
    """

    def setUp(self):
        """Setup function for RnaFoldParser tests.
        """
        self.rna_fold_parser_results = ('ACGUGCUAG',
            [(1, 7, float(0.01462)),
            (2, 9, float(0.11118)),
            (3, 7, float(0.00985)),
            (4, 8, float(0.01005)),
            (4, 9, float(0.01586))])
        
        self.sequence_lines = ['/sequence line before where sequences start\n',
            '  ACCUGUCUAUCGCUGC&*$#@(*\n',
            'ACGGUUAUAUUAUCUCUG\\\n',
            ') end of sequence\n']
        self.sequence_lines_empty = ['/sequence \n',
            '\n',
            ')\n']
        self.index_lines = ['unimportant line',
            '1 3 0.332 ubox',
            '1 4 0.003 ubox']
        self.index_lines_no_ubox = ['unimportant line',
            '1 2 0.432 u box',
            '1 4 0.32 ubo x']
    
    def test_getSequence(self):
        self.assertEqual(getSequence(self.sequence_lines),
            'ACCUGUCUAUCGCUGC&*$#@(*ACGGUUAUAUUAUCUCUG')
        self.assertEqual(getSequence(self.sequence_lines_empty),'')
    
    def test_getIndices(self):
        self.assertEqual(getIndices(self.index_lines),[(1,3,float(0.332)),
                                                        (1,4,float(0.003))])
        self.assertEqual(getIndices(self.index_lines_no_ubox),[])
    
    def test_RnaFoldParser(self):
        self.assertEqual(RnaFoldParser([]), ('',[]))
        self.assertEqual(RnaFoldParser(RNA_FOLD_RESULTS),
            self.rna_fold_parser_results)


RNA_FOLD_RESULTS = ['%!PS-Adobe-3.0 EPSF-3.0\n',
 '%%Title: RNA DotPlot\n',
 '%%Creator: PS_dot.c,v 1.24 2003/08/07 09:01:00 ivo Exp $, ViennaRNA-1.5\n',
 '%%CreationDate: Fri Oct  8 13:15:01 2004\n',
 '%%BoundingBox: 66 211 518 662\n',
 '%%DocumentFonts: Helvetica\n',
 '%%Pages: 1\n',
 '%%EndComments\n',
 '\n',
 '%Options: \n',
 '%This file contains the square roots of the base pair probabilities in the form\n',
 '% i  j  sqrt(p(i,j)) ubox\n',
 '100 dict begin\n',
 '\n',
 '/logscale false def\n',
 '\n',
 '%delete next line to get rid of title\n',
 '270 665 moveto /Helvetica findfont 14 scalefont setfont (dot.ps) show\n',
 '\n',
 '/lpmin {\n',
 '   1e-05 log  % log(pmin) only probs>pmin will be shown\n',
 '} bind def\n',
 '\n',
 '/box { %size x y box - draws box centered on x,y\n',
 '   2 index 0.5 mul add            % x += 0.5\n',
 '   exch 2 index 0.5 mul add exch  % x += 0.5\n',
 '   newpath\n',
 '   moveto\n',
 '   dup neg   0 rlineto\n',
 '   dup neg   0 exch rlineto\n',
 '             0 rlineto\n',
 '   closepath\n',
 '   fill\n',
 '} bind def\n',
 '\n',
 '/sequence { (\\\n',
 'ACGUGCUAG\\\n',
 ') } def\n',
 '/len { sequence length } bind def\n',
 '\n',
 '/ubox {\n',
 '   logscale {\n',
 '      log dup add lpmin div 1 exch sub dup 0 lt { pop 0 } if\n',
 '   } if\n',
 '   3 1 roll\n',
 '   exch len exch sub 1 add box\n',
 '} bind def\n',
 '\n',
 '/lbox {\n',
 '   3 1 roll\n',
 '   len exch sub 1 add box\n',
 '} bind def\n',
 '\n',
 '72 216 translate\n',
 '72 6 mul len 1 add div dup scale\n',
 '/Helvetica findfont 0.95 scalefont setfont\n',
 '\n',
 '% print sequence along all 4 sides\n',
 '[ [0.7 -0.3 0 ]\n',
 '  [0.7 0.7 len add 0]\n',
 '  [0.7 -0.2 90]\n',
 '  [-0.3 len sub 0.7 len add -90]\n',
 '] {\n',
 '  gsave\n',
 '    aload pop rotate translate\n',
 '    0 1 len 1 sub {\n',
 '     dup 0 moveto\n',
 '     sequence exch 1 getinterval\n',
 '     show\n',
 '    } for\n',
 '  grestore\n',
 '} forall\n',
 '\n',
 '0.5 dup translate\n',
 '% draw diagonal\n',
 '0.04 setlinewidth\n',
 '0 len moveto len 0 lineto stroke \n',
 '\n',
 '%draw grid\n',
 '0.01 setlinewidth\n',
 'len log 0.9 sub cvi 10 exch exp  % grid spacing\n',
 'dup 1 gt {\n',
 '   dup dup 20 div dup 2 array astore exch 40 div setdash\n',
 '} { [0.3 0.7] 0.1 setdash } ifelse\n',
 '0 exch len {\n',
 '   dup dup\n',
 '   0 moveto\n',
 '   len lineto \n',
 '   dup\n',
 '   len exch sub 0 exch moveto\n',
 '   len exch len exch sub lineto\n',
 '   stroke\n',
 '} for\n',
 '0.5 neg dup translate\n',
 '\n',
 '1 7 0.01462 ubox\n',
 '2 9 0.11118 ubox\n',
 '3 7 0.00985 ubox\n',
 '4 8 0.01005 ubox\n',
 '4 9 0.01586 ubox\n',
 'showpage\n',
 'end\n',
 '%%EOF\n']
     
#run if called from command-line
if __name__ == "__main__":
    main()