Repository navigation
Expand file tree
/
Copy pathPDBStatus.java
More file actions
708 lines (616 loc) · 20.2 KB
/
Copy pathPDBStatus.java
File metadata and controls
708 lines (616 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
*/
/**
*
*/
package org.biojava.nbio.structure;
import org.biojava.nbio.structure.align.util.URLConnectionTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;
/**
* Methods for getting the status of a PDB file (current, obsolete, etc)
* and for accessing different versions of the structure.
*
* <p>All methods query the
* <a href="http://www.rcsb.org/pdb/rest/idStatus?structureId=1HHB,3HHB,4HHB">
* PDB website.</a>
*
* <p>PDB supersessions form a directed acyclic graph, where edges point from an
* obsolete ID to the entry that directly superseded it. For example, here are
* edges from one portion of the graph:<br/>
*
* 1CAT -> 3CAT<br/>
* 3CAT -> 7CAT<br/>
* 3CAT -> 8CAT<br/>
*
* <p>The methods {@link #getReplaces(String, boolean) getReplaces(pdbId, false)}/
* {@link #getReplacement(String, boolean, boolean) getReplacement(pdbId, false, true)}
* just get the incoming/outgoing edges for a single node. The recursive versions
* ({@link #getReplaces(String, boolean) getReplaces(pdbId, true)},
* {@link #getReplacement(String, boolean, boolean) getReplacement(pdbId, true, false)})
* will do a depth-first search up/down the tree and return a list of all nodes ]
* reached.
*
* <p>Finally, the getCurrent() method returns a single PDB ID from among the
* results of
* {@link #getReplacement(String, boolean) getReplacement(pdbId, true)}.
* To be consistent with the old REST ordering, this is the PDB ID that occurs
* last alphabetically.
*
* <p>Results are cached to reduce server load.
*
* @author Spencer Bliven <sbliven@ucsd.edu>
* @author Amr AL-Hossary
* @since 3.0.2
*/
public class PDBStatus {
private static final Logger logger = LoggerFactory.getLogger(PDBStatus.class);
public static final String DEFAULT_PDB_SERVER = "www.rcsb.org";
public static final String PDB_SERVER_PROPERTY = "PDB.SERVER";
/**
* saves the returned results for further use.
*
*/
//TODO Use SoftReferences to allow garbage collection
private static Map<String, Map<String, String>> recordsCache= new Hashtable<String, Map<String, String>>();
/**
* Represents the status of PDB IDs. 'OBSOLETE' and 'CURRENT' are the most
* common.
* @author Spencer Bliven <sbliven@ucsd.edu>
*
*/
public enum Status {
OBSOLETE,
CURRENT,
AUTH,
HOLD,
HPUB,
POLC,
PROC,
REFI,
REPL,
WAIT,
WDRN,
MODEL,
UNKNOWN;
/**
*
* @param statusStr
* @return
* @throws IllegalArgumentException If the string is not recognized
*/
public static Status fromString(String statusStr) {
Status status;
String statusStrUpper = statusStr.toUpperCase();
if(statusStrUpper.equalsIgnoreCase("OBSOLETE"))
status = Status.OBSOLETE;
else if(statusStrUpper.equalsIgnoreCase("CURRENT"))
status = Status.CURRENT;
else if(statusStrUpper.equalsIgnoreCase("AUTH"))
status = Status.AUTH;
else if(statusStrUpper.equalsIgnoreCase("HOLD"))
status = Status.HOLD;
else if(statusStrUpper.equalsIgnoreCase("HPUB"))
status = Status.HPUB;
else if(statusStrUpper.equalsIgnoreCase("POLC"))
status = Status.POLC;
else if(statusStrUpper.equalsIgnoreCase("PROC"))
status = Status.PROC;
else if(statusStrUpper.equalsIgnoreCase("REFI"))
status = Status.REFI;
else if(statusStrUpper.equalsIgnoreCase("REPL"))
status = Status.REPL;
else if(statusStrUpper.equalsIgnoreCase("WAIT"))
status = Status.WAIT;
else if(statusStrUpper.equalsIgnoreCase("WDRN"))
status = Status.WDRN;
else if(statusStrUpper.equalsIgnoreCase("MODEL"))
status = Status.MODEL;
else if(statusStrUpper.equalsIgnoreCase("UNKNOWN"))
status = Status.UNKNOWN;
else {
throw new IllegalArgumentException("Unable to parse status '"+statusStrUpper+"'.");
}
return status;
}
}
/**
* Get the status of the PDB in question.
*
* @param pdbId
* @return The status, or null if an error occurred.
*/
public static Status getStatus(String pdbId) {
Status[] statuses = getStatus(new String[] {pdbId});
if(statuses != null) {
assert(statuses.length == 1);
return statuses[0];
} else {
return null;
}
}
/**
* Get the status of the a collection of PDBs in question in a single query.
*
* @see #getStatus(String)
* @param pdbIds
* @return The status array, or null if an error occurred.
*/
public static Status[] getStatus(String[] pdbIds) {
Status[] statuses = new Status[pdbIds.length];
List<Map<String,String>> attrList = getStatusIdRecords(pdbIds);
//Expect a single record
if(attrList == null || attrList.size() != pdbIds.length) {
logger.error("Error getting Status for {} from the PDB website.", Arrays.toString(pdbIds));
return null;
}
for(int pdbNum = 0;pdbNum<pdbIds.length;pdbNum++) {
//Locate first element of attrList with matching structureId.
//attrList is usually short, so don't worry about performance
boolean foundAttr = false;
for( Map<String,String> attrs : attrList) {
//Check that the record matches pdbId
String id = attrs.get("structureId");
if(id == null || !id.equalsIgnoreCase(pdbIds[pdbNum])) {
continue;
}
//Check that the status is given
String statusStr = attrs.get("status");
Status status = null;
if(statusStr == null ) {
logger.error("No status returned for {}", pdbIds[pdbNum]);
statuses[pdbNum] = null;
} else {
status = Status.fromString(statusStr);
}
if(status == null) {
logger.error("Unknown status '{}'", statusStr);
statuses[pdbNum] = null;
}
statuses[pdbNum] = status;
foundAttr = true;
}
if(!foundAttr) {
logger.error("No result found for {}", pdbIds[pdbNum]);
statuses[pdbNum] = null;
}
}
return statuses;
}
/**
* Gets the current version of a PDB ID. This is equivalent to selecting
* the first element from
* {@link #getReplacement(String,boolean,boolean)
*
* @param oldPdbId
* @return The replacement for oldPdbId, or null if none are found or if an error occurred.
*/
public static String getCurrent(String oldPdbId) {
List<String> replacements = getReplacement(oldPdbId,true, false);
if(replacements != null && !replacements.isEmpty())
return replacements.get(0);
else
return null;
}
/**
* Gets the PDB which superseded oldPdbId. For CURRENT IDs, this will
* be itself. For obsolete IDs, the behavior depends on the recursion
* parameter. If false, only IDs which directly supersede oldPdbId are
* returned. If true, the replacements for obsolete records are recursively
* fetched, yielding a list of all current replacements of oldPdbId.
*
*
*
* @param oldPdbId A pdb ID
* @param recurse Indicates whether the replacements for obsolete records
* should be fetched.
* @param includeObsolete Indicates whether obsolete records should be
* included in the results.
* @return The PDB which replaced oldPdbId. This may be oldPdbId itself, for
* current records. A return value of null indicates that the ID has
* been removed from the PDB or that an error has occurred.
*/
public static List<String> getReplacement(String oldPdbId, boolean recurse, boolean includeObsolete) {
List<Map<String,String>> attrList = getStatusIdRecords(new String[] {oldPdbId});
//Expect a single record
if(attrList == null || attrList.size() != 1) {
logger.error("Error getting Status for {} from the PDB website.", oldPdbId);
return null;
}
Map<String,String> attrs = attrList.get(0);
//Check that the record matches pdbId
String id = attrs.get("structureId");
if(id == null || !id.equalsIgnoreCase(oldPdbId)) {
logger.error("Results returned from the query don't match {}", oldPdbId);
return null;
}
//Check that the status is given
String statusStr = attrs.get("status");
if(statusStr == null ) {
logger.error("No status returned for {}", oldPdbId);
return null;
}
Status status = Status.fromString(statusStr);
if(status == null ) {
logger.error("Unknown status '{}'", statusStr);
return null;
}
// If we're current, just return
LinkedList<String> results = new LinkedList<String>();
switch(status) {
case CURRENT:
results.add(oldPdbId);
return results;
case OBSOLETE: {
String replacementStr = attrs.get("replacedBy");
if(replacementStr == null) {
logger.error("{} is OBSOLETE but lacks a replacedBy attribute.", oldPdbId);
return null;
}
replacementStr = replacementStr.toUpperCase();
//include this result
if(includeObsolete) {
results.add(oldPdbId);
}
// Some PDBs are not replaced.
if(replacementStr.equals("NONE")) {
return results; //empty
}
String[] replacements = replacementStr.split(" ");
Arrays.sort(replacements, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareToIgnoreCase(o1);
}
});
for(String replacement : replacements) {
// Return the replacement.
if(recurse) {
List<String> others = PDBStatus.getReplacement(replacement, recurse, includeObsolete);
mergeReversed(results,others);
}
else {
if(includeObsolete) {
mergeReversed(results,Arrays.asList(replacement));
} else {
// check status of replacement
Status replacementStatus = getStatus(replacement);
switch(replacementStatus) {
case OBSOLETE:
//ignore obsolete
break;
case CURRENT:
default:
// include it
mergeReversed(results,Arrays.asList(replacement));
}
}
}
}
return results;
}
case UNKNOWN:
return null;
default: { //TODO handle other cases explicitly. They might have other syntax than "replacedBy"
String replacementStr = attrs.get("replacedBy");
if(replacementStr == null) {
// If no "replacedBy" attribute, treat like we're current
// TODO is this correct?
results.add(oldPdbId);
return results;
}
replacementStr = replacementStr.toUpperCase();
// Some PDBs are not replaced.
if(replacementStr.equals("NONE")) {
return null;
}
//include this result, since it's not obsolete
results.add(oldPdbId);
String[] replacements = replacementStr.split(" ");
Arrays.sort(replacements, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareToIgnoreCase(o1);
}
});
for(String replacement : replacements) {
// Return the replacement.
if(recurse) {
List<String> others = PDBStatus.getReplacement(replacement, recurse, includeObsolete);
mergeReversed(results,others);
}
else {
mergeReversed(results,Arrays.asList(replacement));
}
}
return results;
}
}
}
/**
* Takes two reverse sorted lists of strings and merges the second into the
* first. Duplicates are removed.
*
* @param merged A reverse sorted list. Modified by this method to contain
* the contents of other.
* @param other A reverse sorted list. Not modified.
*/
private static void mergeReversed(List<String> merged,
final List<String> other) {
if(other.isEmpty())
return;
if(merged.isEmpty()) {
merged.addAll(other);
return;
}
ListIterator<String> m = merged.listIterator();
ListIterator<String> o = other.listIterator();
String nextM, prevO;
prevO = o.next();
while(m.hasNext()) {
// peek at m
nextM = m.next();
m.previous();
//insert from O until exhausted or occurs after nextM
while(prevO.compareTo(nextM) > 0) {
m.add(prevO);
if(!o.hasNext()) {
return;
}
prevO = o.next();
}
//remove duplicates
if(prevO.equals(nextM)) {
if(!o.hasNext()) {
return;
}
prevO = o.next();
}
m.next();
}
m.add(prevO);
while(o.hasNext()) {
m.add(o.next());
}
}
/**
* Get the ID of the protein which was made obsolete by newPdbId.
*
* @param newPdbId PDB ID of the newer structure
* @param recurse If true, return all ancestors of newPdbId.
* Otherwise, just go one step newer than oldPdbId.
* @return A (possibly empty) list of ID(s) of the ancestor(s) of
* newPdbId, or <tt>null</tt> if an error occurred.
*/
public static List<String> getReplaces(String newPdbId, boolean recurse) {
List<Map<String,String>> attrList = getStatusIdRecords(new String[] {newPdbId});
//Expect a single record
if(attrList == null || attrList.size() != 1) {
//TODO Is it possible to have multiple record per ID?
// They seem to be combined into one record with space-delimited 'replaces'
logger.error("Error getting Status for {} from the PDB website.", newPdbId);
return null;
}
Map<String,String> attrs = attrList.get(0);
//Check that the record matches pdbId
String id = attrs.get("structureId");
if(id == null || !id.equals(newPdbId)) {
logger.error("Results returned from the query don't match {}", newPdbId);
return null;
}
String replacedList = attrs.get("replaces"); //space-delimited list
if(replacedList == null) {
// no replaces value; assume root
return new ArrayList<String>();
}
String[] directDescendents = replacedList.split("\\s");
// Not the root! Return the replaced PDB.
if(recurse) {
// Note: Assumes a proper directed acyclic graph of revisions
// Cycles will cause infinite loops.
List<String> allDescendents = new LinkedList<String>();
for(String replaced : directDescendents) {
List<String> roots = PDBStatus.getReplaces(replaced, recurse);
mergeReversed(allDescendents,roots);
}
mergeReversed(allDescendents,Arrays.asList(directDescendents));
return allDescendents;
} else {
return Arrays.asList(directDescendents);
}
}
/**
* The status of PDB IDs are cached to reduce server overload.
*
* This method clears the cached records.
*/
public static void clearCache() {
recordsCache.clear();
}
/**
* Fetches the status of one or more pdbIDs from the server.
*
* <p>Returns the results as a list of Attributes.
* Each attribute should contain "structureId" and "status" attributes, and
* possibly more.
*
* <p>Example:</br>
* <tt>http://www.rcsb.org/pdb/rest/idStatus?structureID=1HHB,4HHB</tt></br>
*<pre><idStatus>
* <record structureId="1HHB" status="OBSOLETE" replacedBy="4HHB"/>
* <record structureId="4HHB" status="CURRENT" replaces="1HHB"/>
*</idStatus>
* </pre>
*
* <p>Results are not guaranteed to be returned in the same order as pdbIDs.
* Refer to the structureId property to match them.
*
* @param pdbIDs
* @return A map between attributes and values
*/
private static List<Map<String, String>> getStatusIdRecords(String[] pdbIDs) {
List<Map<String,String>> result = new ArrayList<Map<String,String>>(pdbIDs.length);
String serverName = System.getProperty(PDB_SERVER_PROPERTY);
if ( serverName == null)
serverName = DEFAULT_PDB_SERVER;
else
logger.info(String.format("Got System property %s=%s",PDB_SERVER_PROPERTY,serverName));
// Build REST query URL
if(pdbIDs.length < 1) {
throw new IllegalArgumentException("No pdbIDs specified");
}
String urlStr = String.format("http://%s/pdb/rest/idStatus?structureId=",serverName);
for(String pdbId : pdbIDs) {
pdbId = pdbId.toUpperCase();
//check the cache
if (recordsCache.containsKey(pdbId)) {
//logger.debug("Fetching "+pdbId+" from Cache");
result.add( recordsCache.get(pdbId) );
} else {
urlStr += pdbId + ",";
}
}
// check if any ids still need fetching
if(urlStr.charAt(urlStr.length()-1) == '=') {
return result;
}
try {
logger.info("Fetching {}", urlStr);
URL url = new URL(urlStr);
InputStream uStream = url.openStream();
InputSource source = new InputSource(uStream);
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
XMLReader reader = parser.getXMLReader();
PDBStatusXMLHandler handler = new PDBStatusXMLHandler();
reader.setContentHandler(handler);
reader.parse(source);
// Fetch results of SAX parsing
List<Map<String,String>> records = handler.getRecords();
//add to cache
for(Map<String,String> record : records) {
String pdbId = record.get("structureId").toUpperCase();
if(pdbId != null) {
recordsCache.put(pdbId, record);
}
}
// return results
result.addAll(handler.getRecords());
// TODO should throw these forward and let the caller log
} catch (IOException e){
logger.error("Problem getting status for {} from PDB server. Error: {}", Arrays.toString(pdbIDs), e.getMessage());
return null;
} catch (SAXException e) {
logger.error("Problem getting status for {} from PDB server. Error: {}", Arrays.toString(pdbIDs), e.getMessage());
return null;
} catch (ParserConfigurationException e) {
logger.error("Problem getting status for {} from PDB server. Error: {}", Arrays.toString(pdbIDs), e.getMessage());
return null;
}
return result;
}
/**
* Handles idStatus xml by storing attributes for all record elements.
*
* @author Spencer Bliven <sbliven@ucsd.edu>
*
*/
private static class PDBStatusXMLHandler extends DefaultHandler {
private List<Map<String,String>> records;
public PDBStatusXMLHandler() {
records = new ArrayList<Map<String,String>>();
}
/**
* @param uri
* @param localName
* @param qName
* @param attributes
* @throws SAXException
* @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//System.out.format("Starting element: uri='%s' localName='%s' qName='%s'\n", uri, localName, qName);
if(qName.equals("record")) {
//Convert attributes into a Map, as it should have been.
//Important since SAX reuses Attributes objects for different calls
Map<String,String> attrMap = new HashMap<String,String>(attributes.getLength()*2);
for(int i=0;i<attributes.getLength();i++) {
attrMap.put(attributes.getQName(i), attributes.getValue(i));
}
records.add(attrMap);
}
}
/**
* @param e
* @throws SAXException
* @see org.xml.sax.helpers.DefaultHandler#error(org.xml.sax.SAXParseException)
*/
@Override
public void error(SAXParseException e) throws SAXException {
logger.error(e.getMessage());
super.error(e);
}
public List<Map<String, String>> getRecords() {
return records;
}
}
/** Returns a list of current PDB IDs
*
* @return a list of PDB IDs, or null if a problem occurred
*/
public static SortedSet<String> getCurrentPDBIds() throws IOException {
SortedSet<String> allPDBs = new TreeSet<String>();
String serverName = System.getProperty(PDB_SERVER_PROPERTY);
if ( serverName == null)
serverName = DEFAULT_PDB_SERVER;
else
logger.info(String.format("Got System property %s=%s",PDB_SERVER_PROPERTY,serverName));
// Build REST query URL
String urlStr = String.format("http://%s/pdb/rest/getCurrent",serverName);
URL u = new URL(urlStr);
InputStream stream = URLConnectionTools.getInputStream(u, 60000);
if (stream != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
String line = null;
while ((line = reader.readLine()) != null) {
int index = line.lastIndexOf("structureId=");
if (index > 0) {
allPDBs.add(line.substring(index + 13, index + 17));
}
}
}
return allPDBs;
}
}