-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathtest_sandbox_scripts.py
257 lines (199 loc) · 8.1 KB
/
test_sandbox_scripts.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
from __future__ import print_function
from __future__ import absolute_import
#
# This file is part of khmer, https://github.com/dib-lab/khmer/, and is
# Copyright (C) Michigan State University, 2015. It is licensed under
# the three-clause BSD license; see LICENSE.
# Contact: khmer-project@idyll.org
#
# pylint: disable=C0111,C0103,E1103,W0612
import sys
import os
import os.path
import shutil
from io import StringIO
import traceback
import nose
from nose.plugins.attrib import attr
import glob
import imp
from . import khmer_tst_utils as utils
import khmer
import screed
def scriptpath(script):
return script
def teardown():
utils.cleanup()
def test_import_all():
sandbox_path = os.path.join(os.path.dirname(__file__), "../sandbox")
if not os.path.exists(sandbox_path):
raise nose.SkipTest("sandbox scripts are only tested in a repository")
path = os.path.join(sandbox_path, "*.py")
scripts = glob.glob(path)
for s in scripts:
s = os.path.normpath(s)
yield _checkImportSucceeds('test_sandbox_scripts.py', s)
class _checkImportSucceeds(object):
def __init__(self, tag, filename):
self.tag = tag
self.filename = filename
self.description = '%s: test import %s' % (self.tag,
os.path.split(filename)[-1])
def __call__(self):
try:
mod = imp.load_source('__zzz', self.filename)
except:
print(traceback.format_exc())
raise AssertionError("%s cannot be imported" % (self.filename,))
#
oldargs = sys.argv
sys.argv = [self.filename]
oldout, olderr = sys.stdout, sys.stderr
sys.stdout = StringIO()
sys.stderr = StringIO()
try:
try:
global_dict = {'__name__': '__main__'}
exec(
compile(open(self.filename).read(), self.filename, 'exec'),
global_dict)
except (ImportError, SyntaxError) as err:
print("{0}".format(err))
raise AssertionError("%s cannot be exec'd" % (self.filename),
"{0}".format(traceback))
except:
pass # other failures are expected :)
finally:
sys.argv = oldargs
out, err = sys.stdout.getvalue(), sys.stderr.getvalue()
sys.stdout, sys.stderr = oldout, olderr
def test_sweep_reads():
readfile = utils.get_temp_filename('reads.fa')
contigfile = utils.get_temp_filename('contigs.fp')
in_dir = os.path.dirname(contigfile)
shutil.copyfile(utils.get_test_data('test-sweep-reads.fa'), readfile)
shutil.copyfile(utils.get_test_data('test-sweep-contigs.fp'), contigfile)
script = scriptpath('sweep-reads.py')
args = ['-k', '25', '--prefix', 'test', '--label-by-pid',
contigfile, readfile, 'junkfile.fa']
status, out, err = utils.runscript(
script, args, in_dir, fail_ok=True, sandbox=True)
# check if the bad file was skipped without issue
assert 'ERROR' in err, err
assert 'skipping' in err, err
out1 = os.path.join(in_dir, 'test_0.fa')
out2 = os.path.join(in_dir, 'test_1.fa')
mout = os.path.join(in_dir, 'test_multi.fa')
oout = os.path.join(in_dir, 'test_orphaned.fa')
print(os.listdir(in_dir))
assert os.path.exists(out1)
assert os.path.exists(out2)
assert os.path.exists(mout)
assert os.path.exists(oout)
seqs1 = set([r.name for r in screed.open(out1)])
seqs2 = set([r.name for r in screed.open(out2)])
seqsm = set([r.name for r in screed.open(mout)])
seqso = set([r.name for r in screed.open(oout)])
print(seqs1)
print(seqs2)
print(seqsm)
print(seqso)
assert seqs1 == set(['read1_p0\t0', 'read2_p0\t0'])
assert seqs2 == set(['read3_p1\t1'])
assert (seqsm == set(['read4_multi\t0\t1']) or
seqsm == set(['read4_multi\t1\t0']))
assert seqso == set(['read5_orphan'])
def test_sweep_reads_fq():
readfile = utils.get_temp_filename('reads.fa')
contigfile = utils.get_temp_filename('contigs.fp')
in_dir = os.path.dirname(contigfile)
shutil.copyfile(utils.get_test_data('test-sweep-reads.fq'), readfile)
shutil.copyfile(utils.get_test_data('test-sweep-contigs.fp'), contigfile)
script = scriptpath('sweep-reads.py')
args = ['-k', '25', '--prefix', 'test', '--label-by-pid',
contigfile, readfile, 'junkfile.fa']
status, out, err = utils.runscript(
script, args, in_dir, fail_ok=True, sandbox=True)
# check if the bad file was skipped without issue
assert 'ERROR' in err, err
assert 'skipping' in err, err
out1 = os.path.join(in_dir, 'test_0.fq')
out2 = os.path.join(in_dir, 'test_1.fq')
mout = os.path.join(in_dir, 'test_multi.fq')
oout = os.path.join(in_dir, 'test_orphaned.fq')
assert os.path.exists(out1)
assert os.path.exists(out2)
assert os.path.exists(mout)
assert os.path.exists(oout)
print(open(out1).read())
print(os.listdir(in_dir))
seqs1 = set([r.name for r in screed.open(out1)])
seqs2 = set([r.name for r in screed.open(out2)])
seqsm = set([r.name for r in screed.open(mout)])
seqso = set([r.name for r in screed.open(oout)])
print(seqs1)
print(seqs2)
print(seqsm)
print(seqso)
assert seqs1 == set(['read1_p0\t0', 'read2_p0\t0'])
assert seqs2 == set(['read3_p1\t1'])
assert (seqsm == set(['read4_multi\t0\t1']) or
seqsm == set(['read4_multi\t1\t0']))
assert seqso == set(['read5_orphan'])
seqs1 = set([r.quality for r in screed.open(out1)])
seqs2 = set([r.quality for r in screed.open(out2)])
seqsm = set([r.quality for r in screed.open(mout)])
seqso = set([r.quality for r in screed.open(oout)])
def test_sweep_reads_2():
infile = utils.get_temp_filename('seqs.fa')
inref = utils.get_temp_filename('ref.fa')
shutil.copyfile(utils.get_test_data('random-20-X2.fa'), infile)
shutil.copyfile(utils.get_test_data('random-20-a.fa'), inref)
wdir = os.path.dirname(inref)
script = scriptpath('sweep-reads.py')
args = ['-m', '50', '-k', '20', '-l', '9', '-b', '60', '--prefix',
'test', '--label-by-seq', inref, infile]
status, out, err = utils.runscript(script, args, wdir, sandbox=True)
for i in range(99):
p = os.path.join(wdir, 'test_{i}.fa'.format(i=i))
print(p, err, out)
assert os.path.exists(p)
os.remove(p)
assert os.path.exists(os.path.join(wdir, 'test.counts.csv'))
assert os.path.exists(os.path.join(wdir, 'test.dist.txt'))
assert not os.path.exists(os.path.join(wdir, 'test_multi.fa'))
def test_sweep_reads_3():
infile = utils.get_temp_filename('seqs.fa')
shutil.copyfile(utils.get_test_data('random-20-a.fa'), infile)
wdir = os.path.dirname(infile)
script = scriptpath('sweep-reads.py')
args = ['-m', '75', '-k', '20', '-l', '1', '--prefix',
'test', '--label-by-group', '10', infile, infile]
status, out, err = utils.runscript(script, args, wdir, sandbox=True)
for i in range(10):
p = os.path.join(wdir, 'test_{i}.fa'.format(i=i))
print(p, err, out)
assert os.path.exists(p)
os.remove(p)
counts_fn = os.path.join(wdir, 'test.counts.csv')
with open(counts_fn) as cfp:
for line in cfp:
_, _, c = line.partition(',')
assert int(c) in [9, 10]
assert os.path.exists(counts_fn)
assert os.path.exists(os.path.join(wdir, 'test.dist.txt'))
assert not os.path.exists(os.path.join(wdir, 'test_multi.fa'))
def test_collect_reads():
outfile = utils.get_temp_filename('out.graph')
infile = utils.get_test_data('test-reads.fa')
script = 'collect-reads.py'
args = ['-M', '1e7', outfile, infile]
status, out, err = utils.runscript(script, args, sandbox=True)
assert status == 0
assert os.path.exists(outfile)
def test_saturate_by_median():
infile = utils.get_test_data('test-reads.fa')
script = 'saturate-by-median.py'
args = ['-M', '1e7', infile]
status, out, err = utils.runscript(script, args, sandbox=True)
assert status == 0