1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """
24 utility funtions for Mod package
25 """
26
27 import os.path
28 import types
29 import Biskit.molUtils as MU
30 import Biskit.tools as T
31
33 """
34 Parse the chaim index file written by TemplateSearcher
35 L{TemplateSearcher.F_CHAIN_INDEX}.
36
37 @param fname: name of file to parse
38 @type fname: str
39
40 @return: key : value mapping
41 @rtype: {key:value}
42 """
43 f = open( fname )
44
45 result = {}
46 for l in f:
47 if not l[0] == '#':
48
49 try:
50 fname, chain_id = l.split()
51
52 if not os.path.exists(fname):
53 fname = '%s/%s'%(T.testRoot(), fname)
54
55 if not len(fname) == 0:
56 result[ fname ] = chain_id
57
58 except:
59 fname = l.strip()
60
61 if not os.path.exists(fname):
62 fname = '%s/%s'%(T.testRoot(), fname)
63
64 result[ fname ] = ''
65
66 f.close()
67
68 return result
69
70
94
95
97 """
98 Verify that a given file or string is in Fasta format.
99 The definition used for a fasta file here is that:
100 - first line starts with '>'
101 - the following sequence lines are not longer that 80 characters
102 - the characters has to belong to the standard amino acid codes
103
104 @param target: name of fasta file OR file contents as list of strings
105 @type target: str OR [str]
106
107 @return: conforms to the fsata format
108 @rtype: True/False
109 """
110 if not type(target) == types.ListType:
111 if os.path.exists( target ):
112 f = open( target, 'r' )
113 target = f.readlines()
114
115 if not target[0][0] == '>':
116 print 'Fasta format does not contain description line.'
117 return False
118
119 for i in range( 1, len(target) ):
120 if len( target[i] ) >= 80:
121 print 'Fasta sequence lines longer that 80 characters'
122 return False
123
124 for j in target[i]:
125 aa_codes = MU.aaDicStandard.values() + [ '\n' ]
126 if not j.upper() in aa_codes:
127 print 'Invalid amino acid code: %s'%j.upper()
128 return False
129
130 return True
131
132
133
134
135
136
137
138
140 """
141 Test class
142 """
143
144 - def run( self, local=0 ):
145 """
146 run function test
147
148 @param local: transfer local variables to global and perform
149 other tasks only when run locally
150 @type local: 1|0
151
152 @return: 1
153 @rtype: int
154 """
155 vf = verify_fasta( T.testRoot() + '/Mod/project/target.fasta')
156
157 if local:
158 globals().update( locals() )
159
160 return vf
161
162
164 """
165 Precalculated result to check for consistent performance.
166
167 @return: 1
168 @rtype: int
169 """
170 return 1
171
172
173 if __name__ == '__main__':
174
175 test = Test()
176
177 assert test.run( local=1 ) == test.expected_result()
178