JAL-4134 JAL-3855 store/restore groups, tree and threshold used to cluster a PAE...
[jalview.git] / src / jalview / project / Jalview2XML.java
index 8d2efe0..36c87ce 100644 (file)
@@ -26,6 +26,7 @@ import static jalview.math.RotatableMatrix.Axis.Z;
 
 import java.awt.Color;
 import java.awt.Font;
+import java.awt.FontMetrics;
 import java.awt.Rectangle;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
@@ -44,6 +45,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.BitSet;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.GregorianCalendar;
@@ -89,6 +91,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;
@@ -148,6 +152,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;
@@ -192,6 +197,7 @@ 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;
@@ -1551,6 +1557,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());
@@ -2289,6 +2297,43 @@ 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()));
+              // consider using an opaque to/from -> allow instance to control its representation ?
+              xmlmat.setElements(ContactMatrix.contactToFloatString(cm));
+              if (cm.hasGroups())
+              {
+                for (BitSet gp: cm.getGroups())
+                {
+                  BigInteger val = new BigInteger(gp.toByteArray());
+                  xmlmat.getGroups().add(val.toString());
+                }
+              }
+              if (cm.hasTree())
+              {
+                // provenance object for tree ?
+                xmlmat.getNewick().add(cm.getNewick());
+                xmlmat.setTreeMethod(cm.getTreeMethod());
+              }
+              if (cm.hasCutHeight())
+              {
+                xmlmat.setCutHeight(cm.getCutHeight());
+              }
+              
+              // set/get properties
+              an.getContactmatrix().add(xmlmat);
+            }
+          }
+        }
       }
       else
       {
@@ -2319,10 +2364,9 @@ public class Jalview2XML
       {
         for (String pr : annotation.getProperties())
         {
-          jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
+          jalview.xml.binding.jalview.Property prop = new jalview.xml.binding.jalview.Property();
           prop.setName(pr);
           prop.setValue(annotation.getProperty(pr));
-          // an.addProperty(prop);
           an.getProperty().add(prop);
         }
       }
@@ -3897,11 +3941,69 @@ public class Jalview2XML
         jaa.setCalcId(annotation.getCalcId());
         if (annotation.getProperty().size() > 0)
         {
-          for (Annotation.Property prop : annotation.getProperty())
+          for (jalview.xml.binding.jalview.Property prop : annotation.getProperty())
           {
             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.getElements(),
+                                  xmlmat.getCols().intValue(),
+                                  xmlmat.getRows().intValue());
+
+                  PAEContactMatrix newpae = new PAEContactMatrix(
+                          jaa.sequenceRef, elements);
+                  List<BitSet> newgroups=new ArrayList<BitSet>();
+                  if (xmlmat.getGroups().size()>0)
+                  {
+                    for (String sgroup:xmlmat.getGroups())
+                    {
+                      try {
+                        BigInteger group = new BigInteger(sgroup);
+                        newgroups.add(BitSet.valueOf(group.toByteArray()));
+                      } catch (NumberFormatException nfe)
+                      {
+                        Console.error("Problem parsing groups for a contact matrix (\""+sgroup+"\"",nfe);
+                      }
+                    }
+                  }
+                  String nwk=xmlmat.getNewick().size()>0 ? xmlmat.getNewick().get(0):null;
+                  if  (xmlmat.getNewick().size()>1)
+                  {
+                    Console.log.info(
+                            "Ignoring additional clusterings for contact matrix");
+                  }
+
+                  String treeMethod = xmlmat.getTreeMethod();
+                  double thresh = xmlmat.getCutHeight()!=null ? xmlmat.getCutHeight() : 0;
+                  newpae.restoreGroups(newgroups, treeMethod, nwk, thresh);
+                  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));
@@ -4828,7 +4930,12 @@ public class Jalview2XML
     viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
     viewport.setFont(new Font(view.getFontName(),
             safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
-            true);
+            (view.getCharWidth()!=null) ? false : true);
+    if (view.getCharWidth()!=null)
+    {
+      viewport.setCharWidth(view.getCharWidth());
+      viewport.setCharHeight(view.getCharHeight());
+    }
     ViewStyleI vs = viewport.getViewStyle();
     vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
     viewport.setViewStyle(vs);