JAL-3976 skip entries with null values when analyzing
[jalview.git] / src / jalview / gui / structurechooser / TDBResultAnalyser.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.gui.structurechooser;
22
23 import java.util.Locale;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.BitSet;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32
33 import jalview.datamodel.SequenceI;
34 import jalview.fts.api.FTSData;
35 import jalview.fts.core.FTSRestRequest;
36
37 public class TDBResultAnalyser
38 {
39
40   /**
41    * model categories - update as needed. warnings output if unknown types
42    * encountered.
43    * 
44    * Order denotes 'trust'
45    */
46   private static List<String> EXP_CATEGORIES = Arrays
47           .asList(new String[]
48           { "EXPERIMENTALLY DETERMINED", "DEEP-LEARNING", "TEMPLATE-BASED",
49               "AB-INITIO", "CONFORMATIONAL ENSEMBLE" });
50
51   private SequenceI seq;
52
53   private Collection<FTSData> collectedResults;
54
55   private FTSRestRequest lastTdbRequest;
56
57   private int idx_ups;
58
59   private int idx_upe;
60
61   private int idx_mcat;
62
63   private int idx_mqual;
64
65   private int idx_resol;
66
67   /**
68    * selection model
69    */
70   private String filter = null;
71
72   /**
73    * limit to particular source
74    */
75   private String sourceFilter = null;
76
77   private int idx_mprov;
78
79   public TDBResultAnalyser(SequenceI seq,
80           Collection<FTSData> collectedResults,
81           FTSRestRequest lastTdbRequest, String fieldToFilterBy,
82           String string)
83   {
84     this.seq = seq;
85     this.collectedResults = collectedResults;
86     this.lastTdbRequest = lastTdbRequest;
87     this.filter = fieldToFilterBy;
88     this.sourceFilter = string;
89     idx_ups = lastTdbRequest.getFieldIndex("Uniprot Start");
90     idx_upe = lastTdbRequest.getFieldIndex("Uniprot End");
91     idx_mcat = lastTdbRequest.getFieldIndex("Model Category");
92     idx_mprov = lastTdbRequest.getFieldIndex("Provider");
93     idx_mqual = lastTdbRequest.getFieldIndex("Confidence");
94     idx_resol = lastTdbRequest.getFieldIndex("Resolution");
95   }
96
97   /**
98    * maintain and resolve categories to 'trust order' TODO: change the trust
99    * scheme to something comprehensible.
100    * 
101    * @param cat
102    * @return 0 for null cat, less than zero for others
103    */
104   public final int scoreCategory(String cat)
105   {
106     if (cat == null)
107     {
108       return 0;
109     }
110     String upper_cat = cat.toUpperCase(Locale.ROOT);
111     int idx = EXP_CATEGORIES.indexOf(upper_cat);
112     if (idx == -1)
113     {
114       System.out.println("Unknown category: '" + cat + "'");
115       EXP_CATEGORIES.add(upper_cat);
116       idx = EXP_CATEGORIES.size() - 1;
117     }
118     return -EXP_CATEGORIES.size() - idx;
119   }
120
121   /**
122    * sorts records discovered by 3D beacons and excludes any that don't
123    * intersect with the sequence's start/end rage
124    * 
125    * @return
126    */
127   public List<FTSData> getFilteredResponse()
128   {
129     List<FTSData> filteredResponse = new ArrayList<FTSData>();
130
131     // ignore anything outside the sequence region
132     for (FTSData row : collectedResults)
133     {
134       if (row.getSummaryData() != null && row.getSummaryData()[idx_ups]!=null)
135       {
136         int up_s = (Integer) row.getSummaryData()[idx_ups];
137         int up_e = (Integer) row.getSummaryData()[idx_upe];
138         String provider = (String) row.getSummaryData()[idx_mprov];
139         String mcat = (String) row.getSummaryData()[idx_mcat];
140         // this makes sure all new categories are in the score array.
141         int scorecat = scoreCategory(mcat);
142         if (sourceFilter == null || sourceFilter.equals(provider))
143         {
144           if (seq == row.getSummaryData()[0] && up_e > seq.getStart()
145                   && up_s < seq.getEnd())
146           {
147             filteredResponse.add(row);
148           }
149         }
150       }
151     }
152     // sort according to decreasing length,
153     // increasing start
154     Collections.sort(filteredResponse, new Comparator<FTSData>()
155     {
156       @Override
157       public int compare(FTSData o1, FTSData o2)
158       {
159         Object[] o1data = o1.getSummaryData();
160         Object[] o2data = o2.getSummaryData();
161         int o1_s = (Integer) o1data[idx_ups];
162         int o1_e = (Integer) o1data[idx_upe];
163         int o1_cat = scoreCategory((String) o1data[idx_mcat]);
164         String o1_prov = ((String) o1data[idx_mprov])
165                 .toUpperCase(Locale.ROOT);
166         int o2_s = (Integer) o2data[idx_ups];
167         int o2_e = (Integer) o2data[idx_upe];
168         int o2_cat = scoreCategory((String) o2data[idx_mcat]);
169         String o2_prov = ((String) o2data[idx_mprov])
170                 .toUpperCase(Locale.ROOT);
171
172         if (o1_cat == o2_cat)
173         {
174           if (o1_s == o2_s)
175           {
176             int o1_xtent = o1_e - o1_s;
177             int o2_xtent = o2_e - o2_s;
178             if (o1_xtent == o2_xtent)
179             {
180               if (o1_cat == scoreCategory(EXP_CATEGORIES.get(0)))
181               {
182                 if (o1_prov.equals(o2_prov))
183                 {
184                   if ("PDBE".equals(o1_prov))
185                   {
186                     if (eitherNull(idx_resol, o1data, o2data))
187                     {
188                       return nonNullFirst(idx_resol, o1data, o2data);
189                     }
190                     // experimental structures, so rank on quality
191                     double o1_res = (Double) o1data[idx_resol];
192                     double o2_res = (Double) o2data[idx_resol];
193                     return (o2_res < o1_res) ? 1
194                             : (o2_res == o1_res) ? 0 : -1;
195                   }
196                   else
197                   {
198                     return 0; // no change in order
199                   }
200                 }
201                 else
202                 {
203                   // PDBe always ranked above all other experimentally
204                   // determined categories
205                   return "PDBE".equals(o1_prov) ? -1
206                           : "PDBE".equals(o2_prov) ? 1 : 0;
207                 }
208               }
209               else
210               {
211                 if (eitherNull(idx_mqual, o1data, o2data))
212                 {
213                   return nonNullFirst(idx_mqual, o1data, o2data);
214                 }
215                 // models, so rank on qmean - b
216                 double o1_mq = (Double) o1data[idx_mqual];
217                 double o2_mq = (Double) o2data[idx_mqual];
218                 return (o2_mq < o1_mq) ? 1 : (o2_mq == o1_mq) ? 0 : -1;
219               }
220             }
221             else
222             {
223               return o1_xtent - o2_xtent;
224             }
225           }
226           else
227           {
228             return o1_s - o2_s;
229           }
230         }
231         else
232         {
233           return o2_cat - o1_cat;
234         }
235       }
236
237       private int nonNullFirst(int idx_resol, Object[] o1data,
238               Object[] o2data)
239       {
240         return o1data[idx_resol] == o2data[idx_resol] ? 0
241                 : o1data[idx_resol] != null ? -1 : 1;
242       }
243
244       private boolean eitherNull(int idx_resol, Object[] o1data,
245               Object[] o2data)
246       {
247         return (o1data[idx_resol] == null || o2data[idx_resol] == null);
248       }
249
250       @Override
251       public boolean equals(Object obj)
252       {
253         return super.equals(obj);
254       }
255     });
256     return filteredResponse;
257   }
258
259   /**
260    * return list of structures to be marked as selected for this sequence
261    * according to given criteria
262    * 
263    * @param filteredStructures
264    *          - sorted, filtered structures from getFilteredResponse
265    * 
266    */
267   public List<FTSData> selectStructures(List<FTSData> filteredStructures)
268   {
269     List<FTSData> selected = new ArrayList<FTSData>();
270     BitSet cover = new BitSet();
271     cover.set(seq.getStart(), seq.getEnd());
272     // walk down the list of structures, selecting some to add to selected
273     // TODO: could do simple DP - double loop to select largest number of
274     // structures covering largest number of sites
275     for (FTSData structure : filteredStructures)
276     {
277       Object[] odata = structure.getSummaryData();
278       int o1_s = (Integer) odata[idx_ups];
279       int o1_e = (Integer) odata[idx_upe];
280       int o1_cat = scoreCategory((String) odata[idx_mcat]);
281       BitSet scover = new BitSet();
282       // measure intersection
283       scover.set(o1_s, o1_e);
284       scover.and(cover);
285       if (scover.cardinality() > 4)
286       {
287         selected.add(structure);
288         // clear the range covered by this structure
289         cover.andNot(scover);
290       }
291     }
292     if (selected.size() == 0)
293     {
294       return selected;
295     }
296     // final step is to sort on length - this might help the superposition
297     // process
298     Collections.sort(selected, new Comparator<FTSData>()
299     {
300       @Override
301       public int compare(FTSData o1, FTSData o2)
302       {
303         Object[] o1data = o1.getSummaryData();
304         Object[] o2data = o2.getSummaryData();
305         int o1_xt = ((Integer) o1data[idx_upe])
306                 - ((Integer) o1data[idx_ups]);
307         int o1_cat = scoreCategory((String) o1data[idx_mcat]);
308         int o2_xt = ((Integer) o2data[idx_upe] - (Integer) o2data[idx_ups]);
309         int o2_cat = scoreCategory((String) o2data[idx_mcat]);
310         return o2_xt - o1_xt;
311       }
312     });
313     if (filter.equals(
314             ThreeDBStructureChooserQuerySource.FILTER_FIRST_BEST_COVERAGE))
315     {
316       return selected.subList(0, 1);
317     }
318     return selected;
319   }
320
321 }