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 if (annotations[i].secondaryStructure == '('
263 || annotations[i].secondaryStructure == '['
264 || annotations[i].secondaryStructure == '<'
265 || annotations[i].secondaryStructure == '{'
266 || annotations[i].secondaryStructure == 'A'
267 || annotations[i].secondaryStructure == 'B'
268 || annotations[i].secondaryStructure == 'C'
269 || annotations[i].secondaryStructure == 'D'
270 || annotations[i].secondaryStructure == 'E'
271 || annotations[i].secondaryStructure == 'F'
272 || annotations[i].secondaryStructure == 'G'
273 || annotations[i].secondaryStructure == 'H'
274 || annotations[i].secondaryStructure == 'I'
275 || annotations[i].secondaryStructure == 'J'
276 || annotations[i].secondaryStructure == 'K'
277 || annotations[i].secondaryStructure == 'L'
278 || annotations[i].secondaryStructure == 'M'
279 || annotations[i].secondaryStructure == 'N'
280 || annotations[i].secondaryStructure == 'O'
281 || annotations[i].secondaryStructure == 'P'
282 || annotations[i].secondaryStructure == 'Q'
283 || annotations[i].secondaryStructure == 'R'
284 || annotations[i].secondaryStructure == 'S'
285 || annotations[i].secondaryStructure == 'T'
286 || annotations[i].secondaryStructure == 'U'
287 || annotations[i].secondaryStructure == 'V'
288 || annotations[i].secondaryStructure == 'W'
289 || annotations[i].secondaryStructure == 'X'
290 || annotations[i].secondaryStructure == 'Y'
291 || annotations[i].secondaryStructure == 'Z')
298 // System.out.println("displaychar " + annotations[i].displayCharacter);
300 if (annotations[i].displayCharacter == null
301 || annotations[i].displayCharacter.length() == 0)
303 rnastring.append('.');
306 if (annotations[i].displayCharacter.length() == 1)
308 firstChar = annotations[i].displayCharacter.charAt(0);
309 // check to see if it looks like a sequence or is secondary structure
311 if (annotations[i].secondaryStructure != ' '
314 // Uncomment to only catch case where
315 // displayCharacter==secondary
317 // to correctly redisplay SS annotation imported from Stockholm,
318 // exported to JalviewXML and read back in again.
320 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
323 && firstChar != '�' // JBPNote should explicitly express as
324 // unicode number to avoid source code
325 // translation problems
357 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
359 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
372 rnastring.append(annotations[i].displayCharacter.charAt(1));
375 if (annotations[i].displayCharacter.length() > 0)
384 for (int j = 0; j < annotations.length; j++)
386 if (annotations[j] != null
387 && annotations[j].secondaryStructure != ' ')
389 annotations[j].displayCharacter = String
390 .valueOf(annotations[j].secondaryStructure);
391 annotations[j].secondaryStructure = ' ';
400 _updateRnaSecStr(new AnnotCharSequence());
404 annotationId = this.hashCode() + "";
408 * flyweight access to positions in the alignment annotation row for RNA
414 private class AnnotCharSequence implements CharSequence
420 public AnnotCharSequence()
422 this(0, annotations.length);
425 public AnnotCharSequence(int start, int end)
432 public CharSequence subSequence(int start, int end)
434 return new AnnotCharSequence(offset + start, offset + end);
444 public char charAt(int index)
447 return ((index + offset < 0) || (index + offset) >= max
448 || annotations[index + offset] == null || (dc = annotations[index
449 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
453 public String toString()
455 char[] string = new char[max - offset];
456 int mx = annotations.length;
458 for (int i = offset; i < mx; i++)
461 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
462 .trim()).length() < 1) ? '.' : dc.charAt(0);
464 return new String(string);
468 private long _lastrnaannot = -1;
470 public String getRNAStruc()
474 String rnastruc = new AnnotCharSequence().toString();
475 if (_lastrnaannot != rnastruc.hashCode())
477 // ensure rna structure contacts are up to date
478 _lastrnaannot = rnastruc.hashCode();
479 _updateRnaSecStr(rnastruc);
487 * Creates a new AlignmentAnnotation object.
502 public AlignmentAnnotation(String label, String description,
503 Annotation[] annotations, float min, float max, int graphType)
505 // graphs are not editable
506 editable = graphType == 0;
509 this.description = description;
510 this.annotations = annotations;
514 validateRangeAndDisplay();
518 * checks graphMin and graphMax, secondary structure symbols, sets graphType
519 * appropriately, sets null labels to the empty string if appropriate.
521 public void validateRangeAndDisplay()
524 if (annotations == null)
526 visible = false; // try to prevent renderer from displaying.
527 return; // this is a non-annotation row annotation - ie a sequence score.
530 int graphType = graph;
531 float min = graphMin;
532 float max = graphMax;
533 boolean drawValues = true;
538 for (int i = 0; i < annotations.length; i++)
540 if (annotations[i] == null)
545 if (drawValues && annotations[i].displayCharacter != null
546 && annotations[i].displayCharacter.length() > 1)
551 if (annotations[i].value > max)
553 max = annotations[i].value;
556 if (annotations[i].value < min)
558 min = annotations[i].value;
560 if (_linecolour == null && annotations[i].colour != null)
562 _linecolour = annotations[i].colour;
565 // ensure zero is origin for min/max ranges on only one side of zero
582 areLabelsSecondaryStructure();
584 if (!drawValues && graphType != NO_GRAPH)
586 for (int i = 0; i < annotations.length; i++)
588 if (annotations[i] != null)
590 annotations[i].displayCharacter = "X";
597 * Copy constructor creates a new independent annotation row with the same
598 * associated sequenceRef
602 public AlignmentAnnotation(AlignmentAnnotation annotation)
604 this.label = new String(annotation.label);
605 if (annotation.description != null)
606 this.description = new String(annotation.description);
607 this.graphMin = annotation.graphMin;
608 this.graphMax = annotation.graphMax;
609 this.graph = annotation.graph;
610 this.graphHeight = annotation.graphHeight;
611 this.graphGroup = annotation.graphGroup;
612 this.groupRef = annotation.groupRef;
613 this.editable = annotation.editable;
614 this.autoCalculated = annotation.autoCalculated;
615 this.hasIcons = annotation.hasIcons;
616 this.hasText = annotation.hasText;
617 this.height = annotation.height;
618 this.label = annotation.label;
619 this.padGaps = annotation.padGaps;
620 this.visible = annotation.visible;
621 this.centreColLabels = annotation.centreColLabels;
622 this.scaleColLabel = annotation.scaleColLabel;
623 this.showAllColLabels = annotation.showAllColLabels;
624 this.calcId = annotation.calcId;
625 if (this.hasScore = annotation.hasScore)
627 this.score = annotation.score;
629 if (annotation.threshold != null)
631 threshold = new GraphLine(annotation.threshold);
633 if (annotation.annotations != null)
635 Annotation[] ann = annotation.annotations;
636 this.annotations = new Annotation[ann.length];
637 for (int i = 0; i < ann.length; i++)
641 annotations[i] = new Annotation(ann[i]);
642 if (_linecolour != null)
644 _linecolour = annotations[i].colour;
649 if (annotation.sequenceRef != null)
651 this.sequenceRef = annotation.sequenceRef;
652 if (annotation.sequenceMapping != null)
655 sequenceMapping = new Hashtable();
656 Enumeration pos = annotation.sequenceMapping.keys();
657 while (pos.hasMoreElements())
659 // could optimise this!
660 p = (Integer) pos.nextElement();
661 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
666 for (int i = 0; i < ann.length; i++)
670 sequenceMapping.put(p, annotations[i]);
677 this.sequenceMapping = null;
681 // TODO: check if we need to do this: JAL-952
682 // if (this.isrna=annotation.isrna)
684 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
686 validateRangeAndDisplay(); // construct hashcodes, etc.
690 * clip the annotation to the columns given by startRes and endRes (inclusive)
691 * and prune any existing sequenceMapping to just those columns.
696 public void restrict(int startRes, int endRes)
698 if (annotations == null)
705 if (startRes >= annotations.length)
706 startRes = annotations.length - 1;
707 if (endRes >= annotations.length)
708 endRes = annotations.length - 1;
709 if (annotations == null)
711 Annotation[] temp = new Annotation[endRes - startRes + 1];
712 if (startRes < annotations.length)
714 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
717 if (sequenceRef != null)
719 // Clip the mapping, if it exists.
720 int spos = sequenceRef.findPosition(startRes);
721 int epos = sequenceRef.findPosition(endRes);
722 if (sequenceMapping != null)
724 Hashtable newmapping = new Hashtable();
725 Enumeration e = sequenceMapping.keys();
726 while (e.hasMoreElements())
728 Integer pos = (Integer) e.nextElement();
729 if (pos.intValue() >= spos && pos.intValue() <= epos)
731 newmapping.put(pos, sequenceMapping.get(pos));
734 sequenceMapping.clear();
735 sequenceMapping = newmapping;
742 * set the annotation row to be at least length Annotations
745 * minimum number of columns required in the annotation row
746 * @return false if the annotation row is greater than length
748 public boolean padAnnotation(int length)
750 if (annotations == null)
752 return true; // annotation row is correct - null == not visible and
755 if (annotations.length < length)
757 Annotation[] na = new Annotation[length];
758 System.arraycopy(annotations, 0, na, 0, annotations.length);
762 return annotations.length > length;
769 * @return DOCUMENT ME!
771 public String toString()
773 StringBuffer buffer = new StringBuffer();
775 for (int i = 0; i < annotations.length; i++)
777 if (annotations[i] != null)
781 buffer.append(annotations[i].value);
785 buffer.append(annotations[i].secondaryStructure);
789 buffer.append(annotations[i].displayCharacter);
795 // TODO: remove disgusting hack for 'special' treatment of consensus line.
796 if (label.indexOf("Consensus") == 0)
800 for (int i = 0; i < annotations.length; i++)
802 if (annotations[i] != null)
804 buffer.append(annotations[i].description);
811 return buffer.toString();
814 public void setThreshold(GraphLine line)
819 public GraphLine getThreshold()
825 * Attach the annotation to seqRef, starting from startRes position. If
826 * alreadyMapped is true then the indices of the annotation[] array are
827 * sequence positions rather than alignment column positions.
831 * @param alreadyMapped
833 public void createSequenceMapping(SequenceI seqRef, int startRes,
834 boolean alreadyMapped)
841 sequenceRef = seqRef;
842 if (annotations == null)
846 sequenceMapping = new java.util.Hashtable();
850 for (int i = 0; i < annotations.length; i++)
852 if (annotations[i] != null)
856 seqPos = seqRef.findPosition(i);
860 seqPos = i + startRes;
863 sequenceMapping.put(new Integer(seqPos), annotations[i]);
869 public void adjustForAlignment()
871 if (sequenceRef == null)
874 if (annotations == null)
879 int a = 0, aSize = sequenceRef.getLength();
888 Annotation[] temp = new Annotation[aSize];
891 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
893 index = new Integer(a);
894 if (sequenceMapping.containsKey(index))
896 position = sequenceRef.findIndex(a) - 1;
898 temp[position] = (Annotation) sequenceMapping.get(index);
906 * remove any null entries in annotation row and return the number of non-null
907 * annotation elements.
911 public int compactAnnotationArray()
913 int i = 0, iSize = annotations.length;
916 if (annotations[i] == null)
919 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
928 Annotation[] ann = annotations;
929 annotations = new Annotation[i];
930 System.arraycopy(ann, 0, annotations, 0, i);
936 * Associate this annotion with the aligned residues of a particular sequence.
937 * sequenceMapping will be updated in the following way: null sequenceI -
938 * existing mapping will be discarded but annotations left in mapped
939 * positions. valid sequenceI not equal to current sequenceRef: mapping is
940 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
941 * parameter to specify correspondence between current and new sequenceRef
945 public void setSequenceRef(SequenceI sequenceI)
947 if (sequenceI != null)
949 if (sequenceRef != null)
951 if (sequenceRef != sequenceI
952 && !sequenceRef.equals(sequenceI)
953 && sequenceRef.getDatasetSequence() != sequenceI
954 .getDatasetSequence())
956 // if sequenceRef isn't intersecting with sequenceI
957 // throw away old mapping and reconstruct.
959 if (sequenceMapping != null)
961 sequenceMapping = null;
962 // compactAnnotationArray();
964 createSequenceMapping(sequenceI, 1, true);
965 adjustForAlignment();
969 // Mapping carried over
970 sequenceRef = sequenceI;
976 createSequenceMapping(sequenceI, 1, true);
977 adjustForAlignment();
982 // throw away the mapping without compacting.
983 sequenceMapping = null;
991 public double getScore()
1000 public void setScore(double score)
1008 * @return true if annotation has an associated score
1010 public boolean hasScore()
1012 return hasScore || !Double.isNaN(score);
1016 * Score only annotation
1019 * @param description
1022 public AlignmentAnnotation(String label, String description, double score)
1024 this(label, description, null);
1029 * copy constructor with edit based on the hidden columns marked in colSel
1031 * @param alignmentAnnotation
1034 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1035 ColumnSelection colSel)
1037 this(alignmentAnnotation);
1038 if (annotations == null)
1042 colSel.makeVisibleAnnotation(this);
1045 public void setPadGaps(boolean padgaps, char gapchar)
1047 this.padGaps = padgaps;
1051 for (int i = 0; i < annotations.length; i++)
1053 if (annotations[i] == null)
1054 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1056 else if (annotations[i].displayCharacter == null
1057 || annotations[i].displayCharacter.equals(" "))
1058 annotations[i].displayCharacter = String.valueOf(gapchar);
1064 * format description string for display
1067 * @return Get the annotation description string optionally prefixed by
1068 * associated sequence name (if any)
1070 public String getDescription(boolean seqname)
1072 if (seqname && this.sequenceRef != null)
1074 int i = description.toLowerCase().indexOf("<html>");
1077 // move the html tag to before the sequence reference.
1078 return "<html>" + sequenceRef.getName() + " : "
1079 + description.substring(i + 6);
1081 return sequenceRef.getName() + " : " + description;
1086 public boolean isValidStruc()
1088 return invalidrnastruc == -1;
1091 public long getInvalidStrucPos()
1093 return invalidrnastruc;
1097 * machine readable ID string indicating what generated this annotation
1099 protected String calcId = "";
1102 * base colour for line graphs. If null, will be set automatically by
1103 * searching the alignment annotation
1105 public java.awt.Color _linecolour;
1107 public String getCalcId()
1112 public void setCalcId(String calcId)
1114 this.calcId = calcId;