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()
70 * Builds a query string for a given sequences using its DBRef entries
73 * the sequences to build a query for
74 * @return the built query string
77 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();
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 - existing set of entries - allows client side selection
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 selectFirstRankedQuery(SequenceI seq,
181 Collection<FTSData> discoveredStructuresSet, 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 filter
204 * 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(String VIEWS_FILTER);
211 * construct a structure chooser query source for the given set of sequences
212 * @param selectedSeqs
213 * @return PDBe or 3DB query source
215 public static StructureChooserQuerySource getQuerySourceFor(
216 SequenceI[] selectedSeqs)
218 ThreeDBStructureChooserQuerySource tdbSource = new ThreeDBStructureChooserQuerySource();
219 boolean hasUniprot=false;
220 boolean hasNA=false,hasProtein=false;
221 for (SequenceI seq:selectedSeqs)
223 hasNA|=!seq.isProtein();
224 hasProtein |= seq.isProtein();
227 String query = tdbSource.buildQuery(seq);
228 if (query!=null && query.length()>0)
234 if (hasProtein && hasUniprot && !hasNA)
238 return new PDBStructureChooserQuerySource();
242 * some filter options may mean the original query needs to be executed again.
243 * @param selectedFilterOpt
244 * @return true if the fetchStructuresMetadata method needs to be called again
246 public abstract boolean needsRefetch(FilterOption selectedFilterOpt);
250 public void updateAvailableFilterOptions(String VIEWS_FILTER,
251 List<FilterOption> xtantOptions, Collection<FTSData> lastFTSData)
253 // TODO Auto-generated method stub