/
test_BioSQL.py
675 lines (591 loc) · 27.3 KB
/
test_BioSQL.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env python
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""Tests for dealing with storage of biopython objects in a relational db.
"""
# standard library
import os
import unittest
from StringIO import StringIO
# local stuff
from Bio import MissingExternalDependencyError
from Bio.Seq import Seq, MutableSeq
from Bio.SeqFeature import SeqFeature
from Bio import Alphabet
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
from BioSQL import BioSeqDatabase
from BioSQL import BioSeq
# This testing suite should try to detect whether a valid database
# installation exists on this computer. Only run the tests if it
# does.
try:
from setup_BioSQL import DBDRIVER, DBTYPE
from setup_BioSQL import DBHOST, DBUSER, DBPASSWD, TESTDB
from setup_BioSQL import DBSCHEMA, SQL_FILE
except (NameError, ImportError):
message = "Check settings in Tests/setup_BioSQL.py "\
"if you plan to use BioSQL."
raise MissingExternalDependencyError(message)
try:
if DBDRIVER in ["sqlite3"]:
server = BioSeqDatabase.open_database(driver = DBDRIVER, db = TESTDB)
else:
server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST)
del server
except Exception, e:
message = "Connection failed, check settings in Tests/setup_BioSQL.py "\
"if you plan to use BioSQL: %s" % str(e)
raise MissingExternalDependencyError(message)
from seq_tests_common import compare_record, compare_records
def _do_db_create():
"""Do the actual work of database creation. Relevant for MySQL and PostgreSQL
"""
# first open a connection to create the database
server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST)
if DBDRIVER == "pgdb":
# The pgdb postgres driver does not support autocommit, so here we
# commit the current transaction so that 'drop database' query will
# be outside a transaction block
server.adaptor.cursor.execute("COMMIT")
else:
# Auto-commit: postgresql cannot drop database in a transaction
try:
server.adaptor.autocommit()
except AttributeError:
pass
# drop anything in the database
try:
# with Postgres, can get errors about database still being used and
# not able to be dropped. Wait briefly to be sure previous tests are
# done with it.
import time
time.sleep(1)
sql = r"DROP DATABASE " + TESTDB
server.adaptor.cursor.execute(sql, ())
except (server.module.OperationalError,
server.module.DatabaseError), e: # the database doesn't exist
pass
except (server.module.IntegrityError,
server.module.ProgrammingError), e: # ditto--perhaps
if str(e).find('database "%s" does not exist' % TESTDB) == -1:
raise
# create a new database
sql = r"CREATE DATABASE " + TESTDB
server.adaptor.execute(sql, ())
server.close()
def create_database():
"""Delete any existing BioSQL test database, then (re)create an empty BioSQL database."""
if DBDRIVER in ["sqlite3"]:
if os.path.exists(TESTDB):
os.remove(TESTDB)
else:
_do_db_create()
# now open a connection to load the database
server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
server.load_database_sql(SQL_FILE)
server.commit()
server.close()
def load_database(gb_handle):
"""Load a GenBank file into a new BioSQL database.
This is useful for running tests against a newly created database.
"""
create_database()
# now open a connection to load the database
db_name = "biosql-test"
server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
db = server.new_database(db_name)
# get the GenBank file we are going to put into it
iterator = SeqIO.parse(gb_handle, "gb")
# finally put it in the database
count = db.load(iterator)
server.commit()
server.close()
return count
class ReadTest(unittest.TestCase):
"""Test reading a database from an already built database.
"""
loaded_db = 0
def setUp(self):
"""Connect to and load up the database.
"""
gb_file = os.path.join(os.getcwd(), "GenBank", "cor6_6.gb")
gb_handle = open(gb_file, "r")
load_database(gb_handle)
gb_handle.close()
self.server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER,
passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
self.db = self.server["biosql-test"]
def tearDown(self):
self.server.close()
del self.db
del self.server
def test_get_db_items(self):
"""Get a list of all items in the database.
"""
items = self.db.values()
def test_lookup_items(self):
"""Test retrieval of items using various ids.
"""
item = self.db.lookup(accession = "X62281")
try:
item = self.db.lookup(accession = "Not real")
raise Assertionerror("No problem on fake id retrieval")
except IndexError:
pass
item = self.db.lookup(display_id = "ATKIN2")
try:
item = self.db.lookup(display_id = "Not real")
raise AssertionError("No problem on fake id retrieval")
except IndexError:
pass
# primary id retrieval
item = self.db.lookup(primary_id = "16353")
try:
item = self.db.lookup(primary_id = "Not Real")
raise AssertionError("No problem on fake primary id retrieval")
except IndexError:
pass
class SeqInterfaceTest(unittest.TestCase):
"""Make sure the BioSQL objects implement the expected biopython interfaces
"""
def setUp(self):
"""Load a database.
"""
gb_file = os.path.join(os.getcwd(), "GenBank", "cor6_6.gb")
gb_handle = open(gb_file, "r")
load_database(gb_handle)
gb_handle.close()
self.server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
self.db = self.server["biosql-test"]
self.item = self.db.lookup(accession = "X62281")
def tearDown(self):
self.server.close()
del self.db
del self.item
del self.server
def test_seq_record(self):
"""Make sure SeqRecords from BioSQL implement the right interface.
"""
test_record = self.item
self.assert_(isinstance(test_record.seq, BioSeq.DBSeq))
self.assertEqual(test_record.id, "X62281.1", test_record.id)
self.assertEqual(test_record.name, "ATKIN2")
self.assertEqual(test_record.description, "A.thaliana kin2 gene.")
annotations = test_record.annotations
# XXX should do something with annotations once they are like
# a dictionary
for feature in test_record.features:
self.assert_(isinstance(feature, SeqFeature))
s = str(test_record) #shouldn't cause any errors!
def test_seq(self):
"""Make sure Seqs from BioSQL implement the right interface.
"""
test_seq = self.item.seq
alphabet = test_seq.alphabet
self.assert_(isinstance(alphabet, Alphabet.Alphabet))
data = test_seq.data
self.assertEqual(type(data), type(""))
string_rep = test_seq.tostring()
self.assertEqual(string_rep, str(test_seq)) #check __str__ too
self.assertEqual(type(string_rep), type(""))
self.assertEqual(len(test_seq), 880)
def test_convert(self):
"""Check can turn a DBSeq object into a Seq or MutableSeq."""
test_seq = self.item.seq
other = test_seq.toseq()
self.assertEqual(str(test_seq), str(other))
self.assertEqual(test_seq.alphabet, other.alphabet)
self.assert_(isinstance(other, Seq))
other = test_seq.tomutable()
self.assertEqual(str(test_seq), str(other))
self.assertEqual(test_seq.alphabet, other.alphabet)
self.assert_(isinstance(other, MutableSeq))
def test_addition(self):
"""Check can add DBSeq objects together."""
test_seq = self.item.seq
for other in [Seq("ACGT",test_seq.alphabet),
MutableSeq("ACGT",test_seq.alphabet),
"ACGT",
test_seq]:
test = test_seq + other
self.assertEqual(str(test), str(test_seq) + str(other))
self.assert_(isinstance(test, Seq))
test = other + test_seq
self.assertEqual(str(test), str(other) + str(test_seq))
def test_seq_slicing(self):
"""Check that slices of sequences are retrieved properly.
"""
test_seq = self.item.seq
new_seq = test_seq[:10]
self.assert_(isinstance(new_seq, BioSeq.DBSeq))
# simple slicing
self.assertEqual(test_seq[:5].tostring(), 'ATTTG')
self.assertEqual(test_seq[0:5].tostring(), 'ATTTG')
self.assertEqual(test_seq[2:3].tostring(), 'T')
self.assertEqual(test_seq[2:4].tostring(), 'TT')
self.assertEqual(test_seq[870:].tostring(), 'TTGAATTATA')
# getting more fancy
self.assertEqual(test_seq[-1], 'A')
self.assertEqual(test_seq[1], 'T')
self.assertEqual(test_seq[-10:][5:].tostring(), "TTATA")
self.assertEqual(str(test_seq[-10:][5:]), "TTATA")
def test_seq_features(self):
"""Check SeqFeatures of a sequence.
"""
test_features = self.item.features
cds_feature = test_features[6]
self.assertEqual(cds_feature.type, "CDS")
self.assertEqual(str(cds_feature.location), "[103:579]")
for sub_feature in cds_feature.sub_features:
self.assertEqual(sub_feature.type, "CDS")
self.assertEqual(sub_feature.location_operator, "join")
try:
self.assertEqual(cds_feature.qualifiers["gene"], ["kin2"])
self.assertEqual(cds_feature.qualifiers["protein_id"], ["CAA44171.1"])
self.assertEqual(cds_feature.qualifiers["codon_start"], ["1"])
except KeyError:
raise KeyError("Missing expected entries, have %s" \
% repr(cds_feature.qualifiers))
self.assert_("db_xref" in cds_feature.qualifiers)
multi_ann = cds_feature.qualifiers["db_xref"]
self.assertEqual(len(multi_ann), 2)
self.assert_("GI:16354" in multi_ann)
self.assert_("SWISS-PROT:P31169" in multi_ann)
class LoaderTest(unittest.TestCase):
"""Load a database from a GenBank file.
"""
def setUp(self):
# create TESTDB
create_database()
# load the database
db_name = "biosql-test"
self.server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
# remove the database if it already exists
try:
self.server[db_name]
self.server.remove_database(db_name)
except KeyError:
pass
self.db = self.server.new_database(db_name)
# get the GenBank file we are going to put into it
input_file = os.path.join(os.getcwd(), "GenBank", "cor6_6.gb")
handle = open(input_file, "r")
self.iterator = SeqIO.parse(handle, "gb")
def tearDown(self):
self.server.close()
del self.db
del self.server
def test_load_database(self):
"""Load SeqRecord objects into a BioSQL database.
"""
self.db.load(self.iterator)
# do some simple tests to make sure we actually loaded the right
# thing. More advanced tests in a different module.
items = self.db.values()
self.assertEqual(len(items), 6)
item_names = []
item_ids = []
for item in items:
item_names.append(item.name)
item_ids.append(item.id)
item_names.sort()
item_ids.sort()
self.assertEqual(item_names, ['AF297471', 'ARU237582', 'ATCOR66M',
'ATKIN2', 'BNAKINI', 'BRRBIF72'])
self.assertEqual(item_ids, ['AF297471.1', 'AJ237582.1', 'L31939.1',
'M81224.1', 'X55053.1', 'X62281.1'])
class DupLoadTest(unittest.TestCase):
"""Check a few duplicate conditions fail."""
def setUp(self):
#drop any old database and create a new one:
create_database()
#connect to new database:
self.server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
#Create new namespace within new empty database:
self.db = self.server.new_database("biosql-test")
def tearDown(self):
self.server.rollback()
self.server.close()
del self.db
del self.server
def test_duplicate_load(self):
"""Make sure can't import a single record twice (in one go)."""
record = SeqRecord(Seq("ATGCTATGACTAT", Alphabet.generic_dna),id="Test1")
try:
count = self.db.load([record,record])
except Exception, err:
#Good!
self.assertEqual("IntegrityError", err.__class__.__name__)
return
raise Exception("Should have failed! Loaded %i records" % count)
def test_duplicate_load2(self):
"""Make sure can't import a single record twice (in steps)."""
record = SeqRecord(Seq("ATGCTATGACTAT", Alphabet.generic_dna),id="Test2")
count = self.db.load([record])
self.assertEqual(count,1)
try:
count = self.db.load([record])
except Exception, err:
#Good!
self.assertEqual("IntegrityError", err.__class__.__name__)
return
raise Exception("Should have failed! Loaded %i records" % count)
def test_duplicate_id_load(self):
"""Make sure can't import records with same ID (in one go)."""
record1 = SeqRecord(Seq("ATGCTATGACTAT", Alphabet.generic_dna),id="TestA")
record2 = SeqRecord(Seq("GGGATGCGACTAT", Alphabet.generic_dna),id="TestA")
try:
count = self.db.load([record1,record2])
except Exception, err:
#Good!
self.assertEqual("IntegrityError", err.__class__.__name__)
return
raise Exception("Should have failed! Loaded %i records" % count)
class ClosedLoopTest(unittest.TestCase):
"""Test file -> BioSQL -> file."""
#NOTE - For speed I don't bother to create a new database each time,
#simple a new unique namespace is used for each test.
def test_NC_005816(self):
"""GenBank file to BioSQL and back to a GenBank file, NC_005816."""
self.loop(os.path.join(os.getcwd(), "GenBank", "NC_005816.gb"), "gb")
def test_NC_000932(self):
"""GenBank file to BioSQL and back to a GenBank file, NC_000932."""
self.loop(os.path.join(os.getcwd(), "GenBank", "NC_000932.gb"), "gb")
def test_NT_019265(self):
"""GenBank file to BioSQL and back to a GenBank file, NT_019265."""
self.loop(os.path.join(os.getcwd(), "GenBank", "NT_019265.gb"), "gb")
def test_protein_refseq2(self):
"""GenBank file to BioSQL and back to a GenBank file, protein_refseq2."""
self.loop(os.path.join(os.getcwd(), "GenBank", "protein_refseq2.gb"), "gb")
def test_no_ref(self):
"""GenBank file to BioSQL and back to a GenBank file, noref."""
self.loop(os.path.join(os.getcwd(), "GenBank", "noref.gb"), "gb")
def test_one_of(self):
"""GenBank file to BioSQL and back to a GenBank file, one_of."""
self.loop(os.path.join(os.getcwd(), "GenBank", "one_of.gb"), "gb")
def test_cor6_6(self):
"""GenBank file to BioSQL and back to a GenBank file, cor6_6."""
self.loop(os.path.join(os.getcwd(), "GenBank", "cor6_6.gb"), "gb")
def test_arab1(self):
"""GenBank file to BioSQL and back to a GenBank file, arab1."""
self.loop(os.path.join(os.getcwd(), "GenBank", "arab1.gb"), "gb")
def loop(self, filename, format):
original_records = list(SeqIO.parse(open(filename, "rU"), format))
# now open a connection to load the database
server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
db_name = "test_loop_%s" % filename #new namespace!
db = server.new_database(db_name)
count = db.load(original_records)
self.assertEqual(count, len(original_records))
server.commit()
#Now read them back...
biosql_records = [db.lookup(name=rec.name) \
for rec in original_records]
#And check they agree
self.assert_(compare_records(original_records, biosql_records))
#Now write to a handle...
handle = StringIO()
SeqIO.write(biosql_records, handle, "gb")
#Now read them back...
handle.seek(0)
new_records = list(SeqIO.parse(handle, "gb"))
#And check they still agree
self.assertEqual(len(new_records), len(original_records))
for old, new in zip(original_records, new_records):
#TODO - remove this hack because we don't yet write these (yet):
for key in ["comment", "references", "db_source"]:
if key in old.annotations and key not in new.annotations:
del old.annotations[key]
#TODO - remove this hack one we write the date properly:
del old.annotations["date"]
del new.annotations["date"]
self.assert_(compare_record(old, new))
#Done
server.close()
class TransferTest(unittest.TestCase):
"""Test file -> BioSQL, BioSQL -> BioSQL."""
#NOTE - For speed I don't bother to create a new database each time,
#simple a new unique namespace is used for each test.
def test_NC_005816(self):
"""GenBank file to BioSQL, then again to a new namespace, NC_005816."""
self.trans(os.path.join(os.getcwd(), "GenBank", "NC_005816.gb"), "gb")
def test_NC_000932(self):
"""GenBank file to BioSQL, then again to a new namespace, NC_000932."""
self.trans(os.path.join(os.getcwd(), "GenBank", "NC_000932.gb"), "gb")
def test_NT_019265(self):
"""GenBank file to BioSQL, then again to a new namespace, NT_019265."""
self.trans(os.path.join(os.getcwd(), "GenBank", "NT_019265.gb"), "gb")
def test_protein_refseq2(self):
"""GenBank file to BioSQL, then again to a new namespace, protein_refseq2."""
self.trans(os.path.join(os.getcwd(), "GenBank", "protein_refseq2.gb"), "gb")
def test_no_ref(self):
"""GenBank file to BioSQL, then again to a new namespace, noref."""
self.trans(os.path.join(os.getcwd(), "GenBank", "noref.gb"), "gb")
def test_one_of(self):
"""GenBank file to BioSQL, then again to a new namespace, one_of."""
self.trans(os.path.join(os.getcwd(), "GenBank", "one_of.gb"), "gb")
def test_cor6_6(self):
"""GenBank file to BioSQL, then again to a new namespace, cor6_6."""
self.trans(os.path.join(os.getcwd(), "GenBank", "cor6_6.gb"), "gb")
def test_arab1(self):
"""GenBank file to BioSQL, then again to a new namespace, arab1."""
self.trans(os.path.join(os.getcwd(), "GenBank", "arab1.gb"), "gb")
def trans(self, filename, format):
original_records = list(SeqIO.parse(open(filename, "rU"), format))
# now open a connection to load the database
server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
db_name = "test_trans1_%s" % filename #new namespace!
db = server.new_database(db_name)
count = db.load(original_records)
self.assertEqual(count, len(original_records))
server.commit()
#Now read them back...
biosql_records = [db.lookup(name=rec.name) \
for rec in original_records]
#And check they agree
self.assert_(compare_records(original_records, biosql_records))
#Now write to a second name space...
db_name = "test_trans2_%s" % filename #new namespace!
db = server.new_database(db_name)
count = db.load(biosql_records)
self.assertEqual(count, len(original_records))
#Now read them back again,
biosql_records2 = [db.lookup(name=rec.name) \
for rec in original_records]
#And check they also agree
self.assert_(compare_records(original_records, biosql_records2))
#Done
server.close()
class InDepthLoadTest(unittest.TestCase):
"""Make sure we are loading and retreiving in a semi-lossless fashion.
"""
def setUp(self):
gb_file = os.path.join(os.getcwd(), "GenBank", "cor6_6.gb")
gb_handle = open(gb_file, "r")
load_database(gb_handle)
gb_handle.close()
self.server = BioSeqDatabase.open_database(driver = DBDRIVER,
user = DBUSER, passwd = DBPASSWD,
host = DBHOST, db = TESTDB)
self.db = self.server["biosql-test"]
def tearDown(self):
self.server.close()
del self.db
del self.server
def test_transfer(self):
"""Make sure can load record into another namespace."""
#Should be in database already...
db_record = self.db.lookup(accession = "X55053")
#Make a new namespace
db2 = self.server.new_database("biosql-test-alt")
#Should be able to load this DBSeqRecord there...
count = db2.load([db_record])
self.assertEqual(count,1)
def test_reload(self):
"""Make sure can't reimport existing records."""
gb_file = os.path.join(os.getcwd(), "GenBank", "cor6_6.gb")
gb_handle = open(gb_file, "r")
record = SeqIO.parse(gb_handle, "gb").next()
gb_handle.close()
#Should be in database already...
db_record = self.db.lookup(accession = "X55053")
self.assertEqual(db_record.id, record.id)
self.assertEqual(db_record.name, record.name)
self.assertEqual(db_record.description, record.description)
self.assertEqual(str(db_record.seq), str(record.seq))
#Good... now try reloading it!
try:
count = self.db.load([record])
except Exception, err:
#Good!
self.assertEqual("IntegrityError", err.__class__.__name__)
return
raise Exception("Should have failed! Loaded %i records" % count)
def test_record_loading(self):
"""Make sure all records are correctly loaded.
"""
test_record = self.db.lookup(accession = "X55053")
self.assertEqual(test_record.name, "ATCOR66M")
self.assertEqual(test_record.id, "X55053.1")
self.assertEqual(test_record.description, "A.thaliana cor6.6 mRNA.")
self.assert_(isinstance(test_record.seq.alphabet, Alphabet.DNAAlphabet))
self.assertEqual(test_record.seq[:10].tostring(), 'AACAAAACAC')
test_record = self.db.lookup(accession = "X62281")
self.assertEqual(test_record.name, "ATKIN2")
self.assertEqual(test_record.id, "X62281.1")
self.assertEqual(test_record.description, "A.thaliana kin2 gene.")
self.assert_(isinstance(test_record.seq.alphabet, Alphabet.DNAAlphabet))
self.assertEqual(test_record.seq[:10].tostring(), 'ATTTGGCCTA')
def test_seq_feature(self):
"""Indepth check that SeqFeatures are transmitted through the db.
"""
test_record = self.db.lookup(accession = "AJ237582")
features = test_record.features
self.assertEqual(len(features), 7)
# test single locations
test_feature = features[0]
self.assertEqual(test_feature.type, "source")
self.assertEqual(str(test_feature.location), "[0:206]")
self.assertEqual(len(test_feature.qualifiers.keys()), 3)
self.assertEqual(test_feature.qualifiers["country"], ["Russia:Bashkortostan"])
self.assertEqual(test_feature.qualifiers["organism"], ["Armoracia rusticana"])
self.assertEqual(test_feature.qualifiers["db_xref"], ["taxon:3704"])
# test split locations
test_feature = features[4]
self.assertEqual(test_feature.type, "CDS")
self.assertEqual(str(test_feature.location), "[0:206]")
self.assertEqual(len(test_feature.sub_features), 2)
self.assertEqual(str(test_feature.sub_features[0].location), "[0:48]")
self.assertEqual(test_feature.sub_features[0].type, "CDS")
self.assertEqual(test_feature.sub_features[0].location_operator, "join")
self.assertEqual(str(test_feature.sub_features[1].location), "[142:206]")
self.assertEqual(test_feature.sub_features[1].type, "CDS")
self.assertEqual(test_feature.sub_features[1].location_operator, "join")
self.assertEqual(len(test_feature.qualifiers.keys()), 6)
self.assertEqual(test_feature.qualifiers["gene"], ["csp14"])
self.assertEqual(test_feature.qualifiers["codon_start"], ["2"])
self.assertEqual(test_feature.qualifiers["product"],
["cold shock protein"])
self.assertEqual(test_feature.qualifiers["protein_id"], ["CAB39890.1"])
self.assertEqual(test_feature.qualifiers["db_xref"], ["GI:4538893"])
self.assertEqual(test_feature.qualifiers["translation"],
["DKAKDAAAAAGASAQQAGKNISDAAAGGVNFVKEKTG"])
# test passing strand information
# XXX We should be testing complement as well
test_record = self.db.lookup(accession = "AJ237582")
test_feature = test_record.features[4] # DNA, no complement
self.assertEqual(test_feature.strand, 1)
for sub_feature in test_feature.sub_features:
self.assertEqual(sub_feature.strand, 1)
test_record = self.db.lookup(accession = "X55053")
test_feature = test_record.features[0]
# mRNA, so really cDNA, so the strand should be 1 (not complemented)
self.assertEqual(test_feature.strand, 1)
#Some of the unit tests don't create their own database,
#so just in case there is no database already:
create_database()
if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity = 2)
unittest.main(testRunner=runner)