Use the isGap method in utils
[jalview.git] / src / jalview / gui / SequenceRenderer.java
1 package jalview.gui;\r
2 \r
3 import jalview.datamodel.*;\r
4 import jalview.schemes.*;\r
5 import java.awt.*;\r
6 import java.util.*;\r
7 \r
8 public class SequenceRenderer implements RendererI\r
9 {\r
10   AlignViewport av;\r
11   FontMetrics fm;\r
12   boolean renderGaps = true;\r
13   SequenceGroup currentSequenceGroup = null;\r
14   Color color;\r
15 \r
16   public SequenceRenderer(AlignViewport av)\r
17   {\r
18     this.av = av;\r
19   }\r
20 \r
21 \r
22   public void renderGaps(boolean b)\r
23   {\r
24     renderGaps = b;\r
25   }\r
26 \r
27   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
28   {\r
29    Color c = Color.white;\r
30 try{\r
31      if (cs != null)\r
32        c = cs.findColour(seq.getSequence(i, i + 1), i, av.getConsensus(false));\r
33    }catch(Exception ex){}\r
34 \r
35     return c;\r
36   }\r
37 \r
38   public void drawSequence(Graphics g,SequenceI seq,SequenceGroup sg, int start, int end, int x1, int y1, int width, int height, Vector pid, int seqnum)\r
39   {\r
40     currentSequenceGroup = sg;\r
41 \r
42     drawBoxes(g, seq, start, end, x1, y1, (int) width, height, pid);\r
43 \r
44     fm = g.getFontMetrics();\r
45     drawText(g,seq,start,end,x1,y1,(int)width,height);\r
46 \r
47   }\r
48 \r
49   public void drawBoxes(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height,Vector freq) {\r
50     int i      = start;\r
51     int length = seq.getLength();\r
52 \r
53     Color currentColor = Color.WHITE;\r
54 \r
55     int curStart = x1;\r
56     int curWidth = width;\r
57 \r
58     while (i <= end && i < length)\r
59     {\r
60       color = color.white;\r
61 \r
62       if(inCurrentSequenceGroup(i))\r
63       {\r
64         if( currentSequenceGroup.getDisplayBoxes())\r
65              color = getResidueBoxColour(currentSequenceGroup.cs, seq, i);\r
66       }\r
67       else if(av.getShowBoxes())\r
68            color = getResidueBoxColour(av.getGlobalColourScheme(), seq, i);\r
69 \r
70 \r
71       if (color != currentColor || color != null)\r
72       {\r
73         g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
74 \r
75         currentColor = color;\r
76         g.setColor(color);\r
77 \r
78         curStart = i;\r
79         curWidth = width;\r
80       }\r
81       else\r
82         curWidth += width;\r
83 \r
84       i++;\r
85     }\r
86     g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
87   }\r
88 \r
89   public void drawText(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
90   {\r
91     int pady = height/5;\r
92     int charOffset=0;\r
93     g.setColor(Color.black);\r
94 \r
95     char s;\r
96     // Need to find the sequence position here.\r
97     for (int i = start; i <= end; i++)\r
98     {\r
99         if(i<seq.getLength())\r
100           s = seq.getSequence().charAt(i);\r
101         else\r
102           s = ' ';\r
103 \r
104         if(!renderGaps && jalview.util.Comparison.isGap(s))\r
105           continue;\r
106 \r
107         g.setColor(Color.black);\r
108 \r
109         if (inCurrentSequenceGroup(i))\r
110         {\r
111           if(!currentSequenceGroup.getDisplayText())\r
112             continue;\r
113 \r
114           if(currentSequenceGroup.getColourText())\r
115             g.setColor(getResidueBoxColour(currentSequenceGroup.cs, seq, i).darker());\r
116         }\r
117         else\r
118         {\r
119           if(!av.getShowText())\r
120             continue;\r
121 \r
122           if(av.getColourText())\r
123             g.setColor(getResidueBoxColour(av.getGlobalColourScheme(), seq, i).darker());\r
124         }\r
125 \r
126       charOffset =  (width - fm.charWidth(s))/2;\r
127       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
128     }\r
129 \r
130 \r
131   }\r
132 \r
133   boolean inCurrentSequenceGroup(int res)\r
134   {\r
135     if(currentSequenceGroup==null)\r
136       return false;\r
137 \r
138     return (currentSequenceGroup.getStartRes()<=res && currentSequenceGroup.getEndRes()>=res)?true:false;\r
139   }\r
140 \r
141   public void drawHighlightedText(Graphics g, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
142   {\r
143     int pady = height/5;\r
144     int charOffset=0;\r
145     g.setColor(Color.BLACK);\r
146     g.fillRect(x1,y1,width*(end-start+1),height);\r
147     g.setColor(Color.white);\r
148 \r
149     char s='~';\r
150     // Need to find the sequence position here.\r
151     for (int i = start; i <= end; i++)\r
152     {\r
153        if(i<seq.getLength())\r
154           s = seq.getSequence().charAt(i);\r
155 \r
156       charOffset =  (width - fm.charWidth(s))/2;\r
157       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
158     }\r
159   }\r
160 \r
161 \r
162 }\r