biopython icon indicating copy to clipboard operation
biopython copied to clipboard

FastaIterator has no attribute `next`

Open michaelfyc opened this issue 3 years ago • 2 comments

Setup

I am reporting a problem with Biopython 1.79, Python 3.10, and operating system Windows 10.

I was trying to split a large fasta file into fragments with fixed sequence length by the batch_iterator method written here: https://biopython.org/wiki/Split_large_file

But when reading fasta file, the iterpreter complains that FastaIterator has no next() attribute.

Expected behaviour

Split the fasta file into fragments in batch size.

Actual behaviour

image

Steps to reproduce

  1. Define the batch_iterator method as the wiki link demonstrates;
  2. run
	record_iter = SeqIO.parse(open('path/to/sequence.fna'), 'fasta')
    for i, batch in enumerate(batch_iterator(record_iter, 3000)):
        filename = 'reference_%i.fasta' % (i + 1)
        with open('/path/files/' + filename, 'w') as output_handle:
            count = SeqIO.write(batch, output_handle, 'fasta')
        print('Wrote %i records to %s' % (count, filename))

michaelfyc avatar Mar 20 '22 09:03 michaelfyc

The example needs updating for Python 3, to use the built-in next(...) function.

peterjc avatar Mar 20 '22 10:03 peterjc

I wonder if there is a more elegant solution, nothing in https://docs.python.org/3/library/itertools.html looks quite right though...

peterjc avatar Mar 20 '22 10:03 peterjc

change entry = iterator.next() in function batch_iterator -> entry = next(iterator)

CatIIIIIIII avatar Sep 01 '22 07:09 CatIIIIIIII

@BrutalStark yes, that's what I meant. Since the original reported never replied, do you want to make the change?

peterjc avatar Sep 01 '22 13:09 peterjc

Never mind now. It might just require a minor update in the document, I guess?🤔

michaelfyc avatar Sep 05 '22 07:09 michaelfyc

Ah, the "next" was never in the example page - it was in the code. You must have used an older Biopython which lacked that Python 3 fix?

peterjc avatar Sep 06 '22 17:09 peterjc