From 43331ab282edf0590e150fc161f400e80eb7089c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20W=C3=B6ste?= Date: Fri, 22 Dec 2017 12:34:03 +0100 Subject: [PATCH] Improve exception handling Also start IGV after loading variants, this way there is no leftover process after shutdown. --- .../viper/api/TableReaderMultiplexer.java | 4 ++- .../de/imi/marw/viper/api/ViperServer.java | 26 ++++++++++++++----- .../java/de/imi/marw/viper/main/Main.java | 11 +++++++- .../viper/variants/table/CsvTableReader.java | 7 ++--- .../viper/variants/table/TableReader.java | 5 +++- .../viper/variants/table/VariantTable.java | 2 +- .../de/imi/marw/viper/test/api/APITest.java | 2 +- .../marw/viper/test/filtering/FilterTest.java | 13 +++++----- .../test/variants/CallStringifierTest.java | 3 ++- .../test/variants/CsvTableReaderTest.java | 9 ++++--- .../test/variants/ProgressManagerTest.java | 5 ++-- .../variants/VariantTableClusterTest.java | 3 ++- .../test/variants/VcfTableReaderTest.java | 11 ++++---- 13 files changed, 66 insertions(+), 35 deletions(-) diff --git a/src/main/java/de/imi/marw/viper/api/TableReaderMultiplexer.java b/src/main/java/de/imi/marw/viper/api/TableReaderMultiplexer.java index d375c25..2a8301e 100644 --- a/src/main/java/de/imi/marw/viper/api/TableReaderMultiplexer.java +++ b/src/main/java/de/imi/marw/viper/api/TableReaderMultiplexer.java @@ -22,6 +22,8 @@ import de.imi.marw.viper.variants.table.TableReader; import de.imi.marw.viper.variants.table.VariantTable; import de.imi.marw.viper.variants.table.VcfTableReader; +import java.io.FileNotFoundException; +import java.io.IOException; /** * @@ -38,7 +40,7 @@ public TableReaderMultiplexer(ViperServerConfig config) { } @Override - public VariantTable readTable(String fileName) { + public VariantTable readTable(String fileName) throws FileNotFoundException, IOException { if (fileName.endsWith(".vcf")) { return vcfReader.readTable(fileName); diff --git a/src/main/java/de/imi/marw/viper/api/ViperServer.java b/src/main/java/de/imi/marw/viper/api/ViperServer.java index 1bd27a4..b1dcbff 100644 --- a/src/main/java/de/imi/marw/viper/api/ViperServer.java +++ b/src/main/java/de/imi/marw/viper/api/ViperServer.java @@ -30,6 +30,8 @@ import de.imi.marw.viper.variants.table.VariantTable; import de.imi.marw.viper.visualization.IGVVisualizer; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import spark.Spark; @@ -46,6 +48,7 @@ public class ViperServer { private final Gson gson; private final VariantClusterBuilder clusterer; private VariantTableCluster variantTableCluster; + private boolean sparkInitDone; private IGVVisualizer igv; @@ -54,32 +57,41 @@ public ViperServer(ViperServerConfig config) { this.config = config; this.gson = new GsonBuilder().serializeNulls().create(); this.clusterer = new VariantClusterBuilder(config.getBreakpointTolerance(), !config.isClusteringEnabled()); + this.sparkInitDone = false; } - public void start() { + public void start() throws FileNotFoundException, IOException { + + this.variantTableCluster = this.loadVariants(); this.igv = this.setupIGV(); this.igv.start(); - this.variantTableCluster = this.loadVariants(); - this.igv.awaitStartup(); this.setupRoutes(); awaitInitialization(); + } public void stop() { - this.igv.shutdown(); - Spark.stop(); + + if (this.igv != null) { + this.igv.shutdown(); + } + + if (this.sparkInitDone) { + Spark.stop(); + } + } public ViperServerConfig getConfig() { return this.config; } - private VariantTableCluster loadVariants() { + private VariantTableCluster loadVariants() throws FileNotFoundException, IOException { TableReaderMultiplexer reader = new TableReaderMultiplexer(config); VariantTable unclusteredTable = reader.readTable(config.getAnalysisFile()); @@ -102,6 +114,8 @@ private void setupRoutes() { setupTableApi(); init(); + + this.sparkInitDone = true; } private void setupTableApi() { diff --git a/src/main/java/de/imi/marw/viper/main/Main.java b/src/main/java/de/imi/marw/viper/main/Main.java index 54e57c0..5b2866e 100644 --- a/src/main/java/de/imi/marw/viper/main/Main.java +++ b/src/main/java/de/imi/marw/viper/main/Main.java @@ -58,17 +58,26 @@ public static void main(String[] args) { configFileName = "config.json"; } + ViperServer server = null; + try { ViperServerConfig config = loadConfig(configFileName); - ViperServer server = new ViperServer(config); + server = new ViperServer(config); server.start(); } catch (FileNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + System.err.println("[ERROR] File not found. Make sure your configuration paths are set correctly."); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + } finally { + + if (server != null) { + server.stop(); + } + } } } diff --git a/src/main/java/de/imi/marw/viper/variants/table/CsvTableReader.java b/src/main/java/de/imi/marw/viper/variants/table/CsvTableReader.java index b368fe3..ee8b2fd 100644 --- a/src/main/java/de/imi/marw/viper/variants/table/CsvTableReader.java +++ b/src/main/java/de/imi/marw/viper/variants/table/CsvTableReader.java @@ -20,6 +20,7 @@ import de.imi.marw.viper.util.Util; import de.imi.marw.viper.variants.VariantPropertyType; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; @@ -182,7 +183,7 @@ private List> parseVariantCalls(String[] colNames, List c } @Override - public VariantTable readTable(String fileName) { + public VariantTable readTable(String fileName) throws FileNotFoundException, IOException { try (Reader reader = new FileReader(fileName)) { List rawStrings = new ArrayList<>(); @@ -208,11 +209,7 @@ public VariantTable readTable(String fileName) { return new VariantTable(parsedCalls, Arrays.asList(header), Arrays.asList(types)); - } catch (IOException ex) { - Logger.getLogger(CsvTableReader.class.getName()).log(Level.SEVERE, null, ex); } - - return null; } } diff --git a/src/main/java/de/imi/marw/viper/variants/table/TableReader.java b/src/main/java/de/imi/marw/viper/variants/table/TableReader.java index e11d51a..7a93486 100644 --- a/src/main/java/de/imi/marw/viper/variants/table/TableReader.java +++ b/src/main/java/de/imi/marw/viper/variants/table/TableReader.java @@ -18,10 +18,13 @@ */ package de.imi.marw.viper.variants.table; +import java.io.FileNotFoundException; +import java.io.IOException; + /** * * @author marius */ public interface TableReader { - VariantTable readTable(String fileName); + VariantTable readTable(String fileName) throws FileNotFoundException, IOException; } diff --git a/src/main/java/de/imi/marw/viper/variants/table/VariantTable.java b/src/main/java/de/imi/marw/viper/variants/table/VariantTable.java index a71a11d..d802a7b 100644 --- a/src/main/java/de/imi/marw/viper/variants/table/VariantTable.java +++ b/src/main/java/de/imi/marw/viper/variants/table/VariantTable.java @@ -275,7 +275,7 @@ private String getSingleColumnValue(List call, String column) { private void checkDataIntegrity() { if (Arrays.stream(MANDATORY_FIELDS).anyMatch((mandatory) -> !columnNames.contains(mandatory))) { - throw new IllegalArgumentException("Data must contain all mandatory columns"); + throw new IllegalArgumentException("Variant call tables must contain all mandatory columns: " + Arrays.toString(VariantTable.MANDATORY_FIELDS) + ". Make sure you use the correct delimiter."); } if (types.size() != columnNames.size()) { diff --git a/src/test/java/de/imi/marw/viper/test/api/APITest.java b/src/test/java/de/imi/marw/viper/test/api/APITest.java index fd6272c..fe256b9 100644 --- a/src/test/java/de/imi/marw/viper/test/api/APITest.java +++ b/src/test/java/de/imi/marw/viper/test/api/APITest.java @@ -60,7 +60,7 @@ private String readFromFile(String testResourceFileName) throws IOException { } @BeforeClass - public static void setUp() { + public static void setUp() throws IOException { ViperServerConfig conf = new ViperServerConfig(); conf.setAnalysisFile(TestUtil.getResourceFile("examples.csv")); diff --git a/src/test/java/de/imi/marw/viper/test/filtering/FilterTest.java b/src/test/java/de/imi/marw/viper/test/filtering/FilterTest.java index 857ccf7..2b6fa19 100644 --- a/src/test/java/de/imi/marw/viper/test/filtering/FilterTest.java +++ b/src/test/java/de/imi/marw/viper/test/filtering/FilterTest.java @@ -26,6 +26,7 @@ import de.imi.marw.viper.variants.filters.StringFilter; import de.imi.marw.viper.variants.table.CsvTableReader; import de.imi.marw.viper.variants.table.VariantTable; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -39,7 +40,7 @@ */ public class FilterTest { - private VariantTable loadData() { + private VariantTable loadData() throws IOException { CsvTableReader reader = new CsvTableReader(';', ","); VariantTable table = reader.readTable(TestUtil.getResourceFile("examples-filtering.csv")); @@ -58,7 +59,7 @@ private void checkIDs(VariantTable table, String... expectedIdsAfterFiltering) { } @Test - public void numericFiltersWorkingCorrectly() { + public void numericFiltersWorkingCorrectly() throws IOException { VariantTable table = loadData(); @@ -79,7 +80,7 @@ public void numericFiltersWorkingCorrectly() { } @Test - public void numericCollectionFilterWorkingCorrectly() { + public void numericCollectionFilterWorkingCorrectly() throws IOException { VariantTable table = loadData(); @@ -100,7 +101,7 @@ public void numericCollectionFilterWorkingCorrectly() { } @Test - public void stringFilterWorkingCorrectly() { + public void stringFilterWorkingCorrectly() throws IOException { VariantTable table = loadData(); @@ -120,7 +121,7 @@ public void stringFilterWorkingCorrectly() { } @Test - public void stringCollectionFilterWorkingCorrectly() { + public void stringCollectionFilterWorkingCorrectly() throws IOException { VariantTable table = loadData(); @@ -144,7 +145,7 @@ public void stringCollectionFilterWorkingCorrectly() { } @Test - public void multipleFiltersWorkingCorrectly() { + public void multipleFiltersWorkingCorrectly() throws IOException { VariantTable table = loadData(); diff --git a/src/test/java/de/imi/marw/viper/test/variants/CallStringifierTest.java b/src/test/java/de/imi/marw/viper/test/variants/CallStringifierTest.java index 1bf80a9..b1d19bc 100644 --- a/src/test/java/de/imi/marw/viper/test/variants/CallStringifierTest.java +++ b/src/test/java/de/imi/marw/viper/test/variants/CallStringifierTest.java @@ -22,6 +22,7 @@ import de.imi.marw.viper.variants.table.CallStringifier; import de.imi.marw.viper.variants.table.CsvTableReader; import de.imi.marw.viper.variants.table.VariantTable; +import java.io.IOException; import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertEquals; @@ -34,7 +35,7 @@ public class CallStringifierTest { @Test - public void callsAreCorrectlyTurnedIntoStrings() { + public void callsAreCorrectlyTurnedIntoStrings() throws IOException { CallStringifier callStringifier = new CallStringifier(","); diff --git a/src/test/java/de/imi/marw/viper/test/variants/CsvTableReaderTest.java b/src/test/java/de/imi/marw/viper/test/variants/CsvTableReaderTest.java index 6baf2ee..ee6ab90 100644 --- a/src/test/java/de/imi/marw/viper/test/variants/CsvTableReaderTest.java +++ b/src/test/java/de/imi/marw/viper/test/variants/CsvTableReaderTest.java @@ -22,6 +22,7 @@ import de.imi.marw.viper.variants.VariantPropertyType; import de.imi.marw.viper.variants.table.CsvTableReader; import de.imi.marw.viper.variants.table.VariantTable; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -46,7 +47,7 @@ private Collection coll(T... vals) { } @Test - public void correctlyReadsSmallExample() { + public void correctlyReadsSmallExample() throws IOException { CsvTableReader reader = new CsvTableReader(';', ","); @@ -81,7 +82,7 @@ public void correctlyReadsSmallExample() { } @Test - public void correctlyReadsBiggerExample() { + public void correctlyReadsBiggerExample() throws IOException { CsvTableReader reader = new CsvTableReader(';', ","); VariantTable table = reader.readTable(TestUtil.getResourceFile("examples.csv")); @@ -127,7 +128,7 @@ public void correctlyReadsBiggerExample() { } @Test - public void correctlyReadsEdgeCases() { + public void correctlyReadsEdgeCases() throws IOException { CsvTableReader reader = new CsvTableReader('\t', "_"); VariantTable table = reader.readTable(TestUtil.getResourceFile("examples-edge-cases.csv")); @@ -155,7 +156,7 @@ public void correctlyReadsEdgeCases() { } @Test - public void correctlyReadsEmptyTable() { + public void correctlyReadsEmptyTable() throws IOException { CsvTableReader reader = new CsvTableReader(';', ","); diff --git a/src/test/java/de/imi/marw/viper/test/variants/ProgressManagerTest.java b/src/test/java/de/imi/marw/viper/test/variants/ProgressManagerTest.java index 1de0b6c..7d04acd 100644 --- a/src/test/java/de/imi/marw/viper/test/variants/ProgressManagerTest.java +++ b/src/test/java/de/imi/marw/viper/test/variants/ProgressManagerTest.java @@ -23,6 +23,7 @@ import de.imi.marw.viper.variants.table.CsvTableReader; import de.imi.marw.viper.variants.table.DecisionManager; import de.imi.marw.viper.variants.table.VariantTable; +import java.io.IOException; import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -32,7 +33,7 @@ */ public class ProgressManagerTest { - private VariantTable loadData() { + private VariantTable loadData() throws IOException { VariantTable unclustered = new CsvTableReader(';', ",").readTable(TestUtil.getResourceFile("examples-unclustered.csv")); VariantTable clustered = new VariantClusterBuilder(5, false).clusterVariantTable(unclustered).getClusteredTable(); @@ -40,7 +41,7 @@ private VariantTable loadData() { } @Test - public void progressIsProperlySavedAndLoaded() { + public void progressIsProperlySavedAndLoaded() throws IOException { VariantTable clustered1 = loadData(); VariantTable clustered2 = loadData(); diff --git a/src/test/java/de/imi/marw/viper/test/variants/VariantTableClusterTest.java b/src/test/java/de/imi/marw/viper/test/variants/VariantTableClusterTest.java index 9e68530..0330e04 100644 --- a/src/test/java/de/imi/marw/viper/test/variants/VariantTableClusterTest.java +++ b/src/test/java/de/imi/marw/viper/test/variants/VariantTableClusterTest.java @@ -23,6 +23,7 @@ import de.imi.marw.viper.variants.VariantTableCluster; import de.imi.marw.viper.variants.table.CsvTableReader; import de.imi.marw.viper.variants.table.VariantTable; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -42,7 +43,7 @@ private Collection coll(T... vals) { } @Test - public void variantsAreCorrectlyClustered() { + public void variantsAreCorrectlyClustered() throws IOException { CsvTableReader reader = new CsvTableReader(';', ","); diff --git a/src/test/java/de/imi/marw/viper/test/variants/VcfTableReaderTest.java b/src/test/java/de/imi/marw/viper/test/variants/VcfTableReaderTest.java index 52113cb..aabd1d8 100644 --- a/src/test/java/de/imi/marw/viper/test/variants/VcfTableReaderTest.java +++ b/src/test/java/de/imi/marw/viper/test/variants/VcfTableReaderTest.java @@ -23,6 +23,7 @@ import de.imi.marw.viper.variants.table.CsvTableReader; import de.imi.marw.viper.variants.table.VariantTable; import de.imi.marw.viper.variants.table.VcfTableReader; +import java.io.IOException; import java.util.List; import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -34,7 +35,7 @@ public class VcfTableReaderTest { //TODO: check all combinations - private void checkVcfCorrectness(boolean simple, boolean excludeReferenceCalls, String targetVcf, String expectedFileName) { + private void checkVcfCorrectness(boolean simple, boolean excludeReferenceCalls, String targetVcf, String expectedFileName) throws IOException { CsvTableReader rd = new CsvTableReader(';', ","); CallStringifier strf = new CallStringifier(","); @@ -54,28 +55,28 @@ private void checkVcfCorrectness(boolean simple, boolean excludeReferenceCalls, } @Test - public void allSimpleCallsCorrectlyLoaded() { + public void allSimpleCallsCorrectlyLoaded() throws IOException { checkVcfCorrectness(true, false, "examples.vcf", "vcf-import-simple-all.csv"); } @Test - public void allNonRefCallsCorrectlyLoaded() { + public void allNonRefCallsCorrectlyLoaded() throws IOException { checkVcfCorrectness(true, true, "examples.vcf", "vcf-import-simple-nonref.csv"); } @Test - public void allNonRefComplexCallsCorrectlyLoaded() { + public void allNonRefComplexCallsCorrectlyLoaded() throws IOException { checkVcfCorrectness(false, true, "examples.vcf", "vcf-import-complex-nonref.csv"); } @Test - public void minimalVcfNonRefComplexCallsCorrectlyLoaded() { + public void minimalVcfNonRefComplexCallsCorrectlyLoaded() throws IOException { checkVcfCorrectness(false, false, "example-minimal.vcf", "vcf-import-minimal-complex-nonref.csv");