+ // 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.");
+ }
+
+ }
+ }
+ 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, hideConsensus,
+ hideQuality, hideConservation, jms, view, uniqueSeqSetId,
+ viewId);
+ 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[]>() });
+ }
+ // TODO: assemble String[] { pdb files }, String[] { id for each
+ // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
+ // seqs_file 2}} from hash
+ Object[] jmoldat = (Object[]) jmolViewIds.get(sviewid);
+ if (((String) jmoldat[1]).length() < ids[p]
+ .getStructureState(s).getContent().length())
+ {
+ {
+ jmoldat[1] = ids[p].getStructureState(s).getContent();
+ }
+ }
+ Object[] seqstrmaps = (Object[]) ((Hashtable) jmoldat[2])
+ .get(ids[p].getFile());
+ if (seqstrmaps == null)
+ {
+ ((Hashtable) jmoldat[2]).put(
+ new File(ids[p].getFile()).toString(),
+ 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
+ }
+ }
+ }
+ }
+ }
+ {
+
+ // 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<String, Object[]> oldFiles = (Hashtable<String, Object[]>) svattrib[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();
+ }
+ 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)
+ .toString());
+ newFileLoc.append(((String) filedat[0]));
+ pdbfilenames.addElement((String) filedat[0]);
+ pdbids.addElement((String) filedat[1]);
+ seqmaps.addElement((SequenceI[]) ((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.
+ }
+ 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 (String 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((SequenceI[]) ((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 = (String[]) pdbfilenames
+ .toArray(new String[pdbfilenames.size()]), id = (String[]) pdbids
+ .toArray(new String[pdbids.size()]);
+ final SequenceI[][] sq = (SequenceI[][]) 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()
+ {
+ public void run()
+ {
+ AppJmol sview=null;
+ try {
+ sview=new AppJmol(pdbf, id, sq, alf.alignPanel, fileloc,
+
+ rect, vid);
+ }
+ 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 (String 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 = (SequenceI[]) ((Vector<SequenceI>) filedat[2])
+ .toArray(new SequenceI[0]);
+ StructureSelectionManager.getStructureSelectionManager()
+ .setMapping(seq, null, pdbFile,
+ jalview.io.AppletFormatAdapter.FILE);
+ ((AppJmol) comp).jmb.addSequenceForStructFile(pdbFile, seq);
+ }
+ }
+ }
+ }
+ }
+ // and finally return.
+ return af;
+ }
+
+ AlignFrame loadViewport(String file, JSeq[] JSEQ, Vector hiddenSeqs,
+ Alignment al, boolean hideConsensus, boolean hideQuality,
+ boolean hideConservation, JalviewModelSequence jms,
+ Viewport view, String uniqueSeqSetId, String viewId)
+ {
+ AlignFrame af = null;
+ af = new AlignFrame(al, view.getWidth(), view.getHeight(),
+ uniqueSeqSetId, viewId);