From: amwaterhouse Date: Fri, 24 Feb 2006 11:41:26 +0000 (+0000) Subject: Features now array for speed of rendering X-Git-Tag: Root_VamJalview_2_07b+~100 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=391968dd70ca76090306df9b5852bbef10ab4ede;p=jalview.git Features now array for speed of rendering --- diff --git a/src/jalview/gui/FeatureRenderer.java b/src/jalview/gui/FeatureRenderer.java index 22789a1..405fe98 100755 --- a/src/jalview/gui/FeatureRenderer.java +++ b/src/jalview/gui/FeatureRenderer.java @@ -24,8 +24,6 @@ import java.awt.*; import java.util.*; -import java.awt.image.*; - /** * DOCUMENT ME! @@ -40,17 +38,19 @@ public class FeatureRenderer float transparency = 1.0f; FontMetrics fm; int charOffset; - boolean drawText = true; - - // The following vector holds the features which are - // to be added, in the correct order or rendering - Vector featuresDisplayed = 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; + + /** * Creates a new FeatureRenderer object. * @@ -62,27 +62,67 @@ public class FeatureRenderer initColours(); } + + public Color findFeatureColour(Color initialCol, SequenceI seq, int i) + { + if(!av.showSequenceFeatures) + return initialCol; + + lastSequence = seq; + sequenceFeatures = lastSequence.getDatasetSequence().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() ); + } /** * This is used by the Molecule Viewer to get the accurate colour * of the rendered sequence */ - BufferedImage bi; - public Color findFeatureColour(Color initialCol, SequenceI seq, int i) + + boolean overview = false; + + int white = Color.white.getRGB(); + public int findFeatureColour(int initialCol, int seqIndex, int column) { if(!av.showSequenceFeatures) return initialCol; - if (bi == null) - bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); + if(seqIndex!=lastSequenceIndex) + { + lastSequence = av.alignment.getSequenceAt(seqIndex); + lastSequenceIndex = seqIndex; + sequenceFeatures = lastSequence.getDatasetSequence().getSequenceFeatures(); + if(sequenceFeatures==null) + return initialCol; - bi.setRGB(0,0, initialCol.getRGB()); + sfSize = sequenceFeatures.length; + } - drawText = false; - drawSequence(bi.getGraphics(), seq, i, i, 0, 0, 1, 1); - drawText = true; + 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 new Color(bi.getRGB(0, 0)); + return ((Integer)currentColour).intValue(); } @@ -99,18 +139,41 @@ 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) { - - //System.out.println(start+" "+end+" "+x1+" "+y1); - if (seq.getDatasetSequence().getSequenceFeatures() == null - || seq.getDatasetSequence().getSequenceFeatures().size()==0) + if ( seq.getDatasetSequence().getSequenceFeatures() == null + || seq.getDatasetSequence().getSequenceFeatures().length==0) return; - fm = g.getFontMetrics(); - if (transparency != 1) + if(g!=null) + fm = g.getFontMetrics(); + + + if (av.featuresDisplayed == null || renderOrder==null) + { + findAllFeatures(); + sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures(); + sfSize = sequenceFeatures.length; + } + + if(lastSequence==null || seq!=lastSequence) + { + lastSequence = seq; + sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures(); + sfSize = sequenceFeatures.length; + } + + + if (transparency != 1 && g!=null) { Graphics2D g2 = (Graphics2D) g; g2.setComposite( @@ -118,61 +181,80 @@ public class FeatureRenderer AlphaComposite.SRC_OVER, transparency)); } - String type; - SequenceFeature sf; - if (featuresDisplayed == null) - findAllFeatures(); - - Enumeration e = featuresDisplayed.elements(), e2; - - // Loop through each visible feature - while (e.hasMoreElements()) + if(!overview) { + spos = lastSequence.findPosition(start); + epos = lastSequence.findPosition(end); + } + + String type; + for(int renderIndex=0; renderIndex seq.getEnd()) + if (!overview && (sequenceFeatures[sfindex].getBegin() > epos + || sequenceFeatures[sfindex].getEnd() < spos)) continue; - if (type.equals("disulfide bond")) + if (overview) + { + + if (sequenceFeatures[sfindex].begin - 1 <= start && + sequenceFeatures[sfindex].end + 1 >= start) + { + currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex]. + type); + } + + } + else if (sequenceFeatures[sfindex].type.equals("disulfide bond")) { renderFeature(g, seq, - seq.findIndex(sf.getBegin()) - 1, - seq.findIndex(sf.getBegin()) - 1, - type, start, end, x1, y1, width, height); + 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(sf.getEnd()) - 1, - seq.findIndex(sf.getEnd()) - 1, - type, start, end, x1, y1, width, height); + 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); } else renderFeature(g, seq, - seq.findIndex(sf.getBegin()) - 1, - seq.findIndex(sf.getEnd()) - 1, - type, start, end, x1, y1, width, height); + seq.findIndex(sequenceFeatures[sfindex].begin) - 1, + seq.findIndex(sequenceFeatures[sfindex].end) - 1, + getColour(sequenceFeatures[sfindex].type), + start, end, y1, width, height); + + } + } - if(transparency!=1.0f) + if(transparency!=1.0f && g!=null) { Graphics2D g2 = (Graphics2D) g; g2.setComposite( @@ -182,69 +264,78 @@ public class FeatureRenderer } + char s; + int i; void renderFeature(Graphics g, SequenceI seq, - int fstart, int fend, String type, int start, int end, int x1, int y1, int width, int height) + 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 (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 (int i = fstart; i <= fend; i++) + if (fend >= end) + { + fend = end; + } + + for (i = fstart; i <= fend; i++) { - char s = seq.getSequence().charAt(i); + s = seq.getSequence().charAt(i); if (jalview.util.Comparison.isGap(s)) { continue; } - g.setColor(getColour(type)); + g.setColor(featureColour); g.fillRect( (i - start) * width, y1, width, height); - if(drawText) - { - g.setColor(Color.white); - charOffset = (width - fm.charWidth(s)) / 2; - g.drawString(String.valueOf(s), - charOffset + x1 + (width * (i - start)), - (y1 + height) - height / 5); //pady = height / 5; - } + 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; + } - } + } } void findAllFeatures() { - Vector features = new Vector(); - SequenceFeature sf; - featuresDisplayed = new Vector(); - Enumeration e; + av.featuresDisplayed = new Hashtable(); for (int i = 0; i < av.alignment.getHeight(); i++) { - features = av.alignment.getSequenceAt(i).getDatasetSequence(). + SequenceFeature [] features = av.alignment.getSequenceAt(i).getDatasetSequence(). getSequenceFeatures(); + if (features == null) continue; - e = features.elements(); - while (e.hasMoreElements()) + int index = 0; + while (index < features.length) { - sf = (SequenceFeature) e.nextElement(); - if (!featuresDisplayed.contains(sf.getType())) + if (!av.featuresDisplayed.containsKey(features[index].getType())) { - featuresDisplayed.addElement(sf.getType()); + av.featuresDisplayed.put(features[index].getType(), + new Integer( getColour(features[index].getType()).getRGB()) ); } + index++; } } + + renderOrder = new String[av.featuresDisplayed.size()]; + Enumeration en = av.featuresDisplayed.keys(); + int i = 0; + while(en.hasMoreElements()) + { + renderOrder[i] = en.nextElement().toString(); + i++; + } } public Color getColour(String featureType) @@ -254,10 +345,13 @@ public class FeatureRenderer public void addNewFeature(String name, Color col) { + setColour(name, col); - if(featuresDisplayed==null) - featuresDisplayed = new Vector(); - featuresDisplayed.insertElementAt(name, 0); + if(av.featuresDisplayed==null) + av.featuresDisplayed = new Hashtable(); + + + av.featuresDisplayed.put(name, "NOGROUP"); } public void setColour(String featureType, Color col) @@ -280,14 +374,22 @@ public class FeatureRenderer // 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.clear(); + av.featuresDisplayed.clear(); + + renderOrder = new String[data.length]; + + if(data.length>0) 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); + { + av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB())); + } + renderOrder[i] = type; } + } Hashtable featureColours = new Hashtable(); diff --git a/src/jalview/gui/FeatureSettings.java b/src/jalview/gui/FeatureSettings.java index eb74454..3979ebe 100755 --- a/src/jalview/gui/FeatureSettings.java +++ b/src/jalview/gui/FeatureSettings.java @@ -35,6 +35,7 @@ public class FeatureSettings extends JPanel final FeatureRenderer fr; final AlignmentPanel ap; final AlignViewport av; + Hashtable allFeatures; Object [][] originalData; final JInternalFrame frame; JScrollPane scrollPane = new JScrollPane(); @@ -124,8 +125,12 @@ public class FeatureSettings extends JPanel bigPanel.add(transPanel, BorderLayout.SOUTH); bigPanel.add(scrollPane, BorderLayout.CENTER); if(groupPanel!=null) + { + groupPanel.setLayout( + new GridLayout(groupPanel.getComponentCount()/3, 4)); + groupPanel.validate(); bigPanel.add(groupPanel, BorderLayout.NORTH); - + } add(bigPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); @@ -139,30 +144,28 @@ public class FeatureSettings extends JPanel else fr.featureGroups.clear(); - Vector allFeatures = new Vector(); - Vector features; - Enumeration e; - SequenceFeature sf; + allFeatures = new Hashtable(); + SequenceFeature[] features; for (int i = 0; i < av.alignment.getHeight(); i++) { features = av.alignment.getSequenceAt(i).getDatasetSequence(). getSequenceFeatures(); + if (features == null) continue; - e = features.elements(); - while (e.hasMoreElements()) + int index = 0; + while (index < features.length) { - sf = (SequenceFeature) e.nextElement(); - if(sf.getFeatureGroup()!=null - && !fr.featureGroups.containsKey(sf.getFeatureGroup())) + if(features[index].getFeatureGroup()!=null + && !fr.featureGroups.containsKey(features[index].getFeatureGroup())) { - fr.featureGroups.put(sf.getFeatureGroup(), new Boolean(true)); + fr.featureGroups.put(features[index].getFeatureGroup(), new Boolean(true)); if(groupPanel==null) groupPanel = new JPanel(); - final JCheckBox check = new JCheckBox(sf.getFeatureGroup(), true); + final JCheckBox check = new JCheckBox(features[index].getFeatureGroup(), true); check.setFont(new Font("Serif", Font.BOLD, 12)); check.addItemListener(new ItemListener() { @@ -176,15 +179,19 @@ public class FeatureSettings extends JPanel if (ap.overviewPanel != null) ap.overviewPanel.updateOverviewImage(); } - + resetTable(); } }); groupPanel.add(check); } - if (!allFeatures.contains(sf.getType())) + if (!allFeatures.contains(features[index].getType())) { - allFeatures.addElement(sf.getType()); + if(features[index].featureGroup!=null) + allFeatures.put(features[index].getType(), features[index].featureGroup); + else + allFeatures.put(features[index].getType(), "NOGROUP"); } + index++; } } if(allFeatures.size()<1) @@ -200,87 +207,111 @@ public class FeatureSettings extends JPanel return; } + resetTable(); + } + + void resetTable() + { int fSize = allFeatures.size(); String type; boolean originalExists = false; - if(originalData!=null) + if (originalData != null) originalExists = true; else originalData = new Object[fSize][3]; - Object [][] data = new Object[fSize][3]; - for(int i=0; ilastcol) { color = sr.getResidueBoxColour( - av.alignment.getSequenceAt(lastrow), lastcol); + av.alignment.getSequenceAt(lastrow), lastcol).getRGB(); if(av.showSequenceFeatures) - color = fr.findFeatureColour(color, av.alignment.getSequenceAt(lastrow), - lastcol); + color = fr.findFeatureColour(color, lastrow, lastcol); } - miniMe.setRGB(col,row,color.getRGB()); + miniMe.setRGB(col,row,color); + - if (av.conservation != null) + } + } + + if (av.conservation != null) + { + for (col = 0; col < width; col++) + { + lastcol = (int) (col * sampleCol); { mg.translate(col, sequencesHeight); ap.annotationPanel.drawGraph(mg, av.conservation, @@ -310,11 +319,10 @@ public class OverviewPanel extends JPanel implements Runnable graphHeight, (int) (col * sampleCol), (int) (col * sampleCol) + 1); - mg.translate(-col, -sequencesHeight); + mg.translate( -col, -sequencesHeight); } } } - System.gc(); resizing = false;