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.Locale;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.Vector;
30 import javax.swing.JTable;
31 import javax.swing.table.TableModel;
33 import jalview.datamodel.DBRefEntry;
34 import jalview.datamodel.PDBEntry;
35 import jalview.datamodel.SequenceI;
36 import jalview.fts.api.FTSData;
37 import jalview.fts.api.FTSDataColumnI;
38 import jalview.fts.api.FTSRestClientI;
39 import jalview.fts.core.FTSDataColumnPreferences;
40 import jalview.fts.core.FTSRestRequest;
41 import jalview.fts.core.FTSRestResponse;
42 import jalview.jbgui.FilterOption;
45 * logic for querying sources of structural data for structures of sequences
51 public abstract class StructureChooserQuerySource
54 protected FTSDataColumnPreferences docFieldPrefs;
57 * max length of a GET URL (probably :( )
59 protected static int MAX_QLENGTH = 7820;
61 public StructureChooserQuerySource()
65 public static StructureChooserQuerySource getPDBfts()
67 return new PDBStructureChooserQuerySource();
70 public static StructureChooserQuerySource getTDBfts()
72 return new ThreeDBStructureChooserQuerySource();
75 public FTSDataColumnPreferences getDocFieldPrefs()
80 public void setDocFieldPrefs(FTSDataColumnPreferences docFieldPrefs)
82 this.docFieldPrefs = docFieldPrefs;
85 public FTSDataColumnPreferences getInitialFieldPreferences()
91 * Builds a query string for a given sequences using its DBRef entries
94 * the sequences to build a query for
95 * @return the built query string
98 public abstract String buildQuery(SequenceI seq);
101 * Remove the following special characters from input string +, -, &, !, (, ),
102 * {, }, [, ], ^, ", ~, *, ?, :, \
107 public static String sanitizeSeqName(String seqName)
109 Objects.requireNonNull(seqName);
110 return seqName.replaceAll("\\[\\d*\\]", "")
111 .replaceAll("[^\\dA-Za-z|_]", "").replaceAll("\\s+", "+");
115 * Ensures sequence ref names are not less than 3 characters and does not
116 * contain a database name
121 static boolean isValidSeqName(String seqName)
123 // jalview.bin.Console.outPrintln("seqName : " + seqName);
124 String ignoreList = "pdb,uniprot,swiss-prot";
125 if (seqName.length() < 3)
129 if (seqName.contains(":"))
133 seqName = seqName.toLowerCase(Locale.ROOT);
134 for (String ignoredEntry : ignoreList.split(","))
136 if (seqName.contains(ignoredEntry))
144 static String getDBRefId(DBRefEntry dbRef)
146 String ref = dbRef.getAccessionId().replaceAll("GO:", "");
150 static PDBEntry getFindEntry(String id, Vector<PDBEntry> pdbEntries)
152 Objects.requireNonNull(id);
153 Objects.requireNonNull(pdbEntries);
154 PDBEntry foundEntry = null;
155 for (PDBEntry entry : pdbEntries)
157 if (entry.getId().equalsIgnoreCase(id))
166 * FTSRestClient specific query builder to recover associated structure data
167 * records for a sequence
170 * - seq to generate a query for
171 * @param wantedFields
172 * - fields to retrieve
173 * @param selectedFilterOpt
174 * - criterion for ranking results (e.g. resolution)
176 * - sort ascending or descending
180 public abstract FTSRestResponse fetchStructuresMetaData(SequenceI seq,
181 Collection<FTSDataColumnI> wantedFields,
182 FilterOption selectedFilterOpt, boolean b) throws Exception;
185 * FTSRestClient specific query builder to pick top ranked entry from a
186 * fetchStructuresMetaData query
189 * - seq to generate a query for
190 * @param discoveredStructuresSet
191 * - existing set of entries - allows client side selection
192 * @param wantedFields
193 * - fields to retrieve
194 * @param selectedFilterOpt
195 * - criterion for ranking results (e.g. resolution)
197 * - sort ascending or descending
201 public abstract FTSRestResponse selectFirstRankedQuery(SequenceI seq,
202 Collection<FTSData> discoveredStructuresSet,
203 Collection<FTSDataColumnI> wantedFields, String fieldToFilterBy,
204 boolean b) throws Exception;
208 * @param discoveredStructuresSet
209 * @return the table model for the given result set for this engine
211 public TableModel getTableModel(
212 Collection<FTSData> discoveredStructuresSet)
214 return FTSRestResponse.getTableModel(getLastFTSRequest(),
215 discoveredStructuresSet);
218 protected abstract FTSRestRequest getLastFTSRequest();
220 public abstract PDBEntry[] collectSelectedRows(JTable restable,
221 int[] selectedRows, List<SequenceI> selectedSeqsToView);
224 * @param VIEWS_FILTER
225 * - a String key that can be used by the caller to tag the returned
226 * filter options to distinguish them in a collection
227 * @return list of FilterOption - convention is that the last one in the list
228 * will be constructed with 'addSeparator==true'
230 public abstract List<FilterOption> getAvailableFilterOptions(
231 String VIEWS_FILTER);
234 * construct a structure chooser query source for the given set of sequences
236 * @param selectedSeqs
237 * @return PDBe or 3DB query source
239 public static StructureChooserQuerySource getQuerySourceFor(
240 SequenceI[] selectedSeqs)
242 ThreeDBStructureChooserQuerySource tdbSource = new ThreeDBStructureChooserQuerySource();
243 boolean hasUniprot = false, hasCanonical = false;
244 boolean hasNA = false, hasProtein = false;
245 int protWithoutUni = 0;
246 int protWithoutCanon = 0;
247 for (SequenceI seq : selectedSeqs)
249 hasNA |= !seq.isProtein();
250 hasProtein |= seq.isProtein();
253 int refsAvailable = ThreeDBStructureChooserQuerySource
254 .checkUniprotRefs(seq.getDBRefs());
255 if (refsAvailable > -2)
257 if (refsAvailable > -1)
275 // logic: all canonicals - no fetchdb
276 // some uniprot no canonicals: defer to PDB, user can optionally fetch
278 if (hasProtein && hasCanonical && !hasNA && protWithoutCanon == 0
279 && protWithoutUni == 0)
284 return new PDBStructureChooserQuerySource();
288 * some filter options may mean the original query needs to be executed again.
290 * @param selectedFilterOpt
291 * @return true if the fetchStructuresMetadata method needs to be called again
293 public abstract boolean needsRefetch(FilterOption selectedFilterOpt);
295 public void updateAvailableFilterOptions(String VIEWS_FILTER,
296 List<FilterOption> xtantOptions, Collection<FTSData> lastFTSData)
298 // TODO Auto-generated method stub