samtools / htsjdk

A Java API for high-throughput sequencing data (HTS) formats.
http://samtools.github.io/htsjdk/
276 stars 244 forks source link

Iterator not working on vcffileReader #1701

Closed AlessandroBalestrucci closed 6 months ago

AlessandroBalestrucci commented 6 months ago

Description of the issue:

Not possible to invoke the iterator() method on object VCFFileReader

Your environment:

version of htsjdk: 4.1.0 version of java: 17

Steps to reproduce

VCFFileReader vcfReader = new VCFFileReader(new File(vcf), false); for (VariantContext variant: vcfReader.iterator()) { //some code here }

Expected behaviour

It should complie

Actual behaviour

Compile error: "Can only iterate over an array or an instance of java.lang.Iterable"

cmnbroad commented 6 months ago

@AlessandroBalestrucci It is a bit confusing, but you can't use for each on an iterator; only an Iterable. The correct syntax to use is:

for (VariantContext variant: vcfReader)

It should work fine if you do it that way.

yfarjoun commented 6 months ago

You can take a look at a project that uses htsjdk, for example Picard.

https://github.com/broadinstitute/picard/blob/master/src/main/java/picard/vcf/LiftoverVcf.java has this use-case (one of many).

you should remove the .iterator() from you code, since the reader is iterable, but the iterator is not...

Cheers,

Y.

On Tue, Jan 23, 2024 at 10:30 AM Chris Norman @.***> wrote:

Closed #1701 https://github.com/samtools/htsjdk/issues/1701 as completed.

— Reply to this email directly, view it on GitHub https://github.com/samtools/htsjdk/issues/1701#event-11571942968, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAU6JUWPX3LBQNYWLH6XKG3YP7JRXAVCNFSM6AAAAABCHE42NWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJRGU3TCOJUGI4TMOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

AlessandroBalestrucci commented 6 months ago

Thanks everyone for your helps