Skip to content

Commit

Permalink
Bio.SeqIO.NibIO - use subclassing (biopython#4854)
Browse files Browse the repository at this point in the history
This also drops the mincount and maxcount arguments on the base
class which were only being used for this file format.
  • Loading branch information
mdehoon authored Sep 24, 2024
1 parent ddd5982 commit e0c4d1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
38 changes: 6 additions & 32 deletions Bio/SeqIO/Interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,18 @@ def write_record(self, record):
# for sequential file formats. #
##################################################

def write_records(self, records, maxcount=None):
def write_records(self, records):
"""Write records to the output file, and return the number of records.
records - A list or iterator returning SeqRecord objects
maxcount - The maximum number of records allowed by the
file format, or None if there is no maximum.
"""
count = 0
if maxcount is None:
for record in records:
self.write_record(record)
count += 1
else:
for record in records:
if count == maxcount:
if maxcount == 1:
raise ValueError("More than one sequence found")
else:
raise ValueError(
"Number of sequences is larger than %d" % maxcount
)
self.write_record(record)
count += 1
for record in records:
self.write_record(record)
count += 1
return count

def write_file(self, records, mincount=0, maxcount=None):
def write_file(self, records):
"""Write a complete file with the records, and return the number of records.
records - A list or iterator returning SeqRecord objects
Expand All @@ -274,21 +260,9 @@ def write_file(self, records, mincount=0, maxcount=None):
##################################################
try:
self.write_header()
count = self.write_records(records, maxcount)
count = self.write_records(records)
self.write_footer()
finally:
if self.handle is not self.target:
self.handle.close()
if count < mincount:
if mincount == 1: # Common case
raise ValueError("Must have one sequence")
elif mincount == maxcount:
raise ValueError(
"Number of sequences is %d (expected %d)" % (count, mincount)
)
else:
raise ValueError(
"Number of sequences is %d (expected at least %d)"
% (count, mincount)
)
return count
16 changes: 13 additions & 3 deletions Bio/SeqIO/NibIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,19 @@ def write_record(self, record):
indices = nucleotides.translate(table)
handle.write(binascii.unhexlify(indices))

def write_file(self, records):
"""Write the complete file with the records, and return the number of records."""
count = super().write_file(records, mincount=1, maxcount=1)
def write_records(self, records):
"""Write records to the output file, and return the number of records.
records - A list or iterator returning SeqRecord objects
"""
count = 0
for record in records:
if count == 1:
raise ValueError("More than one sequence found")
self.write_record(record)
count += 1
if count != 1:
raise ValueError("Must have one sequence")
return count


Expand Down

0 comments on commit e0c4d1a

Please sign in to comment.