Skip to content

Commit 911ba72

Browse files
committed
Better exception handling.
1 parent 42f3337 commit 911ba72

File tree

6 files changed

+52
-71
lines changed

6 files changed

+52
-71
lines changed

biojava-core/src/main/java/org/biojava/nbio/core/search/io/ResultFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.biojava.nbio.core.search.io;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.text.ParseException;
46
import java.util.List;
57

68
/**
@@ -28,15 +30,15 @@ public interface ResultFactory {
2830
* @return
2931
* @throws Exception
3032
*/
31-
List<Result> createObjects(double maxEScore) throws Exception;
33+
List<Result> createObjects(double maxEScore) throws IOException, ParseException;
3234
/**
3335
* The factory that implements this method will be able to save the Search results
3436
* to a file in the same format that it is able to read.
3537
*
3638
* @param results
3739
* @throws Exception
3840
*/
39-
void storeObjects(List<Result> results) throws Exception;
41+
void storeObjects(List<Result> results) throws IOException, ParseException;
4042

4143
/**
4244
* Specify the collection of sequences objects used as queries in the Search run.

biojava-core/src/main/java/org/biojava/nbio/core/search/io/SearchIO.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.biojava.nbio.core.search.io;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.text.ParseException;
46
import java.util.HashMap;
57
import java.util.Iterator;
68
import java.util.List;
@@ -42,7 +44,7 @@ public class SearchIO implements Iterable<Result>{
4244
* @param f
4345
* @throws Exception
4446
*/
45-
public SearchIO (File f) throws Exception{
47+
public SearchIO (File f) throws IOException, ParseException{
4648
factory = guessFactory(f);
4749
file = f;
4850
if (file.exists()) readResults();
@@ -54,9 +56,11 @@ public SearchIO (File f) throws Exception{
5456
*
5557
* @param f
5658
* @param factory
57-
* @throws Exception
59+
*
60+
* @throws java.io.IOException for file access related issues
61+
* @throws java.text.ParseException for file format related issues
5862
*/
59-
public SearchIO (File f, ResultFactory factory) throws Exception{
63+
public SearchIO (File f, ResultFactory factory) throws IOException, ParseException{
6064
file = f;
6165
this.factory = factory;
6266
if (file.exists()) readResults();
@@ -68,9 +72,11 @@ public SearchIO (File f, ResultFactory factory) throws Exception{
6872
* @param f
6973
* @param factory
7074
* @param maxEvalue
71-
* @throws Exception
75+
*
76+
* @throws java.io.IOException for file access related issues
77+
* @throws java.text.ParseException for file format related issues
7278
*/
73-
public SearchIO(File f, ResultFactory factory, double maxEvalue) throws Exception{
79+
public SearchIO(File f, ResultFactory factory, double maxEvalue) throws IOException, ParseException{
7480
file = f;
7581
this.factory = factory;
7682
this.evalueThreshold = maxEvalue;
@@ -81,18 +87,21 @@ public SearchIO(File f, ResultFactory factory, double maxEvalue) throws Exceptio
8187
* This method is declared private because it is the default action of constructor
8288
* when file exists
8389
*
84-
* @throws Exception
90+
* @throws java.io.IOException for file access related issues
91+
* @throws java.text.ParseException for file format related issues
8592
*/
86-
private void readResults() throws Exception {
93+
private void readResults() throws IOException, ParseException {
8794
factory.setFile(file);
8895
results = factory.createObjects(evalueThreshold);
8996
}
9097

9198
/**
9299
* used to write a search report using the guessed or specified factory
93-
* @throws Exception
100+
*
101+
* @throws java.io.IOException for file access related issues
102+
* @throws java.text.ParseException for file format related issues
94103
*/
95-
void writeResults() throws Exception {
104+
public void writeResults() throws IOException, ParseException {
96105
factory.setFile(file);
97106
factory.createObjects(evalueThreshold);
98107
}

biojava-core/src/main/java/org/biojava/nbio/core/search/io/blast/BlastTabularParser.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileReader;
6+
import java.io.IOException;
67
import java.io.LineNumberReader;
8+
import java.text.ParseException;
79
import java.util.ArrayList;
810
import java.util.HashMap;
911
import java.util.List;
1012
import java.util.Scanner;
1113
import java.util.logging.Logger;
12-
import org.biojava.nbio.core.exceptions.ParserException;
1314
import org.biojava.nbio.core.search.io.Hit;
1415
import org.biojava.nbio.core.search.io.Hsp;
1516
import org.biojava.nbio.core.search.io.Result;
@@ -77,7 +78,7 @@ public void setFile(File f) {
7778
}
7879

7980
@Override
80-
public List<Result> createObjects(double maxEScore) throws Exception {
81+
public List<Result> createObjects(double maxEScore) throws IOException, ParseException {
8182
List<Result> results = new ArrayList();
8283

8384
log.info("Query for hits");
@@ -91,6 +92,7 @@ public List<Result> createObjects(double maxEScore) throws Exception {
9192
Scanner scanner = new Scanner(fileInputStream);
9293

9394
String line = fetchData(scanner);
95+
int lineNumber=1;
9496
while (scanner.hasNext()){
9597
try {
9698
BlastResultBuilder resultBuilder = new BlastResultBuilder();
@@ -113,6 +115,7 @@ public List<Result> createObjects(double maxEScore) throws Exception {
113115
while (currentSubjectId.equals(subjectId) && scanner.hasNext()){
114116
if (new Double(evalue) > maxEScore) {
115117
line = fetchData(scanner);
118+
lineNumber++;
116119
continue;
117120
}
118121
BlastHspBuilder hspBuilder = new BlastHspBuilder();
@@ -129,12 +132,13 @@ public List<Result> createObjects(double maxEScore) throws Exception {
129132
.setMismatchCount(new Integer(mismatchCount));
130133
hsps.add(hspBuilder.createBlastHsp());
131134
line = fetchData(scanner);
135+
lineNumber++;
132136
}
133137
hits.add(hitBuilder.setHsps(hsps).createBlastHit());
134138
}
135139
results.add(resultBuilder.setHits(hits).createBlastResult());
136140
} catch (NumberFormatException e) {
137-
throw new ParserException("Invalid numeric value met in:\n"+line);
141+
throw new ParseException("Invalid numeric value met at line "+ lineNumber+" in:\n"+line,0);
138142
}
139143
}
140144
return results;
@@ -190,8 +194,8 @@ private String fetchData(Scanner scanner){
190194
}
191195

192196
@Override
193-
public void storeObjects(List<Result> results) throws Exception {
194-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
197+
public void storeObjects(List<Result> results) throws IOException, ParseException {
198+
throw new UnsupportedOperationException("Not supported yet.");
195199
}
196200

197201
/**

biojava-core/src/main/java/org/biojava/nbio/core/search/io/blast/BlastXMLQuery.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
import org.biojava.nbio.core.search.io.Result;
77
import org.biojava.nbio.core.search.io.ResultFactory;
88
import java.io.File;
9+
import java.io.IOException;
910
import java.text.ParseException;
1011
import java.util.ArrayList;
1112
import java.util.HashMap;
1213
import java.util.LinkedHashMap;
1314
import java.util.List;
1415
import java.util.Map;
16+
import java.util.logging.Level;
1517
import java.util.logging.Logger;
18+
import javax.xml.parsers.ParserConfigurationException;
1619
import javax.xml.xpath.XPathException;
1720
import org.biojava.nbio.core.sequence.template.Sequence;
1821
import org.biojava.nbio.core.util.XMLHelper;
1922
import org.w3c.dom.Document;
2023
import org.w3c.dom.Element;
24+
import org.xml.sax.SAXException;
2125

2226
/**
2327
* Re-designed by Paolo Pavan.
@@ -89,21 +93,25 @@ public LinkedHashMap<String, ArrayList<String>> getHitsQueryDef(double maxEScore
8993
return hitsHashMap;
9094
}
9195

92-
private void readFile(String blastFile) throws Exception{
96+
private void readFile(String blastFile) throws IOException, ParseException{
9397
log.info("Start reading " + blastFile);
94-
blastDoc = XMLHelper.loadXML(blastFile);
98+
try {
99+
blastDoc = XMLHelper.loadXML(blastFile);
100+
} catch (SAXException ex) {
101+
Logger.getLogger(BlastXMLQuery.class.getName()).log(Level.SEVERE, null, ex);
102+
throw new ParseException(ex.getMessage(),0);
103+
} catch (ParserConfigurationException ex) {
104+
Logger.getLogger(BlastXMLQuery.class.getName()).log(Level.SEVERE, null, ex);
105+
throw new ParseException(ex.getMessage(),0);
106+
}
95107
log.info("Read finished");
96108
}
97109

98-
public ArrayList<Result> createObjects(double maxEScore) throws ParseException {
110+
public List<Result> createObjects(double maxEScore) throws IOException, ParseException {
99111
if (targetFile == null) throw new IllegalStateException("File to be parsed not specified.");
100112

101-
try {
102-
// getAbsolutePath throws SecurityException
103-
readFile(targetFile.getAbsolutePath());
104-
} catch (Exception e){
105-
throw new ParseException(e.getMessage(),0);
106-
}
113+
// getAbsolutePath throws SecurityException
114+
readFile(targetFile.getAbsolutePath());
107115
// create mappings between sequences and blast id
108116
mapIds();
109117

@@ -249,7 +257,7 @@ private void mapIds() {
249257
}
250258

251259
@Override
252-
public void storeObjects(List<Result> results) throws Exception {
260+
public void storeObjects(List<Result> results) throws IOException, ParseException {
253261
throw new UnsupportedOperationException("This parser does not support writing yet.");
254262
}
255263
}

biojava-core/src/test/java/org/biojava/nbio/core/search/io/blast/BlastTabularParserTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package org.biojava.nbio.core.search.io.blast;
72

83
import java.io.File;
@@ -21,7 +16,7 @@
2116

2217
/**
2318
*
24-
* @author pavanpa
19+
* @author Paolo Pavan
2520
*/
2621
public class BlastTabularParserTest {
2722

biojava-core/src/test/java/org/biojava/nbio/core/search/io/blast/BlastXMLQueryTest.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
/**
2828
*
29-
* @author pavanpa
29+
* @author Paolo Pavan
3030
*/
3131
public class BlastXMLQueryTest {
3232

@@ -76,7 +76,7 @@ public void testCreateObjects() throws Exception {
7676

7777
//instance.setQueryReferences(null);
7878
//instance.setDatabaseReferences(null);
79-
ArrayList<Result> result = instance.createObjects(1e-10);
79+
List<Result> result = instance.createObjects(1e-10);
8080

8181
// test with random manual selected results
8282
BlastHsp hsp1hit1res1 = new BlastHspBuilder()
@@ -171,41 +171,4 @@ public void testGetFileExtensions() {
171171
public void testStoreObjects() throws Exception {
172172
// not implemented yet
173173
}
174-
175-
private File unpackResourceFile(String resource) {
176-
177-
File file = null;
178-
URL res = getClass().getResource(resource);
179-
if (res == null) {
180-
throw new RuntimeException(" not supported");
181-
}
182-
if (res.toString().startsWith("jar:")) {
183-
try {
184-
InputStream input = getClass().getResourceAsStream(resource);
185-
file = File.createTempFile("javawrapped", ".exe");
186-
//if (!DEBUG) file.deleteOnExit();
187-
file.setExecutable(true, true);
188-
OutputStream out = new FileOutputStream(file);
189-
int read;
190-
byte[] bytes = new byte[1024];
191-
192-
while ((read = input.read(bytes)) != -1) {
193-
out.write(bytes, 0, read);
194-
}
195-
out.close();
196-
out.flush();
197-
} catch (IOException ex) {
198-
throw new RuntimeException("Resource not available for this OS!");
199-
}
200-
} else {
201-
//this will probably work in your IDE, but not from a JAR
202-
file = new File(res.getFile());
203-
}
204-
205-
if (file == null) {
206-
throw new RuntimeException("Error: File " + file + " not found!");
207-
}
208-
return file;
209-
}
210-
211174
}

0 commit comments

Comments
 (0)