From 6d03a7bc7ca503f0e845dcbc568e44a39c842612 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Tue, 27 Jul 2021 13:34:48 +0100 Subject: [PATCH] JAL-3855 check for existence of an alpha fold model when pinging the PDBe --- .../fts/service/alphafold/AlphafoldRestClient.java | 157 ++++++++++++++++++++ src/jalview/fts/service/pdb/PDBFTSRestClient.java | 14 +- 2 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 src/jalview/fts/service/alphafold/AlphafoldRestClient.java diff --git a/src/jalview/fts/service/alphafold/AlphafoldRestClient.java b/src/jalview/fts/service/alphafold/AlphafoldRestClient.java new file mode 100644 index 0000000..6b855fc --- /dev/null +++ b/src/jalview/fts/service/alphafold/AlphafoldRestClient.java @@ -0,0 +1,157 @@ +package jalview.fts.service.alphafold; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import jalview.datamodel.DBRefEntry; +import jalview.datamodel.DBRefSource; +import jalview.datamodel.SequenceI; +import jalview.fts.api.FTSData; +import jalview.fts.api.FTSDataColumnI; +import jalview.fts.core.FTSRestRequest; +import jalview.util.DBRefUtils; +import jalview.util.HttpUtils; +import jalview.ws.dbsources.EBIAlfaFold; + +public class AlphafoldRestClient +{ + + /** + * turns a uniprot ID into a fake alphafold entry for the structure chooser - + * fakes PDB fields in response + * + * @param UniprotID + * @return null or an FTS Record (if alphafold thinks it has a structure) + */ + public static List getFTSData(// Map pdbJsonDoc, + FTSRestRequest request) + { + List records = new ArrayList(); + String primaryKey = null; + + Object[] summaryRowData; + + SequenceI associatedSequence; + + Collection diplayFields = request.getWantedFields(); + SequenceI associatedSeq = request.getAssociatedSequence(); + + for (DBRefEntry upref : DBRefUtils + .selectRefs(associatedSeq.getPrimaryDBRefs(), new String[] + { DBRefSource.UNIPROT })) + { + String alphaFoldId = "AF-" + upref.getAccessionId() + "-F1"; + try + { + String urls = EBIAlfaFold.getAlphaFoldCifDownloadUrl(alphaFoldId); + URL url = new URL(urls); + if (!HttpUtils.checkUrlAvailable(url, 50)) + { + continue; + } + } catch (Exception mfe) + { + jalview.bin.Cache.log.debug("Exception accessing urls", mfe); + continue; + } + int colCounter = 0; + summaryRowData = new Object[(associatedSeq != null) + ? diplayFields.size() + 1 + : diplayFields.size()]; + if (associatedSeq != null) + { + associatedSequence = associatedSeq; + summaryRowData[0] = associatedSequence; + colCounter = 1; + } + + for (FTSDataColumnI field : diplayFields) + { + String fieldData = "alphafold";// (pdbJsonDoc.get(field.getCode()) == + // null) ? "" + // : pdbJsonDoc.get(field.getCode()).toString(); + if (field.isPrimaryKeyColumn()) + { + primaryKey = alphaFoldId; + summaryRowData[colCounter++] = alphaFoldId; + } + else if (fieldData == null || fieldData.isEmpty()) + { + summaryRowData[colCounter++] = null; + } + else + { + try + { + summaryRowData[colCounter++] = (field.getDataType() + .getDataTypeClass() == Integer.class) + ? 1 + : (field.getDataType() + .getDataTypeClass() == Double.class) + ? 1.3131313 + : "AlphaFold clarity"; + } catch (Exception e) + { + e.printStackTrace(); + System.out.println("offending value:" + fieldData); + } + } + } + + final String primaryKey1 = primaryKey; + + final Object[] summaryRowData1 = summaryRowData; + records.add(new FTSData() + { + @Override + public Object[] getSummaryData() + { + return summaryRowData1; + } + + @Override + public Object getPrimaryKey() + { + return primaryKey1; + } + + /** + * Returns a string representation of this object; + */ + @Override + public String toString() + { + StringBuilder summaryFieldValues = new StringBuilder(); + for (Object summaryField : summaryRowData1) + { + summaryFieldValues.append( + summaryField == null ? " " : summaryField.toString()) + .append("\t"); + } + return summaryFieldValues.toString(); + } + + /** + * Returns hash code value for this object + */ + @Override + public int hashCode() + { + return Objects.hash(primaryKey1, this.toString()); + } + + @Override + public boolean equals(Object that) + { + return this.toString().equals(that.toString()); + } + }); + } + return records; + } +} diff --git a/src/jalview/fts/service/pdb/PDBFTSRestClient.java b/src/jalview/fts/service/pdb/PDBFTSRestClient.java index 22ed591..963778c 100644 --- a/src/jalview/fts/service/pdb/PDBFTSRestClient.java +++ b/src/jalview/fts/service/pdb/PDBFTSRestClient.java @@ -44,6 +44,7 @@ import jalview.fts.api.FTSRestClientI; import jalview.fts.core.FTSRestClient; import jalview.fts.core.FTSRestRequest; import jalview.fts.core.FTSRestResponse; +import jalview.fts.service.alphafold.AlphafoldRestClient; import jalview.util.JSONUtils; import jalview.util.MessageManager; import jalview.util.Platform; @@ -318,20 +319,23 @@ public static String parseJsonExceptionString(String jsonErrorResponse) .get("QTime").toString(); int numFound = Integer .valueOf(pdbResponse.get("numFound").toString()); + List docs = (List) pdbResponse.get("docs"); + // add in any alphafold bits at the top + result = AlphafoldRestClient.getFTSData(pdbRestRequest); if (numFound > 0) { - result = new ArrayList<>(); - List docs = (List) pdbResponse.get("docs"); + for (Iterator docIter = docs.iterator(); docIter .hasNext();) { Map doc = (Map) docIter.next(); result.add(getFTSData(doc, pdbRestRequest)); } - searchResult.setNumberOfItemsFound(numFound); - searchResult.setResponseTime(queryTime); - searchResult.setSearchSummary(result); } + searchResult.setNumberOfItemsFound(result.size()); + searchResult.setResponseTime(queryTime); + searchResult.setSearchSummary(result); + } catch (ParseException e) { e.printStackTrace(); -- 1.7.10.2