Skip to content

Commit 1b60601

Browse files
author
me
committed
url cache updates
1 parent 80f097f commit 1b60601

24 files changed

Lines changed: 169 additions & 173 deletions

File tree

biojava-core/src/main/java/org/biojava/nbio/core/exceptions/CompoundNotFoundException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
*/
2626
package org.biojava.nbio.core.exceptions;
2727

28-
public class CompoundNotFoundException extends Exception {
28+
import java.io.IOException;
29+
30+
public class CompoundNotFoundException extends IOException {
2931
/**
3032
*
3133
*/

biojava-core/src/main/java/org/biojava/nbio/core/sequence/ProteinSequence.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.biojava.nbio.core.sequence.template.AbstractSequence;
3636
import org.biojava.nbio.core.sequence.template.CompoundSet;
3737
import org.biojava.nbio.core.sequence.template.ProxySequenceReader;
38-
import org.biojava.nbio.core.util.FileDownloadUtils;
38+
import org.biojava.nbio.core.util.Download;
3939
import org.slf4j.Logger;
4040
import org.slf4j.LoggerFactory;
4141

@@ -157,9 +157,9 @@ private DNASequence getRawParentSequence(String accessId) throws IOException {
157157
String seqUrlTemplate = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=%s&rettype=fasta&retmode=text";
158158
URL url = new URL(String.format(seqUrlTemplate, accessId));
159159

160-
logger.trace("Getting parent DNA sequence from URL: {}", url.toString());
160+
logger.trace("Getting parent DNA sequence from URL: {}", url);
161161

162-
InputStream is = FileDownloadUtils.downloadStream(url, 60000); //url.openConnection().getInputStream();
162+
InputStream is = Download.stream(url); //url.openConnection().getInputStream();
163163

164164
FastaReader<DNASequence, NucleotideCompound> parentReader
165165
= new FastaReader<>(is,

biojava-core/src/main/java/org/biojava/nbio/core/sequence/features/FeatureRetriever.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
package org.biojava.nbio.core.sequence.features;
2424

2525
import java.util.ArrayList;
26-
import java.util.HashMap;
26+
import java.util.Map;
27+
2728
/**
2829
* If a SequenceProxyReader implements this interface then that external source
2930
* has a list features
3031
* @author @author Paolo Pavan
3132
*/
3233
public interface FeatureRetriever {
33-
HashMap<String, ArrayList<AbstractFeature>> getFeatures();
34+
Map<String, ArrayList<AbstractFeature>> getFeatures();
3435
}

biojava-core/src/main/java/org/biojava/nbio/core/sequence/io/GenbankSequenceParser.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
import java.io.BufferedReader;
5656
import java.io.IOException;
57+
import java.io.Reader;
5758
import java.util.ArrayList;
5859
import java.util.HashMap;
5960
import java.util.LinkedHashMap;
@@ -143,19 +144,20 @@ private String parse(BufferedReader bufferedReader) {
143144
// Get an ordered list of key->value pairs in array-tuples
144145
do {
145146
section = this.readSection(bufferedReader);
146-
sectionKey = section.get(0)[0];
147+
String[] s0 = section.get(0);
148+
sectionKey = s0[0];
147149
if (sectionKey == null) {
148150
//if we reach the end of the file, section contains empty strings
149-
if(section.get(0)[1]==null || section.get(0)[1].equals("") ||
150-
section.get(0)[1].length()==0) {
151+
if(s0[1]==null || s0[1].equals("") ||
152+
s0[1].length()==0) {
151153
throw new ParserException(Messages.ENDOFFILE);
152154
}
153155
throw new ParserException(Messages.SECTIONKEYNULL);
154156
}
155157
// process section-by-section
156158
switch (sectionKey) {
157159
case LOCUS_TAG: {
158-
String loc = section.get(0)[1];
160+
String loc = s0[1];
159161
header = loc;
160162
Matcher m = lp.matcher(loc);
161163
if (m.matches()) {
@@ -169,7 +171,7 @@ private String parse(BufferedReader bufferedReader) {
169171
compoundType = AminoAcidCompoundSet.aminoAcidCompoundSet;
170172
} else if (lengthUnits.equalsIgnoreCase("bp")) {
171173
if (type != null) {
172-
if (type.contains("RNA")) {
174+
if (type.contains("RNA")) { //HACK
173175
compoundType = RNACompoundSet.getRNACompoundSet();
174176
} else {
175177
compoundType = DNACompoundSet.getDNACompoundSet();
@@ -179,25 +181,25 @@ private String parse(BufferedReader bufferedReader) {
179181
}
180182
}
181183

182-
log.debug("compound type: {}", compoundType.getClass().getSimpleName());
184+
//log.debug("compound type: {}", compoundType.getClass().getSimpleName());
183185

184186
} else {
185187
throw new ParserException("Bad locus line");
186188
}
187189
break;
188190
}
189191
case DEFINITION_TAG:
190-
headerParser.setDescription(section.get(0)[1]);
192+
headerParser.setDescription(s0[1]);
191193
break;
192194
case ACCESSION_TAG:
193195
// if multiple accessions, store only first as accession,
194196
// and store rest in annotation
195-
String[] accs = section.get(0)[1].split("\\s+");
197+
String[] accs = s0[1].split("\\s+");
196198
accession = accs[0].trim();
197199
headerParser.setAccession(accession);
198200
break;
199201
case VERSION_TAG: {
200-
String ver = section.get(0)[1];
202+
String ver = s0[1];
201203
Matcher m = vp.matcher(ver);
202204
if (m.matches()) {
203205
String verAcc = m.group(1);
@@ -244,15 +246,16 @@ private String parse(BufferedReader bufferedReader) {
244246
break;
245247
case COMMENT_TAG:
246248
// Set up some comments
247-
headerParser.setComment(section.get(0)[1]);
249+
headerParser.setComment(s0[1]);
248250
break;
249251
case FEATURE_TAG:
250252
// starting from second line of input, start a new feature whenever we come across
251253
// a key that does not start with /
252254
AbstractFeature gbFeature = null;
253255
for (int i = 1; i < section.size(); i++) {
254-
String key = section.get(i)[0];
255-
String val = section.get(i)[1];
256+
String[] si = section.get(i);
257+
String key = si[0];
258+
String val = si[1];
256259
if (key.startsWith("/")) {
257260
if (gbFeature == null) {
258261
throw new ParserException("Malformed GenBank file: found a qualifier without feature.");
@@ -271,7 +274,7 @@ private String parse(BufferedReader bufferedReader) {
271274
DBReferenceInfo xref = new DBReferenceInfo(dbname, raccession);
272275
gbFeature.addQualifier(key, xref);
273276

274-
ArrayList<DBReferenceInfo> listDBEntry = new ArrayList<>();
277+
ArrayList<DBReferenceInfo> listDBEntry = new ArrayList<>(1);
275278
listDBEntry.add(xref);
276279
mapDB.put(key, listDBEntry);
277280
} else {
@@ -284,24 +287,17 @@ private String parse(BufferedReader bufferedReader) {
284287
if (key.equalsIgnoreCase("translation")) {
285288
// strip spaces from sequence
286289
val = val.replaceAll("\\s+", "");
287-
Qualifier q = new Qualifier(key, val);
288-
gbFeature.addQualifier(key, q);
290+
gbFeature.addQualifier(key, new Qualifier(key, val));
289291
} else {
290-
Qualifier q = new Qualifier(key, val);
291-
gbFeature.addQualifier(key, q);
292+
gbFeature.addQualifier(key, new Qualifier(key, val));
292293
}
293294
}
294295
} else {
295296
// new feature!
296297
gbFeature = new TextFeature(key, val, key, key);
297-
Location l =
298-
locationParser.parse(val);
299-
gbFeature.setLocation((AbstractLocation) l);
298+
gbFeature.setLocation((AbstractLocation) locationParser.parse(val));
300299

301-
if (!featureCollection.containsKey(key)) {
302-
featureCollection.put(key, new ArrayList());
303-
}
304-
featureCollection.get(key).add(gbFeature);
300+
featureCollection.computeIfAbsent(key, (x)->new ArrayList<>(1)).add(gbFeature);
305301
}
306302
}
307303
break;
@@ -363,8 +359,7 @@ private List<String[]> readSection(BufferedReader bufferedReader) {
363359
// having only white space characters
364360
continue;
365361
}
366-
if (line == null
367-
|| (!line.startsWith(" ") && linecount++ > 0 && (!firstSecKey
362+
if (line == null || (!line.startsWith(" ") && linecount++ > 0 && (!firstSecKey
368363
.equals(START_SEQUENCE_TAG) || line
369364
.startsWith(END_SEQUENCE_TAG)))) {
370365
// dump out last part of section
@@ -417,7 +412,7 @@ public String getSequence(BufferedReader bufferedReader, int sequenceLength) {
417412
try {
418413
parse(bufferedReader);
419414
} catch (ParserException e) {
420-
if(e.getMessage().equalsIgnoreCase(Messages.ENDOFFILE)) return null;
415+
if (e.getMessage().equalsIgnoreCase(Messages.ENDOFFILE)) return null;
421416
else throw new ParserException(e.getMessage());
422417
}
423418

biojava-core/src/main/java/org/biojava/nbio/core/sequence/io/template/SequenceParserInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.BufferedReader;
2626
import java.io.IOException;
27+
import java.io.Reader;
2728

2829
/**
2930
*

0 commit comments

Comments
 (0)