Buggy code:
} catch ( MD5MismatchError e ) { Bam2Cram . log . error ( e . getMessage ( ) ) ; System . exit ( 1 ) ; } fixBAMFileHeader . addCramtoolsPG ( samFileHeader ) ; CramHeader h = new CramHeader ( Utils . getMajorVersion ( ) , Utils . getMinorVersion ( ) , ( ( params . bamFile ) == null ? "STDIN" : params . bamFile . getName ( ) ) , samFileHeader ) ; long offset = CramIO . writeCramHeader ( h , os ) ; long bases = 0 ; long coreBytes = 0 ; Map < Integer , Long > externalBytes = new HashMap < Integer , Long > ( ) ; ContainerFactory cf = new ContainerFactory ( samFileHeader , params . maxContainerSize , params . preserveReadNames ) ; do { if ( ( ( params . outputCramFile ) == null ) && ( System . out . checkError ( ) ) ) return ; samRecord = ( iterator . hasNext ( ) ) ? iterator . next ( ) : null ; if ( ( ( samRecord == null ) || ( ( samRecord . getReferenceIndex ( ) ) != prevSeqId ) ) || ( ( samRecords . size ( ) ) >= ( params . maxContainerSize ) ) ) { if ( ! ( samRecords . isEmpty ( ) ) ) { long convertNanos = 0 ; convertNanos = System . nanoTime ( ) ; List < CramRecord > records = Bam2Cram . convert ( samRecords , samFileHeader , ref , tracks , preservation , params . captureAllTags , params . captureTags , params . ignoreTags ) ; convertNanos = ( System . nanoTime ( ) ) - convertNanos ; samRecords . clear ( ) ; Container container = cf . buildContainer ( records ) ; for ( Slice s : container . slices ) s . setRefMD5 ( ref ) ; records . clear ( ) ; long len = CramIO . writeContainer ( container , os ) ; container . offset = offset ; offset += len ; Bam2Cram . log . info ( String . format ( "CONTAINER<seq2seq4repair_space>WRITE<seq2seq4repair_space>TIMES:<seq2seq4repair_space>records<seq2seq4repair_space>build<seq2seq4repair_space>time<seq2seq4repair_space>%dms,<seq2seq4repair_space>header<seq2seq4repair_space>build<seq2seq4repair_space>time<seq2seq4repair_space>%dms,<seq2seq4repair_space>slices<seq2seq4repair_space>build<seq2seq4repair_space>time<seq2seq4repair_space>%dms,<seq2seq4repair_space>io<seq2seq4repair_space>time<seq2seq4repair_space>%dms." , ( convertNanos // 1000000 ) , ( ( container . buildHeaderTime ) // 1000000 ) , ( ( container . buildSlicesTime ) // 1000000 ) , ( ( container . writeTime ) // 1000000 ) ) ) ; for ( Slice s : container . slices ) { coreBytes += s . coreBlock . getCompressedContent ( ) . length ; for ( Integer i : s . external . keySet ( ) ) { if ( ! ( externalBytes . containsKey ( i ) ) ) externalBytes . put ( i , 0L ) ; long value = ( externalBytes . get ( i ) ) + ( s . external . get ( i ) . getCompressedContent ( ) . length ) ; externalBytes . put ( i , value ) ; } } } } if ( samRecord == null ) break ; if ( prevSeqId != ( samRecord . getReferenceIndex ( ) ) ) { if ( ( samRecord . getReferenceIndex ( ) ) != ( SAMRecord . NO_ALIGNMENT_REFERENCE_INDEX ) ) { samSequenceRecord = samFileHeader . getSequence ( samRecord . getReferenceName ( ) ) ; ref = referenceSource . getReferenceBases ( samSequenceRecord , true ) ; tracks = new ReferenceTracks ( samSequenceRecord . getSequenceIndex ( ) , samSequenceRecord . getSequenceName ( ) , ref ) ; } else { ref = new byte [ ] { } ; tracks = new ReferenceTracks ( SAMRecord . NO_ALIGNMENT_REFERENCE_INDEX , SAMRecord . NO_ALIGNMENT_REFERENCE_NAME , ref ) ; } prevSeqId = samRecord . getReferenceIndex ( ) ; } samRecords . add ( samRecord ) ; bases += samRecord . getReadLength ( ) ; if ( ( ( params . maxRecords ) -- ) < 1 ) break ; <START_BUG> } while ( iterator . hasNext ( ) ) ; <END_BUG> iterator . close ( ) ; samFileReader . close ( ) ; if ( params . addEOF ) CramIO . issueZeroF_EOF_marker ( os ) ; os . close ( ) ; StringBuilder sb = new StringBuilder ( ) ; sb . append ( String . format ( "STATS:<seq2seq4repair_space>core<seq2seq4repair_space>%.2f<seq2seq4repair_space>b//b" , ( ( 8.0F * coreBytes ) // bases ) ) ) ; for ( int i : externalBytes . keySet ( ) ) if ( ( externalBytes . get ( i ) ) > 0 ) sb . append ( String . format ( ",<seq2seq4repair_space>ex%d<seq2seq4repair_space>%.2f<seq2seq4repair_space>b//b,<seq2seq4repair_space>" , i , ( ( 8.0F * ( externalBytes . get ( i ) ) ) // bases ) ) ) ; Bam2Cram . log . info ( sb . toString ( ) ) ; if ( ( params . outputCramFile ) != null ) Bam2Cram . log . info ( String . format ( "Compression:<seq2seq4repair_space>%.2f<seq2seq4repair_space>b//b." , ( ( 8.0F * ( params . outputCramFile . length ( ) ) ) // bases ) ) ) ; } @ Parameters ( commandDescription = "BAM<seq2seq4repair_space>to<seq2seq4repair_space>CRAM<seq2seq4repair_space>converter.<seq2seq4repair_space>" ) static class Params { @ Parameter ( names = { "-l" , "--log-level" } , description = "Change<seq2seq4repair_space>log<seq2seq4repair_space>level:<seq2seq4repair_space>DEBUG,<seq2seq4repair_space>INFO,<seq2seq4repair_space>WARNING,<seq2seq4repair_space>ERROR." , converter = LevelConverter . class ) LogLevel logLevel = LogLevel . ERROR ; @ Parameter ( names = { "--input-bam-file" , "-I" } , converter = FileConverter . class , description = "Path<seq2seq4repair_space>to<seq2seq4repair_space>a<seq2seq4repair_space>BAM<seq2seq4repair_space>file<seq2seq4repair_space>to<seq2seq4repair_space>be<seq2seq4repair_space>converted<seq2seq4repair_space>to<seq2seq4repair_space>CRAM.<seq2seq4repair_space>Omit<seq2seq4repair_space>if<seq2seq4repair_space>standard<seq2seq4repair_space>input<seq2seq4repair_space>(pipe)." ) File bamFile ; @ Parameter ( names = { "--reference-fasta-file" , "-R" } , converter = FileConverter . class , description = "The<seq2seq4repair_space>reference<seq2seq4repair_space>fasta<seq2seq4repair_space>file,<seq2seq4repair_space>uncompressed<seq2seq4repair_space>and<seq2seq4repair_space>indexed<seq2seq4repair_space>(.fai<seq2seq4repair_space>file,<seq2seq4repair_space>use<seq2seq4repair_space>'samtools<seq2seq4repair_space>faidx').<seq2seq4repair_space>" ) File referenceFasta ; @ Parameter ( names = { "--output-cram-file" , "-O" } , converter = FileConverter . class , description = "The<seq2seq4repair_space>path<seq2seq4repair_space>for<seq2seq4repair_space>the<seq2seq4repair_space>output<seq2seq4repair_space>CRAM<seq2seq4repair_space>file.<seq2seq4repair_space>Omit<seq2seq4repair_space>if<seq2seq4repair_space>standard<seq2seq4repair_space>output<seq2seq4repair_space>(pipe)." ) File outputCramFile = null ; @ Parameter ( names = { "--max-records" } , description = "Stop<seq2seq4repair_space>after<seq2seq4repair_space>compressing<seq2seq4repair_space>this<seq2seq4repair_space>many<seq2seq4repair_space>records.<seq2seq4repair_space>" ) long maxRecords = Long . MAX_VALUE ; @ Parameter List < String > sequences ; @ Parameter ( names = { "-h" , "--help" } , description = "Print<seq2seq4repair_space>help<seq2seq4repair_space>and<seq2seq4repair_space>quit" ) boolean help = false ; @ Parameter ( names = { "--max-slice-size" }

Fix:
} while ( ( samRecord != null ) && ( ! ( samRecords . isEmpty ( ) ) ) ) ;