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