Skip to content

Commit 85e8956

Browse files
authored
BioSQL updates for python3 (biopython#2654)
1 parent 3018ad5 commit 85e8956

File tree

4 files changed

+32
-94
lines changed

4 files changed

+32
-94
lines changed

BioSQL/BioSeq.py

-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ def __len__(self):
4545

4646
def __getitem__(self, index): # Seq API requirement
4747
"""Return a subsequence or single letter."""
48-
# Note since Python 2.0, __getslice__ is deprecated
49-
# and __getitem__ is used instead.
50-
# See http://docs.python.org/ref/sequence-methods.html
5148
if isinstance(index, int):
5249
# Return a single letter as a string
5350
i = index

BioSQL/BioSeqDatabase.py

+22-82
Original file line numberDiff line numberDiff line change
@@ -213,49 +213,19 @@ def __iter__(self):
213213
# TODO - Iterate over the cursor, much more efficient
214214
return iter(self.adaptor.list_biodatabase_names())
215215

216-
if hasattr(dict, "iteritems"):
217-
# Python 2, use iteritems etc
218-
def keys(self):
219-
"""List of namespaces (sub-databases) in the database."""
220-
return self.adaptor.list_biodatabase_names()
221-
222-
def values(self):
223-
"""List of BioSeqDatabase objects in the database."""
224-
return [self[key] for key in self]
225-
226-
def items(self):
227-
"""List of (namespace, BioSeqDatabase) for entries in database."""
228-
return [(key, self[key]) for key in self]
229-
230-
def iterkeys(self):
231-
"""Iterate over namespaces (sub-databases) in the database."""
232-
return iter(self)
233-
234-
def itervalues(self):
235-
"""Iterate over BioSeqDatabase objects in the database."""
236-
for key in self:
237-
yield self[key]
238-
239-
def iteritems(self):
240-
"""Iterate over (namespace, BioSeqDatabase) in the database."""
241-
for key in self:
242-
yield key, self[key]
243-
244-
else:
245-
# Python 3, items etc are all iterators
246-
def keys(self):
247-
"""Iterate over namespaces (sub-databases) in the database."""
248-
return iter(self)
216+
def keys(self):
217+
"""Iterate over namespaces (sub-databases) in the database."""
218+
return iter(self)
249219

250-
def values(self):
251-
"""Iterate over BioSeqDatabase objects in the database."""
252-
for key in self:
253-
yield self[key]
220+
def values(self):
221+
"""Iterate over BioSeqDatabase objects in the database."""
222+
for key in self:
223+
yield self[key]
254224

255-
def items(self):
256-
"""Iterate over (namespace, BioSeqDatabase) in the database."""
257-
for key in self:
258-
yield key, self[key]
225+
def items(self):
226+
"""Iterate over (namespace, BioSeqDatabase) in the database."""
227+
for key in self:
228+
yield key, self[key]
259229

260230
def __delitem__(self, name):
261231
"""Remove a namespace and all its entries."""
@@ -832,49 +802,19 @@ def __iter__(self):
832802
# TODO - Iterate over the cursor, much more efficient
833803
return iter(self.adaptor.list_bioentry_ids(self.dbid))
834804

835-
if hasattr(dict, "iteritems"):
836-
# Python 2, use iteritems etc
837-
def keys(self):
838-
"""List of ids which may not be meaningful outside this database."""
839-
return self.adaptor.list_bioentry_ids(self.dbid)
840-
841-
def values(self):
842-
"""List of DBSeqRecord objects in the namespace (sub database)."""
843-
return [self[key] for key in self]
844-
845-
def items(self):
846-
"""List of (id, DBSeqRecord) for the namespace (sub database)."""
847-
return [(key, self[key]) for key in self]
848-
849-
def iterkeys(self):
850-
"""Iterate over ids (which may not be meaningful outside this database)."""
851-
return iter(self)
852-
853-
def itervalues(self):
854-
"""Iterate over DBSeqRecord objects in the namespace (sub database)."""
855-
for key in self:
856-
yield self[key]
805+
def keys(self):
806+
"""Iterate over ids (which may not be meaningful outside this database)."""
807+
return iter(self)
857808

858-
def iteritems(self):
859-
"""Iterate over (id, DBSeqRecord) for the namespace (sub database)."""
860-
for key in self:
861-
yield key, self[key]
809+
def values(self):
810+
"""Iterate over DBSeqRecord objects in the namespace (sub database)."""
811+
for key in self:
812+
yield self[key]
862813

863-
else:
864-
# Python 3, items etc are all iterators
865-
def keys(self):
866-
"""Iterate over ids (which may not be meaningful outside this database)."""
867-
return iter(self)
868-
869-
def values(self):
870-
"""Iterate over DBSeqRecord objects in the namespace (sub database)."""
871-
for key in self:
872-
yield self[key]
873-
874-
def items(self):
875-
"""Iterate over (id, DBSeqRecord) for the namespace (sub database)."""
876-
for key in self:
877-
yield key, self[key]
814+
def items(self):
815+
"""Iterate over (id, DBSeqRecord) for the namespace (sub database)."""
816+
for key in self:
817+
yield key, self[key]
878818

879819
def lookup(self, **kwargs):
880820
"""Return a DBSeqRecord using an acceptable identifier.

BioSQL/Loader.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ def _load_seqfeature_dbxref(self, dbxrefs, seqfeature_id):
10951095
db = dbxref_data[0]
10961096
accessions = dbxref_data[1:]
10971097
except Exception:
1098-
raise ValueError("Parsing of db_xref failed: '%s'" % value)
1098+
raise ValueError("Parsing of db_xref failed: '%s'" % value) from None
10991099
# Loop over all the grabbed accessions, and attempt to fill the
11001100
# table
11011101
for accession in accessions:
@@ -1177,7 +1177,9 @@ def _load_dbxrefs(self, record, bioentry_id):
11771177
db = db.strip()
11781178
accession = accession.strip()
11791179
except Exception:
1180-
raise ValueError("Parsing of dbxrefs list failed: '%s'" % value)
1180+
raise ValueError(
1181+
"Parsing of dbxrefs list failed: '%s'" % value
1182+
) from None
11811183
# Get the dbxref_id value for the dbxref data
11821184
dbxref_id = self._get_dbxref_id(db, accession)
11831185
# Insert the bioentry_dbxref data

Tests/common_BioSQL.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def check_config(dbdriver, dbtype, dbhost, dbuser, dbpasswd, testdb):
9494
import org.postgresql.Driver
9595
except ImportError:
9696
message = "Install the JDBC driver for %s to use BioSQL " % DBTYPE
97-
raise MissingExternalDependencyError(message)
97+
raise MissingExternalDependencyError(message) from None
9898
else:
9999
try:
100100
__import__(DBDRIVER)
@@ -103,7 +103,7 @@ def check_config(dbdriver, dbtype, dbhost, dbuser, dbpasswd, testdb):
103103
message = "Install MySQLdb or mysqlclient if you want to use %s with BioSQL " % (DBTYPE)
104104
else:
105105
message = "Install %s if you want to use %s with BioSQL " % (DBDRIVER, DBTYPE)
106-
raise MissingExternalDependencyError(message)
106+
raise MissingExternalDependencyError(message) from None
107107

108108
try:
109109
if DBDRIVER in ["sqlite3"]:
@@ -115,7 +115,7 @@ def check_config(dbdriver, dbtype, dbhost, dbuser, dbpasswd, testdb):
115115
del server
116116
except Exception as e:
117117
message = "Connection failed, check settings if you plan to use BioSQL: %s" % e
118-
raise MissingExternalDependencyError(message)
118+
raise MissingExternalDependencyError(message) from None
119119

120120
DBSCHEMA = "biosqldb-" + DBTYPE + ".sql"
121121
SQL_FILE = os.path.join(os.getcwd(), "BioSQL", DBSCHEMA)
@@ -573,7 +573,7 @@ def test_seq_features(self):
573573
self.assertEqual(cds_feature.qualifiers["codon_start"], ["1"])
574574
except KeyError:
575575
raise KeyError("Missing expected entries, have %s"
576-
% repr(cds_feature.qualifiers))
576+
% repr(cds_feature.qualifiers)) from None
577577

578578
self.assertIn("db_xref", cds_feature.qualifiers)
579579
multi_ann = cds_feature.qualifiers["db_xref"]
@@ -961,9 +961,8 @@ def test_transfer(self):
961961
def test_reload(self):
962962
"""Make sure can't reimport existing records."""
963963
gb_file = os.path.join(os.getcwd(), "GenBank", "cor6_6.gb")
964-
gb_handle = open(gb_file)
965-
record = next(SeqIO.parse(gb_handle, "gb"))
966-
gb_handle.close()
964+
with open(gb_file) as gb_handle:
965+
record = next(SeqIO.parse(gb_handle, "gb"))
967966
# Should be in database already...
968967
db_record = self.db.lookup(accession="X55053")
969968
self.assertEqual(db_record.id, record.id)

0 commit comments

Comments
 (0)