JAL-3746 apply copyright to source
[jalview.git] / src / jalview / fts / service / alphafold / AlphafoldRestClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.fts.service.alphafold;
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Objects;
30
31 import jalview.bin.Console;
32 import jalview.datamodel.DBRefEntry;
33 import jalview.datamodel.DBRefSource;
34 import jalview.datamodel.SequenceI;
35 import jalview.fts.api.FTSData;
36 import jalview.fts.api.FTSDataColumnI;
37 import jalview.fts.core.FTSRestRequest;
38 import jalview.util.DBRefUtils;
39 import jalview.util.HttpUtils;
40 import jalview.ws.dbsources.EBIAlfaFold;
41
42 public class AlphafoldRestClient
43 {
44
45   /**
46    * turns a uniprot ID into a fake alphafold entry for the structure chooser -
47    * fakes PDB fields in response
48    * 
49    * @param UniprotID
50    * @return null or an FTS Record (if alphafold thinks it has a structure)
51    */
52   public static List<FTSData> getFTSData(// Map<String, Object> pdbJsonDoc,
53           FTSRestRequest request)
54   {
55     List<FTSData> records = new ArrayList<FTSData>();
56     String primaryKey = null;
57
58     Object[] summaryRowData;
59
60     SequenceI associatedSequence;
61
62     Collection<FTSDataColumnI> diplayFields = request.getWantedFields();
63     SequenceI associatedSeq = request.getAssociatedSequence();
64
65     for (DBRefEntry upref : DBRefUtils
66             .selectRefs(associatedSeq.getPrimaryDBRefs(), new String[]
67             { DBRefSource.UNIPROT }))
68     {
69       String alphaFoldId = "AF-" + upref.getAccessionId() + "-F1";
70       try
71       {
72         String urls = EBIAlfaFold.getAlphaFoldCifDownloadUrl(alphaFoldId);
73         URL url = new URL(urls);
74         if (!HttpUtils.checkUrlAvailable(url, 50))
75         {
76           continue;
77         }
78       } catch (Exception mfe)
79       {
80         Console.debug("Exception accessing urls", mfe);
81         continue;
82       }
83       int colCounter = 0;
84       summaryRowData = new Object[(associatedSeq != null)
85               ? diplayFields.size() + 1
86               : diplayFields.size()];
87       if (associatedSeq != null)
88       {
89         associatedSequence = associatedSeq;
90         summaryRowData[0] = associatedSequence;
91         colCounter = 1;
92       }
93
94       for (FTSDataColumnI field : diplayFields)
95       {
96         String fieldData = "alphafold";// (pdbJsonDoc.get(field.getCode()) ==
97                                        // null) ? ""
98         // : pdbJsonDoc.get(field.getCode()).toString();
99         if (field.isPrimaryKeyColumn())
100         {
101           primaryKey = alphaFoldId;
102           summaryRowData[colCounter++] = alphaFoldId;
103         }
104         else if (fieldData == null || fieldData.isEmpty())
105         {
106           summaryRowData[colCounter++] = null;
107         }
108         else
109         {
110           try
111           {
112             summaryRowData[colCounter++] = (field.getDataType()
113                     .getDataTypeClass() == Integer.class)
114                             ? 1
115                             : (field.getDataType()
116                                     .getDataTypeClass() == Double.class)
117                                             ? 1.3131313
118                                             : "AlphaFold clarity";
119           } catch (Exception e)
120           {
121             e.printStackTrace();
122             System.out.println("offending value:" + fieldData);
123           }
124         }
125       }
126
127       final String primaryKey1 = primaryKey;
128
129       final Object[] summaryRowData1 = summaryRowData;
130       records.add(new FTSData()
131       {
132         @Override
133         public Object[] getSummaryData()
134         {
135           return summaryRowData1;
136         }
137
138         @Override
139         public Object getPrimaryKey()
140         {
141           return primaryKey1;
142         }
143
144         /**
145          * Returns a string representation of this object;
146          */
147         @Override
148         public String toString()
149         {
150           StringBuilder summaryFieldValues = new StringBuilder();
151           for (Object summaryField : summaryRowData1)
152           {
153             summaryFieldValues.append(
154                     summaryField == null ? " " : summaryField.toString())
155                     .append("\t");
156           }
157           return summaryFieldValues.toString();
158         }
159
160         /**
161          * Returns hash code value for this object
162          */
163         @Override
164         public int hashCode()
165         {
166           return Objects.hash(primaryKey1, this.toString());
167         }
168
169         @Override
170         public boolean equals(Object that)
171         {
172           return this.toString().equals(that.toString());
173         }
174       });
175     }
176     return records;
177   }
178 }