JAL-4133 store/restore alignment view’s charWidth and charHeight in Jalview project
[jalview.git] / src / jalview / project / Jalview2XML.java
index c8f9be6..8142435 100644 (file)
@@ -89,6 +89,8 @@ import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ContactMatrix;
+import jalview.datamodel.ContactMatrixI;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GeneLocus;
 import jalview.datamodel.GraphLine;
@@ -112,6 +114,7 @@ import jalview.gui.AppVarna;
 import jalview.gui.Desktop;
 import jalview.gui.JvOptionPane;
 import jalview.gui.OOMWarning;
+import jalview.gui.OverviewPanel;
 import jalview.gui.PCAPanel;
 import jalview.gui.PaintRefresher;
 import jalview.gui.SplitFrame;
@@ -147,6 +150,7 @@ import jalview.viewmodel.ViewportRanges;
 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
+import jalview.ws.datamodel.alphafold.PAEContactMatrix;
 import jalview.ws.jws2.Jws2Discoverer;
 import jalview.ws.jws2.dm.AAConSettings;
 import jalview.ws.jws2.jabaws2.Jws2Instance;
@@ -185,11 +189,13 @@ import jalview.xml.binding.jalview.JalviewModel.UserColours;
 import jalview.xml.binding.jalview.JalviewModel.Viewport;
 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
+import jalview.xml.binding.jalview.JalviewModel.Viewport.Overview;
 import jalview.xml.binding.jalview.JalviewUserColours;
 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
 import jalview.xml.binding.jalview.MapListType.MapListFrom;
 import jalview.xml.binding.jalview.MapListType.MapListTo;
 import jalview.xml.binding.jalview.Mapping;
+import jalview.xml.binding.jalview.MatrixType;
 import jalview.xml.binding.jalview.NoValueColour;
 import jalview.xml.binding.jalview.ObjectFactory;
 import jalview.xml.binding.jalview.PcaDataType;
@@ -1492,6 +1498,23 @@ public class Jalview2XML
       view.setStartRes(vpRanges.getStartRes());
       view.setStartSeq(vpRanges.getStartSeq());
 
+      OverviewPanel ov = ap.getOverviewPanel();
+      if (ov != null)
+      {
+        Overview overview = new Overview();
+        overview.setTitle(ov.getTitle());
+        Rectangle bounds = ov.getFrameBounds();
+        overview.setXpos(bounds.x);
+        overview.setYpos(bounds.y);
+        overview.setWidth(bounds.width);
+        overview.setHeight(bounds.height);
+        overview.setShowHidden(ov.isShowHiddenRegions());
+        overview.setGapColour(ov.getCanvas().getGapColour().getRGB());
+        overview.setResidueColour(
+                ov.getCanvas().getResidueColour().getRGB());
+        overview.setHiddenColour(ov.getCanvas().getHiddenColour().getRGB());
+        view.setOverview(overview);
+      } 
       if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
       {
         view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
@@ -1532,6 +1555,8 @@ public class Jalview2XML
 
       view.setConservationSelected(av.getConservationSelected());
       view.setPidSelected(av.getAbovePIDThreshold());
+      view.setCharHeight(av.getCharHeight());
+      view.setCharWidth(av.getCharWidth());
       final Font font = av.getFont();
       view.setFontName(font.getName());
       view.setFontSize(font.getSize());
@@ -2270,6 +2295,22 @@ public class Jalview2XML
           line.setColour(annotation.getThreshold().colour.getRGB());
           an.setThresholdLine(line);
         }
+        if (annotation.graph==AlignmentAnnotation.CONTACT_MAP)
+        {
+          if (annotation.sequenceRef.getContactMaps()!=null)
+          {
+            ContactMatrixI cm = annotation.sequenceRef.getContactMatrixFor(annotation);
+            if (cm!=null)
+            {
+              MatrixType xmlmat = new MatrixType();
+              xmlmat.setType(cm.getType());
+              xmlmat.setRows(BigInteger.valueOf(cm.getWidth()));
+              xmlmat.setCols(BigInteger.valueOf(cm.getHeight()));
+              xmlmat.setValue(ContactMatrix.contactToFloatString(cm));
+              an.getContactmatrix().add(xmlmat);
+            }
+          }
+        }
       }
       else
       {
@@ -3883,6 +3924,40 @@ public class Jalview2XML
             jaa.setProperty(prop.getName(), prop.getValue());
           }
         }
+        if (jaa.graph == AlignmentAnnotation.CONTACT_MAP)
+        {
+          if (annotation.getContactmatrix() != null
+                  && annotation.getContactmatrix().size() > 0)
+          {
+            for (MatrixType xmlmat : annotation.getContactmatrix())
+            {
+              if (PAEContactMatrix.PAEMATRIX.equals(xmlmat.getType()))
+              {
+                if (!xmlmat.getRows().equals(xmlmat.getCols()))
+                {
+                  Console.error("Can't handle non square PAE Matrices");
+                }
+                else
+                {
+                  float[][] elements = ContactMatrix
+                          .fromFloatStringToContacts(xmlmat.getValue(),
+                                  xmlmat.getCols().intValue(),
+                                  xmlmat.getRows().intValue());
+
+                  PAEContactMatrix newpae = new PAEContactMatrix(
+                          jaa.sequenceRef, elements);
+                  jaa.sequenceRef.addContactListFor(jaa, newpae);
+                }
+              }
+              else
+              {
+                Console.error("Ignoring CONTACT_MAP annotation with type "
+                        + xmlmat.getType());
+              }
+            }
+          }
+        }
+
         if (jaa.autoCalculated)
         {
           autoAlan.add(new JvAnnotRow(i, jaa));
@@ -4093,7 +4168,7 @@ public class Jalview2XML
     }
 
     /*
-     * Load any trees, PDB structures and viewers
+     * Load any trees, PDB structures and viewers, Overview
      * 
      * Not done if flag is false (when this method is used for New View)
      */
@@ -4103,12 +4178,49 @@ public class Jalview2XML
       loadPCAViewers(jalviewModel, ap);
       loadPDBStructures(jprovider, jseqs, af, ap);
       loadRnaViewers(jprovider, jseqs, ap);
+      loadOverview(view, jalviewModel.getVersion(), af);
     }
     // and finally return.
     return af;
   }
 
   /**
+   * Load Overview window, restoring colours, 'show hidden regions' flag, title
+   * and geometry as saved
+   * 
+   * @param view
+   * @param af
+   */
+  protected void loadOverview(Viewport view, String version, AlignFrame af)
+  {
+    if (!isVersionStringLaterThan("2.11.3",
+            version) && view.getOverview()==null)
+    {
+      return;
+    }
+    /*
+     * first close any Overview that was opened automatically
+     * (if so configured in Preferences) so that the view is
+     * restored in the same state as saved
+     */
+    af.alignPanel.closeOverviewPanel();
+
+    Overview overview = view.getOverview();
+    if (overview != null)
+    {
+      OverviewPanel overviewPanel = af
+              .openOverviewPanel(overview.isShowHidden());
+      overviewPanel.setTitle(overview.getTitle());
+      overviewPanel.setFrameBounds(overview.getXpos(), overview.getYpos(),
+              overview.getWidth(), overview.getHeight());
+      Color gap = new Color(overview.getGapColour());
+      Color residue = new Color(overview.getResidueColour());
+      Color hidden = new Color(overview.getHiddenColour());
+      overviewPanel.getCanvas().setColours(gap, residue, hidden);
+    }
+  }
+
+  /**
    * 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.
@@ -4625,7 +4737,7 @@ public class Jalview2XML
    *          - minimum version we are comparing against
    * @param version
    *          - version of data being processsed
-   * @return
+   * @return true if version is equal to or later than supported
    */
   public static boolean isVersionStringLaterThan(String supported,
           String version)
@@ -4770,6 +4882,11 @@ public class Jalview2XML
     viewport.setIncrement(safeInt(view.getConsThreshold()));
     viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
     viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
+    if (view.getCharWidth()!=null)
+    {
+      viewport.setCharWidth(view.getCharWidth());
+      viewport.setCharHeight(view.getCharHeight());
+    }
     viewport.setFont(new Font(view.getFontName(),
             safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
             true);
@@ -5031,6 +5148,7 @@ public class Jalview2XML
     {
       splitFrameCandidates.put(view, af);
     }
+
     return af;
   }
 
@@ -5094,7 +5212,10 @@ public class Jalview2XML
               + annotationId);
       return null;
     }
-    if (matchedAnnotation.getThreshold() == null)
+    // belt-and-braces create a threshold line if the 
+    // colourscheme needs one but the matchedAnnotation doesn't have one
+    if (safeInt(viewAnnColour.getAboveThreshold()) != 0
+            && matchedAnnotation.getThreshold() == null)
     {
       matchedAnnotation.setThreshold(
               new GraphLine(safeFloat(viewAnnColour.getThreshold()),