Beispiel #1
0
 def loop(self, filename, format):
     original_records = list(SeqIO.parse(filename, 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.assertTrue(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]
         self.assertTrue(compare_record(old, new))
     # Done
     handle.close()
     server.close()
 def test_draw_ascii(self):
     """Tree to Graph conversion."""
     handle = StringIO()
     tree = Phylo.read(EX_APAF, 'phyloxml')
     Phylo.draw_ascii(tree, file=handle)
     Phylo.draw_ascii(tree, file=handle, column_width=120)
     handle.close()
Beispiel #3
0
    def test_phenotype_IO(self):
        """Test basic functionalities of phenotype IO methods."""
        p1 = phenotype.read(SMALL_JSON_PLATE, "pm-json")
        p2 = next(phenotype.parse(SMALL_CSV_PLATES, "pm-csv"))

        handle = StringIO()

        c = phenotype.write([p1, p2], handle, "pm-json")
        self.assertEqual(c, 2)

        handle.flush()
        handle.seek(0)
        # Now ready to read back from the handle...
        try:
            records = list(phenotype.parse(handle, "pm-json"))
        except ValueError as e:
            # This is BAD.  We can't read our own output.
            # I want to see the output when called from the test harness,
            # run_tests.py (which can be funny about new lines on Windows)
            handle.seek(0)
            raise ValueError("%s\n\n%s\n\n%s" %
                             (str(e), repr(handle.read()), repr(records)))

        self.assertEqual(p1, records[0])

        handle.close()
        handle = StringIO()
        self.assertRaises(TypeError, phenotype.write, p1, handle, 1)
        self.assertRaises(ValueError, phenotype.write, p1, handle, "PM-JSON")
        self.assertRaises(ValueError, phenotype.write, p1, handle, "pm-csv")
        handle.close()
Beispiel #4
0
 def loop(self, filename, format):
     original_records = list(SeqIO.parse(filename, 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.assertTrue(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]
         self.assertTrue(compare_record(old, new))
     # Done
     handle.close()
     server.close()
    def test_phenotype_IO(self):
        '''Test basic functionalities of phenotype IO methods'''
        p1 = phenotype.read(SMALL_JSON_PLATE, 'pm-json')
        p2 = next(phenotype.parse(SMALL_CSV_PLATES, 'pm-csv'))

        handle = StringIO()

        c = phenotype.write([p1, p2], handle, 'pm-json')
        self.assertEqual(c, 2)

        handle.flush()
        handle.seek(0)
        # Now ready to read back from the handle...
        try:
            records = list(phenotype.parse(handle, 'pm-json'))
        except ValueError as e:
            # This is BAD.  We can't read our own output.
            # I want to see the output when called from the test harness,
            # run_tests.py (which can be funny about new lines on Windows)
            handle.seek(0)
            raise ValueError("%s\n\n%s\n\n%s"
                             % (str(e), repr(handle.read()), repr(records)))

        self.assertEqual(p1, records[0])

        handle.close()
        handle = StringIO()
        self.assertRaises(TypeError, phenotype.write, p1, handle, 1)
        self.assertRaises(ValueError, phenotype.write, p1, handle, 'PM-JSON')
        self.assertRaises(ValueError, phenotype.write, p1, handle, 'pm-csv')
        handle.close()
 def test_draw_ascii(self):
     """Tree to Graph conversion."""
     handle = StringIO()
     tree = Phylo.read(EX_APAF, 'phyloxml')
     Phylo.draw_ascii(tree, file=handle)
     Phylo.draw_ascii(tree, file=handle, column_width=120)
     handle.close()
Beispiel #7
0
 def test_no_name(self):
     """Test FASTA record with no identifier."""
     handle = StringIO(">\nACGT")
     record = SeqIO.read(handle, "fasta")
     handle.close()
     self.assertEqual(str(record.seq), "ACGT")
     self.assertEqual("", record.id)
     self.assertEqual("", record.name)
     self.assertEqual("", record.description)
 def test_no_name(self):
     """Test FASTA record with no identifier."""
     handle = StringIO(">\nACGT")
     record = SeqIO.read(handle, "fasta")
     handle.close()
     self.assertEqual(str(record.seq), "ACGT")
     self.assertEqual("", record.id)
     self.assertEqual("", record.name)
     self.assertEqual("", record.description)
Beispiel #9
0
    def externalEntityRefHandler(self, context, base, systemId, publicId):
        """The purpose of this function is to load the DTD locally, instead
        of downloading it from the URL specified in the XML. Using the local
        DTD results in much faster parsing. If the DTD is not found locally,
        we try to download it. If new DTDs become available from NCBI,
        putting them in Bio/Entrez/DTDs will allow the parser to see them."""
        urlinfo = _urlparse(systemId)
        #Following attribute requires Python 2.5+
        #if urlinfo.scheme=='http':
        if urlinfo[0] == 'http':
            # Then this is an absolute path to the DTD.
            url = systemId
        elif urlinfo[0] == '':
            # Then this is a relative path to the DTD.
            # Look at the parent URL to find the full path.
            try:
                url = self.dtd_urls[-1]
            except IndexError:
                # Assume the default URL for DTDs if the top parent
                # does not contain an absolute path
                source = "http://www.ncbi.nlm.nih.gov/dtd/"
            else:
                source = os.path.dirname(url)
            # urls always have a forward slash, don't use os.path.join
            url = source.rstrip("/") + "/" + systemId
        self.dtd_urls.append(url)
        # First, try to load the local version of the DTD file
        location, filename = os.path.split(systemId)
        handle = self.open_dtd_file(filename)
        if not handle:
            # DTD is not available as a local file. Try accessing it through
            # the internet instead.
            from Bio._py3k import StringIO
            try:
                handle = _urlopen(url)
            except IOError:
                raise RuntimeError("Failed to access %s at %s" %
                                   (filename, url))
            text = handle.read()
            handle.close()
            self.save_dtd_file(filename, text)
            handle = StringIO(text)

        parser = self.parser.ExternalEntityParserCreate(context)
        parser.ElementDeclHandler = self.elementDecl
        parser.ParseFile(handle)
        handle.close()
        self.dtd_urls.pop()
        return 1
Beispiel #10
0
    def externalEntityRefHandler(self, context, base, systemId, publicId):
        """The purpose of this function is to load the DTD locally, instead
        of downloading it from the URL specified in the XML. Using the local
        DTD results in much faster parsing. If the DTD is not found locally,
        we try to download it. If new DTDs become available from NCBI,
        putting them in Bio/Entrez/DTDs will allow the parser to see them."""
        urlinfo = _urlparse(systemId)
        #Following attribute requires Python 2.5+
        #if urlinfo.scheme=='http':
        if urlinfo[0]=='http':
            # Then this is an absolute path to the DTD.
            url = systemId
        elif urlinfo[0]=='':
            # Then this is a relative path to the DTD.
            # Look at the parent URL to find the full path.
            try:
                url = self.dtd_urls[-1]
            except IndexError:
                # Assume the default URL for DTDs if the top parent
                # does not contain an absolute path
                source = "http://www.ncbi.nlm.nih.gov/dtd/"
            else:
                source = os.path.dirname(url)
            # urls always have a forward slash, don't use os.path.join
            url = source.rstrip("/") + "/" + systemId
        self.dtd_urls.append(url)
        # First, try to load the local version of the DTD file
        location, filename = os.path.split(systemId)
        handle = self.open_dtd_file(filename)
        if not handle:
            # DTD is not available as a local file. Try accessing it through
            # the internet instead.
            from Bio._py3k import StringIO
            try:
                handle = _urlopen(url)
            except IOError:
                raise RuntimeException("Failed to access %s at %s" % (filename, url))
            text = handle.read()
            handle.close()
            self.save_dtd_file(filename, text)
            handle = StringIO(text)

        parser = self.parser.ExternalEntityParserCreate(context)
        parser.ElementDeclHandler = self.elementDecl
        parser.ParseFile(handle)
        handle.close()
        self.dtd_urls.pop()
        return 1
    def test_write_read(self):
        handle = StringIO(phylip_text5a)
        list5 = list(PhylipIterator(handle))
        handle.close()

        handle = StringIO()
        PhylipWriter(handle).write_file(list5)
        handle.seek(0)
        list6 = list(PhylipIterator(handle))

        self.assertEqual(len(list5), len(list6))
        for a1, a2 in zip(list5, list6):
            self.assertEqual(len(a1), len(a2))
            for r1, r2 in zip(a1, a2):
                self.assertEqual(r1.id, r2.id)
                self.assertEqual(str(r1.seq), str(r2.seq))
    def test_two_and_three(self):
        handle = StringIO(phylip_text2)
        list2 = list(PhylipIterator(handle))
        handle.close()
        self.assertEqual(len(list2), 1)
        self.assertEqual(len(list2[0]), 5)

        handle = StringIO(phylip_text3)
        list3 = list(PhylipIterator(handle))
        handle.close()
        self.assertEqual(len(list3), 1)
        self.assertEqual(len(list3[0]), 5)

        for i in range(0, 5):
            self.assertEqual(list2[0][i].id, list3[0][i].id)
            self.assertEqual(str(list2[0][i].seq), str(list3[0][i].seq))
    def test_two_and_three(self):
        handle = StringIO(phylip_text2)
        list2 = list(PhylipIterator(handle))
        handle.close()
        self.assertEqual(len(list2), 1)
        self.assertEqual(len(list2[0]), 5)

        handle = StringIO(phylip_text3)
        list3 = list(PhylipIterator(handle))
        handle.close()
        self.assertEqual(len(list3), 1)
        self.assertEqual(len(list3[0]), 5)

        for i in range(0, 5):
            self.assertEqual(list2[0][i].id, list3[0][i].id)
            self.assertEqual(str(list2[0][i].seq), str(list3[0][i].seq))
    def test_write_read(self):
        handle = StringIO(phylip_text5a)
        list5 = list(PhylipIterator(handle))
        handle.close()

        handle = StringIO()
        PhylipWriter(handle).write_file(list5)
        handle.seek(0)
        list6 = list(PhylipIterator(handle))

        self.assertEqual(len(list5), len(list6))
        for a1, a2 in zip(list5, list6):
            self.assertEqual(len(a1), len(a2))
            for r1, r2 in zip(a1, a2):
                self.assertEqual(r1.id, r2.id)
                self.assertEqual(str(r1.seq), str(r2.seq))
Beispiel #15
0
check_phylip_reject_duplicate()


# Check parsers can cope with an empty file
for t_format in AlignIO._FormatToIterator:
    handle = StringIO()
    alignments = list(AlignIO.parse(handle, t_format))
    assert len(alignments) == 0

# Check writers can cope with no alignments
for t_format in list(AlignIO._FormatToWriter) + list(SeqIO._FormatToWriter):
    handle = StringIO()
    assert 0 == AlignIO.write([], handle, t_format), \
           "Writing no alignments to %s format should work!" \
           % t_format
    handle.close()

# Check writers reject non-alignments
list_of_records = list(AlignIO.read("Clustalw/opuntia.aln", "clustal"))
for t_format in list(AlignIO._FormatToWriter) + list(SeqIO._FormatToWriter):
    handle = StringIO()
    try:
        AlignIO.write([list_of_records], handle, t_format)
        assert False, "Writing non-alignment to %s format should fail!" \
            % t_format
    except (TypeError, AttributeError, ValueError):
        pass
    handle.close()
    del handle
del list_of_records, t_format
 def test_four(self):
     handle = StringIO(phylip_text4)
     list4 = list(PhylipIterator(handle))
     handle.close()
     self.assertEqual(len(list4), 1)
     self.assertEqual(len(list4[0]), 5)
Beispiel #17
0

check_phylip_reject_duplicate()

# Check parsers can cope with an empty file
for t_format in AlignIO._FormatToIterator:
    handle = StringIO()
    alignments = list(AlignIO.parse(handle, t_format))
    assert len(alignments) == 0

# Check writers can cope with no alignments
for t_format in list(AlignIO._FormatToWriter) + list(SeqIO._FormatToWriter):
    handle = StringIO()
    msg = "Writing no alignments to %s format should work!" % t_format
    assert AlignIO.write([], handle, t_format) == 0, msg
    handle.close()

# Check writers reject non-alignments
list_of_records = list(AlignIO.read("Clustalw/opuntia.aln", "clustal"))
for t_format in list(AlignIO._FormatToWriter) + list(SeqIO._FormatToWriter):
    handle = StringIO()
    try:
        AlignIO.write([list_of_records], handle, t_format)
        assert False, "Writing non-alignment to %s format should fail!" \
            % t_format
    except (TypeError, AttributeError, ValueError):
        pass
    handle.close()
    del handle
del list_of_records, t_format
 def test_five(self):
     handle = StringIO(phylip_text5)
     self.assertRaises(ValueError, list, PhylipIterator(handle))
     handle.close()
 def test_five_a(self):
     handle = StringIO(phylip_text5a)
     list5 = list(PhylipIterator(handle))
     handle.close()
     self.assertEqual(len(list5), 1)
 def test_five(self):
     handle = StringIO(phylip_text5)
     self.assertRaises(ValueError, list, PhylipIterator(handle))
     handle.close()
 def test_four(self):
     handle = StringIO(phylip_text4)
     list4 = list(PhylipIterator(handle))
     handle.close()
     self.assertEqual(len(list4), 1)
     self.assertEqual(len(list4[0]), 5)
 def test_five_a(self):
     handle = StringIO(phylip_text5a)
     list5 = list(PhylipIterator(handle))
     handle.close()
     self.assertEqual(len(list5), 1)