JAL-2944 tidies for review comments
[jalview.git] / src / jalview / gui / StructureChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21
22 package jalview.gui;
23
24 import jalview.api.structures.JalviewStructureDisplayI;
25 import jalview.bin.Cache;
26 import jalview.bin.Jalview;
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.DBRefSource;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.SequenceI;
31 import jalview.fts.api.FTSData;
32 import jalview.fts.api.FTSDataColumnI;
33 import jalview.fts.api.FTSRestClientI;
34 import jalview.fts.core.FTSRestRequest;
35 import jalview.fts.core.FTSRestResponse;
36 import jalview.fts.service.pdb.PDBFTSRestClient;
37 import jalview.io.DataSourceType;
38 import jalview.jbgui.GStructureChooser;
39 import jalview.structure.StructureMapping;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.util.MessageManager;
42 import jalview.ws.DBRefFetcher;
43 import jalview.ws.sifts.SiftsSettings;
44
45 import java.awt.event.ItemEvent;
46 import java.util.ArrayList;
47 import java.util.Collection;
48 import java.util.HashSet;
49 import java.util.LinkedHashSet;
50 import java.util.List;
51 import java.util.Objects;
52 import java.util.Set;
53 import java.util.Vector;
54
55 import javax.swing.JCheckBox;
56 import javax.swing.JComboBox;
57 import javax.swing.JLabel;
58 import javax.swing.JTable;
59 import javax.swing.SwingUtilities;
60 import javax.swing.table.AbstractTableModel;
61
62 /**
63  * Provides the behaviors for the Structure chooser Panel
64  * 
65  * @author tcnofoegbu
66  *
67  */
68 @SuppressWarnings("serial")
69 public class StructureChooser extends GStructureChooser
70         implements IProgressIndicator
71 {
72   private static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE";
73
74   private static int MAX_QLENGTH = 7820;
75
76   private SequenceI selectedSequence;
77
78   private SequenceI[] selectedSequences;
79
80   private IProgressIndicator progressIndicator;
81
82   private Collection<FTSData> discoveredStructuresSet;
83
84   private FTSRestRequest lastPdbRequest;
85
86   private FTSRestClientI pdbRestCleint;
87
88   private String selectedPdbFileName;
89
90   private boolean isValidPBDEntry;
91
92   private boolean cachedPDBExists;
93
94   private static StructureViewer lastTargetedView = null;
95
96   public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq,
97           AlignmentPanel ap)
98   {
99     this.ap = ap;
100     this.selectedSequence = selectedSeq;
101     this.selectedSequences = selectedSeqs;
102     this.progressIndicator = (ap == null) ? null : ap.alignFrame;
103     init();
104   }
105
106   /**
107    * Initializes parameters used by the Structure Chooser Panel
108    */
109   protected void init()
110   {
111     if (!Jalview.isHeadlessMode())
112     {
113       progressBar = new ProgressBar(this.statusPanel, this.statusBar);
114     }
115
116     chk_superpose.setSelected(Cache.getDefault(AUTOSUPERIMPOSE, true));
117
118     // ensure a filter option is in force for search
119     populateFilterComboBox(true, cachedPDBExists);
120     Thread discoverPDBStructuresThread = new Thread(new Runnable()
121     {
122       @Override
123       public void run()
124       {
125         long startTime = System.currentTimeMillis();
126         updateProgressIndicator(MessageManager
127                 .getString("status.loading_cached_pdb_entries"), startTime);
128         loadLocalCachedPDBEntries();
129         updateProgressIndicator(null, startTime);
130         updateProgressIndicator(MessageManager.getString(
131                 "status.searching_for_pdb_structures"), startTime);
132         fetchStructuresMetaData();
133         // revise filter options if no results were found
134         populateFilterComboBox(isStructuresDiscovered(), cachedPDBExists);
135         discoverStructureViews();
136         updateProgressIndicator(null, startTime);
137         mainFrame.setVisible(true);
138         updateCurrentView();
139       }
140     });
141     discoverPDBStructuresThread.start();
142   }
143
144   /**
145    * Builds a drop-down choice list of existing structure viewers to which new
146    * structures may be added. If this list is empty then it, and the 'Add'
147    * button, are hidden.
148    */
149   private void discoverStructureViews()
150   {
151     if (Desktop.instance != null)
152     {
153       targetView.removeAllItems();
154       if (lastTargetedView != null && !lastTargetedView.isVisible())
155       {
156         lastTargetedView = null;
157       }
158       int linkedViewsAt = 0;
159       for (StructureViewerBase view : Desktop.instance
160               .getStructureViewers(null, null))
161       {
162         StructureViewer viewHandler = (lastTargetedView != null
163                 && lastTargetedView.sview == view) ? lastTargetedView
164                         : StructureViewer.reconfigure(view);
165
166         if (view.isLinkedWith(ap))
167         {
168           targetView.insertItemAt(viewHandler,
169                   linkedViewsAt++);
170         }
171         else
172         {
173           targetView.addItem(viewHandler);
174         }
175       }
176
177       /*
178        * show option to Add to viewer if at least 1 viewer found
179        */
180       targetView.setVisible(false);
181       if (targetView.getItemCount() > 0)
182       {
183         targetView.setVisible(true);
184         if (lastTargetedView != null)
185         {
186           targetView.setSelectedItem(lastTargetedView);
187         }
188         else
189         {
190           targetView.setSelectedIndex(0);
191         }
192       }
193       btn_add.setVisible(targetView.isVisible());
194     }
195   }
196
197   /**
198    * Updates the progress indicator with the specified message
199    * 
200    * @param message
201    *          displayed message for the operation
202    * @param id
203    *          unique handle for this indicator
204    */
205   protected void updateProgressIndicator(String message, long id)
206   {
207     if (progressIndicator != null)
208     {
209       progressIndicator.setProgressBar(message, id);
210     }
211   }
212
213   /**
214    * Retrieve meta-data for all the structure(s) for a given sequence(s) in a
215    * selection group
216    */
217   void fetchStructuresMetaData()
218   {
219     long startTime = System.currentTimeMillis();
220     pdbRestCleint = PDBFTSRestClient.getInstance();
221     Collection<FTSDataColumnI> wantedFields = pdbDocFieldPrefs
222             .getStructureSummaryFields();
223
224     discoveredStructuresSet = new LinkedHashSet<>();
225     HashSet<String> errors = new HashSet<>();
226     for (SequenceI seq : selectedSequences)
227     {
228       FTSRestRequest pdbRequest = new FTSRestRequest();
229       pdbRequest.setAllowEmptySeq(false);
230       pdbRequest.setResponseSize(500);
231       pdbRequest.setFieldToSearchBy("(");
232       FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
233               .getSelectedItem());
234       pdbRequest.setFieldToSortBy(selectedFilterOpt.getValue(),
235               !chk_invertFilter.isSelected());
236       pdbRequest.setWantedFields(wantedFields);
237       pdbRequest.setSearchTerm(buildQuery(seq) + ")");
238       pdbRequest.setAssociatedSequence(seq);
239       FTSRestResponse resultList;
240       try
241       {
242         resultList = pdbRestCleint.executeRequest(pdbRequest);
243       } catch (Exception e)
244       {
245         e.printStackTrace();
246         errors.add(e.getMessage());
247         continue;
248       }
249       lastPdbRequest = pdbRequest;
250       if (resultList.getSearchSummary() != null
251               && !resultList.getSearchSummary().isEmpty())
252       {
253         discoveredStructuresSet.addAll(resultList.getSearchSummary());
254       }
255     }
256
257     int noOfStructuresFound = 0;
258     String totalTime = (System.currentTimeMillis() - startTime)
259             + " milli secs";
260     if (discoveredStructuresSet != null
261             && !discoveredStructuresSet.isEmpty())
262     {
263       getResultTable().setModel(FTSRestResponse
264               .getTableModel(lastPdbRequest, discoveredStructuresSet));
265       noOfStructuresFound = discoveredStructuresSet.size();
266       mainFrame.setTitle(MessageManager.formatMessage(
267               "label.structure_chooser_no_of_structures",
268               noOfStructuresFound, totalTime));
269     }
270     else
271     {
272       mainFrame.setTitle(MessageManager
273               .getString("label.structure_chooser_manual_association"));
274       if (errors.size() > 0)
275       {
276         StringBuilder errorMsg = new StringBuilder();
277         for (String error : errors)
278         {
279           errorMsg.append(error).append("\n");
280         }
281         JvOptionPane.showMessageDialog(this, errorMsg.toString(),
282                 MessageManager.getString("label.pdb_web-service_error"),
283                 JvOptionPane.ERROR_MESSAGE);
284       }
285     }
286   }
287
288   protected void loadLocalCachedPDBEntries()
289   {
290     ArrayList<CachedPDB> entries = new ArrayList<>();
291     for (SequenceI seq : selectedSequences)
292     {
293       if (seq.getDatasetSequence() != null
294               && seq.getDatasetSequence().getAllPDBEntries() != null)
295       {
296         for (PDBEntry pdbEntry : seq.getDatasetSequence()
297                 .getAllPDBEntries())
298         {
299           if (pdbEntry.getFile() != null)
300           {
301             entries.add(new CachedPDB(seq, pdbEntry));
302           }
303         }
304       }
305     }
306     cachedPDBExists = !entries.isEmpty();
307     PDBEntryTableModel tableModelx = new PDBEntryTableModel(entries);
308     tbl_local_pdb.setModel(tableModelx);
309   }
310
311   /**
312    * Builds a query string for a given sequences using its DBRef entries
313    * 
314    * @param seq
315    *          the sequences to build a query for
316    * @return the built query string
317    */
318
319   static String buildQuery(SequenceI seq)
320   {
321     boolean isPDBRefsFound = false;
322     boolean isUniProtRefsFound = false;
323     StringBuilder queryBuilder = new StringBuilder();
324     Set<String> seqRefs = new LinkedHashSet<>();
325
326     if (seq.getAllPDBEntries() != null
327             && queryBuilder.length() < MAX_QLENGTH)
328     {
329       for (PDBEntry entry : seq.getAllPDBEntries())
330       {
331         if (isValidSeqName(entry.getId()))
332         {
333           queryBuilder.append("pdb_id:").append(entry.getId().toLowerCase())
334                   .append(" OR ");
335           isPDBRefsFound = true;
336         }
337       }
338     }
339
340     if (seq.getDBRefs() != null && seq.getDBRefs().length != 0)
341     {
342       for (DBRefEntry dbRef : seq.getDBRefs())
343       {
344         if (isValidSeqName(getDBRefId(dbRef))
345                 && queryBuilder.length() < MAX_QLENGTH)
346         {
347           if (dbRef.getSource().equalsIgnoreCase(DBRefSource.UNIPROT))
348           {
349             queryBuilder.append("uniprot_accession:")
350                     .append(getDBRefId(dbRef)).append(" OR ");
351             queryBuilder.append("uniprot_id:").append(getDBRefId(dbRef))
352                     .append(" OR ");
353             isUniProtRefsFound = true;
354           }
355           else if (dbRef.getSource().equalsIgnoreCase(DBRefSource.PDB))
356           {
357
358             queryBuilder.append("pdb_id:")
359                     .append(getDBRefId(dbRef).toLowerCase()).append(" OR ");
360             isPDBRefsFound = true;
361           }
362           else
363           {
364             seqRefs.add(getDBRefId(dbRef));
365           }
366         }
367       }
368     }
369
370     if (!isPDBRefsFound && !isUniProtRefsFound)
371     {
372       String seqName = seq.getName();
373       seqName = sanitizeSeqName(seqName);
374       String[] names = seqName.toLowerCase().split("\\|");
375       for (String name : names)
376       {
377         // System.out.println("Found name : " + name);
378         name.trim();
379         if (isValidSeqName(name))
380         {
381           seqRefs.add(name);
382         }
383       }
384
385       for (String seqRef : seqRefs)
386       {
387         queryBuilder.append("text:").append(seqRef).append(" OR ");
388       }
389     }
390
391     int endIndex = queryBuilder.lastIndexOf(" OR ");
392     if (queryBuilder.toString().length() < 6)
393     {
394       return null;
395     }
396     String query = queryBuilder.toString().substring(0, endIndex);
397     return query;
398   }
399
400   /**
401    * Remove the following special characters from input string +, -, &, !, (, ),
402    * {, }, [, ], ^, ", ~, *, ?, :, \
403    * 
404    * @param seqName
405    * @return
406    */
407   static String sanitizeSeqName(String seqName)
408   {
409     Objects.requireNonNull(seqName);
410     return seqName.replaceAll("\\[\\d*\\]", "")
411             .replaceAll("[^\\dA-Za-z|_]", "").replaceAll("\\s+", "+");
412   }
413
414   /**
415    * Ensures sequence ref names are not less than 3 characters and does not
416    * contain a database name
417    * 
418    * @param seqName
419    * @return
420    */
421   static boolean isValidSeqName(String seqName)
422   {
423     // System.out.println("seqName : " + seqName);
424     String ignoreList = "pdb,uniprot,swiss-prot";
425     if (seqName.length() < 3)
426     {
427       return false;
428     }
429     if (seqName.contains(":"))
430     {
431       return false;
432     }
433     seqName = seqName.toLowerCase();
434     for (String ignoredEntry : ignoreList.split(","))
435     {
436       if (seqName.contains(ignoredEntry))
437       {
438         return false;
439       }
440     }
441     return true;
442   }
443
444   static String getDBRefId(DBRefEntry dbRef)
445   {
446     String ref = dbRef.getAccessionId().replaceAll("GO:", "");
447     return ref;
448   }
449
450   /**
451    * Filters a given list of discovered structures based on supplied argument
452    * 
453    * @param fieldToFilterBy
454    *          the field to filter by
455    */
456   void filterResultSet(final String fieldToFilterBy)
457   {
458     Thread filterThread = new Thread(new Runnable()
459     {
460       @Override
461       public void run()
462       {
463         long startTime = System.currentTimeMillis();
464         pdbRestCleint = PDBFTSRestClient.getInstance();
465         lbl_loading.setVisible(true);
466         Collection<FTSDataColumnI> wantedFields = pdbDocFieldPrefs
467                 .getStructureSummaryFields();
468         Collection<FTSData> filteredResponse = new HashSet<>();
469         HashSet<String> errors = new HashSet<>();
470
471         for (SequenceI seq : selectedSequences)
472         {
473           FTSRestRequest pdbRequest = new FTSRestRequest();
474           if (fieldToFilterBy.equalsIgnoreCase("uniprot_coverage"))
475           {
476             pdbRequest.setAllowEmptySeq(false);
477             pdbRequest.setResponseSize(1);
478             pdbRequest.setFieldToSearchBy("(");
479             pdbRequest.setSearchTerm(buildQuery(seq) + ")");
480             pdbRequest.setWantedFields(wantedFields);
481             pdbRequest.setAssociatedSequence(seq);
482             pdbRequest.setFacet(true);
483             pdbRequest.setFacetPivot(fieldToFilterBy + ",entry_entity");
484             pdbRequest.setFacetPivotMinCount(1);
485           }
486           else
487           {
488             pdbRequest.setAllowEmptySeq(false);
489             pdbRequest.setResponseSize(1);
490             pdbRequest.setFieldToSearchBy("(");
491             pdbRequest.setFieldToSortBy(fieldToFilterBy,
492                     !chk_invertFilter.isSelected());
493             pdbRequest.setSearchTerm(buildQuery(seq) + ")");
494             pdbRequest.setWantedFields(wantedFields);
495             pdbRequest.setAssociatedSequence(seq);
496           }
497           FTSRestResponse resultList;
498           try
499           {
500             resultList = pdbRestCleint.executeRequest(pdbRequest);
501           } catch (Exception e)
502           {
503             e.printStackTrace();
504             errors.add(e.getMessage());
505             continue;
506           }
507           lastPdbRequest = pdbRequest;
508           if (resultList.getSearchSummary() != null
509                   && !resultList.getSearchSummary().isEmpty())
510           {
511             filteredResponse.addAll(resultList.getSearchSummary());
512           }
513         }
514
515         String totalTime = (System.currentTimeMillis() - startTime)
516                 + " milli secs";
517         if (!filteredResponse.isEmpty())
518         {
519           final int filterResponseCount = filteredResponse.size();
520           Collection<FTSData> reorderedStructuresSet = new LinkedHashSet<>();
521           reorderedStructuresSet.addAll(filteredResponse);
522           reorderedStructuresSet.addAll(discoveredStructuresSet);
523           getResultTable().setModel(FTSRestResponse
524                   .getTableModel(lastPdbRequest, reorderedStructuresSet));
525
526           FTSRestResponse.configureTableColumn(getResultTable(),
527                   wantedFields, tempUserPrefs);
528           getResultTable().getColumn("Ref Sequence").setPreferredWidth(120);
529           getResultTable().getColumn("Ref Sequence").setMinWidth(100);
530           getResultTable().getColumn("Ref Sequence").setMaxWidth(200);
531           // Update table selection model here
532           getResultTable().addRowSelectionInterval(0,
533                   filterResponseCount - 1);
534           mainFrame.setTitle(MessageManager.formatMessage(
535                   "label.structure_chooser_filter_time", totalTime));
536         }
537         else
538         {
539           mainFrame.setTitle(MessageManager.formatMessage(
540                   "label.structure_chooser_filter_time", totalTime));
541           if (errors.size() > 0)
542           {
543             StringBuilder errorMsg = new StringBuilder();
544             for (String error : errors)
545             {
546               errorMsg.append(error).append("\n");
547             }
548             JvOptionPane.showMessageDialog(null, errorMsg.toString(),
549                     MessageManager.getString("label.pdb_web-service_error"),
550                     JvOptionPane.ERROR_MESSAGE);
551           }
552         }
553
554         lbl_loading.setVisible(false);
555
556         validateSelections();
557       }
558     });
559     filterThread.start();
560   }
561
562   /**
563    * Handles action event for btn_pdbFromFile
564    */
565   @Override
566   protected void pdbFromFile_actionPerformed()
567   {
568     jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(
569             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
570     chooser.setFileView(new jalview.io.JalviewFileView());
571     chooser.setDialogTitle(
572             MessageManager.formatMessage("label.select_pdb_file_for",
573                     selectedSequence.getDisplayId(false)));
574     chooser.setToolTipText(MessageManager.formatMessage(
575             "label.load_pdb_file_associate_with_sequence",
576             selectedSequence.getDisplayId(false)));
577
578     int value = chooser.showOpenDialog(null);
579     if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
580     {
581       selectedPdbFileName = chooser.getSelectedFile().getPath();
582       jalview.bin.Cache.setProperty("LAST_DIRECTORY", selectedPdbFileName);
583       validateSelections();
584     }
585   }
586
587   /**
588    * Populates the filter combo-box options dynamically depending on discovered
589    * structures
590    */
591   protected void populateFilterComboBox(boolean haveData,
592           boolean cachedPDBExist)
593   {
594     /*
595      * temporarily suspend the change listener behaviour
596      */
597     cmb_filterOption.removeItemListener(this);
598
599     cmb_filterOption.removeAllItems();
600     if (haveData)
601     {
602       cmb_filterOption.addItem(new FilterOption(
603               MessageManager.getString("label.best_quality"),
604               "overall_quality", VIEWS_FILTER, false));
605       cmb_filterOption.addItem(new FilterOption(
606               MessageManager.getString("label.best_resolution"),
607               "resolution", VIEWS_FILTER, false));
608       cmb_filterOption.addItem(new FilterOption(
609               MessageManager.getString("label.most_protein_chain"),
610               "number_of_protein_chains", VIEWS_FILTER, false));
611       cmb_filterOption.addItem(new FilterOption(
612               MessageManager.getString("label.most_bound_molecules"),
613               "number_of_bound_molecules", VIEWS_FILTER, false));
614       cmb_filterOption.addItem(new FilterOption(
615               MessageManager.getString("label.most_polymer_residues"),
616               "number_of_polymer_residues", VIEWS_FILTER, true));
617     }
618     cmb_filterOption.addItem(
619             new FilterOption(MessageManager.getString("label.enter_pdb_id"),
620                     "-", VIEWS_ENTER_ID, false));
621     cmb_filterOption.addItem(
622             new FilterOption(MessageManager.getString("label.from_file"),
623                     "-", VIEWS_FROM_FILE, false));
624
625     if (cachedPDBExist)
626     {
627       FilterOption cachedOption = new FilterOption(
628               MessageManager.getString("label.cached_structures"),
629               "-", VIEWS_LOCAL_PDB, false);
630       cmb_filterOption.addItem(cachedOption);
631       cmb_filterOption.setSelectedItem(cachedOption);
632     }
633
634     cmb_filterOption.addItemListener(this);
635   }
636
637   /**
638    * Updates the displayed view based on the selected filter option
639    */
640   protected void updateCurrentView()
641   {
642     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
643             .getSelectedItem());
644     layout_switchableViews.show(pnl_switchableViews,
645             selectedFilterOpt.getView());
646     String filterTitle = mainFrame.getTitle();
647     mainFrame.setTitle(frameTitle);
648     chk_invertFilter.setVisible(false);
649     if (selectedFilterOpt.getView() == VIEWS_FILTER)
650     {
651       mainFrame.setTitle(filterTitle);
652       chk_invertFilter.setVisible(true);
653       filterResultSet(selectedFilterOpt.getValue());
654     }
655     else if (selectedFilterOpt.getView() == VIEWS_ENTER_ID
656             || selectedFilterOpt.getView() == VIEWS_FROM_FILE)
657     {
658       mainFrame.setTitle(MessageManager
659               .getString("label.structure_chooser_manual_association"));
660       idInputAssSeqPanel.loadCmbAssSeq();
661       fileChooserAssSeqPanel.loadCmbAssSeq();
662     }
663     validateSelections();
664   }
665
666   /**
667    * Validates user selection and enables the 'Add' and 'New View' buttons if
668    * all parameters are correct (the Add button will only be visible if there is
669    * at least one existing structure viewer open). This basically means at least
670    * one structure selected and no error messages.
671    * <p>
672    * The 'Superpose Structures' option is enabled if either more than one
673    * structure is selected, or the 'Add' to existing view option is enabled, and
674    * disabled if the only option is to open a new view of a single structure.
675    */
676   @Override
677   protected void validateSelections()
678   {
679     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
680             .getSelectedItem());
681     btn_add.setEnabled(false);
682     String currentView = selectedFilterOpt.getView();
683     int selectedCount = 0;
684     if (currentView == VIEWS_FILTER)
685     {
686       selectedCount = getResultTable().getSelectedRows().length;
687       if (selectedCount > 0)
688       {
689         btn_add.setEnabled(true);
690       }
691     }
692     else if (currentView == VIEWS_LOCAL_PDB)
693     {
694       selectedCount = tbl_local_pdb.getSelectedRows().length;
695       if (selectedCount > 0)
696       {
697         btn_add.setEnabled(true);
698       }
699     }
700     else if (currentView == VIEWS_ENTER_ID)
701     {
702       validateAssociationEnterPdb();
703     }
704     else if (currentView == VIEWS_FROM_FILE)
705     {
706       validateAssociationFromFile();
707     }
708
709     btn_newView.setEnabled(btn_add.isEnabled());
710
711     /*
712      * enable 'Superpose' option if more than one structure is selected,
713      * or there are view(s) available to add structure(s) to
714      */
715     chk_superpose
716             .setEnabled(selectedCount > 1 || targetView.getItemCount() > 0);
717   }
718
719   /**
720    * Validates inputs from the Manual PDB entry panel
721    */
722   protected void validateAssociationEnterPdb()
723   {
724     AssociateSeqOptions assSeqOpt = (AssociateSeqOptions) idInputAssSeqPanel
725             .getCmb_assSeq().getSelectedItem();
726     lbl_pdbManualFetchStatus.setIcon(errorImage);
727     lbl_pdbManualFetchStatus.setToolTipText("");
728     if (txt_search.getText().length() > 0)
729     {
730       lbl_pdbManualFetchStatus.setToolTipText(JvSwingUtils.wrapTooltip(true,
731               MessageManager.formatMessage("info.no_pdb_entry_found_for",
732                       txt_search.getText())));
733     }
734
735     if (errorWarning.length() > 0)
736     {
737       lbl_pdbManualFetchStatus.setIcon(warningImage);
738       lbl_pdbManualFetchStatus.setToolTipText(
739               JvSwingUtils.wrapTooltip(true, errorWarning.toString()));
740     }
741
742     if (selectedSequences.length == 1 || !assSeqOpt.getName()
743             .equalsIgnoreCase("-Select Associated Seq-"))
744     {
745       txt_search.setEnabled(true);
746       if (isValidPBDEntry)
747       {
748         btn_add.setEnabled(true);
749         lbl_pdbManualFetchStatus.setToolTipText("");
750         lbl_pdbManualFetchStatus.setIcon(goodImage);
751       }
752     }
753     else
754     {
755       txt_search.setEnabled(false);
756       lbl_pdbManualFetchStatus.setIcon(errorImage);
757     }
758   }
759
760   /**
761    * Validates inputs for the manual PDB file selection options
762    */
763   protected void validateAssociationFromFile()
764   {
765     AssociateSeqOptions assSeqOpt = (AssociateSeqOptions) fileChooserAssSeqPanel
766             .getCmb_assSeq().getSelectedItem();
767     lbl_fromFileStatus.setIcon(errorImage);
768     if (selectedSequences.length == 1 || (assSeqOpt != null && !assSeqOpt
769             .getName().equalsIgnoreCase("-Select Associated Seq-")))
770     {
771       btn_pdbFromFile.setEnabled(true);
772       if (selectedPdbFileName != null && selectedPdbFileName.length() > 0)
773       {
774         btn_add.setEnabled(true);
775         lbl_fromFileStatus.setIcon(goodImage);
776       }
777     }
778     else
779     {
780       btn_pdbFromFile.setEnabled(false);
781       lbl_fromFileStatus.setIcon(errorImage);
782     }
783   }
784
785   @Override
786   protected void cmbAssSeqStateChanged()
787   {
788     validateSelections();
789   }
790
791   /**
792    * Handles the state change event for the 'filter' combo-box and 'invert'
793    * check-box
794    */
795   @Override
796   protected void stateChanged(ItemEvent e)
797   {
798     if (e.getSource() instanceof JCheckBox)
799     {
800       updateCurrentView();
801     }
802     else
803     {
804       if (e.getStateChange() == ItemEvent.SELECTED)
805       {
806         updateCurrentView();
807       }
808     }
809
810   }
811
812   /**
813    * select structures for viewing by their PDB IDs
814    * 
815    * @param pdbids
816    * @return true if structures were found and marked as selected
817    */
818   public boolean selectStructure(String... pdbids)
819   {
820     boolean found = false;
821
822     FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
823             .getSelectedItem());
824     String currentView = selectedFilterOpt.getView();
825     JTable restable = (currentView == VIEWS_FILTER) ? getResultTable()
826             : (currentView == VIEWS_LOCAL_PDB) ? tbl_local_pdb : null;
827
828     if (restable == null)
829     {
830       // can't select (enter PDB ID, or load file - need to also select which
831       // sequence to associate with)
832       return false;
833     }
834
835     int pdbIdColIndex = restable.getColumn("PDB Id").getModelIndex();
836     for (int r = 0; r < restable.getRowCount(); r++)
837     {
838       for (int p = 0; p < pdbids.length; p++)
839       {
840         if (String.valueOf(restable.getValueAt(r, pdbIdColIndex))
841                 .equalsIgnoreCase(pdbids[p]))
842         {
843           restable.setRowSelectionInterval(r, r);
844           found = true;
845         }
846       }
847     }
848     return found;
849   }
850   
851   /**
852    * Handles the 'New View' action
853    */
854   @Override
855   protected void newView_ActionPerformed()
856   {
857     targetView.setSelectedItem(null);
858     showStructures(false);
859   }
860
861   /**
862    * Handles the 'Add to existing viewer' action
863    */
864   @Override
865   protected void add_ActionPerformed()
866   {
867     showStructures(false);
868   }
869
870   /**
871    * structure viewer opened by this dialog, or null
872    */
873   private StructureViewer sViewer = null;
874
875   public void showStructures(boolean waitUntilFinished)
876   {
877
878     final StructureSelectionManager ssm = ap.getStructureSelectionManager();
879
880     final int preferredHeight = pnl_filter.getHeight();
881
882     Runnable viewStruc = new Runnable()
883     {
884       @Override
885       public void run()
886       {
887         FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
888                 .getSelectedItem());
889         String currentView = selectedFilterOpt.getView();
890         JTable restable = (currentView == VIEWS_FILTER) ? getResultTable()
891                 : tbl_local_pdb;
892
893         if (currentView == VIEWS_FILTER)
894         {
895           int pdbIdColIndex = restable.getColumn("PDB Id")
896                   .getModelIndex();
897           int refSeqColIndex = restable.getColumn("Ref Sequence")
898                   .getModelIndex();
899           int[] selectedRows = restable.getSelectedRows();
900           PDBEntry[] pdbEntriesToView = new PDBEntry[selectedRows.length];
901           int count = 0;
902           List<SequenceI> selectedSeqsToView = new ArrayList<>();
903           for (int row : selectedRows)
904           {
905             String pdbIdStr = restable
906                     .getValueAt(row, pdbIdColIndex).toString();
907             SequenceI selectedSeq = (SequenceI) restable
908                     .getValueAt(row, refSeqColIndex);
909             selectedSeqsToView.add(selectedSeq);
910             PDBEntry pdbEntry = selectedSeq.getPDBEntry(pdbIdStr);
911             if (pdbEntry == null)
912             {
913               pdbEntry = getFindEntry(pdbIdStr,
914                       selectedSeq.getAllPDBEntries());
915             }
916
917             if (pdbEntry == null)
918             {
919               pdbEntry = new PDBEntry();
920               pdbEntry.setId(pdbIdStr);
921               pdbEntry.setType(PDBEntry.Type.PDB);
922               selectedSeq.getDatasetSequence().addPDBId(pdbEntry);
923             }
924             pdbEntriesToView[count++] = pdbEntry;
925           }
926           SequenceI[] selectedSeqs = selectedSeqsToView
927                   .toArray(new SequenceI[selectedSeqsToView.size()]);
928           sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
929                   selectedSeqs);
930         }
931         else if (currentView == VIEWS_LOCAL_PDB)
932         {
933           int[] selectedRows = tbl_local_pdb.getSelectedRows();
934           PDBEntry[] pdbEntriesToView = new PDBEntry[selectedRows.length];
935           int count = 0;
936           int pdbIdColIndex = tbl_local_pdb.getColumn("PDB Id")
937                   .getModelIndex();
938           int refSeqColIndex = tbl_local_pdb.getColumn("Ref Sequence")
939                   .getModelIndex();
940           List<SequenceI> selectedSeqsToView = new ArrayList<>();
941           for (int row : selectedRows)
942           {
943             PDBEntry pdbEntry = (PDBEntry) tbl_local_pdb.getValueAt(row,
944                     pdbIdColIndex);
945             pdbEntriesToView[count++] = pdbEntry;
946             SequenceI selectedSeq = (SequenceI) tbl_local_pdb
947                     .getValueAt(row, refSeqColIndex);
948             selectedSeqsToView.add(selectedSeq);
949           }
950           SequenceI[] selectedSeqs = selectedSeqsToView
951                   .toArray(new SequenceI[selectedSeqsToView.size()]);
952           sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
953                   selectedSeqs);
954         }
955         else if (currentView == VIEWS_ENTER_ID)
956         {
957           SequenceI userSelectedSeq = ((AssociateSeqOptions) idInputAssSeqPanel
958                   .getCmb_assSeq().getSelectedItem()).getSequence();
959           if (userSelectedSeq != null)
960           {
961             selectedSequence = userSelectedSeq;
962           }
963           String pdbIdStr = txt_search.getText();
964           PDBEntry pdbEntry = selectedSequence.getPDBEntry(pdbIdStr);
965           if (pdbEntry == null)
966           {
967             pdbEntry = new PDBEntry();
968             if (pdbIdStr.split(":").length > 1)
969             {
970               pdbEntry.setId(pdbIdStr.split(":")[0]);
971               pdbEntry.setChainCode(pdbIdStr.split(":")[1].toUpperCase());
972             }
973             else
974             {
975               pdbEntry.setId(pdbIdStr);
976             }
977             pdbEntry.setType(PDBEntry.Type.PDB);
978             selectedSequence.getDatasetSequence().addPDBId(pdbEntry);
979           }
980
981           PDBEntry[] pdbEntriesToView = new PDBEntry[] { pdbEntry };
982           sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap,
983                   new SequenceI[]
984                   { selectedSequence });
985         }
986         else if (currentView == VIEWS_FROM_FILE)
987         {
988           SequenceI userSelectedSeq = ((AssociateSeqOptions) fileChooserAssSeqPanel
989                   .getCmb_assSeq().getSelectedItem()).getSequence();
990           if (userSelectedSeq != null)
991           {
992             selectedSequence = userSelectedSeq;
993           }
994           PDBEntry fileEntry = new AssociatePdbFileWithSeq()
995                   .associatePdbWithSeq(selectedPdbFileName,
996                           DataSourceType.FILE, selectedSequence, true,
997                           Desktop.instance);
998
999           sViewer = launchStructureViewer(
1000                   ssm, new PDBEntry[]
1001                   { fileEntry }, ap,
1002                   new SequenceI[]
1003                   { selectedSequence });
1004         }
1005         SwingUtilities.invokeLater(new Runnable()
1006         {
1007           @Override
1008           public void run()
1009           {
1010             closeAction(preferredHeight);
1011             mainFrame.dispose();
1012           }
1013         });
1014       }
1015     };
1016     Thread runner = new Thread(viewStruc);
1017     runner.start();
1018     if (waitUntilFinished)
1019     {
1020       while (sViewer == null ? runner.isAlive()
1021               : (sViewer.sview == null ? true
1022                       : !sViewer.sview.hasMapping()))
1023       {
1024         try
1025         {
1026           Thread.sleep(300);
1027         } catch (InterruptedException ie)
1028         {
1029
1030         }
1031       }
1032     }
1033   }
1034
1035   private PDBEntry getFindEntry(String id, Vector<PDBEntry> pdbEntries)
1036   {
1037     Objects.requireNonNull(id);
1038     Objects.requireNonNull(pdbEntries);
1039     PDBEntry foundEntry = null;
1040     for (PDBEntry entry : pdbEntries)
1041     {
1042       if (entry.getId().equalsIgnoreCase(id))
1043       {
1044         return entry;
1045       }
1046     }
1047     return foundEntry;
1048   }
1049
1050   /**
1051    * Answers a structure viewer (new or existing) configured to superimpose
1052    * added structures or not according to the user's choice
1053    * 
1054    * @param ssm
1055    * @return
1056    */
1057   StructureViewer getTargetedStructureViewer(
1058           StructureSelectionManager ssm)
1059   {
1060     Object sv = targetView.getSelectedItem();
1061
1062     return sv == null ? new StructureViewer(ssm) : (StructureViewer) sv;
1063   }
1064
1065   /**
1066    * Adds PDB structures to a new or existing structure viewer
1067    * 
1068    * @param ssm
1069    * @param pdbEntriesToView
1070    * @param alignPanel
1071    * @param sequences
1072    * @return
1073    */
1074   private StructureViewer launchStructureViewer(
1075           StructureSelectionManager ssm,
1076           final PDBEntry[] pdbEntriesToView,
1077           final AlignmentPanel alignPanel, SequenceI[] sequences)
1078   {
1079     long progressId = sequences.hashCode();
1080     setProgressBar(MessageManager
1081             .getString("status.launching_3d_structure_viewer"), progressId);
1082     final StructureViewer theViewer = getTargetedStructureViewer(ssm);
1083     boolean superimpose = chk_superpose.isSelected();
1084     theViewer.setSuperpose(superimpose);
1085
1086     /*
1087      * remember user's choice of superimpose or not
1088      */
1089     Cache.setProperty(AUTOSUPERIMPOSE,
1090             Boolean.valueOf(superimpose).toString());
1091
1092     setProgressBar(null, progressId);
1093     if (SiftsSettings.isMapWithSifts())
1094     {
1095       List<SequenceI> seqsWithoutSourceDBRef = new ArrayList<>();
1096       int p = 0;
1097       // TODO: skip PDBEntry:Sequence pairs where PDBEntry doesn't look like a
1098       // real PDB ID. For moment, we can also safely do this if there is already
1099       // a known mapping between the PDBEntry and the sequence.
1100       for (SequenceI seq : sequences)
1101       {
1102         PDBEntry pdbe = pdbEntriesToView[p++];
1103         if (pdbe != null && pdbe.getFile() != null)
1104         {
1105           StructureMapping[] smm = ssm.getMapping(pdbe.getFile());
1106           if (smm != null && smm.length > 0)
1107           {
1108             for (StructureMapping sm : smm)
1109             {
1110               if (sm.getSequence() == seq)
1111               {
1112                 continue;
1113               }
1114             }
1115           }
1116         }
1117         if (seq.getPrimaryDBRefs().isEmpty())
1118         {
1119           seqsWithoutSourceDBRef.add(seq);
1120           continue;
1121         }
1122       }
1123       if (!seqsWithoutSourceDBRef.isEmpty())
1124       {
1125         int y = seqsWithoutSourceDBRef.size();
1126         setProgressBar(MessageManager.formatMessage(
1127                 "status.fetching_dbrefs_for_sequences_without_valid_refs",
1128                 y), progressId);
1129         SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef
1130                 .toArray(new SequenceI[y]);
1131         DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef);
1132         dbRefFetcher.fetchDBRefs(true);
1133
1134         setProgressBar("Fetch complete.", progressId); // todo i18n
1135       }
1136     }
1137     if (pdbEntriesToView.length > 1)
1138     {
1139       setProgressBar(MessageManager.getString(
1140               "status.fetching_3d_structures_for_selected_entries"),
1141               progressId);
1142       theViewer.viewStructures(pdbEntriesToView, sequences, alignPanel);
1143     }
1144     else
1145     {
1146       setProgressBar(MessageManager.formatMessage(
1147               "status.fetching_3d_structures_for",
1148               pdbEntriesToView[0].getId()),progressId);
1149       theViewer.viewStructures(pdbEntriesToView[0], sequences, alignPanel);
1150     }
1151     setProgressBar(null, progressId);
1152     // remember the last viewer we used...
1153     lastTargetedView = theViewer;
1154     return theViewer;
1155   }
1156
1157   /**
1158    * Populates the combo-box used in associating manually fetched structures to
1159    * a unique sequence when more than one sequence selection is made.
1160    */
1161   @Override
1162   protected void populateCmbAssociateSeqOptions(
1163           JComboBox<AssociateSeqOptions> cmb_assSeq,
1164           JLabel lbl_associateSeq)
1165   {
1166     cmb_assSeq.removeAllItems();
1167     cmb_assSeq.addItem(
1168             new AssociateSeqOptions("-Select Associated Seq-", null));
1169     lbl_associateSeq.setVisible(false);
1170     if (selectedSequences.length > 1)
1171     {
1172       for (SequenceI seq : selectedSequences)
1173       {
1174         cmb_assSeq.addItem(new AssociateSeqOptions(seq));
1175       }
1176     }
1177     else
1178     {
1179       String seqName = selectedSequence.getDisplayId(false);
1180       seqName = seqName.length() <= 40 ? seqName : seqName.substring(0, 39);
1181       lbl_associateSeq.setText(seqName);
1182       lbl_associateSeq.setVisible(true);
1183       cmb_assSeq.setVisible(false);
1184     }
1185   }
1186
1187   protected boolean isStructuresDiscovered()
1188   {
1189     return discoveredStructuresSet != null
1190             && !discoveredStructuresSet.isEmpty();
1191   }
1192
1193   @Override
1194   protected void txt_search_ActionPerformed()
1195   {
1196     new Thread()
1197     {
1198       @Override
1199       public void run()
1200       {
1201         errorWarning.setLength(0);
1202         isValidPBDEntry = false;
1203         if (txt_search.getText().length() > 0)
1204         {
1205           String searchTerm = txt_search.getText().toLowerCase();
1206           searchTerm = searchTerm.split(":")[0];
1207           // System.out.println(">>>>> search term : " + searchTerm);
1208           List<FTSDataColumnI> wantedFields = new ArrayList<>();
1209           FTSRestRequest pdbRequest = new FTSRestRequest();
1210           pdbRequest.setAllowEmptySeq(false);
1211           pdbRequest.setResponseSize(1);
1212           pdbRequest.setFieldToSearchBy("(pdb_id:");
1213           pdbRequest.setWantedFields(wantedFields);
1214           pdbRequest.setSearchTerm(searchTerm + ")");
1215           pdbRequest.setAssociatedSequence(selectedSequence);
1216           pdbRestCleint = PDBFTSRestClient.getInstance();
1217           wantedFields.add(pdbRestCleint.getPrimaryKeyColumn());
1218           FTSRestResponse resultList;
1219           try
1220           {
1221             resultList = pdbRestCleint.executeRequest(pdbRequest);
1222           } catch (Exception e)
1223           {
1224             errorWarning.append(e.getMessage());
1225             return;
1226           } finally
1227           {
1228             validateSelections();
1229           }
1230           if (resultList.getSearchSummary() != null
1231                   && resultList.getSearchSummary().size() > 0)
1232           {
1233             isValidPBDEntry = true;
1234           }
1235         }
1236         validateSelections();
1237       }
1238     }.start();
1239   }
1240
1241   @Override
1242   protected void tabRefresh()
1243   {
1244     if (selectedSequences != null)
1245     {
1246       Thread refreshThread = new Thread(new Runnable()
1247       {
1248         @Override
1249         public void run()
1250         {
1251           fetchStructuresMetaData();
1252           filterResultSet(
1253                   ((FilterOption) cmb_filterOption.getSelectedItem())
1254                           .getValue());
1255         }
1256       });
1257       refreshThread.start();
1258     }
1259   }
1260
1261   public class PDBEntryTableModel extends AbstractTableModel
1262   {
1263     String[] columns = { "Ref Sequence", "PDB Id", "Chain", "Type",
1264         "File" };
1265
1266     private List<CachedPDB> pdbEntries;
1267
1268     public PDBEntryTableModel(List<CachedPDB> pdbEntries)
1269     {
1270       this.pdbEntries = new ArrayList<>(pdbEntries);
1271     }
1272
1273     @Override
1274     public String getColumnName(int columnIndex)
1275     {
1276       return columns[columnIndex];
1277     }
1278
1279     @Override
1280     public int getRowCount()
1281     {
1282       return pdbEntries.size();
1283     }
1284
1285     @Override
1286     public int getColumnCount()
1287     {
1288       return columns.length;
1289     }
1290
1291     @Override
1292     public boolean isCellEditable(int row, int column)
1293     {
1294       return false;
1295     }
1296
1297     @Override
1298     public Object getValueAt(int rowIndex, int columnIndex)
1299     {
1300       Object value = "??";
1301       CachedPDB entry = pdbEntries.get(rowIndex);
1302       switch (columnIndex)
1303       {
1304       case 0:
1305         value = entry.getSequence();
1306         break;
1307       case 1:
1308         value = entry.getPdbEntry();
1309         break;
1310       case 2:
1311         value = entry.getPdbEntry().getChainCode() == null ? "_"
1312                 : entry.getPdbEntry().getChainCode();
1313         break;
1314       case 3:
1315         value = entry.getPdbEntry().getType();
1316         break;
1317       case 4:
1318         value = entry.getPdbEntry().getFile();
1319         break;
1320       }
1321       return value;
1322     }
1323
1324     @Override
1325     public Class<?> getColumnClass(int columnIndex)
1326     {
1327       return columnIndex == 0 ? SequenceI.class : PDBEntry.class;
1328     }
1329
1330     public CachedPDB getPDBEntryAt(int row)
1331     {
1332       return pdbEntries.get(row);
1333     }
1334
1335   }
1336
1337   private class CachedPDB
1338   {
1339     private SequenceI sequence;
1340
1341     private PDBEntry pdbEntry;
1342
1343     public CachedPDB(SequenceI sequence, PDBEntry pdbEntry)
1344     {
1345       this.sequence = sequence;
1346       this.pdbEntry = pdbEntry;
1347     }
1348
1349     public SequenceI getSequence()
1350     {
1351       return sequence;
1352     }
1353
1354     public PDBEntry getPdbEntry()
1355     {
1356       return pdbEntry;
1357     }
1358
1359   }
1360
1361   private IProgressIndicator progressBar;
1362
1363   @Override
1364   public void setProgressBar(String message, long id)
1365   {
1366     progressBar.setProgressBar(message, id);
1367   }
1368
1369   @Override
1370   public void registerHandler(long id, IProgressIndicatorHandler handler)
1371   {
1372     progressBar.registerHandler(id, handler);
1373   }
1374
1375   @Override
1376   public boolean operationInProgress()
1377   {
1378     return progressBar.operationInProgress();
1379   }
1380
1381   public JalviewStructureDisplayI getOpenedStructureViewer()
1382   {
1383     return sViewer == null ? null : sViewer.sview;
1384   }
1385 }