JAL-1588 part work save/restore of Chimera session
[jalview.git] / src / jalview / gui / Jalview2XML.java
index 8ddd050..9ba8fb7 100644 (file)
@@ -25,6 +25,7 @@ import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.schemabinding.version2.AlcodMap;
 import jalview.schemabinding.version2.Alcodon;
@@ -64,7 +65,9 @@ import jalview.schemes.ColourSchemeProperty;
 import jalview.schemes.GraduatedColor;
 import jalview.schemes.ResidueColourScheme;
 import jalview.schemes.ResidueProperties;
+import jalview.schemes.UserColourScheme;
 import jalview.structure.StructureSelectionManager;
+import jalview.structures.models.AAStructureBindingModel;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.util.jarInputStreamProvider;
@@ -92,11 +95,13 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.StringTokenizer;
@@ -123,6 +128,64 @@ import org.exolab.castor.xml.Unmarshaller;
  */
 public class Jalview2XML
 {
+
+  /**
+   * A data bean to hold stored data about a structure viewer.
+   */
+  public class ViewerData
+  {
+
+    private int x;
+
+    private int y;
+
+    private int width;
+
+    private int height;
+
+    public boolean alignWithPanel;
+
+    public boolean colourWithAlignPanel;
+
+    public boolean colourByViewer;
+
+    String stateData = "";
+
+    // todo: java bean in place of Object []
+    private Map<File, Object[]> fileData = new HashMap<File, Object[]>();
+
+    public ViewerData(int x, int y, int width, int height,
+            boolean alignWithPanel, boolean colourWithAlignPanel,
+            boolean colourByViewer)
+    {
+      this.x = x;
+      this.y = y;
+      this.width = width;
+      this.height = height;
+      this.alignWithPanel = alignWithPanel;
+      this.colourWithAlignPanel = colourWithAlignPanel;
+      this.colourByViewer = colourByViewer;
+    }
+
+  }
+
+  /*
+   * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
+   * of sequence objects are created.
+   */
+  IdentityHashMap<SequenceI, String> seqsToIds = null;
+
+  /**
+   * jalview XML Sequence ID to jalview sequence object reference (both dataset
+   * and alignment sequences. Populated as XML reps of sequence objects are
+   * created.)
+   */
+  Map<String, SequenceI> seqRefIds = null;
+
+  Vector frefedSequence = null;
+
+  boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
+
   /**
    * create/return unique hash string for sq
    * 
@@ -137,7 +200,7 @@ public class Jalview2XML
     }
     if (seqsToIds.containsKey(sq))
     {
-      return (String) seqsToIds.get(sq);
+      return seqsToIds.get(sq);
     }
     else
     {
@@ -178,31 +241,14 @@ public class Jalview2XML
   {
     if (seqsToIds == null)
     {
-      seqsToIds = new IdentityHashMap();
+      seqsToIds = new IdentityHashMap<SequenceI, String>();
     }
     if (seqRefIds == null)
     {
-      seqRefIds = new Hashtable();
+      seqRefIds = new HashMap<String, SequenceI>();
     }
   }
 
-  /**
-   * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
-   * of sequence objects are created.
-   */
-  java.util.IdentityHashMap seqsToIds = null;
-
-  /**
-   * jalview XML Sequence ID to jalview sequence object reference (both dataset
-   * and alignment sequences. Populated as XML reps of sequence objects are
-   * created.)
-   */
-  java.util.Hashtable seqRefIds = null; // key->SequenceI resolution
-
-  Vector frefedSequence = null;
-
-  boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
-
   public Jalview2XML()
   {
   }
@@ -227,7 +273,7 @@ public class Jalview2XML
           {
             if (ref[1] instanceof jalview.datamodel.Mapping)
             {
-              SequenceI seq = (SequenceI) seqRefIds.get(sref);
+              SequenceI seq = seqRefIds.get(sref);
               while (seq.getDatasetSequence() != null)
               {
                 seq = seq.getDatasetSequence();
@@ -238,7 +284,7 @@ public class Jalview2XML
             {
               if (ref[1] instanceof jalview.datamodel.AlignedCodonFrame)
               {
-                SequenceI seq = (SequenceI) seqRefIds.get(sref);
+                SequenceI seq = seqRefIds.get(sref);
                 while (seq.getDatasetSequence() != null)
                 {
                   seq = seq.getDatasetSequence();
@@ -300,16 +346,17 @@ public class Jalview2XML
   /**
    * List of pdbfiles added to Jar
    */
-  Vector pdbfiles = null;
+  List<String> pdbfiles = null;
 
   // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
-  public void SaveState(File statefile)
+  public void saveState(File statefile)
   {
+    FileOutputStream fos = null;
     try
     {
-      FileOutputStream fos = new FileOutputStream(statefile);
+      fos = new FileOutputStream(statefile);
       JarOutputStream jout = new JarOutputStream(fos);
-      SaveState(jout);
+      saveState(jout);
 
     } catch (Exception e)
     {
@@ -325,6 +372,18 @@ public class Jalview2XML
         errorMessage += "(output file was '" + statefile + "')";
       }
       e.printStackTrace();
+    } finally
+    {
+      if (fos != null)
+      {
+        try
+        {
+          fos.close();
+        } catch (IOException e)
+        {
+          // ignore
+        }
+      }
     }
     reportErrors();
   }
@@ -334,7 +393,7 @@ public class Jalview2XML
    * 
    * @param jout
    */
-  public void SaveState(JarOutputStream jout)
+  public void saveState(JarOutputStream jout)
   {
     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
@@ -350,8 +409,6 @@ public class Jalview2XML
 
       // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
       // //////////////////////////////////////////////////
-      // NOTE ALSO new PrintWriter must be used for each new JarEntry
-      PrintWriter out = null;
 
       Vector shortNames = new Vector();
 
@@ -410,7 +467,7 @@ public class Jalview2XML
               fileName = fileName + ".xml";
             }
 
-            SaveState(apanel, fileName, jout);
+            saveState(apanel, fileName, jout);
 
             String dssid = getDatasetIdRef(af.getViewport().getAlignment()
                     .getDataset());
@@ -447,7 +504,7 @@ public class Jalview2XML
   }
 
   // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
-  public boolean SaveAlignment(AlignFrame af, String jarFile,
+  public boolean saveAlignment(AlignFrame af, String jarFile,
           String fileName)
   {
     try
@@ -465,7 +522,7 @@ public class Jalview2XML
         {
           jfileName = jfileName + ".xml";
         }
-        SaveState(apanel, jfileName, jout);
+        saveState(apanel, jfileName, jout);
         String dssid = getDatasetIdRef(af.getViewport().getAlignment()
                 .getDataset());
         if (!dsses.containsKey(dssid))
@@ -498,17 +555,17 @@ public class Jalview2XML
     for (String dssids : dsses.keySet())
     {
       AlignFrame _af = dsses.get(dssids);
-      String jfileName = MessageManager.formatMessage("label.dataset_for", new String[]{fileName,_af.getTitle()});
+      String jfileName = fileName + " Dataset for " + _af.getTitle();
       if (!jfileName.endsWith(".xml"))
       {
         jfileName = jfileName + ".xml";
       }
-      SaveState(_af.alignPanel, jfileName, true, jout);
+      saveState(_af.alignPanel, jfileName, true, jout);
     }
   }
 
   /**
-   * create a JalviewModel from an algnment view and marshall it to a
+   * create a JalviewModel from an alignment view and marshall it to a
    * JarOutputStream
    * 
    * @param ap
@@ -520,14 +577,14 @@ public class Jalview2XML
    * @param out
    *          jar entry name
    */
-  public JalviewModel SaveState(AlignmentPanel ap, String fileName,
+  public JalviewModel saveState(AlignmentPanel ap, String fileName,
           JarOutputStream jout)
   {
-    return SaveState(ap, fileName, false, jout);
+    return saveState(ap, fileName, false, jout);
   }
 
   /**
-   * create a JalviewModel from an algnment view and marshall it to a
+   * create a JalviewModel from an alignment view and marshall it to a
    * JarOutputStream
    * 
    * @param ap
@@ -542,12 +599,12 @@ public class Jalview2XML
    * @param out
    *          jar entry name
    */
-  public JalviewModel SaveState(AlignmentPanel ap, String fileName,
+  public JalviewModel saveState(AlignmentPanel ap, String fileName,
           boolean storeDS, JarOutputStream jout)
   {
     initSeqRefs();
-    Vector jmolViewIds = new Vector(); //
-    Vector userColours = new Vector();
+    List<String> viewIds = new ArrayList<String>();
+    List<UserColourScheme> userColours = new ArrayList<UserColourScheme>();
 
     AlignViewport av = ap.av;
 
@@ -716,81 +773,22 @@ public class Jalview2XML
 
           pdb.setId(entry.getId());
           pdb.setType(entry.getType());
-          //
-          // store any JMol views associated with this seqeunce
-          // this section copes with duplicate entries in the project, so a
-          // dataset only view *should* be coped with sensibly
-          AppJmol jmol;
+
+          /*
+           * Store any structure views associated with this sequence. This
+           * section copes with duplicate entries in the project, so a dataset
+           * only view *should* be coped with sensibly.
+           */
           // This must have been loaded, is it still visible?
           JInternalFrame[] frames = Desktop.desktop.getAllFrames();
           String matchedFile = null;
           for (int f = frames.length - 1; f > -1; f--)
           {
-            if (frames[f] instanceof AppJmol)
+            if (frames[f] instanceof StructureViewerBase)
             {
-              jmol = (AppJmol) frames[f];
-              for (int peid = 0; peid < jmol.jmb.pdbentry.length; peid++)
-              {
-                if (!jmol.jmb.pdbentry[peid].getId().equals(entry.getId())
-                        && !(entry.getId().length() > 4 && entry
-                                .getId()
-                                .toLowerCase()
-                                .startsWith(
-                                        jmol.jmb.pdbentry[peid].getId()
-                                                .toLowerCase())))
-                {
-                  continue;
-                }
-                if (matchedFile == null)
-                {
-                  matchedFile = jmol.jmb.pdbentry[peid].getFile();
-                }
-                else if (!matchedFile.equals(jmol.jmb.pdbentry[peid]
-                        .getFile()))
-                {
-                  Cache.log
-                          .warn("Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
-                                  + jmol.jmb.pdbentry[peid].getFile());
-                  ; // record the
-                }
-                // file so we
-                // can get at it if the ID
-                // match is ambiguous (e.g.
-                // 1QIP==1qipA)
-                String statestring = jmol.jmb.viewer.getStateInfo();
-
-                for (int smap = 0; smap < jmol.jmb.sequence[peid].length; smap++)
-                {
-                  // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
-                  if (jds == jmol.jmb.sequence[peid][smap])
-                  {
-                    StructureState state = new StructureState();
-                    state.setVisible(true);
-                    state.setXpos(jmol.getX());
-                    state.setYpos(jmol.getY());
-                    state.setWidth(jmol.getWidth());
-                    state.setHeight(jmol.getHeight());
-                    state.setViewId(jmol.getViewId());
-                    state.setAlignwithAlignPanel(jmol.isUsedforaligment(ap));
-                    state.setColourwithAlignPanel(jmol
-                            .isUsedforcolourby(ap));
-                    state.setColourByJmol(jmol.isColouredByJmol());
-                    if (!jmolViewIds.contains(state.getViewId()))
-                    {
-                      // Make sure we only store a Jmol state once in each XML
-                      // document.
-                      jmolViewIds.addElement(state.getViewId());
-                      state.setContent(statestring.replaceAll("\n", ""));
-                    }
-                    else
-                    {
-                      state.setContent("# duplicate state");
-                    }
-                    pdb.addStructureState(state);
-                  }
-
-                }
-              }
+              StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
+              matchedFile = saveStructureState(ap, jds, pdb, entry,
+                      viewIds, matchedFile, viewFrame);
             }
           }
 
@@ -804,12 +802,13 @@ public class Jalview2XML
             pdb.setFile(matchedFile); // entry.getFile());
             if (pdbfiles == null)
             {
-              pdbfiles = new Vector();
+              pdbfiles = new ArrayList<String>();
             }
 
             if (!pdbfiles.contains(entry.getId()))
             {
-              pdbfiles.addElement(entry.getId());
+              pdbfiles.add(entry.getId());
+              DataInputStream dis = null;
               try
               {
                 File file = new File(matchedFile);
@@ -817,7 +816,7 @@ public class Jalview2XML
                 {
                   byte[] data = new byte[(int) file.length()];
                   jout.putNextEntry(new JarEntry(entry.getId()));
-                  DataInputStream dis = new DataInputStream(
+                  dis = new DataInputStream(
                           new FileInputStream(file));
                   dis.readFully(data);
 
@@ -829,6 +828,18 @@ public class Jalview2XML
               } catch (Exception ex)
               {
                 ex.printStackTrace();
+              } finally
+              {
+                if (dis != null)
+                {
+                  try
+                  {
+                    dis.close();
+                  } catch (IOException e)
+                  {
+                    // ignore
+                  }
+                }
               }
 
             }
@@ -997,7 +1008,7 @@ public class Jalview2XML
 
             if (sg.cs instanceof jalview.schemes.UserColourScheme)
             {
-              groups[i].setColour(SetUserColourScheme(sg.cs, userColours,
+              groups[i].setColour(setUserColourScheme(sg.cs, userColours,
                       jms));
             }
             else
@@ -1016,7 +1027,7 @@ public class Jalview2XML
           else if (sg.cs instanceof jalview.schemes.UserColourScheme)
           {
             groups[i]
-                    .setColour(SetUserColourScheme(sg.cs, userColours, jms));
+                    .setColour(setUserColourScheme(sg.cs, userColours, jms));
           }
           else
           {
@@ -1079,7 +1090,7 @@ public class Jalview2XML
 
       if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
       {
-        view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(),
+        view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
                 userColours, jms));
       }
       else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
@@ -1107,7 +1118,7 @@ public class Jalview2XML
           view.setConsThreshold(cs.getConservationInc());
           if (cs instanceof jalview.schemes.UserColourScheme)
           {
-            view.setBgColour(SetUserColourScheme(cs, userColours, jms));
+            view.setBgColour(setUserColourScheme(cs, userColours, jms));
           }
         }
 
@@ -1145,7 +1156,7 @@ public class Jalview2XML
       view.setFollowHighlight(av.followHighlight);
       view.setFollowSelection(av.followSelection);
       view.setIgnoreGapsinConsensus(av.getIgnoreGapsConsensus());
-      if (av.featuresDisplayed != null)
+      if (av.getFeaturesDisplayed() != null)
       {
         jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
 
@@ -1180,7 +1191,7 @@ public class Jalview2XML
                       .getColour(renderOrder[ro]).getRGB());
             }
 
-            setting.setDisplay(av.featuresDisplayed
+            setting.setDisplay(av.getFeaturesDisplayed()
                     .containsKey(renderOrder[ro]));
             float rorder = ap.getSeqPanel().seqCanvas.getFeatureRenderer()
                     .getOrder(renderOrder[ro]);
@@ -1308,8 +1319,94 @@ public class Jalview2XML
     return object;
   }
 
+  /**
+   * Save the state of a structure viewer
+   * 
+   * @param ap
+   * @param jds
+   * @param pdb
+   *          the archive XML element under which to save the state
+   * @param entry
+   * @param viewIds
+   * @param matchedFile
+   * @param viewFrame
+   * @return
+   */
+  protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
+          Pdbids pdb, PDBEntry entry, List<String> viewIds,
+          String matchedFile, StructureViewerBase viewFrame)
+  {
+    final AAStructureBindingModel bindingModel = viewFrame
+            .getBinding();
+    for (int peid = 0; peid < bindingModel
+            .getPdbCount(); peid++)
+    {
+      final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
+      final String pdbId = pdbentry.getId();
+      if (!pdbId.equals(entry.getId())
+              && !(entry.getId().length() > 4 && entry.getId()
+                      .toLowerCase()
+                      .startsWith(pdbId.toLowerCase())))
+      {
+        continue;
+      }
+      if (matchedFile == null)
+      {
+        matchedFile = pdbentry.getFile();
+      }
+      else if (!matchedFile.equals(pdbentry
+              .getFile()))
+      {
+        Cache.log
+                .warn("Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
+                        + pdbentry.getFile());
+      }
+      // record the
+      // file so we
+      // can get at it if the ID
+      // match is ambiguous (e.g.
+      // 1QIP==1qipA)
+      String statestring = viewFrame.getStateInfo();
+
+      for (int smap = 0; smap < viewFrame.getBinding()
+              .getSequence()[peid].length; smap++)
+      {
+        // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
+        if (jds == viewFrame.getBinding().getSequence()[peid][smap])
+        {
+          StructureState state = new StructureState();
+          state.setVisible(true);
+          state.setXpos(viewFrame.getX());
+          state.setYpos(viewFrame.getY());
+          state.setWidth(viewFrame.getWidth());
+          state.setHeight(viewFrame.getHeight());
+          final String viewId = viewFrame.getViewId();
+          state.setViewId(viewId);
+          state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
+          state.setColourwithAlignPanel(viewFrame
+                  .isUsedforcolourby(ap));
+          state.setColourByJmol(viewFrame.isColouredByViewer());
+          /*
+           * Only store each structure viewer's state once in each XML document.
+           */
+          if (!viewIds.contains(viewId))
+          {
+            viewIds.add(viewId);
+            state.setContent(statestring.replaceAll("\n", ""));
+          }
+          else
+          {
+            state.setContent("# duplicate state");
+          }
+          pdb.addStructureState(state);
+        }
+      }
+    }
+    return matchedFile;
+  }
+
   private AnnotationColours constructAnnotationColours(
-          AnnotationColourGradient acg, Vector userColours,
+          AnnotationColourGradient acg, List<UserColourScheme> userColours,
           JalviewModelSequence jms)
   {
     AnnotationColours ac = new AnnotationColours();
@@ -1318,7 +1415,7 @@ public class Jalview2XML
     ac.setAnnotation(acg.getAnnotation());
     if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme)
     {
-      ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(),
+      ac.setColourScheme(setUserColourScheme(acg.getBaseColour(),
               userColours, jms));
     }
     else
@@ -1456,8 +1553,7 @@ public class Jalview2XML
           }
 
           ae.setPosition(a);
-          if (aa[i].annotations[a].secondaryStructure != ' '
-                  && aa[i].annotations[a].secondaryStructure != '\0')
+          if (aa[i].annotations[a].secondaryStructure > ' ')
           {
             ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure
                     + "");
@@ -1763,8 +1859,8 @@ public class Jalview2XML
     return mp;
   }
 
-  String SetUserColourScheme(jalview.schemes.ColourSchemeI cs,
-          Vector userColours, JalviewModelSequence jms)
+  String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
+          List<UserColourScheme> userColours, JalviewModelSequence jms)
   {
     String id = null;
     jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
@@ -1809,7 +1905,7 @@ public class Jalview2XML
     return id;
   }
 
-  jalview.schemes.UserColourScheme GetUserColourScheme(
+  jalview.schemes.UserColourScheme getUserColourScheme(
           JalviewModelSequence jms, String id)
   {
     UserColours[] uc = jms.getUserColours();
@@ -1867,7 +1963,7 @@ public class Jalview2XML
    * @param file
    *          - HTTP URL or filename
    */
-  public AlignFrame LoadJalviewAlign(final String file)
+  public AlignFrame loadJalviewAlign(final String file)
   {
 
     jalview.gui.AlignFrame af = null;
@@ -1882,7 +1978,7 @@ public class Jalview2XML
       // so we can re-open the jar input stream for each entry.
 
       jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
-      af = LoadJalviewAlign(jprovider);
+      af = loadJalviewAlign(jprovider);
 
     } catch (MalformedURLException e)
     {
@@ -1955,7 +2051,7 @@ public class Jalview2XML
    * @param jprovider
    * @return
    */
-  public AlignFrame LoadJalviewAlign(final jarInputStreamProvider jprovider)
+  public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
   {
     errorMessage = null;
     if (uniqueSetSuffix == null)
@@ -1964,7 +2060,7 @@ public class Jalview2XML
     }
     if (seqRefIds == null)
     {
-      seqRefIds = new Hashtable();
+      seqRefIds = new HashMap<String, SequenceI>();
     }
     if (viewportsAdded == null)
     {
@@ -2002,7 +2098,7 @@ public class Jalview2XML
           object = (JalviewModel) unmar.unmarshal(in);
           if (true) // !skipViewport(object))
           {
-            _af = LoadFromObject(object, file, true, jprovider);
+            _af = loadFromObject(object, file, true, jprovider);
             if (object.getJalviewModelSequence().getViewportCount() > 0)
             {
               af = _af;
@@ -2228,7 +2324,7 @@ public class Jalview2XML
    *          data source provider
    * @return alignment frame created from view stored in DOM
    */
-  AlignFrame LoadFromObject(JalviewModel object, String file,
+  AlignFrame loadFromObject(JalviewModel object, String file,
           boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
   {
     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
@@ -2249,11 +2345,11 @@ public class Jalview2XML
 
     boolean multipleView = false;
 
-    JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
+    JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
     int vi = 0; // counter in vamsasSeq array
-    for (int i = 0; i < JSEQ.length; i++)
+    for (int i = 0; i < jseqs.length; i++)
     {
-      String seqId = JSEQ[i].getId();
+      String seqId = jseqs[i].getId();
 
       if (seqRefIds.get(seqId) != null)
       {
@@ -2265,15 +2361,15 @@ public class Jalview2XML
         jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
                 vamsasSeq[vi].getSequence());
         jseq.setDescription(vamsasSeq[vi].getDescription());
-        jseq.setStart(JSEQ[i].getStart());
-        jseq.setEnd(JSEQ[i].getEnd());
+        jseq.setStart(jseqs[i].getStart());
+        jseq.setEnd(jseqs[i].getEnd());
         jseq.setVamsasId(uniqueSetSuffix + seqId);
         seqRefIds.put(vamsasSeq[vi].getId(), jseq);
         tmpseqs.add(jseq);
         vi++;
       }
 
-      if (JSEQ[i].getHidden())
+      if (jseqs[i].getHidden())
       {
         if (hiddenSeqs == null)
         {
@@ -2314,7 +2410,10 @@ public class Jalview2XML
     }
     else
     {
-      recoverDatasetFor(vamsasSet, al);
+      // recover dataset - passing on flag indicating if this a 'viewless'
+      // sequence set (a.k.a. a stored dataset for the project)
+      recoverDatasetFor(vamsasSet, al, object.getJalviewModelSequence()
+              .getViewportCount() == 0);
     }
     // ///////////////////////////////
 
@@ -2325,9 +2424,9 @@ public class Jalview2XML
       // structures for the alignment
       for (int i = 0; i < vamsasSeq.length; i++)
       {
-        if (JSEQ[i].getFeaturesCount() > 0)
+        if (jseqs[i].getFeaturesCount() > 0)
         {
-          Features[] features = JSEQ[i].getFeatures();
+          Features[] features = jseqs[i].getFeatures();
           for (int f = 0; f < features.length; f++)
           {
             jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
@@ -2357,9 +2456,9 @@ public class Jalview2XML
         {
           addDBRefs(al.getSequenceAt(i).getDatasetSequence(), vamsasSeq[i]);
         }
-        if (JSEQ[i].getPdbidsCount() > 0)
+        if (jseqs[i].getPdbidsCount() > 0)
         {
-          Pdbids[] ids = JSEQ[i].getPdbids();
+          Pdbids[] ids = jseqs[i].getPdbids();
           for (int p = 0; p < ids.length; p++)
           {
             jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
@@ -2422,7 +2521,7 @@ public class Jalview2XML
           AlcodMap[] maps = alc[i].getAlcodMap();
           for (int m = 0; m < maps.length; m++)
           {
-            SequenceI dnaseq = (SequenceI) seqRefIds
+            SequenceI dnaseq = seqRefIds
                     .get(maps[m].getDnasq());
             // Load Mapping
             jalview.datamodel.Mapping mapping = null;
@@ -2676,7 +2775,7 @@ public class Jalview2XML
         {
           if (groups[i].getColour().startsWith("ucs"))
           {
-            cs = GetUserColourScheme(jms, groups[i].getColour());
+            cs = getUserColourScheme(jms, groups[i].getColour());
           }
           else if (groups[i].getColour().equals("AnnotationColourGradient")
                   && groups[i].getAnnotationColours() != null)
@@ -2700,7 +2799,7 @@ public class Jalview2XML
         for (int s = 0; s < groups[i].getSeqCount(); s++)
         {
           String seqId = groups[i].getSeq(s) + "";
-          jalview.datamodel.SequenceI ts = (jalview.datamodel.SequenceI) seqRefIds
+          jalview.datamodel.SequenceI ts = seqRefIds
                   .get(seqId);
 
           if (ts != null)
@@ -2872,7 +2971,7 @@ public class Jalview2XML
 
     if (isnewview)
     {
-      af = loadViewport(file, JSEQ, hiddenSeqs, al, jms, view,
+      af = loadViewport(file, jseqs, hiddenSeqs, al, jms, view,
               uniqueSeqSetId, viewId, autoAlan);
       av = af.viewport;
       ap = af.alignPanel;
@@ -2957,358 +3056,460 @@ public class Jalview2XML
     // //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();
+      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 < JSEQ.length; i++)
+    for (int i = 0; i < jseqs.length; i++)
+    {
+      if (jseqs[i].getPdbidsCount() > 0)
       {
-        if (JSEQ[i].getPdbidsCount() > 0)
+        Pdbids[] ids = jseqs[i].getPdbids();
+        for (int p = 0; p < ids.length; p++)
         {
-          Pdbids[] ids = JSEQ[i].getPdbids();
-          for (int p = 0; p < ids.length; p++)
+          final int structureStateCount = ids[p].getStructureStateCount();
+          for (int s = 0; s < structureStateCount; s++)
           {
-            for (int s = 0; s < ids[p].getStructureStateCount(); 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)
             {
-              // 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
-              }
+              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
-              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())
+            // 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[1] = ids[p].getStructureState(s).getContent();
-                }
+                jmoldat.stateData = structureState.getContent();
               }
-              if (ids[p].getFile() != null)
+            }
+            if (ids[p].getFile() != null)
+            {
+              File mapkey = new File(ids[p].getFile());
+              Object[] seqstrmaps = jmoldat.fileData.get(mapkey);
+              if (seqstrmaps == 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
-                }
+                jmoldat.fileData.put(mapkey,
+                        seqstrmaps = new Object[]
+                        { pdbFile, ids[p].getId(), new Vector(),
+                            new Vector() });
               }
-              else
+              if (!((Vector) seqstrmaps[2]).contains(seq))
               {
-                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);
+                ((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);
+        }
+      }
 
-        // Instantiate the associated Jmol views
-        for (Entry<String, Object[]> entry : jmolViewIds.entrySet())
+      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()
         {
-          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
+          @Override
+          public void run()
           {
+            JalviewStructureDisplayI sview = null;
             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)
+              // 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)
             {
-              if (sviewid != null
-                      && ((AppJmol) frames[f]).getViewId().equals(sviewid))
+              new OOMWarning("restoring structure view for PDB id " + id,
+                      (OutOfMemoryError) ex.getCause());
+              if (sview != null && sview.isVisible())
               {
-                // 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];
+                sview.closeViewer();
+                sview.setVisible(false);
+                sview.dispose();
               }
             }
           }
+        });
+      } catch (InvocationTargetException ex)
+      {
+        warn("Unexpected error when opening Jmol view.", ex);
 
-          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("\"");
+      } catch (InterruptedException e)
+      {
+        // e.printStackTrace();
+      }
+    }
+  }
 
-              }
-              newFileLoc.append(";");
-            }
+  /**
+   * 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;
+  }
 
-            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);
+  /**
+   * 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.
 
-              } catch (InterruptedException e)
-              {
-                // e.printStackTrace();
-              }
-            }
+    final boolean useinViewerSuperpos = svattrib.alignWithPanel;
+    final boolean usetoColourbyseq = svattrib.colourWithAlignPanel;
+    final boolean viewerColouring = svattrib.colourByViewer;
+    Map<File, Object[]> oldFiles = svattrib.fileData;
 
-          }
-          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 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);
+    }
+  }
 
-            // 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);
-            }
-          }
+  /**
+   * 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
+      {
+        frames = Desktop.desktop.getAllFrames();
+      } catch (ArrayIndexOutOfBoundsException e)
+      {
+        // occasional No such child exceptions are thrown here...
+        try
+        {
+          Thread.sleep(10);
+        } catch (InterruptedException f)
+        {
         }
       }
-    }
-    // and finally return.
-    return af;
+    } while (frames == null);
+    return frames;
   }
 
   /**
@@ -3500,7 +3701,7 @@ public class Jalview2XML
     {
       if (view.getBgColour().startsWith("ucs"))
       {
-        cs = GetUserColourScheme(jms, view.getBgColour());
+        cs = getUserColourScheme(jms, view.getBgColour());
       }
       else if (view.getBgColour().startsWith("Annotation"))
       {
@@ -3604,7 +3805,7 @@ public class Jalview2XML
     // recover featre settings
     if (jms.getFeatureSettings() != null)
     {
-      af.viewport.featuresDisplayed = new Hashtable();
+      af.viewport.setFeaturesDisplayed(new Hashtable());
       String[] renderOrder = new String[jms.getFeatureSettings()
               .getSettingCount()];
       for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
@@ -3656,7 +3857,7 @@ public class Jalview2XML
         }
         if (setting.getDisplay())
         {
-          af.viewport.featuresDisplayed.put(setting.getType(), new Integer(
+          af.viewport.getFeaturesDisplayed().put(setting.getType(), new Integer(
                   setting.getColour()));
         }
       }
@@ -3757,7 +3958,7 @@ public class Jalview2XML
           {
             cs = new AnnotationColourGradient(
                     annAlignment.getAlignmentAnnotation()[i],
-                    GetUserColourScheme(jms,
+                    getUserColourScheme(jms,
                             viewAnnColour.getColourScheme()),
                     viewAnnColour.getAboveThreshold());
           }
@@ -3968,7 +4169,7 @@ public class Jalview2XML
     return false;
   }
 
-  public void AddToSkipList(AlignFrame af)
+  public void addToSkipList(AlignFrame af)
   {
     if (skipList == null)
     {
@@ -3986,7 +4187,8 @@ public class Jalview2XML
     }
   }
 
-  private void recoverDatasetFor(SequenceSet vamsasSet, Alignment al)
+  private void recoverDatasetFor(SequenceSet vamsasSet, Alignment al,
+          boolean ignoreUnrefed)
   {
     jalview.datamodel.Alignment ds = getDatasetFor(vamsasSet.getDatasetId());
     Vector dseqs = null;
@@ -3998,7 +4200,7 @@ public class Jalview2XML
     for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
     {
       Sequence vamsasSeq = vamsasSet.getSequence(i);
-      ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs);
+      ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed);
     }
     // create a new dataset
     if (ds == null)
@@ -4011,7 +4213,7 @@ public class Jalview2XML
       addDatasetRef(vamsasSet.getDatasetId(), ds);
     }
     // set the dataset for the newly imported alignment.
-    if (al.getDataset() == null)
+    if (al.getDataset() == null && !ignoreUnrefed)
     {
       al.setDataset(ds);
     }
@@ -4027,7 +4229,7 @@ public class Jalview2XML
    *          vector to add new dataset sequence to
    */
   private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
-          AlignmentI ds, Vector dseqs)
+          AlignmentI ds, Vector dseqs, boolean ignoreUnrefed)
   {
     // JBP TODO: Check this is called for AlCodonFrames to support recovery of
     // xRef Codon Maps
@@ -4038,14 +4240,17 @@ public class Jalview2XML
     {
       dsq = sq.getDatasetSequence();
     }
-
+    if (sq == null && ignoreUnrefed)
+    {
+      return;
+    }
     String sqid = vamsasSeq.getDsseqid();
     if (dsq == null)
     {
       // need to create or add a new dataset sequence reference to this sequence
       if (sqid != null)
       {
-        dsq = (jalview.datamodel.SequenceI) seqRefIds.get(sqid);
+        dsq = seqRefIds.get(sqid);
       }
       // check again
       if (dsq == null)
@@ -4241,7 +4446,7 @@ public class Jalview2XML
           /**
            * recover from hash
            */
-          jmap.setTo((SequenceI) seqRefIds.get(dsfor));
+          jmap.setTo(seqRefIds.get(dsfor));
         }
         else
         {
@@ -4300,7 +4505,7 @@ public class Jalview2XML
           boolean keepSeqRefs)
   {
     initSeqRefs();
-    jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null,
+    jalview.schemabinding.version2.JalviewModel jm = saveState(ap, null,
             null);
 
     if (!keepSeqRefs)
@@ -4323,7 +4528,7 @@ public class Jalview2XML
 
     viewportsAdded = new Hashtable();
 
-    AlignFrame af = LoadFromObject(jm, null, false, null);
+    AlignFrame af = loadFromObject(jm, null, false, null);
     af.alignPanels.clear();
     af.closeMenuItem_actionPerformed(true);
 
@@ -4457,14 +4662,14 @@ public class Jalview2XML
         // register sequence object so the XML parser can recover it.
         if (seqRefIds == null)
         {
-          seqRefIds = new Hashtable();
+          seqRefIds = new HashMap<String, SequenceI>();
         }
         if (seqsToIds == null)
         {
-          seqsToIds = new IdentityHashMap();
+          seqsToIds = new IdentityHashMap<SequenceI, String>();
         }
-        seqRefIds.put(jv2vobj.get(jvobj).toString(), jvobj);
-        seqsToIds.put(jvobj, id);
+        seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
+        seqsToIds.put((SequenceI) jvobj, id);
       }
       else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
       {