2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.analysis.Rna;
24 import jalview.analysis.SecStrConsensus.SimpleBP;
26 import jalview.analysis.WUSSParseException;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.Hashtable;
32 import fr.orsay.lri.varna.models.rna.RNA;
40 public class AlignmentAnnotation
43 * If true, this annotations is calculated every edit, eg consensus, quality
44 * or conservation graphs
46 public boolean autoCalculated = false;
48 public String annotationId;
50 public SequenceI sequenceRef;
56 public String description;
59 public Annotation[] annotations;
63 public ArrayList<SimpleBP> bps=null;
65 * RNA secondary structure contact positions
67 public SequenceFeature[] _rnasecstr = null;
70 * position of annotation resulting in invalid WUSS parsing or -1
72 private long invalidrnastruc = -1;
75 * Updates the _rnasecstr field Determines the positions that base pair and
76 * the positions of helices based on secondary structure from a Stockholm file
80 private void _updateRnaSecStr(CharSequence RNAannot)
83 _rnasecstr = Rna.GetBasePairs(RNAannot);
84 bps = Rna.GetModeleBP(RNAannot);
87 catch (WUSSParseException px)
89 // DEBUG System.out.println(px);
90 invalidrnastruc=px.getProblemPos();
92 if (invalidrnastruc > -1)
96 Rna.HelixMap(_rnasecstr);
97 // setRNAStruc(RNAannot);
99 if (_rnasecstr != null && _rnasecstr.length > 0)
101 // show all the RNA secondary structure annotation symbols.
103 showAllColLabels = true;
104 scaleColLabel = true;
106 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
109 public java.util.Hashtable sequenceMapping;
112 public float graphMin;
115 public float graphMax;
118 * Score associated with label and description.
120 public double score = Double.NaN;
123 * flag indicating if annotation has a score.
125 public boolean hasScore = false;
127 public GraphLine threshold;
129 // Graphical hints and tips
131 /** Can this row be edited by the user ? */
132 public boolean editable = false;
134 /** Indicates if annotation has a graphical symbol track */
135 public boolean hasIcons; //
137 /** Indicates if annotation has a text character label */
138 public boolean hasText;
140 /** is the row visible */
141 public boolean visible = true;
143 public int graphGroup = -1;
145 /** Displayed height of row in pixels */
146 public int height = 0;
148 public int graph = 0;
150 public int graphHeight = 40;
152 public boolean padGaps = false;
154 public static final int NO_GRAPH = 0;
156 public static final int BAR_GRAPH = 1;
158 public static final int LINE_GRAPH = 2;
160 public boolean belowAlignment = true;
162 public SequenceGroup groupRef = null;
165 * display every column label, even if there is a row of identical labels
167 public boolean showAllColLabels = false;
170 * scale the column label to fit within the alignment column.
172 public boolean scaleColLabel = false;
175 * centre the column labels relative to the alignment column
177 public boolean centreColLabels = false;
179 private boolean isrna;
184 * @see java.lang.Object#finalize()
186 protected void finalize() throws Throwable
193 public static int getGraphValueFromString(String string)
195 if (string.equalsIgnoreCase("BAR_GRAPH"))
199 else if (string.equalsIgnoreCase("LINE_GRAPH"))
208 // JBPNote: what does this do ?
209 public void ConcenStru(CharSequence RNAannot) throws WUSSParseException
211 bps = Rna.GetModeleBP(RNAannot);
214 * Creates a new AlignmentAnnotation object.
217 * short label shown under sequence labels
219 * text displayed on mouseover
221 * set of positional annotation elements
223 public AlignmentAnnotation(String label, String description,
224 Annotation[] annotations)
229 this.description = description;
230 this.annotations = annotations;
232 validateRangeAndDisplay();
236 * Checks if annotation labels represent secondary structures
239 void areLabelsSecondaryStructure()
241 boolean nonSSLabel = false;
243 StringBuffer rnastring = new StringBuffer();
246 for (int i = 0; i < annotations.length; i++)
248 if (annotations[i] == null)
252 if (annotations[i].secondaryStructure == 'H'
253 || annotations[i].secondaryStructure == 'E')
258 // Check for RNA secondary structure
260 //System.out.println(annotations[i].secondaryStructure);
261 if (annotations[i].secondaryStructure == '('
262 || annotations[i].secondaryStructure == '['
263 || annotations[i].secondaryStructure == '<'
264 || annotations[i].secondaryStructure == '{'
265 || annotations[i].secondaryStructure == 'A'
266 || annotations[i].secondaryStructure == 'B'
267 || annotations[i].secondaryStructure == 'C'
268 || annotations[i].secondaryStructure == 'D'
269 || annotations[i].secondaryStructure == 'E'
270 || annotations[i].secondaryStructure == 'F'
271 || annotations[i].secondaryStructure == 'G'
272 || annotations[i].secondaryStructure == 'H'
273 || annotations[i].secondaryStructure == 'I'
274 || annotations[i].secondaryStructure == 'J'
275 || annotations[i].secondaryStructure == 'K'
276 || annotations[i].secondaryStructure == 'L'
277 || annotations[i].secondaryStructure == 'M'
278 || annotations[i].secondaryStructure == 'N'
279 || annotations[i].secondaryStructure == 'O'
280 || annotations[i].secondaryStructure == 'P'
281 || annotations[i].secondaryStructure == 'Q'
282 || annotations[i].secondaryStructure == 'R'
283 || annotations[i].secondaryStructure == 'S'
284 || annotations[i].secondaryStructure == 'T'
285 || annotations[i].secondaryStructure == 'U'
286 || annotations[i].secondaryStructure == 'V'
287 || annotations[i].secondaryStructure == 'W'
288 || annotations[i].secondaryStructure == 'X'
289 || annotations[i].secondaryStructure == 'Y'
290 || annotations[i].secondaryStructure == 'Z')
297 // System.out.println("displaychar " + annotations[i].displayCharacter);
299 if (annotations[i].displayCharacter == null
300 || annotations[i].displayCharacter.length() == 0)
302 rnastring.append('.');
305 if (annotations[i].displayCharacter.length() == 1)
307 firstChar = annotations[i].displayCharacter.charAt(0);
308 // check to see if it looks like a sequence or is secondary structure
310 if (annotations[i].secondaryStructure != ' '
313 // Uncomment to only catch case where
314 // displayCharacter==secondary
316 // to correctly redisplay SS annotation imported from Stockholm,
317 // exported to JalviewXML and read back in again.
319 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
322 && firstChar != '�' // JBPNote should explicitly express as unicode number to avoid source code translation problems
354 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
356 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
369 rnastring.append(annotations[i].displayCharacter.charAt(1));
372 if (annotations[i].displayCharacter.length() > 0)
381 for (int j = 0; j < annotations.length; j++)
383 if (annotations[j] != null
384 && annotations[j].secondaryStructure != ' ')
386 annotations[j].displayCharacter = String
387 .valueOf(annotations[j].secondaryStructure);
388 annotations[j].secondaryStructure = ' ';
397 _updateRnaSecStr(new AnnotCharSequence());
401 annotationId = this.hashCode() + "";
405 * flyweight access to positions in the alignment annotation row for RNA
411 private class AnnotCharSequence implements CharSequence
417 public AnnotCharSequence()
419 this(0, annotations.length);
422 public AnnotCharSequence(int start, int end)
429 public CharSequence subSequence(int start, int end)
431 return new AnnotCharSequence(offset + start, offset + end);
441 public char charAt(int index)
444 return ((index + offset < 0) || (index + offset) >= max
445 || annotations[index + offset] == null || (dc = annotations[index
446 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
450 public String toString()
452 char[] string = new char[max - offset];
453 int mx = annotations.length;
455 for (int i = offset; i < mx; i++)
458 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
459 .trim()).length() < 1) ? '.' : dc.charAt(0);
461 return new String(string);
465 private long _lastrnaannot = -1;
467 public String getRNAStruc()
471 String rnastruc = new AnnotCharSequence().toString();
472 if (_lastrnaannot != rnastruc.hashCode())
474 // ensure rna structure contacts are up to date
475 _lastrnaannot = rnastruc.hashCode();
476 _updateRnaSecStr(rnastruc);
484 * Creates a new AlignmentAnnotation object.
499 public AlignmentAnnotation(String label, String description,
500 Annotation[] annotations, float min, float max, int graphType)
502 // graphs are not editable
503 editable = graphType == 0;
506 this.description = description;
507 this.annotations = annotations;
511 validateRangeAndDisplay();
515 * checks graphMin and graphMax, secondary structure symbols, sets graphType
516 * appropriately, sets null labels to the empty string if appropriate.
518 public void validateRangeAndDisplay()
521 if (annotations == null)
523 visible = false; // try to prevent renderer from displaying.
524 return; // this is a non-annotation row annotation - ie a sequence score.
527 int graphType = graph;
528 float min = graphMin;
529 float max = graphMax;
530 boolean drawValues = true;
535 for (int i = 0; i < annotations.length; i++)
537 if (annotations[i] == null)
542 if (drawValues && annotations[i].displayCharacter != null
543 && annotations[i].displayCharacter.length() > 1)
548 if (annotations[i].value > max)
550 max = annotations[i].value;
553 if (annotations[i].value < min)
555 min = annotations[i].value;
557 if (_linecolour == null && annotations[i].colour != null)
559 _linecolour = annotations[i].colour;
562 // ensure zero is origin for min/max ranges on only one side of zero
579 areLabelsSecondaryStructure();
581 if (!drawValues && graphType != NO_GRAPH)
583 for (int i = 0; i < annotations.length; i++)
585 if (annotations[i] != null)
587 annotations[i].displayCharacter = "X";
594 * Copy constructor creates a new independent annotation row with the same
595 * associated sequenceRef
599 public AlignmentAnnotation(AlignmentAnnotation annotation)
601 this.label = new String(annotation.label);
602 if (annotation.description != null)
603 this.description = new String(annotation.description);
604 this.graphMin = annotation.graphMin;
605 this.graphMax = annotation.graphMax;
606 this.graph = annotation.graph;
607 this.graphHeight = annotation.graphHeight;
608 this.graphGroup = annotation.graphGroup;
609 this.groupRef = annotation.groupRef;
610 this.editable = annotation.editable;
611 this.autoCalculated = annotation.autoCalculated;
612 this.hasIcons = annotation.hasIcons;
613 this.hasText = annotation.hasText;
614 this.height = annotation.height;
615 this.label = annotation.label;
616 this.padGaps = annotation.padGaps;
617 this.visible = annotation.visible;
618 this.centreColLabels = annotation.centreColLabels;
619 this.scaleColLabel = annotation.scaleColLabel;
620 this.showAllColLabels = annotation.showAllColLabels;
621 this.calcId = annotation.calcId;
622 if (this.hasScore = annotation.hasScore)
624 this.score = annotation.score;
626 if (annotation.threshold != null)
628 threshold = new GraphLine(annotation.threshold);
630 if (annotation.annotations != null)
632 Annotation[] ann = annotation.annotations;
633 this.annotations = new Annotation[ann.length];
634 for (int i = 0; i < ann.length; i++)
638 annotations[i] = new Annotation(ann[i]);
639 if (_linecolour != null)
641 _linecolour = annotations[i].colour;
646 if (annotation.sequenceRef != null)
648 this.sequenceRef = annotation.sequenceRef;
649 if (annotation.sequenceMapping != null)
652 sequenceMapping = new Hashtable();
653 Enumeration pos = annotation.sequenceMapping.keys();
654 while (pos.hasMoreElements())
656 // could optimise this!
657 p = (Integer) pos.nextElement();
658 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
663 for (int i = 0; i < ann.length; i++)
667 sequenceMapping.put(p, annotations[i]);
674 this.sequenceMapping = null;
678 // TODO: check if we need to do this: JAL-952
679 // if (this.isrna=annotation.isrna)
681 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
683 validateRangeAndDisplay(); // construct hashcodes, etc.
687 * clip the annotation to the columns given by startRes and endRes (inclusive)
688 * and prune any existing sequenceMapping to just those columns.
693 public void restrict(int startRes, int endRes)
695 if (annotations == null)
702 if (startRes >= annotations.length)
703 startRes = annotations.length - 1;
704 if (endRes >= annotations.length)
705 endRes = annotations.length - 1;
706 if (annotations == null)
708 Annotation[] temp = new Annotation[endRes - startRes + 1];
709 if (startRes < annotations.length)
711 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
714 if (sequenceRef != null)
716 // Clip the mapping, if it exists.
717 int spos = sequenceRef.findPosition(startRes);
718 int epos = sequenceRef.findPosition(endRes);
719 if (sequenceMapping != null)
721 Hashtable newmapping = new Hashtable();
722 Enumeration e = sequenceMapping.keys();
723 while (e.hasMoreElements())
725 Integer pos = (Integer) e.nextElement();
726 if (pos.intValue() >= spos && pos.intValue() <= epos)
728 newmapping.put(pos, sequenceMapping.get(pos));
731 sequenceMapping.clear();
732 sequenceMapping = newmapping;
739 * set the annotation row to be at least length Annotations
742 * minimum number of columns required in the annotation row
743 * @return false if the annotation row is greater than length
745 public boolean padAnnotation(int length)
747 if (annotations == null)
749 return true; // annotation row is correct - null == not visible and
752 if (annotations.length < length)
754 Annotation[] na = new Annotation[length];
755 System.arraycopy(annotations, 0, na, 0, annotations.length);
759 return annotations.length > length;
766 * @return DOCUMENT ME!
768 public String toString()
770 StringBuffer buffer = new StringBuffer();
772 for (int i = 0; i < annotations.length; i++)
774 if (annotations[i] != null)
778 buffer.append(annotations[i].value);
782 buffer.append(annotations[i].secondaryStructure);
786 buffer.append(annotations[i].displayCharacter);
792 // TODO: remove disgusting hack for 'special' treatment of consensus line.
793 if (label.indexOf("Consensus") == 0)
797 for (int i = 0; i < annotations.length; i++)
799 if (annotations[i] != null)
801 buffer.append(annotations[i].description);
808 return buffer.toString();
811 public void setThreshold(GraphLine line)
816 public GraphLine getThreshold()
822 * Attach the annotation to seqRef, starting from startRes position. If
823 * alreadyMapped is true then the indices of the annotation[] array are
824 * sequence positions rather than alignment column positions.
828 * @param alreadyMapped
830 public void createSequenceMapping(SequenceI seqRef, int startRes,
831 boolean alreadyMapped)
838 sequenceRef = seqRef;
839 if (annotations == null)
843 sequenceMapping = new java.util.Hashtable();
847 for (int i = 0; i < annotations.length; i++)
849 if (annotations[i] != null)
853 seqPos = seqRef.findPosition(i);
857 seqPos = i + startRes;
860 sequenceMapping.put(new Integer(seqPos), annotations[i]);
866 public void adjustForAlignment()
868 if (sequenceRef == null)
871 if (annotations == null)
876 int a = 0, aSize = sequenceRef.getLength();
885 Annotation[] temp = new Annotation[aSize];
888 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
890 index = new Integer(a);
891 if (sequenceMapping.containsKey(index))
893 position = sequenceRef.findIndex(a) - 1;
895 temp[position] = (Annotation) sequenceMapping.get(index);
903 * remove any null entries in annotation row and return the number of non-null
904 * annotation elements.
908 public int compactAnnotationArray()
910 int i = 0, iSize = annotations.length;
913 if (annotations[i] == null)
916 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
925 Annotation[] ann = annotations;
926 annotations = new Annotation[i];
927 System.arraycopy(ann, 0, annotations, 0, i);
933 * Associate this annotion with the aligned residues of a particular sequence.
934 * sequenceMapping will be updated in the following way: null sequenceI -
935 * existing mapping will be discarded but annotations left in mapped
936 * positions. valid sequenceI not equal to current sequenceRef: mapping is
937 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
938 * parameter to specify correspondence between current and new sequenceRef
942 public void setSequenceRef(SequenceI sequenceI)
944 if (sequenceI != null)
946 if (sequenceRef != null)
948 if (sequenceRef != sequenceI
949 && !sequenceRef.equals(sequenceI)
950 && sequenceRef.getDatasetSequence() != sequenceI
951 .getDatasetSequence())
953 // if sequenceRef isn't intersecting with sequenceI
954 // throw away old mapping and reconstruct.
956 if (sequenceMapping != null)
958 sequenceMapping = null;
959 // compactAnnotationArray();
961 createSequenceMapping(sequenceI, 1, true);
962 adjustForAlignment();
966 // Mapping carried over
967 sequenceRef = sequenceI;
973 createSequenceMapping(sequenceI, 1, true);
974 adjustForAlignment();
979 // throw away the mapping without compacting.
980 sequenceMapping = null;
988 public double getScore()
997 public void setScore(double score)
1005 * @return true if annotation has an associated score
1007 public boolean hasScore()
1009 return hasScore || !Double.isNaN(score);
1013 * Score only annotation
1016 * @param description
1019 public AlignmentAnnotation(String label, String description, double score)
1021 this(label, description, null);
1026 * copy constructor with edit based on the hidden columns marked in colSel
1028 * @param alignmentAnnotation
1031 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1032 ColumnSelection colSel)
1034 this(alignmentAnnotation);
1035 if (annotations == null)
1039 colSel.makeVisibleAnnotation(this);
1042 public void setPadGaps(boolean padgaps, char gapchar)
1044 this.padGaps = padgaps;
1048 for (int i = 0; i < annotations.length; i++)
1050 if (annotations[i] == null)
1051 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1053 else if (annotations[i].displayCharacter == null
1054 || annotations[i].displayCharacter.equals(" "))
1055 annotations[i].displayCharacter = String.valueOf(gapchar);
1061 * format description string for display
1064 * @return Get the annotation description string optionally prefixed by
1065 * associated sequence name (if any)
1067 public String getDescription(boolean seqname)
1069 if (seqname && this.sequenceRef != null)
1071 int i = description.toLowerCase().indexOf("<html>");
1074 // move the html tag to before the sequence reference.
1075 return "<html>" + sequenceRef.getName() + " : "
1076 + description.substring(i + 6);
1078 return sequenceRef.getName() + " : " + description;
1083 public boolean isValidStruc()
1085 return invalidrnastruc == -1;
1088 public long getInvalidStrucPos()
1090 return invalidrnastruc;
1094 * machine readable ID string indicating what generated this annotation
1096 protected String calcId = "";
1099 * base colour for line graphs. If null, will be set automatically by
1100 * searching the alignment annotation
1102 public java.awt.Color _linecolour;
1104 public String getCalcId()
1109 public void setCalcId(String calcId)
1111 this.calcId = calcId;