Check hiddenSequence before placing in tmp
[jalview.git] / src / jalview / gui / FeatureRenderer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import java.awt.*;\r
24 \r
25 import java.util.*;\r
26 \r
27 import java.awt.image.*;\r
28 import javax.swing.*;\r
29 import javax.swing.JOptionPane;\r
30 \r
31 \r
32 /**\r
33  * DOCUMENT ME!\r
34  *\r
35  * @author $author$\r
36  * @version $Revision$\r
37  */\r
38 public class FeatureRenderer\r
39 {\r
40     AlignViewport av;\r
41     Color resBoxColour;\r
42     float transparency = 1.0f;\r
43     FontMetrics fm;\r
44     int charOffset;\r
45 \r
46     Hashtable featureColours = new Hashtable();\r
47 \r
48 \r
49     // A higher level for grouping features of a\r
50    // particular type\r
51     Hashtable featureGroups = null;\r
52 \r
53     // This is actually an Integer held in the hashtable,\r
54     // Retrieved using the key feature type\r
55     Object currentColour;\r
56 \r
57     String [] renderOrder;\r
58 \r
59     /**\r
60      * Creates a new FeatureRenderer object.\r
61      *\r
62      * @param av DOCUMENT ME!\r
63      */\r
64     public FeatureRenderer(AlignViewport av)\r
65     {\r
66         this.av = av;\r
67     }\r
68 \r
69     public void transferSettings(FeatureRenderer fr)\r
70     {\r
71       renderOrder = fr.renderOrder;\r
72       featureGroups = fr.featureGroups;\r
73       featureColours = fr.featureColours;\r
74       transparency =  fr.transparency;\r
75     }\r
76 \r
77     BufferedImage offscreenImage;\r
78     boolean offscreenRender = false;\r
79     public Color findFeatureColour(Color initialCol, SequenceI seq, int res)\r
80     {\r
81       return new Color( findFeatureColour (initialCol.getRGB(),\r
82                         seq, res ));\r
83     }\r
84 \r
85     /**\r
86      * This is used by the Molecule Viewer and Overview to\r
87      * get the accurate colourof the rendered sequence\r
88      */\r
89     public int findFeatureColour(int initialCol, SequenceI seq, int column)\r
90     {\r
91       if(!av.showSequenceFeatures)\r
92         return initialCol;\r
93 \r
94       if(seq!=lastSeq)\r
95       {\r
96         lastSeq = seq;\r
97         sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();\r
98         if(sequenceFeatures==null)\r
99           return initialCol;\r
100 \r
101         sfSize = sequenceFeatures.length;\r
102       }\r
103 \r
104       if(jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))\r
105         return Color.white.getRGB();\r
106 \r
107 \r
108       //Only bother making an offscreen image if transparency is applied\r
109       if(transparency!=1.0f && offscreenImage==null)\r
110       {\r
111         offscreenImage = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB);\r
112       }\r
113 \r
114       currentColour = null;\r
115 \r
116       offscreenRender = true;\r
117 \r
118       if(offscreenImage!=null)\r
119       {\r
120         offscreenImage.setRGB(0,0,initialCol);\r
121         drawSequence(offscreenImage.getGraphics(),\r
122                      lastSeq,\r
123                      column,column,0);\r
124 \r
125         return offscreenImage.getRGB(0,0);\r
126       }\r
127       else\r
128       {\r
129         drawSequence(null,\r
130                     lastSeq,\r
131                     lastSeq.findPosition(column),\r
132                     -1, -1);\r
133 \r
134         if (currentColour == null)\r
135           return initialCol;\r
136         else\r
137           return  ((Integer)currentColour).intValue();\r
138       }\r
139 \r
140 \r
141     }\r
142 \r
143 \r
144     /**\r
145      * DOCUMENT ME!\r
146      *\r
147      * @param g DOCUMENT ME!\r
148      * @param seq DOCUMENT ME!\r
149      * @param sg DOCUMENT ME!\r
150      * @param start DOCUMENT ME!\r
151      * @param end DOCUMENT ME!\r
152      * @param x1 DOCUMENT ME!\r
153      * @param y1 DOCUMENT ME!\r
154      * @param width DOCUMENT ME!\r
155      * @param height DOCUMENT ME!\r
156      */\r
157    // String type;\r
158    // SequenceFeature sf;\r
159     SequenceI lastSeq;\r
160     SequenceFeature [] sequenceFeatures;\r
161     int sfSize, sfindex, spos, epos;\r
162 \r
163     public void drawSequence(Graphics g, SequenceI seq,\r
164                              int start, int end, int y1)\r
165     {\r
166       if ( seq.getDatasetSequence().getSequenceFeatures() == null\r
167           || seq.getDatasetSequence().getSequenceFeatures().length==0)\r
168         return;\r
169 \r
170       if(g!=null)\r
171         fm = g.getFontMetrics();\r
172 \r
173 \r
174       if (av.featuresDisplayed == null\r
175           || renderOrder==null\r
176           || newFeatureAdded)\r
177        {\r
178          findAllFeatures();\r
179          if(av.featuresDisplayed.size()<1)\r
180            return;\r
181 \r
182          sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();\r
183          sfSize = sequenceFeatures.length;\r
184        }\r
185 \r
186        if(lastSeq==null || seq!=lastSeq)\r
187       {\r
188         lastSeq = seq;\r
189         sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();\r
190         sfSize = sequenceFeatures.length;\r
191       }\r
192 \r
193 \r
194       if (transparency != 1 && g!=null)\r
195       {\r
196         Graphics2D g2 = (Graphics2D) g;\r
197         g2.setComposite(\r
198             AlphaComposite.getInstance(\r
199                 AlphaComposite.SRC_OVER, transparency));\r
200       }\r
201 \r
202       if(!offscreenRender)\r
203       {\r
204         spos = lastSeq.findPosition(start);\r
205         epos = lastSeq.findPosition(end);\r
206       }\r
207 \r
208 \r
209       String type;\r
210       for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)\r
211        {\r
212         type =  renderOrder[renderIndex];\r
213 \r
214         if(!av.featuresDisplayed.containsKey(type))\r
215           continue;\r
216 \r
217         // loop through all features in sequence to find\r
218         // current feature to render\r
219         for (sfindex = 0; sfindex < sfSize; sfindex++)\r
220         {\r
221           if(sequenceFeatures.length<=sfindex)\r
222           {\r
223             continue;\r
224           }\r
225           if (!sequenceFeatures[sfindex].type.equals(type))\r
226             continue;\r
227 \r
228           if (featureGroups != null\r
229               && sequenceFeatures[sfindex].featureGroup != null\r
230               &&\r
231               featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)\r
232               &&\r
233               ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).\r
234               booleanValue())\r
235           {\r
236             continue;\r
237           }\r
238 \r
239           if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos\r
240                             || sequenceFeatures[sfindex].getEnd() < spos))\r
241             continue;\r
242 \r
243           if (offscreenRender && offscreenImage==null)\r
244           {\r
245             if (sequenceFeatures[sfindex].begin <= start &&\r
246                 sequenceFeatures[sfindex].end  >= start)\r
247             {\r
248               currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].\r
249                  type);\r
250             }\r
251           }\r
252           else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))\r
253           {\r
254 \r
255             renderFeature(g, seq,\r
256                           seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
257                           seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
258                           new Color( ( (Integer) av.featuresDisplayed.get(\r
259                 sequenceFeatures[sfindex].type)).intValue()),\r
260                           start, end, y1);\r
261             renderFeature(g, seq,\r
262                           seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
263                           seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
264                           new Color( ( (Integer) av.featuresDisplayed.get(\r
265                 sequenceFeatures[sfindex].type)).intValue()),\r
266                           start, end, y1);\r
267 \r
268           }\r
269           else\r
270             renderFeature(g, seq,\r
271                           seq.findIndex(sequenceFeatures[sfindex].begin) - 1,\r
272                           seq.findIndex(sequenceFeatures[sfindex].end) - 1,\r
273                           getColour(sequenceFeatures[sfindex].type),\r
274                           start, end, y1);\r
275 \r
276 \r
277         }\r
278 \r
279       }\r
280 \r
281         if(transparency!=1.0f && g!=null)\r
282         {\r
283           Graphics2D g2 = (Graphics2D) g;\r
284           g2.setComposite(\r
285               AlphaComposite.getInstance(\r
286                   AlphaComposite.SRC_OVER, 1.0f));\r
287         }\r
288     }\r
289 \r
290 \r
291     char s;\r
292     int i;\r
293     void renderFeature(Graphics g, SequenceI seq,\r
294                        int fstart, int fend, Color featureColour, int start, int end,  int y1)\r
295     {\r
296 \r
297       if (((fstart <= end) && (fend >= start)))\r
298       {\r
299         if (fstart < start)\r
300         { // fix for if the feature we have starts before the sequence start,\r
301           fstart = start; // but the feature end is still valid!!\r
302         }\r
303 \r
304         if (fend >= end)\r
305         {\r
306           fend = end;\r
307         }\r
308           int pady = (y1 + av.charHeight) - av.charHeight / 5;\r
309           for (i = fstart; i <= fend; i++)\r
310           {\r
311             s = seq.getSequence().charAt(i);\r
312 \r
313             if (jalview.util.Comparison.isGap(s))\r
314             {\r
315               continue;\r
316             }\r
317 \r
318             g.setColor(featureColour);\r
319 \r
320             g.fillRect( (i - start) * av.charWidth, y1, av.charWidth,av.charHeight);\r
321 \r
322             if(offscreenRender || !av.validCharWidth)\r
323               continue;\r
324 \r
325             g.setColor(Color.white);\r
326             charOffset = (av.charWidth - fm.charWidth(s)) / 2;\r
327             g.drawString(String.valueOf(s),\r
328                          charOffset + (av.charWidth * (i - start)),\r
329                          pady);\r
330 \r
331           }\r
332       }\r
333     }\r
334 \r
335 \r
336     boolean newFeatureAdded = false;\r
337 \r
338     public void featuresAdded()\r
339     {\r
340       findAllFeatures();\r
341     }\r
342 \r
343    boolean findingFeatures = false;\r
344    synchronized void findAllFeatures()\r
345    {\r
346       newFeatureAdded = false;\r
347 \r
348       if(findingFeatures)\r
349       {\r
350         newFeatureAdded = true;\r
351         return;\r
352       }\r
353 \r
354       findingFeatures = true;\r
355       jalview.schemes.UserColourScheme ucs = new\r
356           jalview.schemes.UserColourScheme();\r
357 \r
358       if(av.featuresDisplayed==null)\r
359         av.featuresDisplayed = new Hashtable();\r
360 \r
361       av.featuresDisplayed.clear();\r
362 \r
363       Vector allfeatures = new Vector();\r
364       for (int i = 0; i < av.alignment.getHeight(); i++)\r
365       {\r
366         SequenceFeature [] features\r
367             = av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures();\r
368 \r
369         if (features == null)\r
370           continue;\r
371 \r
372         int index = 0;\r
373         while (index < features.length)\r
374         {\r
375           if (!av.featuresDisplayed.containsKey(features[index].getType()))\r
376           {\r
377             if(!(features[index].begin == 0 && features[index].end ==0))\r
378             {\r
379               // If beginning and end are 0, the feature is for the whole sequence\r
380               // and we don't want to render the feature in the normal way\r
381 \r
382               if (getColour(features[index].getType()) == null)\r
383               {\r
384                  featureColours.put(features[index].getType(),\r
385                                    ucs.createColourFromName(features[index].\r
386                     getType()));\r
387               }\r
388 \r
389               av.featuresDisplayed.put(features[index].getType(),\r
390                                        new Integer(getColour(features[index].\r
391                   getType()).getRGB()));\r
392               allfeatures.addElement(features[index].getType());\r
393             }\r
394           }\r
395           index++;\r
396         }\r
397       }\r
398 \r
399       renderOrder = new String[allfeatures.size()];\r
400       Enumeration en = allfeatures.elements();\r
401       int i = allfeatures.size()-1;\r
402       while(en.hasMoreElements())\r
403       {\r
404         renderOrder[i] = en.nextElement().toString();\r
405         i--;\r
406       }\r
407 \r
408       findingFeatures = false;\r
409     }\r
410 \r
411     public Color getColour(String featureType)\r
412     {\r
413       Color colour = (Color)featureColours.get(featureType);\r
414       return colour;\r
415     }\r
416 \r
417 \r
418     static String lastFeatureAdded;\r
419     static String lastFeatureGroupAdded;\r
420     static String lastDescriptionAdded;\r
421 \r
422     public boolean createNewFeatures(SequenceI[] sequences,\r
423                                   SequenceFeature [] features)\r
424     {\r
425 \r
426       JPanel bigPanel = new JPanel(new BorderLayout());\r
427       JComboBox name = new JComboBox();\r
428       JComboBox source = new JComboBox();\r
429       JTextArea description = new JTextArea(3,25);\r
430 \r
431       JPanel panel = new JPanel(new GridLayout(2, 2));\r
432       panel.add(new JLabel("New Sequence Feature Name: ",JLabel.RIGHT));\r
433       panel.add(name);\r
434       panel.add(new JLabel("Feature Group: ", JLabel.RIGHT));\r
435       panel.add(source);\r
436       name.setEditable(true);\r
437       source.setEditable(true);\r
438 \r
439       bigPanel.add(panel, BorderLayout.NORTH);\r
440       panel = new JPanel();\r
441       panel.add(new JLabel("Description: ", JLabel.RIGHT));\r
442       description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));\r
443       description.setLineWrap(true);\r
444       panel.add(new JScrollPane(description));\r
445       bigPanel.add(panel, BorderLayout.CENTER);\r
446 \r
447 \r
448       Enumeration en;\r
449       if (av.featuresDisplayed != null)\r
450       {\r
451         en = av.featuresDisplayed.keys();\r
452         while (en.hasMoreElements())\r
453         {\r
454           name.addItem(en.nextElement().toString());\r
455         }\r
456       }\r
457 \r
458       if (featureGroups != null)\r
459       {\r
460         en = featureGroups.keys();\r
461         while (en.hasMoreElements())\r
462         {\r
463           source.addItem(en.nextElement().toString());\r
464         }\r
465       }\r
466 \r
467       if (lastFeatureAdded == null)\r
468         if (features[0].type != null)\r
469           lastFeatureAdded = features[0].type;\r
470         else\r
471           lastFeatureAdded = "feature_1";\r
472 \r
473       if (lastFeatureGroupAdded == null)\r
474         if (features[0].featureGroup != null)\r
475           lastFeatureGroupAdded = features[0].featureGroup;\r
476         else\r
477           lastFeatureAdded = "Jalview";\r
478 \r
479 \r
480       name.setSelectedItem(lastFeatureAdded);\r
481       source.setSelectedItem(lastFeatureGroupAdded);\r
482       description.setText(\r
483           lastDescriptionAdded == null ?\r
484           features[0].description : lastDescriptionAdded);\r
485 \r
486 \r
487       int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,\r
488           bigPanel, "Create New Sequence Feature(s)",\r
489           JOptionPane.OK_CANCEL_OPTION);\r
490 \r
491 \r
492       if(reply==JOptionPane.OK_OPTION\r
493          && name.getSelectedItem()!=null\r
494          && source.getSelectedItem()!=null)\r
495       {\r
496         lastFeatureAdded = name.getSelectedItem().toString();\r
497         lastFeatureGroupAdded = source.getSelectedItem().toString();\r
498         lastDescriptionAdded = description.getText().replaceAll("\n"," ");\r
499 \r
500         jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();\r
501 \r
502 \r
503         for(int i=0; i<sequences.length; i++)\r
504         {\r
505           features[i].type = lastFeatureAdded;\r
506           features[i].featureGroup = lastFeatureGroupAdded;\r
507           features[i].description = lastDescriptionAdded;\r
508           sequences[i].addSequenceFeature(features[i]);\r
509           ffile.parseDescriptionHTML(features[i], false);\r
510         }\r
511 \r
512         if (av.featuresDisplayed == null)\r
513           av.featuresDisplayed = new Hashtable();\r
514 \r
515         if(featureGroups==null)\r
516           featureGroups = new Hashtable();\r
517 \r
518         featureGroups.put(lastFeatureGroupAdded, new Boolean(true));\r
519 \r
520         Color col = getColour(lastFeatureAdded);\r
521         if(col==null)\r
522         {\r
523           col = new Color(60, 160, 115);\r
524           setColour(lastFeatureAdded, col);\r
525         }\r
526 \r
527         av.featuresDisplayed.put(lastFeatureGroupAdded, new Integer(col.getRGB()));\r
528         findAllFeatures();\r
529         return true;\r
530       }\r
531       else\r
532         return false;\r
533 \r
534     }\r
535 \r
536     public void setColour(String featureType, Color col)\r
537     {\r
538       featureColours.put(featureType, col);\r
539     }\r
540 \r
541     public void setTransparency(float value)\r
542     {\r
543       transparency = value;\r
544     }\r
545 \r
546     public float getTransparency()\r
547     {\r
548       return transparency;\r
549     }\r
550 \r
551     public void setFeaturePriority(Object [][] data)\r
552     {\r
553       // The feature table will display high priority\r
554       // features at the top, but theses are the ones\r
555       // we need to render last, so invert the data\r
556       if(av.featuresDisplayed!=null)\r
557         av.featuresDisplayed.clear();\r
558       else\r
559         av.featuresDisplayed = new Hashtable();\r
560 \r
561       renderOrder = new String[data.length];\r
562 \r
563       if (data.length > 0)\r
564         for (int i = 0; i < data.length; i++)\r
565         {\r
566           String type = data[i][0].toString();\r
567           setColour(type, (Color) data[i][1]);\r
568           if ( ( (Boolean) data[i][2]).booleanValue())\r
569           {\r
570             av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));\r
571           }\r
572 \r
573           renderOrder[data.length - i - 1] = type;\r
574         }\r
575 \r
576     }\r
577 \r
578 \r
579 \r
580 \r
581 }\r