JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / jalview / renderer / seqfeatures / FeatureRenderer.js
1 Clazz.declarePackage ("jalview.renderer.seqfeatures");\r
2 Clazz.load (["jalview.viewmodel.seqfeatures.FeatureRendererModel"], "jalview.renderer.seqfeatures.FeatureRenderer", ["jalview.util.Comparison", "java.awt.AlphaComposite", "$.Color", "java.awt.image.BufferedImage", "java.lang.Float"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.fm = null;\r
5 this.charOffset = 0;\r
6 this.offscreenRender = false;\r
7 this.lastSeq = null;\r
8 this.s = '\0';\r
9 this.i = 0;\r
10 this.av_charHeight = 0;\r
11 this.av_charWidth = 0;\r
12 this.av_validCharWidth = false;\r
13 this.av_isShowSeqFeatureHeight = false;\r
14 this.offscreenImage = null;\r
15 this.lastSequenceFeatures = null;\r
16 this.sfSize = 0;\r
17 this.sfindex = 0;\r
18 this.spos = 0;\r
19 this.epos = 0;\r
20 this.transparencyAvailable = true;\r
21 Clazz.instantialize (this, arguments);\r
22 }, jalview.renderer.seqfeatures, "FeatureRenderer", jalview.viewmodel.seqfeatures.FeatureRendererModel);\r
23 Clazz.defineMethod (c$, "updateAvConfig", \r
24 function () {\r
25 this.av_charHeight = this.av.getCharHeight ();\r
26 this.av_charWidth = this.av.getCharWidth ();\r
27 this.av_validCharWidth = this.av.isValidCharWidth ();\r
28 this.av_isShowSeqFeatureHeight = this.av.isShowSequenceFeaturesHeight ();\r
29 });\r
30 Clazz.defineMethod (c$, "renderFeature", \r
31 function (g, seq, fstart, fend, featureColour, start, end, y1) {\r
32 this.updateAvConfig ();\r
33 if (((fstart <= end) && (fend >= start))) {\r
34 if (fstart < start) {\r
35 fstart = start;\r
36 }if (fend >= end) {\r
37 fend = end;\r
38 }var pady = (y1 + this.av_charHeight) - Clazz.doubleToInt (this.av_charHeight / 5);\r
39 for (this.i = fstart; this.i <= fend; this.i++) {\r
40 this.s = seq.getCharAt (this.i);\r
41 if (jalview.util.Comparison.isGap (this.s)) {\r
42 continue;\r
43 }g.setColor (featureColour);\r
44 g.fillRect ((this.i - start) * this.av_charWidth, y1, this.av_charWidth, this.av_charHeight);\r
45 if (this.offscreenRender || !this.av_validCharWidth) {\r
46 continue;\r
47 }g.setColor (java.awt.Color.white);\r
48 this.charOffset = Clazz.doubleToInt ((this.av_charWidth - this.fm.charWidth (this.s)) / 2);\r
49 g.drawString (String.valueOf (this.s), this.charOffset + (this.av_charWidth * (this.i - start)), pady);\r
50 }\r
51 }}, "java.awt.Graphics,jalview.datamodel.SequenceI,~N,~N,java.awt.Color,~N,~N,~N");\r
52 Clazz.defineMethod (c$, "renderScoreFeature", \r
53 function (g, seq, fstart, fend, featureColour, start, end, y1, bs) {\r
54 this.updateAvConfig ();\r
55 if (((fstart <= end) && (fend >= start))) {\r
56 if (fstart < start) {\r
57 fstart = start;\r
58 }if (fend >= end) {\r
59 fend = end;\r
60 }var pady = (y1 + this.av_charHeight) - Clazz.doubleToInt (this.av_charHeight / 5);\r
61 var ystrt = 0;\r
62 var yend = this.av_charHeight;\r
63 if (bs[0] != 0) {\r
64 if (bs[1] < 128) {\r
65 yend = Clazz.doubleToInt (this.av_charHeight * (128 - bs[1]) / 512);\r
66 ystrt = this.av_charHeight - Clazz.doubleToInt (yend / 2);\r
67 } else {\r
68 ystrt = Clazz.doubleToInt (this.av_charHeight / 2);\r
69 yend = Clazz.doubleToInt (this.av_charHeight * (bs[1] - 128) / 512);\r
70 }} else {\r
71 yend = Clazz.doubleToInt (this.av_charHeight * bs[1] / 255);\r
72 ystrt = this.av_charHeight - yend;\r
73 }for (this.i = fstart; this.i <= fend; this.i++) {\r
74 this.s = seq.getCharAt (this.i);\r
75 if (jalview.util.Comparison.isGap (this.s)) {\r
76 continue;\r
77 }g.setColor (featureColour);\r
78 var x = (this.i - start) * this.av_charWidth;\r
79 g.drawRect (x, y1, this.av_charWidth, this.av_charHeight);\r
80 g.fillRect (x, y1 + ystrt, this.av_charWidth, yend);\r
81 if (this.offscreenRender || !this.av_validCharWidth) {\r
82 continue;\r
83 }g.setColor (java.awt.Color.black);\r
84 this.charOffset = Clazz.doubleToInt ((this.av_charWidth - this.fm.charWidth (this.s)) / 2);\r
85 g.drawString (String.valueOf (this.s), this.charOffset + (this.av_charWidth * (this.i - start)), pady);\r
86 }\r
87 }}, "java.awt.Graphics,jalview.datamodel.SequenceI,~N,~N,java.awt.Color,~N,~N,~N,~A");\r
88 Clazz.defineMethod (c$, "findFeatureColour", \r
89 function (initialCol, seq, res) {\r
90 return  new java.awt.Color (this.findFeatureColour (initialCol.getRGB (), seq, res));\r
91 }, "java.awt.Color,jalview.datamodel.SequenceI,~N");\r
92 Clazz.defineMethod (c$, "findFeatureColour", \r
93 function (initialCol, seq, column) {\r
94 if (!this.av.isShowSequenceFeatures ()) {\r
95 return initialCol;\r
96 }var sequenceFeatures = seq.getSequenceFeatures ();\r
97 if (seq !== this.lastSeq) {\r
98 this.lastSeq = seq;\r
99 this.lastSequenceFeatures = sequenceFeatures;\r
100 if (this.lastSequenceFeatures != null) {\r
101 this.sfSize = this.lastSequenceFeatures.length;\r
102 }} else {\r
103 if (this.lastSequenceFeatures !== sequenceFeatures) {\r
104 this.lastSequenceFeatures = sequenceFeatures;\r
105 if (this.lastSequenceFeatures != null) {\r
106 this.sfSize = this.lastSequenceFeatures.length;\r
107 }}}if (this.lastSequenceFeatures == null || this.sfSize == 0) {\r
108 return initialCol;\r
109 }if (jalview.util.Comparison.isGap (this.lastSeq.getCharAt (column))) {\r
110 return java.awt.Color.white.getRGB ();\r
111 }if (this.transparency != 1.0 && this.offscreenImage == null) {\r
112 this.offscreenImage =  new java.awt.image.BufferedImage (1, 1, 2);\r
113 }this.currentColour = null;\r
114 this.offscreenRender = true;\r
115 if (this.offscreenImage != null) {\r
116 this.offscreenImage.setRGB (0, 0, initialCol);\r
117 this.drawSequence (this.offscreenImage.getGraphics (), this.lastSeq, column, column, 0);\r
118 return this.offscreenImage.getRGB (0, 0);\r
119 } else {\r
120 this.drawSequence (null, this.lastSeq, this.lastSeq.findPosition (column), -1, -1);\r
121 if (this.currentColour == null) {\r
122 return initialCol;\r
123 } else {\r
124 return (this.currentColour).intValue ();\r
125 }}}, "~N,jalview.datamodel.SequenceI,~N");\r
126 Clazz.defineMethod (c$, "drawSequence", \r
127 function (g, seq, start, end, y1) {\r
128 var sequenceFeatures = seq.getSequenceFeatures ();\r
129 if (sequenceFeatures == null || sequenceFeatures.length == 0) {\r
130 return;\r
131 }if (g != null) {\r
132 this.fm = g.getFontMetrics ();\r
133 }this.updateFeatures ();\r
134 if (this.lastSeq == null || seq !== this.lastSeq || sequenceFeatures !== this.lastSequenceFeatures) {\r
135 this.lastSeq = seq;\r
136 this.lastSequenceFeatures = sequenceFeatures;\r
137 }if (this.transparency != 1 && g != null) {\r
138 var g2 = g;\r
139 g2.setComposite (java.awt.AlphaComposite.getInstance (3, this.transparency));\r
140 }if (!this.offscreenRender) {\r
141 this.spos = this.lastSeq.findPosition (start);\r
142 this.epos = this.lastSeq.findPosition (end);\r
143 }this.sfSize = this.lastSequenceFeatures.length;\r
144 var type;\r
145 for (var renderIndex = 0; renderIndex < this.renderOrder.length; renderIndex++) {\r
146 type = this.renderOrder[renderIndex];\r
147 if (type == null || !this.showFeatureOfType (type)) {\r
148 continue;\r
149 }for (this.sfindex = 0; this.sfindex < this.sfSize; this.sfindex++) {\r
150 var sequenceFeature = this.lastSequenceFeatures[this.sfindex];\r
151 if (!sequenceFeature.type.equals (type)) {\r
152 continue;\r
153 }if (this.featureGroups != null && sequenceFeature.featureGroup != null && sequenceFeature.featureGroup.length != 0 && this.featureGroups.containsKey (sequenceFeature.featureGroup) && !this.featureGroups.get (sequenceFeature.featureGroup).booleanValue ()) {\r
154 continue;\r
155 }if (!this.offscreenRender && (sequenceFeature.getBegin () > this.epos || sequenceFeature.getEnd () < this.spos)) {\r
156 continue;\r
157 }if (this.offscreenRender && this.offscreenImage == null) {\r
158 if (sequenceFeature.begin <= start && sequenceFeature.end >= start) {\r
159 this.currentColour =  new Integer (this.getColour (sequenceFeature).getRGB ());\r
160 }} else if (sequenceFeature.type.equals ("disulfide bond")) {\r
161 this.renderFeature (g, seq, seq.findIndex (sequenceFeature.begin) - 1, seq.findIndex (sequenceFeature.begin) - 1, this.getColour (sequenceFeature), start, end, y1);\r
162 this.renderFeature (g, seq, seq.findIndex (sequenceFeature.end) - 1, seq.findIndex (sequenceFeature.end) - 1, this.getColour (sequenceFeature), start, end, y1);\r
163 } else if (this.showFeature (sequenceFeature)) {\r
164 if (this.av_isShowSeqFeatureHeight && !Float.isNaN (sequenceFeature.score)) {\r
165 this.renderScoreFeature (g, seq, seq.findIndex (sequenceFeature.begin) - 1, seq.findIndex (sequenceFeature.end) - 1, this.getColour (sequenceFeature), start, end, y1, this.normaliseScore (sequenceFeature));\r
166 } else {\r
167 this.renderFeature (g, seq, seq.findIndex (sequenceFeature.begin) - 1, seq.findIndex (sequenceFeature.end) - 1, this.getColour (sequenceFeature), start, end, y1);\r
168 }}}\r
169 }\r
170 if (this.transparency != 1.0 && g != null && this.transparencyAvailable) {\r
171 var g2 = g;\r
172 g2.setComposite (java.awt.AlphaComposite.getInstance (3, 1.0));\r
173 }}, "java.awt.Graphics,jalview.datamodel.SequenceI,~N,~N,~N");\r
174 Clazz.defineMethod (c$, "setTransparencyAvailable", \r
175 function (isTransparencyAvailable) {\r
176 this.transparencyAvailable = isTransparencyAvailable;\r
177 }, "~B");\r
178 Clazz.overrideMethod (c$, "isTransparencyAvailable", \r
179 function () {\r
180 return this.transparencyAvailable;\r
181 });\r
182 Clazz.overrideMethod (c$, "featuresAdded", \r
183 function () {\r
184 this.lastSeq = null;\r
185 this.findAllFeatures ();\r
186 });\r
187 });\r