JAL-3855 check for existence of an alpha fold model when pinging the PDBe
authorJim Procter <j.procter@dundee.ac.uk>
Tue, 27 Jul 2021 12:34:48 +0000 (13:34 +0100)
committerJim Procter <j.procter@dundee.ac.uk>
Tue, 27 Jul 2021 12:44:01 +0000 (13:44 +0100)
src/jalview/fts/service/alphafold/AlphafoldRestClient.java [new file with mode: 0644]
src/jalview/fts/service/pdb/PDBFTSRestClient.java

diff --git a/src/jalview/fts/service/alphafold/AlphafoldRestClient.java b/src/jalview/fts/service/alphafold/AlphafoldRestClient.java
new file mode 100644 (file)
index 0000000..6b855fc
--- /dev/null
@@ -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<FTSData> getFTSData(// Map<String, Object> pdbJsonDoc,
+          FTSRestRequest request)
+  {
+    List<FTSData> records = new ArrayList<FTSData>();
+    String primaryKey = null;
+
+    Object[] summaryRowData;
+
+    SequenceI associatedSequence;
+
+    Collection<FTSDataColumnI> 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;
+  }
+}
index 22ed591..963778c 100644 (file)
@@ -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<Object> docs = (List<Object>) pdbResponse.get("docs");
+      // add in any alphafold bits at the top
+      result = AlphafoldRestClient.getFTSData(pdbRestRequest);
       if (numFound > 0)
       {
-        result = new ArrayList<>();
-        List<Object> docs = (List<Object>) pdbResponse.get("docs");
+
         for (Iterator<Object> docIter = docs.iterator(); docIter
                 .hasNext();)
         {
           Map<String, Object> doc = (Map<String, Object>) 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();