JAL-2630 first pass groovy colour scheme (with slight refactoring)
[jalview.git] / examples / groovy / colourSchemes.groovy
diff --git a/examples/groovy/colourSchemes.groovy b/examples/groovy/colourSchemes.groovy
new file mode 100644 (file)
index 0000000..84eabbf
--- /dev/null
@@ -0,0 +1,125 @@
+import java.awt.Color;
+import jalview.schemes.ResidueColourScheme;
+import jalview.schemes.ColourSchemes;
+import jalview.datamodel.AnnotatedCollectionI;
+import java.util.Map;
+import jalview.datamodel.SequenceI;
+
+/*
+ * Example script that registers two new alignment colour schemes
+ */
+
+/*
+ * Class that defines a colour scheme where odd columns are red,
+ * even numbered columns are blue, and gaps are yellow  
+ */
+class Stripy extends ResidueColourScheme {
+    Stripy() { }
+    String getSchemeName() { "stripy" }
+    Stripy getInstance(AnnotatedCollectionI coll, Map map) { new Stripy() }
+    Color findColour(char res, int col, SequenceI seq) {
+      // determine the colour
+      Color colour = findColour(res, col)
+      // let Jalview apply conservation or consensus shading
+      adjustColour(res, col, colour);
+    }
+    Color findColour(char res, int col) {
+        if (res == ' ' || res == '-' || res == '.') 
+        {
+            Color.yellow
+         } else if (col % 2 == 0) 
+         {
+            Color.blue
+         } else 
+         {
+            Color.red
+         }
+    }
+}
+
+/*
+ * Class that defines a colour scheme graduated 
+ * (approximately) by amino acid weight  
+ */
+class ByWeight extends ResidueColourScheme {
+    int min = 75
+    int max = 204
+    ByWeight() { }
+    boolean isPeptideSpecific() {true}
+    String getSchemeName() { "By Weight" }
+    ByWeight getInstance(AnnotatedCollectionI coll, Map map) { new ByWeight() }
+    Color makeColour(int weight) {
+      int i = 255 * (weight - min) / (max - min);
+      new Color(i, 0, i);
+    }
+    Color findColour(char res, int col, SequenceI seq) {
+      // determine the colour
+      Color colour = findColour(res, col)
+      // let Jalview apply any conservation or consensus shading
+      adjustColour(res, col, colour);
+    }
+    Color findColour(char res, int col) {
+        switch (res) {
+          case ' ':
+          case '-':
+          case '.':
+            Color.white
+             break
+          case 'A':
+            makeColour(89)
+            break
+          case 'R':
+            makeColour(174)
+            break
+          case 'N':
+          case 'D':
+          case 'B':
+          case 'I':
+          case 'L':
+            makeColour(132)
+            break
+          case 'C':
+            makeColour(121)
+            break
+          case 'Q':
+          case 'E':
+          case 'Z':
+          case 'K':
+          case 'M':
+            makeColour(146)
+            break
+          case 'G':
+            makeColour(75)
+            break
+          case 'H':
+            makeColour(155)
+            break
+          case 'F':
+            makeColour(165)
+            break
+          case 'P':
+            makeColour(115)
+            break
+          case 'S':
+            makeColour(105)
+            break
+          case 'T':
+            makeColour(119)
+            break
+          case 'W':
+            makeColour(204)
+            break
+          case 'Y':
+            makeColour(181)
+            break
+          case 'V':
+            makeColour(117)
+            break
+          default:
+            makeColour(150)
+        }
+      }
+}
+
+ColourSchemes.instance.registerColourScheme(new Stripy())
+ColourSchemes.instance.registerColourScheme(new ByWeight())