Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix CRAMIterator when next() is called without hasNext() #1193

Merged
merged 2 commits into from
Oct 16, 2018

Conversation

lbergelson
Copy link
Member

  • fixing a bug in CRAMIterator which previously threw if next() was called without first calling hasNext()
  • adding CRAMTestUtils and extracting a method to it from CramEdgeCasesTest

Checklist

  • Code compiles correctly
  • New tests covering changes and new functionality
  • All tests passing
  • Extended the README / documentation, if necessary
  • Is not backward compatible (breaks binary or source compatibility)

@codecov-io
Copy link

codecov-io commented Oct 10, 2018

Codecov Report

Merging #1193 into master will increase coverage by 0.018%.
The diff coverage is 75%.

@@               Coverage Diff               @@
##              master     #1193       +/-   ##
===============================================
+ Coverage     68.405%   68.423%   +0.018%     
- Complexity      8017      8020        +3     
===============================================
  Files            542       542               
  Lines          32701     32701               
  Branches        5530      5530               
===============================================
+ Hits           22369     22375        +6     
+ Misses          8127      8123        -4     
+ Partials        2205      2203        -2
Impacted Files Coverage Δ Complexity Δ
src/main/java/htsjdk/samtools/CRAMIterator.java 78.767% <75%> (+1.37%) 33 <2> (+1) ⬆️
...c/main/java/htsjdk/samtools/SAMFileWriterImpl.java 81.944% <0%> (+1.389%) 25% <0%> (+1%) ⬆️
...samtools/util/AsyncBlockCompressedInputStream.java 76% <0%> (+4%) 13% <0%> (+1%) ⬆️

Copy link
Contributor

@pshapiro4broad pshapiro4broad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor things

@@ -271,7 +266,11 @@ public boolean hasNext() {

@Override
public SAMRecord next() {
return iterator.next();
if(hasNext()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space after if


public final class CRAMTestUtils {

private CRAMTestUtils(){};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that there's no point in instantiating this class, but why bother enforcing it with a private constructor?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does make it clear that there is no point in instantiating the class, which I like.

Copy link
Contributor

@pshapiro4broad pshapiro4broad Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike a singleton, there's no harm if a user decides to instantiate it. They can't break anything or change behavior that we'd like to control. So I'd still say there is no need to bother enforcing it for this class. But I guess it comes down to a matter of taste..


private static CRAMFileReader writeAndReadFromInMemoryCram(Collection<SAMRecord> records, CRAMReferenceSource source, SAMFileHeader header) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CRAMFileWriter cramFileWriter = new CRAMFileWriter(baos, source, header, "whatever");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use try-with-resources to avoid close() below. Since baos is closable too you could put that in the try as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stuck it in a try with resources so that it will always close on exit, but I left the close to force a flush before reading from the buffer, the alternative was a gross nested try statement.

/**
* return a CRAMReferenceSource that returns all A's for any sequence queried
*/
public static CRAMReferenceSource getFakeReferenceSource(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space between ){

Copy link
Collaborator

@cmnbroad cmnbroad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of minor comments. Thx for adding the utilities.

final CRAMFileReader reader = CRAMTestUtils.writeAndReadFromInMemoryCram(builder);
final SAMRecordIterator iterator = reader.getIterator();
Assert.assertNotNull(iterator.next());
Assert.assertThrows(NoSuchElementException.class, iterator::next);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That two tests, but, ok...


public final class CRAMTestUtils {

private CRAMTestUtils(){};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does make it clear that there is no point in instantiating the class, which I like.

* fixing a bug in CRAMIterator which previously threw if next() was called without first calling hasNext()
* adding CRAMTestUtils and extracting a method to it from CramEdgeCasesTest
@lbergelson lbergelson merged commit d504256 into master Oct 16, 2018
@lbergelson lbergelson deleted the lb_fix_CramIterator_next branch October 16, 2018 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants