From 6a09579124d734cd2e136f4e32926464ec3c2477 Mon Sep 17 00:00:00 2001 From: BobHanson Date: Mon, 1 Jun 2020 17:18:07 -0500 Subject: [PATCH] JAL-3626 from JAL-3253-applet StructureViewer/Chooser -- disassociates viewer launch from chooser -- for JavaScript interface (sorry about JAL-3262 ref previously) --- src/jalview/gui/StructureChooser.java | 237 ++++++++++----------------------- src/jalview/gui/StructureViewer.java | 117 ++++++++++++++++ 2 files changed, 189 insertions(+), 165 deletions(-) diff --git a/src/jalview/gui/StructureChooser.java b/src/jalview/gui/StructureChooser.java index 33d8c33..4a8a7d8 100644 --- a/src/jalview/gui/StructureChooser.java +++ b/src/jalview/gui/StructureChooser.java @@ -36,11 +36,8 @@ import jalview.fts.core.FTSRestResponse; import jalview.fts.service.pdb.PDBFTSRestClient; import jalview.io.DataSourceType; import jalview.jbgui.GStructureChooser; -import jalview.structure.StructureMapping; import jalview.structure.StructureSelectionManager; import jalview.util.MessageManager; -import jalview.ws.DBRefFetcher; -import jalview.ws.sifts.SiftsSettings; import java.awt.event.ItemEvent; import java.util.ArrayList; @@ -69,7 +66,7 @@ import javax.swing.table.AbstractTableModel; public class StructureChooser extends GStructureChooser implements IProgressIndicator { - private static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE"; + static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE"; private static int MAX_QLENGTH = 7820; @@ -91,7 +88,7 @@ public class StructureChooser extends GStructureChooser private boolean cachedPDBExists; - private static StructureViewer lastTargetedView = null; + static StructureViewer lastTargetedView = null; public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq, AlignmentPanel ap) @@ -165,8 +162,7 @@ public class StructureChooser extends GStructureChooser if (view.isLinkedWith(ap)) { - targetView.insertItemAt(viewHandler, - linkedViewsAt++); + targetView.insertItemAt(viewHandler, linkedViewsAt++); } else { @@ -322,7 +318,7 @@ public class StructureChooser extends GStructureChooser boolean isUniProtRefsFound = false; StringBuilder queryBuilder = new StringBuilder(); Set seqRefs = new LinkedHashSet<>(); - + /* * note PDBs as DBRefEntry so they are not duplicated in query */ @@ -348,7 +344,7 @@ public class StructureChooser extends GStructureChooser { for (int ib = 0, nb = refs.size(); ib < nb; ib++) { - DBRefEntry dbRef = refs.get(ib); + DBRefEntry dbRef = refs.get(ib); if (isValidSeqName(getDBRefId(dbRef)) && queryBuilder.length() < MAX_QLENGTH) { @@ -577,7 +573,8 @@ public class StructureChooser extends GStructureChooser @Override protected void pdbFromFile_actionPerformed() { - // TODO: JAL-3048 not needed for Jalview-JS until JSmol dep and StructureChooser + // TODO: JAL-3048 not needed for Jalview-JS until JSmol dep and + // StructureChooser // works jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( jalview.bin.Cache.getProperty("LAST_DIRECTORY")); @@ -639,8 +636,8 @@ public class StructureChooser extends GStructureChooser if (cachedPDBExist) { FilterOption cachedOption = new FilterOption( - MessageManager.getString("label.cached_structures"), - "-", VIEWS_LOCAL_PDB, false); + MessageManager.getString("label.cached_structures"), "-", + VIEWS_LOCAL_PDB, false); cmb_filterOption.addItem(cachedOption); cmb_filterOption.setSelectedItem(cachedOption); } @@ -861,7 +858,7 @@ public class StructureChooser extends GStructureChooser } return found; } - + /** * Handles the 'New View' action */ @@ -893,6 +890,9 @@ public class StructureChooser extends GStructureChooser final int preferredHeight = pnl_filter.getHeight(); + final StructureViewer theViewer = getTargetedStructureViewer(ssm); + boolean superimpose = chk_superpose.isSelected(); + Runnable viewStruc = new Runnable() { @Override @@ -906,8 +906,7 @@ public class StructureChooser extends GStructureChooser if (currentView == VIEWS_FILTER) { - int pdbIdColIndex = restable.getColumn("PDB Id") - .getModelIndex(); + int pdbIdColIndex = restable.getColumn("PDB Id").getModelIndex(); int refSeqColIndex = restable.getColumn("Ref Sequence") .getModelIndex(); int[] selectedRows = restable.getSelectedRows(); @@ -916,10 +915,10 @@ public class StructureChooser extends GStructureChooser List selectedSeqsToView = new ArrayList<>(); for (int row : selectedRows) { - String pdbIdStr = restable - .getValueAt(row, pdbIdColIndex).toString(); - SequenceI selectedSeq = (SequenceI) restable - .getValueAt(row, refSeqColIndex); + String pdbIdStr = restable.getValueAt(row, pdbIdColIndex) + .toString(); + SequenceI selectedSeq = (SequenceI) restable.getValueAt(row, + refSeqColIndex); selectedSeqsToView.add(selectedSeq); PDBEntry pdbEntry = selectedSeq.getPDBEntry(pdbIdStr); if (pdbEntry == null) @@ -939,8 +938,8 @@ public class StructureChooser extends GStructureChooser } SequenceI[] selectedSeqs = selectedSeqsToView .toArray(new SequenceI[selectedSeqsToView.size()]); - sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap, - selectedSeqs); + sViewer = StructureViewer.launchStructureViewer(ap, pdbEntriesToView, + selectedSeqs, superimpose, theViewer, progressBar); } else if (currentView == VIEWS_LOCAL_PDB) { @@ -963,8 +962,8 @@ public class StructureChooser extends GStructureChooser } SequenceI[] selectedSeqs = selectedSeqsToView .toArray(new SequenceI[selectedSeqsToView.size()]); - sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap, - selectedSeqs); + sViewer = StructureViewer.launchStructureViewer(ap, pdbEntriesToView, + selectedSeqs, superimpose, theViewer, progressBar); } else if (currentView == VIEWS_ENTER_ID) { @@ -993,9 +992,10 @@ public class StructureChooser extends GStructureChooser } PDBEntry[] pdbEntriesToView = new PDBEntry[] { pdbEntry }; - sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap, + sViewer = StructureViewer.launchStructureViewer(ap, pdbEntriesToView, new SequenceI[] - { selectedSequence }); + { selectedSequence }, superimpose, theViewer, + progressBar); } else if (currentView == VIEWS_FROM_FILE) { @@ -1009,12 +1009,10 @@ public class StructureChooser extends GStructureChooser .associatePdbWithSeq(selectedPdbFileName, DataSourceType.FILE, selectedSequence, true, Desktop.instance); - - sViewer = launchStructureViewer( - ssm, new PDBEntry[] - { fileEntry }, ap, + sViewer = StructureViewer.launchStructureViewer(ap, new PDBEntry[] { fileEntry }, new SequenceI[] - { selectedSequence }); + { selectedSequence }, superimpose, theViewer, + progressBar); } SwingUtilities.invokeLater(new Runnable() { @@ -1068,8 +1066,7 @@ public class StructureChooser extends GStructureChooser * @param ssm * @return */ - StructureViewer getTargetedStructureViewer( - StructureSelectionManager ssm) + StructureViewer getTargetedStructureViewer(StructureSelectionManager ssm) { Object sv = targetView.getSelectedItem(); @@ -1077,98 +1074,6 @@ public class StructureChooser extends GStructureChooser } /** - * Adds PDB structures to a new or existing structure viewer - * - * @param ssm - * @param pdbEntriesToView - * @param alignPanel - * @param sequences - * @return - */ - private StructureViewer launchStructureViewer( - StructureSelectionManager ssm, - final PDBEntry[] pdbEntriesToView, - final AlignmentPanel alignPanel, SequenceI[] sequences) - { - long progressId = sequences.hashCode(); - setProgressBar(MessageManager - .getString("status.launching_3d_structure_viewer"), progressId); - final StructureViewer theViewer = getTargetedStructureViewer(ssm); - boolean superimpose = chk_superpose.isSelected(); - theViewer.setSuperpose(superimpose); - - /* - * remember user's choice of superimpose or not - */ - Cache.setProperty(AUTOSUPERIMPOSE, - Boolean.valueOf(superimpose).toString()); - - setProgressBar(null, progressId); - if (SiftsSettings.isMapWithSifts()) - { - List seqsWithoutSourceDBRef = new ArrayList<>(); - int p = 0; - // TODO: skip PDBEntry:Sequence pairs where PDBEntry doesn't look like a - // real PDB ID. For moment, we can also safely do this if there is already - // a known mapping between the PDBEntry and the sequence. - for (SequenceI seq : sequences) - { - PDBEntry pdbe = pdbEntriesToView[p++]; - if (pdbe != null && pdbe.getFile() != null) - { - StructureMapping[] smm = ssm.getMapping(pdbe.getFile()); - if (smm != null && smm.length > 0) - { - for (StructureMapping sm : smm) - { - if (sm.getSequence() == seq) - { - continue; - } - } - } - } - if (seq.getPrimaryDBRefs().isEmpty()) - { - seqsWithoutSourceDBRef.add(seq); - continue; - } - } - if (!seqsWithoutSourceDBRef.isEmpty()) - { - int y = seqsWithoutSourceDBRef.size(); - setProgressBar(MessageManager.formatMessage( - "status.fetching_dbrefs_for_sequences_without_valid_refs", - y), progressId); - SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef - .toArray(new SequenceI[y]); - DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef); - dbRefFetcher.fetchDBRefs(true); - - setProgressBar("Fetch complete.", progressId); // todo i18n - } - } - if (pdbEntriesToView.length > 1) - { - setProgressBar(MessageManager.getString( - "status.fetching_3d_structures_for_selected_entries"), - progressId); - theViewer.viewStructures(pdbEntriesToView, sequences, alignPanel); - } - else - { - setProgressBar(MessageManager.formatMessage( - "status.fetching_3d_structures_for", - pdbEntriesToView[0].getId()),progressId); - theViewer.viewStructures(pdbEntriesToView[0], sequences, alignPanel); - } - setProgressBar(null, progressId); - // remember the last viewer we used... - lastTargetedView = theViewer; - return theViewer; - } - - /** * Populates the combo-box used in associating manually fetched structures to * a unique sequence when more than one sequence selection is made. */ @@ -1204,7 +1109,8 @@ public class StructureChooser extends GStructureChooser && !discoveredStructuresSet.isEmpty(); } - protected int PDB_ID_MIN = 3;// or: (Jalview.isJS() ? 3 : 1); // Bob proposes this. + protected int PDB_ID_MIN = 3;// or: (Jalview.isJS() ? 3 : 1); // Bob proposes + // this. // Doing a search for "1" or "1c" is valuable? // Those work but are enormously slow. @@ -1212,51 +1118,51 @@ public class StructureChooser extends GStructureChooser protected void txt_search_ActionPerformed() { String text = txt_search.getText().trim(); - if (text.length() >= PDB_ID_MIN) - new Thread() - { - - @Override - public void run() + if (text.length() >= PDB_ID_MIN) + new Thread() { - errorWarning.setLength(0); - isValidPBDEntry = false; - if (text.length() > 0) + + @Override + public void run() { - String searchTerm = text.toLowerCase(); - searchTerm = searchTerm.split(":")[0]; - // System.out.println(">>>>> search term : " + searchTerm); - List wantedFields = new ArrayList<>(); - FTSRestRequest pdbRequest = new FTSRestRequest(); - pdbRequest.setAllowEmptySeq(false); - pdbRequest.setResponseSize(1); - pdbRequest.setFieldToSearchBy("(pdb_id:"); - pdbRequest.setWantedFields(wantedFields); - pdbRequest.setSearchTerm(searchTerm + ")"); - pdbRequest.setAssociatedSequence(selectedSequence); - pdbRestClient = PDBFTSRestClient.getInstance(); - wantedFields.add(pdbRestClient.getPrimaryKeyColumn()); - FTSRestResponse resultList; - try - { - resultList = pdbRestClient.executeRequest(pdbRequest); - } catch (Exception e) - { - errorWarning.append(e.getMessage()); - return; - } finally - { - validateSelections(); - } - if (resultList.getSearchSummary() != null - && resultList.getSearchSummary().size() > 0) + errorWarning.setLength(0); + isValidPBDEntry = false; + if (text.length() > 0) { - isValidPBDEntry = true; + String searchTerm = text.toLowerCase(); + searchTerm = searchTerm.split(":")[0]; + // System.out.println(">>>>> search term : " + searchTerm); + List wantedFields = new ArrayList<>(); + FTSRestRequest pdbRequest = new FTSRestRequest(); + pdbRequest.setAllowEmptySeq(false); + pdbRequest.setResponseSize(1); + pdbRequest.setFieldToSearchBy("(pdb_id:"); + pdbRequest.setWantedFields(wantedFields); + pdbRequest.setSearchTerm(searchTerm + ")"); + pdbRequest.setAssociatedSequence(selectedSequence); + pdbRestClient = PDBFTSRestClient.getInstance(); + wantedFields.add(pdbRestClient.getPrimaryKeyColumn()); + FTSRestResponse resultList; + try + { + resultList = pdbRestClient.executeRequest(pdbRequest); + } catch (Exception e) + { + errorWarning.append(e.getMessage()); + return; + } finally + { + validateSelections(); + } + if (resultList.getSearchSummary() != null + && resultList.getSearchSummary().size() > 0) + { + isValidPBDEntry = true; + } } + validateSelections(); } - validateSelections(); - } - }.start(); + }.start(); } @Override @@ -1403,4 +1309,5 @@ public class StructureChooser extends GStructureChooser { return sViewer == null ? null : sViewer.sview; } + } diff --git a/src/jalview/gui/StructureViewer.java b/src/jalview/gui/StructureViewer.java index c62f72f..e0c33e5 100644 --- a/src/jalview/gui/StructureViewer.java +++ b/src/jalview/gui/StructureViewer.java @@ -25,8 +25,12 @@ import jalview.bin.Cache; import jalview.datamodel.PDBEntry; import jalview.datamodel.SequenceI; import jalview.datamodel.StructureViewerModel; +import jalview.structure.StructureMapping; import jalview.structure.StructureSelectionManager; +import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.ws.DBRefFetcher; +import jalview.ws.sifts.SiftsSettings; import java.awt.Rectangle; import java.util.ArrayList; @@ -399,4 +403,117 @@ public class StructureViewer superposeAdded = alignAddedStructures; } + /** + * Launch a minimal implementation of a StructureViewer. + * + * @param alignPanel + * @param pdb + * @param seqs + * @return + */ + public static StructureViewer launchStructureViewer( + AlignmentPanel alignPanel, PDBEntry pdb, SequenceI[] seqs) + { + return launchStructureViewer(alignPanel, new PDBEntry[] { pdb }, seqs, + false, null, null); + } + + /** + * Adds PDB structures to a new or existing structure viewer + * + * @param ssm + * @param pdbEntriesToView + * @param alignPanel + * @param sequences + * @return + */ + protected static StructureViewer launchStructureViewer( + final AlignmentPanel ap, final PDBEntry[] pdbEntriesToView, + SequenceI[] sequences, boolean superimpose, + StructureViewer theViewer, IProgressIndicator pb) + { + final StructureSelectionManager ssm = ap.getStructureSelectionManager(); + long progressId = sequences.hashCode(); + if (pb != null) + pb.setProgressBar(MessageManager.getString( + "status.launching_3d_structure_viewer"), progressId); + theViewer.setSuperpose(superimpose); + + /* + * remember user's choice of superimpose or not + */ + Cache.setProperty(StructureChooser.AUTOSUPERIMPOSE, + Boolean.valueOf(superimpose).toString()); + + if (pb != null) + pb.setProgressBar(null, progressId); + if (SiftsSettings.isMapWithSifts()) + { + List seqsWithoutSourceDBRef = new ArrayList<>(); + int p = 0; + // TODO: skip PDBEntry:Sequence pairs where PDBEntry doesn't look like a + // real PDB ID. For moment, we can also safely do this if there is already + // a known mapping between the PDBEntry and the sequence. + for (SequenceI seq : sequences) + { + PDBEntry pdbe = pdbEntriesToView[p++]; + if (pdbe != null && pdbe.getFile() != null) + { + StructureMapping[] smm = ssm.getMapping(pdbe.getFile()); + if (smm != null && smm.length > 0) + { + for (StructureMapping sm : smm) + { + if (sm.getSequence() == seq) + { + continue; + } + } + } + } + if (seq.getPrimaryDBRefs().isEmpty()) + { + seqsWithoutSourceDBRef.add(seq); + continue; + } + } + if (!seqsWithoutSourceDBRef.isEmpty()) + { + int y = seqsWithoutSourceDBRef.size(); + if (pb != null) + pb.setProgressBar(MessageManager.formatMessage( + "status.fetching_dbrefs_for_sequences_without_valid_refs", + y), progressId); + SequenceI[] seqWithoutSrcDBRef = seqsWithoutSourceDBRef + .toArray(new SequenceI[y]); + DBRefFetcher dbRefFetcher = new DBRefFetcher(seqWithoutSrcDBRef); + dbRefFetcher.fetchDBRefs(true); + + if (pb != null) + pb.setProgressBar("Fetch complete.", progressId); // todo i18n + } + } + if (pdbEntriesToView.length > 1) + { + if (pb != null) + pb.setProgressBar(MessageManager.getString( + "status.fetching_3d_structures_for_selected_entries"), + progressId); + theViewer.viewStructures(pdbEntriesToView, sequences, ap); + } + else + { + if (pb != null) + pb.setProgressBar(MessageManager.formatMessage( + "status.fetching_3d_structures_for", + pdbEntriesToView[0].getId()), progressId); + theViewer.viewStructures(pdbEntriesToView[0], sequences, ap); + } + if (pb != null) + pb.setProgressBar(null, progressId); + // remember the last viewer we used... + StructureChooser.lastTargetedView = theViewer; + return theViewer; + } + } -- 1.7.10.2