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;
61 public ArrayList<SimpleBP> bps = null;
64 * RNA secondary structure contact positions
66 public SequenceFeature[] _rnasecstr = null;
69 * position of annotation resulting in invalid WUSS parsing or -1
71 private long invalidrnastruc = -1;
74 * Updates the _rnasecstr field Determines the positions that base pair and
75 * the positions of helices based on secondary structure from a Stockholm file
79 private void _updateRnaSecStr(CharSequence RNAannot)
83 _rnasecstr = Rna.GetBasePairs(RNAannot);
84 bps = Rna.GetModeleBP(RNAannot);
86 } catch (WUSSParseException px)
88 // DEBUG System.out.println(px);
89 invalidrnastruc = px.getProblemPos();
91 if (invalidrnastruc > -1)
95 Rna.HelixMap(_rnasecstr);
96 // setRNAStruc(RNAannot);
98 if (_rnasecstr != null && _rnasecstr.length > 0)
100 // show all the RNA secondary structure annotation symbols.
102 showAllColLabels = true;
103 scaleColLabel = true;
105 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
108 public java.util.Hashtable sequenceMapping;
111 public float graphMin;
114 public float graphMax;
117 * Score associated with label and description.
119 public double score = Double.NaN;
122 * flag indicating if annotation has a score.
124 public boolean hasScore = false;
126 public GraphLine threshold;
128 // Graphical hints and tips
130 /** Can this row be edited by the user ? */
131 public boolean editable = false;
133 /** Indicates if annotation has a graphical symbol track */
134 public boolean hasIcons; //
136 /** Indicates if annotation has a text character label */
137 public boolean hasText;
139 /** is the row visible */
140 public boolean visible = true;
142 public int graphGroup = -1;
144 /** Displayed height of row in pixels */
145 public int height = 0;
147 public int graph = 0;
149 public int graphHeight = 40;
151 public boolean padGaps = false;
153 public static final int NO_GRAPH = 0;
155 public static final int BAR_GRAPH = 1;
157 public static final int LINE_GRAPH = 2;
159 public boolean belowAlignment = true;
161 public SequenceGroup groupRef = null;
164 * display every column label, even if there is a row of identical labels
166 public boolean showAllColLabels = false;
169 * scale the column label to fit within the alignment column.
171 public boolean scaleColLabel = false;
174 * centre the column labels relative to the alignment column
176 public boolean centreColLabels = false;
178 private boolean isrna;
183 * @see java.lang.Object#finalize()
185 protected void finalize() throws Throwable
192 public static int getGraphValueFromString(String string)
194 if (string.equalsIgnoreCase("BAR_GRAPH"))
198 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);
215 * Creates a new AlignmentAnnotation object.
218 * short label shown under sequence labels
220 * text displayed on mouseover
222 * set of positional annotation elements
224 public AlignmentAnnotation(String label, String description,
225 Annotation[] annotations)
230 this.description = description;
231 this.annotations = annotations;
233 validateRangeAndDisplay();
237 * Checks if annotation labels represent secondary structures
240 void areLabelsSecondaryStructure()
242 boolean nonSSLabel = false;
244 StringBuffer rnastring = new StringBuffer();
247 for (int i = 0; i < annotations.length; i++)
249 if (annotations[i] == null)
253 if (annotations[i].secondaryStructure == 'H'
254 || annotations[i].secondaryStructure == 'E')
259 // Check for RNA secondary structure
261 // System.out.println(annotations[i].secondaryStructure);
262 // TODO: 2.8.2 should this ss symbol validation check be a function in RNA/ResidueProperties ?
263 if (annotations[i].secondaryStructure == '('
264 || annotations[i].secondaryStructure == '['
265 || annotations[i].secondaryStructure == '<'
266 || annotations[i].secondaryStructure == '{'
267 || annotations[i].secondaryStructure == 'A'
268 || annotations[i].secondaryStructure == 'B'
269 || annotations[i].secondaryStructure == 'C'
270 || annotations[i].secondaryStructure == 'D'
271 || annotations[i].secondaryStructure == 'E'
272 || annotations[i].secondaryStructure == 'F'
273 || annotations[i].secondaryStructure == 'G'
274 || annotations[i].secondaryStructure == 'H'
275 || annotations[i].secondaryStructure == 'I'
276 || annotations[i].secondaryStructure == 'J'
277 || annotations[i].secondaryStructure == 'K'
278 || annotations[i].secondaryStructure == 'L'
279 || annotations[i].secondaryStructure == 'M'
280 || annotations[i].secondaryStructure == 'N'
281 || annotations[i].secondaryStructure == 'O'
282 || annotations[i].secondaryStructure == 'P'
283 || annotations[i].secondaryStructure == 'Q'
284 || annotations[i].secondaryStructure == 'R'
285 || annotations[i].secondaryStructure == 'S'
286 || annotations[i].secondaryStructure == 'T'
287 || annotations[i].secondaryStructure == 'U'
288 || annotations[i].secondaryStructure == 'V'
289 || annotations[i].secondaryStructure == 'W'
290 || annotations[i].secondaryStructure == 'X'
291 || annotations[i].secondaryStructure == 'Y'
292 || annotations[i].secondaryStructure == 'Z')
299 // System.out.println("displaychar " + annotations[i].displayCharacter);
301 if (annotations[i].displayCharacter == null
302 || annotations[i].displayCharacter.length() == 0)
304 rnastring.append('.');
307 if (annotations[i].displayCharacter.length() == 1)
309 firstChar = annotations[i].displayCharacter.charAt(0);
310 // check to see if it looks like a sequence or is secondary structure
312 if (annotations[i].secondaryStructure != ' '
315 // Uncomment to only catch case where
316 // displayCharacter==secondary
318 // to correctly redisplay SS annotation imported from Stockholm,
319 // exported to JalviewXML and read back in again.
321 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
356 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
358 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
371 rnastring.append(annotations[i].displayCharacter.charAt(1));
374 if (annotations[i].displayCharacter.length() > 0)
383 for (int j = 0; j < annotations.length; j++)
385 if (annotations[j] != null
386 && annotations[j].secondaryStructure != ' ')
388 annotations[j].displayCharacter = String
389 .valueOf(annotations[j].secondaryStructure);
390 annotations[j].secondaryStructure = ' ';
399 _updateRnaSecStr(new AnnotCharSequence());
403 annotationId = this.hashCode() + "";
407 * flyweight access to positions in the alignment annotation row for RNA
413 private class AnnotCharSequence implements CharSequence
419 public AnnotCharSequence()
421 this(0, annotations.length);
424 public AnnotCharSequence(int start, int end)
431 public CharSequence subSequence(int start, int end)
433 return new AnnotCharSequence(offset + start, offset + end);
443 public char charAt(int index)
446 return ((index + offset < 0) || (index + offset) >= max
447 || annotations[index + offset] == null || (dc = annotations[index
448 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
452 public String toString()
454 char[] string = new char[max - offset];
455 int mx = annotations.length;
457 for (int i = offset; i < mx; i++)
460 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
461 .trim()).length() < 1) ? '.' : dc.charAt(0);
463 return new String(string);
467 private long _lastrnaannot = -1;
469 public String getRNAStruc()
473 String rnastruc = new AnnotCharSequence().toString();
474 if (_lastrnaannot != rnastruc.hashCode())
476 // ensure rna structure contacts are up to date
477 _lastrnaannot = rnastruc.hashCode();
478 _updateRnaSecStr(rnastruc);
486 * Creates a new AlignmentAnnotation object.
501 public AlignmentAnnotation(String label, String description,
502 Annotation[] annotations, float min, float max, int graphType)
504 // graphs are not editable
505 editable = graphType == 0;
508 this.description = description;
509 this.annotations = annotations;
513 validateRangeAndDisplay();
517 * checks graphMin and graphMax, secondary structure symbols, sets graphType
518 * appropriately, sets null labels to the empty string if appropriate.
520 public void validateRangeAndDisplay()
523 if (annotations == null)
525 visible = false; // try to prevent renderer from displaying.
526 return; // this is a non-annotation row annotation - ie a sequence score.
529 int graphType = graph;
530 float min = graphMin;
531 float max = graphMax;
532 boolean drawValues = true;
537 for (int i = 0; i < annotations.length; i++)
539 if (annotations[i] == null)
544 if (drawValues && annotations[i].displayCharacter != null
545 && annotations[i].displayCharacter.length() > 1)
550 if (annotations[i].value > max)
552 max = annotations[i].value;
555 if (annotations[i].value < min)
557 min = annotations[i].value;
559 if (_linecolour == null && annotations[i].colour != null)
561 _linecolour = annotations[i].colour;
564 // ensure zero is origin for min/max ranges on only one side of zero
581 areLabelsSecondaryStructure();
583 if (!drawValues && graphType != NO_GRAPH)
585 for (int i = 0; i < annotations.length; i++)
587 if (annotations[i] != null)
589 annotations[i].displayCharacter = "X";
596 * Copy constructor creates a new independent annotation row with the same
597 * associated sequenceRef
601 public AlignmentAnnotation(AlignmentAnnotation annotation)
603 this.label = new String(annotation.label);
604 if (annotation.description != null)
605 this.description = new String(annotation.description);
606 this.graphMin = annotation.graphMin;
607 this.graphMax = annotation.graphMax;
608 this.graph = annotation.graph;
609 this.graphHeight = annotation.graphHeight;
610 this.graphGroup = annotation.graphGroup;
611 this.groupRef = annotation.groupRef;
612 this.editable = annotation.editable;
613 this.autoCalculated = annotation.autoCalculated;
614 this.hasIcons = annotation.hasIcons;
615 this.hasText = annotation.hasText;
616 this.height = annotation.height;
617 this.label = annotation.label;
618 this.padGaps = annotation.padGaps;
619 this.visible = annotation.visible;
620 this.centreColLabels = annotation.centreColLabels;
621 this.scaleColLabel = annotation.scaleColLabel;
622 this.showAllColLabels = annotation.showAllColLabels;
623 this.calcId = annotation.calcId;
624 if (this.hasScore = annotation.hasScore)
626 this.score = annotation.score;
628 if (annotation.threshold != null)
630 threshold = new GraphLine(annotation.threshold);
632 if (annotation.annotations != null)
634 Annotation[] ann = annotation.annotations;
635 this.annotations = new Annotation[ann.length];
636 for (int i = 0; i < ann.length; i++)
640 annotations[i] = new Annotation(ann[i]);
641 if (_linecolour != null)
643 _linecolour = annotations[i].colour;
648 if (annotation.sequenceRef != null)
650 this.sequenceRef = annotation.sequenceRef;
651 if (annotation.sequenceMapping != null)
654 sequenceMapping = new Hashtable();
655 Enumeration pos = annotation.sequenceMapping.keys();
656 while (pos.hasMoreElements())
658 // could optimise this!
659 p = (Integer) pos.nextElement();
660 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
665 for (int i = 0; i < ann.length; i++)
669 sequenceMapping.put(p, annotations[i]);
676 this.sequenceMapping = null;
680 // TODO: check if we need to do this: JAL-952
681 // if (this.isrna=annotation.isrna)
683 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
685 validateRangeAndDisplay(); // construct hashcodes, etc.
689 * clip the annotation to the columns given by startRes and endRes (inclusive)
690 * and prune any existing sequenceMapping to just those columns.
695 public void restrict(int startRes, int endRes)
697 if (annotations == null)
704 if (startRes >= annotations.length)
705 startRes = annotations.length - 1;
706 if (endRes >= annotations.length)
707 endRes = annotations.length - 1;
708 if (annotations == null)
710 Annotation[] temp = new Annotation[endRes - startRes + 1];
711 if (startRes < annotations.length)
713 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
716 if (sequenceRef != null)
718 // Clip the mapping, if it exists.
719 int spos = sequenceRef.findPosition(startRes);
720 int epos = sequenceRef.findPosition(endRes);
721 if (sequenceMapping != null)
723 Hashtable newmapping = new Hashtable();
724 Enumeration e = sequenceMapping.keys();
725 while (e.hasMoreElements())
727 Integer pos = (Integer) e.nextElement();
728 if (pos.intValue() >= spos && pos.intValue() <= epos)
730 newmapping.put(pos, sequenceMapping.get(pos));
733 sequenceMapping.clear();
734 sequenceMapping = newmapping;
741 * set the annotation row to be at least length Annotations
744 * minimum number of columns required in the annotation row
745 * @return false if the annotation row is greater than length
747 public boolean padAnnotation(int length)
749 if (annotations == null)
751 return true; // annotation row is correct - null == not visible and
754 if (annotations.length < length)
756 Annotation[] na = new Annotation[length];
757 System.arraycopy(annotations, 0, na, 0, annotations.length);
761 return annotations.length > length;
768 * @return DOCUMENT ME!
770 public String toString()
772 StringBuffer buffer = new StringBuffer();
774 for (int i = 0; i < annotations.length; i++)
776 if (annotations[i] != null)
780 buffer.append(annotations[i].value);
784 buffer.append(annotations[i].secondaryStructure);
788 buffer.append(annotations[i].displayCharacter);
794 // TODO: remove disgusting hack for 'special' treatment of consensus line.
795 if (label.indexOf("Consensus") == 0)
799 for (int i = 0; i < annotations.length; i++)
801 if (annotations[i] != null)
803 buffer.append(annotations[i].description);
810 return buffer.toString();
813 public void setThreshold(GraphLine line)
818 public GraphLine getThreshold()
824 * Attach the annotation to seqRef, starting from startRes position. If
825 * alreadyMapped is true then the indices of the annotation[] array are
826 * sequence positions rather than alignment column positions.
830 * @param alreadyMapped
832 public void createSequenceMapping(SequenceI seqRef, int startRes,
833 boolean alreadyMapped)
840 sequenceRef = seqRef;
841 if (annotations == null)
845 sequenceMapping = new java.util.Hashtable();
849 for (int i = 0; i < annotations.length; i++)
851 if (annotations[i] != null)
855 seqPos = seqRef.findPosition(i);
859 seqPos = i + startRes;
862 sequenceMapping.put(new Integer(seqPos), annotations[i]);
868 public void adjustForAlignment()
870 if (sequenceRef == null)
873 if (annotations == null)
878 int a = 0, aSize = sequenceRef.getLength();
887 Annotation[] temp = new Annotation[aSize];
890 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
892 index = new Integer(a);
893 if (sequenceMapping.containsKey(index))
895 position = sequenceRef.findIndex(a) - 1;
897 temp[position] = (Annotation) sequenceMapping.get(index);
905 * remove any null entries in annotation row and return the number of non-null
906 * annotation elements.
910 public int compactAnnotationArray()
912 int i = 0, iSize = annotations.length;
915 if (annotations[i] == null)
918 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
927 Annotation[] ann = annotations;
928 annotations = new Annotation[i];
929 System.arraycopy(ann, 0, annotations, 0, i);
935 * Associate this annotion with the aligned residues of a particular sequence.
936 * sequenceMapping will be updated in the following way: null sequenceI -
937 * existing mapping will be discarded but annotations left in mapped
938 * positions. valid sequenceI not equal to current sequenceRef: mapping is
939 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
940 * parameter to specify correspondence between current and new sequenceRef
944 public void setSequenceRef(SequenceI sequenceI)
946 if (sequenceI != null)
948 if (sequenceRef != null)
950 if (sequenceRef != sequenceI
951 && !sequenceRef.equals(sequenceI)
952 && sequenceRef.getDatasetSequence() != sequenceI
953 .getDatasetSequence())
955 // if sequenceRef isn't intersecting with sequenceI
956 // throw away old mapping and reconstruct.
958 if (sequenceMapping != null)
960 sequenceMapping = null;
961 // compactAnnotationArray();
963 createSequenceMapping(sequenceI, 1, true);
964 adjustForAlignment();
968 // Mapping carried over
969 sequenceRef = sequenceI;
975 createSequenceMapping(sequenceI, 1, true);
976 adjustForAlignment();
981 // throw away the mapping without compacting.
982 sequenceMapping = null;
990 public double getScore()
999 public void setScore(double score)
1007 * @return true if annotation has an associated score
1009 public boolean hasScore()
1011 return hasScore || !Double.isNaN(score);
1015 * Score only annotation
1018 * @param description
1021 public AlignmentAnnotation(String label, String description, double score)
1023 this(label, description, null);
1028 * copy constructor with edit based on the hidden columns marked in colSel
1030 * @param alignmentAnnotation
1033 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1034 ColumnSelection colSel)
1036 this(alignmentAnnotation);
1037 if (annotations == null)
1041 colSel.makeVisibleAnnotation(this);
1044 public void setPadGaps(boolean padgaps, char gapchar)
1046 this.padGaps = padgaps;
1050 for (int i = 0; i < annotations.length; i++)
1052 if (annotations[i] == null)
1053 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1055 else if (annotations[i].displayCharacter == null
1056 || annotations[i].displayCharacter.equals(" "))
1057 annotations[i].displayCharacter = String.valueOf(gapchar);
1063 * format description string for display
1066 * @return Get the annotation description string optionally prefixed by
1067 * associated sequence name (if any)
1069 public String getDescription(boolean seqname)
1071 if (seqname && this.sequenceRef != null)
1073 int i = description.toLowerCase().indexOf("<html>");
1076 // move the html tag to before the sequence reference.
1077 return "<html>" + sequenceRef.getName() + " : "
1078 + description.substring(i + 6);
1080 return sequenceRef.getName() + " : " + description;
1085 public boolean isValidStruc()
1087 return invalidrnastruc == -1;
1090 public long getInvalidStrucPos()
1092 return invalidrnastruc;
1096 * machine readable ID string indicating what generated this annotation
1098 protected String calcId = "";
1101 * base colour for line graphs. If null, will be set automatically by
1102 * searching the alignment annotation
1104 public java.awt.Color _linecolour;
1106 public String getCalcId()
1111 public void setCalcId(String calcId)
1113 this.calcId = calcId;
1116 public boolean isRNA()