2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.gui.structurechooser;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.BitSet;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Locale;
32 import jalview.datamodel.SequenceI;
33 import jalview.fts.api.FTSData;
34 import jalview.fts.core.FTSRestRequest;
36 public class TDBResultAnalyser
40 * model categories - update as needed. warnings output if unknown types
43 * Order denotes 'trust'
45 private static List<String> EXP_CATEGORIES = Arrays
47 { "EXPERIMENTALLY DETERMINED", "DEEP-LEARNING", "AB-INITIO",
48 "TEMPLATE-BASED", "CONFORMATIONAL ENSEMBLE" });
50 private SequenceI seq;
52 private Collection<FTSData> collectedResults;
54 private FTSRestRequest lastTdbRequest;
62 private int idx_mqual;
64 private int idx_resol;
69 private String filter = null;
72 * limit to particular source
74 private String sourceFilter = null;
76 private int idx_mprov;
78 public TDBResultAnalyser(SequenceI seq,
79 Collection<FTSData> collectedResults,
80 FTSRestRequest lastTdbRequest, String fieldToFilterBy,
84 this.collectedResults = collectedResults;
85 this.lastTdbRequest = lastTdbRequest;
86 this.filter = fieldToFilterBy;
87 this.sourceFilter = string;
88 idx_ups = lastTdbRequest.getFieldIndex("Uniprot Start");
89 idx_upe = lastTdbRequest.getFieldIndex("Uniprot End");
90 idx_mcat = lastTdbRequest.getFieldIndex("Model Category");
91 idx_mprov = lastTdbRequest.getFieldIndex("Provider");
92 idx_mqual = lastTdbRequest.getFieldIndex("Confidence");
93 idx_resol = lastTdbRequest.getFieldIndex("Resolution");
97 * maintain and resolve categories to 'trust order' TODO: change the trust
98 * scheme to something comprehensible.
101 * @return 0 for null cat, less than zero for others
103 public final int scoreCategory(String cat)
109 String upper_cat = cat.toUpperCase(Locale.ROOT);
110 int idx = EXP_CATEGORIES.indexOf(upper_cat);
113 System.out.println("Unknown category: '" + cat + "'");
114 EXP_CATEGORIES.add(upper_cat);
115 idx = EXP_CATEGORIES.size() - 1;
117 return -EXP_CATEGORIES.size() - idx;
121 * sorts records discovered by 3D beacons and excludes any that don't
122 * intersect with the sequence's start/end rage
126 public List<FTSData> getFilteredResponse()
128 List<FTSData> filteredResponse = new ArrayList<FTSData>();
130 // ignore anything outside the sequence region
131 for (FTSData row : collectedResults)
133 if (row.getSummaryData() != null
134 && row.getSummaryData()[idx_ups] != null)
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))
144 if (seq == row.getSummaryData()[0] && up_e > seq.getStart()
145 && up_s < seq.getEnd())
147 filteredResponse.add(row);
152 // sort according to decreasing length,
154 Collections.sort(filteredResponse, new Comparator<FTSData>()
157 public int compare(FTSData o1, FTSData o2)
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);
172 if (o1_cat == o2_cat)
176 int o1_xtent = o1_e - o1_s;
177 int o2_xtent = o2_e - o2_s;
178 if (o1_xtent == o2_xtent)
180 // EXPERIMENTAL DATA ALWAYS TRUMPS MODELS
181 if (o1_cat == scoreCategory(EXP_CATEGORIES.get(0)))
183 if (o1_prov.equals(o2_prov))
185 if ("PDBE".equals(o1_prov))
187 if (eitherNull(idx_resol, o1data, o2data))
189 return nonNullFirst(idx_resol, o1data, o2data);
191 // experimental structures, so rank on quality
192 double o1_res = (Double) o1data[idx_resol];
193 double o2_res = (Double) o2data[idx_resol];
194 return (o2_res < o1_res) ? 1
195 : (o2_res == o1_res) ? 0 : -1;
199 return 0; // no change in order
204 // PDBe always ranked above all other experimentally
205 // determined categories
206 return "PDBE".equals(o1_prov) ? -1
207 : "PDBE".equals(o2_prov) ? 1 : 0;
212 if (eitherNull(idx_mqual, o1data, o2data))
214 return nonNullFirst(idx_mqual, o1data, o2data);
216 // models, so rank on qmean - b
217 double o1_mq = (Double) o1data[idx_mqual];
218 double o2_mq = (Double) o2data[idx_mqual];
219 return (o2_mq < o1_mq) ? 1 : (o2_mq == o1_mq) ? 0 : -1;
224 return o1_xtent - o2_xtent;
234 // if both are not experimental, then favour alphafold
235 if (o2_cat > 0 && o1_cat > 0)
237 return "ALPHAFOLD DB".equals(o1_prov) ? -1
238 : "ALPHAFOLD DB".equals(o2_prov) ? 1 : 0;
240 return o2_cat - o1_cat;
244 private int nonNullFirst(int idx_resol, Object[] o1data,
247 return o1data[idx_resol] == o2data[idx_resol] ? 0
248 : o1data[idx_resol] != null ? -1 : 1;
251 private boolean eitherNull(int idx_resol, Object[] o1data,
254 return (o1data[idx_resol] == null || o2data[idx_resol] == null);
258 public boolean equals(Object obj)
260 return super.equals(obj);
263 return filteredResponse;
267 * return list of structures to be marked as selected for this sequence
268 * according to given criteria
270 * @param filteredStructures
271 * - sorted, filtered structures from getFilteredResponse
274 public List<FTSData> selectStructures(List<FTSData> filteredStructures)
276 List<FTSData> selected = new ArrayList<FTSData>();
277 BitSet cover = new BitSet();
278 cover.set(seq.getStart(), seq.getEnd());
279 // walk down the list of structures, selecting some to add to selected
280 // TODO: could do simple DP - double loop to select largest number of
281 // structures covering largest number of sites
282 for (FTSData structure : filteredStructures)
284 Object[] odata = structure.getSummaryData();
285 int o1_s = (Integer) odata[idx_ups];
286 int o1_e = (Integer) odata[idx_upe];
287 int o1_cat = scoreCategory((String) odata[idx_mcat]);
288 BitSet scover = new BitSet();
289 // measure intersection
290 scover.set(o1_s, o1_e);
292 if (scover.cardinality() > 4)
294 selected.add(structure);
295 // clear the range covered by this structure
296 cover.andNot(scover);
299 if (selected.size() == 0)
303 // final step is to sort on length - this might help the superposition
305 Collections.sort(selected, new Comparator<FTSData>()
308 public int compare(FTSData o1, FTSData o2)
310 Object[] o1data = o1.getSummaryData();
311 Object[] o2data = o2.getSummaryData();
312 int o1_xt = ((Integer) o1data[idx_upe])
313 - ((Integer) o1data[idx_ups]);
314 int o1_cat = scoreCategory((String) o1data[idx_mcat]);
315 int o2_xt = ((Integer) o2data[idx_upe] - (Integer) o2data[idx_ups]);
316 int o2_cat = scoreCategory((String) o2data[idx_mcat]);
317 return o2_xt - o1_xt;
321 ThreeDBStructureChooserQuerySource.FILTER_FIRST_BEST_COVERAGE))
323 return selected.subList(0, 1);