Merge branch 'develop' into spike/matrix_annot
[jalview.git] / src / jalview / renderer / AnnotationRenderer.java
index 5083ac8..f5562d7 100644 (file)
@@ -29,8 +29,12 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.ProfilesI;
+import jalview.renderer.api.AnnotationRendererFactoryI;
+import jalview.renderer.api.AnnotationRowRendererI;
 import jalview.schemes.ColourSchemeI;
+import jalview.schemes.NucleotideColourScheme;
 import jalview.schemes.ResidueProperties;
+import jalview.schemes.ZappoColourScheme;
 import jalview.util.Platform;
 
 import java.awt.BasicStroke;
@@ -70,7 +74,7 @@ public class AnnotationRenderer
   boolean av_renderHistogram = true, av_renderProfile = true,
           av_normaliseProfile = false;
 
-  ColourSchemeI profcolour = null;
+  ResidueShaderI profcolour = null;
 
   private ColumnSelection columnSelection;
 
@@ -150,6 +154,7 @@ public class AnnotationRenderer
     hStrucConsensus = null;
     fadedImage = null;
     annotationPanel = null;
+    rendererFactoryI = null;
   }
 
   void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
@@ -299,6 +304,7 @@ public class AnnotationRenderer
       useClip = false;
     }
 
+    rendererFactoryI = AnnotationRendererFactory.getRendererFactory();
     updateFromAlignViewport(av);
   }
 
@@ -312,13 +318,17 @@ public class AnnotationRenderer
     av_renderHistogram = av.isShowConsensusHistogram();
     av_renderProfile = av.isShowSequenceLogo();
     av_normaliseProfile = av.isNormaliseSequenceLogo();
-    profcolour = av.getGlobalColourScheme();
-    if (profcolour == null)
+    profcolour = av.getResidueShading();
+    if (profcolour == null || profcolour.getColourScheme() == null)
     {
-      // Set the default colour for sequence logo if the alignnent has no
-      // colourscheme set
-      profcolour = av.getAlignment().isNucleotide() ? new jalview.schemes.NucleotideColourScheme()
-              : new jalview.schemes.ZappoColourScheme();
+      /*
+       * Use default colour for sequence logo if 
+       * the alignment has no colourscheme set
+       * (would like to use user preference but n/a for applet)
+       */
+      ColourSchemeI col = av.getAlignment().isNucleotide() ? new NucleotideColourScheme()
+              : new ZappoColourScheme();
+      profcolour = new ResidueShader(col);
     }
     columnSelection = av.getColumnSelection();
     hconsensus = av.getSequenceConsensusHash();
@@ -400,6 +410,8 @@ public class AnnotationRenderer
 
   boolean rna = false;
 
+  private AnnotationRendererFactoryI rendererFactoryI;
+
   /**
    * Render the annotation rows associated with an alignment.
    * 
@@ -1029,18 +1041,35 @@ public class AnnotationRenderer
           }
           else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
           {
-            if (row.autoCalculated && row.label != null
-                    && row.label.startsWith("Consen"))
-            {
-              drawProfileDensity(g, row, row_annotations, startRes, endRes,
-                      row.graphMin, row.graphMax, y);
-            }
-            else
-            {
             drawBarGraph(g, row, row_annotations, startRes, endRes,
                     row.graphMin, row.graphMax, y, renderHistogram,
                     renderProfile, normaliseProfile);
+          }
+          else
+          {
+            AnnotationRowRendererI renderer = rendererFactoryI
+                    .getRendererFor(row);
+            if (renderer != null)
+            {
+              renderer.renderRow(g, charWidth, charHeight,
+                      hasHiddenColumns, av, columnSelection, row,
+                      row_annotations, startRes, endRes, row.graphMin,
+                      row.graphMax, y);
+            }
+            if (debugRedraw)
+            {
+              if (renderer == null)
+              {
+                System.err.println("No renderer found for "
+                        + row.toString());
+              }
+              else
+              {
+                System.err.println("rendered with "
+                        + renderer.getClass().toString());
+              }
             }
+
           }
         }
       }
@@ -1627,129 +1656,4 @@ public class AnnotationRenderer
 
     }
   }
-
-  void drawProfileDensity(Graphics g, AlignmentAnnotation _aa,
-          Annotation[] aa_annotations, int sRes, int eRes, float min,
-          float max, int y)
-  {
-    if (sRes > aa_annotations.length)
-    {
-      return;
-    }
-    Font ofont = g.getFont();
-    eRes = Math.min(eRes, aa_annotations.length);
-
-    int x = 0, y2 = y;
-
-
-
-    g.setColor(Color.pink);
-
-    g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
-
-    int column;
-    int aaMax = aa_annotations.length - 1;
-    while (x < eRes - sRes)
-    {
-      column = sRes + x;
-      if (hasHiddenColumns)
-      {
-        column = columnSelection.adjustForHiddenColumns(column);
-      }
-
-      if (column > aaMax)
-      {
-        break;
-      }
-
-      if (aa_annotations[column] == null)
-      {
-        x++;
-        continue;
-      }
-      if (aa_annotations[column].colour == null)
-      {
-        g.setColor(Color.black);
-      }
-      else
-      {
-        g.setColor(aa_annotations[column].colour);
-      }
-
-
-      /*
-       * {profile type, #values, total count, char1, pct1, char2, pct2...}
-       */
-      int profl[] = getProfileFor(_aa, column);
-
-      // just try to draw the logo if profl is not null
-      if (profl != null && profl[2] != 0)
-      {
-        boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
-        boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
-        float ht = y2; // - _aa.graphHeight;
-        char[] dc;
-
-        /**
-         * Render a single base for a sequence profile, a base pair for
-         * structure profile, and a triplet for a cdna profile
-         */
-        dc = new char[isStructureProfile ? 2 : (isCdnaProfile ? 3 : 1)];
-
-        double scale = _aa.graphHeight / (profl[1]);
-
-        /*
-         * Traverse the character(s)/percentage data in the array
-         */
-        int c = 3;
-        int valuesProcessed = 0;
-        // profl[1] is the number of values in the profile
-
-        while (valuesProcessed < profl[1])
-        {
-          if (isStructureProfile)
-          {
-            // todo can we encode a structure pair as an int, like codons?
-            dc[0] = (char) profl[c++];
-            dc[1] = (char) profl[c++];
-          }
-          else if (isCdnaProfile)
-          {
-            dc = CodingUtils.decodeCodon(profl[c++]);
-          }
-          else
-          {
-            dc[0] = (char) profl[c++];
-          }
-
-          ht -= scale;
-          // next profl[] position is profile % for the character(s)
-          // render boxes
-          g.setColor(jalview.util.ColorUtils.getGraduatedColour(profl[c++],
-                  0f, Color.gray, 100, Color.blue));
-          // g.setColor(profcolour.findColour(dc[0]).darker());
-
-          g.fillRect(x * charWidth, (int) ht, charWidth, (int) scale);
-          valuesProcessed++;
-        }
-        g.setFont(ofont);
-      }
-      x++;
-
-    }
-
-    // if (_aa.threshold != null)
-    // {
-    // g.setColor(_aa.threshold.colour);
-    // Graphics2D g2 = (Graphics2D) g;
-    // g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
-    // BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
-    //
-    // y2 = (int) (y - ((_aa.threshold.value - min) / range)
-    // * _aa.graphHeight);
-    // g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
-    // g2.setStroke(new BasicStroke());
-    // }
-  }
-
 }