-
Notifications
You must be signed in to change notification settings - Fork 4
/
bioioTest.py
308 lines (264 loc) · 11 KB
/
bioioTest.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
#Copyright (C) 2006-2012 by Benedict Paten ([email protected])
#
#Released under the MIT license, see LICENSE.txt
import unittest
import os
import sys
import random
import math
import io
from sonLib.bioio import getTempFile
from sonLib.bioio import getTempDirectory
from sonLib.bioio import TempFileTree
from sonLib.bioio import getRandomAlphaNumericString
from sonLib.bioio import fastaRead
from sonLib.bioio import fastaWrite
from sonLib.bioio import fastqRead
from sonLib.bioio import fastqWrite
from sonLib.bioio import getRandomSequence
from sonLib.bioio import pWMRead
from sonLib.bioio import pWMWrite
from sonLib.bioio import newickTreeParser
from sonLib.bioio import printBinaryTree
from sonLib.bioio import cigarRead
from sonLib.bioio import cigarWrite
from sonLib.bioio import PairwiseAlignment
from sonLib.bioio import getRandomPairwiseAlignment
from sonLib.bioio import TestStatus
from sonLib.bioio import system
from sonLib.bioio import logger
class TestCase(unittest.TestCase):
def setUp(self):
self.testNo = TestStatus.getTestSetup()
unittest.TestCase.setUp(self)
self.tempDir = getTempDirectory(os.getcwd())
self.tempFiles = []
def tearDown(self):
unittest.TestCase.tearDown(self)
system("rm -rf %s" % self.tempDir)
for tempFile in self.tempFiles:
os.remove(tempFile)
#########################################################
#########################################################
#########################################################
#temp file functions
#########################################################
#########################################################
#########################################################
def testTempFileTree(self):
for test in range(100): #self.testNo):
levels = random.choice(range(1, 4))
fileNo = random.choice(range(1, 6))
maxTempFiles = int(math.pow(fileNo, levels))
print("Got %s levels, %s fileNo and %s maxTempFiles" % (levels, fileNo, maxTempFiles))
tempFileTreeRootDir = os.path.join(self.tempDir, getRandomAlphaNumericString())
tempFileTree = TempFileTree(tempFileTreeRootDir, fileNo, levels)
tempFiles = []
tempDirs = []
#Check we can mac number of temp files.
for i in range(maxTempFiles):
if random.random() > 0.5:
tempFile = tempFileTree.getTempFile()
assert os.path.isfile(tempFile)
tempFiles.append(tempFile)
else:
tempFile = tempFileTree.getTempDirectory()
assert os.path.isdir(tempFile)
tempDirs.append(tempFile)
#Check assertion is created
try:
tempFileTree.getTempFile()
assert False
except RuntimeError:
logger.debug("Got expected error message")
#Now remove a few temp files
while random.random() > 0.1 and len(tempFiles) > 0:
tempFile = tempFiles.pop()
assert os.path.isfile(tempFile)
tempFileTree.destroyTempFile(tempFile)
assert not os.path.isfile(tempFile)
#Now remove a few temp dirs
while random.random() > 0.1 and len(tempDirs) > 0:
tempDir = tempDirs.pop()
assert os.path.isdir(tempDir)
tempFileTree.destroyTempDir(tempDir)
assert not os.path.isdir(tempDir)
#Check temp files is okay
set(tempFileTree.listFiles()) == set(tempFiles + tempDirs)
#Either remove all the temp files or just destroy the whole thing
if random.random() > 0.5:
#Remove all temp files and check thing is empty.
for tempFile in tempFiles:
tempFileTree.destroyTempFile(tempFile)
for tempDir in tempDirs:
tempFileTree.destroyTempDir(tempDir)
os.remove(os.path.join(tempFileTreeRootDir, "lock"))
os.rmdir(tempFileTreeRootDir)
else:
tempFileTree.destroyTempFiles()
assert not os.path.isdir(tempFileTreeRootDir)
#########################################################
#########################################################
#########################################################
#fasta functions
#########################################################
#########################################################
#########################################################
def testFastaReadWrite(self):
tempFile = getTempFile()
self.tempFiles.append(tempFile)
for test in range(0, self.testNo):
fastaNumber = random.choice(range(10))
l = [ getRandomSequence() for i in range(fastaNumber) ]
fileHandle = open(tempFile, 'w')
for name, seq in l:
fastaWrite(fileHandle, name, seq)
fileHandle.close()
fileHandle = open(tempFile, 'r')
l.reverse()
outFh = io.StringIO()
for i in fastaRead(fileHandle):
assert i == l.pop()
name, seq = i
fastaWrite(outFh, name, seq)
outFh.close()
fileHandle.close()
def testFastqReadWrite(self):
tempFile = getTempFile()
self.tempFiles.append(tempFile)
for test in range(0, self.testNo):
fastaNumber = random.choice(range(10))
fastqs = [ (name, seq, [ random.randint(33, 126) for i in range(len(seq)) ]) for name, seq in [ getRandomSequence() for i in range(fastaNumber) ]]
fH = open(tempFile, 'w')
for name, seq, quals in fastqs:
fastqWrite(fH, name, seq, quals)
fH.close()
fastqs.reverse()
outFh = io.StringIO()
for i in fastqRead(tempFile):
assert i == fastqs.pop()
name, seq, quals = i
fastqWrite(outFh, name, seq, quals)
outFh.close()
def testFastaReadWriteC(self):
"""Tests consistency with C version of this function.
"""
tempFile = getTempFile()
self.tempFiles.append(tempFile)
tempFile2 = getTempFile()
self.tempFiles.append(tempFile2)
for test in range(0, self.testNo):
fastaNumber = random.choice(range(10))
l = [ getRandomSequence() for i in range(fastaNumber) ]
fileHandle = open(tempFile, 'w')
for name, seq in l:
fastaWrite(fileHandle, name, seq)
fileHandle.close()
command = "sonLib_fastaCTest %s %s" % (tempFile, tempFile2)
print(command)
system(command)
fileHandle = open(tempFile2, 'r')
l.reverse()
outFh = io.StringIO()
for i in fastaRead(fileHandle):
name, seq = i
assert i == l.pop()
fastaWrite(outFh, name, seq)
outFh.close()
fileHandle.close()
#########################################################
#########################################################
#########################################################
#newick tree functions
#########################################################
#########################################################
#########################################################
def testNewickTreeParser(self):
if self.testNo > 0:
d = '((human,baboon),chimp);'
e = newickTreeParser(d)
f = printBinaryTree(e, False)
print(d, f)
assert d == f
def testNewickTreeParser_UnaryNodes(self):
#tests with unary nodes
for test in range(0, self.testNo):
tree = getRandomTreeString()
logger.debug("tree to try\t", tree)
tree2 = newickTreeParser(tree, reportUnaryNodes=True)
tree3 = printBinaryTree(tree2, True)
logger.debug("tree found\t", tree3)
assert tree == tree3
#########################################################
#########################################################
#########################################################
#functions for position weight matrices
#########################################################
#########################################################
#########################################################
def testPWMParser(self):
tempFile = getTempFile()
self.tempFiles.append(tempFile)
for test in range(0, self.testNo):
pWM = getRandomPWM()
fileHandle = open(tempFile, 'w')
pWMWrite(fileHandle, pWM)
fileHandle.close()
fileHandle = open(tempFile, 'r')
pWM2 = pWMRead(fileHandle)
fileHandle.close()
for i in range(0, len(pWM)):
pWM[i] == pWM2[i]
#########################################################
#########################################################
#########################################################
#Cigar functions
#########################################################
#########################################################
#########################################################
def testCigarReadWrite(self):
tempFile = getTempFile()
self.tempFiles.append(tempFile)
for test in range(0, self.testNo):
cigarNumber = random.choice(range(10))
l = [ getRandomPairwiseAlignment() for i in range(cigarNumber) ]
fileHandle = open(tempFile, 'w')
for cigar in l:
cigarWrite(fileHandle, cigar)
fileHandle.close()
fileHandle = open(tempFile, 'r')
l.reverse()
for cigar in cigarRead(fileHandle):
cigarWrite(sys.stdout, l[-1])
cigarWrite(sys.stdout, cigar)
assert cigar == l.pop()
assert len(l) == 0
fileHandle.close()
def getRandomTreeString():
def iDFn():
return random.choice([ "one", "1", "", "he44"])
def dFn():
#if random.random() > 0.5:
return ":%.6f" % random.random()
#return ''
def fn3():
if random.random() > 0.5:
if random.random() > 0.5:
#is unary
return '(' + fn3() + ')' + iDFn() + dFn()
else:
return '(' + fn3() + "," + fn3() + ')' + iDFn() + dFn()
else:
return iDFn() + dFn()
return fn3() + ';'
def getRandomPWM(length=-1):
if length == -1:
length = 1 + int(random.random()*10)
def fn():
l = [ random.random()*100 for i in range(0, 4) ]
i = sum(l)
return [ j/i for j in l ]
return [ fn() for i in range(0, length) ]
if __name__ == '__main__':
unittest.main()