0778fc362cd69abc2cbf67dc91b14fa669e90025
[jalview.git] / src / jalview / fts / service / alphafold / AlphafoldRestClient.java
1 package jalview.fts.service.alphafold;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Objects;
10
11 import jalview.bin.Cache;
12 import jalview.datamodel.DBRefEntry;
13 import jalview.datamodel.DBRefSource;
14 import jalview.datamodel.SequenceI;
15 import jalview.fts.api.FTSData;
16 import jalview.fts.api.FTSDataColumnI;
17 import jalview.fts.core.FTSRestRequest;
18 import jalview.util.DBRefUtils;
19 import jalview.util.HttpUtils;
20 import jalview.ws.dbsources.EBIAlfaFold;
21
22 public class AlphafoldRestClient
23 {
24
25   /**
26    * turns a uniprot ID into a fake alphafold entry for the structure chooser -
27    * fakes PDB fields in response
28    * 
29    * @param UniprotID
30    * @return null or an FTS Record (if alphafold thinks it has a structure)
31    */
32   public static List<FTSData> getFTSData(// Map<String, Object> pdbJsonDoc,
33           FTSRestRequest request)
34   {
35     List<FTSData> records = new ArrayList<FTSData>();
36     String primaryKey = null;
37
38     Object[] summaryRowData;
39
40     SequenceI associatedSequence;
41
42     Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
43     SequenceI associatedSeq = request.getAssociatedSequence();
44
45     for (DBRefEntry upref : DBRefUtils
46             .selectRefs(associatedSeq.getPrimaryDBRefs(), new String[]
47             { DBRefSource.UNIPROT }))
48     {
49       String alphaFoldId = "AF-" + upref.getAccessionId() + "-F1";
50       try
51       {
52         String urls = EBIAlfaFold.getAlphaFoldCifDownloadUrl(alphaFoldId);
53         URL url = new URL(urls);
54         if (!HttpUtils.checkUrlAvailable(url, 50))
55         {
56           continue;
57         }
58       } catch (Exception mfe)
59       {
60         Cache.debug("Exception accessing urls", mfe);
61         continue;
62       }
63       int colCounter = 0;
64       summaryRowData = new Object[(associatedSeq != null)
65               ? diplayFields.size() + 1
66               : diplayFields.size()];
67       if (associatedSeq != null)
68       {
69         associatedSequence = associatedSeq;
70         summaryRowData[0] = associatedSequence;
71         colCounter = 1;
72       }
73
74       for (FTSDataColumnI field : diplayFields)
75       {
76         String fieldData = "alphafold";// (pdbJsonDoc.get(field.getCode()) ==
77                                        // null) ? ""
78         // : pdbJsonDoc.get(field.getCode()).toString();
79         if (field.isPrimaryKeyColumn())
80         {
81           primaryKey = alphaFoldId;
82           summaryRowData[colCounter++] = alphaFoldId;
83         }
84         else if (fieldData == null || fieldData.isEmpty())
85         {
86           summaryRowData[colCounter++] = null;
87         }
88         else
89         {
90           try
91           {
92             summaryRowData[colCounter++] = (field.getDataType()
93                     .getDataTypeClass() == Integer.class)
94                             ? 1
95                             : (field.getDataType()
96                                     .getDataTypeClass() == Double.class)
97                                             ? 1.3131313
98                                             : "AlphaFold clarity";
99           } catch (Exception e)
100           {
101             e.printStackTrace();
102             System.out.println("offending value:" + fieldData);
103           }
104         }
105       }
106
107       final String primaryKey1 = primaryKey;
108
109       final Object[] summaryRowData1 = summaryRowData;
110       records.add(new FTSData()
111       {
112         @Override
113         public Object[] getSummaryData()
114         {
115           return summaryRowData1;
116         }
117
118         @Override
119         public Object getPrimaryKey()
120         {
121           return primaryKey1;
122         }
123
124         /**
125          * Returns a string representation of this object;
126          */
127         @Override
128         public String toString()
129         {
130           StringBuilder summaryFieldValues = new StringBuilder();
131           for (Object summaryField : summaryRowData1)
132           {
133             summaryFieldValues.append(
134                     summaryField == null ? " " : summaryField.toString())
135                     .append("\t");
136           }
137           return summaryFieldValues.toString();
138         }
139
140         /**
141          * Returns hash code value for this object
142          */
143         @Override
144         public int hashCode()
145         {
146           return Objects.hash(primaryKey1, this.toString());
147         }
148
149         @Override
150         public boolean equals(Object that)
151         {
152           return this.toString().equals(that.toString());
153         }
154       });
155     }
156     return records;
157   }
158 }