1 package jalview.gui.structurechooser;
3 import java.util.Collection;
5 import java.util.Objects;
6 import java.util.Vector;
8 import javax.swing.JTable;
9 import javax.swing.table.TableModel;
11 import jalview.datamodel.DBRefEntry;
12 import jalview.datamodel.PDBEntry;
13 import jalview.datamodel.SequenceI;
14 import jalview.fts.api.FTSData;
15 import jalview.fts.api.FTSDataColumnI;
16 import jalview.fts.api.FTSRestClientI;
17 import jalview.fts.core.FTSDataColumnPreferences;
18 import jalview.fts.core.FTSRestRequest;
19 import jalview.fts.core.FTSRestResponse;
20 import jalview.jbgui.FilterOption;
23 * logic for querying sources of structural data for structures of sequences
29 public abstract class StructureChooserQuerySource
32 protected FTSDataColumnPreferences docFieldPrefs;
35 * max length of a GET URL (probably :( )
37 protected static int MAX_QLENGTH = 7820;
39 public StructureChooserQuerySource()
43 public static StructureChooserQuerySource getPDBfts()
45 return new PDBStructureChooserQuerySource();
48 public static StructureChooserQuerySource getTDBfts()
50 return new ThreeDBStructureChooserQuerySource();
53 public FTSDataColumnPreferences getDocFieldPrefs()
58 public void setDocFieldPrefs(FTSDataColumnPreferences docFieldPrefs)
60 this.docFieldPrefs = docFieldPrefs;
63 public FTSDataColumnPreferences getInitialFieldPreferences()
69 * Builds a query string for a given sequences using its DBRef entries
72 * the sequences to build a query for
73 * @return the built query string
76 public abstract String buildQuery(SequenceI seq);
79 * Remove the following special characters from input string +, -, &, !, (, ),
80 * {, }, [, ], ^, ", ~, *, ?, :, \
85 public static String sanitizeSeqName(String seqName)
87 Objects.requireNonNull(seqName);
88 return seqName.replaceAll("\\[\\d*\\]", "")
89 .replaceAll("[^\\dA-Za-z|_]", "").replaceAll("\\s+", "+");
93 * Ensures sequence ref names are not less than 3 characters and does not
94 * contain a database name
99 static boolean isValidSeqName(String seqName)
101 // System.out.println("seqName : " + seqName);
102 String ignoreList = "pdb,uniprot,swiss-prot";
103 if (seqName.length() < 3)
107 if (seqName.contains(":"))
111 seqName = seqName.toLowerCase();
112 for (String ignoredEntry : ignoreList.split(","))
114 if (seqName.contains(ignoredEntry))
122 static String getDBRefId(DBRefEntry dbRef)
124 String ref = dbRef.getAccessionId().replaceAll("GO:", "");
128 static PDBEntry getFindEntry(String id, Vector<PDBEntry> pdbEntries)
130 Objects.requireNonNull(id);
131 Objects.requireNonNull(pdbEntries);
132 PDBEntry foundEntry = null;
133 for (PDBEntry entry : pdbEntries)
135 if (entry.getId().equalsIgnoreCase(id))
144 * FTSRestClient specific query builder to recover associated structure data
145 * records for a sequence
148 * - seq to generate a query for
149 * @param wantedFields
150 * - fields to retrieve
151 * @param selectedFilterOpt
152 * - criterion for ranking results (e.g. resolution)
154 * - sort ascending or descending
158 public abstract FTSRestResponse fetchStructuresMetaData(SequenceI seq,
159 Collection<FTSDataColumnI> wantedFields,
160 FilterOption selectedFilterOpt, boolean b) throws Exception;
163 * FTSRestClient specific query builder to pick top ranked entry from a
164 * fetchStructuresMetaData query
167 * - seq to generate a query for
168 * @param discoveredStructuresSet
169 * - existing set of entries - allows client side selection
170 * @param wantedFields
171 * - fields to retrieve
172 * @param selectedFilterOpt
173 * - criterion for ranking results (e.g. resolution)
175 * - sort ascending or descending
179 public abstract FTSRestResponse selectFirstRankedQuery(SequenceI seq,
180 Collection<FTSData> discoveredStructuresSet,
181 Collection<FTSDataColumnI> wantedFields, String fieldToFilterBy,
182 boolean b) throws Exception;
186 * @param discoveredStructuresSet
187 * @return the table model for the given result set for this engine
189 public TableModel getTableModel(
190 Collection<FTSData> discoveredStructuresSet)
192 return FTSRestResponse.getTableModel(getLastFTSRequest(),
193 discoveredStructuresSet);
196 protected abstract FTSRestRequest getLastFTSRequest();
198 public abstract PDBEntry[] collectSelectedRows(JTable restable,
199 int[] selectedRows, List<SequenceI> selectedSeqsToView);
202 * @param VIEWS_FILTER
203 * - a String key that can be used by the caller to tag the returned
204 * filter options to distinguish them in a collection
205 * @return list of FilterOption - convention is that the last one in the list
206 * will be constructed with 'addSeparator==true'
208 public abstract List<FilterOption> getAvailableFilterOptions(
209 String VIEWS_FILTER);
212 * construct a structure chooser query source for the given set of sequences
214 * @param selectedSeqs
215 * @return PDBe or 3DB query source
217 public static StructureChooserQuerySource getQuerySourceFor(
218 SequenceI[] selectedSeqs)
220 ThreeDBStructureChooserQuerySource tdbSource = new ThreeDBStructureChooserQuerySource();
221 boolean hasUniprot = false, hasCanonical = false;
222 boolean hasNA = false, hasProtein = false;
223 int protWithoutUni = 0;
224 int protWithoutCanon = 0;
225 for (SequenceI seq : selectedSeqs)
227 hasNA |= !seq.isProtein();
228 hasProtein |= seq.isProtein();
231 int refsAvailable = ThreeDBStructureChooserQuerySource
232 .checkUniprotRefs(seq.getDBRefs());
233 if (refsAvailable > -2)
235 if (refsAvailable > -1)
249 // logic: all canonicals - no fetchdb
250 // some uniprot no canonicals: defer to PDB, user can optionally fetch
252 if (hasProtein && hasCanonical && !hasNA && protWithoutCanon == 0 && protWithoutUni == 0)
257 return new PDBStructureChooserQuerySource();
261 * some filter options may mean the original query needs to be executed again.
263 * @param selectedFilterOpt
264 * @return true if the fetchStructuresMetadata method needs to be called again
266 public abstract boolean needsRefetch(FilterOption selectedFilterOpt);
268 public void updateAvailableFilterOptions(String VIEWS_FILTER,
269 List<FilterOption> xtantOptions, Collection<FTSData> lastFTSData)
271 // TODO Auto-generated method stub