Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / schemes / UserColourScheme.js
1 Clazz.declarePackage ("jalview.schemes");
2 Clazz.load (["jalview.schemes.ResidueColourScheme"], "jalview.schemes.UserColourScheme", ["jalview.schemes.ColourSchemeProperty", "$.ResidueProperties", "java.awt.Color", "java.util.StringTokenizer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.lowerCaseColours = null;
5 this.schemeName = null;
6 Clazz.instantialize (this, arguments);
7 }, jalview.schemes, "UserColourScheme", jalview.schemes.ResidueColourScheme);
8 Clazz.makeConstructor (c$, 
9 function () {
10 Clazz.superConstructor (this, jalview.schemes.UserColourScheme, [jalview.schemes.ResidueProperties.aaIndex]);
11 });
12 Clazz.makeConstructor (c$, 
13 function (newColors) {
14 Clazz.superConstructor (this, jalview.schemes.UserColourScheme, [jalview.schemes.ResidueProperties.aaIndex]);
15 this.colors = newColors;
16 }, "~A");
17 Clazz.overrideMethod (c$, "applyTo", 
18 function (sg, hiddenRepSequences) {
19 var usc =  new jalview.schemes.UserColourScheme (this.colors);
20 if (this.lowerCaseColours != null) {
21 usc.schemeName =  String.instantialize (this.schemeName);
22 usc.lowerCaseColours =  new Array (this.lowerCaseColours.length);
23 System.arraycopy (this.lowerCaseColours, 0, usc.lowerCaseColours, 0, this.lowerCaseColours.length);
24 }return usc;
25 }, "jalview.datamodel.AnnotatedCollectionI,java.util.Map");
26 Clazz.makeConstructor (c$, 
27 function (colour) {
28 Clazz.superConstructor (this, jalview.schemes.UserColourScheme, [jalview.schemes.ResidueProperties.aaIndex]);
29 var col = this.getColourFromString (colour);
30 if (col == null) {
31 System.out.println ("Unknown colour!! " + colour);
32 col = this.createColourFromName (colour);
33 }this.colors =  new Array (24);
34 for (var i = 0; i < 24; i++) {
35 this.colors[i] = col;
36 }
37 this.schemeName = colour;
38 }, "~S");
39 Clazz.defineMethod (c$, "getColours", 
40 function () {
41 return this.colors;
42 });
43 Clazz.defineMethod (c$, "getLowerCaseColours", 
44 function () {
45 return this.lowerCaseColours;
46 });
47 Clazz.defineMethod (c$, "setName", 
48 function (name) {
49 this.schemeName = name;
50 }, "~S");
51 Clazz.defineMethod (c$, "getName", 
52 function () {
53 return this.schemeName;
54 });
55 Clazz.defineMethod (c$, "getColourFromString", 
56 function (colour) {
57 colour = colour.trim ();
58 var col = null;
59 try {
60 var value = Integer.parseInt (colour, 16);
61 col =  new java.awt.Color (value);
62 } catch (ex) {
63 if (Clazz.exceptionOf (ex, NumberFormatException)) {
64 } else {
65 throw ex;
66 }
67 }
68 if (col == null) {
69 col = jalview.schemes.ColourSchemeProperty.getAWTColorFromName (colour);
70 }if (col == null) {
71 try {
72 var st =  new java.util.StringTokenizer (colour, ",");
73 var r = Integer.parseInt (st.nextToken ());
74 var g = Integer.parseInt (st.nextToken ());
75 var b = Integer.parseInt (st.nextToken ());
76 col =  new java.awt.Color (r, g, b);
77 } catch (ex) {
78 if (Clazz.exceptionOf (ex, Exception)) {
79 } else {
80 throw ex;
81 }
82 }
83 }return col;
84 }, "~S");
85 Clazz.defineMethod (c$, "createColourFromName", 
86 function (name) {
87 var r;
88 var g;
89 var b;
90 var lsize = name.length;
91 var start = 0;
92 var end = Clazz.doubleToInt (lsize / 3);
93 var rgbOffset = Math.abs (name.hashCode () % 10) * 15;
94 r = Math.abs (name.substring (start, end).hashCode () + rgbOffset) % 210 + 20;
95 start = end;
96 end += Clazz.doubleToInt (lsize / 3);
97 if (end > lsize) {
98 end = lsize;
99 }g = Math.abs (name.substring (start, end).hashCode () + rgbOffset) % 210 + 20;
100 b = Math.abs (name.substring (end).hashCode () + rgbOffset) % 210 + 20;
101 var color =  new java.awt.Color (r, g, b);
102 return color;
103 }, "~S");
104 Clazz.defineMethod (c$, "parseAppletParameter", 
105 function (paramValue) {
106 var st =  new java.util.StringTokenizer (paramValue, ";");
107 var st2;
108 var token = null;
109 var colour;
110 var residues;
111 try {
112 while (st.hasMoreElements ()) {
113 token = st.nextToken ().trim ();
114 residues = token.substring (0, token.indexOf ("="));
115 colour = token.substring (token.indexOf ("=") + 1);
116 st2 =  new java.util.StringTokenizer (residues, " ,");
117 while (st2.hasMoreTokens ()) {
118 token = st2.nextToken ();
119 if (jalview.schemes.ResidueProperties.aaIndex[token.charCodeAt (0)] == -1) {
120 continue;
121 }var colIndex = jalview.schemes.ResidueProperties.aaIndex[token.charCodeAt (0)];
122 if (token.equalsIgnoreCase ("lowerCase")) {
123 if (this.lowerCaseColours == null) {
124 this.lowerCaseColours =  new Array (23);
125 }for (var i = 0; i < 23; i++) {
126 if (this.lowerCaseColours[i] == null) {
127 this.lowerCaseColours[i] = this.getColourFromString (colour);
128 }}
129 continue;
130 }if (token.equals (token.toLowerCase ())) {
131 if (this.lowerCaseColours == null) {
132 this.lowerCaseColours =  new Array (23);
133 }this.lowerCaseColours[colIndex] = this.getColourFromString (colour);
134 } else {
135 this.colors[colIndex] = this.getColourFromString (colour);
136 }}
137 }
138 } catch (ex) {
139 if (Clazz.exceptionOf (ex, Exception)) {
140 System.out.println ("Error parsing userDefinedColours:\n" + token + "\n" + ex);
141 } else {
142 throw ex;
143 }
144 }
145 }, "~S");
146 Clazz.defineMethod (c$, "findColour", 
147 function (c, j, seq) {
148 var currentColour;
149 var index = jalview.schemes.ResidueProperties.aaIndex[c.charCodeAt (0)];
150 if ((this.threshold == 0) || this.aboveThreshold (c, j)) {
151 if (this.lowerCaseColours != null && 'a' <= c && c <= 'z') {
152 currentColour = this.lowerCaseColours[index];
153 } else {
154 currentColour = this.colors[index];
155 }} else {
156 currentColour = java.awt.Color.white;
157 }if (this.conservationColouring) {
158 currentColour = this.applyConservation (currentColour, j);
159 }return currentColour;
160 }, "~S,~N,jalview.datamodel.SequenceI");
161 Clazz.defineMethod (c$, "setLowerCaseColours", 
162 function (lcolours) {
163 this.lowerCaseColours = lcolours;
164 }, "~A");
165 });