1 package jalview.gui.structurechooser;
3 import java.util.Locale;
5 import java.util.Collection;
7 import java.util.Objects;
8 import java.util.Vector;
10 import javax.swing.JTable;
11 import javax.swing.table.TableModel;
13 import jalview.datamodel.DBRefEntry;
14 import jalview.datamodel.PDBEntry;
15 import jalview.datamodel.SequenceI;
16 import jalview.fts.api.FTSData;
17 import jalview.fts.api.FTSDataColumnI;
18 import jalview.fts.api.FTSRestClientI;
19 import jalview.fts.core.FTSDataColumnPreferences;
20 import jalview.fts.core.FTSRestRequest;
21 import jalview.fts.core.FTSRestResponse;
22 import jalview.jbgui.FilterOption;
25 * logic for querying sources of structural data for structures of sequences
31 public abstract class StructureChooserQuerySource
34 protected FTSDataColumnPreferences docFieldPrefs;
37 * max length of a GET URL (probably :( )
39 protected static int MAX_QLENGTH = 7820;
41 public StructureChooserQuerySource()
45 public static StructureChooserQuerySource getPDBfts()
47 return new PDBStructureChooserQuerySource();
50 public static StructureChooserQuerySource getTDBfts()
52 return new ThreeDBStructureChooserQuerySource();
55 public FTSDataColumnPreferences getDocFieldPrefs()
60 public void setDocFieldPrefs(FTSDataColumnPreferences docFieldPrefs)
62 this.docFieldPrefs = docFieldPrefs;
65 public FTSDataColumnPreferences getInitialFieldPreferences()
71 * Builds a query string for a given sequences using its DBRef entries
74 * the sequences to build a query for
75 * @return the built query string
78 public abstract String buildQuery(SequenceI seq);
81 * Remove the following special characters from input string +, -, &, !, (, ),
82 * {, }, [, ], ^, ", ~, *, ?, :, \
87 public static String sanitizeSeqName(String seqName)
89 Objects.requireNonNull(seqName);
90 return seqName.replaceAll("\\[\\d*\\]", "")
91 .replaceAll("[^\\dA-Za-z|_]", "").replaceAll("\\s+", "+");
95 * Ensures sequence ref names are not less than 3 characters and does not
96 * contain a database name
101 static boolean isValidSeqName(String seqName)
103 // System.out.println("seqName : " + seqName);
104 String ignoreList = "pdb,uniprot,swiss-prot";
105 if (seqName.length() < 3)
109 if (seqName.contains(":"))
113 seqName = seqName.toLowerCase(Locale.ROOT);
114 for (String ignoredEntry : ignoreList.split(","))
116 if (seqName.contains(ignoredEntry))
124 static String getDBRefId(DBRefEntry dbRef)
126 String ref = dbRef.getAccessionId().replaceAll("GO:", "");
130 static PDBEntry getFindEntry(String id, Vector<PDBEntry> pdbEntries)
132 Objects.requireNonNull(id);
133 Objects.requireNonNull(pdbEntries);
134 PDBEntry foundEntry = null;
135 for (PDBEntry entry : pdbEntries)
137 if (entry.getId().equalsIgnoreCase(id))
146 * FTSRestClient specific query builder to recover associated structure data
147 * records for a sequence
150 * - seq to generate a query for
151 * @param wantedFields
152 * - fields to retrieve
153 * @param selectedFilterOpt
154 * - criterion for ranking results (e.g. resolution)
156 * - sort ascending or descending
160 public abstract FTSRestResponse fetchStructuresMetaData(SequenceI seq,
161 Collection<FTSDataColumnI> wantedFields,
162 FilterOption selectedFilterOpt, boolean b) throws Exception;
165 * FTSRestClient specific query builder to pick top ranked entry from a
166 * fetchStructuresMetaData query
169 * - seq to generate a query for
170 * @param discoveredStructuresSet
171 * - existing set of entries - allows client side selection
172 * @param wantedFields
173 * - fields to retrieve
174 * @param selectedFilterOpt
175 * - criterion for ranking results (e.g. resolution)
177 * - sort ascending or descending
181 public abstract FTSRestResponse selectFirstRankedQuery(SequenceI seq,
182 Collection<FTSData> discoveredStructuresSet,
183 Collection<FTSDataColumnI> wantedFields, String fieldToFilterBy,
184 boolean b) throws Exception;
188 * @param discoveredStructuresSet
189 * @return the table model for the given result set for this engine
191 public TableModel getTableModel(
192 Collection<FTSData> discoveredStructuresSet)
194 return FTSRestResponse.getTableModel(getLastFTSRequest(),
195 discoveredStructuresSet);
198 protected abstract FTSRestRequest getLastFTSRequest();
200 public abstract PDBEntry[] collectSelectedRows(JTable restable,
201 int[] selectedRows, List<SequenceI> selectedSeqsToView);
204 * @param VIEWS_FILTER
205 * - a String key that can be used by the caller to tag the returned
206 * filter options to distinguish them in a collection
207 * @return list of FilterOption - convention is that the last one in the list
208 * will be constructed with 'addSeparator==true'
210 public abstract List<FilterOption> getAvailableFilterOptions(
211 String VIEWS_FILTER);
214 * construct a structure chooser query source for the given set of sequences
216 * @param selectedSeqs
217 * @return PDBe or 3DB query source
219 public static StructureChooserQuerySource getQuerySourceFor(
220 SequenceI[] selectedSeqs)
222 ThreeDBStructureChooserQuerySource tdbSource = new ThreeDBStructureChooserQuerySource();
223 boolean hasUniprot = false, hasCanonical = false;
224 boolean hasNA = false, hasProtein = false;
225 int protWithoutUni = 0;
226 int protWithoutCanon = 0;
227 for (SequenceI seq : selectedSeqs)
229 hasNA |= !seq.isProtein();
230 hasProtein |= seq.isProtein();
233 int refsAvailable = ThreeDBStructureChooserQuerySource
234 .checkUniprotRefs(seq.getDBRefs());
235 if (refsAvailable > -2)
237 if (refsAvailable > -1)
251 // logic: all canonicals - no fetchdb
252 // some uniprot no canonicals: defer to PDB, user can optionally fetch
254 if (hasProtein && hasCanonical && !hasNA && protWithoutCanon == 0 && protWithoutUni == 0)
259 return new PDBStructureChooserQuerySource();
263 * some filter options may mean the original query needs to be executed again.
265 * @param selectedFilterOpt
266 * @return true if the fetchStructuresMetadata method needs to be called again
268 public abstract boolean needsRefetch(FilterOption selectedFilterOpt);
270 public void updateAvailableFilterOptions(String VIEWS_FILTER,
271 List<FilterOption> xtantOptions, Collection<FTSData> lastFTSData)
273 // TODO Auto-generated method stub