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;
25 import jalview.analysis.WUSSParseException;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.Hashtable;
30 import java.util.Map.Entry;
38 public class AlignmentAnnotation
41 * If true, this annotations is calculated every edit, eg consensus, quality
42 * or conservation graphs
44 public boolean autoCalculated = false;
46 public String annotationId;
48 public SequenceI sequenceRef;
54 public String description;
57 public Annotation[] annotations;
59 public ArrayList<SimpleBP> bps = null;
62 * RNA secondary structure contact positions
64 public SequenceFeature[] _rnasecstr = null;
67 * position of annotation resulting in invalid WUSS parsing or -1
69 private long invalidrnastruc = -1;
72 * Updates the _rnasecstr field Determines the positions that base pair and
73 * the positions of helices based on secondary structure from a Stockholm file
77 private void _updateRnaSecStr(CharSequence RNAannot)
81 _rnasecstr = Rna.GetBasePairs(RNAannot);
82 bps = Rna.GetModeleBP(RNAannot);
84 } catch (WUSSParseException px)
86 // DEBUG System.out.println(px);
87 invalidrnastruc = px.getProblemPos();
89 if (invalidrnastruc > -1)
93 Rna.HelixMap(_rnasecstr);
94 // setRNAStruc(RNAannot);
96 if (_rnasecstr != null && _rnasecstr.length > 0)
98 // show all the RNA secondary structure annotation symbols.
100 showAllColLabels = true;
101 scaleColLabel = true;
103 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
106 public java.util.Hashtable<Integer, Annotation> sequenceMapping;
109 public float graphMin;
112 public float graphMax;
115 * Score associated with label and description.
117 public double score = Double.NaN;
120 * flag indicating if annotation has a score.
122 public boolean hasScore = false;
124 public GraphLine threshold;
126 // Graphical hints and tips
128 /** Can this row be edited by the user ? */
129 public boolean editable = false;
131 /** Indicates if annotation has a graphical symbol track */
132 public boolean hasIcons; //
134 /** Indicates if annotation has a text character label */
135 public boolean hasText;
137 /** is the row visible */
138 public boolean visible = true;
140 public int graphGroup = -1;
142 /** Displayed height of row in pixels */
143 public int height = 0;
145 public int graph = 0;
147 public int graphHeight = 40;
149 public boolean padGaps = false;
151 public static final int NO_GRAPH = 0;
153 public static final int BAR_GRAPH = 1;
155 public static final int LINE_GRAPH = 2;
157 public boolean belowAlignment = true;
159 public SequenceGroup groupRef = null;
162 * display every column label, even if there is a row of identical labels
164 public boolean showAllColLabels = false;
167 * scale the column label to fit within the alignment column.
169 public boolean scaleColLabel = false;
172 * centre the column labels relative to the alignment column
174 public boolean centreColLabels = false;
176 private boolean isrna;
181 * @see java.lang.Object#finalize()
183 protected void finalize() throws Throwable
190 public static int getGraphValueFromString(String string)
192 if (string.equalsIgnoreCase("BAR_GRAPH"))
196 else if (string.equalsIgnoreCase("LINE_GRAPH"))
206 // JBPNote: what does this do ?
207 public void ConcenStru(CharSequence RNAannot) throws WUSSParseException
209 bps = Rna.GetModeleBP(RNAannot);
213 * Creates a new AlignmentAnnotation object.
216 * short label shown under sequence labels
218 * text displayed on mouseover
220 * set of positional annotation elements
222 public AlignmentAnnotation(String label, String description,
223 Annotation[] annotations)
228 this.description = description;
229 this.annotations = annotations;
231 validateRangeAndDisplay();
235 * Checks if annotation labels represent secondary structures
238 void areLabelsSecondaryStructure()
240 boolean nonSSLabel = false;
242 StringBuffer rnastring = new StringBuffer();
245 for (int i = 0; i < annotations.length; i++)
247 if (annotations[i] == null)
251 if (annotations[i].secondaryStructure == 'H'
252 || annotations[i].secondaryStructure == 'E')
257 // Check for RNA secondary structure
259 // System.out.println(annotations[i].secondaryStructure);
260 // TODO: 2.8.2 should this ss symbol validation check be a function in
261 // RNA/ResidueProperties ?
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
355 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
357 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
370 rnastring.append(annotations[i].displayCharacter.charAt(1));
373 if (annotations[i].displayCharacter.length() > 0)
382 for (int j = 0; j < annotations.length; j++)
384 if (annotations[j] != null
385 && annotations[j].secondaryStructure != ' ')
387 annotations[j].displayCharacter = String
388 .valueOf(annotations[j].secondaryStructure);
389 annotations[j].secondaryStructure = ' ';
398 _updateRnaSecStr(new AnnotCharSequence());
402 annotationId = this.hashCode() + "";
406 * flyweight access to positions in the alignment annotation row for RNA
412 private class AnnotCharSequence implements CharSequence
418 public AnnotCharSequence()
420 this(0, annotations.length);
423 public AnnotCharSequence(int start, int end)
430 public CharSequence subSequence(int start, int end)
432 return new AnnotCharSequence(offset + start, offset + end);
442 public char charAt(int index)
445 return ((index + offset < 0) || (index + offset) >= max
446 || annotations[index + offset] == null || (dc = annotations[index
447 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
451 public String toString()
453 char[] string = new char[max - offset];
454 int mx = annotations.length;
456 for (int i = offset; i < mx; i++)
459 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
460 .trim()).length() < 1) ? '.' : dc.charAt(0);
462 return new String(string);
466 private long _lastrnaannot = -1;
468 public String getRNAStruc()
472 String rnastruc = new AnnotCharSequence().toString();
473 if (_lastrnaannot != rnastruc.hashCode())
475 // ensure rna structure contacts are up to date
476 _lastrnaannot = rnastruc.hashCode();
477 _updateRnaSecStr(rnastruc);
485 * Creates a new AlignmentAnnotation object.
500 public AlignmentAnnotation(String label, String description,
501 Annotation[] annotations, float min, float max, int graphType)
503 // graphs are not editable
504 editable = graphType == 0;
507 this.description = description;
508 this.annotations = annotations;
512 validateRangeAndDisplay();
516 * checks graphMin and graphMax, secondary structure symbols, sets graphType
517 * appropriately, sets null labels to the empty string if appropriate.
519 public void validateRangeAndDisplay()
522 if (annotations == null)
524 visible = false; // try to prevent renderer from displaying.
525 return; // this is a non-annotation row annotation - ie a sequence score.
528 int graphType = graph;
529 float min = graphMin;
530 float max = graphMax;
531 boolean drawValues = true;
536 for (int i = 0; i < annotations.length; i++)
538 if (annotations[i] == null)
543 if (drawValues && annotations[i].displayCharacter != null
544 && annotations[i].displayCharacter.length() > 1)
549 if (annotations[i].value > max)
551 max = annotations[i].value;
554 if (annotations[i].value < min)
556 min = annotations[i].value;
558 if (_linecolour == null && annotations[i].colour != null)
560 _linecolour = annotations[i].colour;
563 // ensure zero is origin for min/max ranges on only one side of zero
580 areLabelsSecondaryStructure();
582 if (!drawValues && graphType != NO_GRAPH)
584 for (int i = 0; i < annotations.length; i++)
586 if (annotations[i] != null)
588 annotations[i].displayCharacter = "X";
595 * Copy constructor creates a new independent annotation row with the same
596 * associated sequenceRef
600 public AlignmentAnnotation(AlignmentAnnotation annotation)
602 this.label = new String(annotation.label);
603 if (annotation.description != null)
605 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.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)
707 if (startRes >= annotations.length)
709 startRes = annotations.length - 1;
711 if (endRes >= annotations.length)
713 endRes = annotations.length - 1;
715 if (annotations == null)
719 Annotation[] temp = new Annotation[endRes - startRes + 1];
720 if (startRes < annotations.length)
722 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
725 if (sequenceRef != null)
727 // Clip the mapping, if it exists.
728 int spos = sequenceRef.findPosition(startRes);
729 int epos = sequenceRef.findPosition(endRes);
730 if (sequenceMapping != null)
732 Hashtable newmapping = new Hashtable();
733 Enumeration e = sequenceMapping.keys();
734 while (e.hasMoreElements())
736 Integer pos = (Integer) e.nextElement();
737 if (pos.intValue() >= spos && pos.intValue() <= epos)
739 newmapping.put(pos, sequenceMapping.get(pos));
742 sequenceMapping.clear();
743 sequenceMapping = newmapping;
750 * set the annotation row to be at least length Annotations
753 * minimum number of columns required in the annotation row
754 * @return false if the annotation row is greater than length
756 public boolean padAnnotation(int length)
758 if (annotations == null)
760 return true; // annotation row is correct - null == not visible and
763 if (annotations.length < length)
765 Annotation[] na = new Annotation[length];
766 System.arraycopy(annotations, 0, na, 0, annotations.length);
770 return annotations.length > length;
777 * @return DOCUMENT ME!
779 public String toString()
781 StringBuffer buffer = new StringBuffer();
783 for (int i = 0; i < annotations.length; i++)
785 if (annotations[i] != null)
789 buffer.append(annotations[i].value);
793 buffer.append(annotations[i].secondaryStructure);
797 buffer.append(annotations[i].displayCharacter);
803 // TODO: remove disgusting hack for 'special' treatment of consensus line.
804 if (label.indexOf("Consensus") == 0)
808 for (int i = 0; i < annotations.length; i++)
810 if (annotations[i] != null)
812 buffer.append(annotations[i].description);
819 return buffer.toString();
822 public void setThreshold(GraphLine line)
827 public GraphLine getThreshold()
833 * Attach the annotation to seqRef, starting from startRes position. If
834 * alreadyMapped is true then the indices of the annotation[] array are
835 * sequence positions rather than alignment column positions.
839 * @param alreadyMapped
841 public void createSequenceMapping(SequenceI seqRef, int startRes,
842 boolean alreadyMapped)
849 sequenceRef = seqRef;
850 if (annotations == null)
854 sequenceMapping = new java.util.Hashtable();
858 for (int i = 0; i < annotations.length; i++)
860 if (annotations[i] != null)
864 seqPos = seqRef.findPosition(i);
868 seqPos = i + startRes;
871 sequenceMapping.put(new Integer(seqPos), annotations[i]);
877 public void adjustForAlignment()
879 if (sequenceRef == null)
884 if (annotations == null)
889 int a = 0, aSize = sequenceRef.getLength();
898 Annotation[] temp = new Annotation[aSize];
901 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
903 index = new Integer(a);
904 if (sequenceMapping.containsKey(index))
906 position = sequenceRef.findIndex(a) - 1;
908 temp[position] = sequenceMapping.get(index);
916 * remove any null entries in annotation row and return the number of non-null
917 * annotation elements.
921 public int compactAnnotationArray()
923 int i = 0, iSize = annotations.length;
926 if (annotations[i] == null)
930 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
940 Annotation[] ann = annotations;
941 annotations = new Annotation[i];
942 System.arraycopy(ann, 0, annotations, 0, i);
948 * Associate this annotion with the aligned residues of a particular sequence.
949 * sequenceMapping will be updated in the following way: null sequenceI -
950 * existing mapping will be discarded but annotations left in mapped
951 * positions. valid sequenceI not equal to current sequenceRef: mapping is
952 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
953 * parameter to specify correspondence between current and new sequenceRef
957 public void setSequenceRef(SequenceI sequenceI)
959 if (sequenceI != null)
961 if (sequenceRef != null)
963 boolean rIsDs=sequenceRef.getDatasetSequence()==null,tIsDs=sequenceI.getDatasetSequence()==null;
964 if (sequenceRef != sequenceI
965 && (rIsDs && !tIsDs && sequenceRef != sequenceI
966 .getDatasetSequence())
967 && (!rIsDs && tIsDs && sequenceRef.getDatasetSequence() != sequenceI)
968 && (!rIsDs && !tIsDs && sequenceRef.getDatasetSequence() != sequenceI
969 .getDatasetSequence())
970 && !sequenceRef.equals(sequenceI))
972 // if sequenceRef isn't intersecting with sequenceI
973 // throw away old mapping and reconstruct.
975 if (sequenceMapping != null)
977 sequenceMapping = null;
978 // compactAnnotationArray();
980 createSequenceMapping(sequenceI, 1, true);
981 adjustForAlignment();
985 // Mapping carried over
986 sequenceRef = sequenceI;
992 createSequenceMapping(sequenceI, 1, true);
993 adjustForAlignment();
998 // throw away the mapping without compacting.
999 sequenceMapping = null;
1007 public double getScore()
1016 public void setScore(double score)
1024 * @return true if annotation has an associated score
1026 public boolean hasScore()
1028 return hasScore || !Double.isNaN(score);
1032 * Score only annotation
1035 * @param description
1038 public AlignmentAnnotation(String label, String description, double score)
1040 this(label, description, null);
1045 * copy constructor with edit based on the hidden columns marked in colSel
1047 * @param alignmentAnnotation
1050 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1051 ColumnSelection colSel)
1053 this(alignmentAnnotation);
1054 if (annotations == null)
1058 colSel.makeVisibleAnnotation(this);
1061 public void setPadGaps(boolean padgaps, char gapchar)
1063 this.padGaps = padgaps;
1067 for (int i = 0; i < annotations.length; i++)
1069 if (annotations[i] == null)
1071 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1074 else if (annotations[i].displayCharacter == null
1075 || annotations[i].displayCharacter.equals(" "))
1077 annotations[i].displayCharacter = String.valueOf(gapchar);
1084 * format description string for display
1087 * @return Get the annotation description string optionally prefixed by
1088 * associated sequence name (if any)
1090 public String getDescription(boolean seqname)
1092 if (seqname && this.sequenceRef != null)
1094 int i = description.toLowerCase().indexOf("<html>");
1097 // move the html tag to before the sequence reference.
1098 return "<html>" + sequenceRef.getName() + " : "
1099 + description.substring(i + 6);
1101 return sequenceRef.getName() + " : " + description;
1106 public boolean isValidStruc()
1108 return invalidrnastruc == -1;
1111 public long getInvalidStrucPos()
1113 return invalidrnastruc;
1117 * machine readable ID string indicating what generated this annotation
1119 protected String calcId = "";
1122 * base colour for line graphs. If null, will be set automatically by
1123 * searching the alignment annotation
1125 public java.awt.Color _linecolour;
1127 public String getCalcId()
1132 public void setCalcId(String calcId)
1134 this.calcId = calcId;
1137 public boolean isRNA()
1143 * transfer annotation to the given sequence using the given mapping from the
1144 * current positions or an existing sequence mapping
1148 * map involving sq as To or From
1150 public void liftOver(SequenceI sq, Mapping sp2sq)
1152 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1154 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and Protein reference frames
1155 throw new Error("liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1157 boolean mapIsTo = (sp2sq != null) ? (sp2sq.getTo() == sq || sp2sq
1158 .getTo() == sq.getDatasetSequence()) : false;
1160 // TODO build a better annotation element map and get rid of annotations[]
1161 Hashtable<Integer, Annotation> mapForsq = new Hashtable();
1162 if (sequenceMapping != null)
1166 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1168 Integer mpos = Integer.valueOf(mapIsTo ? sp2sq
1169 .getMappedPosition(ie.getKey()) : sp2sq.getPosition(ie
1171 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1173 mapForsq.put(mpos, ie.getValue());
1176 sequenceMapping = mapForsq;
1178 adjustForAlignment();