Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / src / jalview / project / Jalview2XML.java
index 5106e45..e88a4a1 100644 (file)
  */
 package jalview.project;
 
+import static jalview.math.RotatableMatrix.Axis.X;
+import static jalview.math.RotatableMatrix.Axis.Y;
+import static jalview.math.RotatableMatrix.Axis.Z;
+
 import jalview.analysis.Conservation;
+import jalview.analysis.PCA;
+import jalview.analysis.scoremodels.ScoreModels;
+import jalview.analysis.scoremodels.SimilarityParams;
+import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureColourI;
 import jalview.api.ViewStyleI;
+import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.GeneLocus;
 import jalview.datamodel.GraphLine;
+import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.PDBEntry;
+import jalview.datamodel.Point;
 import jalview.datamodel.RnaViewerModel;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
@@ -49,17 +63,22 @@ import jalview.gui.AppVarna;
 import jalview.gui.ChimeraViewFrame;
 import jalview.gui.Desktop;
 import jalview.gui.FeatureRenderer;
-import jalview.gui.Jalview2XML_V1;
 import jalview.gui.JvOptionPane;
 import jalview.gui.OOMWarning;
+import jalview.gui.PCAPanel;
 import jalview.gui.PaintRefresher;
 import jalview.gui.SplitFrame;
 import jalview.gui.StructureViewer;
 import jalview.gui.StructureViewer.ViewerType;
 import jalview.gui.StructureViewerBase;
 import jalview.gui.TreePanel;
+import jalview.io.BackupFiles;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
+import jalview.io.HMMFile;
+import jalview.io.NewickFile;
+import jalview.math.Matrix;
+import jalview.math.MatrixI;
 import jalview.renderer.ResidueShaderI;
 import jalview.schemes.AnnotationColourGradient;
 import jalview.schemes.ColourSchemeI;
@@ -67,7 +86,6 @@ import jalview.schemes.ColourSchemeProperty;
 import jalview.schemes.FeatureColour;
 import jalview.schemes.ResidueProperties;
 import jalview.schemes.UserColourScheme;
-import jalview.structure.StructureSelectionManager;
 import jalview.structures.models.AAStructureBindingModel;
 import jalview.util.Format;
 import jalview.util.MessageManager;
@@ -76,12 +94,14 @@ import jalview.util.StringUtils;
 import jalview.util.jarInputStreamProvider;
 import jalview.util.matcher.Condition;
 import jalview.viewmodel.AlignmentViewport;
+import jalview.viewmodel.PCAModel;
 import jalview.viewmodel.ViewportRanges;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
-import jalview.ws.jws2.Jws2Discoverer;
+import jalview.ws.api.ServiceWithParameters;
+import jalview.ws.jws2.PreferredServiceRegistry;
 import jalview.ws.jws2.dm.AAConSettings;
-import jalview.ws.jws2.jabaws2.Jws2Instance;
 import jalview.ws.params.ArgumentI;
 import jalview.ws.params.AutoCalcSetting;
 import jalview.ws.params.WsParamSetI;
@@ -91,6 +111,8 @@ import jalview.xml.binding.jalview.Annotation;
 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
 import jalview.xml.binding.jalview.AnnotationColourScheme;
 import jalview.xml.binding.jalview.AnnotationElement;
+import jalview.xml.binding.jalview.DoubleMatrix;
+import jalview.xml.binding.jalview.DoubleVector;
 import jalview.xml.binding.jalview.Feature;
 import jalview.xml.binding.jalview.Feature.OtherData;
 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
@@ -105,6 +127,11 @@ import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
+import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
+import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
+import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
+import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
+import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
 import jalview.xml.binding.jalview.JalviewModel.Tree;
 import jalview.xml.binding.jalview.JalviewModel.UserColours;
 import jalview.xml.binding.jalview.JalviewModel.Viewport;
@@ -117,6 +144,7 @@ import jalview.xml.binding.jalview.MapListType.MapListTo;
 import jalview.xml.binding.jalview.Mapping;
 import jalview.xml.binding.jalview.NoValueColour;
 import jalview.xml.binding.jalview.ObjectFactory;
+import jalview.xml.binding.jalview.PcaDataType;
 import jalview.xml.binding.jalview.Pdbentry.Property;
 import jalview.xml.binding.jalview.Sequence;
 import jalview.xml.binding.jalview.Sequence.DBRef;
@@ -126,19 +154,21 @@ import jalview.xml.binding.jalview.ThresholdType;
 import jalview.xml.binding.jalview.VAMSAS;
 
 import java.awt.Color;
+import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Rectangle;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
-import java.lang.reflect.InvocationTargetException;
 import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -185,12 +215,29 @@ import javax.xml.stream.XMLStreamReader;
  */
 public class Jalview2XML
 {
+
+  // BH 2018 we add the .jvp binary extension to J2S so that
+  // it will declare that binary when we do the file save from the browser
+
+  static
+  {
+    Platform.addJ2SBinaryType(".jvp?");
+  }
+
   private static final String VIEWER_PREFIX = "viewer_";
 
   private static final String RNA_PREFIX = "rna_";
 
+  private static final String HMMER_PREFIX = "hmmer_";
+
   private static final String UTF_8 = "UTF-8";
 
+  /**
+   * prefix for recovering datasets for alignments with multiple views where
+   * non-existent dataset IDs were written for some views
+   */
+  private static final String UNIQSEQSETID = "uniqueSeqSetId.";
+
   // use this with nextCounter() to make unique names for entities
   private int counter = 0;
 
@@ -226,6 +273,64 @@ public class Jalview2XML
   private Map<RnaModel, String> rnaSessions = new HashMap<>();
 
   /**
+   * contains last error message (if any) encountered by XML loader.
+   */
+  String errorMessage = null;
+
+  /**
+   * flag to control whether the Jalview2XML_V1 parser should be deferred to if
+   * exceptions are raised during project XML parsing
+   */
+  public boolean attemptversion1parse = false;
+
+  /*
+   * JalviewJS only -- to allow read file bytes to be saved in the
+   * created AlignFrame, allowing File | Reload of a project file to work
+   * 
+   * BH 2019 JAL-3436
+   */
+  private File jarFile;
+
+  /**
+   * A helper method for safely using the value of an optional attribute that
+   * may be null if not present in the XML. Answers the boolean value, or false
+   * if null.
+   * 
+   * @param b
+   * @return
+   */
+  public static boolean safeBoolean(Boolean b)
+  {
+    return b == null ? false : b.booleanValue();
+  }
+
+  /**
+   * A helper method for safely using the value of an optional attribute that
+   * may be null if not present in the XML. Answers the integer value, or zero
+   * if null.
+   * 
+   * @param i
+   * @return
+   */
+  public static int safeInt(Integer i)
+  {
+    return i == null ? 0 : i.intValue();
+  }
+
+  /**
+   * A helper method for safely using the value of an optional attribute that
+   * may be null if not present in the XML. Answers the float value, or zero if
+   * null.
+   * 
+   * @param f
+   * @return
+   */
+  public static float safeFloat(Float f)
+  {
+    return f == null ? 0f : f.floatValue();
+  }
+
+  /**
    * create/return unique hash string for sq
    * 
    * @param sq
@@ -346,7 +451,7 @@ public class Jalview2XML
    * @param _jmap
    * @return
    */
-  public SeqFref newMappingRef(final String sref,
+  protected SeqFref newMappingRef(final String sref,
           final jalview.datamodel.Mapping _jmap)
   {
     SeqFref fref = new SeqFref(sref, "Mapping")
@@ -368,7 +473,7 @@ public class Jalview2XML
     return fref;
   }
 
-  public SeqFref newAlcodMapRef(final String sref,
+  protected SeqFref newAlcodMapRef(final String sref,
           final AlignedCodonFrame _cf,
           final jalview.datamodel.Mapping _jmap)
   {
@@ -383,7 +488,7 @@ public class Jalview2XML
       public boolean isResolvable()
       {
         return super.isResolvable() && mp.getTo() != null;
-      };
+      }
 
       @Override
       boolean resolve()
@@ -400,7 +505,7 @@ public class Jalview2XML
     return fref;
   }
 
-  public void resolveFrefedSequences()
+  protected void resolveFrefedSequences()
   {
     Iterator<SeqFref> nextFref = frefedSequence.iterator();
     int toresolve = frefedSequence.size();
@@ -483,24 +588,30 @@ public class Jalview2XML
   public void saveState(File statefile)
   {
     FileOutputStream fos = null;
+
     try
     {
+
       fos = new FileOutputStream(statefile);
+
       JarOutputStream jout = new JarOutputStream(fos);
       saveState(jout);
+      fos.close();
 
     } catch (Exception e)
     {
+      Cache.log.error("Couln't write Jalview state to " + statefile, e);
       // TODO: inform user of the problem - they need to know if their data was
       // not saved !
       if (errorMessage == null)
       {
-        errorMessage = "Couldn't write Jalview Archive to output file '"
+        errorMessage = "Did't write Jalview Archive to output file '"
                 + statefile + "' - See console error log for details";
       }
       else
       {
-        errorMessage += "(output file was '" + statefile + "')";
+        errorMessage += "(Didn't write Jalview Archive to output file '"
+                + statefile + ")";
       }
       e.printStackTrace();
     } finally
@@ -539,13 +650,14 @@ public class Jalview2XML
    * core method for storing state for a set of AlignFrames.
    * 
    * @param frames
-   *          - frames involving all data to be exported (including containing
-   *          splitframes)
+   *          - frames involving all data to be exported (including those
+   *          contained in splitframes, though not the split frames themselves)
    * @param jout
    *          - project output stream
    */
   private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
   {
+
     Hashtable<String, AlignFrame> dsses = new Hashtable<>();
 
     /*
@@ -568,21 +680,22 @@ public class Jalview2XML
       for (int i = frames.size() - 1; i > -1; i--)
       {
         AlignFrame af = frames.get(i);
+        AlignViewport vp = af.getViewport();
         // skip ?
         if (skipList != null && skipList
-                .containsKey(af.getViewport().getSequenceSetId()))
+                .containsKey(vp.getSequenceSetId()))
         {
           continue;
         }
 
         String shortName = makeFilename(af, shortNames);
 
-        int apSize = af.getAlignPanels().size();
-
+        AlignmentI alignment = vp.getAlignment();
+        List<? extends AlignmentViewPanel> panels = af.getAlignPanels();
+        int apSize = panels.size();
         for (int ap = 0; ap < apSize; ap++)
-        {
-          AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
-                  .get(ap);
+          {
+          AlignmentPanel apanel = (AlignmentPanel) panels.get(ap);
           String fileName = apSize == 1 ? shortName : ap + shortName;
           if (!fileName.endsWith(".xml"))
           {
@@ -590,11 +703,17 @@ public class Jalview2XML
           }
 
           saveState(apanel, fileName, jout, viewIds);
-
-          String dssid = getDatasetIdRef(
-                  af.getViewport().getAlignment().getDataset());
+        }
+        if (apSize > 0)
+        {
+          // BH moved next bit out of inner loop, not that it really matters.
+          // so we are testing to make sure we actually have an alignment,
+          // apparently.
+          String dssid = getDatasetIdRef(alignment.getDataset());
           if (!dsses.containsKey(dssid))
           {
+            // We have not already covered this data by reference from another
+            // frame.
             dsses.put(dssid, af);
           }
         }
@@ -609,7 +728,6 @@ public class Jalview2XML
       } catch (Exception foo)
       {
       }
-      ;
       jout.close();
     } catch (Exception ex)
     {
@@ -670,7 +788,12 @@ public class Jalview2XML
   {
     try
     {
-      FileOutputStream fos = new FileOutputStream(jarFile);
+      // create backupfiles object and get new temp filename destination
+      boolean doBackup = BackupFiles.getEnabled();
+      BackupFiles backupfiles = doBackup ? new BackupFiles(jarFile) : null;
+      FileOutputStream fos = new FileOutputStream(doBackup ? 
+              backupfiles.getTempFilePath() : jarFile);
+
       JarOutputStream jout = new JarOutputStream(fos);
       List<AlignFrame> frames = new ArrayList<>();
 
@@ -690,9 +813,16 @@ public class Jalview2XML
       } catch (Exception foo)
       {
       }
-      ;
       jout.close();
-      return true;
+      boolean success = true;
+
+      if (doBackup)
+      {
+        backupfiles.setWriteSuccess(success);
+        success = backupfiles.rollBackupsAndRenameTempFile();
+      }
+
+      return success;
     } catch (Exception ex)
     {
       errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
@@ -701,10 +831,22 @@ public class Jalview2XML
     }
   }
 
+  /**
+   * Each AlignFrame has a single data set associated with it. Note that none of
+   * these frames are split frames, because Desktop.getAlignFrames() collects
+   * top and bottom separately here.
+   * 
+   * @param dsses
+   * @param fileName
+   * @param jout
+   */
   private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
           String fileName, JarOutputStream jout)
   {
 
+    // Note that in saveAllFrames we have associated each specific dataset to
+    // ONE of its associated frames.
+
     for (String dssids : dsses.keySet())
     {
       AlignFrame _af = dsses.get(dssids);
@@ -731,7 +873,7 @@ public class Jalview2XML
    * @param out
    *          jar entry name
    */
-  public JalviewModel saveState(AlignmentPanel ap, String fileName,
+  protected JalviewModel saveState(AlignmentPanel ap, String fileName,
           JarOutputStream jout, List<String> viewIds)
   {
     return saveState(ap, fileName, false, jout, viewIds);
@@ -753,7 +895,7 @@ public class Jalview2XML
    * @param out
    *          jar entry name
    */
-  public JalviewModel saveState(AlignmentPanel ap, String fileName,
+  protected JalviewModel saveState(AlignmentPanel ap, String fileName,
           boolean storeDS, JarOutputStream jout, List<String> viewIds)
   {
     if (viewIds == null)
@@ -967,6 +1109,9 @@ public class Jalview2XML
         jseq.getFeatures().add(features);
       }
 
+      /*
+       * save PDB entries for sequence
+       */
       if (jdatasq.getAllPDBEntries() != null)
       {
         Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
@@ -985,7 +1130,7 @@ public class Jalview2XML
            * only view *should* be coped with sensibly.
            */
           // This must have been loaded, is it still visible?
-          JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+          JInternalFrame[] frames = Desktop.getDesktopPane().getAllFrames();
           String matchedFile = null;
           for (int f = frames.length - 1; f > -1; f--)
           {
@@ -1059,6 +1204,11 @@ public class Jalview2XML
 
       saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
 
+      if (jds.hasHMMProfile())
+      {
+        saveHmmerProfile(jout, jseq, jds);
+      }
+
       // jms.addJSeq(jseq);
       object.getJSeq().add(jseq);
     }
@@ -1137,9 +1287,9 @@ public class Jalview2XML
     {
       // FIND ANY ASSOCIATED TREES
       // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
-      if (Desktop.desktop != null)
+      if (Desktop.getDesktopPane() != null)
       {
-        JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+        JInternalFrame[] frames = Desktop.getDesktopPane().getAllFrames();
 
         for (int t = 0; t < frames.length; t++)
         {
@@ -1169,6 +1319,9 @@ public class Jalview2XML
               tree.setXpos(tp.getX());
               tree.setYpos(tp.getY());
               tree.setId(makeHashCode(tp, null));
+              tree.setLinkToAllViews(
+                      tp.getTreeCanvas().isApplyToAllViews());
+
               // jms.addTree(tree);
               object.getTree().add(tree);
             }
@@ -1177,6 +1330,24 @@ public class Jalview2XML
       }
     }
 
+    /*
+     * save PCA viewers
+     */
+    if (!storeDS && Desktop.getDesktopPane() != null)
+    {
+      for (JInternalFrame frame : Desktop.getDesktopPane().getAllFrames())
+      {
+        if (frame instanceof PCAPanel)
+        {
+          PCAPanel panel = (PCAPanel) frame;
+          if (panel.getAlignViewport().getAlignment() == jal)
+          {
+            savePCA(panel, object);
+          }
+        }
+      }
+    }
+
     // SAVE ANNOTATIONS
     /**
      * store forward refs from an annotationRow to any groups
@@ -1395,11 +1566,14 @@ public class Jalview2XML
       view.setFollowHighlight(av.isFollowHighlight());
       view.setFollowSelection(av.followSelection);
       view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
+      view.setShowComplementFeatures(av.isShowComplementFeatures());
+      view.setShowComplementFeaturesOnTop(
+              av.isShowComplementFeaturesOnTop());
       if (av.getFeaturesDisplayed() != null)
       {
         FeatureSettings fs = new FeatureSettings();
 
-        FeatureRenderer fr = ap.getSeqPanel().seqCanvas
+        FeatureRendererModel fr = ap.getSeqPanel().seqCanvas
                 .getFeatureRenderer();
         String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
 
@@ -1555,6 +1729,7 @@ public class Jalview2XML
       // using save and then load
       try
       {
+       fileName = fileName.replace('\\', '/');
         System.out.println("Writing jar entry " + fileName);
         JarEntry entry = new JarEntry(fileName);
         jout.putNextEntry(entry);
@@ -1582,6 +1757,228 @@ public class Jalview2XML
     }
     return object;
   }
+  /**
+   * Saves the HMMER profile associated with the sequence as a file in the jar,
+   * in HMMER format, and saves the name of the file as a child element of the
+   * XML sequence element
+   * 
+   * @param jout
+   * @param xmlSeq
+   * @param seq
+   */
+  protected void saveHmmerProfile(JarOutputStream jout, JSeq xmlSeq,
+          SequenceI seq)
+  {
+    HiddenMarkovModel profile = seq.getHMM();
+    if (profile == null)
+    {
+      warn("Want to save HMM profile for " + seq.getName()
+              + " but none found");
+      return;
+    }
+    HMMFile hmmFile = new HMMFile(profile);
+    String hmmAsString = hmmFile.print();
+    String jarEntryName = HMMER_PREFIX + nextCounter();
+    try
+    {
+      writeJarEntry(jout, jarEntryName, hmmAsString.getBytes());
+      xmlSeq.setHmmerProfile(jarEntryName);
+    } catch (IOException e)
+    {
+      warn("Error saving HMM profile: " + e.getMessage());
+    }
+  }
+
+    
+  /**
+   * Writes PCA viewer attributes and computed values to an XML model object and
+   * adds it to the JalviewModel. Any exceptions are reported by logging.
+   */
+  protected void savePCA(PCAPanel panel, JalviewModel object)
+  {
+    try
+    {
+      PcaViewer viewer = new PcaViewer();
+      viewer.setHeight(panel.getHeight());
+      viewer.setWidth(panel.getWidth());
+      viewer.setXpos(panel.getX());
+      viewer.setYpos(panel.getY());
+      viewer.setTitle(panel.getTitle());
+      PCAModel pcaModel = panel.getPcaModel();
+      viewer.setScoreModelName(pcaModel.getScoreModelName());
+      viewer.setXDim(panel.getSelectedDimensionIndex(X));
+      viewer.setYDim(panel.getSelectedDimensionIndex(Y));
+      viewer.setZDim(panel.getSelectedDimensionIndex(Z));
+      viewer.setBgColour(
+              panel.getRotatableCanvas().getBackgroundColour().getRGB());
+      viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
+      float[] spMin = panel.getRotatableCanvas().getSeqMin();
+      SeqPointMin spmin = new SeqPointMin();
+      spmin.setXPos(spMin[0]);
+      spmin.setYPos(spMin[1]);
+      spmin.setZPos(spMin[2]);
+      viewer.setSeqPointMin(spmin);
+      float[] spMax = panel.getRotatableCanvas().getSeqMax();
+      SeqPointMax spmax = new SeqPointMax();
+      spmax.setXPos(spMax[0]);
+      spmax.setYPos(spMax[1]);
+      spmax.setZPos(spMax[2]);
+      viewer.setSeqPointMax(spmax);
+      viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
+      viewer.setLinkToAllViews(
+              panel.getRotatableCanvas().isApplyToAllViews());
+      SimilarityParamsI sp = pcaModel.getSimilarityParameters();
+      viewer.setIncludeGaps(sp.includeGaps());
+      viewer.setMatchGaps(sp.matchGaps());
+      viewer.setIncludeGappedColumns(sp.includeGappedColumns());
+      viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
+
+      /*
+       * sequence points on display
+       */
+      for (jalview.datamodel.SequencePoint spt : pcaModel
+              .getSequencePoints())
+      {
+        SequencePoint point = new SequencePoint();
+        point.setSequenceRef(seqHash(spt.getSequence()));
+        point.setXPos(spt.coord.x);
+        point.setYPos(spt.coord.y);
+        point.setZPos(spt.coord.z);
+        viewer.getSequencePoint().add(point);
+      }
+
+      /*
+       * (end points of) axes on display
+       */
+      for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
+      {
+
+        Axis axis = new Axis();
+        axis.setXPos(p.x);
+        axis.setYPos(p.y);
+        axis.setZPos(p.z);
+        viewer.getAxis().add(axis);
+      }
+
+      /*
+       * raw PCA data (note we are not restoring PCA inputs here -
+       * alignment view, score model, similarity parameters)
+       */
+      PcaDataType data = new PcaDataType();
+      viewer.setPcaData(data);
+      PCA pca = pcaModel.getPcaData();
+
+      DoubleMatrix pm = new DoubleMatrix();
+      saveDoubleMatrix(pca.getPairwiseScores(), pm);
+      data.setPairwiseMatrix(pm);
+
+      DoubleMatrix tm = new DoubleMatrix();
+      saveDoubleMatrix(pca.getTridiagonal(), tm);
+      data.setTridiagonalMatrix(tm);
+
+      DoubleMatrix eigenMatrix = new DoubleMatrix();
+      data.setEigenMatrix(eigenMatrix);
+      saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
+
+      object.getPcaViewer().add(viewer);
+    } catch (Throwable t)
+    {
+      Cache.log.error("Error saving PCA: " + t.getMessage());
+    }
+  }
+
+  /**
+   * Stores values from a matrix into an XML element, including (if present) the
+   * D or E vectors
+   * 
+   * @param m
+   * @param xmlMatrix
+   * @see #loadDoubleMatrix(DoubleMatrix)
+   */
+  protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
+  {
+    xmlMatrix.setRows(m.height());
+    xmlMatrix.setColumns(m.width());
+    for (int i = 0; i < m.height(); i++)
+    {
+      DoubleVector row = new DoubleVector();
+      for (int j = 0; j < m.width(); j++)
+      {
+        row.getV().add(m.getValue(i, j));
+      }
+      xmlMatrix.getRow().add(row);
+    }
+    if (m.getD() != null)
+    {
+      DoubleVector dVector = new DoubleVector();
+      for (double d : m.getD())
+      {
+        dVector.getV().add(d);
+      }
+      xmlMatrix.setD(dVector);
+    }
+    if (m.getE() != null)
+    {
+      DoubleVector eVector = new DoubleVector();
+      for (double e : m.getE())
+      {
+        eVector.getV().add(e);
+      }
+      xmlMatrix.setE(eVector);
+    }
+  }
+
+  /**
+   * Loads XML matrix data into a new Matrix object, including the D and/or E
+   * vectors (if present)
+   * 
+   * @param mData
+   * @return
+   * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
+   */
+  protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
+  {
+    int rows = mData.getRows();
+    double[][] vals = new double[rows][];
+
+    for (int i = 0; i < rows; i++)
+    {
+      List<Double> dVector = mData.getRow().get(i).getV();
+      vals[i] = new double[dVector.size()];
+      int dvi = 0;
+      for (Double d : dVector)
+      {
+        vals[i][dvi++] = d;
+      }
+    }
+
+    MatrixI m = new Matrix(vals);
+
+    if (mData.getD() != null)
+    {
+      List<Double> dVector = mData.getD().getV();
+      double[] vec = new double[dVector.size()];
+      int dvi = 0;
+      for (Double d : dVector)
+      {
+        vec[dvi++] = d;
+      }
+      m.setD(vec);
+    }
+    if (mData.getE() != null)
+    {
+      List<Double> dVector = mData.getE().getV();
+      double[] vec = new double[dVector.size()];
+      int dvi = 0;
+      for (Double d : dVector)
+      {
+        vec[dvi++] = d;
+      }
+      m.setE(vec);
+    }
+
+    return m;
+  }
 
   /**
    * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
@@ -1611,11 +2008,11 @@ public class Jalview2XML
           final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
           boolean storeDataset)
   {
-    if (Desktop.desktop == null)
+    if (Desktop.getDesktopPane() == null)
     {
       return;
     }
-    JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+    JInternalFrame[] frames = Desktop.getDesktopPane().getAllFrames();
     for (int f = frames.length - 1; f > -1; f--)
     {
       if (frames[f] instanceof AppVarna)
@@ -1735,6 +2132,7 @@ public class Jalview2XML
   {
     if (jout != null)
     {
+      jarEntryName = jarEntryName.replace('\\','/');
       System.out.println("Writing jar entry " + jarEntryName);
       jout.putNextEntry(new JarEntry(jarEntryName));
       DataOutputStream dout = new DataOutputStream(jout);
@@ -1786,9 +2184,9 @@ public class Jalview2XML
       }
       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());
+         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
@@ -2070,7 +2468,7 @@ public class Jalview2XML
     if (calcIdParam.getVersion().equals("1.0"))
     {
       final String[] calcIds = calcIdParam.getServiceURL().toArray(new String[0]);
-      Jws2Instance service = Jws2Discoverer.getDiscoverer()
+      ServiceWithParameters service = PreferredServiceRegistry.getRegistry()
               .getPreferredServiceFor(calcIds);
       if (service != null)
       {
@@ -2103,7 +2501,7 @@ public class Jalview2XML
           argList = parmSet.getArguments();
           parmSet = null;
         }
-        AAConSettings settings = new AAConSettings(
+        AutoCalcSetting settings = new AAConSettings(
                 calcIdParam.isAutoUpdate(), service, parmSet, argList);
         av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
                 calcIdParam.isNeedsUpdate());
@@ -2111,7 +2509,7 @@ public class Jalview2XML
       }
       else
       {
-        warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
+        warn("Cannot resolve a service for the parameters used in this project. Try configuring a server in the Web Services preferences tab.");
         return false;
       }
     }
@@ -2195,7 +2593,7 @@ public class Jalview2XML
     vamsasSeq.setName(jds.getName());
     vamsasSeq.setSequence(jds.getSequenceAsString());
     vamsasSeq.setDescription(jds.getDescription());
-    jalview.datamodel.DBRefEntry[] dbrefs = null;
+    List<DBRefEntry> dbrefs = null;
     if (jds.getDatasetSequence() != null)
     {
       vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
@@ -2211,21 +2609,29 @@ public class Jalview2XML
         parentseq = jds;
       }
     }
+
+    /*
+     * save any dbrefs; special subclass GeneLocus is flagged as 'locus'
+     */
     if (dbrefs != null)
     {
-      for (int d = 0; d < dbrefs.length; d++)
+      for (int d = 0, nd = dbrefs.size(); d < nd; d++)
       {
         DBRef dbref = new DBRef();
-        dbref.setSource(dbrefs[d].getSource());
-        dbref.setVersion(dbrefs[d].getVersion());
-        dbref.setAccessionId(dbrefs[d].getAccessionId());
-        if (dbrefs[d].hasMap())
+        DBRefEntry ref = dbrefs.get(d);
+        dbref.setSource(ref.getSource());
+        dbref.setVersion(ref.getVersion());
+        dbref.setAccessionId(ref.getAccessionId());
+        if (ref instanceof GeneLocus)
         {
-          Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
+          dbref.setLocus(true);
+        }
+        if (ref.hasMap())
+        {
+          Mapping mp = createVamsasMapping(ref.getMap(), parentseq,
                   jds, recurse);
           dbref.setMapping(mp);
         }
-        // vamsasSeq.addDBRef(dbref);
         vamsasSeq.getDBRef().add(dbref);
       }
     }
@@ -2401,23 +2807,12 @@ public class Jalview2XML
   }
 
   /**
-   * contains last error message (if any) encountered by XML loader.
-   */
-  String errorMessage = null;
-
-  /**
-   * flag to control whether the Jalview2XML_V1 parser should be deferred to if
-   * exceptions are raised during project XML parsing
-   */
-  public boolean attemptversion1parse = false;
-
-  /**
    * Load a jalview project archive from a jar file
    * 
    * @param file
    *          - HTTP URL or filename
    */
-  public AlignFrame loadJalviewAlign(final String file)
+  public AlignFrame loadJalviewAlign(final Object file)
   {
 
     jalview.gui.AlignFrame af = null;
@@ -2445,59 +2840,76 @@ public class Jalview2XML
     {
       try
       {
-        SwingUtilities.invokeAndWait(new Runnable()
+// was invokeAndWait
+         
+         // BH 2019 -- can't wait
+        SwingUtilities.invokeLater(new Runnable()
         {
           @Override
           public void run()
           {
             setLoadingFinishedForNewStructureViewers();
-          };
+          }
         });
       } catch (Exception x)
       {
         System.err.println("Error loading alignment: " + x.getMessage());
       }
     }
+    this.jarFile = null;
     return af;
   }
 
+       @SuppressWarnings("unused")
   private jarInputStreamProvider createjarInputStreamProvider(
-          final String file) throws MalformedURLException
+          final Object ofile) throws MalformedURLException
   {
-    URL url = null;
-    errorMessage = null;
-    uniqueSetSuffix = null;
-    seqRefIds = null;
-    viewportsAdded.clear();
-    frefedSequence = null;
-
-    if (file.startsWith("http://"))
-    {
-      url = new URL(file);
-    }
-    final URL _url = url;
-    return new jarInputStreamProvider()
+    try
     {
+      String file = (ofile instanceof File
+              ? ((File) ofile).getCanonicalPath()
+              : ofile.toString());
+      byte[] bytes = Platform.isJS() ? Platform.getFileBytes((File) ofile)
+              : null;
+      if (bytes != null)
+      {
+        this.jarFile = (File) ofile;
+      }
+      errorMessage = null;
+      uniqueSetSuffix = null;
+      seqRefIds = null;
+      viewportsAdded.clear();
+      frefedSequence = null;
 
-      @Override
-      public JarInputStream getJarInputStream() throws IOException
+      URL url = file.startsWith("http://") ? new URL(file) : null;
+      return new jarInputStreamProvider()
       {
-        if (_url != null)
+        @Override
+        public JarInputStream getJarInputStream() throws IOException
         {
-          return new JarInputStream(_url.openStream());
+          InputStream is = bytes != null ? new ByteArrayInputStream(bytes)
+                  : (url != null ? url.openStream()
+                          : new FileInputStream(file));
+          return new JarInputStream(is);
         }
-        else
+
+        @Override
+        public File getFile()
         {
-          return new JarInputStream(new FileInputStream(file));
+          return jarFile;
         }
-      }
 
-      @Override
-      public String getFilename()
-      {
-        return file;
-      }
-    };
+        @Override
+        public String getFilename()
+        {
+          return file;
+        }
+      };
+    } catch (IOException e)
+    {
+      e.printStackTrace();
+      return null;
+    }
   }
 
   /**
@@ -2523,13 +2935,20 @@ public class Jalview2XML
     AlignFrame af = null, _af = null;
     IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
     Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
-    final String file = jprovider.getFilename();
+    String fileName = jprovider.getFilename();
+    File file = jprovider.getFile();
+    List<AlignFrame> alignFrames = new ArrayList<>();
+
     try
     {
       JarInputStream jin = null;
       JarEntry jarentry = null;
       int entryCount = 1;
 
+
+      // Look for all the entry names ending with ".xml"
+      // This includes all panels and at least one frame.
+//      Platform.timeCheck(null, Platform.TIME_MARK);
       do
       {
         jin = jprovider.getJarInputStream();
@@ -2537,12 +2956,27 @@ public class Jalview2XML
         {
           jarentry = jin.getNextJarEntry();
         }
+        String name = (jarentry == null ? null : jarentry.getName());
 
-        if (jarentry != null && jarentry.getName().endsWith(".xml"))
+//        System.out.println("Jalview2XML opening " + name);
+        if (name != null && name.endsWith(".xml"))
         {
-          InputStreamReader in = new InputStreamReader(jin, UTF_8);
-          // JalviewModel object = new JalviewModel();
 
+          // DataSet for.... is read last.
+          
+          
+          // The question here is what to do with the two
+          // .xml files in the jvp file.
+          // Some number of them, "...Dataset for...", will be the
+          // Only AlignPanels and will have Viewport.
+          // One or more will be the source data, with the DBRefs.
+          //
+          // JVP file writing (above) ensures tha the AlignPanels are written
+          // first, then all relevant datasets (which are
+          // Jalview.datamodel.Alignment).
+          //
+
+//          Platform.timeCheck("Jalview2XML JAXB " + name, Platform.TIME_MARK);
           JAXBContext jc = JAXBContext
                   .newInstance("jalview.xml.binding.jalview");
           XMLStreamReader streamReader = XMLInputFactory.newInstance()
@@ -2550,19 +2984,25 @@ public class Jalview2XML
           javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
           JAXBElement<JalviewModel> jbe = um
                   .unmarshal(streamReader, JalviewModel.class);
-          JalviewModel object = jbe.getValue();
+          JalviewModel model = jbe.getValue();
 
-          /*
-          Unmarshaller unmar = new Unmarshaller(object);
-          unmar.setValidation(false);
-          object = (JalviewModel) unmar.unmarshal(in);
-          */
           if (true) // !skipViewport(object))
           {
-            _af = loadFromObject(object, file, true, jprovider);
-            if (_af != null && object.getViewport().size() > 0)
-            // getJalviewModelSequence().getViewportCount() > 0)
+            // Q: Do we have to load from the model, even if it
+            // does not have a viewport, could we discover that early on?
+            // Q: Do we need to load this object?
+            _af = loadFromObject(model, fileName, file, true, jprovider);
+//            Platform.timeCheck("Jalview2XML.loadFromObject",
+            // Platform.TIME_MARK);
+
+            if (_af != null)
             {
+              alignFrames.add(_af);
+            }
+            if (_af != null && model.getViewport().size() > 0)
+            {
+
+              // That is, this is one of the AlignmentPanel models
               if (af == null)
               {
                 // store a reference to the first view
@@ -2594,7 +3034,7 @@ public class Jalview2XML
     } catch (IOException ex)
     {
       ex.printStackTrace();
-      errorMessage = "Couldn't locate Jalview XML file : " + file;
+      errorMessage = "Couldn't locate Jalview XML file : " + fileName;
       System.err.println(
               "Exception whilst loading jalview XML file : " + ex + "\n");
     } catch (Exception ex)
@@ -2603,20 +3043,11 @@ public class Jalview2XML
       ex.printStackTrace(System.err);
       if (attemptversion1parse)
       {
-        // Is Version 1 Jar file?
-        try
-        {
-          af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
-        } catch (Exception ex2)
-        {
-          System.err.println("Exception whilst loading as jalviewXMLV1:");
-          ex2.printStackTrace();
-          af = null;
-        }
+        // used to attempt to parse as V1 castor-generated xml
       }
-      if (Desktop.instance != null)
+      if (Desktop.getInstance() != null)
       {
-        Desktop.instance.stopLoading();
+        Desktop.getInstance().stopLoading();
       }
       if (af != null)
       {
@@ -2633,6 +3064,13 @@ public class Jalview2XML
       errorMessage = "Out of memory loading jalview XML file";
       System.err.println("Out of memory whilst loading jalview XML file");
       e.printStackTrace();
+    } finally
+    {
+      for (AlignFrame alf : alignFrames)
+      {
+        alf.alignPanel.setHoldRepaint(false);
+      }
+
     }
 
     /*
@@ -2644,7 +3082,7 @@ public class Jalview2XML
      */
     for (AlignFrame fr : gatherToThisFrame.values())
     {
-      Desktop.instance.gatherViews(fr);
+      Desktop.getInstance().gatherViews(fr);
     }
 
     restoreSplitFrames();
@@ -2652,8 +3090,7 @@ public class Jalview2XML
     {
       if (ds.getCodonFrames() != null)
       {
-        StructureSelectionManager
-                .getStructureSelectionManager(Desktop.instance)
+        Desktop.getStructureSelectionManager()
                 .registerMappings(ds.getCodonFrames());
       }
     }
@@ -2662,9 +3099,9 @@ public class Jalview2XML
       reportErrors();
     }
 
-    if (Desktop.instance != null)
+    if (Desktop.getInstance() != null)
     {
-      Desktop.instance.stopLoading();
+      Desktop.getInstance().stopLoading();
     }
 
     return af;
@@ -2732,8 +3169,8 @@ public class Jalview2XML
       if (!addedToSplitFrames.contains(af))
       {
         Viewport view = candidate.getKey();
-        Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
-                view.getHeight());
+        Desktop.addInternalFrame(af, view.getTitle(),
+                safeInt(view.getWidth()), safeInt(view.getHeight()));
         af.setMenusForViewport();
         System.err.println("Failed to restore view " + view.getTitle()
                 + " to split frame");
@@ -2745,7 +3182,7 @@ public class Jalview2XML
      */
     for (SplitFrame sf : gatherTo)
     {
-      Desktop.instance.gatherViews(sf);
+      Desktop.getInstance().gatherViews(sf);
     }
 
     splitFrameCandidates.clear();
@@ -2804,7 +3241,7 @@ public class Jalview2XML
           @Override
           public void run()
           {
-            JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+            JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
                     finalErrorMessage,
                     "Error " + (saving ? "saving" : "loading")
                             + " Jalview file",
@@ -2963,24 +3400,28 @@ public class Jalview2XML
   }
 
   /**
-   * Load alignment frame from jalview XML DOM object
+   * Load alignment frame from jalview XML DOM object. For a DOM object that
+   * includes one or more Viewport elements (one with a title that does NOT
+   * contain "Dataset for"), create the frame.
    * 
    * @param jalviewModel
    *          DOM
-   * @param file
+   * @param fileName
    *          filename source string
+   * @param file 
    * @param loadTreesAndStructures
    *          when false only create Viewport
    * @param jprovider
    *          data source provider
    * @return alignment frame created from view stored in DOM
    */
-  AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
-          boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
+  AlignFrame loadFromObject(JalviewModel jalviewModel, String fileName,
+          File file, boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
   {
     SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet().get(0);
     List<Sequence> vamsasSeqs = vamsasSet.getSequence();
 
+
     // JalviewModelSequence jms = object.getJalviewModelSequence();
 
     // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
@@ -2990,6 +3431,28 @@ public class Jalview2XML
             : null;
 
     // ////////////////////////////////
+    // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
+    //
+    //
+    // 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
+
+    /**
+     * used to resolve correct alignment dataset for alignments with multiple
+     * views
+     */
+    String uniqueSeqSetId = null;
+    String viewId = null;
+    if (view != null)
+    {
+      uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
+      viewId = (view.getId() == null ? null
+              : view.getId() + uniqueSetSuffix);
+    }
+
+    // ////////////////////////////////
     // LOAD SEQUENCES
 
     List<SequenceI> hiddenSeqs = null;
@@ -3016,8 +3479,10 @@ public class Jalview2XML
                   || tmpSeq.getEnd() != jseq.getEnd())
           {
             System.err.println(
-                    "Warning JAL-2154 regression: updating start/end for sequence "
-                            + tmpSeq.toString() + " to " + jseq);
+                    String.format("Warning JAL-2154 regression: updating start/end for sequence %s from %d/%d to %d/%d",
+                            tmpSeq.getName(), tmpSeq.getStart(),
+                            tmpSeq.getEnd(), jseq.getStart(),
+                            jseq.getEnd()));
           }
         }
         else
@@ -3057,8 +3522,7 @@ public class Jalview2XML
         vi++;
       }
 
-      if (jseq.isViewreference() != null
-              && jseq.isViewreference().booleanValue())
+      if (safeBoolean(jseq.isViewreference()))
       {
         referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
       }
@@ -3111,7 +3575,7 @@ public class Jalview2XML
 
       // finally, verify all data in vamsasSet is actually present in al
       // passing on flag indicating if it is actually a stored dataset
-      recoverDatasetFor(vamsasSet, al, isdsal);
+      recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
     }
 
     if (referenceseqForView != null)
@@ -3150,7 +3614,7 @@ public class Jalview2XML
             Feature feat = features.get(f);
             SequenceFeature sf = new SequenceFeature(feat.getType(),
                     feat.getDescription(), feat.getBegin(), feat.getEnd(),
-                    feat.getScore(), feat.getFeatureGroup());
+                    safeFloat(feat.getScore()), feat.getFeatureGroup());
             sf.setStatus(feat.getStatus());
 
             /*
@@ -3253,8 +3717,7 @@ public class Jalview2XML
             {
               entry.setProperty(prop.getName(), prop.getValue());
             }
-            StructureSelectionManager
-                    .getStructureSelectionManager(Desktop.instance)
+            Desktop.getStructureSelectionManager()
                     .registerPDBEntry(entry);
             // adds PDBEntry to datasequence's set (since Jalview 2.10)
             if (al.getSequenceAt(i).getDatasetSequence() != null)
@@ -3267,6 +3730,17 @@ public class Jalview2XML
             }
           }
         }
+
+        /*
+         * load any HMMER profile
+         */
+        // TODO fix this
+
+        String hmmJarFile = jseqs.get(i).getHmmerProfile();
+        if (hmmJarFile != null && jprovider != null)
+        {
+          loadHmmerProfile(jprovider, hmmJarFile, al.getSequenceAt(i));
+        }
       }
     } // end !multipleview
 
@@ -3344,22 +3818,21 @@ public class Jalview2XML
           // annotation.setAutoCalculated(true);
           // }
         }
-        if (autoForView || // (annotation.hasAutoCalculated() &&
-                annotation.isAutoCalculated())
+        if (autoForView || annotation.isAutoCalculated())
         {
           // remove ID - we don't recover annotation from other views for
           // view-specific annotation
           annotation.setId(null);
         }
 
-        // set visiblity for other annotation in this view
+        // set visibility for other annotation in this view
         String annotationId = annotation.getId();
         if (annotationId != null && annotationIds.containsKey(annotationId))
         {
           AlignmentAnnotation jda = annotationIds.get(annotationId);
           // in principle Visible should always be true for annotation displayed
           // in multiple views
-          if (annotation.isVisible() != null) // annotation.hasVisible())
+          if (annotation.isVisible() != null)
           {
             jda.visible = annotation.isVisible();
           }
@@ -3386,8 +3859,7 @@ public class Jalview2XML
               continue;
             }
 
-            float value = annElement.getValue() == null ? 0f
-                    : annElement.getValue().floatValue();
+            float value = safeFloat(annElement.getValue());
             anot[anpos] = new jalview.datamodel.Annotation(
                     annElement.getDisplayCharacter(),
                     annElement.getDescription(),
@@ -3399,22 +3871,14 @@ public class Jalview2XML
                                                     .getSecondaryStructure()
                                                     .charAt(0),
                     value);
-            // JBPNote: Consider verifying dataflow for IO of secondary
-            // structure annotation read from Stockholm files
-            // this was added to try to ensure that
-            // if (anot[ annElement.getPosition()].secondaryStructure>' ')
-            // {
-            // anot[ annElement.getPosition()].displayCharacter = "";
-            // }
-            final int colourValue = annElement.getColour() == null ? 0
-                    : annElement.getColour().intValue();
-            anot[anpos].colour = new java.awt.Color(colourValue);
+            anot[anpos].colour = new Color(safeInt(annElement.getColour()));
             if (firstColour == null)
             {
               firstColour = anot[anpos].colour;
             }
           }
         }
+        // create the new AlignmentAnnotation
         jalview.datamodel.AlignmentAnnotation jaa = null;
 
         if (annotation.isGraph())
@@ -3425,18 +3889,17 @@ public class Jalview2XML
           // }
           jaa = new jalview.datamodel.AlignmentAnnotation(
                   annotation.getLabel(), annotation.getDescription(), anot,
-                  llim, hlim, annotation.getGraphType());
+                  llim, hlim, safeInt(annotation.getGraphType()));
 
-          jaa.graphGroup = annotation.getGraphGroup();
+          jaa.graphGroup = safeInt(annotation.getGraphGroup());
           jaa._linecolour = firstColour;
           if (annotation.getThresholdLine() != null)
           {
             jaa.setThreshold(new jalview.datamodel.GraphLine(
-                    annotation.getThresholdLine().getValue(),
+                    safeFloat(annotation.getThresholdLine().getValue()),
                     annotation.getThresholdLine().getLabel(),
-                    new java.awt.Color(
-                            annotation.getThresholdLine().getColour())));
-
+                    new java.awt.Color(safeInt(
+                            annotation.getThresholdLine().getColour()))));
           }
           if (autoForView || annotation.isAutoCalculated())
           {
@@ -3452,6 +3915,7 @@ public class Jalview2XML
           jaa._linecolour = firstColour;
         }
         // register new annotation
+        // Annotation graphs such as Conservation will not have id.
         if (annotation.getId() != null)
         {
           annotationIds.put(annotation.getId(), jaa);
@@ -3490,7 +3954,7 @@ public class Jalview2XML
 
         if (annotation.getScore() != null)
         {
-          jaa.setScore(annotation.getScore());
+          jaa.setScore(annotation.getScore().doubleValue());
         }
         if (annotation.isVisible() != null)
         {
@@ -3507,8 +3971,7 @@ public class Jalview2XML
         {
           jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
         }
-        if (/*annotation.hasAutoCalculated() && */annotation
-                .isAutoCalculated())
+        if (annotation.isAutoCalculated())
         {
           // newer files have an 'autoCalculated' flag and store calculation
           // state in viewport properties
@@ -3519,11 +3982,7 @@ public class Jalview2XML
         {
           jaa.graphHeight = annotation.getGraphHeight().intValue();
         }
-        // if (annotation.hasBelowAlignment())
-        // {
-        // schema specifies default for optional attribute
-          jaa.belowAlignment = annotation.isBelowAlignment();
-        // }
+        jaa.belowAlignment = annotation.isBelowAlignment();
         jaa.setCalcId(annotation.getCalcId());
         if (annotation.getProperty().size() > 0)
         {
@@ -3570,12 +4029,11 @@ public class Jalview2XML
           }
           else
           {
-            cs = ColourSchemeProperty.getColourScheme(al,
+            cs = ColourSchemeProperty.getColourScheme(null, al,
                     jGroup.getColour());
           }
         }
-        int pidThreshold = jGroup.getPidThreshold() == null ? 0
-                : jGroup.getPidThreshold().intValue();
+        int pidThreshold = safeInt(jGroup.getPidThreshold());
 
         Vector<SequenceI> seqs = new Vector<>();
 
@@ -3596,40 +4054,24 @@ public class Jalview2XML
         }
 
         SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
-                jGroup.isDisplayBoxes(), jGroup.isDisplayText(),
-                jGroup.isColourText(), jGroup.getStart(), jGroup.getEnd());
+                safeBoolean(jGroup.isDisplayBoxes()),
+                safeBoolean(jGroup.isDisplayText()),
+                safeBoolean(jGroup.isColourText()),
+                safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
         sg.getGroupColourScheme().setThreshold(pidThreshold, true);
         sg.getGroupColourScheme()
-                .setConservationInc(jGroup.getConsThreshold() == null ? 0
-                        : jGroup.getConsThreshold().intValue());
-        sg.setOutlineColour(
-                new java.awt.Color(jGroup.getOutlineColour() == null ? 0
-                        : jGroup.getOutlineColour().intValue()));
-
-        sg.textColour = new java.awt.Color(jGroup.getTextCol1());
-        sg.textColour2 = new java.awt.Color(jGroup.getTextCol2());
-        sg.setShowNonconserved(
-                jGroup.isShowUnconserved() != null
-                        ? jGroup.isShowUnconserved().booleanValue()
-                        : false);
-        sg.thresholdTextColour = jGroup.getTextColThreshold();
+                .setConservationInc(safeInt(jGroup.getConsThreshold()));
+        sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
+
+        sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
+        sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
+        sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
+        sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
         // attributes with a default in the schema are never null
-        // if (jGroup.hasShowConsensusHistogram())
-        // {
           sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
-        // }
-        // if (jGroup.hasShowSequenceLogo())
-        // {
           sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
-        // }
-        // if (jGroup.hasNormaliseSequenceLogo())
-        // {
           sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
-        // }
-        // if (jGroup.hasIgnoreGapsinConsensus())
-        // {
         sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
-        // }
         if (jGroup.getConsThreshold() != null
                 && jGroup.getConsThreshold().intValue() != 0)
         {
@@ -3685,15 +4127,6 @@ public class Jalview2XML
     // ///////////////////////////////
     // LOAD VIEWPORT
 
-    // 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)
     {
@@ -3732,8 +4165,9 @@ public class Jalview2XML
     boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
             jalviewModel.getVersion());
 
+    AlignFrame af = null;
     AlignmentPanel ap = null;
-    boolean isnewview = true;
+    AlignViewport av = null;
     if (viewId != null)
     {
       // Check to see if this alignment already has a view id == viewId
@@ -3743,25 +4177,27 @@ public class Jalview2XML
       {
         for (int v = 0; v < views.length; v++)
         {
-          if (views[v].av.getViewId().equalsIgnoreCase(viewId))
+          ap = views[v];
+          av = ap.av;
+          if (av.getViewId().equalsIgnoreCase(viewId))
           {
             // recover the existing alignpanel, alignframe, viewport
-            af = views[v].alignFrame;
-            av = views[v].av;
-            ap = views[v];
+            af = ap.alignFrame;
+            break;
             // 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)
+    if (af == null)
     {
-      af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
+      af = loadViewport(fileName, file, jseqs, hiddenSeqs, al, jalviewModel, view,
               uniqueSeqSetId, viewId, autoAlan);
       av = af.getViewport();
+      // note that this only retrieves the most recently accessed
+      // tab of an AlignFrame.
       ap = af.alignPanel;
     }
 
@@ -3770,17 +4206,90 @@ public class Jalview2XML
      * 
      * Not done if flag is false (when this method is used for New View)
      */
+    final AlignFrame af0 = af;
+    final AlignViewport av0 = av;
+    final AlignmentPanel ap0 = ap;
+//    Platform.timeCheck("Jalview2XML.loadFromObject-beforetree",
+//            Platform.TIME_MARK);
     if (loadTreesAndStructures)
     {
-      loadTrees(jalviewModel, view, af, av, ap);
-      loadPDBStructures(jprovider, jseqs, af, ap);
-      loadRnaViewers(jprovider, jseqs, ap);
+      if (!jalviewModel.getTree().isEmpty())
+      {
+        SwingUtilities.invokeLater(new Runnable()
+        {
+          @Override
+          public void run()
+          {
+//            Platform.timeCheck(null, Platform.TIME_MARK);
+            loadTrees(jalviewModel, view, af0, av0, ap0);
+//            Platform.timeCheck("Jalview2XML.loadTrees", Platform.TIME_MARK);
+          }
+        });
+      }
+      if (!jalviewModel.getPcaViewer().isEmpty())
+      {
+        SwingUtilities.invokeLater(new Runnable()
+        {
+          @Override
+          public void run()
+          {
+//            Platform.timeCheck(null, Platform.TIME_MARK);
+            loadPCAViewers(jalviewModel, ap0);
+//            Platform.timeCheck("Jalview2XML.loadPCA", Platform.TIME_MARK);
+          }
+        });
+      }
+      SwingUtilities.invokeLater(new Runnable()
+      {
+        @Override
+        public void run()
+        {
+//          Platform.timeCheck(null, Platform.TIME_MARK);
+          loadPDBStructures(jprovider, jseqs, af0, ap0);
+//          Platform.timeCheck("Jalview2XML.loadPDB", Platform.TIME_MARK);
+        }
+      });
+      SwingUtilities.invokeLater(new Runnable()
+      {
+        @Override
+        public void run()
+        {
+          loadRnaViewers(jprovider, jseqs, ap0);
+        }
+      });
     }
     // and finally return.
+    // but do not set holdRepaint true just yet, because this could be the
+    // initial frame with just its dataset.
     return af;
   }
 
   /**
+   * Loads a HMMER profile from a file stored in the project, and associates it
+   * with the specified sequence
+   * 
+   * @param jprovider
+   * @param hmmJarFile
+   * @param seq
+   */
+  protected void loadHmmerProfile(jarInputStreamProvider jprovider,
+          String hmmJarFile, SequenceI seq)
+  {
+    try
+    {
+      String hmmFile = copyJarEntry(jprovider, hmmJarFile, "hmm", null);
+      HMMFile parser = new HMMFile(hmmFile, DataSourceType.FILE);
+      HiddenMarkovModel hmmModel = parser.getHMM();
+      hmmModel = new HiddenMarkovModel(hmmModel, seq);
+      seq.setHMM(hmmModel);
+    } catch (IOException e)
+    {
+      warn("Error loading HMM profile for " + seq.getName() + ": "
+              + e.getMessage());
+    }
+  }
+
+  /**
    * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
    * panel is restored from separate jar entries, two (gapped and trimmed) per
    * sequence and secondary structure.
@@ -3793,7 +4302,7 @@ public class Jalview2XML
    * @param jseqs
    * @param ap
    */
-  private void loadRnaViewers(jarInputStreamProvider jprovider,
+  protected void loadRnaViewers(jarInputStreamProvider jprovider,
           List<JSeq> jseqs, AlignmentPanel ap)
   {
     /*
@@ -3819,7 +4328,7 @@ public class Jalview2XML
            * add the structure to the Varna display (with session state copied
            * from the jar to a temporary file)
            */
-          boolean gapped = ss.isGapped();
+          boolean gapped = safeBoolean(ss.isGapped());
           String rnaTitle = ss.getTitle();
           String sessionState = ss.getViewerState();
           String tempStateFile = copyJarEntry(jprovider, sessionState,
@@ -3827,7 +4336,7 @@ public class Jalview2XML
           RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
           appVarna.addModelSession(rna, rnaTitle, tempStateFile);
         }
-        appVarna.setInitialSelection(viewer.getSelectedRna());
+        appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
       }
     }
   }
@@ -3868,8 +4377,9 @@ public class Jalview2XML
      * viewer not found - make it
      */
     RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
-            viewer.getXpos(), viewer.getYpos(), viewer.getWidth(),
-            viewer.getHeight(), viewer.getDividerLocation());
+            safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
+            safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
+            safeInt(viewer.getDividerLocation()));
     AppVarna varna = new AppVarna(model, ap);
 
     return varna;
@@ -3898,10 +4408,16 @@ public class Jalview2XML
         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());
+          tp = af.showNewickTree(new NewickFile(tree.getNewick()),
+                  tree.getTitle(), safeInt(tree.getWidth()),
+                  safeInt(tree.getHeight()), safeInt(tree.getXpos()),
+                  safeInt(tree.getYpos()));
+          if (tp == null)
+          {
+            warn("There was a problem recovering stored Newick tree: \n"
+                    + tree.getNewick());
+            continue;
+          }
           if (tree.getId() != null)
           {
             // perhaps bind the tree id to something ?
@@ -3913,43 +4429,38 @@ public class Jalview2XML
           // 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.setViewport(av); // af.viewport; // TODO: verify 'associate with all
-          // views'
-          // works still
+          tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
+                  safeInt(tree.getYpos()), safeInt(tree.getWidth()),
+                  safeInt(tree.getHeight())));
+          tp.setViewport(av); // af.viewport;
+          // TODO: verify 'associate with all views' works still
           tp.getTreeCanvas().setViewport(av); // af.viewport;
           tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
-
-        }
-        if (tp == null)
-        {
-          warn("There was a problem recovering stored Newick tree: \n"
-                  + tree.getNewick());
-          continue;
         }
-
-        tp.fitToWindow.setState(tree.isFitToWindow());
+        tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
+        tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
         tp.fitToWindow_actionPerformed(null);
 
         if (tree.getFontName() != null)
         {
-          tp.setTreeFont(new java.awt.Font(tree.getFontName(),
-                  tree.getFontStyle(), tree.getFontSize()));
+          tp.setTreeFont(
+                  new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
+                          safeInt(tree.getFontSize())));
         }
         else
         {
-          tp.setTreeFont(new java.awt.Font(view.getFontName(),
-                  view.getFontStyle(), tree.getFontSize()));
+          tp.setTreeFont(
+                  new Font(view.getFontName(), safeInt(view.getFontStyle()),
+                          safeInt(view.getFontSize())));
         }
 
-        tp.showPlaceholders(tree.isMarkUnlinked());
-        tp.showBootstrap(tree.isShowBootstrap());
-        tp.showDistances(tree.isShowDistances());
+        tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
+        tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
+        tp.showDistances(safeBoolean(tree.isShowDistances()));
 
-        tp.getTreeCanvas().setThreshold(tree.getThreshold());
+        tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
 
-        if (tree.isCurrentTree())
+        if (safeBoolean(tree.isCurrentTree()))
         {
           af.getViewport().setCurrentTree(tp.getTree());
         }
@@ -4003,13 +4514,13 @@ public class Jalview2XML
                     loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
             jpdb.setId(pdbid.getId());
 
-            int x = structureState.getXpos();
-            int y = structureState.getYpos();
-            int width = structureState.getWidth();
-            int height = structureState.getHeight();
+            int x = safeInt(structureState.getXpos());
+            int y = safeInt(structureState.getYpos());
+            int width = safeInt(structureState.getWidth());
+            int height = safeInt(structureState.getHeight());
 
             // Probably don't need to do this anymore...
-            // Desktop.desktop.getComponentAt(x, y);
+            // Desktop.getDesktop().getComponentAt(x, y);
             // TODO: NOW: check that this recovers the PDB file correctly.
             String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
                     pdbid.getFile());
@@ -4165,7 +4676,7 @@ public class Jalview2XML
      */
     String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
     chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
-            "chimera", null);
+            "chimera", ".py");
 
     Set<Entry<File, StructureData>> fileData = data.getFileData()
             .entrySet();
@@ -4251,7 +4762,8 @@ public class Jalview2XML
           String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
           filedat = oldFiles.get(new File(reformatedOldFilename));
         }
-        newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
+        newFileLoc
+                .append(Platform.escapeBackslashes(filedat.getFilePath()));
         pdbfilenames.add(filedat.getFilePath());
         pdbids.add(filedat.getPdbId());
         seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
@@ -4328,9 +4840,11 @@ public class Jalview2XML
     final AlignFrame alf = af;
     final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
             svattrib.getWidth(), svattrib.getHeight());
-    try
-    {
-      javax.swing.SwingUtilities.invokeAndWait(new Runnable()
+    
+    // BH again was invokeAndWait
+    // try
+    // {
+      javax.swing.SwingUtilities.invokeLater(new Runnable()
       {
         @Override
         public void run()
@@ -4357,14 +4871,14 @@ public class Jalview2XML
           }
         }
       });
-    } catch (InvocationTargetException ex)
-    {
-      warn("Unexpected error when opening Jmol view.", ex);
-
-    } catch (InterruptedException e)
-    {
-      // e.printStackTrace();
-    }
+    // } catch (InvocationTargetException ex)
+    // {
+    // warn("Unexpected error when opening Jmol view.", ex);
+    //
+    // } catch (InterruptedException e)
+    // {
+    // // e.printStackTrace();
+    // }
 
   }
 
@@ -4493,7 +5007,7 @@ public class Jalview2XML
     {
       try
       {
-        frames = Desktop.desktop.getAllFrames();
+        frames = Desktop.getDesktopPane().getAllFrames();
       } catch (ArrayIndexOutOfBoundsException e)
       {
         // occasional No such child exceptions are thrown here...
@@ -4563,23 +5077,35 @@ public class Jalview2XML
     }
   }
 
-  AlignFrame loadViewport(String file, List<JSeq> JSEQ,
-          List<SequenceI> hiddenSeqs, AlignmentI al,
-          JalviewModel jm, Viewport view, String uniqueSeqSetId,
-          String viewId, List<JvAnnotRow> autoAlan)
+  AlignFrame loadViewport(String fileName, File file, List<JSeq> JSEQ,
+          List<SequenceI> hiddenSeqs, AlignmentI al, JalviewModel jm,
+          Viewport view, String uniqueSeqSetId, String viewId,
+          List<JvAnnotRow> autoAlan)
   {
     AlignFrame af = null;
-    af = new AlignFrame(al, view.getWidth(), view.getHeight(),
-            uniqueSeqSetId, viewId);
-
-    af.setFileName(file, FileFormat.Jalview);
+    af = new AlignFrame(al, safeInt(view.getWidth()),
+            safeInt(view.getHeight()), uniqueSeqSetId, viewId)
+    // {
+    //
+    // @Override
+    // protected void processKeyEvent(java.awt.event.KeyEvent e) {
+    // System.out.println("Jalview2XML AF " + e);
+    // super.processKeyEvent(e);
+    //
+    // }
+    //
+    // }
+    ;
+    af.alignPanel.setHoldRepaint(true);
+    af.setFile(fileName, file, null, FileFormat.Jalview);
+    af.setFileObject(jarFile); // BH 2019 JAL-3436
 
     final AlignViewport viewport = af.getViewport();
     for (int i = 0; i < JSEQ.size(); i++)
     {
-      viewport.setSequenceColour(
-              viewport.getAlignment().getSequenceAt(i),
-              new java.awt.Color(JSEQ.get(i).getColour()));
+      int colour = safeInt(JSEQ.get(i).getColour());
+      viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
+              new Color(colour));
     }
 
     if (al.hasSeqrep())
@@ -4588,7 +5114,7 @@ public class Jalview2XML
       viewport.setDisplayReferenceSeq(true);
     }
 
-    viewport.setGatherViewsHere(view.isGatheredViews());
+    viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
 
     if (view.getSequenceSetId() != null)
     {
@@ -4640,49 +5166,57 @@ public class Jalview2XML
     }
     // recover view properties and display parameters
 
-    viewport.setShowAnnotation(view.isShowAnnotation());
-    viewport.setAbovePIDThreshold(view.isPidSelected());
-    final int pidThreshold = view.getPidThreshold() == null ? 0
-            : view.getPidThreshold().intValue();
+    viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
+    viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
+    final int pidThreshold = safeInt(view.getPidThreshold());
     viewport.setThreshold(pidThreshold);
 
-    viewport.setColourText(view.isShowColourText());
+    viewport.setColourText(safeBoolean(view.isShowColourText()));
 
-    viewport
-            .setConservationSelected(view.isConservationSelected());
-    viewport.setIncrement(view.getConsThreshold() == null ? 0
-            : view.getConsThreshold().intValue());
-    viewport.setShowJVSuffix(view.isShowFullId());
-    viewport.setRightAlignIds(view.isRightAlignIds());
-    viewport.setFont(new java.awt.Font(view.getFontName(),
-            view.getFontStyle(), view.getFontSize()), true);
+    viewport.setConservationSelected(
+            safeBoolean(view.isConservationSelected()));
+    viewport.setIncrement(safeInt(view.getConsThreshold()));
+    viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
+    viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
+    viewport.setFont(new Font(view.getFontName(),
+            safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
+            true);
     ViewStyleI vs = viewport.getViewStyle();
     vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
     viewport.setViewStyle(vs);
     // TODO: allow custom charWidth/Heights to be restored by updating them
     // after setting font - which means set above to false
-    viewport.setRenderGaps(view.isRenderGaps());
-    viewport.setWrapAlignment(view.isWrapAlignment());
-    viewport.setShowAnnotation(view.isShowAnnotation());
+    viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
+    viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
+    viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
 
-    viewport.setShowBoxes(view.isShowBoxes());
+    viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
 
-    viewport.setShowText(view.isShowText());
+    viewport.setShowText(safeBoolean(view.isShowText()));
 
-    viewport.setTextColour(new java.awt.Color(view.getTextCol1()));
-    viewport.setTextColour2(new java.awt.Color(view.getTextCol2()));
-    viewport.setThresholdTextColour(view.getTextColThreshold());
-    viewport.setShowUnconserved(
-            view.isShowUnconserved() ? view.isShowUnconserved() : false);
-    viewport.getRanges().setStartRes(view.getStartRes());
+    viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
+    viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
+    viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
+    viewport.setShowUnconserved(view.isShowUnconserved());
+    viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
 
     if (view.getViewName() != null)
     {
       viewport.setViewName(view.getViewName());
       af.setInitialTabVisible();
     }
-    af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
-            view.getHeight());
+    int x = safeInt(view.getXpos());
+    int y = safeInt(view.getYpos());
+    int w = safeInt(view.getWidth());
+    int h = safeInt(view.getHeight());
+    // // BH we cannot let the title bar go off the top
+    // if (Platform.isJS())
+    // {
+    // x = Math.max(50 - w, x);
+    // y = Math.max(0, y);
+    // }
+
+    af.setBounds(x, y, w, h);
     // startSeq set in af.alignPanel.updateLayout below
     af.alignPanel.updateLayout();
     ColourSchemeI cs = null;
@@ -4703,91 +5237,46 @@ public class Jalview2XML
       }
       else
       {
-        cs = ColourSchemeProperty.getColourScheme(al, view.getBgColour());
+        cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
+                view.getBgColour());
       }
     }
 
+    /*
+     * turn off 'alignment colour applies to all groups'
+     * while restoring global colour scheme
+     */
+    viewport.setColourAppliesToAllGroups(false);
     viewport.setGlobalColourScheme(cs);
     viewport.getResidueShading().setThreshold(pidThreshold,
             view.isIgnoreGapsinConsensus());
     viewport.getResidueShading()
             .setConsensus(viewport.getSequenceConsensusHash());
-    viewport.setColourAppliesToAllGroups(false);
-
-    if (view.isConservationSelected() && cs != null)
+    if (safeBoolean(view.isConservationSelected()) && cs != null)
     {
       viewport.getResidueShading()
-              .setConservationInc(view.getConsThreshold());
+              .setConservationInc(safeInt(view.getConsThreshold()));
     }
-
     af.changeColour(cs);
-
     viewport.setColourAppliesToAllGroups(true);
 
-    viewport
-            .setShowSequenceFeatures(view.isShowSequenceFeatures());
+    viewport.setShowSequenceFeatures(
+            safeBoolean(view.isShowSequenceFeatures()));
 
-    // if (view.hasCentreColumnLabels())
-    // {
     viewport.setCentreColumnLabels(view.isCentreColumnLabels());
-    // }
-    // if (view.hasIgnoreGapsinConsensus())
-    // {
     viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
-    // }
-    // if (view.hasFollowHighlight())
-    // {
-
     viewport.setFollowHighlight(view.isFollowHighlight());
-    // }
-    // if (view.hasFollowSelection())
-    // {
     viewport.followSelection = view.isFollowSelection();
-    // }
-    // if (view.hasShowConsensusHistogram())
-    // {
     viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
-    // }
-    // else
-    // {
-    // viewport.setShowConsensusHistogram(true);
-    // }
-    // if (view.hasShowSequenceLogo())
-    // {
     viewport.setShowSequenceLogo(view.isShowSequenceLogo());
-    // }
-    // else
-    // {
-    // viewport.setShowSequenceLogo(false);
-    // }
-    // if (view.hasNormaliseSequenceLogo())
-    // {
     viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
-    // }
-    if (view.isShowDbRefTooltip() != null)
-    {
-      viewport.setShowDBRefs(view.isShowDbRefTooltip());
-    }
-    if (view.isShowNPfeatureTooltip() != null)
-    {
-      viewport.setShowNPFeats(view.isShowNPfeatureTooltip());
-    }
-    // if (view.hasShowGroupConsensus())
-    // {
+    viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
+    viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
     viewport.setShowGroupConsensus(view.isShowGroupConsensus());
-    // }
-    // else
-    // {
-    // viewport.setShowGroupConsensus(false);
-    // }
-    // if (view.hasShowGroupConservation())
-    // {
     viewport.setShowGroupConservation(view.isShowGroupConservation());
-    // }
-    // else
-    // {
-    // viewport.setShowGroupConservation(false);
-    // }
+    viewport.setShowComplementFeatures(view.isShowComplementFeatures());
+    viewport.setShowComplementFeaturesOnTop(
+            view.isShowComplementFeaturesOnTop());
 
     // recover feature settings
     if (jm.getFeatureSettings() != null)
@@ -4796,13 +5285,13 @@ public class Jalview2XML
               .getFeatureRenderer();
       FeaturesDisplayed fdi;
       viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
-      String[] renderOrder = new String[jm.getFeatureSettings()
-              .getSetting().size()];
+      String[] renderOrder = new String[jm.getFeatureSettings().getSetting()
+              .size()];
       Map<String, FeatureColourI> featureColours = new Hashtable<>();
       Map<String, Float> featureOrder = new Hashtable<>();
 
-      for (int fs = 0; fs < jm.getFeatureSettings()
-              .getSetting().size(); fs++)
+      for (int fs = 0; fs < jm.getFeatureSettings().getSetting()
+              .size(); fs++)
       {
         Setting setting = jm.getFeatureSettings().getSetting().get(fs);
         String featureType = setting.getType();
@@ -4814,8 +5303,8 @@ public class Jalview2XML
                 .getMatcherSet();
         if (filters != null)
         {
-          FeatureMatcherSetI filter = Jalview2XML
-                  .unmarshalFilter(featureType, filters);
+          FeatureMatcherSetI filter = Jalview2XML.parseFilter(featureType,
+                  filters);
           if (!filter.isEmpty())
           {
             fr.setFeatureFilter(featureType, filter);
@@ -4843,14 +5332,11 @@ public class Jalview2XML
           {
             noValueColour = maxColour;
           }
-          float min = setting.getMin() != null
-                  ? setting.getMin().floatValue()
-                  : 0f;
-          float max = setting.getMin() != null
-                  ? setting.getMax().floatValue()
-                  : 1f;
-          FeatureColourI gc = new FeatureColour(minColour, maxColour,
-                  noValueColour, min, max);
+          float min = safeFloat(safeFloat(setting.getMin()));
+          float max = setting.getMax() == null ? 1f
+                  : setting.getMax().floatValue();
+          FeatureColourI gc = new FeatureColour(maxColour, minColour,
+                  maxColour, noValueColour, min, max);
           if (setting.getAttributeName().size() > 0)
           {
             gc.setAttributeName(setting.getAttributeName().toArray(
@@ -4859,7 +5345,7 @@ public class Jalview2XML
           if (setting.getThreshold() != null)
           {
             gc.setThreshold(setting.getThreshold().floatValue());
-            int threshstate = setting.getThreshstate().intValue();
+            int threshstate = safeInt(setting.getThreshstate());
             // -1 = None, 0 = Below, 1 = Above threshold
             if (threshstate == 0)
             {
@@ -4884,8 +5370,7 @@ public class Jalview2XML
         }
         else
         {
-          featureColours.put(featureType,
-                  new FeatureColour(maxColour));
+          featureColours.put(featureType, new FeatureColour(maxColour));
         }
         renderOrder[fs] = featureType;
         if (setting.getOrder() != null)
@@ -4894,10 +5379,10 @@ public class Jalview2XML
         }
         else
         {
-          featureOrder.put(featureType, new Float(
+          featureOrder.put(featureType, Float.valueOf(
                   fs / jm.getFeatureSettings().getSetting().size()));
         }
-        if (setting.isDisplay())
+        if (safeBoolean(setting.isDisplay()))
         {
           fdi.setVisible(featureType);
         }
@@ -4906,7 +5391,7 @@ public class Jalview2XML
       for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
       {
         Group grp = jm.getFeatureSettings().getGroup().get(gs);
-        fgtable.put(grp.getName(), new Boolean(grp.isDisplay()));
+        fgtable.put(grp.getName(), Boolean.valueOf(grp.isDisplay()));
       }
       // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
       // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
@@ -4920,9 +5405,9 @@ public class Jalview2XML
     {
       for (int c = 0; c < view.getHiddenColumns().size(); c++)
       {
-        viewport.hideColumns(view.getHiddenColumns().get(c).getStart(),
-                view.getHiddenColumns().get(c).getEnd() // +1
-        );
+        final HiddenColumns hc = view.getHiddenColumns().get(c);
+        viewport.hideColumns(safeInt(hc.getStart()),
+                safeInt(hc.getEnd()) /* +1 */);
       }
     }
     if (view.getCalcIdParam() != null)
@@ -4953,8 +5438,9 @@ public class Jalview2XML
     String complementaryViewId = view.getComplementId();
     if (complementaryViewId == null)
     {
-      Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
-              view.getHeight());
+      Dimension dim = Platform.getDimIfEmbedded(af,
+              safeInt(view.getWidth()), safeInt(view.getHeight()));
+      Desktop.addInternalFrame(af, view.getTitle(), dim.width, dim.height);
       // recompute any autoannotation
       af.alignPanel.updateAnnotation(false, true);
       reorderAutoannotation(af, al, autoAlan);
@@ -5029,34 +5515,36 @@ public class Jalview2XML
     }
     if (matchedAnnotation.getThreshold() == null)
     {
-      matchedAnnotation.setThreshold(new GraphLine(
-              viewAnnColour.getThreshold(), "Threshold", Color.black));
+      matchedAnnotation.setThreshold(
+              new GraphLine(safeFloat(viewAnnColour.getThreshold()),
+                      "Threshold", Color.black));
     }
 
     AnnotationColourGradient cs = null;
     if (viewAnnColour.getColourScheme().equals("None"))
     {
       cs = new AnnotationColourGradient(matchedAnnotation,
-              new Color(viewAnnColour.getMinColour()),
-              new Color(viewAnnColour.getMaxColour()),
-              viewAnnColour.getAboveThreshold());
+              new Color(safeInt(viewAnnColour.getMinColour())),
+              new Color(safeInt(viewAnnColour.getMaxColour())),
+              safeInt(viewAnnColour.getAboveThreshold()));
     }
     else if (viewAnnColour.getColourScheme().startsWith("ucs"))
     {
       cs = new AnnotationColourGradient(matchedAnnotation,
               getUserColourScheme(model, viewAnnColour.getColourScheme()),
-              viewAnnColour.getAboveThreshold());
+              safeInt(viewAnnColour.getAboveThreshold()));
     }
     else
     {
       cs = new AnnotationColourGradient(matchedAnnotation,
-              ColourSchemeProperty.getColourScheme(al,
+              ColourSchemeProperty.getColourScheme(af.getViewport(), al,
                       viewAnnColour.getColourScheme()),
-              viewAnnColour.getAboveThreshold());
+              safeInt(viewAnnColour.getAboveThreshold()));
     }
 
-    boolean perSequenceOnly = viewAnnColour.isPerSequence();
-    boolean useOriginalColours = viewAnnColour.isPredefinedColours();
+    boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
+    boolean useOriginalColours = safeBoolean(
+            viewAnnColour.isPredefinedColours());
     cs.setSeqAssociated(perSequenceOnly);
     cs.setPredefinedColours(useOriginalColours);
 
@@ -5073,7 +5561,7 @@ public class Jalview2XML
 
         AnnotationColourGradient groupScheme = new AnnotationColourGradient(
                 matchedAnnotation, sg.getColourScheme(),
-                viewAnnColour.getAboveThreshold());
+                safeInt(viewAnnColour.getAboveThreshold()));
         sg.setColourScheme(groupScheme);
         groupScheme.setSeqAssociated(perSequenceOnly);
         groupScheme.setPredefinedColours(useOriginalColours);
@@ -5210,16 +5698,16 @@ public class Jalview2XML
     String id = object.getViewport().get(0).getSequenceSetId();
     if (skipList.containsKey(id))
     {
-      if (Cache.log != null && Cache.log.isDebugEnabled())
-      {
-        Cache.log.debug("Skipping seuqence set id " + id);
-      }
+       if (Cache.log != null && Cache.log.isDebugEnabled())
+        {
+          Cache.log.debug("Skipping seuqence set id " + id);
+        }
       return true;
     }
     return false;
   }
 
-  public void addToSkipList(AlignFrame af)
+  protected void addToSkipList(AlignFrame af)
   {
     if (skipList == null)
     {
@@ -5228,7 +5716,7 @@ public class Jalview2XML
     skipList.put(af.getViewport().getSequenceSetId(), af);
   }
 
-  public void clearSkipList()
+  protected void clearSkipList()
   {
     if (skipList != null)
     {
@@ -5238,13 +5726,51 @@ public class Jalview2XML
   }
 
   private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
-          boolean ignoreUnrefed)
+          boolean ignoreUnrefed, String uniqueSeqSetId)
   {
     jalview.datamodel.AlignmentI ds = getDatasetFor(
             vamsasSet.getDatasetId());
+    AlignmentI xtant_ds = ds;
+    if (xtant_ds == null)
+    {
+      // good chance we are about to create a new dataset, but check if we've
+      // seen some of the dataset sequence IDs before.
+      // TODO: skip this check if we are working with project generated by
+      // version 2.11 or later
+      xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
+      if (xtant_ds != null)
+      {
+        ds = xtant_ds;
+        addDatasetRef(vamsasSet.getDatasetId(), ds);
+      }
+    }
     Vector dseqs = null;
+    if (!ignoreUnrefed)
+    {
+      // recovering an alignment View
+      AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
+      if (seqSetDS != null)
+      {
+        if (ds != null && ds != seqSetDS)
+        {
+          warn("JAL-3171 regression: Overwriting a dataset reference for an alignment"
+                  + " - CDS/Protein crossreference data may be lost");
+          if (xtant_ds != null)
+          {
+            // This can only happen if the unique sequence set ID was bound to a
+            // dataset that did not contain any of the sequences in the view
+            // currently being restored.
+            warn("JAL-3171 SERIOUS!  TOTAL CONFUSION - please consider contacting the Jalview Development team so they can investigate why your project caused this message to be displayed.");
+          }
+        }
+        ds = seqSetDS;
+        addDatasetRef(vamsasSet.getDatasetId(), ds);
+      }
+    }
     if (ds == null)
     {
+      // try even harder to restore dataset
+      AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
       // create a list of new dataset sequences
       dseqs = new Vector();
     }
@@ -5259,18 +5785,66 @@ public class Jalview2XML
       SequenceI[] dsseqs = new SequenceI[dseqs.size()];
       dseqs.copyInto(dsseqs);
       ds = new jalview.datamodel.Alignment(dsseqs);
-      debug("Created new dataset " + vamsasSet.getDatasetId()
-              + " for alignment " + System.identityHashCode(al));
+//      debug("Jalview2XML Created new dataset " + vamsasSet.getDatasetId()
+//              + " for alignment " + System.identityHashCode(al));
       addDatasetRef(vamsasSet.getDatasetId(), ds);
     }
     // set the dataset for the newly imported alignment.
     if (al.getDataset() == null && !ignoreUnrefed)
     {
       al.setDataset(ds);
+      // register dataset for the alignment's uniqueSeqSetId for legacy projects
+      addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
     }
+    updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
   }
 
   /**
+   * XML dataset sequence ID to materialised dataset reference
+   */
+  HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
+
+  /**
+   * @return the first materialised dataset reference containing a dataset
+   *         sequence referenced in the given view
+   * @param list
+   *          - sequences from the view
+   */
+  AlignmentI checkIfHasDataset(List<Sequence> list)
+  {
+    for (Sequence restoredSeq : list)
+    {
+      AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
+      if (datasetFor != null)
+      {
+        return datasetFor;
+      }
+    }
+    return null;
+  }
+
+  /**
+   * Register ds as the containing dataset for the dataset sequences referenced
+   * by sequences in list
+   * 
+   * @param list
+   *          - sequences in a view
+   * @param ds
+   */
+  void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
+  {
+    for (Sequence restoredSeq : list)
+    {
+      AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
+      if (prevDS != null && prevDS != ds)
+      {
+        warn("Dataset sequence appears in many datasets: "
+                + restoredSeq.getDsseqid());
+        // TODO: try to merge!
+      }
+    }
+  }
+  /**
    * 
    * @param vamsasSeq
    *          sequence definition to create/merge dataset sequence for
@@ -5491,13 +6065,29 @@ public class Jalview2XML
     return datasetId;
   }
 
+  /**
+   * Add any saved DBRefEntry's to the sequence. An entry flagged as 'locus' is
+   * constructed as a special subclass GeneLocus.
+   * 
+   * @param datasetSequence
+   * @param sequence
+   */
   private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
   {
     for (int d = 0; d < sequence.getDBRef().size(); d++)
     {
       DBRef dr = sequence.getDBRef().get(d);
-      jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
-              dr.getSource(), dr.getVersion(), dr.getAccessionId());
+      DBRefEntry entry;
+      if (dr.isLocus())
+      {
+        entry = new GeneLocus(dr.getSource(), dr.getVersion(),
+                dr.getAccessionId());
+      }
+      else
+      {
+        entry = new DBRefEntry(dr.getSource(), dr.getVersion(),
+                dr.getAccessionId());
+      }
       if (dr.getMapping() != null)
       {
         entry.setMap(addMapping(dr.getMapping()));
@@ -5529,15 +6119,16 @@ public class Jalview2XML
     jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
             fto, m.getMapFromUnit().intValue(),
             m.getMapToUnit().intValue());
-    // if (m.getMappingChoice() != null)
-    // {
-    // MappingChoice mc = m.getMappingChoice();
+
+    /*
+     * (optional) choice of dseqFor or Sequence
+     */
     if (m.getDseqFor() != null)
     {
       String dsfor = m.getDseqFor();
       if (seqRefIds.containsKey(dsfor))
       {
-        /**
+        /*
          * recover from hash
          */
         jmap.setTo(seqRefIds.get(dsfor));
@@ -5547,9 +6138,9 @@ public class Jalview2XML
         frefedSequence.add(newMappingRef(dsfor, jmap));
       }
     }
-    else
+    else if (m.getSequence() != null)
     {
-      /**
+      /*
        * local sequence definition
        */
       Sequence ms = m.getSequence();
@@ -5584,7 +6175,6 @@ public class Jalview2XML
         jmap.setTo(djs);
         incompleteSeqs.put(sqid, djs);
         seqRefIds.put(sqid, djs);
-
       }
       jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
       addDBRefs(djs, ms);
@@ -5606,6 +6196,10 @@ public class Jalview2XML
     initSeqRefs();
     JalviewModel jm = saveState(ap, null, null, null);
 
+    addDatasetRef(
+            jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
+            ap.getAlignment().getDataset());
+
     uniqueSetSuffix = "";
     // jm.getJalviewModelSequence().getViewport(0).setId(null);
     jm.getViewport().get(0).setId(null);
@@ -5618,9 +6212,11 @@ public class Jalview2XML
 
     viewportsAdded.clear();
 
-    AlignFrame af = loadFromObject(jm, null, false, null);
+    AlignFrame af = loadFromObject(jm, null, null, false, null);
     af.getAlignPanels().clear();
     af.closeMenuItem_actionPerformed(true);
+    af.alignPanel.setHoldRepaint(false);
+    this.jarFile = null;
 
     /*
      * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
@@ -5764,7 +6360,7 @@ public class Jalview2XML
       }
       else
       {
-        Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
+          Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
       }
     }
   }
@@ -5867,13 +6463,137 @@ public class Jalview2XML
   }
 
   /**
+   * Loads any saved PCA viewers
+   * 
+   * @param jms
+   * @param ap
+   */
+  protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
+  {
+    try
+    {
+      List<PcaViewer> pcaviewers = model.getPcaViewer();
+      for (PcaViewer viewer : pcaviewers)
+      {
+        String modelName = viewer.getScoreModelName();
+        SimilarityParamsI params = new SimilarityParams(
+                viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
+                viewer.isIncludeGaps(),
+                viewer.isDenominateByShortestLength());
+
+        /*
+         * create the panel (without computing the PCA)
+         */
+        PCAPanel panel = new PCAPanel(ap, modelName, params);
+
+        panel.setTitle(viewer.getTitle());
+        panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
+                viewer.getWidth(), viewer.getHeight()));
+
+        boolean showLabels = viewer.isShowLabels();
+        panel.setShowLabels(showLabels);
+        panel.getRotatableCanvas().setShowLabels(showLabels);
+        panel.getRotatableCanvas()
+                .setBgColour(new Color(viewer.getBgColour()));
+        panel.getRotatableCanvas()
+                .setApplyToAllViews(viewer.isLinkToAllViews());
+
+        /*
+         * load PCA output data
+         */
+        ScoreModelI scoreModel = ScoreModels.getInstance()
+                .getScoreModel(modelName, ap);
+        PCA pca = new PCA(null, scoreModel, params);
+        PcaDataType pcaData = viewer.getPcaData();
+
+        MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
+        pca.setPairwiseScores(pairwise);
+
+        MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
+        pca.setTridiagonal(triDiag);
+
+        MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
+        pca.setEigenmatrix(result);
+
+        panel.getPcaModel().setPCA(pca);
+
+        /*
+         * we haven't saved the input data! (JAL-2647 to do)
+         */
+        panel.setInputData(null);
+
+        /*
+         * add the sequence points for the PCA display
+         */
+        List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
+        for (SequencePoint sp : viewer.getSequencePoint())
+        {
+          String seqId = sp.getSequenceRef();
+          SequenceI seq = seqRefIds.get(seqId);
+          if (seq == null)
+          {
+            throw new IllegalStateException(
+                    "Unmatched seqref for PCA: " + seqId);
+          }
+          Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
+          jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
+                  seq, pt);
+          seqPoints.add(seqPoint);
+        }
+        panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
+
+        /*
+         * set min-max ranges and scale after setPoints (which recomputes them)
+         */
+        panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
+        SeqPointMin spMin = viewer.getSeqPointMin();
+        float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
+            spMin.getZPos() };
+        SeqPointMax spMax = viewer.getSeqPointMax();
+        float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
+            spMax.getZPos() };
+        panel.getRotatableCanvas().setSeqMinMax(min, max);
+
+        // todo: hold points list in PCAModel only
+        panel.getPcaModel().setSequencePoints(seqPoints);
+
+        panel.setSelectedDimensionIndex(viewer.getXDim(), X);
+        panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
+        panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
+
+        // is this duplication needed?
+        panel.setTop(seqPoints.size() - 1);
+        panel.getPcaModel().setTop(seqPoints.size() - 1);
+
+        /*
+         * add the axes' end points for the display
+         */
+        for (int i = 0; i < 3; i++)
+        {
+          Axis axis = viewer.getAxis().get(i);
+          panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
+                  axis.getXPos(), axis.getYPos(), axis.getZPos());
+        }
+
+        Dimension dim = Platform.getDimIfEmbedded(panel, 475, 450);
+        Desktop.addInternalFrame(panel, MessageManager.formatMessage(
+                "label.calc_title", "PCA", modelName), dim.width,
+                dim.height);
+      }
+    } catch (Exception ex)
+    {
+      Cache.log.error("Error loading PCA: " + ex.toString());
+    }
+  }
+
+  /**
    * Populates an XML model of the feature colour scheme for one feature type
    * 
    * @param featureType
    * @param fcol
    * @return
    */
-  protected static Colour marshalColour(
+  public static Colour marshalColour(
           String featureType, FeatureColourI fcol)
   {
     Colour col = new Colour();
@@ -5930,7 +6650,7 @@ public class Jalview2XML
    * @param and
    *          if true, conditions are and-ed, else or-ed
    */
-  protected static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
+  public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
           FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
           boolean and)
   {
@@ -5996,14 +6716,14 @@ public class Jalview2XML
    * @param matcherSetModel
    * @return
    */
-  protected static FeatureMatcherSetI unmarshalFilter(
+  public static FeatureMatcherSetI parseFilter(
           String featureType,
           jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
   {
     FeatureMatcherSetI result = new FeatureMatcherSet();
     try
     {
-      unmarshalFilterConditions(result, matcherSetModel, true);
+      parseFilterConditions(result, matcherSetModel, true);
     } catch (IllegalStateException e)
     {
       // mixing AND and OR conditions perhaps
@@ -6027,7 +6747,7 @@ public class Jalview2XML
    * @throws IllegalStateException
    *           if AND and OR conditions are mixed
    */
-  protected static void unmarshalFilterConditions(
+  protected static void parseFilterConditions(
           FeatureMatcherSetI matcherSet,
           jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
           boolean and)
@@ -6080,13 +6800,12 @@ public class Jalview2XML
        * compound condition
        */
       List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
-              .getCompoundMatcher()
-              .getMatcherSet();
+              .getCompoundMatcher().getMatcherSet();
       boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
       if (matchers.size() == 2)
       {
-        unmarshalFilterConditions(matcherSet, matchers.get(0), anded);
-        unmarshalFilterConditions(matcherSet, matchers.get(1), anded);
+        parseFilterConditions(matcherSet, matchers.get(0), anded);
+        parseFilterConditions(matcherSet, matchers.get(1), anded);
       }
       else
       {
@@ -6101,7 +6820,7 @@ public class Jalview2XML
    * @param colourModel
    * @return
    */
-  protected static FeatureColourI unmarshalColour(Colour colourModel)
+  public static FeatureColourI parseColour(Colour colourModel)
   {
     FeatureColourI colour = null;
   
@@ -6117,7 +6836,7 @@ public class Jalview2XML
         maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
       } catch (Exception e)
       {
-        Cache.log.warn("Couldn't parse out graduated feature color.", e);
+          Cache.log.warn("Couldn't parse out graduated feature color.", e);
       }
   
       NoValueColour noCol = colourModel.getNoValueColour();
@@ -6130,9 +6849,9 @@ public class Jalview2XML
         noValueColour = maxcol;
       }
   
-      colour = new FeatureColour(mincol, maxcol, noValueColour,
-              colourModel.getMin(),
-              colourModel.getMax());
+      colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
+              safeFloat(colourModel.getMin()),
+              safeFloat(colourModel.getMax()));
       final List<String> attributeName = colourModel.getAttributeName();
       String[] attributes = attributeName
               .toArray(new String[attributeName.size()]);