Skip to content

Commit 26b21d7

Browse files
committed
Added access to the iterable
1 parent ef665e2 commit 26b21d7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/sequence/io/FastaStreamer.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,34 @@ public FastaStreamer batchSize(int size) {
7171
return this;
7272
}
7373

74+
/**
75+
* Enable iteration through the proteins in the file using syntax such as:
76+
* <pre>
77+
* for(ProteinSequence sequence : FastaStreamer.from(path).each()) {
78+
* .
79+
* .
80+
* .
81+
* }
82+
* </pre>
83+
*
84+
* @return an iterable suitable for an iteration loop
85+
*/
86+
public Iterable<ProteinSequence> each() {
87+
return () -> stream().iterator();
88+
}
89+
7490
/**
7591
* Create a stream of protein sequences from the contents of the path
7692
* @return the stream
77-
* @throws IOException if there is an error opening the file
7893
*/
79-
public Stream<ProteinSequence> stream() throws IOException {
94+
public Stream<ProteinSequence> stream() {
8095
InputStreamProvider provider = new InputStreamProvider();
81-
InputStream input = provider.getInputStream(getPath().toFile());
96+
InputStream input;
97+
try {
98+
input = provider.getInputStream(getPath().toFile());
99+
} catch (IOException exception) {
100+
throw new RuntimeException(exception);
101+
}
82102
FastaReader<ProteinSequence, AminoAcidCompound> reader = new FastaReader<>(input, getHeaderParser(), getSequenceCreator());
83103
Spliterator<ProteinSequence> source = new Spliterators.AbstractSpliterator<>(Integer.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.NONNULL) {
84104
@Override

biojava-core/src/test/java/org/biojava/nbio/core/sequence/io/FastaStreamerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,15 @@ public void stream() throws IOException {
3636
.collect(Collectors.toList());
3737
Assert.assertEquals("Count", 283, sequences.size());
3838
}
39+
40+
@Test
41+
public void iterate() {
42+
String file = this.getClass().getResource("PF00104_small.fasta.gz").getFile();
43+
Path path = Paths.get(file);
44+
int count = 0;
45+
for (ProteinSequence sequence : FastaStreamer.from(path).each()) {
46+
count++;
47+
}
48+
Assert.assertEquals("Count", 283, count);
49+
}
3950
}

0 commit comments

Comments
 (0)