X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fschemes%2FResidueColourScheme.java;fp=src%2Fjalview%2Fschemes%2FResidueColourScheme.java;h=34a5daa6ab9dca3ef2c246f1857263dfdf493cbe;hb=f063821ed0be9c1581af74643a1aa5798731af65;hp=f6b7c5e16e31cd3da8f34f14597aa751cb071c90;hpb=fd18e2c73cd015d4e38ad91da0e5d7532ff0ef42;p=jalview.git diff --git a/src/jalview/schemes/ResidueColourScheme.java b/src/jalview/schemes/ResidueColourScheme.java index f6b7c5e..34a5daa 100755 --- a/src/jalview/schemes/ResidueColourScheme.java +++ b/src/jalview/schemes/ResidueColourScheme.java @@ -20,69 +20,58 @@ */ package jalview.schemes; -import jalview.analysis.Conservation; import jalview.datamodel.AnnotatedCollectionI; -import jalview.datamodel.ProfileI; -import jalview.datamodel.ProfilesI; import jalview.datamodel.SequenceCollectionI; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; -import jalview.util.ColorUtils; -import jalview.util.Comparison; -import jalview.util.MessageManager; import java.awt.Color; import java.util.Map; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * Base class for residue-based colour schemes */ -public class ResidueColourScheme implements ColourSchemeI +public abstract class ResidueColourScheme implements ColourSchemeI { - final int[] symbolIndex; + public static final String NONE = "None"; - boolean conservationColouring = false; - - Color[] colors = null; - - int threshold = 0; - - /* Set when threshold colouring to either pid_gaps or pid_nogaps */ - protected boolean ignoreGaps = false; + /* + * default display name for a user defined colour scheme + */ + public static final String USER_DEFINED = "User Defined"; /* - * Consensus data indexed by column + * name for (new) "User Defined.." colour scheme menu item */ - ProfilesI consensus; + public static final String USER_DEFINED_MENU = "*User Defined*"; /* - * Conservation string as a char array + * lookup up by character value e.g. 'G' to the colors array index + * e.g. if symbolIndex['K'] = 11 then colors[11] is the colour for K */ - char[] conservation; + final int[] symbolIndex; /* - * The conservation slider percentage setting + * colour for residue characters as indexed by symbolIndex */ - int inc = 30; + Color[] colors = null; + + /* Set when threshold colouring to either pid_gaps or pid_nogaps */ + protected boolean ignoreGaps = false; /** * Creates a new ResidueColourScheme object. * - * @param final int[] index table into colors (ResidueProperties.naIndex or - * ResidueProperties.aaIndex) + * @param final + * int[] index table into colors (ResidueProperties.naIndex or + * ResidueProperties.aaIndex) * @param colors * colours for symbols in sequences - * @param threshold - * threshold for conservation shading */ - public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours, - int threshold) + public ResidueColourScheme(int[] aaOrnaIndex, Color[] colours) { symbolIndex = aaOrnaIndex; this.colors = colours; - this.threshold = threshold; } /** @@ -106,241 +95,117 @@ public class ResidueColourScheme implements ColourSchemeI /** * Find a colour without an index in a sequence */ - @Override public Color findColour(char c) { - return colors == null ? Color.white : colors[symbolIndex[c]]; - } - - @Override - public Color findColour(char c, int j, SequenceI seq) - { - Color currentColour; + Color colour = Color.white; - if (colors != null && symbolIndex != null && (threshold == 0) - || aboveThreshold(c, j)) + if (colors != null && symbolIndex != null && c < symbolIndex.length + && symbolIndex[c] < colors.length) { - currentColour = colors[symbolIndex[c]]; + colour = colors[symbolIndex[c]]; } - else - { - currentColour = Color.white; - } - - if (conservationColouring) - { - currentColour = applyConservation(currentColour, j); - } - - return currentColour; - } - /** - * Get the percentage threshold for this colour scheme - * - * @return Returns the percentage threshold - */ - @Override - public int getThreshold() - { - return threshold; + return colour; } /** - * Sets the percentage consensus threshold value, and whether gaps are ignored - * in percentage identity calculation - * - * @param consensusThreshold - * @param ignoreGaps + * Default is to call the overloaded method that ignores consensus. A colour + * scheme that depends on consensus (for example, Blosum62), should override + * this method instead. */ @Override - public void setThreshold(int consensusThreshold, boolean ignoreGaps) + public Color findColour(char c, int j, SequenceI seq, + String consensusResidue, float pid) { - threshold = consensusThreshold; - this.ignoreGaps = ignoreGaps; + return findColour(c, j, seq); } /** - * Answers true if there is a consensus profile for the specified column, and - * the given residue matches the consensus (or joint consensus) residue for - * the column, and the percentage identity for the profile is equal to or - * greater than the current threshold; else answers false. The percentage - * calculation depends on whether or not we are ignoring gapped sequences. - * - * @param residue - * @param column - * (index into consensus profiles) + * Default implementation looks up the residue colour in a fixed scheme, or + * returns White if not found. Override this method for a colour scheme that + * depends on the column position or sequence. * + * @param c + * @param j + * @param seq * @return - * @see #setThreshold(int, boolean) */ - public boolean aboveThreshold(char residue, int column) - { - if ('a' <= residue && residue <= 'z') - { - // TO UPPERCASE !!! - // Faster than toUpperCase - residue -= ('a' - 'A'); - } - - if (consensus == null) - { - return false; - } - - ProfileI profile = consensus.get(column); - - /* - * test whether this is the consensus (or joint consensus) residue - */ - if (profile != null - && profile.getModalResidue().contains(String.valueOf(residue))) - { - if (profile.getPercentageIdentity(ignoreGaps) >= threshold) - { - return true; - } - } - - return false; - } - - @Override - public boolean conservationApplied() + protected Color findColour(char c, int j, SequenceI seq) { - return conservationColouring; + return findColour(c); } @Override - public void setConservationApplied(boolean conservationApplied) - { - conservationColouring = conservationApplied; - } - - @Override - public void setConservationInc(int i) - { - inc = i; - } - - @Override - public int getConservationInc() + public void alignmentChanged(AnnotatedCollectionI alignment, + Map hiddenReps) { - return inc; } /** - * DOCUMENT ME! - * - * @param consensus - * DOCUMENT ME! + * Answers false if the colour scheme is nucleotide or peptide specific, and + * the data does not match, else true. Override to modify or extend this test + * as required. */ @Override - public void setConsensus(ProfilesI consensus) - { - if (consensus == null) - { - return; - } - - this.consensus = consensus; - } - - @Override - public void setConservation(Conservation cons) + public boolean isApplicableTo(AnnotatedCollectionI ac) { - if (cons == null) + if (!isPeptideSpecific() && !isNucleotideSpecific()) { - conservationColouring = false; - conservation = null; + return true; } - else + if (ac == null) { - conservationColouring = true; - int iSize = cons.getConsSequence().getLength(); - conservation = new char[iSize]; - for (int i = 0; i < iSize; i++) - { - conservation[i] = cons.getConsSequence().getCharAt(i); - } + return true; } - - } - - /** - * Applies a combination of column conservation score, and conservation - * percentage slider, to 'bleach' out the residue colours towards white. - *

- * If a column is fully conserved (identical residues, conservation score 11, - * shown as *), or all 10 physico-chemical properties are conserved - * (conservation score 10, shown as +), then the colour is left unchanged. - *

- * Otherwise a 'bleaching' factor is computed and applied to the colour. This - * is designed to fade colours for scores of 0-9 completely to white at slider - * positions ranging from 18% - 100% respectively. - * - * @param currentColour - * @param column - * - * @return bleached (or unmodified) colour - */ - Color applyConservation(Color currentColour, int column) - { - if (conservation == null || conservation.length <= column) - { - return currentColour; - } - char conservationScore = conservation[column]; - /* - * if residues are fully conserved (* or 11), or all properties - * are conserved (+ or 10), leave colour unchanged + * pop-up menu on selection group before group created + * (no alignment context) */ - if (conservationScore == '*' || conservationScore == '+' - || conservationScore == (char) 10 - || conservationScore == (char) 11) + // TODO: add nucleotide flag to SequenceGroup? + if (ac instanceof SequenceGroup && ac.getContext() == null) { - return currentColour; - } - - if (Comparison.isGap(conservationScore)) - { - return Color.white; + return true; } /* - * convert score 0-9 to a bleaching factor 1.1 - 0.2 + * inspect the data context (alignment) for residue type */ - float bleachFactor = (11 - (conservationScore - '0')) / 10f; + boolean nucleotide = ac.isNucleotide(); /* - * scale this up by 0-5 (percentage slider / 20) - * as a result, scores of: 0 1 2 3 4 5 6 7 8 9 - * fade to white at slider value: 18 20 22 25 29 33 40 50 67 100% + * does data type match colour scheme type? */ - bleachFactor *= (inc / 20f); + return (nucleotide && isNucleotideSpecific()) + || (!nucleotide && isPeptideSpecific()); + } - return ColorUtils.bleachColour(currentColour, bleachFactor); + /** + * Answers true if the colour scheme is normally only for peptide data + * + * @return + */ + public boolean isPeptideSpecific() + { + return false; } - @Override - public void alignmentChanged(AnnotatedCollectionI alignment, - Map hiddenReps) + /** + * Answers true if the colour scheme is normally only for nucleotide data + * + * @return + */ + public boolean isNucleotideSpecific() { + return false; } + /** + * Default method returns true. Override this to return false in colour + * schemes that are not determined solely by the sequence symbol. + */ @Override - public ColourSchemeI applyTo(AnnotatedCollectionI sg, - Map hiddenRepSequences) + public boolean isSimple() { - try - { - return getClass().newInstance(); - } catch (Exception q) - { - throw new Error(MessageManager.formatMessage( - "error.implementation_error_cannot_duplicate_colour_scheme", - new String[] { getClass().getName() }), q); - } + return true; } }