Back to index

python-biopython  1.60
test_SeqIO_online.py
Go to the documentation of this file.
00001 # Copyright 2007-2010 by Peter Cock.  All rights reserved.
00002 # Revisions copyright 2007-2008 by Michiel de Hoon.  All rights reserved.
00003 # This code is part of the Biopython distribution and governed by its
00004 # license.  Please see the LICENSE file that should have been included
00005 # as part of this package.
00006 
00007 """Testing online code for fetching sequences, and parsing them
00008 
00009 Uses Bio.SeqIO to parse files downloaded with Bio.GenBank, Bio.WWW.NCBI, 
00010 Bio.ExPASy etc.
00011 
00012 Goals:
00013     Make sure that all retrieval is working as expected.
00014     May catch some format changes early too.
00015 """
00016 import unittest
00017 
00018 import requires_internet
00019 requires_internet.check()
00020 
00021 from Bio import MissingExternalDependencyError
00022 
00023 #We want to test these:
00024 from Bio import Entrez
00025 from Bio import ExPASy
00026 
00027 #In order to check any sequences returned
00028 from Bio import SeqIO
00029 from StringIO import StringIO
00030 from Bio.SeqUtils.CheckSum import seguid
00031 
00032 from Bio.File import UndoHandle
00033 from Bio._py3k import _as_string
00034 
00035 #This lets us set the email address to be sent to NCBI Entrez:
00036 Entrez.email = "biopython-dev@biopython.org"
00037 
00038 class ExPASyTests(unittest.TestCase):
00039     """Tests for Bio.ExPASy module."""
00040     def test_get_sprot_raw(self):
00041         """Bio.ExPASy.get_sprot_raw("O23729")"""
00042         identifier = "O23729"
00043         try:
00044             #This is to catch an error page from our proxy:
00045             handle = UndoHandle(ExPASy.get_sprot_raw(identifier))
00046             if _as_string(handle.peekline()).startswith("<!DOCTYPE HTML"):
00047                 raise IOError
00048             record = SeqIO.read(handle, "swiss")
00049             handle.close()
00050         except IOError:
00051             raise MissingExternalDependencyError(
00052                   "internet (or maybe just ExPASy) not available")
00053         self.assertEqual(record.id, identifier)
00054         self.assertEqual(len(record), 394)
00055         self.assertEqual(seguid(record.seq), "5Y08l+HJRDIlhLKzFEfkcKd1dkM")
00056 
00057 class EntrezTests(unittest.TestCase):
00058     def simple(self, database, formats, entry, length, checksum):
00059         for f in formats:
00060             try:
00061                 handle = Entrez.efetch(db=database, id=entry, rettype=f, retmode="text")
00062                 record = SeqIO.read(handle, f)
00063                 handle.close()
00064             except IOError:
00065                 raise MissingExternalDependencyError(
00066                       "internet (or maybe just NCBI) not available")
00067             self.assertTrue((entry in record.name) or \
00068                          (entry in record.id) or \
00069                          ("gi" in record.annotations \
00070                           and record.annotations["gi"]==entry),
00071                          "%s got %s, %s" % (entry, record.name, record.id))
00072             self.assertEqual(len(record), length)
00073             self.assertEqual(seguid(record.seq), checksum)
00074 
00075 for database, formats, entry, length, checksum in [
00076     ("nuccore", ["fasta", "gb"], "X52960", 248,
00077      "Ktxz0HgMlhQmrKTuZpOxPZJ6zGU"),
00078     ("nucleotide", ["fasta", "gb"], "6273291", 902,
00079      "bLhlq4mEFJOoS9PieOx4nhGnjAQ"),
00080     ("protein", ["fasta", "gb"], "16130152", 367,
00081      "fCjcjMFeGIrilHAn6h+yju267lg"),
00082     ]:
00083     def funct(d, f, e, l, c):
00084         method = lambda x : x.simple(d, f, e, l, c)
00085         method.__doc__ = "Bio.Entrez.efetch(%s, %s, ...)" % (d, e)
00086         return method
00087     setattr(EntrezTests, "test_%s_%s" % (database, entry),
00088             funct(database, formats, entry, length, checksum))
00089     del funct        
00090 del database, formats, entry, length, checksum
00091 
00092 if __name__ == "__main__":
00093     runner = unittest.TextTestRunner(verbosity = 2)
00094     unittest.main(testRunner=runner)
00095 
00096