Back to index

python-biopython  1.60
Public Member Functions
test_SeqIO_index.IndexDictTests Class Reference

List of all members.

Public Member Functions

def simple_check
def key_check
def check_dict_methods
def get_raw_check
def test_duplicates_index_db
def test_duplicates_index
def test_duplicates_to_dict

Detailed Description

Cunning unit test where methods are added at run time.

Definition at line 93 of file test_SeqIO_index.py.


Member Function Documentation

def test_SeqIO_index.IndexDictTests.check_dict_methods (   self,
  rec_dict,
  keys,
  ids 
)

Definition at line 218 of file test_SeqIO_index.py.

00218 
00219     def check_dict_methods(self, rec_dict, keys, ids):
00220         self.assertEqual(set(keys), set(rec_dict.keys()))
00221         #This is redundant, I just want to make sure len works:
00222         self.assertEqual(len(keys), len(rec_dict))
00223         #Make sure boolean evaluation works
00224         self.assertEqual(bool(keys), bool(rec_dict))
00225         for key,id in zip(keys, ids):
00226             self.assertTrue(key in rec_dict)
00227             self.assertEqual(id, rec_dict[key].id)
00228             self.assertEqual(id, rec_dict.get(key).id)
00229         #Check non-existant keys,
00230         assert chr(0) not in keys, "Bad example in test"
00231         try:
00232             rec = rec_dict[chr(0)]
00233             raise ValueError("Accessing a non-existent key should fail")
00234         except KeyError:
00235             pass
00236         self.assertEqual(rec_dict.get(chr(0)), None)
00237         self.assertEqual(rec_dict.get(chr(0), chr(1)), chr(1))
00238         if hasattr(dict, "iteritems"):
00239             #Python 2.x
00240             for key, rec in rec_dict.iteritems():
00241                 self.assertTrue(key in keys)
00242                 self.assertTrue(isinstance(rec, SeqRecord))
00243                 self.assertTrue(rec.id in ids)
00244             #Now check non-defined methods...
00245             self.assertRaises(NotImplementedError, rec_dict.items)
00246             self.assertRaises(NotImplementedError, rec_dict.values)
00247         else:
00248             #Python 3
00249             assert not hasattr(rec_dict, "iteritems")
00250             for key, rec in rec_dict.iteritems():
00251                 self.assertTrue(key in keys)
00252                 self.assertTrue(isinstance(rec, SeqRecord))
00253                 self.assertTrue(rec.id in ids)
00254             for rec in rec_dict.itervalues():
00255                 self.assertTrue(key in keys)
00256                 self.assertTrue(isinstance(rec, SeqRecord))
00257                 self.assertTrue(rec.id in ids)
00258         #Check the following fail
00259         self.assertRaises(NotImplementedError, rec_dict.popitem)
00260         self.assertRaises(NotImplementedError, rec_dict.pop, chr(0))
00261         self.assertRaises(NotImplementedError, rec_dict.pop, chr(0), chr(1))
00262         self.assertRaises(NotImplementedError, rec_dict.clear)
00263         self.assertRaises(NotImplementedError, rec_dict.__setitem__, "X", None)
00264         self.assertRaises(NotImplementedError, rec_dict.copy)
00265         self.assertRaises(NotImplementedError, rec_dict.fromkeys, [])

Here is the caller graph for this function:

def test_SeqIO_index.IndexDictTests.get_raw_check (   self,
  filename,
  format,
  alphabet,
  comp 
)

Definition at line 266 of file test_SeqIO_index.py.

00266 
00267     def get_raw_check(self, filename, format, alphabet, comp):
00268         #Also checking the key_function here
00269         if comp:
00270             h = gzip.open(filename, "rb")
00271             raw_file = h.read()
00272             h.close()
00273             h = gzip_open(filename, format)
00274             id_list = [rec.id.lower() for rec in \
00275                        SeqIO.parse(h, format, alphabet)]
00276             h.close()
00277         else:
00278             h = open(filename, "rb")
00279             raw_file = h.read()
00280             h.close()
00281             id_list = [rec.id.lower() for rec in \
00282                        SeqIO.parse(filename, format, alphabet)]
00283         rec_dict = SeqIO.index(filename, format, alphabet,
00284                                key_function = lambda x : x.lower())
00285         self.assertEqual(set(id_list), set(rec_dict.keys()))
00286         self.assertEqual(len(id_list), len(rec_dict))
00287         for key in id_list:
00288             self.assertTrue(key in rec_dict)
00289             self.assertEqual(key, rec_dict[key].id.lower())
00290             self.assertEqual(key, rec_dict.get(key).id.lower())
00291             raw = rec_dict.get_raw(key)
00292             self.assertTrue(raw.strip())
00293             self.assertTrue(raw in raw_file)
00294             rec1 = rec_dict[key]
00295             #Following isn't very elegant, but it lets me test the
00296             #__getitem__ SFF code is working.
00297             if format in SeqIO._BinaryFormats:
00298                 handle = BytesIO(raw)
00299             else:
00300                 handle = StringIO(_bytes_to_string(raw))
00301             if format == "sff":
00302                 rec2 = SeqIO.SffIO._sff_read_seq_record(handle,
00303                             rec_dict._proxy._flows_per_read,
00304                             rec_dict._proxy._flow_chars,
00305                             rec_dict._proxy._key_sequence,
00306                             rec_dict._proxy._alphabet,
00307                             trim=False)
00308             elif format == "sff-trim":
00309                 rec2 = SeqIO.SffIO._sff_read_seq_record(handle,
00310                             rec_dict._proxy._flows_per_read,
00311                             rec_dict._proxy._flow_chars,
00312                             rec_dict._proxy._key_sequence,
00313                             rec_dict._proxy._alphabet,
00314                             trim=True)
00315             elif format == "uniprot-xml":
00316                 self.assertTrue(raw.startswith(_as_bytes("<entry ")))
00317                 self.assertTrue(raw.endswith(_as_bytes("</entry>")))
00318                 #Currently the __getitem__ method uses this
00319                 #trick too, but we hope to fix that later
00320                 raw = """<?xml version='1.0' encoding='UTF-8'?>
00321                 <uniprot xmlns="http://uniprot.org/uniprot"
00322                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
00323                 xsi:schemaLocation="http://uniprot.org/uniprot
00324                 http://www.uniprot.org/support/docs/uniprot.xsd">
00325                 %s
00326                 </uniprot>
00327                 """ % _bytes_to_string(raw)
00328                 handle = StringIO(raw)
00329                 rec2 = SeqIO.read(handle, format, alphabet)
00330             else:
00331                 rec2 = SeqIO.read(handle, format, alphabet)
00332             self.assertEqual(True, compare_record(rec1, rec2))
00333         rec_dict._proxy._handle.close() #TODO - Better solution
00334         del rec_dict

Here is the call graph for this function:

def test_SeqIO_index.IndexDictTests.key_check (   self,
  filename,
  format,
  alphabet,
  comp 
)
Check indexing with a key function.

Definition at line 157 of file test_SeqIO_index.py.

00157 
00158     def key_check(self, filename, format, alphabet, comp):
00159         """Check indexing with a key function."""
00160         if comp:
00161             h = gzip_open(filename, format)
00162             id_list = [rec.id for rec in SeqIO.parse(h, format, alphabet)]
00163             h.close()
00164         else:
00165             id_list = [rec.id for rec in SeqIO.parse(filename, format, alphabet)]
00166 
00167         key_list = [add_prefix(id) for id in id_list]
00168         rec_dict = SeqIO.index(filename, format, alphabet, add_prefix)
00169         self.check_dict_methods(rec_dict, key_list, id_list)
00170         rec_dict._proxy._handle.close() #TODO - Better solution
00171         del rec_dict
00172 
00173         if not sqlite3:
00174             return
00175 
00176         #In memory,
00177         rec_dict = SeqIO.index_db(":memory:", [filename], format, alphabet,
00178                                   add_prefix)
00179         self.check_dict_methods(rec_dict, key_list, id_list)
00180         #check error conditions
00181         self.assertRaises(ValueError, SeqIO.index_db,
00182                           ":memory:", format="dummy",
00183                           key_function=add_prefix)
00184         self.assertRaises(ValueError, SeqIO.index_db,
00185                           ":memory:", filenames=["dummy"],
00186                           key_function=add_prefix)
00187         rec_dict.close()
00188         del rec_dict
00189 
00190         #Saving to file...
00191         index_tmp = filename + ".key.idx"
00192         if os.path.isfile(index_tmp):
00193             os.remove(index_tmp)
00194         rec_dict = SeqIO.index_db(index_tmp, [filename], format, alphabet,
00195                                   add_prefix)
00196         self.check_dict_methods(rec_dict, key_list, id_list)
00197         rec_dict.close()
00198         rec_dict._con.close() #hack for PyPy
00199         del rec_dict
00200 
00201         #Now reload it...
00202         rec_dict = SeqIO.index_db(index_tmp, [filename], format, alphabet,
00203                                   add_prefix)
00204         self.check_dict_methods(rec_dict, key_list, id_list)
00205         rec_dict.close()
00206         rec_dict._con.close() #hack for PyPy
00207         del rec_dict
00208 
00209         #Now reload without passing filenames and format
00210         rec_dict = SeqIO.index_db(index_tmp, alphabet=alphabet,
00211                                   key_function=add_prefix)
00212         self.check_dict_methods(rec_dict, key_list, id_list)
00213         rec_dict.close()
00214         rec_dict._con.close() #hack for PyPy
00215         del rec_dict
00216         os.remove(index_tmp)
00217         #Done
    

Here is the call graph for this function:

def test_SeqIO_index.IndexDictTests.simple_check (   self,
  filename,
  format,
  alphabet,
  comp 
)
Check indexing (without a key function).

Definition at line 95 of file test_SeqIO_index.py.

00095 
00096     def simple_check(self, filename, format, alphabet, comp):
00097         """Check indexing (without a key function)."""
00098         if comp:
00099             h = gzip_open(filename, format)
00100             id_list = [rec.id for rec in SeqIO.parse(h, format, alphabet)]
00101             h.close()
00102         else:
00103             id_list = [rec.id for rec in SeqIO.parse(filename, format, alphabet)]
00104 
00105         rec_dict = SeqIO.index(filename, format, alphabet)
00106         self.check_dict_methods(rec_dict, id_list, id_list)
00107         rec_dict._proxy._handle.close() #TODO - Better solution
00108         del rec_dict
00109 
00110         if not sqlite3:
00111             return
00112 
00113         #In memory,
00114         #note here give filenames as list of strings
00115         rec_dict = SeqIO.index_db(":memory:", [filename], format,
00116                                   alphabet)
00117         self.check_dict_methods(rec_dict, id_list, id_list)
00118         rec_dict.close()
00119         del rec_dict
00120 
00121         #check error conditions
00122         self.assertRaises(ValueError, SeqIO.index_db,
00123                           ":memory:", format="dummy")
00124         self.assertRaises(ValueError, SeqIO.index_db,
00125                           ":memory:", filenames=["dummy"])
00126 
00127         #Saving to file...
00128         index_tmp = filename + ".idx"
00129         if os.path.isfile(index_tmp):
00130             os.remove(index_tmp)
00131 
00132         #To disk,
00133         #note here we give the filename as a single string
00134         #to confirm that works too (convience feature).
00135         rec_dict = SeqIO.index_db(index_tmp, filename, format,
00136                                   alphabet)
00137         self.check_dict_methods(rec_dict, id_list, id_list)
00138         rec_dict.close()
00139         rec_dict._con.close() #hack for PyPy
00140         del rec_dict
00141 
00142         #Now reload it...
00143         rec_dict = SeqIO.index_db(index_tmp, [filename], format,
00144                                   alphabet)
00145         self.check_dict_methods(rec_dict, id_list, id_list)
00146         rec_dict.close()
00147         rec_dict._con.close() #hack for PyPy
00148         del rec_dict
00149 
00150         #Now reload without passing filenames and format
00151         rec_dict = SeqIO.index_db(index_tmp, alphabet=alphabet)
00152         self.check_dict_methods(rec_dict, id_list, id_list)
00153         rec_dict.close()
00154         rec_dict._con.close() #hack for PyPy
00155         del rec_dict
00156         os.remove(index_tmp)
    

Here is the call graph for this function:

Index file with duplicate identifers with Bio.SeqIO.index()

Definition at line 341 of file test_SeqIO_index.py.

00341 
00342     def test_duplicates_index(self):
00343         """Index file with duplicate identifers with Bio.SeqIO.index()"""
00344         self.assertRaises(ValueError, SeqIO.index, "Fasta/dups.fasta", "fasta")

Index file with duplicate identifers with Bio.SeqIO.index_db()

Definition at line 336 of file test_SeqIO_index.py.

00336 
00337         def test_duplicates_index_db(self):
00338             """Index file with duplicate identifers with Bio.SeqIO.index_db()"""
00339             self.assertRaises(ValueError, SeqIO.index_db, ":memory:",
00340                               ["Fasta/dups.fasta"], "fasta")

Index file with duplicate identifers with Bio.SeqIO.to_dict()

Definition at line 345 of file test_SeqIO_index.py.

00345 
00346     def test_duplicates_to_dict(self):
00347         """Index file with duplicate identifers with Bio.SeqIO.to_dict()"""
00348         handle = open("Fasta/dups.fasta", "rU")
00349         iterator = SeqIO.parse(handle, "fasta")
00350         self.assertRaises(ValueError, SeqIO.to_dict, iterator)
00351         handle.close()

Here is the call graph for this function:


The documentation for this class was generated from the following file: