From add2f44022857817d1bb9a502f0e37627a34c5d8 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Fri, 24 Mar 2006 15:34:21 +0000 Subject: [PATCH] Groups compatibility --- src/jalview/gui/FeatureRenderer.java | 173 +++++++++++++++----------- src/jalview/gui/FeatureSettings.java | 222 ++++++++++++++++++++-------------- 2 files changed, 237 insertions(+), 158 deletions(-) diff --git a/src/jalview/gui/FeatureRenderer.java b/src/jalview/gui/FeatureRenderer.java index 405fe98..6fbdee8 100755 --- a/src/jalview/gui/FeatureRenderer.java +++ b/src/jalview/gui/FeatureRenderer.java @@ -24,6 +24,8 @@ import java.awt.*; import java.util.*; +import java.awt.image.*; + /** * DOCUMENT ME! @@ -62,39 +64,28 @@ public class FeatureRenderer initColours(); } - - public Color findFeatureColour(Color initialCol, SequenceI seq, int i) + public void transferSettings(FeatureRenderer fr) { - 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); + renderOrder = fr.renderOrder; + featureGroups = fr.featureGroups; + featureColours = fr.featureColours; + transparency = fr.transparency; + } - if(currentColour==null) - return initialCol; + BufferedImage offscreenImage; + boolean offscreenRender = false; + public Color findFeatureColour(Color initialCol, SequenceI seq, int res) + { + int seqindex = av.alignment.findIndex(seq); - return new Color( ((Integer)currentColour).intValue() ); + return new Color( findFeatureColour (initialCol.getRGB(), + seqindex, res )); } + /** * 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) @@ -111,18 +102,43 @@ public class FeatureRenderer sfSize = sequenceFeatures.length; } - if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column))) return Color.white.getRGB(); + + //Only bother making an offscreen image if transparency is applied + if(transparency!=1.0f && offscreenImage==null) + { + offscreenImage = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB); + } + currentColour = null; - drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1); + offscreenRender = true; + + if(offscreenImage!=null) + { + offscreenImage.setRGB(0,0,initialCol); + drawSequence(offscreenImage.getGraphics(), + lastSequence, + column,column,0); + + return offscreenImage.getRGB(0,0); + } + else + { + drawSequence(null, + lastSequence, + lastSequence.findPosition(column), + -1, -1); + + if (currentColour == null) + return initialCol; + else + return ((Integer)currentColour).intValue(); + } - if(currentColour==null) - return initialCol; - return ((Integer)currentColour).intValue(); } @@ -147,9 +163,9 @@ public class FeatureRenderer int sfSize, sfindex, spos, epos; public void drawSequence(Graphics g, SequenceI seq, - int start, int end, int y1, int width, int height) + int start, int end, int y1) { - if ( seq.getDatasetSequence().getSequenceFeatures() == null + if ( seq.getDatasetSequence().getSequenceFeatures() == null || seq.getDatasetSequence().getSequenceFeatures().length==0) return; @@ -161,6 +177,9 @@ public class FeatureRenderer if (av.featuresDisplayed == null || renderOrder==null) { findAllFeatures(); + if(av.featuresDisplayed.size()<1) + return; + sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures(); sfSize = sequenceFeatures.length; } @@ -181,21 +200,28 @@ public class FeatureRenderer AlphaComposite.SRC_OVER, transparency)); } - if(!overview) + if(!offscreenRender) { spos = lastSequence.findPosition(start); epos = lastSequence.findPosition(end); } + String type; for(int renderIndex=0; renderIndex epos + if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos || sequenceFeatures[sfindex].getEnd() < spos)) continue; - if (overview) + if (offscreenRender && offscreenImage==null) { - - if (sequenceFeatures[sfindex].begin - 1 <= start && - sequenceFeatures[sfindex].end + 1 >= start) + if (sequenceFeatures[sfindex].begin <= start && + sequenceFeatures[sfindex].end >= start) { currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex]. - type); + type); } - } else if (sequenceFeatures[sfindex].type.equals("disulfide bond")) { @@ -233,13 +257,13 @@ public class FeatureRenderer seq.findIndex(sequenceFeatures[sfindex].begin) - 1, new Color( ( (Integer) av.featuresDisplayed.get( sequenceFeatures[sfindex].type)).intValue()), - start, end, y1, width, height); + start, end, y1); 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); + start, end, y1); } else @@ -247,7 +271,7 @@ public class FeatureRenderer seq.findIndex(sequenceFeatures[sfindex].begin) - 1, seq.findIndex(sequenceFeatures[sfindex].end) - 1, getColour(sequenceFeatures[sfindex].type), - start, end, y1, width, height); + start, end, y1); } @@ -267,7 +291,7 @@ public class FeatureRenderer 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) + int fstart, int fend, Color featureColour, int start, int end, int y1) { if (((fstart <= end) && (fend >= start))) @@ -281,7 +305,7 @@ public class FeatureRenderer { fend = end; } - + int pady = (y1 + av.charHeight) - av.charHeight / 5; for (i = fstart; i <= fend; i++) { s = seq.getSequence().charAt(i); @@ -293,13 +317,16 @@ public class FeatureRenderer g.setColor(featureColour); - g.fillRect( (i - start) * width, y1, width, height); + g.fillRect( (i - start) * av.charWidth, y1, av.charWidth,av.charHeight); + + if(offscreenRender) + continue; g.setColor(Color.white); - charOffset = (width - fm.charWidth(s)) / 2; + charOffset = (av.charWidth - fm.charWidth(s)) / 2; g.drawString(String.valueOf(s), - charOffset + (width * (i - start)), - (y1 + height) - height / 5); //pady = height / 5; + charOffset + (av.charWidth * (i - start)), + pady); } } @@ -308,10 +335,11 @@ public class FeatureRenderer void findAllFeatures() { av.featuresDisplayed = new Hashtable(); + Vector allfeatures = new Vector(); for (int i = 0; i < av.alignment.getHeight(); i++) { - SequenceFeature [] features = av.alignment.getSequenceAt(i).getDatasetSequence(). - getSequenceFeatures(); + SequenceFeature [] features + = av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures(); if (features == null) continue; @@ -323,18 +351,19 @@ public class FeatureRenderer { av.featuresDisplayed.put(features[index].getType(), new Integer( getColour(features[index].getType()).getRGB()) ); + allfeatures.addElement(features[index].getType()); } index++; } } - renderOrder = new String[av.featuresDisplayed.size()]; - Enumeration en = av.featuresDisplayed.keys(); - int i = 0; + renderOrder = new String[allfeatures.size()]; + Enumeration en = allfeatures.elements(); + int i = allfeatures.size()-1; while(en.hasMoreElements()) { renderOrder[i] = en.nextElement().toString(); - i++; + i--; } } @@ -343,15 +372,17 @@ public class FeatureRenderer return (Color)featureColours.get(featureType); } - public void addNewFeature(String name, Color col) + public void addNewFeature(String name, Color col, String group) { setColour(name, col); if(av.featuresDisplayed==null) av.featuresDisplayed = new Hashtable(); + if(group == null) + group = "NOGROUP"; - av.featuresDisplayed.put(name, "NOGROUP"); + av.featuresDisplayed.put(name, new Integer(col.getRGB())); } public void setColour(String featureType, Color col) @@ -374,21 +405,25 @@ 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 - av.featuresDisplayed.clear(); + if(av.featuresDisplayed!=null) + av.featuresDisplayed.clear(); + else + av.featuresDisplayed = new Hashtable(); 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() ) - { - av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB())); - } - renderOrder[i] = type; - } + 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; + } } diff --git a/src/jalview/gui/FeatureSettings.java b/src/jalview/gui/FeatureSettings.java index 7c95ed7..f14ec51 100755 --- a/src/jalview/gui/FeatureSettings.java +++ b/src/jalview/gui/FeatureSettings.java @@ -35,13 +35,14 @@ 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(); JTable table; JPanel groupPanel; + boolean alignmentHasFeatures = false; + public FeatureSettings(AlignViewport av, final AlignmentPanel ap) { this.ap = ap; @@ -127,7 +128,8 @@ public class FeatureSettings extends JPanel if(groupPanel!=null) { groupPanel.setLayout( - new GridLayout(groupPanel.getComponentCount()/3, 4)); + new GridLayout(fr.featureGroups.size() / 4 + 1, 4)); + groupPanel.validate(); bigPanel.add(groupPanel, BorderLayout.NORTH); } @@ -139,62 +141,78 @@ public class FeatureSettings extends JPanel void setTableData() { + alignmentHasFeatures = false; + if (fr.featureGroups == null) fr.featureGroups = new Hashtable(); - else - fr.featureGroups.clear(); - allFeatures = new Hashtable(); - SequenceFeature[] features; + Vector allFeatures = new Vector(); + Vector allGroups = new Vector(); + SequenceFeature[] tmpfeatures; + String group; - for (int i = 0; i < av.alignment.getHeight(); i++) - { - features = av.alignment.getSequenceAt(i).getDatasetSequence(). - getSequenceFeatures(); + for (int i = 0; i < av.alignment.getHeight(); i++) + { + if (av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures() == null) + continue; - if (features == null) - continue; + alignmentHasFeatures = true; - int index = 0; - while (index < features.length) - { - if(features[index].getFeatureGroup()!=null - && !fr.featureGroups.containsKey(features[index].getFeatureGroup())) + tmpfeatures = av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures(); + int index = 0; + while (index < tmpfeatures.length) + { + if(tmpfeatures[index].getFeatureGroup()!=null) + { + group = tmpfeatures[index].featureGroup; + if(!allGroups.contains(group)) + { + allGroups.addElement(group); + + boolean visible = true; + if (fr.featureGroups.containsKey(group)) + { + visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue(); + } + + fr.featureGroups.put(group, new Boolean(visible)); + + if (groupPanel == null) + { + groupPanel = new JPanel(); + } + + final JCheckBox check = new JCheckBox(group, visible); + check.setFont(new Font("Serif", Font.BOLD, 12)); + check.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent evt) + { + fr.featureGroups.put(check.getText(), + new Boolean(check.isSelected())); + ap.seqPanel.seqCanvas.repaint(); + if (ap.overviewPanel != null) + ap.overviewPanel.updateOverviewImage(); + + resetTable(true); + } + }); + groupPanel.add(check); + + } + + } + + if (!allFeatures.contains(tmpfeatures[index].getType())) { - fr.featureGroups.put(features[index].getFeatureGroup(), new Boolean(true)); - if(groupPanel==null) - groupPanel = new JPanel(); - - final JCheckBox check = new JCheckBox(features[index].getFeatureGroup(), true); - check.setFont(new Font("Serif", Font.BOLD, 12)); - check.addItemListener(new ItemListener() - { - public void itemStateChanged(ItemEvent evt) - { - if (fr.featureGroups.containsKey(check.getText())) - { - fr.featureGroups.put(check.getText(), - new Boolean(check.isSelected())); - ap.seqPanel.seqCanvas.repaint(); - if (ap.overviewPanel != null) - ap.overviewPanel.updateOverviewImage(); - } - resetTable(); - } - }); - groupPanel.add(check); + allFeatures.addElement(tmpfeatures[index].getType()); } - if (!allFeatures.contains(features[index].getType())) - { - if(features[index].featureGroup!=null) - allFeatures.put(features[index].getType(), features[index].featureGroup); - else - allFeatures.put(features[index].getType(), "NOGROUP"); - } - index++; + index ++; } } - if(allFeatures.size()<1) + + + if(!alignmentHasFeatures) { try { frame.setClosed(true); } @@ -207,60 +225,81 @@ public class FeatureSettings extends JPanel return; } - resetTable(); + resetTable(false); } - void resetTable() + void resetTable(boolean groupsChanged) { - int fSize = allFeatures.size(); + SequenceFeature [] tmpfeatures; + String group=null, type; + Vector visibleChecks = new Vector(); - String type; - boolean originalExists = false; - if (originalData != null) - originalExists = true; - else - originalData = new Object[fSize][3]; + //Find out which features should be visible depending on which groups + //are selected / deselected + for (int i = 0; i < av.alignment.getHeight(); i++) + { + if (av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures() == null) + continue; - Object[][] tmp = new Object[fSize][3]; - int tmpIndex = 0; + tmpfeatures = av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures(); + int index = 0; + while (index < tmpfeatures.length) + { + group = tmpfeatures[index].featureGroup; - Enumeration en = allFeatures.keys(); - int i=0; - while(en.hasMoreElements()) - { - type = en.nextElement().toString(); - Color col = fr.getColour(type); + if (group==null || fr.featureGroups.get(group)==null || + ((Boolean) fr.featureGroups.get(group)).booleanValue()) + { + type = tmpfeatures[index].getType(); + if(!visibleChecks.contains(type) ) + { + visibleChecks.addElement(type); + } + } + index++; + } + } + + int fSize = visibleChecks.size(); + Object [][] data = new Object[fSize][3]; + int dataIndex = 0; - //This will ignore any features which are in groups not shown - Object visibleGroup = fr.featureGroups.get(allFeatures.get(type)); - if(visibleGroup == null - || ((Boolean)(visibleGroup)).booleanValue()) + if(fr.renderOrder!=null) + { + //First add the checks in the previous render order, + //in case the window has been closed and reopened + for(int ro=fr.renderOrder.length-1; ro>-1; ro--) { - tmp[tmpIndex][0] = type; - tmp[tmpIndex][1] = col; - if (av.featuresDisplayed != null) - tmp[tmpIndex][2] = new Boolean(av.featuresDisplayed.containsKey(type)); - else - tmp[tmpIndex][2] = new Boolean(true); - - tmpIndex++; - } + type = fr.renderOrder[ro]; + if(!visibleChecks.contains(type)) + continue; - if (!originalExists) - { - originalData[i][0] = type; - originalData[i][1] = col; - if (av.featuresDisplayed != null) - originalData[i][2] = new Boolean(av.featuresDisplayed.contains(type)); - else - originalData[i][2] = new Boolean(true); + data[dataIndex][0] = type; + data[dataIndex][1] = fr.getColour(type); + data[dataIndex][2] = new Boolean(av.featuresDisplayed.containsKey(type)); + dataIndex++; + visibleChecks.removeElement(type); } - i++; } - Object [][]data = new Object[tmpIndex][3]; - System.arraycopy(tmp, 0, data,0, tmpIndex); + fSize = visibleChecks.size(); + for(int i=0; i