aaIndex replaces aaHash
[jalview.git] / src / jalview / gui / Jalview2XML.java
index 8cf676d..a38c530 100755 (executable)
@@ -48,10 +48,9 @@ import jalview.schemabinding.version2.*;
  */
 public class Jalview2XML
 {
-    // SAVES SEVERAL ALIGNEMENT WINDOWS TO SAME JARFILE
+    // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
     public void SaveState(File statefile)
     {
-        long creation = System.currentTimeMillis();
         JInternalFrame[] frames = Desktop.desktop.getAllFrames();
 
         if (frames == null)
@@ -107,7 +106,19 @@ public class Jalview2XML
                         shortName = shortName + ".xml";
                     }
 
-                    SaveState(af, creation, shortName, jout, out);
+                    int ap, apSize= af.alignPanels.size();
+                    for (ap = 0; ap < apSize; ap++)
+                    {
+                      AlignmentPanel apanel = (AlignmentPanel) af.alignPanels.
+                          elementAt(ap);
+
+                      if (apanel.av.explodedPosition == null)
+                        apanel.av.explodedPosition = af.getBounds();
+
+                      SaveState(apanel,
+                                apSize == 1 ? shortName : ap+shortName,
+                                jout, out);
+                    }
                 }
             }
 
@@ -126,17 +137,25 @@ public class Jalview2XML
     {
         try
         {
-            FileOutputStream fos = new FileOutputStream(jarFile);
-            JarOutputStream jout = new JarOutputStream(fos);
+          int ap, apSize= af.alignPanels.size();
+          FileOutputStream fos = new FileOutputStream(jarFile);
+          JarOutputStream jout = new JarOutputStream(fos);
+          PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
+                "UTF-8"));
+          for( ap=0; ap<apSize; ap++)
+          {
+            AlignmentPanel apanel = (AlignmentPanel)af.alignPanels.elementAt(ap);
 
-            //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
-            ////////////////////////////////////////////////////
-            PrintWriter out = new PrintWriter(new OutputStreamWriter(jout,
-                        "UTF-8"));
+            if (apanel.av.explodedPosition == null)
+              apanel.av.explodedPosition = af.getBounds();
 
-            SaveState(af, System.currentTimeMillis(), fileName, jout, out);
-            out.close();
-            jout.close();
+            SaveState(apanel,
+                      apSize==1?fileName:fileName+ap,
+                      jout, out);
+          }
+
+          out.close();
+          jout.close();
         }
         catch (Exception ex)
         {
@@ -153,30 +172,31 @@ public class Jalview2XML
      * @param jout DOCUMENT ME!
      * @param out DOCUMENT ME!
      */
-    public void SaveState(AlignFrame af, long timeStamp,
-        String fileName, JarOutputStream jout, PrintWriter out)
+    public void SaveState(AlignmentPanel ap,
+                          String fileName,
+                          JarOutputStream jout,
+                          PrintWriter out)
     {
-        Vector seqids = new Vector();
+      if (seqRefIds == null)
+        seqRefIds = new Hashtable();
+
         Vector userColours = new Vector();
 
-        AlignViewport av = af.viewport;
+        AlignViewport av = ap.av;
 
         JalviewModel object = new JalviewModel();
         object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
 
-        object.setCreationDate(new java.util.Date(timeStamp));
+        object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
         object.setVersion(jalview.bin.Cache.getProperty("VERSION"));
 
-        jalview.datamodel.AlignmentI jal = af.viewport.alignment;
-        jalview.datamodel.AlignmentI jalhidden = null;
+        jalview.datamodel.AlignmentI jal = av.alignment;
 
         if(av.hasHiddenRows)
         {
-          jalhidden = jal;
           jal = jal.getHiddenSequences().getFullAlignment();
         }
 
-
         SequenceSet vamsasSet = new SequenceSet();
         Sequence vamsasSeq;
         JalviewModelSequence jms = new JalviewModelSequence();
@@ -190,26 +210,37 @@ public class Jalview2XML
         int id = 0;
         for (int i = 0; i < jal.getHeight(); i++)
         {
-            seqids.add(jal.getSequenceAt(i));
-            vamsasSeq = new Sequence();
-            vamsasSeq.setId(id + "");
-            vamsasSeq.setName(jal.getSequenceAt(i).getName());
-            vamsasSeq.setSequence(jal.getSequenceAt(i).getSequence());
-            vamsasSeq.setDescription(jal.getSequenceAt(i).getDescription());
-
-            if(jal.getSequenceAt(i).getDatasetSequence().getDBRef()!=null)
+            id = jal.getSequenceAt(i).hashCode();
+
+            if(seqRefIds.get(id+"")!=null)
+            {
+
+            }
+            else
             {
-              jalview.datamodel.DBRefEntry [] dbrefs =
-                  jal.getSequenceAt(i).getDatasetSequence().getDBRef();
+              vamsasSeq = new Sequence();
+              vamsasSeq.setId(id + "");
+              vamsasSeq.setName(jal.getSequenceAt(i).getName());
+              vamsasSeq.setSequence(jal.getSequenceAt(i).getSequence());
+              vamsasSeq.setDescription(jal.getSequenceAt(i).getDescription());
 
-              for(int d=0; d<dbrefs.length; d++)
+              if (jal.getSequenceAt(i).getDatasetSequence().getDBRef() != null)
               {
-                DBRef dbref = new DBRef();
-                dbref.setSource( dbrefs[d].getSource() );
-                dbref.setVersion( dbrefs[d].getVersion());
-                dbref.setAccessionId(dbrefs[d].getAccessionId());
-                vamsasSeq.addDBRef(dbref);
+                jalview.datamodel.DBRefEntry[] dbrefs =
+                    jal.getSequenceAt(i).getDatasetSequence().getDBRef();
+
+                for (int d = 0; d < dbrefs.length; d++)
+                {
+                  DBRef dbref = new DBRef();
+                  dbref.setSource(dbrefs[d].getSource());
+                  dbref.setVersion(dbrefs[d].getVersion());
+                  dbref.setAccessionId(dbrefs[d].getAccessionId());
+                  vamsasSeq.addDBRef(dbref);
+                }
               }
+
+              vamsasSet.addSequence(vamsasSeq);
+              seqRefIds.put(id+"", vamsasSeq);
             }
 
             jseq = new JSeq();
@@ -221,7 +252,7 @@ public class Jalview2XML
 
             if (av.hasHiddenRows)
             {
-              jseq.setHidden(jalhidden.getHiddenSequences().isHidden(
+              jseq.setHidden(av.alignment.getHiddenSequences().isHidden(
                   jal.getSequenceAt(i)));
 
               if(jal.getSequenceAt(i).getHiddenSequences()!=null)
@@ -350,13 +381,15 @@ public class Jalview2XML
             }
 
             jms.addJSeq(jseq);
-            vamsasSet.addSequence(vamsasSeq);
-            id++;
         }
 
+        if(av.hasHiddenRows)
+          jal = av.alignment;
+
+
         //SAVE TREES
         ///////////////////////////////////
-        if (af.viewport.currentTree != null)
+        if (av.currentTree != null)
         {
           // FIND ANY ASSOCIATED TREES
           // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
@@ -374,7 +407,7 @@ public class Jalview2XML
                 {
                   Tree tree = new Tree();
                   tree.setTitle(tp.getTitle());
-                  tree.setCurrentTree( (af.viewport.currentTree == tp.getTree()));
+                  tree.setCurrentTree( (av.currentTree == tp.getTree()));
                   tree.setNewick(tp.getTree().toString());
                   tree.setThreshold(tp.treeCanvas.threshold);
 
@@ -531,9 +564,9 @@ public class Jalview2XML
 
                 for (int s = 0; s < sg.getSize(false); s++)
                 {
-                    jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg.getSequenceAt(s);
-                    int index = seqids.indexOf(seq);
-                    groups[i].addSeq(index);
+                    jalview.datamodel.Sequence seq =
+                        (jalview.datamodel.Sequence) sg.getSequenceAt(s);
+                    groups[i].addSeq(seq.hashCode());
                 }
             }
 
@@ -543,11 +576,15 @@ public class Jalview2XML
 
         ///////////SAVE VIEWPORT
         Viewport view = new Viewport();
-        view.setTitle(af.getTitle());
-        view.setXpos(af.getX());
-        view.setYpos(af.getY());
-        view.setWidth(af.getWidth());
-        view.setHeight(af.getHeight());
+        view.setTitle(ap.alignFrame.getTitle());
+        view.setSequenceSetId(av.getSequenceSetId());
+        view.setViewName(av.viewName);
+        view.setGatheredViews(av.gatherViewsHere);
+
+        view.setXpos(av.explodedPosition.x);
+        view.setYpos(av.explodedPosition.y);
+        view.setWidth(av.explodedPosition.width);
+        view.setHeight(av.explodedPosition.height);
         view.setStartRes(av.startRes);
         view.setStartSeq(av.startSeq);
 
@@ -622,7 +659,7 @@ public class Jalview2XML
               = new jalview.schemabinding.version2.FeatureSettings();
 
           String [] renderOrder =
-              af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
+              ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
 
           Vector settingsAdded = new Vector();
           for(int ro=0; ro<renderOrder.length; ro++)
@@ -630,7 +667,7 @@ public class Jalview2XML
             Setting setting = new Setting();
             setting.setType(renderOrder[ro]);
             setting.setColour(
-                af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().getColour(renderOrder[ro]).getRGB()
+                ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(renderOrder[ro]).getRGB()
                 );
 
             setting.setDisplay(
@@ -643,7 +680,7 @@ public class Jalview2XML
 
           //Make sure we save none displayed feature settings
           Enumeration en =
-              af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureColours.keys();
+              ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours.keys();
           while(en.hasMoreElements())
           {
             String key = en.nextElement().toString();
@@ -653,7 +690,7 @@ public class Jalview2XML
             Setting setting = new Setting();
             setting.setType(key);
             setting.setColour(
-                af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().getColour(key).getRGB()
+                ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(key).getRGB()
                 );
 
             setting.setDisplay(false);
@@ -719,9 +756,21 @@ public class Jalview2XML
             for (int i = 0; i < colours.length; i++)
             {
                 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
+                col.setName(ResidueProperties.aa[i]);
                 col.setRGB(jalview.util.Format.getHexString(colours[i]));
                 jbucs.addColour(col);
             }
+            if(ucs.getLowerCaseColours()!=null)
+            {
+              colours = ucs.getLowerCaseColours();
+              for (int i = 0; i < colours.length; i++)
+             {
+                 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
+                 col.setName(ResidueProperties.aa[i].toLowerCase());
+                 col.setRGB(jalview.util.Format.getHexString(colours[i]));
+                 jbucs.addColour(col);
+             }
+            }
 
             id = "ucs" + userColours.indexOf(ucs);
             uc.setId(id);
@@ -748,18 +797,33 @@ public class Jalview2XML
             }
         }
 
-        int csize = colours.getUserColourScheme().getColourCount();
-        java.awt.Color[] newColours = new java.awt.Color[csize];
+        java.awt.Color[] newColours = new java.awt.Color[24];
 
-        for (int i = 0; i < csize; i++)
+        for (int i = 0; i < 24; i++)
         {
             newColours[i] = new java.awt.Color(Integer.parseInt(
                         colours.getUserColourScheme().getColour(i).getRGB(), 16));
         }
 
-        return new jalview.schemes.UserColourScheme(newColours);
+        jalview.schemes.UserColourScheme ucs =
+            new jalview.schemes.UserColourScheme(newColours);
+
+        if (colours.getUserColourScheme().getColourCount() > 24)
+        {
+          newColours = new java.awt.Color[23];
+          for (int i = 0; i < 23; i++)
+          {
+            newColours[i] = new java.awt.Color(Integer.parseInt(
+                colours.getUserColourScheme().getColour(i+24).getRGB(), 16));
+          }
+          ucs.setLowerCaseColours(newColours);
+        }
+
+        return ucs;
     }
 
+    Hashtable seqRefIds;
+
     /**
      * DOCUMENT ME!
      *
@@ -768,6 +832,10 @@ public class Jalview2XML
     public AlignFrame LoadJalviewAlign(final String file)
     {
         jalview.gui.AlignFrame af = null;
+
+        seqRefIds = new Hashtable();
+        Vector gatherToThisFrame= new Vector();
+
         try
         {
             //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
@@ -808,6 +876,10 @@ public class Jalview2XML
                     object = (JalviewModel) unmar.unmarshal( in );
 
                     af = LoadFromObject(object, file);
+                    if(af.viewport.gatherViewsHere)
+                    {
+                      gatherToThisFrame.add(af);
+                    }
                     entryCount++;
                 }
                 else if (jarentry != null)
@@ -837,7 +909,6 @@ public class Jalview2XML
         }
         catch (Exception ex)
         {
-
           //Is Version 1 Jar file?
           af =  new Jalview2XML_V1().LoadJalviewAlign(file);
 
@@ -846,7 +917,7 @@ public class Jalview2XML
             System.out.println("Successfully loaded archive file");
             return af;
           }
-          ex.printStackTrace();
+
             System.err.println("Exception whilst loading jalview XML file : " +
                 ex + "\n");
             javax.swing.SwingUtilities.invokeLater(new Runnable()
@@ -861,12 +932,20 @@ public class Jalview2XML
               }});
         }
 
+        if (Desktop.instance != null)
+          Desktop.instance.stopLoading();
+
+        for (int i = 0; i < gatherToThisFrame.size(); i++)
+        {
+          Desktop.instance.gatherViews(
+              (AlignFrame) gatherToThisFrame.elementAt(i));
+        }
+
         return af;
     }
 
     String loadPDBFile(String file, String pdbId)
     {
-      System.out.println("load file "+file);
       try
       {
         JarInputStream jin = null;
@@ -912,112 +991,150 @@ public class Jalview2XML
 
     AlignFrame LoadFromObject(JalviewModel object, String file)
     {
-        Vector seqids = new Vector();
         SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
         Sequence[] vamsasSeq = vamsasSet.getSequence();
 
         JalviewModelSequence jms = object.getJalviewModelSequence();
 
+        Viewport[] views = jms.getViewport();
+        Viewport view = views[0]; // DEAL WITH MULTIPLE VIEWPORTS LATER
+
         //////////////////////////////////
         //LOAD SEQUENCES
+
         Vector hiddenSeqs = null;
-        jalview.datamodel.Sequence[] jseqs = new jalview.datamodel.Sequence[vamsasSeq.length];
+        jalview.datamodel.Sequence jseq;
+
+        ArrayList tmpseqs = new ArrayList();
+
+
+        boolean multipleView = false;
+
         JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
-        for (int i = 0; i < vamsasSeq.length; i++)
+        for (int i = 0; i < JSEQ.length; i++)
         {
-            jseqs[i] = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),
-                    vamsasSeq[i].getSequence());
-            jseqs[i].setDescription( vamsasSeq[i].getDescription() );
+          String seqId = JSEQ[i].getId() + "";
+
+          if (seqRefIds.get(seqId) != null)
+          {
+            tmpseqs.add( (jalview.datamodel.Sequence) seqRefIds.get(seqId));
+            multipleView = true;
+          }
+          else
+          {
+            jseq = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),
+                vamsasSeq[i].getSequence());
+            jseq.setDescription(vamsasSeq[i].getDescription());
+            jseq.setStart(JSEQ[i].getStart());
+            jseq.setEnd(JSEQ[i].getEnd());
+            jseq.setColor(new java.awt.Color(JSEQ[i].getColour()));
+            seqRefIds.put(vamsasSeq[i].getId(), jseq);
+            tmpseqs.add( jseq );
+          }
+
+
+          if (JSEQ[i].getHidden())
+          {
+            if (hiddenSeqs == null)
+              hiddenSeqs = new Vector();
+
+
+            hiddenSeqs.addElement(
+                (jalview.datamodel.Sequence) seqRefIds.get(seqId));
+          }
 
-            if(JSEQ[i].getHidden())
-            {
-              if(hiddenSeqs == null)
-                hiddenSeqs = new Vector();
-              hiddenSeqs.addElement(jseqs[i]);
-            }
-            jseqs[i].setStart(JSEQ[i].getStart());
-            jseqs[i].setEnd(JSEQ[i].getEnd());
-            jseqs[i].setColor(new java.awt.Color(JSEQ[i].getColour()));
-            seqids.add(jseqs[i]);
         }
 
         ///SequenceFeatures are added to the DatasetSequence,
         // so we must create the dataset before loading features
         /////////////////////////////////
-        jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(jseqs);
+
+
+      jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[
+          tmpseqs.size()];
+
+       tmpseqs.toArray(orderedSeqs) ;
+
+
+        jalview.datamodel.Alignment al =
+            new jalview.datamodel.Alignment(orderedSeqs);
+
         al.setDataset(null);
         /////////////////////////////////
 
 
         Hashtable pdbloaded = new Hashtable();
-        for (int i = 0; i < vamsasSeq.length; i++)
+        if(!multipleView)
         {
-          if (JSEQ[i].getFeaturesCount() > 0)
+          for (int i = 0; i < vamsasSeq.length; i++)
           {
-            Features[] features = JSEQ[i].getFeatures();
-            for (int f = 0; f < features.length; f++)
+            if (JSEQ[i].getFeaturesCount() > 0)
             {
-              jalview.datamodel.SequenceFeature sf
-                  = new jalview.datamodel.SequenceFeature(features[f].getType(),
-                  features[f].getDescription(), features[f].getStatus(),
-                  features[f].getBegin(), features[f].getEnd(),
-                  features[f].getFeatureGroup());
-
-              sf.setScore(features[f].getScore());
-              for(int od=0; od<features[f].getOtherDataCount(); od++)
+              Features[] features = JSEQ[i].getFeatures();
+              for (int f = 0; f < features.length; f++)
               {
-                OtherData keyValue = features[f].getOtherData(od);
-                if(keyValue.getKey().startsWith("LINK"))
-                  sf.addLink(keyValue.getValue());
-                else
-                  sf.setValue(keyValue.getKey(), keyValue.getValue());
+                jalview.datamodel.SequenceFeature sf
+                    = new jalview.datamodel.SequenceFeature(features[f].getType(),
+                    features[f].getDescription(), features[f].getStatus(),
+                    features[f].getBegin(), features[f].getEnd(),
+                    features[f].getFeatureGroup());
+
+                sf.setScore(features[f].getScore());
+                for (int od = 0; od < features[f].getOtherDataCount(); od++)
+                {
+                  OtherData keyValue = features[f].getOtherData(od);
+                  if (keyValue.getKey().startsWith("LINK"))
+                    sf.addLink(keyValue.getValue());
+                  else
+                    sf.setValue(keyValue.getKey(), keyValue.getValue());
 
-              }
+                }
 
-              al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
+                al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
+              }
             }
-          }
-          if (JSEQ[i].getPdbidsCount() > 0)
-          {
-            Pdbids[] ids = JSEQ[i].getPdbids();
-            for (int p = 0; p < ids.length; p++)
+            if (JSEQ[i].getPdbidsCount() > 0)
             {
-              jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
-              entry.setId(ids[p].getId());
-              entry.setType(ids[p].getType());
-              if (ids[p].getFile() != null)
+              Pdbids[] ids = JSEQ[i].getPdbids();
+              for (int p = 0; p < ids.length; p++)
               {
-                if (!pdbloaded.containsKey(ids[p].getFile()))
+                jalview.datamodel.PDBEntry entry = new jalview.datamodel.
+                    PDBEntry();
+                entry.setId(ids[p].getId());
+                entry.setType(ids[p].getType());
+                if (ids[p].getFile() != null)
                 {
-                  String tmppdb = loadPDBFile(file, ids[p].getId());
-                  entry.setFile(tmppdb);
-                  pdbloaded.put(ids[p].getId(), tmppdb);
+                  if (!pdbloaded.containsKey(ids[p].getFile()))
+                  {
+                    String tmppdb = loadPDBFile(file, ids[p].getId());
+                    entry.setFile(tmppdb);
+                    pdbloaded.put(ids[p].getId(), tmppdb);
+                  }
+                  else
+                    entry.setFile(pdbloaded.get(ids[p].getId()).toString());
                 }
-                else
-                  entry.setFile(pdbloaded.get(ids[p].getId()).toString());
-              }
 
-              al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
+                al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
+              }
             }
-          }
-          if(vamsasSeq[i].getDBRefCount()>0)
-          {
-            for(int d=0; d<vamsasSeq[i].getDBRefCount(); d++)
+            if (vamsasSeq[i].getDBRefCount() > 0)
             {
-              jalview.datamodel.DBRefEntry entry =
-                  new jalview.datamodel.DBRefEntry(
-                      vamsasSeq[i].getDBRef(d).getSource(),
-                      vamsasSeq[i].getDBRef(d).getVersion(),
-                      vamsasSeq[i].getDBRef(d).getAccessionId()
-                      );
-              al.getSequenceAt(i).getDatasetSequence().addDBRef(entry);
-            }
+              for (int d = 0; d < vamsasSeq[i].getDBRefCount(); d++)
+              {
+                jalview.datamodel.DBRefEntry entry =
+                    new jalview.datamodel.DBRefEntry(
+                        vamsasSeq[i].getDBRef(d).getSource(),
+                        vamsasSeq[i].getDBRef(d).getVersion(),
+                        vamsasSeq[i].getDBRef(d).getAccessionId()
+                    );
+                al.getSequenceAt(i).getDatasetSequence().addDBRef(entry);
+              }
 
+            }
           }
         }
 
 
-
          /////////////////////////////////
         //////////////////////////////////
         //LOAD ANNOTATIONS
@@ -1025,7 +1142,7 @@ public class Jalview2XML
             hideConservation = true,
             hideConsensus = true;
 
-        if (vamsasSet.getAnnotation() != null)
+        if (vamsasSet.getAnnotationCount()>0)
         {
             Annotation[] an = vamsasSet.getAnnotation();
 
@@ -1089,7 +1206,7 @@ public class Jalview2XML
                 if(an[i].getSequenceRef()!=null)
                 {
                   jaa.createSequenceMapping(
-                      al.findName(an[i].getSequenceRef()), 1
+                      al.findName(an[i].getSequenceRef()), 1, true
                       );
                   al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
                 }
@@ -1127,12 +1244,11 @@ public class Jalview2XML
                 }
 
                 Vector seqs = new Vector();
-                int[] ids = groups[i].getSeq();
 
-                for (int s = 0; s < ids.length; s++)
+                for (int s = 0; s < groups[i].getSeqCount(); s++)
                 {
-                    seqs.addElement((jalview.datamodel.SequenceI) seqids.elementAt(
-                            ids[s]));
+                    String seqId = groups[i].getSeq(s)+"";
+                    seqs.addElement((jalview.datamodel.SequenceI) seqRefIds.get(seqId));
                 }
 
                 jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(seqs,
@@ -1160,11 +1276,17 @@ public class Jalview2XML
 
         /////////////////////////////////
         // LOAD VIEWPORT
-        Viewport[] views = jms.getViewport();
-        Viewport view = views[0]; // DEAL WITH MULTIPLE VIEWPORTS LATER
 
+        AlignFrame af = new AlignFrame(al,
+                                       view.getWidth(),
+                                       view.getHeight() );
+
+        af.viewport.sequenceSetID = view.getSequenceSetId();
 
-        AlignFrame af = new AlignFrame(al);
+        af.viewport.gatherViewsHere = view.getGatheredViews();
+
+        if (view.getSequenceSetId() != null)
+          PaintRefresher.Register(af.alignPanel, view.getSequenceSetId());
 
         if(hiddenSeqs!=null)
         {
@@ -1178,15 +1300,19 @@ public class Jalview2XML
             }
           }
 
+          jalview.datamodel.SequenceI [] hseqs = new
+              jalview.datamodel.SequenceI[hiddenSeqs.size()];
+
           for(int s=0; s<hiddenSeqs.size(); s++)
-          {
-              af.viewport.hideSequence(
-                (jalview.datamodel.SequenceI)hiddenSeqs.elementAt(s));
-          }
+            hseqs[s] = (jalview.datamodel.SequenceI)hiddenSeqs.elementAt(s);
+
+          af.viewport.hideSequence( hseqs );
+
         }
 
 
-        if(hideConsensus || hideQuality || hideConservation)
+        if((hideConsensus || hideQuality || hideConservation)
+            && al.getAlignmentAnnotation()!=null)
         {
           int hSize = al.getAlignmentAnnotation().length;
           for (int h = 0; h < hSize; h++)
@@ -1209,6 +1335,7 @@ public class Jalview2XML
           af.alignPanel.adjustAnnotationHeight();
         }
 
+        af.viewport.viewName = view.getViewName();
         af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
             view.getHeight());
         af.viewport.setStartRes(view.getStartRes());
@@ -1354,7 +1481,7 @@ public class Jalview2XML
             if(cs!=null)
             {
               cs.setThreshold(view.getPidThreshold(), true);
-              cs.setConsensus(af.viewport.vconsensus);
+              cs.setConsensus(af.viewport.hconsensus);
             }
         }