Extend XML model for graduated feature colours, group associated manual/auto annotati...
authorjprocter <Jim Procter>
Mon, 5 Apr 2010 15:36:25 +0000 (15:36 +0000)
committerjprocter <Jim Procter>
Mon, 5 Apr 2010 15:36:25 +0000 (15:36 +0000)
src/jalview/gui/Jalview2XML.java

index 4d883c4..51e26b8 100755 (executable)
@@ -762,6 +762,10 @@ public class Jalview2XML
     }
 
     // SAVE ANNOTATIONS
+    /**
+     * store forward refs from an annotationRow to any groups
+     */
+    IdentityHashMap groupRefs = new IdentityHashMap();
     if (jal.getAlignmentAnnotation() != null)
     {
       jalview.datamodel.AlignmentAnnotation[] aa = jal
@@ -778,15 +782,7 @@ public class Jalview2XML
 
         an.setId(aa[i].annotationId);
 
-        if (aa[i] == av.quality || aa[i] == av.conservation
-                || aa[i] == av.consensus)
-        {
-          an.setLabel(aa[i].label);
-          an.setGraph(true);
-          vamsasSet.addAnnotation(an);
-          continue;
-        }
-
+        
         an.setVisible(aa[i].visible);
 
         an.setDescription(aa[i].description);
@@ -797,6 +793,27 @@ public class Jalview2XML
           // sequence rather than its display name
           an.setSequenceRef(aa[i].sequenceRef.getName());
         }
+        if (aa[i].groupRef!=null)
+        {
+          Object groupIdr = groupRefs.get(aa[i].groupRef);
+          if (groupIdr==null)
+          {
+            // make a locally unique String
+            groupRefs.put(aa[i].groupRef, groupIdr = (""+System.currentTimeMillis()+aa[i].groupRef.getName()+groupRefs.size()));
+          }
+          an.setGroupRef(groupIdr.toString());
+        }
+        if (aa[i] == av.quality || aa[i] == av.conservation
+                || aa[i] == av.consensus || aa[i].autoCalculated)
+        {
+          // new way of indicating autocalculated annotation - 
+          an.setAutoCalculated(aa[i].autoCalculated);
+          // write a stub for this annotation - indicate presence of autocalc rows
+          an.setLabel(aa[i].label);
+          an.setGraph(true);
+          vamsasSet.addAnnotation(an);
+          continue;
+        }
 
         if (aa[i].graph > 0)
         {
@@ -865,7 +882,6 @@ public class Jalview2XML
         vamsasSet.addAnnotation(an);
       }
     }
-
     // SAVE GROUPS
     if (jal.getGroups() != null)
     {
@@ -879,9 +895,11 @@ public class Jalview2XML
                 .getGroups().elementAt(i);
         groups[i].setStart(sg.getStartRes());
         groups[i].setEnd(sg.getEndRes());
-        groups[i].setName(sg.getName()); // TODO later sequence group should
-        // specify IDs of sequences, not just
-        // names
+        groups[i].setName(sg.getName()); 
+        if (groupRefs.containsKey(sg)) {
+          // group has references so set it's ID field 
+          groups[i].setId(groupRefs.get(sg).toString());
+        }
         if (sg.cs != null)
         {
           if (sg.cs.conservationApplied())
@@ -927,6 +945,9 @@ public class Jalview2XML
         groups[i].setTextCol2(sg.textColour2.getRGB());
         groups[i].setTextColThreshold(sg.thresholdTextColour);
         groups[i].setShowUnconserved(sg.getShowunconserved());
+        groups[i].setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
+        groups[i].setShowConsensusHistogram(sg.isShowConsensusHistogram());
+        groups[i].setShowConsensusProfile(sg.isIncludeAllConsSymbols());
         for (int s = 0; s < sg.getSize(); s++)
         {
           jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg
@@ -1038,7 +1059,15 @@ public class Jalview2XML
     view.setTextCol1(av.textColour.getRGB());
     view.setTextCol2(av.textColour2.getRGB());
     view.setTextColThreshold(av.thresholdTextColour);
-
+    view.setShowConsensusHistogram(av.isShowConsensusHistogram());
+    view.setShowConsensusProfile(av.isShowConsensusProfile());
+    view.setShowGroupConsensus(av.isShowGroupConsensus());
+    view.setShowGroupConservation(av.isShowGroupConservation());
+    view.setShowNPfeatureTooltip(av.isShowNpFeats());
+    view.setShowDbRefTooltip(av.isShowDbRefs());
+    view.setFollowHighlight(av.followHighlight);
+    view.setFollowSelection(av.followSelection);
+    view.setIgnoreGapsinConsensus(av.getIgnoreGapsConsensus());
     if (av.featuresDisplayed != null)
     {
       jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
@@ -1046,13 +1075,27 @@ public class Jalview2XML
       String[] renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
 
       Vector settingsAdded = new Vector();
+      Object gstyle=null;
+      GraduatedColor gcol = null; 
       for (int ro = 0; ro < renderOrder.length; ro++)
       {
+        gstyle = ap.seqPanel.seqCanvas.getFeatureRenderer().getFeatureStyle(renderOrder[ro]);
         Setting setting = new Setting();
         setting.setType(renderOrder[ro]);
-        setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
-                .getColour(renderOrder[ro]).getRGB());
-
+        if (gstyle instanceof GraduatedColor) {
+          gcol  = (GraduatedColor) gstyle;
+          setting.setColour(gcol.getMaxColor().getRGB());
+          setting.setMincolour(gcol.getMinColor().getRGB());
+          setting.setMin(gcol.getMin());
+          setting.setMax(gcol.getMax());
+          setting.setColourByLabel(gcol.isColourByLabel());
+          setting.setAutoScale(gcol.isAutoScale());
+          setting.setThreshold(gcol.getThresh());
+          setting.setThreshstate(gcol.getThreshType());
+        } else {
+          setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()      .getColour(renderOrder[ro]).getRGB());
+        }
+        
         setting.setDisplay(av.featuresDisplayed
                 .containsKey(renderOrder[ro]));
         float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(
@@ -1348,11 +1391,15 @@ public class Jalview2XML
   {
     String id = null;
     jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
-
+    boolean newucs=false;
     if (!userColours.contains(ucs))
     {
       userColours.add(ucs);
-
+      newucs=true;
+    }
+    id = "ucs" + userColours.indexOf(ucs);
+    if (newucs) {
+      // actually create the scheme's entry in the XML model
       java.awt.Color[] colours = ucs.getColours();
       jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
       jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
@@ -1376,7 +1423,6 @@ public class Jalview2XML
         }
       }
 
-      id = "ucs" + userColours.indexOf(ucs);
       uc.setId(id);
       uc.setUserColourScheme(jbucs);
       jms.addUserColours(uc);
@@ -1976,6 +2022,10 @@ public class Jalview2XML
     // ////////////////////////////////
     // LOAD ANNOTATIONS
     boolean hideQuality = true, hideConservation = true, hideConsensus = true;
+    /**
+     * store any annotations which forward reference a group's ID
+     */
+    Hashtable groupAnnotRefs = new Hashtable();
 
     if (vamsasSet.getAnnotationCount() > 0)
     {
@@ -1998,7 +2048,7 @@ public class Jalview2XML
         {
           hideConsensus = false;
           continue;
-        }
+        }        
         // set visiblity for other annotation in this view
         if (an[i].getId() != null
                 && annotationIds.containsKey(an[i].getId()))
@@ -2086,14 +2136,31 @@ public class Jalview2XML
             al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
           }
         }
+        // and make a note of any group association
+        if (an[i].getGroupRef()!=null && an[i].getGroupRef().length()>0)
+        {
+          groupAnnotRefs.put(an[i].getGroupRef(),jaa);
+        }
+
         if (an[i].hasScore())
         {
           jaa.setScore(an[i].getScore());
         }
-
         if (an[i].hasVisible())
           jaa.visible = an[i].getVisible();
-
+        
+        if (an[i].hasCentreColLabels())
+          jaa.centreColLabels = an[i].getCentreColLabels();
+        
+        if (an[i].hasScaleColLabels())
+        {
+          jaa.scaleColLabel = an[i].getScaleColLabels();
+        }
+        if (an[i].hasAutoCalculated() && an[i].isAutoCalculated())
+        {
+          // newer files have an 'autoCalculated' flag and store calculation state in viewport properties
+          jaa.autoCalculated = true; // means annotation will be marked for update at end of load. 
+        }
         al.addAnnotation(jaa);
       }
     }
@@ -2159,7 +2226,16 @@ public class Jalview2XML
         sg.setShowunconserved(groups[i].hasShowUnconserved() ? groups[i]
                 .isShowUnconserved() : false);
         sg.thresholdTextColour = groups[i].getTextColThreshold();
-
+        if (groups[i].hasShowConsensusHistogram()) {
+          sg.setShowConsensusHistogram(groups[i].isShowConsensusHistogram());
+        };
+        if (groups[i].hasShowConsensusProfile()) {
+          sg.setIncludeAllConsSymbols(groups[i].isShowConsensusProfile());
+        }
+        if (groups[i].hasIgnoreGapsinConsensus())
+        {
+          sg.setIgnoreGapsConsensus(groups[i].getIgnoreGapsinConsensus());
+        }
         if (groups[i].getConsThreshold() != 0)
         {
           jalview.analysis.Conservation c = new jalview.analysis.Conservation(
@@ -2169,8 +2245,19 @@ public class Jalview2XML
           c.verdict(false, 25);
           sg.cs.setConservation(c);
         }
-
+        
+        if (groups[i].getId()!=null
+                && groupAnnotRefs.size()>0)
+        {
+          // re-instate unique group/annotation row reference
+          jalview.datamodel.AlignmentAnnotation jaa = (jalview.datamodel.AlignmentAnnotation) groupAnnotRefs.get(groups[i].getId());
+          if (jaa!=null)
+          {
+            jaa.groupRef = sg;
+          }
+        }
         al.addGroup(sg);
+        
       }
     }
 
@@ -2705,6 +2792,47 @@ public class Jalview2XML
     {
       af.viewport.showSequenceFeatures = true;
     }
+    if (view.hasCentreColumnLabels())
+    {
+      af.viewport.setCentreColumnLabels(view.getCentreColumnLabels());
+    }
+    if (view.hasIgnoreGapsinConsensus())
+    {
+      af.viewport.ignoreGapsInConsensusCalculation=view.getIgnoreGapsinConsensus();
+    }
+    if (view.hasFollowHighlight())
+    {
+      af.viewport.followHighlight = view.getFollowHighlight();
+    }
+    if (view.hasFollowSelection())
+    {
+      af.viewport.followSelection = view.getFollowSelection();
+    }
+    if (view.hasShowConsensusHistogram())
+    {
+      af.viewport.setShowConsensusHistogram(view.getShowConsensusHistogram());
+    }
+    if (view.hasShowConsensusProfile())
+    {
+      af.viewport.setShowConsensusProfile(view.getShowConsensusProfile());
+    }
+    if (view.hasShowDbRefTooltip())
+    {
+      af.viewport.setShowDbRefs(view.getShowDbRefTooltip());
+    }
+    if (view.hasShowNPfeatureTooltip())
+    {
+      af.viewport.setShowNpFeats(view.hasShowNPfeatureTooltip());
+    }
+    if (view.hasShowGroupConsensus())
+    {
+      af.viewport.setShowGroupConsensus(view.getShowGroupConsensus());
+    }
+    if (view.hasShowGroupConservation())
+    {
+      af.viewport.setShowGroupConservation(view.getShowGroupConservation());
+    }
+    
     // recover featre settings
     if (jms.getFeatureSettings() != null)
     {
@@ -2716,16 +2844,28 @@ public class Jalview2XML
         Setting setting = jms.getFeatureSettings().getSetting(fs);
         if (setting.hasMincolour())
         {
-          // TODO: determine how to set data independent bounds for a graduated
-          // colour scheme's range.
-          GraduatedColor gc = new GraduatedColor(new java.awt.Color(setting
+          GraduatedColor gc = setting.hasMin() ? new GraduatedColor(new java.awt.Color(setting
+                  .getMincolour()),
+                  new java.awt.Color(setting.getColour()), setting.getMin(), setting.getMax())
+          : new GraduatedColor(new java.awt.Color(setting
                   .getMincolour()),
-                  new java.awt.Color(setting.getColour()), 0, 1);
+                  new java.awt.Color(setting.getColour()), 0,1);
           if (setting.hasThreshold())
           {
             gc.setThresh(setting.getThreshold());
             gc.setThreshType(setting.getThreshstate());
           }
+          gc.setAutoScaled(true); // default
+          if (setting.hasAutoScale())
+          {
+            gc.setAutoScaled(setting.getAutoScale());
+          }
+          if (setting.hasColourByLabel())
+          {
+            gc.setColourByLabel(setting.getColourByLabel());
+          }
+          // and put in the feature colour table.
+          af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(setting.getType(),gc);
         }
         else
         {
@@ -2768,6 +2908,7 @@ public class Jalview2XML
     }
 
     af.setMenusFromViewport(af.viewport);
+    af.alignPanel.updateAnnotation(false); // recompute any autoannotation
     // TODO: we don't need to do this if the viewport is aready visible.
     Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(), view
             .getHeight());