X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FFeatureRenderer.java;h=0b504be57b500af56ebac59cb8f562b70a763d2a;hb=47a3f3d338d24f2a92d8249eede1ed0a6fd43d87;hp=048df285e1bec1b15582087b7ba2f997f0ac4181;hpb=bc156d3f5e5184da7d103658603bb3b1a831aee1;p=jalview.git diff --git a/src/jalview/appletgui/FeatureRenderer.java b/src/jalview/appletgui/FeatureRenderer.java index 048df28..0b504be 100755 --- a/src/jalview/appletgui/FeatureRenderer.java +++ b/src/jalview/appletgui/FeatureRenderer.java @@ -33,16 +33,20 @@ import java.util.*; public class FeatureRenderer { AlignViewport av; - SequenceGroup currentSequenceGroup = null; - SequenceGroup[] allGroups = null; - Graphics graphics; - float transparency = .4f; - // The following vector holds the features which are - // to be added, in the correct order or rendering - Vector featuresDisplayed; - Color currentColour = null; + // A higher level for grouping features of a + // particular type + Hashtable featureGroups = null; + + // This is actually an Integer held in the hashtable, + // Retrieved using the key feature type + Object currentColour; + + String [] renderOrder; + + FontMetrics fm; + int charOffset; /** * Creates a new FeatureRenderer object. @@ -53,13 +57,72 @@ public class FeatureRenderer { this.av = av; initColours(); - String version = System.getProperty("java.version"); - if (version.indexOf("1.1") == 0) - highlightTransparent = false; + } + + + public Color findFeatureColour(Color initialCol, SequenceI seq, int i) + { + if(!av.showSequenceFeatures) + return initialCol; + + lastSequence = seq; + sequenceFeatures = lastSequence.getSequenceFeatures(); + if(sequenceFeatures==null) + return initialCol; + + sfSize = sequenceFeatures.length; + + if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i))) + return Color.white; + + currentColour = null; + + drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1, -1, -1); + + if(currentColour==null) + return initialCol; + return new Color( ((Integer)currentColour).intValue() ); } - boolean highlightTransparent = true; + /** + * This is used by the Molecule Viewer to get the accurate colour + * of the rendered sequence + */ + boolean overview = false; + + int white = Color.white.getRGB(); + public int findFeatureColour(int initialCol, int seqIndex, int column) + { + if(!av.showSequenceFeatures) + return initialCol; + + if(seqIndex!=lastSequenceIndex) + { + lastSequence = av.alignment.getSequenceAt(seqIndex); + lastSequenceIndex = seqIndex; + sequenceFeatures = lastSequence.getSequenceFeatures(); + if(sequenceFeatures==null) + return initialCol; + + sfSize = sequenceFeatures.length; + } + + + if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column))) + return Color.white.getRGB(); + + currentColour = null; + + drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1); + + if(currentColour==null) + return initialCol; + + return ((Integer)currentColour).intValue(); + } + + /** * DOCUMENT ME! * @@ -73,174 +136,235 @@ public class FeatureRenderer * @param width DOCUMENT ME! * @param height DOCUMENT ME! */ + // String type; + // SequenceFeature sf; + int lastSequenceIndex=-1; + SequenceI lastSequence; + SequenceFeature [] sequenceFeatures; + int sfSize, sfindex, spos, epos; + public void drawSequence(Graphics g, SequenceI seq, - int start, int end, int x1, int y1, int width, int height) + int start, int end, int y1, int width, int height) { + if ( seq.getSequenceFeatures() == null + || seq.getSequenceFeatures().length==0) + return; - if(seq.getSequenceFeatures()==null) - return; - Enumeration e = null, e2; - String type; - if(featuresDisplayed!=null) - e = featuresDisplayed.elements(); - else - e = seq.getSequenceFeatures().elements(); + if (av.featuresDisplayed == null || renderOrder==null) + { + findAllFeatures(); + if(av.featuresDisplayed.size()<1) + return; - if(highlightTransparent) + sequenceFeatures = seq.getSequenceFeatures(); + sfSize = sequenceFeatures.length; + } + if(lastSequence==null || seq!=lastSequence) + { + lastSequence = seq; + sequenceFeatures = seq.getSequenceFeatures(); + sfSize = sequenceFeatures.length; + } + if(!overview) + { + spos = lastSequence.findPosition(start); + epos = lastSequence.findPosition(end); + if(g!=null) + fm = g.getFontMetrics(); + } + String type; + for(int renderIndex=0; renderIndex epos + || sequenceFeatures[sfindex].getEnd() < spos)) + continue; - while (e.hasMoreElements()) - { - SequenceFeature sf=null; - if(featuresDisplayed!=null) - { - e2 = seq.getSequenceFeatures().elements(); - type = e.nextElement().toString(); - while(e2.hasMoreElements()) - { - sf = (SequenceFeature) e2.nextElement(); - if(sf.getType().equals(type)) - break; - else - sf = null; - } - } - else + if (overview) + { + + if (sequenceFeatures[sfindex].begin - 1 <= start && + sequenceFeatures[sfindex].end + 1 >= start) { - sf = (SequenceFeature) e.nextElement(); - type = sf.getType(); + currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex]. + type); } - if(sf==null) - continue; + } + else if (sequenceFeatures[sfindex].type.equals("disulfide bond")) + { + renderFeature(g, seq, + seq.findIndex(sequenceFeatures[sfindex].begin) - 1, + seq.findIndex(sequenceFeatures[sfindex].begin) - 1, + new Color( ( (Integer) av.featuresDisplayed.get( + sequenceFeatures[sfindex].type)).intValue()), + start, end, y1, width, height); + renderFeature(g, seq, + seq.findIndex(sequenceFeatures[sfindex].end) - 1, + seq.findIndex(sequenceFeatures[sfindex].end) - 1, + new Color( ( (Integer) av.featuresDisplayed.get( + sequenceFeatures[sfindex].type)).intValue()), + start, end, y1, width, height); - if (sf.getBegin() > seq.getEnd()) - { - continue; - } + } + else + renderFeature(g, seq, + seq.findIndex(sequenceFeatures[sfindex].begin) - 1, + seq.findIndex(sequenceFeatures[sfindex].end) - 1, + getColour(sequenceFeatures[sfindex].type), + start, end, y1, width, height); - int fstart = seq.findIndex(sf.getBegin()) - 1; - int fend = seq.findIndex(sf.getEnd()) - 1; - if (((fstart <= end) && (fend >= start))) + } + + } + } + + + char s; + int i; + void renderFeature(Graphics g, SequenceI seq, + int fstart, int fend, Color featureColour, int start, int end, int y1, int width, int height) + { + + if (((fstart <= end) && (fend >= start))) + { + if (fstart < start) + { // fix for if the feature we have starts before the sequence start, + fstart = start; // but the feature end is still valid!! + } + + if (fend >= end) + { + fend = end; + } + + for (i = fstart; i <= fend; i++) + { + s = seq.getSequence().charAt(i); + + if (jalview.util.Comparison.isGap(s)) { - if (fstart < start) - { // fix for if the feature we have starts before the sequence start, - fstart = start; // but the feature end is still valid!! - } - - if (fend >= end) - { - fend = end; - } - - if (fstart == fend) - { - currentColour = getColour(type); - g.setColor(currentColour); - g.fillRoundRect((fstart - start) * width, y1, width, - height, 4, 4); - g.setColor(Color.white); - - char s = seq.getSequence().charAt(fstart); - FontMetrics fm = g.getFontMetrics(); - int charOffset = (width - fm.charWidth(s)) / 2; - int pady = height / 5; - g.drawString(String.valueOf(s), - charOffset + x1 + (width * (fstart - start)), - (y1 + height) - pady); - } - else - { - for (int i = fstart; i <= fend; i++) - { - char s = seq.getSequence().charAt(i); - - if (jalview.util.Comparison.isGap(s)) - { - continue; - } - currentColour = getColour(type); - g.setColor(currentColour); - - g.fillRect((i - start) * width, y1, width, height); - - g.setColor(Color.white); - - FontMetrics fm = g.getFontMetrics(); - int charOffset = (width - fm.charWidth(s)) / 2; - int pady = height / 5; - g.drawString(String.valueOf(s), - charOffset + x1 + (width * (i - start)), - (y1 + height) - pady); - } - } + continue; } - } - if (highlightTransparent) - { - // MyGraphics.SetTransparency(g, 1.0f); + g.setColor(featureColour); + + g.fillRect( (i - start) * width, y1, width, height); + + g.setColor(Color.white); + charOffset = (width - fm.charWidth(s)) / 2; + g.drawString(String.valueOf(s), + charOffset + (width * (i - start)), + (y1 + height) - height / 5); //pady = height / 5; + } + } } - - Image offscreen; - Frame nullframe; - public Color findFeatureColour(Color initialCol, SequenceI seq, int i) + void findAllFeatures() { - currentColour = initialCol; - if(offscreen == null) + av.featuresDisplayed = new Hashtable(); + Vector allfeatures = new Vector(); + for (int i = 0; i < av.alignment.getHeight(); i++) { - nullframe = new Frame(); - nullframe.addNotify(); - offscreen = nullframe.createImage(1, 1); - } + SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures(); - drawSequence(offscreen.getGraphics(), seq, i, i, 0, 0, 1, 1); - return currentColour; - } + if (features == null) + continue; + int index = 0; + while (index < features.length) + { + if (!av.featuresDisplayed.containsKey(features[index].getType())) + { + av.featuresDisplayed.put(features[index].getType(), + new Integer( getColour(features[index].getType()).getRGB()) ); + allfeatures.addElement(features[index].getType()); + } + index++; + } + } + renderOrder = new String[allfeatures.size()]; + Enumeration en = allfeatures.elements(); + int i = allfeatures.size()-1; + while(en.hasMoreElements()) + { + renderOrder[i] = en.nextElement().toString(); + i--; + } + } public Color getColour(String featureType) { return (Color)featureColours.get(featureType); } - public void setColour(String featureType, Color col) + public void addNewFeature(String name, Color col) { - featureColours.put(featureType, col); - } - public void setTransparency(float value) - { - transparency = value; + setColour(name, col); + if(av.featuresDisplayed==null) + av.featuresDisplayed = new Hashtable(); + + + av.featuresDisplayed.put(name, "NOGROUP"); } - public float getTransparency() + public void setColour(String featureType, Color col) { - return transparency; + featureColours.put(featureType, col); } public void setFeaturePriority(Object [][] data) { - // The feature table will display high priority - // features at the top, but theses are the ones - // we need to render last, so invert the data - featuresDisplayed = new Vector(); - for(int i=data.length-1; i>-1; i--) - { - String type = data[i][0].toString(); - setColour(type, (Color)data[i][1]); - if( ((Boolean)data[i][2]).booleanValue() ) - featuresDisplayed.addElement(type); - } + // The feature table will display high priority + // features at the top, but theses are the ones + // we need to render last, so invert the data + if(av.featuresDisplayed!=null) + av.featuresDisplayed.clear(); + + renderOrder = new String[data.length]; + + if (data.length > 0) + for (int i = 0; i < data.length; i++) + { + String type = data[i][0].toString(); + setColour(type, (Color) data[i][1]); + if ( ( (Boolean) data[i][2]).booleanValue()) + { + av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB())); + } + + renderOrder[data.length - i - 1] = type; + } } Hashtable featureColours = new Hashtable();