Skip to content

Commit 282bf5b

Browse files
committed
Fixing #784
1 parent 5d11f63 commit 282bf5b

File tree

1 file changed

+34
-46
lines changed

1 file changed

+34
-46
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/symmetry/utils/BlastClustReader.java

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
*/
2121
package org.biojava.nbio.structure.symmetry.utils;
2222

23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2326
import java.io.BufferedReader;
2427
import java.io.IOException;
2528
import java.io.InputStream;
@@ -33,21 +36,25 @@ public class BlastClustReader implements Serializable {
3336

3437
private static final long serialVersionUID = 1L;
3538

39+
private static final Logger logger = LoggerFactory.getLogger(BlastClustReader.class);
40+
3641
private int sequenceIdentity = 0;
37-
private List<List<String>> clusters = new ArrayList<List<String>>();
38-
private static final String coreUrl = "ftp://resources.rcsb.org/sequence/clusters/";
39-
private static List<Integer> seqIdentities = Arrays.asList(30, 40, 50, 70, 90, 95, 100);
42+
private List<List<String>> clusters = new ArrayList<>();
43+
// https://cdn.rcsb.org/resources/sequence/clusters/bc-95.out
44+
private static final String coreUrl = "https://cdn.rcsb.org/resources/sequence/clusters/";
45+
46+
private static final List<Integer> seqIdentities = Arrays.asList(30, 40, 50, 70, 90, 95, 100);
4047

4148
public BlastClustReader(int sequenceIdentity) {
4249
this.sequenceIdentity = sequenceIdentity;
4350
}
4451

45-
public List<List<String>> getPdbChainIdClusters() {
52+
public List<List<String>> getPdbChainIdClusters() throws IOException {
4653
loadClusters(sequenceIdentity);
4754
return clusters;
4855
}
4956

50-
public Map<String,String> getRepresentatives(String pdbId) {
57+
public Map<String,String> getRepresentatives(String pdbId) throws IOException {
5158
loadClusters(sequenceIdentity);
5259
String pdbIdUc = pdbId.toUpperCase();
5360

@@ -64,7 +71,7 @@ public Map<String,String> getRepresentatives(String pdbId) {
6471
return representatives;
6572
}
6673

67-
public String getRepresentativeChain(String pdbId, String chainId) {
74+
public String getRepresentativeChain(String pdbId, String chainId) throws IOException {
6875
loadClusters(sequenceIdentity);
6976

7077
String pdbChainId = pdbId.toUpperCase() + "." + chainId;
@@ -77,7 +84,7 @@ public String getRepresentativeChain(String pdbId, String chainId) {
7784
return "";
7885
}
7986

80-
public int indexOf(String pdbId, String chainId) {
87+
public int indexOf(String pdbId, String chainId) throws IOException {
8188
loadClusters(sequenceIdentity);
8289

8390
String pdbChainId = pdbId.toUpperCase() + "." + chainId;
@@ -91,7 +98,7 @@ public int indexOf(String pdbId, String chainId) {
9198
return -1;
9299
}
93100

94-
public List<List<String>> getPdbChainIdClusters(String pdbId) {
101+
public List<List<String>> getPdbChainIdClusters(String pdbId) throws IOException {
95102
loadClusters(sequenceIdentity);
96103
String pdbIdUpper = pdbId.toUpperCase();
97104

@@ -107,7 +114,7 @@ public List<List<String>> getPdbChainIdClusters(String pdbId) {
107114
return matches;
108115
}
109116

110-
public List<List<String>> getChainIdsInEntry(String pdbId) {
117+
public List<List<String>> getChainIdsInEntry(String pdbId) throws IOException {
111118
loadClusters(sequenceIdentity);
112119

113120
List<List<String>> matches = new ArrayList<List<String>>();
@@ -131,55 +138,36 @@ public List<List<String>> getChainIdsInEntry(String pdbId) {
131138
return matches;
132139
}
133140

134-
private void loadClusters(int sequenceIdentity) {
141+
private void loadClusters(int sequenceIdentity) throws IOException {
135142
// load clusters only once
136143
if (clusters.size() > 0) {
137144
return;
138145
}
139146

140147
if (!seqIdentities.contains(sequenceIdentity)) {
141-
System.err.println("Error: representative chains are not available for %sequence identity: "
142-
+ sequenceIdentity);
148+
logger.error("Representative chains are not available for %sequence identity: {}", sequenceIdentity);
143149
return;
144150
}
145151

146-
try {
147-
URL u = new URL(coreUrl + "bc-" + sequenceIdentity + ".out");
148-
InputStream stream = u.openStream();
149-
// URLConnection connection = u.openConnection();
150-
// connection.setConnectTimeout(60000);
151-
// InputStream stream = connection.getInputStream();
152-
153-
if (stream != null) {
154-
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
155-
156-
String line = null;
157-
try {
158-
while ((line = reader.readLine()) != null) {
159-
line = line.replaceAll("_", ".");
160-
List<String> cluster = Arrays.asList(line.split(" "));
161-
clusters.add(cluster);
162-
}
163-
reader.close();
164-
stream.close();
165-
} catch (IOException e) {
166-
//e.printStackTrace();
167-
} finally {
168-
// try {
169-
// System.out.println("closing reader");
170-
// reader.close();
171-
// stream.close();
172-
// } catch (IOException e) {
173-
// e.printStackTrace();
174-
// }
175-
}
176-
}
152+
String urlString = coreUrl + "bc-" + sequenceIdentity + ".out";
153+
URL u = new URL(urlString);
154+
InputStream stream = u.openStream();
177155

178-
} catch (Exception e) {
179-
e.printStackTrace();
156+
if (stream != null) {
157+
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
158+
159+
String line = null;
160+
while ((line = reader.readLine()) != null) {
161+
line = line.replaceAll("_", ".");
162+
List<String> cluster = Arrays.asList(line.split(" "));
163+
clusters.add(cluster);
164+
}
165+
reader.close();
166+
stream.close();
167+
} else {
168+
throw new IOException("Got null stream for URL " + urlString);
180169
}
181170

182-
return;
183171
}
184172

185173
}

0 commit comments

Comments
 (0)