Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Improve exception handling
Browse files Browse the repository at this point in the history
Also start IGV after loading variants, this way there is no leftover process after shutdown.
  • Loading branch information
Marius Wöste committed Dec 22, 2017
1 parent dfc32fe commit 43331ab
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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);
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/de/imi/marw/viper/api/ViperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +48,7 @@ public class ViperServer {
private final Gson gson;
private final VariantClusterBuilder clusterer;
private VariantTableCluster variantTableCluster;
private boolean sparkInitDone;

private IGVVisualizer igv;

Expand All @@ -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());
Expand All @@ -102,6 +114,8 @@ private void setupRoutes() {
setupTableApi();

init();

this.sparkInitDone = true;
}

private void setupTableApi() {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/de/imi/marw/viper/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,7 +183,7 @@ private List<List<Object>> parseVariantCalls(String[] colNames, List<String[]> c
}

@Override
public VariantTable readTable(String fileName) {
public VariantTable readTable(String fileName) throws FileNotFoundException, IOException {
try (Reader reader = new FileReader(fileName)) {

List<String[]> rawStrings = new ArrayList<>();
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/de/imi/marw/viper/test/api/APITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/de/imi/marw/viper/test/filtering/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));

Expand All @@ -58,7 +59,7 @@ private void checkIDs(VariantTable table, String... expectedIdsAfterFiltering) {
}

@Test
public void numericFiltersWorkingCorrectly() {
public void numericFiltersWorkingCorrectly() throws IOException {

VariantTable table = loadData();

Expand All @@ -79,7 +80,7 @@ public void numericFiltersWorkingCorrectly() {
}

@Test
public void numericCollectionFilterWorkingCorrectly() {
public void numericCollectionFilterWorkingCorrectly() throws IOException {

VariantTable table = loadData();

Expand All @@ -100,7 +101,7 @@ public void numericCollectionFilterWorkingCorrectly() {
}

@Test
public void stringFilterWorkingCorrectly() {
public void stringFilterWorkingCorrectly() throws IOException {

VariantTable table = loadData();

Expand All @@ -120,7 +121,7 @@ public void stringFilterWorkingCorrectly() {
}

@Test
public void stringCollectionFilterWorkingCorrectly() {
public void stringCollectionFilterWorkingCorrectly() throws IOException {

VariantTable table = loadData();

Expand All @@ -144,7 +145,7 @@ public void stringCollectionFilterWorkingCorrectly() {
}

@Test
public void multipleFiltersWorkingCorrectly() {
public void multipleFiltersWorkingCorrectly() throws IOException {

VariantTable table = loadData();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +35,7 @@
public class CallStringifierTest {

@Test
public void callsAreCorrectlyTurnedIntoStrings() {
public void callsAreCorrectlyTurnedIntoStrings() throws IOException {

CallStringifier callStringifier = new CallStringifier(",");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +47,7 @@ private <T> Collection<T> coll(T... vals) {
}

@Test
public void correctlyReadsSmallExample() {
public void correctlyReadsSmallExample() throws IOException {

CsvTableReader reader = new CsvTableReader(';', ",");

Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -155,7 +156,7 @@ public void correctlyReadsEdgeCases() {
}

@Test
public void correctlyReadsEmptyTable() {
public void correctlyReadsEmptyTable() throws IOException {

CsvTableReader reader = new CsvTableReader(';', ",");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,15 +33,15 @@
*/
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();

return clustered;
}

@Test
public void progressIsProperlySavedAndLoaded() {
public void progressIsProperlySavedAndLoaded() throws IOException {

VariantTable clustered1 = loadData();
VariantTable clustered2 = loadData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +43,7 @@ private <T> Collection<T> coll(T... vals) {
}

@Test
public void variantsAreCorrectlyClustered() {
public void variantsAreCorrectlyClustered() throws IOException {

CsvTableReader reader = new CsvTableReader(';', ",");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(",");
Expand All @@ -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");

Expand Down

0 comments on commit 43331ab

Please sign in to comment.