Skip to content

Commit

Permalink
Deleted useless memory allocation
Browse files Browse the repository at this point in the history
Moved records correctness's checking from nextContainer() to next()

Moved records correctness's validation in the separate method

Added tests relate new functionality

Add inheriting from HtsjdkTest in new test
  • Loading branch information
AntonMazur authored and AntonMazur committed Feb 20, 2018
1 parent e051a56 commit b3e4f77
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/main/java/htsjdk/samtools/CRAMIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public CRAMIterator(final InputStream inputStream, final CRAMReferenceSource ref
this.containerIterator = containerIterator;

firstContainerOffset = this.countingInputStream.getCount();
records = new ArrayList<SAMRecord>(10000);
records = new ArrayList<SAMRecord>();
normalizer = new CramNormalizer(cramHeader.getSamFileHeader(),
referenceSource);
parser = new ContainerParser(cramHeader.getSamFileHeader());
Expand All @@ -106,7 +106,7 @@ public CRAMIterator(final SeekableStream seekableStream, final CRAMReferenceSour
this.containerIterator = containerIterator;

firstContainerOffset = containerIterator.getFirstContainerOffset();
records = new ArrayList<SAMRecord>(10000);
records = new ArrayList<SAMRecord>();
normalizer = new CramNormalizer(cramHeader.getSamFileHeader(),
referenceSource);
parser = new ContainerParser(cramHeader.getSamFileHeader());
Expand All @@ -131,6 +131,7 @@ void nextContainer() throws IOException, IllegalArgumentException,
nextRecord = null;
return;
}

container = containerIterator.next();
if (container.isEOF()) {
records.clear();
Expand All @@ -146,10 +147,7 @@ void nextContainer() throws IOException, IllegalArgumentException,
}
}

if (records == null)
records = new ArrayList<SAMRecord>(container.nofRecords);
else
records.clear();
records.clear();
if (cramRecords == null)
cramRecords = new ArrayList<CramCompressionRecord>(container.nofRecords);
else
Expand All @@ -175,8 +173,10 @@ void nextContainer() throws IOException, IllegalArgumentException,

for (int i = 0; i < container.slices.length; i++) {
final Slice slice = container.slices[i];

if (slice.sequenceId < 0)
continue;

if (!slice.validateRefMD5(refs)) {
final String msg = String.format(
"Reference sequence MD5 mismatch for slice: sequence id %d, start %d, span %d, expected MD5 %s",
Expand Down Expand Up @@ -204,12 +204,6 @@ void nextContainer() throws IOException, IllegalArgumentException,

samRecord.setValidationStringency(validationStringency);

if (validationStringency != ValidationStringency.SILENT) {
final List<SAMValidationError> validationErrors = samRecord.isValid();
SAMUtils.processValidationErrors(validationErrors,
samRecordIndex, validationStringency);
}

if (mReader != null) {
final long chunkStart = (container.offset << 16) | cramRecord.sliceIndex;
final long chunkEnd = ((container.offset << 16) | cramRecord.sliceIndex) + 1;
Expand Down Expand Up @@ -271,7 +265,22 @@ public boolean hasNext() {

@Override
public SAMRecord next() {
return iterator.next();
SAMRecord samRecord = iterator.next();
if (validationStringency != ValidationStringency.SILENT) {
validateRecord(samRecord);
}
return samRecord;
}

/**
* Perform various validations of SAMRecord and handle
* found errors according to the validation stringency.
*
* @param samRecord - validated record
*/
public void validateRecord(SAMRecord samRecord){
final List<SAMValidationError> validationErrors = samRecord.isValid();
SAMUtils.processValidationErrors(validationErrors, samRecordIndex, validationStringency);
}

@Override
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/htsjdk/samtools/CRAMIteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package htsjdk.samtools;

import htsjdk.HtsjdkTest;
import htsjdk.samtools.cram.ref.ReferenceSource;
import htsjdk.samtools.seekablestream.SeekableStream;
import org.junit.Assert;
import org.testng.annotations.Test;

import java.io.File;


/**
* @author [email protected], EPAM Systems, Inc.
*
* This test serve for verifying CRAMIterator records validation using strict validation strategy
**/

public class CRAMIteratorTest extends HtsjdkTest {
private static final File refFile = new File("src/test/resources/htsjdk/samtools/cram/ce.fa");
private static final File cramFile = new File("src/test/resources/htsjdk/samtools/cram/ce#supp.3.0.cram");
ReferenceSource source = new ReferenceSource(refFile);

@Test()
public void shouldNotThrowsExceptionIfCRAMfileContainsInvalidRecords() {
SAMRecordIterator cramIter =
getCramFileIterator(cramFile, source, ValidationStringency.STRICT);

Assert.assertTrue(cramIter.hasNext());
}

@Test(expectedExceptions = SAMException.class)
public void shouldThrowExceptionIfCRAMFileContainsInvalidRecods() {
SAMRecordIterator cramIter =
getCramFileIterator(cramFile, source, ValidationStringency.STRICT);

while (cramIter.hasNext())
{
cramIter.next();
}
}

private SAMRecordIterator getCramFileIterator(File cramFile,
ReferenceSource source,
ValidationStringency valStrigency) {

CRAMFileReader cramFileReader = new CRAMFileReader(cramFile, (SeekableStream) null, source);
cramFileReader.setValidationStringency(valStrigency);
return cramFileReader.getIterator();
}
}



0 comments on commit b3e4f77

Please sign in to comment.