2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.api.structures.JalviewStructureDisplayI;
24 import jalview.bin.Cache;
25 import jalview.datamodel.PDBEntry;
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.StructureViewerModel;
28 import jalview.structure.StructureSelectionManager;
30 import java.awt.Rectangle;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
39 * A proxy for handling structure viewers, that orchestrates adding selected
40 * structures, associated with sequences in Jalview, to an existing viewer, or
41 * opening a new one. Currently supports either Jmol or Chimera as the structure
46 public class StructureViewer
48 private static final String UNKNOWN_VIEWER_TYPE = "Unknown structure viewer type ";
50 StructureSelectionManager ssm;
52 public enum ViewerType
60 * @param structureSelectionManager
62 public StructureViewer(StructureSelectionManager structureSelectionManager)
64 ssm = structureSelectionManager;
68 * Factory to create a proxy for modifying existing structure viewer
71 public static StructureViewer reconfigure(
72 JalviewStructureDisplayI display)
74 StructureViewer sv = new StructureViewer(display.getBinding().getSsm());
78 public ViewerType getViewerType()
80 String viewType = Cache.getDefault(Preferences.STRUCTURE_DISPLAY,
81 ViewerType.JMOL.name());
82 return ViewerType.valueOf(viewType);
85 public void setViewerType(ViewerType type)
87 Cache.setProperty(Preferences.STRUCTURE_DISPLAY, type.name());
91 * View multiple PDB entries, each with associated sequences
98 public JalviewStructureDisplayI viewStructures(PDBEntry[] pdbs,
99 SequenceI[] seqs, AlignmentPanel ap)
101 JalviewStructureDisplayI viewer = onlyOnePdb(pdbs, seqs, ap);
105 * user added structure to an existing viewer - all done
110 ViewerType viewerType = getViewerType();
112 Map<PDBEntry, SequenceI[]> seqsForPdbs = getSequencesForPdbs(pdbs,
114 PDBEntry[] pdbsForFile = seqsForPdbs.keySet().toArray(
115 new PDBEntry[seqsForPdbs.size()]);
116 SequenceI[][] theSeqs = seqsForPdbs.values().toArray(
117 new SequenceI[seqsForPdbs.size()][]);
120 new Thread(new Runnable()
126 for (int pdbep = 0; pdbep < pdbsForFile.length; pdbep++)
128 PDBEntry pdb = pdbsForFile[pdbep];
129 if (!sview.addAlreadyLoadedFile(theSeqs[pdbep], null, ap,
132 sview.addToExistingViewer(pdb, theSeqs[pdbep], null, ap,
137 sview.updateTitleAndMenus();
143 if (viewerType.equals(ViewerType.JMOL))
145 sview = new AppJmol(ap, pdbsForFile, theSeqs);
147 else if (viewerType.equals(ViewerType.CHIMERA))
149 sview = new ChimeraViewFrame(pdbsForFile, theSeqs, ap);
153 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
159 * Converts the list of selected PDB entries (possibly including duplicates
160 * for multiple chains), and corresponding sequences, into a map of sequences
161 * for each distinct PDB file. Returns null if either argument is null, or
162 * their lengths do not match.
168 Map<PDBEntry, SequenceI[]> getSequencesForPdbs(PDBEntry[] pdbs,
171 if (pdbs == null || seqs == null || pdbs.length != seqs.length)
177 * we want only one 'representative' PDBEntry per distinct file name
178 * (there may be entries for distinct chains)
180 Map<String, PDBEntry> pdbsSeen = new HashMap<>();
183 * LinkedHashMap preserves order of PDB entries (significant if they
184 * will get superimposed to the first structure)
186 Map<PDBEntry, List<SequenceI>> pdbSeqs = new LinkedHashMap<>();
187 for (int i = 0; i < pdbs.length; i++)
189 PDBEntry pdb = pdbs[i];
190 SequenceI seq = seqs[i];
191 String pdbFile = pdb.getFile();
192 if (pdbFile == null || pdbFile.length() == 0)
194 pdbFile = pdb.getId();
196 if (!pdbsSeen.containsKey(pdbFile))
198 pdbsSeen.put(pdbFile, pdb);
199 pdbSeqs.put(pdb, new ArrayList<SequenceI>());
203 pdb = pdbsSeen.get(pdbFile);
205 List<SequenceI> seqsForPdb = pdbSeqs.get(pdb);
206 if (!seqsForPdb.contains(seq))
213 * convert to Map<PDBEntry, SequenceI[]>
215 Map<PDBEntry, SequenceI[]> result = new LinkedHashMap<>();
216 for (Entry<PDBEntry, List<SequenceI>> entry : pdbSeqs.entrySet())
218 List<SequenceI> theSeqs = entry.getValue();
219 result.put(entry.getKey(),
220 theSeqs.toArray(new SequenceI[theSeqs.size()]));
227 * A strictly temporary method pending JAL-1761 refactoring. Determines if all
228 * the passed PDB entries are the same (this is the case if selected sequences
229 * to view structure for are chains of the same structure). If so, calls the
230 * single-pdb version of viewStructures and returns the viewer, else returns
238 private JalviewStructureDisplayI onlyOnePdb(PDBEntry[] pdbs,
239 SequenceI[] seqsForPdbs, AlignmentPanel ap)
241 List<SequenceI> seqs = new ArrayList<>();
242 if (pdbs == null || pdbs.length == 0)
247 String firstFile = pdbs[0].getFile();
248 for (PDBEntry pdb : pdbs)
250 String pdbFile = pdb.getFile();
251 if (pdbFile == null || !pdbFile.equals(firstFile))
255 SequenceI pdbseq = seqsForPdbs[i++];
261 return viewStructures(pdbs[0], seqs.toArray(new SequenceI[seqs.size()]),
265 JalviewStructureDisplayI sview = null;
267 public JalviewStructureDisplayI viewStructures(PDBEntry pdb,
268 SequenceI[] seqsForPdb, AlignmentPanel ap)
272 if (!sview.addAlreadyLoadedFile(seqsForPdb, null, ap, pdb.getId()))
274 sview.addToExistingViewer(pdb, seqsForPdb, null, ap, pdb.getId());
276 sview.updateTitleAndMenus();
279 ViewerType viewerType = getViewerType();
280 if (viewerType.equals(ViewerType.JMOL))
282 sview = new AppJmol(pdb, seqsForPdb, null, ap);
284 else if (viewerType.equals(ViewerType.CHIMERA))
286 sview = new ChimeraViewFrame(pdb, seqsForPdb, null, ap);
290 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
296 * Create a new panel controlling a structure viewer.
309 public JalviewStructureDisplayI createView(ViewerType type, String[] pdbf,
310 String[] id, SequenceI[][] sq, AlignmentPanel alignPanel,
311 StructureViewerModel viewerData, String fileloc, Rectangle rect,
314 final boolean useinViewerSuperpos = viewerData.isAlignWithPanel();
315 final boolean usetoColourbyseq = viewerData.isColourWithAlignPanel();
316 final boolean viewerColouring = viewerData.isColourByViewer();
321 sview = new AppJmol(pdbf, id, sq, alignPanel, usetoColourbyseq,
322 useinViewerSuperpos, viewerColouring, fileloc, rect, vid);
326 "Unsupported structure viewer type " + type.toString());
329 Cache.log.error(UNKNOWN_VIEWER_TYPE + type.toString());
334 public boolean isBusy()
338 if (!sview.hasMapping())