3 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
4 * Copyright (C) 2014 The Jalview Authors
6 * This file is part of Jalview.
8 * Jalview is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation, either version 3
11 * of the License, or (at your option) any later version.
13 * Jalview is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 * The Jalview Authors are detailed in the 'AUTHORS' file.
25 import jalview.datamodel.DBRefEntry;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.jbgui.GStructureChooser;
29 import jalview.jbgui.PDBDocFieldPreferences;
30 import jalview.util.MessageManager;
31 import jalview.ws.dbsources.PDBRestClient;
32 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
33 import jalview.ws.uimodel.PDBRestRequest;
34 import jalview.ws.uimodel.PDBRestResponse;
35 import jalview.ws.uimodel.PDBRestResponse.PDBResponseSummary;
37 import java.awt.event.ItemEvent;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.HashSet;
41 import java.util.LinkedHashSet;
42 import java.util.List;
43 import java.util.Vector;
45 import javax.swing.JCheckBox;
46 import javax.swing.JComboBox;
47 import javax.swing.JLabel;
50 * Provides the behaviors for the Structure chooser Panel
55 @SuppressWarnings("serial")
56 public class StructureChooser extends GStructureChooser
58 private boolean structuresDiscovered = false;
60 private SequenceI selectedSequence;
62 private SequenceI[] selectedSequences;
64 private IProgressIndicator progressIndicator;
66 private Collection<PDBResponseSummary> discoveredStructuresSet;
68 private PDBRestRequest lastPdbRequest;
70 private PDBRestClient pdbRestCleint;
72 private String selectedPdbFileName;
74 private boolean isValidPBDEntry;
76 public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
80 this.selectedSequence = selectedSeq;
81 this.selectedSequences = selectedSeqs;
82 this.progressIndicator = (ap == null) ? null : ap.alignFrame;
87 * Initializes parameters used by the Structure Chooser Panel
91 Thread discoverPDBStructuresThread = new Thread(new Runnable()
96 long startTime = System.currentTimeMillis();
97 String msg = MessageManager.getString("status.fetching_db_refs");
98 updateProgressIndicator(msg, startTime);
99 fetchStructuresMetaData();
100 populateFilterComboBox();
101 updateProgressIndicator(null, startTime);
102 mainFrame.setVisible(true);
106 discoverPDBStructuresThread.start();
110 * Updates the progress indicator with the specified message
113 * displayed message for the operation
115 * unique handle for this indicator
117 public void updateProgressIndicator(String message, long id)
119 if (progressIndicator != null)
121 progressIndicator.setProgressBar(message, id);
126 * Retrieve meta-data for all the structure(s) for a given sequence(s) in a
129 public void fetchStructuresMetaData()
131 long startTime = System.currentTimeMillis();
132 Collection<PDBDocField> wantedFields = PDBDocFieldPreferences
133 .getStructureSummaryFields();
135 discoveredStructuresSet = new LinkedHashSet<PDBResponseSummary>();
136 for (SequenceI seq : selectedSequences)
138 PDBRestRequest pdbRequest = new PDBRestRequest();
139 pdbRequest.setAllowEmptySeq(false);
140 pdbRequest.setResponseSize(500);
141 pdbRequest.setFieldToSearchBy("(text:");
142 pdbRequest.setWantedFields(wantedFields);
143 pdbRequest.setSearchTerm(buildQuery(seq) + ")");
144 pdbRequest.setAssociatedSequence(seq.getName());
145 pdbRestCleint = new PDBRestClient();
146 PDBRestResponse resultList = pdbRestCleint.executeRequest(pdbRequest);
147 lastPdbRequest = pdbRequest;
148 if (resultList.getSearchSummary() != null
149 && !resultList.getSearchSummary().isEmpty())
151 discoveredStructuresSet.addAll(resultList.getSearchSummary());
152 updateSequenceDbRef(seq, resultList.getSearchSummary());
156 int noOfStructuresFound = 0;
157 if (discoveredStructuresSet != null
158 && !discoveredStructuresSet.isEmpty())
160 tbl_summary.setModel(PDBRestResponse.getTableModel(lastPdbRequest,
161 discoveredStructuresSet));
162 structuresDiscovered = true;
163 noOfStructuresFound = discoveredStructuresSet.size();
165 String totalTime = (System.currentTimeMillis() - startTime)
167 mainFrame.setTitle("Structure Chooser - " + noOfStructuresFound
168 + " Found (" + totalTime + ")");
172 * Update the DBRef entry for a given sequence with values retrieved from
176 * the Sequence to update its DBRef entry
177 * @param responseSummaries
178 * a collection of PDBResponseSummary
180 public void updateSequenceDbRef(SequenceI seq,
181 Collection<PDBResponseSummary> responseSummaries)
183 for (PDBResponseSummary response : responseSummaries)
185 PDBEntry newEntry = new PDBEntry();
186 newEntry.setId(response.getPdbId());
187 newEntry.setType("PDB");
188 seq.getDatasetSequence().addPDBId(newEntry);
193 * Builds a query string for a given sequences using its DBRef entries
196 * the sequences to build a query for
197 * @return the built query string
199 @SuppressWarnings("unchecked")
200 public static String buildQuery(SequenceI seq)
202 String query = seq.getName();
203 StringBuilder queryBuilder = new StringBuilder();
206 if (seq.getPDBId() != null)
208 for (PDBEntry entry : (Vector<PDBEntry>) seq.getPDBId())
210 queryBuilder.append("text:").append(entry.getId()).append(" OR ");
214 if (seq.getDBRef() != null && seq.getDBRef().length != 0)
216 for (DBRefEntry dbRef : seq.getDBRef())
218 queryBuilder.append("text:")
219 .append(dbRef.getAccessionId().replaceAll("GO:", ""))
227 int endIndex = queryBuilder.lastIndexOf(" OR ");
228 query = queryBuilder.toString().substring(5, endIndex);
234 * Filters a given list of discovered structures based on supplied argument
236 * @param fieldToFilterBy
237 * the field to filter by
239 public void filterResultSet(final String fieldToFilterBy)
241 Thread filterThread = new Thread(new Runnable()
246 long startTime = System.currentTimeMillis();
249 lbl_loading.setVisible(true);
251 Collection<PDBDocField> wantedFields = PDBDocFieldPreferences
252 .getStructureSummaryFields();
253 Collection<PDBResponseSummary> filteredResponse = new HashSet<PDBResponseSummary>();
254 for (SequenceI seq : selectedSequences)
256 PDBRestRequest pdbRequest = new PDBRestRequest();
257 pdbRequest.setAllowEmptySeq(false);
258 pdbRequest.setResponseSize(1);
259 pdbRequest.setFieldToSearchBy("(text:");
260 pdbRequest.setFieldToSortBy(fieldToFilterBy,
261 !chk_invertFilter.isSelected());
262 pdbRequest.setSearchTerm(buildQuery(seq) + ")");
263 pdbRequest.setWantedFields(wantedFields);
264 pdbRequest.setAssociatedSequence(seq.getName());
265 pdbRestCleint = new PDBRestClient();
266 PDBRestResponse resultList = pdbRestCleint
267 .executeRequest(pdbRequest);
268 lastPdbRequest = pdbRequest;
269 if (resultList.getSearchSummary() != null
270 && !resultList.getSearchSummary().isEmpty())
272 filteredResponse.addAll(resultList.getSearchSummary());
276 if (!filteredResponse.isEmpty())
278 final int filterResponseCount = filteredResponse.size();
279 Collection<PDBResponseSummary> reorderedStructuresSet = new LinkedHashSet<PDBResponseSummary>();
280 reorderedStructuresSet.addAll(filteredResponse);
281 reorderedStructuresSet.addAll(discoveredStructuresSet);
282 tbl_summary.setModel(PDBRestResponse.getTableModel(
283 lastPdbRequest, reorderedStructuresSet));
285 // Update table selection model here
286 tbl_summary.addRowSelectionInterval(0, filterResponseCount - 1);
290 lbl_loading.setVisible(false);
291 String totalTime = (System.currentTimeMillis() - startTime)
293 mainFrame.setTitle("Structure Chooser - Filter time ("
296 validateSelections();
297 } catch (Exception e)
303 filterThread.start();
308 * Handles action event for btn_pdbFromFile
310 public void pdbFromFile_actionPerformed()
312 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(
313 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
314 chooser.setFileView(new jalview.io.JalviewFileView());
315 chooser.setDialogTitle(MessageManager.formatMessage(
316 "label.select_pdb_file_for", new String[]
317 { selectedSequence.getDisplayId(false) }));
318 chooser.setToolTipText(MessageManager.formatMessage(
319 "label.load_pdb_file_associate_with_sequence", new String[]
320 { selectedSequence.getDisplayId(false) }));
322 int value = chooser.showOpenDialog(null);
323 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
325 selectedPdbFileName = chooser.getSelectedFile().getPath();
326 jalview.bin.Cache.setProperty("LAST_DIRECTORY", selectedPdbFileName);
327 validateSelections();
332 * Populates the filter combo-box options dynamically depending on discovered
335 protected void populateFilterComboBox()
337 if (isStructuresDiscovered())
339 cmb_filterOption.addItem(new FilterOption("Best Quality",
340 PDBDocField.OVERALL_QUALITY.getCode(), VIEWS_FILTER));
341 cmb_filterOption.addItem(new FilterOption("Best UniProt Coverage",
342 PDBDocField.UNIPROT_COVERAGE.getCode(), VIEWS_FILTER));
343 cmb_filterOption.addItem(new FilterOption("Highest Resolution",
344 PDBDocField.RESOLUTION.getCode(), VIEWS_FILTER));
345 cmb_filterOption.addItem(new FilterOption("Highest Protein Chain",
346 PDBDocField.PROTEIN_CHAIN_COUNT.getCode(), VIEWS_FILTER));
347 cmb_filterOption.addItem(new FilterOption("Highest Bound Molecules",
348 PDBDocField.BOUND_MOLECULE_COUNT.getCode(), VIEWS_FILTER));
349 cmb_filterOption.addItem(new FilterOption("Highest Polymer Residues",
350 PDBDocField.POLYMER_RESIDUE_COUNT.getCode(), VIEWS_FILTER));
352 cmb_filterOption.addItem(new FilterOption("Enter PDB Id", "-",
354 cmb_filterOption.addItem(new FilterOption("From File", "-",
359 * Updates the displayed view based on the selected filter option
361 protected void updateCurrentView()
363 FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
365 layout_switchableViews.show(pnl_switchableViews,
366 selectedFilterOpt.getView());
367 String filterTitle = mainFrame.getTitle();
368 mainFrame.setTitle(frameTitle);
369 chk_invertFilter.setVisible(false);
370 if (selectedFilterOpt.getView() == VIEWS_FILTER)
372 mainFrame.setTitle(filterTitle);
373 chk_invertFilter.setVisible(true);
374 filterResultSet(selectedFilterOpt.getValue());
378 idInputAssSeqPanel.loadCmbAssSeq();
379 fileChooserAssSeqPanel.loadCmbAssSeq();
381 validateSelections();
385 * Validates user selection and activates the view button if all parameters
388 public void validateSelections()
390 FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
392 btn_view.setEnabled(false);
393 String currentView = selectedFilterOpt.getView();
394 if (currentView == VIEWS_FILTER)
396 if (tbl_summary.getSelectedRows().length > 0)
398 btn_view.setEnabled(true);
401 else if (currentView == VIEWS_ENTER_ID)
403 validateAssociationEnterPdb();
405 else if (currentView == VIEWS_FROM_FILE)
407 validateAssociationFromFile();
412 * Validates inputs from the Manual PDB entry panel
414 public void validateAssociationEnterPdb()
416 AssociateSeqOptions assSeqOpt = (AssociateSeqOptions) idInputAssSeqPanel
417 .getCmb_assSeq().getSelectedItem();
418 lbl_pdbManualFetchStatus.setIcon(errorImage);
419 if (selectedSequences.length == 1
420 || !assSeqOpt.getName().equalsIgnoreCase(
421 "-Select Associated Seq-"))
423 txt_search.setEnabled(true);
426 btn_view.setEnabled(true);
427 lbl_pdbManualFetchStatus.setIcon(goodImage);
432 txt_search.setEnabled(false);
433 lbl_pdbManualFetchStatus.setIcon(errorImage);
438 * Validates inputs for the manual PDB file selection options
440 public void validateAssociationFromFile()
442 AssociateSeqOptions assSeqOpt = (AssociateSeqOptions) fileChooserAssSeqPanel
443 .getCmb_assSeq().getSelectedItem();
444 lbl_fromFileStatus.setIcon(errorImage);
445 if (selectedSequences.length == 1
446 || (assSeqOpt != null
447 && !assSeqOpt.getName().equalsIgnoreCase(
448 "-Select Associated Seq-")))
450 btn_pdbFromFile.setEnabled(true);
451 if (selectedPdbFileName != null && selectedPdbFileName.length() > 0)
453 btn_view.setEnabled(true);
454 lbl_fromFileStatus.setIcon(goodImage);
459 btn_pdbFromFile.setEnabled(false);
460 lbl_fromFileStatus.setIcon(errorImage);
465 public void cmbAssSeqStateChanged()
467 validateSelections();
471 * Handles the state change event for the 'filter' combo-box and 'invert'
475 protected void stateChanged(ItemEvent e)
477 if (e.getSource() instanceof JCheckBox)
483 if (e.getStateChange() == ItemEvent.SELECTED)
492 * Handles action event for btn_ok
495 public void ok_ActionPerformed()
497 FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
499 String currentView = selectedFilterOpt.getView();
500 if (currentView == VIEWS_FILTER)
502 int pdbIdCol = PDBRestClient.getPDBIdColumIndex(
503 lastPdbRequest.getWantedFields(), true);
504 int[] selectedRows = tbl_summary.getSelectedRows();
505 PDBEntry[] pdbEntriesToView = new PDBEntry[selectedRows.length];
507 for (int summaryRow : selectedRows)
509 String pdbIdStr = tbl_summary.getValueAt(summaryRow, pdbIdCol)
511 PDBEntry pdbEntry = new PDBEntry();
512 pdbEntry.setId(pdbIdStr);
513 pdbEntry.setType("PDB");
514 pdbEntriesToView[count++] = pdbEntry;
516 new StructureViewer(ap.getStructureSelectionManager())
517 .viewStructures(ap, pdbEntriesToView,
518 ap.av.collateForPDB(pdbEntriesToView));
520 else if (currentView == VIEWS_ENTER_ID)
522 selectedSequence = ((AssociateSeqOptions) idInputAssSeqPanel
523 .getCmb_assSeq().getSelectedItem()).getSequence();
524 PDBEntry pdbEntry = new PDBEntry();
525 pdbEntry.setId(txt_search.getText());
526 pdbEntry.setType("PDB");
527 selectedSequence.getDatasetSequence().addPDBId(pdbEntry);
528 PDBEntry[] pdbEntriesToView = new PDBEntry[]
530 new StructureViewer(ap.getStructureSelectionManager())
531 .viewStructures(ap, pdbEntriesToView,
532 ap.av.collateForPDB(pdbEntriesToView));
534 else if (currentView == VIEWS_FROM_FILE)
536 selectedSequence = ((AssociateSeqOptions) fileChooserAssSeqPanel
537 .getCmb_assSeq().getSelectedItem()).getSequence();
538 new AssociatePdbFileWithSeq().associatePdbWithSeq(
539 selectedPdbFileName, jalview.io.AppletFormatAdapter.FILE,
540 selectedSequence, true, Desktop.instance);
546 * Populates the combo-box used in associating manually fetched structures to
547 * a unique sequence when more than one sequence selection is made.
549 public void populateCmbAssociateSeqOptions(
550 JComboBox<AssociateSeqOptions> cmb_assSeq, JLabel lbl_associateSeq)
552 cmb_assSeq.removeAllItems();
553 cmb_assSeq.addItem(new AssociateSeqOptions("-Select Associated Seq-",
555 // cmb_assSeq.addItem(new AssociateSeqOptions("Auto Detect", null));
556 lbl_associateSeq.setVisible(false);
557 if (selectedSequences.length > 1)
559 for (SequenceI seq : selectedSequences)
561 cmb_assSeq.addItem(new AssociateSeqOptions(seq));
566 String seqName = selectedSequence.getDisplayId(false);
567 seqName = seqName.length() <= 40 ? seqName : seqName.substring(0, 39);
568 lbl_associateSeq.setText(seqName);
569 lbl_associateSeq.setVisible(true);
570 cmb_assSeq.setVisible(false);
574 public boolean isStructuresDiscovered()
576 return structuresDiscovered;
579 public void setStructuresDiscovered(boolean structuresDiscovered)
581 this.structuresDiscovered = structuresDiscovered;
584 public Collection<PDBResponseSummary> getDiscoveredStructuresSet()
586 return discoveredStructuresSet;
590 protected void txt_search_ActionPerformed()
592 isValidPBDEntry = false;
593 if (txt_search.getText().length() > 0)
595 List<PDBDocField> wantedFields = new ArrayList<PDBDocField>();
596 wantedFields.add(PDBDocField.PDB_ID);
597 PDBRestRequest pdbRequest = new PDBRestRequest();
598 pdbRequest.setAllowEmptySeq(false);
599 pdbRequest.setResponseSize(1);
600 pdbRequest.setFieldToSearchBy("(pdb_id:");
601 pdbRequest.setWantedFields(wantedFields);
602 pdbRequest.setSearchTerm(txt_search.getText() + ")");
603 pdbRequest.setAssociatedSequence(selectedSequence.getName());
604 pdbRestCleint = new PDBRestClient();
605 PDBRestResponse resultList = pdbRestCleint.executeRequest(pdbRequest);
606 if (resultList.getSearchSummary() != null
607 && resultList.getSearchSummary().size() > 0)
609 isValidPBDEntry = true;
612 validateSelections();
616 public void tabRefresh()
618 if (selectedSequences != null)
620 Thread refreshThread = new Thread(new Runnable()
625 fetchStructuresMetaData();
626 filterResultSet(((FilterOption) cmb_filterOption
627 .getSelectedItem()).getValue());
630 refreshThread.start();