From 5b984acbabc173ccaf6b49d4363c6ab1189f2392 Mon Sep 17 00:00:00 2001 From: Alex Aiezza Date: Tue, 3 May 2016 17:20:42 -0400 Subject: [PATCH] Fix bug where FASTQ inflation caused crash --- .../rit/flick/genetics/FastFileDeflator.java | 2 +- .../rit/flick/genetics/FastFileInflator.java | 42 ++++++++++++++----- .../rit/flick/genetics/FastaFileInflator.java | 3 +- .../rit/flick/genetics/FastqFileInflator.java | 9 ++-- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/edu/rit/flick/genetics/FastFileDeflator.java b/src/edu/rit/flick/genetics/FastFileDeflator.java index c8222df..e68c2a8 100644 --- a/src/edu/rit/flick/genetics/FastFileDeflator.java +++ b/src/edu/rit/flick/genetics/FastFileDeflator.java @@ -164,7 +164,7 @@ public File deflate( final Configuration configuration, final File fileIn, final } catch ( final Exception e ) { if ( !interrupted ) - e.printStackTrace(); + System.err.println( e.getMessage() ); } }, "Default_Deflation_Thread" ); diff --git a/src/edu/rit/flick/genetics/FastFileInflator.java b/src/edu/rit/flick/genetics/FastFileInflator.java index dc37499..5d5a9d7 100644 --- a/src/edu/rit/flick/genetics/FastFileInflator.java +++ b/src/edu/rit/flick/genetics/FastFileInflator.java @@ -6,8 +6,8 @@ */ package edu.rit.flick.genetics; -import static edu.rit.flick.config.DefaultOptionSet.VERBOSE_FLAG; import static edu.rit.flick.config.DefaultOptionSet.DELETE_FLAG; +import static edu.rit.flick.config.DefaultOptionSet.VERBOSE_FLAG; import static org.apache.commons.io.FileUtils.getFile; import java.io.File; @@ -107,6 +107,9 @@ protected void beforeSequence() throws IOException protected void close() throws IOException, InterruptedException { + if ( fastOut == null ) + return; + fastOut.close(); iupacfile.close(); datahcf.close(); @@ -227,7 +230,10 @@ protected Properties getProperties( final File propertiesFile ) throws IOExcepti } @Override - public File inflate( final Configuration configuration, final File fileIn, final File fileOut ) + public synchronized File inflate( + final Configuration configuration, + final File fileIn, + final File fileOut ) { assert fileIn.exists(); @@ -264,7 +270,7 @@ public File inflate( final Configuration configuration, final File fileIn, final } catch ( final Exception e ) { if ( !interrupted ) - e.printStackTrace(); + System.err.println( e.getMessage() ); } }, "Default_Inflation_Thread" ); @@ -275,19 +281,32 @@ public File inflate( final Configuration configuration, final File fileIn, final configuration.setFlag( DELETE_FLAG, false ); try { + if ( inflateToDirectoryThread.isAlive() ) + inflateToDirectoryThread.interrupt(); + // Clean up IO close(); System.gc(); Thread.sleep( 100 ); - // Clean up temporary directory - FileUtils.deleteDirectory( tmpOutputDirectory ); - // Clean up INCOMPLETE output file - FileUtils.deleteQuietly( fileOut ); + synchronized ( this ) + { + while ( inflateToDirectoryThread.isAlive() ) + this.wait(); + } + } catch ( final IOException | InterruptedException e ) { e.printStackTrace(); + } finally + { + // Clean up temporary directory + FileUtils.deleteQuietly( tmpOutputDirectory ); + // Clean up INCOMPLETE output file + FileUtils.deleteQuietly( fileOut ); + System.out.println(); } + }, "Inflation_Cleaning_Thread" ); cleanHookAtomic.set( cleanHook ); @@ -342,7 +361,8 @@ protected File inflateFromFile( final File fileIn, final File tmpOutputDirectory zipFile.extractAll( tmpOutputDirectory.getPath() ); } catch ( final ZipException e ) { - e.printStackTrace(); + if ( !interrupted ) + System.err.println( e.getMessage() ); } return tmpOutputDirectory; @@ -423,7 +443,8 @@ protected final void writeNextHeader() seqDnaPosition.set( 0 ); } catch ( final IOException e ) { - e.printStackTrace(); + if ( !interrupted ) + e.printStackTrace(); } } @@ -454,7 +475,8 @@ protected void writeNucleotide( final byte base ) fastOut.write( nucleotide ); } catch ( final IOException e ) { - e.printStackTrace(); + if ( !interrupted ) + System.err.println( e.getMessage() ); } dnaPosition.increment(); } diff --git a/src/edu/rit/flick/genetics/FastaFileInflator.java b/src/edu/rit/flick/genetics/FastaFileInflator.java index 88e3b62..37db442 100644 --- a/src/edu/rit/flick/genetics/FastaFileInflator.java +++ b/src/edu/rit/flick/genetics/FastaFileInflator.java @@ -52,7 +52,8 @@ protected void afterWriteNucleotide() protected void close() throws IOException, InterruptedException { super.close(); - tandemFile.close(); + if ( tandemFile != null ) + tandemFile.close(); } @SuppressWarnings ( "resource" ) diff --git a/src/edu/rit/flick/genetics/FastqFileInflator.java b/src/edu/rit/flick/genetics/FastqFileInflator.java index 4f0ef64..ad2edd4 100644 --- a/src/edu/rit/flick/genetics/FastqFileInflator.java +++ b/src/edu/rit/flick/genetics/FastqFileInflator.java @@ -48,9 +48,12 @@ protected void beforeSequence() throws IOException @Override protected void close() throws IOException, InterruptedException { - lengthfile.close(); - commentsfile.close(); - scorefile.close(); + if ( lengthfile != null ) + lengthfile.close(); + if ( commentsfile != null ) + commentsfile.close(); + if ( scorefile != null ) + scorefile.close(); scorefile = null;