+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ // //LOAD STRUCTURES
+ if (loadTreesAndStructures)
+ {
+ loadStructures(jprovider, jseqs, af, ap);
+ }
+ // and finally return.
+ return af;
+ }
+
+ /**
+ * Load and link any saved structure viewers.
+ *
+ * @param jprovider
+ * @param jseqs
+ * @param af
+ * @param ap
+ */
+ protected void loadStructures(jarInputStreamProvider jprovider,
+ JSeq[] jseqs, AlignFrame af, AlignmentPanel ap)
+ {
+ // run through all PDB ids on the alignment, and collect mappings between
+ // jmol view ids and all sequences referring to it
+ Map<String, ViewerData> jmolViewIds = new HashMap<String, ViewerData>();
+
+ for (int i = 0; i < jseqs.length; i++)
+ {
+ if (jseqs[i].getPdbidsCount() > 0)
+ {
+ Pdbids[] ids = jseqs[i].getPdbids();
+ for (int p = 0; p < ids.length; p++)
+ {
+ final int structureStateCount = ids[p].getStructureStateCount();
+ for (int s = 0; s < structureStateCount; s++)
+ {
+ // check to see if we haven't already created this structure view
+ final StructureState structureState = ids[p].getStructureState(s);
+ String sviewid = (structureState.getViewId() == null) ? null
+ : structureState.getViewId()
+ + uniqueSetSuffix;
+ jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
+ // Originally : ids[p].getFile()
+ // : TODO: verify external PDB file recovery still works in normal
+ // jalview project load
+ jpdb.setFile(loadPDBFile(jprovider, ids[p].getId()));
+ jpdb.setId(ids[p].getId());
+
+ int x = structureState.getXpos();
+ int y = structureState.getYpos();
+ int width = structureState.getWidth();
+ int height = structureState.getHeight();
+
+ // Probably don't need to do this anymore...
+ // Desktop.desktop.getComponentAt(x, y);
+ // TODO: NOW: check that this recovers the PDB file correctly.
+ String pdbFile = loadPDBFile(jprovider, ids[p].getId());
+ jalview.datamodel.SequenceI seq = seqRefIds
+ .get(jseqs[i].getId() + "");
+ if (sviewid == null)
+ {
+ sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
+ + "," + height;
+ }
+ if (!jmolViewIds.containsKey(sviewid))
+ {
+ jmolViewIds.put(sviewid, new ViewerData(x, y, width, height,
+ false, false, true));
+ // Legacy pre-2.7 conversion JAL-823 :
+ // do not assume any view has to be linked for colour by
+ // sequence
+ }
+
+ // assemble String[] { pdb files }, String[] { id for each
+ // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
+ // seqs_file 2}, boolean[] {
+ // linkAlignPanel,superposeWithAlignpanel}} from hash
+ ViewerData jmoldat = jmolViewIds.get(sviewid);
+ jmoldat.alignWithPanel |= structureState
+ .hasAlignwithAlignPanel() ? structureState.getAlignwithAlignPanel() : false;
+ // never colour by linked panel if not specified
+ jmoldat.colourWithAlignPanel |= structureState
+ .hasColourwithAlignPanel() ? structureState.getColourwithAlignPanel()
+ : false;
+ // default for pre-2.7 projects is that Jmol colouring is enabled
+ jmoldat.colourByViewer &= structureState
+ .hasColourByJmol() ? structureState
+ .getColourByJmol() : true;
+
+ if (jmoldat.stateData.length() < structureState.getContent()
+ .length())
+ {
+ {
+ jmoldat.stateData = structureState.getContent();
+ }
+ }
+ if (ids[p].getFile() != null)
+ {
+ File mapkey = new File(ids[p].getFile());
+ Object[] seqstrmaps = jmoldat.fileData.get(mapkey);
+ if (seqstrmaps == null)
+ {
+ jmoldat.fileData.put(mapkey,
+ seqstrmaps = new Object[]
+ { pdbFile, ids[p].getId(), new Vector(),
+ new Vector() });
+ }
+ if (!((Vector) seqstrmaps[2]).contains(seq))
+ {
+ ((Vector) seqstrmaps[2]).addElement(seq);
+ // ((Vector)seqstrmaps[3]).addElement(n) :
+ // in principle, chains
+ // should be stored here : do we need to
+ // TODO: store and recover seq/pdb_id :
+ // chain mappings
+ }
+ }
+ else
+ {
+ errorMessage = ("The Jmol views in this project were imported\nfrom an older version of Jalview.\nPlease review the sequence colour associations\nin the Colour by section of the Jmol View menu.\n\nIn the case of problems, see note at\nhttp://issues.jalview.org/browse/JAL-747");
+ warn(errorMessage);
+ }
+ }
+ }
+ }
+ }
+ // Instantiate the associated structure views
+ for (Entry<String, ViewerData> entry : jmolViewIds.entrySet())
+ {
+ createOrLinkStructureViewer(entry, af, ap);
+ }
+ }
+
+ /**
+ *
+ * @param viewerData
+ * @param af
+ * @param ap
+ */
+ protected void createOrLinkStructureViewer(
+ Entry<String, ViewerData> viewerData, AlignFrame af,
+ AlignmentPanel ap)
+ {
+ final ViewerData svattrib = viewerData.getValue();
+
+ /*
+ * Search for any viewer windows already open from other alignment views
+ * that exactly match the stored structure state
+ */
+ StructureViewerBase comp = findMatchingViewer(viewerData);
+
+ if (comp != null)
+ {
+ linkStructureViewer(ap, comp, svattrib);
+ return;
+ }
+
+ /*
+ * Pending an XML element for ViewerType, just check if stateData contains
+ * "chimera" (part of the chimera session filename).
+ */
+ if (svattrib.stateData.indexOf("chimera") > -1)
+ {
+ createChimeraViewer(viewerData, af);
+ }
+ else
+ {
+ createJmolViewer(viewerData, af);
+ }
+ }
+
+ /**
+ * Create a new Chimera viewer.
+ *
+ * @param viewerData
+ * @param af
+ */
+ protected void createChimeraViewer(Entry<String, ViewerData> viewerData,
+ AlignFrame af)
+ {
+ final ViewerData svattrib = viewerData.getValue();
+ ChimeraViewFrame cvf = new ChimeraViewFrame(svattrib, af);
+ }
+
+ /**
+ * Create a new Jmol window. First parse the Jmol state to translate filenames
+ * loaded into the view, and record the order in which files are shown in the
+ * Jmol view, so we can add the sequence mappings in same order.
+ *
+ * @param viewerData
+ * @param af
+ */
+ protected void createJmolViewer(
+ final Entry<String, ViewerData> viewerData, AlignFrame af)
+ {
+ final ViewerData svattrib = viewerData.getValue();
+ String state = svattrib.stateData;
+ List<String> pdbfilenames = new ArrayList<String>();
+ List<SequenceI[]> seqmaps = new ArrayList<SequenceI[]>();
+ List<String> pdbids = new ArrayList<String>();
+ StringBuilder newFileLoc = new StringBuilder(64);
+ int cp = 0, ncp, ecp;
+ Map<File, Object[]> oldFiles = svattrib.fileData;
+ while ((ncp = state.indexOf("load ", cp)) > -1)
+ {
+ do
+ {
+ // look for next filename in load statement
+ newFileLoc.append(state.substring(cp,
+ ncp = (state.indexOf("\"", ncp + 1) + 1)));
+ String oldfilenam = state.substring(ncp,
+ ecp = state.indexOf("\"", ncp));
+ // recover the new mapping data for this old filename
+ // have to normalize filename - since Jmol and jalview do
+ // filename
+ // translation differently.
+ Object[] filedat = oldFiles.get(new File(oldfilenam));
+ newFileLoc.append(Platform.escapeString((String) filedat[0]));
+ pdbfilenames.add((String) filedat[0]);
+ pdbids.add((String) filedat[1]);
+ seqmaps.add(((Vector<SequenceI>) filedat[2])
+ .toArray(new SequenceI[0]));
+ newFileLoc.append("\"");
+ cp = ecp + 1; // advance beyond last \" and set cursor so we can
+ // look for next file statement.
+ } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
+ }
+ if (cp > 0)
+ {
+ // just append rest of state
+ newFileLoc.append(state.substring(cp));
+ }
+ else
+ {
+ System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
+ newFileLoc = new StringBuilder(state);
+ newFileLoc.append("; load append ");
+ for (File id : oldFiles.keySet())
+ {
+ // add this and any other pdb files that should be present in
+ // the viewer
+ Object[] filedat = oldFiles.get(id);
+ String nfilename;
+ newFileLoc.append(((String) filedat[0]));
+ pdbfilenames.add((String) filedat[0]);
+ pdbids.add((String) filedat[1]);
+ seqmaps.add(((Vector<SequenceI>) filedat[2])
+ .toArray(new SequenceI[0]));
+ newFileLoc.append(" \"");
+ newFileLoc.append((String) filedat[0]);
+ newFileLoc.append("\"");
+
+ }
+ newFileLoc.append(";");
+ }
+
+ if (newFileLoc.length() > 0)
+ {
+ int histbug = newFileLoc.indexOf("history = ");
+ histbug += 10;
+ int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
+ String val = (diff == -1) ? null : newFileLoc
+ .substring(histbug, diff);
+ if (val != null && val.length() >= 4)
+ {
+ if (val.contains("e"))
+ {
+ if (val.trim().equals("true"))
+ {
+ val = "1";
+ }
+ else
+ {
+ val = "0";
+ }
+ newFileLoc.replace(histbug, diff, val);
+ }
+ }
+
+ final String[] pdbf = pdbfilenames.toArray(new String[pdbfilenames
+ .size()]);
+ final String[] id = pdbids.toArray(new String[pdbids.size()]);
+ final SequenceI[][] sq = seqmaps
+ .toArray(new SequenceI[seqmaps.size()][]);
+ final String fileloc = newFileLoc.toString();
+ final String sviewid = viewerData.getKey();
+ final AlignFrame alf = af;
+ final java.awt.Rectangle rect = new java.awt.Rectangle(svattrib.x,
+ svattrib.y, svattrib.width, svattrib.height);
+ try
+ {
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ JalviewStructureDisplayI sview = null;
+ try
+ {
+ // JAL-1333 note - we probably can't migrate Jmol views to UCSF
+ // Chimera!
+ sview = new StructureViewer(alf.alignPanel
+ .getStructureSelectionManager()).createView(
+ StructureViewer.ViewerType.JMOL, pdbf, id, sq,
+ alf.alignPanel, svattrib, fileloc, rect, sviewid);
+ addNewStructureViewer(sview);
+ } catch (OutOfMemoryError ex)
+ {
+ new OOMWarning("restoring structure view for PDB id " + id,
+ (OutOfMemoryError) ex.getCause());
+ if (sview != null && sview.isVisible())
+ {
+ sview.closeViewer();
+ sview.setVisible(false);
+ sview.dispose();
+ }
+ }
+ }
+ });
+ } catch (InvocationTargetException ex)
+ {
+ warn("Unexpected error when opening Jmol view.", ex);
+
+ } catch (InterruptedException e)
+ {
+ // e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Returns any open frame that matches given structure viewer data. The match
+ * is based on the unique viewId, or (for older project versions) the frame's
+ * geometry.
+ *
+ * @param viewerData
+ * @return
+ */
+ protected StructureViewerBase findMatchingViewer(
+ Entry<String, ViewerData> viewerData)
+ {
+ final String sviewid = viewerData.getKey();
+ final ViewerData svattrib = viewerData.getValue();
+ StructureViewerBase comp = null;
+ JInternalFrame[] frames = getAllFrames();
+ for (JInternalFrame frame : frames)
+ {
+ if (frame instanceof StructureViewerBase)
+ {
+ /*
+ * Post jalview 2.4 schema includes structure view id
+ */
+ if (sviewid != null
+ && ((StructureViewerBase) frame).getViewId().equals(
+ sviewid))
+ {
+ comp = (AppJmol) frame;
+ // todo: break?
+ }
+ /*
+ * Otherwise test for matching position and size of viewer frame
+ */
+ else if (frame.getX() == svattrib.x && frame.getY() == svattrib.y
+ && frame.getHeight() == svattrib.height
+ && frame.getWidth() == svattrib.width)
+ {
+ comp = (AppJmol) frame;
+ // todo: break?
+ }
+ }
+ }
+ return comp;
+ }
+
+ /**
+ * Link an AlignmentPanel to an existing structure viewer.
+ *
+ * @param ap
+ * @param viewer
+ * @param oldFiles
+ * @param useinViewerSuperpos
+ * @param usetoColourbyseq
+ * @param viewerColouring
+ */
+ protected void linkStructureViewer(AlignmentPanel ap,
+ StructureViewerBase viewer, ViewerData svattrib)
+ {
+ // NOTE: if the jalview project is part of a shared session then
+ // view synchronization should/could be done here.
+
+ final boolean useinViewerSuperpos = svattrib.alignWithPanel;
+ final boolean usetoColourbyseq = svattrib.colourWithAlignPanel;
+ final boolean viewerColouring = svattrib.colourByViewer;
+ Map<File, Object[]> oldFiles = svattrib.fileData;
+
+ /*
+ * Add mapping for sequences in this view to an already open viewer
+ */
+ final AAStructureBindingModel binding = viewer.getBinding();
+ for (File id : oldFiles.keySet())
+ {
+ // add this and any other pdb files that should be present in the
+ // viewer
+ Object[] filedat = oldFiles.get(id);
+ String pdbFile = (String) filedat[0];
+ SequenceI[] seq = ((Vector<SequenceI>) filedat[2])
+ .toArray(new SequenceI[0]);
+ binding
+ .getSsm()
+ .setMapping(seq, null, pdbFile,
+ jalview.io.AppletFormatAdapter.FILE);
+ binding.addSequenceForStructFile(pdbFile, seq);
+ }
+ // and add the AlignmentPanel's reference to the view panel
+ viewer.addAlignmentPanel(ap);
+ if (useinViewerSuperpos)
+ {
+ viewer.useAlignmentPanelForSuperposition(ap);
+ }
+ else
+ {
+ viewer.excludeAlignmentPanelForSuperposition(ap);
+ }
+ if (usetoColourbyseq)
+ {
+ viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
+ }
+ else
+ {
+ viewer.excludeAlignmentPanelForColourbyseq(ap);
+ }
+ }
+
+ /**
+ * Get all frames within the Desktop.
+ *
+ * @return
+ */
+ protected JInternalFrame[] getAllFrames()
+ {
+ JInternalFrame[] frames = null;
+ // TODO is this necessary - is it safe - risk of hanging?
+ do
+ {
+ try