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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
"""
Tests for common features of *all* tables, including 1-bit presence/absence
tables.
This tests:
* method implementations in common between all "table types", including
error handling code;
* basic API compatibility between all "table types", for methods with
their own implementations.
Error handling & other class-specific code (e.g. in methods like
'load' and 'save' that are implemented differently for each type) will
be tested separately. We can use code coverage to identify that
code...
"""
import sys
import pytest
from . import khmer_tst_utils as utils
import khmer
from khmer import _Countgraph, _Counttable, _SmallCountgraph, _SmallCounttable
from khmer import _Nodegraph, _Nodetable
from khmer import ReadParser
import screed
PRIMES_1m = [1000003, 1009837]
# all the table types!
@pytest.fixture(params=[_Countgraph, _Counttable, _SmallCountgraph,
_SmallCounttable, _Nodegraph, _Nodetable])
def tabletype(request):
return request.param
# For map(long, [list of ints]) cross-version hackery
if sys.version_info.major > 2:
long = int # pylint: disable=redefined-builtin
unicode = str
def test_presence(tabletype):
# basic get/add test
tt = tabletype(12, PRIMES_1m)
kmer = 'G' * 12
hashval = tt.hash('G' * 12)
assert tt.get(kmer) == 0
assert tt.get(hashval) == 0
tt.add(kmer)
assert tt.get(kmer) == 1
assert tt.get(hashval) == 1
def test_bad_create(tabletype):
# creation should fail w/bad parameters
try:
tt = tabletype(5, [])
except ValueError as err:
assert 'tablesizes needs to be one or more numbers' in str(err)
def test_get_ksize(tabletype):
# ksize() function.
kh = tabletype(22, PRIMES_1m)
assert kh.ksize() == 22
def test_hash(tabletype):
# hashing of strings -> numbers.
kh = tabletype(5, PRIMES_1m)
x = kh.hash("ATGGC")
assert type(x) == long
def test_hash_bad_dna(tabletype):
# hashing of bad dna -> succeeds w/o complaint
kh = tabletype(5, PRIMES_1m)
x = kh.hash("ATGYC")
def test_hash_bad_length(tabletype):
# hashing of bad dna length -> error
kh = tabletype(5, PRIMES_1m)
with pytest.raises(ValueError):
x = kh.hash("ATGGGC")
with pytest.raises(ValueError):
x = kh.hash("ATGG")
def test_reverse_hash(tabletype):
# hashing of strings -> numbers.
kh = tabletype(5, PRIMES_1m)
try:
x = kh.reverse_hash(15)
except ValueError:
pytest.skip("reverse_hash not implemented on this table type")
assert isinstance(x, (unicode, str))
def test_hashsizes(tabletype):
# hashsizes method.
kh = tabletype(5, PRIMES_1m)
assert kh.hashsizes() == PRIMES_1m
def test_add_hashval(tabletype):
# test add(hashval)
kh = tabletype(5, PRIMES_1m)
x = kh.hash("ATGGC")
y = kh.add(x)
assert y
z = kh.get(x)
assert z == 1
def test_add_dna_kmer(tabletype):
# test add(dna)
kh = tabletype(5, PRIMES_1m)
x = kh.add("ATGGC")
assert x
z = kh.get("ATGGC")
assert z == 1
def test_add_bad_dna_kmer(tabletype):
# even with 'bad' dna, should succeed.
kh = tabletype(5, PRIMES_1m)
x = kh.add("ATYGC")
def test_get_hashval(tabletype):
# test get(hashval)
kh = tabletype(5, PRIMES_1m)
hashval = kh.hash("ATGGC")
kh.add(hashval)
z = kh.get(hashval)
assert z == 1
def test_get_hashval_rc(tabletype):
# test get(hashval)
kh = tabletype(4, PRIMES_1m)
hashval = kh.hash("ATGC")
rc = kh.hash("GCAT")
assert hashval == rc
def test_get_dna_kmer(tabletype):
# test get(dna)
kh = tabletype(5, PRIMES_1m)
hashval = kh.hash("ATGGC")
kh.add(hashval)
z = kh.get("ATGGC")
assert z == 1
def test_get_bad_dna_kmer(tabletype):
# test get(dna) with bad dna; should be fine.
kh = tabletype(5, PRIMES_1m)
kh.hash("ATYGC")
def test_consume_and_count(tabletype):
tt = tabletype(6, PRIMES_1m)
x = "ATGCCGATGCA"
num_kmers = tt.consume(x)
assert num_kmers == len(x) - tt.ksize() + 1 # num k-mers consumed
for start in range(len(x) - 6 + 1):
assert tt.get(x[start:start + 6]) == 1
def test_consume_and_count_bad_dna(tabletype):
# while we don't specifically handle bad DNA, we should at least be
# consistent...
tt = tabletype(6, PRIMES_1m)
x = "ATGCCGNTGCA"
num_kmers = tt.consume(x)
for start in range(len(x) - 6 + 1):
assert tt.get(x[start:start + 6]) == 1
def test_consume_short(tabletype):
# raise error on too short when consume is run
tt = tabletype(6, PRIMES_1m)
x = "ATGCA"
with pytest.raises(ValueError):
tt.consume(x)
def test_get_kmer_counts(tabletype):
hi = tabletype(6, PRIMES_1m)
hi.consume("AAAAAA")
counts = hi.get_kmer_counts("AAAAAA")
print(counts)
assert len(counts) == 1
assert counts[0] == 1
hi.consume("AAAAAA")
counts = hi.get_kmer_counts("AAAAAA")
print(counts)
assert len(counts) == 1
assert counts[0] >= 1
hi.consume("AAAAAT")
counts = hi.get_kmer_counts("AAAAAAT")
print(counts)
assert len(counts) == 2
assert counts[0] >= 1
assert counts[1] == 1
def test_get_kmer_hashes(tabletype):
hi = tabletype(6, PRIMES_1m)
hashes = hi.get_kmer_hashes("ACGTGCGT")
print(hashes)
assert len(hashes) == 3
assert hashes[0] == hi.hash("ACGTGC")
assert hashes[1] == hi.hash("CGTGCG")
assert hashes[2] == hi.hash("GTGCGT")
def test_get_min_count(tabletype):
hi = tabletype(6, PRIMES_1m)
# master string, 3 k-mers
x = "ACGTGCGT"
hi.add("ACGTGC") # 3
hi.add("ACGTGC")
hi.add("ACGTGC")
hi.add("CGTGCG") # 1
hi.add("GTGCGT") # 2
hi.add("GTGCGT")
counts = hi.get_kmer_counts(x)
assert hi.get_min_count(x) == min(counts)
assert hi.get_max_count(x) == max(counts)
med, _, _ = hi.get_median_count(x)
assert med == list(sorted(counts))[len(counts) // 2]
def test_get_kmers(tabletype):
hi = tabletype(6, PRIMES_1m)
kmers = hi.get_kmers("AAAAAA")
assert kmers == ["AAAAAA"]
kmers = hi.get_kmers("AAAAAAT")
assert kmers == ["AAAAAA", "AAAAAT"]
kmers = hi.get_kmers("AGCTTTTC")
assert kmers == ['AGCTTT', 'GCTTTT', 'CTTTTC']
def test_trim_on_abundance(tabletype):
hi = tabletype(6, PRIMES_1m)
x = "ATGGCAGTAGCAGTGAGC"
hi.consume(x[:10])
(y, pos) = hi.trim_on_abundance(x, 1)
assert pos == 10
assert x[:pos] == y
def test_trim_below_abundance(tabletype):
hi = tabletype(6, PRIMES_1m)
x = "ATGGCAGTAGCAGTGAGC"
x_rc = screed.rc(x)
hi.consume(x_rc[:10])
print(len(x))
(y, pos) = hi.trim_below_abundance(x, 0)
assert pos == len(x) - hi.ksize() + 1
assert x[:pos] == y
DNA = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"
def test_find_spectral_error_positions(tabletype):
kh = tabletype(8, PRIMES_1m)
kh.consume(DNA[:30])
for n in range(len(DNA) - 8 + 1):
print(n, kh.get(DNA[n:n + 8]))
posns = kh.find_spectral_error_positions(DNA, 0)
assert posns == [30], posns
def test_find_spectral_error_positions_6(tabletype):
kh = tabletype(8, PRIMES_1m)
kh.consume(DNA[1:])
for n in range(len(DNA) - 8 + 1):
print(n, kh.get(DNA[n:n + 8]))
posns = kh.find_spectral_error_positions(DNA, 0)
assert posns == [0], posns
def test_find_spectral_error_positions_5(tabletype):
kh = tabletype(8, PRIMES_1m)
kh.consume(DNA[:10])
kh.consume(DNA[11:])
posns = kh.find_spectral_error_positions(DNA, 0)
assert posns == [10], posns
def test_consume_seqfile_reads_parser(tabletype):
kh = tabletype(5, PRIMES_1m)
rparser = ReadParser(utils.get_test_data('test-fastq-reads.fq'))
kh.consume_seqfile_with_reads_parser(rparser)
kh2 = tabletype(5, PRIMES_1m)
for record in screed.open(utils.get_test_data('test-fastq-reads.fq')):
kh2.consume(record.sequence)
assert kh.get('CCGGC') == kh2.get('CCGGC')
def test_consume_seqfile(tabletype):
kh = tabletype(5, PRIMES_1m)
kh.consume_seqfile(utils.get_test_data('test-fastq-reads.fq'))
kh2 = tabletype(5, PRIMES_1m)
for record in screed.open(utils.get_test_data('test-fastq-reads.fq')):
kh2.consume(record.sequence)
assert kh.get('CCGGC') == kh2.get('CCGGC')
def test_save_load(tabletype):
kh = tabletype(5, PRIMES_1m)
savefile = utils.get_temp_filename('tablesave.out')
# test add(dna)
x = kh.add("ATGGC")
z = kh.get("ATGGC")
assert z == 1
kh.save(savefile)
# should we provide a single load function here? yes, probably. @CTB
if tabletype == _Countgraph:
loaded = khmer.load_countgraph(savefile)
elif tabletype == _Counttable:
loaded = khmer.load_counttable(savefile)
elif tabletype == _SmallCountgraph:
loaded = khmer.load_countgraph(savefile, small=True)
elif tabletype == _SmallCounttable:
loaded = khmer.load_counttable(savefile, small=True)
elif tabletype == _Nodegraph:
loaded = khmer.load_nodegraph(savefile)
elif tabletype == _Nodetable:
loaded = khmer.load_nodetable(savefile)
else:
raise Exception("unknown tabletype")
z = loaded.get('ATGGC')
assert z == 1
def test_get_bigcount(tabletype):
# get_bigcount should return false by default
tt = tabletype(12, PRIMES_1m)
assert not tt.get_use_bigcount()
def test_set_bigcount(tabletype):
supports_bigcount = [_Countgraph, _Counttable]
tt = tabletype(12, PRIMES_1m)
if tabletype in supports_bigcount:
tt.set_use_bigcount(True)
for i in range(300):
tt.add('G' * 12)
assert tt.get('G' * 12) == 300
else:
with pytest.raises(ValueError):
tt.set_use_bigcount(True)
def test_abund_dist_A(tabletype):
A_filename = utils.get_test_data('all-A.fa')
kh = tabletype(4, PRIMES_1m)
tracking = khmer._Nodegraph(4, PRIMES_1m)
kh.consume_seqfile(A_filename)
dist = kh.abundance_distribution(A_filename, tracking)
print(dist[:10])
assert sum(dist) == 1
assert dist[0] == 0
def test_abund_dist_A_readparser(tabletype):
A_filename = utils.get_test_data('all-A.fa')
rparser = ReadParser(A_filename)
kh = tabletype(4, PRIMES_1m)
tracking = khmer._Nodetable(4, PRIMES_1m)
kh.consume_seqfile(A_filename)
dist = kh.abundance_distribution(A_filename, tracking)
print(dist[:10])
assert sum(dist) == 1
assert dist[0] == 0
|