X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Frenderer%2Fseqfeatures%2FFeatureRenderer.java;h=e66b7d5189753053692555297a9c9bb1d11b2451;hb=5c041c1e56a818e673f4a141520a4becf81ae801;hp=02cfd05535f4366298b3af56bf412f2aa926dff2;hpb=5a5663e814e0af0dbe6061a257fb4fb1baebd4e3;p=jalview.git diff --git a/src/jalview/renderer/seqfeatures/FeatureRenderer.java b/src/jalview/renderer/seqfeatures/FeatureRenderer.java index 02cfd05..e66b7d5 100644 --- a/src/jalview/renderer/seqfeatures/FeatureRenderer.java +++ b/src/jalview/renderer/seqfeatures/FeatureRenderer.java @@ -20,29 +20,30 @@ */ package jalview.renderer.seqfeatures; -import jalview.api.AlignViewportI; -import jalview.datamodel.SequenceFeature; -import jalview.datamodel.SequenceI; -import jalview.util.Comparison; -import jalview.viewmodel.seqfeatures.FeatureRendererModel; - import java.awt.AlphaComposite; import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; +import java.util.List; + +import jalview.api.AlignViewportI; +import jalview.api.FeatureColourI; +import jalview.datamodel.ContiguousI; +import jalview.datamodel.MappedFeatures; +import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceI; +import jalview.gui.AlignFrame; +import jalview.gui.Desktop; +import jalview.util.Comparison; +import jalview.util.ReverseListIterator; +import jalview.viewmodel.seqfeatures.FeatureRendererModel; public class FeatureRenderer extends FeatureRendererModel { private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite .getInstance(AlphaComposite.SRC_OVER, 1.0f); - protected SequenceI lastSeq; - - private volatile SequenceFeature[] lastSequenceFeatures; - - int sfSize; - /** * Constructor given a viewport * @@ -104,18 +105,21 @@ public class FeatureRenderer extends FeatureRendererModel g.setColor(featureColour); - g.fillRect((i - start) * charWidth, y1, charWidth, - charHeight); + g.fillRect((i - start) * charWidth, y1, charWidth, charHeight); if (colourOnly || !validCharWidth) { continue; } + /* + * JAL-3045 text is always drawn over features, even if + * 'Show Text' is unchecked in the format menu + */ g.setColor(Color.white); int charOffset = (charWidth - fm.charWidth(s)) / 2; - g.drawString(String.valueOf(s), charOffset - + (charWidth * (i - start)), pady); + g.drawString(String.valueOf(s), + charOffset + (charWidth * (i - start)), pady); } return true; } @@ -203,8 +207,8 @@ public class FeatureRenderer extends FeatureRendererModel g.setColor(Color.black); int charOffset = (charWidth - fm.charWidth(s)) / 2; - g.drawString(String.valueOf(s), charOffset - + (charWidth * (i - start)), pady); + g.drawString(String.valueOf(s), + charOffset + (charWidth * (i - start)), pady); } return true; } @@ -220,45 +224,23 @@ public class FeatureRenderer extends FeatureRendererModel return null; } - SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures(); - if (seq != lastSeq) - { - lastSeq = seq; - lastSequenceFeatures = sequenceFeatures; - if (lastSequenceFeatures != null) - { - sfSize = lastSequenceFeatures.length; - } - } - else - { - if (lastSequenceFeatures != sequenceFeatures) - { - lastSequenceFeatures = sequenceFeatures; - if (lastSequenceFeatures != null) - { - sfSize = lastSequenceFeatures.length; - } - } - } - - if (lastSequenceFeatures == null || sfSize == 0) + // column is 'base 1' but getCharAt is an array index (ie from 0) + if (Comparison.isGap(seq.getCharAt(column - 1))) { + /* + * returning null allows the colour scheme to provide gap colour + * - normally white, but can be customised + */ return null; } - if (Comparison.isGap(lastSeq.getCharAt(column))) - { - return Color.white; - } - Color renderedColour = null; if (transparency == 1.0f) { /* * simple case - just find the topmost rendered visible feature colour */ - renderedColour = findFeatureColour(seq, seq.findPosition(column)); + renderedColour = findFeatureColour(seq, column); } else { @@ -266,7 +248,7 @@ public class FeatureRenderer extends FeatureRendererModel * transparency case - draw all visible features in render order to * build up a composite colour on the graphics context */ - renderedColour = drawSequence(g, lastSeq, column, column, 0, true); + renderedColour = drawSequence(g, seq, column, column, 0, true); } return renderedColour; } @@ -295,21 +277,18 @@ public class FeatureRenderer extends FeatureRendererModel final SequenceI seq, int start, int end, int y1, boolean colourOnly) { - SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures(); - if (sequenceFeatures == null || sequenceFeatures.length == 0) + /* + * if columns are all gapped, or sequence has no features, nothing to do + */ + ContiguousI visiblePositions = seq.findPositions(start + 1, end + 1); + if (visiblePositions == null || !seq.getFeatures().hasFeatures() + && !av.isShowComplementFeatures()) { return null; } updateFeatures(); - if (lastSeq == null || seq != lastSeq - || sequenceFeatures != lastSequenceFeatures) - { - lastSeq = seq; - lastSequenceFeatures = sequenceFeatures; - } - if (transparency != 1f && g != null) { Graphics2D g2 = (Graphics2D) g; @@ -317,13 +296,19 @@ public class FeatureRenderer extends FeatureRendererModel transparency)); } - int startPos = lastSeq.findPosition(start); - int endPos = lastSeq.findPosition(end); - - sfSize = lastSequenceFeatures.length; Color drawnColour = null; /* + * draw 'complement' features below ours if configured to do so + */ + if (av.isShowComplementFeatures() + && !av.isShowComplementFeaturesOnTop()) + { + drawnColour = drawComplementFeatures(g, seq, start, end, y1, + colourOnly, visiblePositions, drawnColour); + } + + /* * iterate over features in ordering of their rendering (last is on top) */ for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++) @@ -334,55 +319,71 @@ public class FeatureRenderer extends FeatureRendererModel continue; } - // loop through all features in sequence to find - // current feature to render - for (int sfindex = 0; sfindex < sfSize; sfindex++) + FeatureColourI fc = getFeatureStyle(type); + List overlaps = seq.getFeatures().findFeatures( + visiblePositions.getBegin(), visiblePositions.getEnd(), type); + + if (overlaps.size() > 1 && fc.isSimpleColour()) { - final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex]; - if (!sequenceFeature.type.equals(type)) + filterFeaturesForDisplay(overlaps); + } + + for (SequenceFeature sf : overlaps) + { + Color featureColour = getColor(sf, fc); + if (featureColour == null) { + /* + * feature excluded by filters, or colour threshold + */ continue; } /* - * a feature type may be flagged as shown but the group - * an instance of it belongs to may be hidden + * if feature starts/ends outside the visible range, + * restrict to visible positions (or if a contact feature, + * to a single position) */ - if (featureGroupNotShown(sequenceFeature)) + int visibleStart = sf.getBegin(); + if (visibleStart < visiblePositions.getBegin()) { - continue; + visibleStart = sf.isContactFeature() ? sf.getEnd() + : visiblePositions.getBegin(); } - - /* - * check feature overlaps the target range - * TODO: efficient retrieval of features overlapping a range - */ - if (sequenceFeature.getBegin() > endPos - || sequenceFeature.getEnd() < startPos) + int visibleEnd = sf.getEnd(); + if (visibleEnd > visiblePositions.getEnd()) { - continue; + visibleEnd = sf.isContactFeature() ? sf.getBegin() + : visiblePositions.getEnd(); } - Color featureColour = getColour(sequenceFeature); - boolean isContactFeature = sequenceFeature.isContactFeature(); + int featureStartCol = seq.findIndex(visibleStart); + int featureEndCol = sf.begin == sf.end ? featureStartCol + : seq.findIndex(visibleEnd); + + // Color featureColour = getColour(sequenceFeature); + + boolean isContactFeature = sf.isContactFeature(); if (isContactFeature) { - boolean drawn = renderFeature(g, seq, - seq.findIndex(sequenceFeature.begin) - 1, - seq.findIndex(sequenceFeature.begin) - 1, featureColour, - start, end, y1, colourOnly); - drawn |= renderFeature(g, seq, - seq.findIndex(sequenceFeature.end) - 1, - seq.findIndex(sequenceFeature.end) - 1, featureColour, - start, end, y1, colourOnly); + boolean drawn = renderFeature(g, seq, featureStartCol - 1, + featureStartCol - 1, featureColour, start, end, y1, + colourOnly); + drawn |= renderFeature(g, seq, featureEndCol - 1, + featureEndCol - 1, featureColour, start, end, y1, + colourOnly); if (drawn) { drawnColour = featureColour; } } - else if (showFeature(sequenceFeature)) + else { + /* + * showing feature score by height of colour + * is not implemented as a selectable option + * if (av.isShowSequenceFeaturesHeight() && !Float.isNaN(sequenceFeature.score)) { @@ -398,19 +399,28 @@ public class FeatureRenderer extends FeatureRendererModel } else { - boolean drawn = renderFeature(g, seq, - seq.findIndex(sequenceFeature.begin) - 1, - seq.findIndex(sequenceFeature.end) - 1, featureColour, - start, end, y1, colourOnly); - if (drawn) - { - drawnColour = featureColour; - } + */ + boolean drawn = renderFeature(g, seq, featureStartCol - 1, + featureEndCol - 1, featureColour, start, end, y1, + colourOnly); + if (drawn) + { + drawnColour = featureColour; } + /*}*/ } } } + /* + * draw 'complement' features above ours if configured to do so + */ + if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop()) + { + drawnColour = drawComplementFeatures(g, seq, start, end, y1, + colourOnly, visiblePositions, drawnColour); + } + if (transparency != 1.0f && g != null) { /* @@ -424,21 +434,48 @@ public class FeatureRenderer extends FeatureRendererModel } /** - * Answers true if the feature belongs to a feature group which is not - * currently displayed, else false + * Find any features on the CDS/protein complement of the sequence region and + * draw them, with visibility and colouring as configured in the complementary + * viewport * - * @param sequenceFeature + * @param g + * @param seq + * @param start + * @param end + * @param y1 + * @param colourOnly + * @param visiblePositions + * @param drawnColour * @return */ - protected boolean featureGroupNotShown( - final SequenceFeature sequenceFeature) + Color drawComplementFeatures(final Graphics g, final SequenceI seq, + int start, int end, int y1, boolean colourOnly, + ContiguousI visiblePositions, Color drawnColour) { - return featureGroups != null - && sequenceFeature.featureGroup != null - && sequenceFeature.featureGroup.length() != 0 - && featureGroups.containsKey(sequenceFeature.featureGroup) - && !featureGroups.get(sequenceFeature.featureGroup) - .booleanValue(); + AlignViewportI comp = av.getCodingComplement(); + FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp) + .getFeatureRenderer(); + + final int visibleStart = visiblePositions.getBegin(); + final int visibleEnd = visiblePositions.getEnd(); + + for (int pos = visibleStart; pos <= visibleEnd; pos++) + { + int column = seq.findIndex(pos); + MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq, pos); + if (mf != null) + { + for (SequenceFeature sf : mf.features) + { + FeatureColourI fc = fr2.getFeatureStyle(sf.getType()); + Color featureColour = fr2.getColor(sf, fc); + renderFeature(g, seq, column - 1, column - 1, featureColour, + start, end, y1, colourOnly); + drawnColour = featureColour; + } + } + } + return drawnColour; } /** @@ -449,38 +486,50 @@ public class FeatureRenderer extends FeatureRendererModel @Override public void featuresAdded() { - lastSeq = null; findAllFeatures(); } /** - * Returns the sequence feature colour rendered at the given sequence - * position, or null if none found. The feature of highest render order (i.e. - * on top) is found, subject to both feature type and feature group being - * visible, and its colour returned. + * Returns the sequence feature colour rendered at the given column position, + * or null if none found. The feature of highest render order (i.e. on top) is + * found, subject to both feature type and feature group being visible, and + * its colour returned. This method is suitable when no feature transparency + * applied (only the topmost visible feature colour is rendered). + *

+ * Note this method does not check for a gap in the column so would return the + * colour for features enclosing a gapped column. Check for gap before calling + * if different behaviour is wanted. * * @param seq - * @param pos + * @param column + * (1..) * @return */ - Color findFeatureColour(SequenceI seq, int pos) + Color findFeatureColour(SequenceI seq, int column) { - SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures(); - if (sequenceFeatures == null || sequenceFeatures.length == 0) - { - return null; - } - /* * check for new feature added while processing */ updateFeatures(); /* + * show complement features on top (if configured to show them) + */ + if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop()) + { + Color col = findComplementFeatureColour(seq, column); + if (col != null) + { + return col; + } + } + + /* * inspect features in reverse renderOrder (the last in the array is * displayed on top) until we find one that is rendered at the position */ - for (int renderIndex = renderOrder.length - 1; renderIndex >= 0; renderIndex--) + for (int renderIndex = renderOrder.length + - 1; renderIndex >= 0; renderIndex--) { String type = renderOrder[renderIndex]; if (!showFeatureOfType(type)) @@ -488,40 +537,64 @@ public class FeatureRenderer extends FeatureRendererModel continue; } - for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++) + /* + * find features of this type, and the colour of the _last_ one + * (the one that would be drawn on top) that has a colour + */ + List overlaps = seq.findFeatures(column, column, + type); + for (int i = overlaps.size() - 1 ; i >= 0 ; i--) { - SequenceFeature sequenceFeature = sequenceFeatures[sfindex]; - if (!sequenceFeature.type.equals(type)) + SequenceFeature sequenceFeature = overlaps.get(i); + if (!featureGroupNotShown(sequenceFeature)) { - continue; + Color col = getColour(sequenceFeature); + if (col != null) + { + return col; + } } + } + } - if (featureGroupNotShown(sequenceFeature)) - { - continue; - } + /* + * show complement features underneath (if configured to show them) + */ + Color col = null; + if (av.isShowComplementFeatures() + && !av.isShowComplementFeaturesOnTop()) + { + col = findComplementFeatureColour(seq, column); + } - /* - * check the column position is within the feature range - * (or is one of the two contact positions for a contact feature) - */ - boolean featureIsAtPosition = sequenceFeature.begin <= pos - && sequenceFeature.end >= pos; - if (sequenceFeature.isContactFeature()) - { - featureIsAtPosition = sequenceFeature.begin == pos - || sequenceFeature.end == pos; - } - if (featureIsAtPosition) + return col; + } + + Color findComplementFeatureColour(SequenceI seq, int column) + { + AlignViewportI complement = av.getCodingComplement(); + AlignFrame af = Desktop.getAlignFrameFor(complement); + FeatureRendererModel fr2 = af.getFeatureRenderer(); + MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq, + seq.findPosition(column - 1)); + if (mf == null) + { + return null; + } + ReverseListIterator it = new ReverseListIterator<>( + mf.features); + while (it.hasNext()) + { + SequenceFeature sf = it.next(); + if (!fr2.featureGroupNotShown(sf)) + { + Color col = fr2.getColour(sf); + if (col != null) { - return getColour(sequenceFeature); + return col; } } } - - /* - * no displayed feature found at position - */ return null; } }