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;
53 * decide if new structures are aligned to existing ones
55 private boolean superposeAdded = true;
57 public enum ViewerType
65 * @param structureSelectionManager
67 public StructureViewer(StructureSelectionManager structureSelectionManager)
69 ssm = structureSelectionManager;
73 * Factory to create a proxy for modifying existing structure viewer
76 public static StructureViewer reconfigure(
77 JalviewStructureDisplayI display)
79 StructureViewer sv = new StructureViewer(display.getBinding().getSsm());
85 public String toString()
89 return sview.toString();
93 public ViewerType getViewerType()
95 String viewType = Cache.getDefault(Preferences.STRUCTURE_DISPLAY,
96 ViewerType.JMOL.name());
97 return ViewerType.valueOf(viewType);
100 public void setViewerType(ViewerType type)
102 Cache.setProperty(Preferences.STRUCTURE_DISPLAY, type.name());
106 * View multiple PDB entries, each with associated sequences
113 public JalviewStructureDisplayI viewStructures(PDBEntry[] pdbs,
114 SequenceI[] seqs, AlignmentPanel ap)
116 JalviewStructureDisplayI viewer = onlyOnePdb(pdbs, seqs, ap);
120 * user added structure to an existing viewer - all done
125 ViewerType viewerType = getViewerType();
127 Map<PDBEntry, SequenceI[]> seqsForPdbs = getSequencesForPdbs(pdbs,
129 PDBEntry[] pdbsForFile = seqsForPdbs.keySet().toArray(
130 new PDBEntry[seqsForPdbs.size()]);
131 SequenceI[][] theSeqs = seqsForPdbs.values().toArray(
132 new SequenceI[seqsForPdbs.size()][]);
135 sview.setAlignAddedStructures(superposeAdded);
136 new Thread(new Runnable()
142 for (int pdbep = 0; pdbep < pdbsForFile.length; pdbep++)
144 PDBEntry pdb = pdbsForFile[pdbep];
145 if (!sview.addAlreadyLoadedFile(theSeqs[pdbep], null, ap,
148 sview.addToExistingViewer(pdb, theSeqs[pdbep], null, ap,
153 sview.updateTitleAndMenus();
159 if (viewerType.equals(ViewerType.JMOL))
161 sview = new AppJmol(ap, superposeAdded, pdbsForFile, theSeqs);
163 else if (viewerType.equals(ViewerType.CHIMERA))
165 sview = new ChimeraViewFrame(pdbsForFile, superposeAdded, theSeqs,
170 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
176 * Converts the list of selected PDB entries (possibly including duplicates
177 * for multiple chains), and corresponding sequences, into a map of sequences
178 * for each distinct PDB file. Returns null if either argument is null, or
179 * their lengths do not match.
185 Map<PDBEntry, SequenceI[]> getSequencesForPdbs(PDBEntry[] pdbs,
188 if (pdbs == null || seqs == null || pdbs.length != seqs.length)
194 * we want only one 'representative' PDBEntry per distinct file name
195 * (there may be entries for distinct chains)
197 Map<String, PDBEntry> pdbsSeen = new HashMap<>();
200 * LinkedHashMap preserves order of PDB entries (significant if they
201 * will get superimposed to the first structure)
203 Map<PDBEntry, List<SequenceI>> pdbSeqs = new LinkedHashMap<>();
204 for (int i = 0; i < pdbs.length; i++)
206 PDBEntry pdb = pdbs[i];
207 SequenceI seq = seqs[i];
208 String pdbFile = pdb.getFile();
209 if (pdbFile == null || pdbFile.length() == 0)
211 pdbFile = pdb.getId();
213 if (!pdbsSeen.containsKey(pdbFile))
215 pdbsSeen.put(pdbFile, pdb);
216 pdbSeqs.put(pdb, new ArrayList<SequenceI>());
220 pdb = pdbsSeen.get(pdbFile);
222 List<SequenceI> seqsForPdb = pdbSeqs.get(pdb);
223 if (!seqsForPdb.contains(seq))
230 * convert to Map<PDBEntry, SequenceI[]>
232 Map<PDBEntry, SequenceI[]> result = new LinkedHashMap<>();
233 for (Entry<PDBEntry, List<SequenceI>> entry : pdbSeqs.entrySet())
235 List<SequenceI> theSeqs = entry.getValue();
236 result.put(entry.getKey(),
237 theSeqs.toArray(new SequenceI[theSeqs.size()]));
244 * A strictly temporary method pending JAL-1761 refactoring. Determines if all
245 * the passed PDB entries are the same (this is the case if selected sequences
246 * to view structure for are chains of the same structure). If so, calls the
247 * single-pdb version of viewStructures and returns the viewer, else returns
255 private JalviewStructureDisplayI onlyOnePdb(PDBEntry[] pdbs,
256 SequenceI[] seqsForPdbs, AlignmentPanel ap)
258 List<SequenceI> seqs = new ArrayList<>();
259 if (pdbs == null || pdbs.length == 0)
264 String firstFile = pdbs[0].getFile();
265 for (PDBEntry pdb : pdbs)
267 String pdbFile = pdb.getFile();
268 if (pdbFile == null || !pdbFile.equals(firstFile))
272 SequenceI pdbseq = seqsForPdbs[i++];
278 return viewStructures(pdbs[0], seqs.toArray(new SequenceI[seqs.size()]),
282 JalviewStructureDisplayI sview = null;
284 public JalviewStructureDisplayI viewStructures(PDBEntry pdb,
285 SequenceI[] seqsForPdb, AlignmentPanel ap)
289 sview.setAlignAddedStructures(superposeAdded);
290 String pdbId = pdb.getId();
291 if (!sview.addAlreadyLoadedFile(seqsForPdb, null, ap, pdbId))
293 sview.addToExistingViewer(pdb, seqsForPdb, null, ap, pdbId);
295 sview.updateTitleAndMenus();
299 ViewerType viewerType = getViewerType();
300 if (viewerType.equals(ViewerType.JMOL))
302 sview = new AppJmol(pdb, seqsForPdb, null, ap);
304 else if (viewerType.equals(ViewerType.CHIMERA))
306 sview = new ChimeraViewFrame(pdb, seqsForPdb, null, ap);
310 Cache.log.error(UNKNOWN_VIEWER_TYPE + getViewerType().toString());
316 * Create a new panel controlling a structure viewer.
329 public JalviewStructureDisplayI createView(ViewerType type, String[] pdbf,
330 String[] id, SequenceI[][] sq, AlignmentPanel alignPanel,
331 StructureViewerModel viewerData, String fileloc, Rectangle rect,
334 final boolean useinViewerSuperpos = viewerData.isAlignWithPanel();
335 final boolean usetoColourbyseq = viewerData.isColourWithAlignPanel();
336 final boolean viewerColouring = viewerData.isColourByViewer();
341 sview = new AppJmol(pdbf, id, sq, alignPanel, usetoColourbyseq,
342 useinViewerSuperpos, viewerColouring, fileloc, rect, vid);
346 "Unsupported structure viewer type " + type.toString());
349 Cache.log.error(UNKNOWN_VIEWER_TYPE + type.toString());
354 public boolean isBusy()
358 if (!sview.hasMapping())
369 * @return true if view is already showing PDBid
371 public boolean hasPdbId(String pDBid)
378 return sview.getBinding().hasPdbId(pDBid);
381 public boolean isVisible()
383 return sview != null && sview.isVisible();
386 public void setSuperpose(boolean alignAddedStructures)
388 superposeAdded = alignAddedStructures;