+ // If we just load in the same jar file again, the sequenceSetId
+ // will be the same, and we end up with multiple references
+ // to the same sequenceSet. We must modify this id on load
+ // so that each load of the file gives a unique id
+ String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
+ String viewId = (view.getId() == null ? null : view.getId()
+ + uniqueSetSuffix);
+ AlignFrame af = null;
+ AlignViewport av = null;
+ // now check to see if we really need to create a new viewport.
+ if (multipleView && viewportsAdded.size() == 0)
+ {
+ // We recovered an alignment for which a viewport already exists.
+ // TODO: fix up any settings necessary for overlaying stored state onto
+ // state recovered from another document. (may not be necessary).
+ // we may need a binding from a viewport in memory to one recovered from
+ // XML.
+ // and then recover its containing af to allow the settings to be applied.
+ // TODO: fix for vamsas demo
+ System.err
+ .println("About to recover a viewport for existing alignment: Sequence set ID is "
+ + uniqueSeqSetId);
+ Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
+ if (seqsetobj != null)
+ {
+ if (seqsetobj instanceof String)
+ {
+ uniqueSeqSetId = (String) seqsetobj;
+ System.err
+ .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
+ + uniqueSeqSetId);
+ }
+ else
+ {
+ System.err
+ .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
+ }
+
+ }
+ }
+ /**
+ * indicate that annotation colours are applied across all groups (pre
+ * Jalview 2.8.1 behaviour)
+ */
+ boolean doGroupAnnColour = isVersionStringLaterThan("2.8.1",
+ object.getVersion());
+
+ AlignmentPanel ap = null;
+ boolean isnewview = true;
+ if (viewId != null)
+ {
+ // Check to see if this alignment already has a view id == viewId
+ jalview.gui.AlignmentPanel views[] = Desktop
+ .getAlignmentPanels(uniqueSeqSetId);
+ if (views != null && views.length > 0)
+ {
+ for (int v = 0; v < views.length; v++)
+ {
+ if (views[v].av.getViewId().equalsIgnoreCase(viewId))
+ {
+ // recover the existing alignpanel, alignframe, viewport
+ af = views[v].alignFrame;
+ av = views[v].av;
+ ap = views[v];
+ // TODO: could even skip resetting view settings if we don't want to
+ // change the local settings from other jalview processes
+ isnewview = false;
+ }
+ }
+ }
+ }
+
+ if (isnewview)
+ {
+ af = loadViewport(file, JSEQ, hiddenSeqs, al, jms, view,
+ uniqueSeqSetId, viewId, autoAlan);
+ av = af.viewport;
+ ap = af.alignPanel;
+ }
+ // LOAD TREES
+ // /////////////////////////////////////
+ if (loadTreesAndStructures && jms.getTreeCount() > 0)
+ {
+ try
+ {
+ for (int t = 0; t < jms.getTreeCount(); t++)
+ {
+
+ Tree tree = jms.getTree(t);
+
+ TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
+ if (tp == null)
+ {
+ tp = af.ShowNewickTree(
+ new jalview.io.NewickFile(tree.getNewick()),
+ tree.getTitle(), tree.getWidth(), tree.getHeight(),
+ tree.getXpos(), tree.getYpos());
+ if (tree.getId() != null)
+ {
+ // perhaps bind the tree id to something ?
+ }
+ }
+ else
+ {
+ // update local tree attributes ?
+ // TODO: should check if tp has been manipulated by user - if so its
+ // settings shouldn't be modified
+ tp.setTitle(tree.getTitle());
+ tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
+ .getWidth(), tree.getHeight()));
+ tp.av = av; // af.viewport; // TODO: verify 'associate with all
+ // views'
+ // works still
+ tp.treeCanvas.av = av; // af.viewport;
+ tp.treeCanvas.ap = ap; // af.alignPanel;
+
+ }
+ if (tp == null)
+ {
+ warn("There was a problem recovering stored Newick tree: \n"
+ + tree.getNewick());
+ continue;
+ }
+
+ tp.fitToWindow.setState(tree.getFitToWindow());
+ tp.fitToWindow_actionPerformed(null);
+
+ if (tree.getFontName() != null)
+ {
+ tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
+ .getFontStyle(), tree.getFontSize()));
+ }
+ else
+ {
+ tp.setTreeFont(new java.awt.Font(view.getFontName(), view
+ .getFontStyle(), tree.getFontSize()));
+ }
+
+ tp.showPlaceholders(tree.getMarkUnlinked());
+ tp.showBootstrap(tree.getShowBootstrap());
+ tp.showDistances(tree.getShowDistances());
+
+ tp.treeCanvas.threshold = tree.getThreshold();
+
+ if (tree.getCurrentTree())
+ {
+ af.viewport.setCurrentTree(tp.getTree());
+ }
+ }
+
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ // //LOAD STRUCTURES
+ if (loadTreesAndStructures)
+ {
+ // run through all PDB ids on the alignment, and collect mappings between
+ // jmol view ids and all sequences referring to it
+ Hashtable<String, Object[]> jmolViewIds = new Hashtable();
+
+ for (int i = 0; i < JSEQ.length; i++)
+ {
+ if (JSEQ[i].getPdbidsCount() > 0)
+ {
+ Pdbids[] ids = JSEQ[i].getPdbids();
+ for (int p = 0; p < ids.length; p++)
+ {
+ for (int s = 0; s < ids[p].getStructureStateCount(); s++)
+ {
+ // check to see if we haven't already created this structure view
+ String sviewid = (ids[p].getStructureState(s).getViewId() == null) ? null
+ : ids[p].getStructureState(s).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 = ids[p].getStructureState(s).getXpos();
+ int y = ids[p].getStructureState(s).getYpos();
+ int width = ids[p].getStructureState(s).getWidth();
+ int height = ids[p].getStructureState(s).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 = (jalview.datamodel.SequenceI) seqRefIds
+ .get(JSEQ[i].getId() + "");
+ if (sviewid == null)
+ {
+ sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
+ + "," + height;
+ }
+ if (!jmolViewIds.containsKey(sviewid))
+ {
+ jmolViewIds.put(sviewid, new Object[]
+ { new int[]
+ { x, y, width, height }, "",
+ new Hashtable<String, Object[]>(), new boolean[]
+ { 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
+ Object[] jmoldat = jmolViewIds.get(sviewid);
+ ((boolean[]) jmoldat[3])[0] |= ids[p].getStructureState(s)
+ .hasAlignwithAlignPanel() ? ids[p].getStructureState(
+ s).getAlignwithAlignPanel() : false;
+ // never colour by linked panel if not specified
+ ((boolean[]) jmoldat[3])[1] |= ids[p].getStructureState(s)
+ .hasColourwithAlignPanel() ? ids[p]
+ .getStructureState(s).getColourwithAlignPanel()
+ : false;
+ // default for pre-2.7 projects is that Jmol colouring is enabled
+ ((boolean[]) jmoldat[3])[2] &= ids[p].getStructureState(s)
+ .hasColourByJmol() ? ids[p].getStructureState(s)
+ .getColourByJmol() : true;
+
+ if (((String) jmoldat[1]).length() < ids[p]
+ .getStructureState(s).getContent().length())
+ {
+ {
+ jmoldat[1] = ids[p].getStructureState(s).getContent();
+ }
+ }
+ if (ids[p].getFile() != null)
+ {
+ File mapkey = new File(ids[p].getFile());
+ Object[] seqstrmaps = (Object[]) ((Hashtable) jmoldat[2])
+ .get(mapkey);
+ if (seqstrmaps == null)
+ {
+ ((Hashtable) jmoldat[2]).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 Jmol views
+ for (Entry<String, Object[]> entry : jmolViewIds.entrySet())
+ {
+ String sviewid = entry.getKey();
+ Object[] svattrib = entry.getValue();
+ int[] geom = (int[]) svattrib[0];
+ String state = (String) svattrib[1];
+ Hashtable<File, Object[]> oldFiles = (Hashtable<File, Object[]>) svattrib[2];
+ final boolean useinJmolsuperpos = ((boolean[]) svattrib[3])[0], usetoColourbyseq = ((boolean[]) svattrib[3])[1], jmolColouring = ((boolean[]) svattrib[3])[2];
+ int x = geom[0], y = geom[1], width = geom[2], height = geom[3];
+ // collate the pdbfile -> sequence mappings from this view
+ Vector<String> pdbfilenames = new Vector<String>();
+ Vector<SequenceI[]> seqmaps = new Vector<SequenceI[]>();
+ Vector<String> pdbids = new Vector<String>();
+
+ // Search to see if we've already created this Jmol view
+ AppJmol comp = null;
+ JInternalFrame[] frames = null;
+ do
+ {
+ try
+ {
+ frames = Desktop.desktop.getAllFrames();
+ } catch (ArrayIndexOutOfBoundsException e)
+ {
+ // occasional No such child exceptions are thrown here...
+ frames = null;
+ try
+ {
+ Thread.sleep(10);
+ } catch (Exception f)
+ {
+ }
+ ;
+ }
+ } while (frames == null);
+ // search for any Jmol windows already open from other
+ // alignment views that exactly match the stored structure state
+ for (int f = 0; comp == null && f < frames.length; f++)
+ {
+ if (frames[f] instanceof AppJmol)
+ {
+ if (sviewid != null
+ && ((AppJmol) frames[f]).getViewId().equals(sviewid))
+ {
+ // post jalview 2.4 schema includes structure view id
+ comp = (AppJmol) frames[f];
+ }
+ else if (frames[f].getX() == x && frames[f].getY() == y
+ && frames[f].getHeight() == height
+ && frames[f].getWidth() == width)
+ {
+ comp = (AppJmol) frames[f];
+ }
+ }
+ }
+
+ if (comp == null)
+ {
+ // 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.
+ StringBuffer newFileLoc = null;
+ int cp = 0, ncp, ecp;
+ while ((ncp = state.indexOf("load ", cp)) > -1)
+ {
+ if (newFileLoc == null)
+ {
+ newFileLoc = new StringBuffer();
+ }
+ 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.addElement((String) filedat[0]);
+ pdbids.addElement((String) filedat[1]);
+ seqmaps.addElement(((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 StringBuffer(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.addElement((String) filedat[0]);
+ pdbids.addElement((String) filedat[1]);
+ seqmaps.addElement(((Vector<SequenceI>) filedat[2])
+ .toArray(new SequenceI[0]));
+ newFileLoc.append(" \"");
+ newFileLoc.append((String) filedat[0]);
+ newFileLoc.append("\"");
+
+ }
+ newFileLoc.append(";");
+ }
+
+ if (newFileLoc != null)
+ {
+ 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);
+ }
+ }
+ // TODO: assemble String[] { pdb files }, String[] { id for each
+ // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
+ // seqs_file 2}} from hash
+ final String[] pdbf = pdbfilenames
+ .toArray(new String[pdbfilenames.size()]), id = pdbids
+ .toArray(new String[pdbids.size()]);
+ final SequenceI[][] sq = seqmaps
+ .toArray(new SequenceI[seqmaps.size()][]);
+ final String fileloc = newFileLoc.toString(), vid = sviewid;
+ final AlignFrame alf = af;
+ final java.awt.Rectangle rect = new java.awt.Rectangle(x, y,
+ width, 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.Viewer.JMOL, pdbf, id, sq, alf.alignPanel,
+ useinJmolsuperpos, usetoColourbyseq,
+ jmolColouring, fileloc, rect, vid);
+ 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();
+ }
+ }
+
+ }
+ else
+ // if (comp != null)
+ {
+ // NOTE: if the jalview project is part of a shared session then
+ // view synchronization should/could be done here.
+
+ // add mapping for sequences in this view to an already open Jmol
+ // instance
+ 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]);
+ comp.jmb.ssm.setMapping(seq, null, pdbFile,
+ jalview.io.AppletFormatAdapter.FILE);
+ comp.jmb.addSequenceForStructFile(pdbFile, seq);
+ }
+ // and add the AlignmentPanel's reference to the Jmol view
+ comp.addAlignmentPanel(ap);
+ if (useinJmolsuperpos)
+ {
+ comp.useAlignmentPanelForSuperposition(ap);
+ }
+ else
+ {
+ comp.excludeAlignmentPanelForSuperposition(ap);
+ }
+ if (usetoColourbyseq)
+ {
+ comp.useAlignmentPanelForColourbyseq(ap, !jmolColouring);
+ }
+ else
+ {
+ comp.excludeAlignmentPanelForColourbyseq(ap);
+ }
+ }
+ }
+ }
+ }
+ // and finally return.
+ return af;
+ }
+
+ /**
+ *
+ * @param supported
+ * - minimum version we are comparing against
+ * @param version
+ * - version of data being processsed.
+ * @return true if version is development/null or evaluates to the same or
+ * later X.Y.Z (where X,Y,Z are like [0-9]+b?[0-9]*)
+ */
+ private boolean isVersionStringLaterThan(String supported, String version)
+ {
+ if (version == null || version.equalsIgnoreCase("DEVELOPMENT BUILD")
+ || version.equalsIgnoreCase("Test")
+ || version.equalsIgnoreCase("AUTOMATED BUILD"))
+ {
+ System.err.println("Assuming project file with "
+ + (version == null ? "null" : version)
+ + " is compatible with Jalview version " + supported);
+ return true;
+ }
+ else
+ {
+ StringTokenizer currentV = new StringTokenizer(supported, "."), fileV = new StringTokenizer(
+ version, ".");
+ while (currentV.hasMoreTokens() && fileV.hasMoreTokens())
+ {
+ // convert b to decimal to catch bugfix releases within a series
+ String curT = currentV.nextToken().toLowerCase().replace('b', '.');
+ String fileT = fileV.nextToken().toLowerCase().replace('b', '.');
+ try
+ {
+ if (Float.valueOf(curT) > Float.valueOf(fileT))
+ {
+ // current version is newer than the version that wrote the file
+ return false;
+ }
+ } catch (NumberFormatException nfe)
+ {
+ System.err
+ .println("** WARNING: Version comparison failed for tokens ("
+ + curT
+ + ") and ("
+ + fileT
+ + ")\n** Current: '"
+ + supported + "' and Version: '" + version + "'");
+ }
+ }
+ if (currentV.hasMoreElements())
+ {
+ // fileV has no minor version but identical series to current
+ return false;
+ }
+ }
+ return true;
+ }
+
+ Vector<JalviewStructureDisplayI> newStructureViewers = null;
+
+ protected void addNewStructureViewer(JalviewStructureDisplayI sview)
+ {
+ if (newStructureViewers != null)
+ {
+ sview.getBinding().setFinishedLoadingFromArchive(false);
+ newStructureViewers.add(sview);
+ }
+ }
+
+ protected void setLoadingFinishedForNewStructureViewers()
+ {
+ if (newStructureViewers != null)
+ {
+ for (JalviewStructureDisplayI sview : newStructureViewers)
+ {
+ sview.getBinding().setFinishedLoadingFromArchive(true);
+ }
+ newStructureViewers.clear();
+ newStructureViewers = null;
+ }
+ }
+
+ AlignFrame loadViewport(String file, JSeq[] JSEQ, Vector hiddenSeqs,
+ Alignment al, JalviewModelSequence jms, Viewport view,
+ String uniqueSeqSetId, String viewId,
+ ArrayList<JvAnnotRow> autoAlan)
+ {
+ AlignFrame af = null;
+ af = new AlignFrame(al, view.getWidth(), view.getHeight(),
+ uniqueSeqSetId, viewId);