Skip to content

Commit

Permalink
Improve Exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
fcyu committed Feb 11, 2018
1 parent 0c60e56 commit da0345c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 24 deletions.
10 changes: 3 additions & 7 deletions src/main/java/proteomics/ECL2.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import proteomics.TheoSeq.MassTool;
import proteomics.Validation.CalFDR;
import uk.ac.ebi.pride.tools.jmzreader.JMzReader;
import uk.ac.ebi.pride.tools.jmzreader.JMzReaderException;
import uk.ac.ebi.pride.tools.mgf_parser.MgfFile;
import uk.ac.ebi.pride.tools.mzxml_parser.MzXMLFile;
import uk.ac.ebi.pride.tools.mzxml_parser.MzXMLParsingException;

import java.io.*;
import java.net.InetAddress;
Expand Down Expand Up @@ -64,18 +62,17 @@ public static void main(String[] args) {
logger.info("Spectra file: {}.", spectra_path);

new ECL2(parameter_path, spectra_path, dbName);
} catch (IOException | MzXMLParsingException | JMzReaderException | ExecutionException | InterruptedException | ClassNotFoundException | IllegalAccessException | InstantiationException | SQLException ex) {
} catch (Exception ex) {
ex.printStackTrace();
logger.error(ex.toString());
System.exit(1);
} finally {
if (dbName != null) {
(new File(dbName)).delete();
}
}
}

private ECL2(String parameter_path, String spectra_path, String dbName) throws IOException, MzXMLParsingException, JMzReaderException, ExecutionException, InterruptedException, ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
private ECL2(String parameter_path, String spectra_path, String dbName) throws Exception{
// Get the parameter map
Parameter parameter = new Parameter(parameter_path);
Map<String, String> parameter_map = parameter.returnParameterMap();
Expand Down Expand Up @@ -133,8 +130,7 @@ private ECL2(String parameter_path, String spectra_path, String dbName) throws I
} else if (ext.toLowerCase().contentEquals("mgf")) {
spectra_parser = new MgfFile(spectra_file);
} else {
logger.error("Unsupported data format {}. ECL2 only support mzXML and MGF.", ext);
System.exit(1);
throw new Exception(String.format(Locale.US, "Unsupported data format %s. ECL2 only support mzXML and MGF.", ext));
}

double ms1Tolerance = Double.valueOf(parameter_map.get("ms1_tolerance"));
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/proteomics/Index/BuildIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import proteomics.TheoSeq.MassTool;
import proteomics.Types.*;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -31,7 +30,7 @@ public class BuildIndex {
private final double ms1_bin_size;
private final double inverseMs1BinSize;

public BuildIndex(Map<String, String> parameter_map) throws IOException {
public BuildIndex(Map<String, String> parameter_map) throws Exception {
// initialize parameters
int min_chain_length = Integer.valueOf(parameter_map.get("min_chain_length")) + 2; // n and c are counted in the sequence
int max_chain_length = Integer.valueOf(parameter_map.get("max_chain_length")) + 2; // n and c are counted in the sequence
Expand Down Expand Up @@ -75,17 +74,15 @@ public BuildIndex(Map<String, String> parameter_map) throws IOException {
if (linker_type == 1) {
if (Math.abs(fix_mod_map.get('K') - fix_mod_map.get('n')) > 1e-6) {
linker_mass = 0;
logger.error("The link sites have different fix modifications.");
System.exit(1);
throw new Exception("The link sites have different fix modifications.");
} else {
linker_mass = Double.valueOf(parameter_map.get("cl_mass")) - fix_mod_map.get('K');
}
} else if (linker_type == 2) {
linker_mass = Double.valueOf(parameter_map.get("cl_mass"));
} else {
linker_mass = 0;
logger.error("The cross-linker type cannot be recognized.");
System.exit(1);
throw new Exception("The cross-linker type cannot be recognized.");
}

// read protein database
Expand Down Expand Up @@ -576,7 +573,7 @@ private Set<Short> getLinkSiteSet(String seq, boolean n_term, boolean c_term, sh
return output;
}

private void getVarModParams(String v, Set<VarModParam> varModParamSet, Set<BinaryModParam> binaryModParamSet) {
private void getVarModParams(String v, Set<VarModParam> varModParamSet, Set<BinaryModParam> binaryModParamSet) throws Exception {
Matcher varModMatcher = varModParamPattern.matcher(v);
if (varModMatcher.matches()) {
double modMass = Double.valueOf(varModMatcher.group(1));
Expand All @@ -594,8 +591,7 @@ private void getVarModParams(String v, Set<VarModParam> varModParamSet, Set<Bina
}
}
} else {
logger.error("Cannot parse variable modification parameter from {}.", v);
System.exit(1);
throw new Exception(String.format(Locale.US, "Cannot parse variable modification parameter from %s.", v));
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/proteomics/Parameter/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ public class Parameter {

private Map<String, String> parameterMap = new LinkedHashMap<>(); // keep the parameter order as it is in the parameter.def

public Parameter(String parameterFile) throws IOException {
public Parameter(String parameterFile) throws Exception {
BufferedReader parameterReader = new BufferedReader(new FileReader(parameterFile));
String line = parameterReader.readLine().trim();
if (!line.contentEquals("# " + ECL2.version)) {
logger.error("The parameter file version ({}) is not compatible with current ECL2 version ({}).", line.substring(2), ECL2.version);
System.exit(1);
throw new Exception(String.format(Locale.US, "The parameter file version (%s) is not compatible with current ECL2 version (%s).", line.substring(2), ECL2.version));
}
while ((line = parameterReader.readLine()) != null) {
line = line.trim();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/proteomics/Spectrum/PreSpectra.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public PreSpectra(JMzReader spectra_parser, double ms1Tolerance, double leftInve
} else if (matcher3.find()) {
scan_num = Integer.valueOf(matcher3.group(1));
} else {
logger.error("Cannot get scan number from the MGF title {}. Please report your MGF title to the author.", mgfTitle);
System.exit(1);
throw new NullPointerException(String.format(Locale.US, "Cannot get scan number from the MGF title %s. Please report your MGF title to the author.", mgfTitle));
}
} else {
scan_num = Integer.valueOf(spectrum.getId());
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/proteomics/TheoSeq/DbTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public DbTool(String db_name, String databaseType) throws IOException {
} else if (databaseType.contentEquals("Others")) {
header_pattern = Pattern.compile("^>(.+)$");
} else {
header_pattern = null;
logger.error("Incorrect database type ({}) in the parameter file.", databaseType);
System.exit(1);
throw new NullPointerException(String.format(Locale.US, "Incorrect database type (%s) in the parameter file.", databaseType));
}

BufferedReader db_reader;
Expand Down

0 comments on commit da0345c

Please sign in to comment.