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 java.util.Map.Entry;
34 import fr.orsay.lri.varna.models.rna.RNA;
42 public class AlignmentAnnotation
45 * If true, this annotations is calculated every edit, eg consensus, quality
46 * or conservation graphs
48 public boolean autoCalculated = false;
50 public String annotationId;
52 public SequenceI sequenceRef;
58 public String description;
61 public Annotation[] annotations;
63 public ArrayList<SimpleBP> bps = null;
66 * RNA secondary structure contact positions
68 public SequenceFeature[] _rnasecstr = null;
71 * position of annotation resulting in invalid WUSS parsing or -1
73 private long invalidrnastruc = -1;
76 * Updates the _rnasecstr field Determines the positions that base pair and
77 * the positions of helices based on secondary structure from a Stockholm file
81 private void _updateRnaSecStr(CharSequence RNAannot)
85 _rnasecstr = Rna.GetBasePairs(RNAannot);
86 bps = Rna.GetModeleBP(RNAannot);
88 } catch (WUSSParseException px)
90 // DEBUG System.out.println(px);
91 invalidrnastruc = px.getProblemPos();
93 if (invalidrnastruc > -1)
97 Rna.HelixMap(_rnasecstr);
98 // setRNAStruc(RNAannot);
100 if (_rnasecstr != null && _rnasecstr.length > 0)
102 // show all the RNA secondary structure annotation symbols.
104 showAllColLabels = true;
105 scaleColLabel = true;
107 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
110 public java.util.Hashtable<Integer, Annotation> sequenceMapping;
113 public float graphMin;
116 public float graphMax;
119 * Score associated with label and description.
121 public double score = Double.NaN;
124 * flag indicating if annotation has a score.
126 public boolean hasScore = false;
128 public GraphLine threshold;
130 // Graphical hints and tips
132 /** Can this row be edited by the user ? */
133 public boolean editable = false;
135 /** Indicates if annotation has a graphical symbol track */
136 public boolean hasIcons; //
138 /** Indicates if annotation has a text character label */
139 public boolean hasText;
141 /** is the row visible */
142 public boolean visible = true;
144 public int graphGroup = -1;
146 /** Displayed height of row in pixels */
147 public int height = 0;
149 public int graph = 0;
151 public int graphHeight = 40;
153 public boolean padGaps = false;
155 public static final int NO_GRAPH = 0;
157 public static final int BAR_GRAPH = 1;
159 public static final int LINE_GRAPH = 2;
161 public boolean belowAlignment = true;
163 public SequenceGroup groupRef = null;
166 * display every column label, even if there is a row of identical labels
168 public boolean showAllColLabels = false;
171 * scale the column label to fit within the alignment column.
173 public boolean scaleColLabel = false;
176 * centre the column labels relative to the alignment column
178 public boolean centreColLabels = false;
180 private boolean isrna;
185 * @see java.lang.Object#finalize()
187 protected void finalize() throws Throwable
194 public static int getGraphValueFromString(String string)
196 if (string.equalsIgnoreCase("BAR_GRAPH"))
200 else if (string.equalsIgnoreCase("LINE_GRAPH"))
210 // JBPNote: what does this do ?
211 public void ConcenStru(CharSequence RNAannot) throws WUSSParseException
213 bps = Rna.GetModeleBP(RNAannot);
217 * Creates a new AlignmentAnnotation object.
220 * short label shown under sequence labels
222 * text displayed on mouseover
224 * set of positional annotation elements
226 public AlignmentAnnotation(String label, String description,
227 Annotation[] annotations)
232 this.description = description;
233 this.annotations = annotations;
235 validateRangeAndDisplay();
239 * Checks if annotation labels represent secondary structures
242 void areLabelsSecondaryStructure()
244 boolean nonSSLabel = false;
246 StringBuffer rnastring = new StringBuffer();
249 for (int i = 0; i < annotations.length; i++)
251 if (annotations[i] == null)
255 if (annotations[i].secondaryStructure == 'H'
256 || annotations[i].secondaryStructure == 'E')
261 // Check for RNA secondary structure
263 // System.out.println(annotations[i].secondaryStructure);
264 // TODO: 2.8.2 should this ss symbol validation check be a function in RNA/ResidueProperties ?
265 if (annotations[i].secondaryStructure == '('
266 || annotations[i].secondaryStructure == '['
267 || annotations[i].secondaryStructure == '<'
268 || annotations[i].secondaryStructure == '{'
269 || annotations[i].secondaryStructure == 'A'
270 || annotations[i].secondaryStructure == 'B'
271 || annotations[i].secondaryStructure == 'C'
272 || annotations[i].secondaryStructure == 'D'
273 || annotations[i].secondaryStructure == 'E'
274 || annotations[i].secondaryStructure == 'F'
275 || annotations[i].secondaryStructure == 'G'
276 || annotations[i].secondaryStructure == 'H'
277 || annotations[i].secondaryStructure == 'I'
278 || annotations[i].secondaryStructure == 'J'
279 || annotations[i].secondaryStructure == 'K'
280 || annotations[i].secondaryStructure == 'L'
281 || annotations[i].secondaryStructure == 'M'
282 || annotations[i].secondaryStructure == 'N'
283 || annotations[i].secondaryStructure == 'O'
284 || annotations[i].secondaryStructure == 'P'
285 || annotations[i].secondaryStructure == 'Q'
286 || annotations[i].secondaryStructure == 'R'
287 || annotations[i].secondaryStructure == 'S'
288 || annotations[i].secondaryStructure == 'T'
289 || annotations[i].secondaryStructure == 'U'
290 || annotations[i].secondaryStructure == 'V'
291 || annotations[i].secondaryStructure == 'W'
292 || annotations[i].secondaryStructure == 'X'
293 || annotations[i].secondaryStructure == 'Y'
294 || annotations[i].secondaryStructure == 'Z')
301 // System.out.println("displaychar " + annotations[i].displayCharacter);
303 if (annotations[i].displayCharacter == null
304 || annotations[i].displayCharacter.length() == 0)
306 rnastring.append('.');
309 if (annotations[i].displayCharacter.length() == 1)
311 firstChar = annotations[i].displayCharacter.charAt(0);
312 // check to see if it looks like a sequence or is secondary structure
314 if (annotations[i].secondaryStructure != ' '
317 // Uncomment to only catch case where
318 // displayCharacter==secondary
320 // to correctly redisplay SS annotation imported from Stockholm,
321 // exported to JalviewXML and read back in again.
323 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
358 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
360 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
373 rnastring.append(annotations[i].displayCharacter.charAt(1));
376 if (annotations[i].displayCharacter.length() > 0)
385 for (int j = 0; j < annotations.length; j++)
387 if (annotations[j] != null
388 && annotations[j].secondaryStructure != ' ')
390 annotations[j].displayCharacter = String
391 .valueOf(annotations[j].secondaryStructure);
392 annotations[j].secondaryStructure = ' ';
401 _updateRnaSecStr(new AnnotCharSequence());
405 annotationId = this.hashCode() + "";
409 * flyweight access to positions in the alignment annotation row for RNA
415 private class AnnotCharSequence implements CharSequence
421 public AnnotCharSequence()
423 this(0, annotations.length);
426 public AnnotCharSequence(int start, int end)
433 public CharSequence subSequence(int start, int end)
435 return new AnnotCharSequence(offset + start, offset + end);
445 public char charAt(int index)
448 return ((index + offset < 0) || (index + offset) >= max
449 || annotations[index + offset] == null || (dc = annotations[index
450 + offset].displayCharacter.trim()).length() < 1) ? '.' : dc
454 public String toString()
456 char[] string = new char[max - offset];
457 int mx = annotations.length;
459 for (int i = offset; i < mx; i++)
462 string[i] = (annotations[i] == null || (dc = annotations[i].displayCharacter
463 .trim()).length() < 1) ? '.' : dc.charAt(0);
465 return new String(string);
469 private long _lastrnaannot = -1;
471 public String getRNAStruc()
475 String rnastruc = new AnnotCharSequence().toString();
476 if (_lastrnaannot != rnastruc.hashCode())
478 // ensure rna structure contacts are up to date
479 _lastrnaannot = rnastruc.hashCode();
480 _updateRnaSecStr(rnastruc);
488 * Creates a new AlignmentAnnotation object.
503 public AlignmentAnnotation(String label, String description,
504 Annotation[] annotations, float min, float max, int graphType)
506 // graphs are not editable
507 editable = graphType == 0;
510 this.description = description;
511 this.annotations = annotations;
515 validateRangeAndDisplay();
519 * checks graphMin and graphMax, secondary structure symbols, sets graphType
520 * appropriately, sets null labels to the empty string if appropriate.
522 public void validateRangeAndDisplay()
525 if (annotations == null)
527 visible = false; // try to prevent renderer from displaying.
528 return; // this is a non-annotation row annotation - ie a sequence score.
531 int graphType = graph;
532 float min = graphMin;
533 float max = graphMax;
534 boolean drawValues = true;
539 for (int i = 0; i < annotations.length; i++)
541 if (annotations[i] == null)
546 if (drawValues && annotations[i].displayCharacter != null
547 && annotations[i].displayCharacter.length() > 1)
552 if (annotations[i].value > max)
554 max = annotations[i].value;
557 if (annotations[i].value < min)
559 min = annotations[i].value;
561 if (_linecolour == null && annotations[i].colour != null)
563 _linecolour = annotations[i].colour;
566 // ensure zero is origin for min/max ranges on only one side of zero
583 areLabelsSecondaryStructure();
585 if (!drawValues && graphType != NO_GRAPH)
587 for (int i = 0; i < annotations.length; i++)
589 if (annotations[i] != null)
591 annotations[i].displayCharacter = "X";
598 * Copy constructor creates a new independent annotation row with the same
599 * associated sequenceRef
603 public AlignmentAnnotation(AlignmentAnnotation annotation)
605 this.label = new String(annotation.label);
606 if (annotation.description != null)
607 this.description = new String(annotation.description);
608 this.graphMin = annotation.graphMin;
609 this.graphMax = annotation.graphMax;
610 this.graph = annotation.graph;
611 this.graphHeight = annotation.graphHeight;
612 this.graphGroup = annotation.graphGroup;
613 this.groupRef = annotation.groupRef;
614 this.editable = annotation.editable;
615 this.autoCalculated = annotation.autoCalculated;
616 this.hasIcons = annotation.hasIcons;
617 this.hasText = annotation.hasText;
618 this.height = annotation.height;
619 this.label = annotation.label;
620 this.padGaps = annotation.padGaps;
621 this.visible = annotation.visible;
622 this.centreColLabels = annotation.centreColLabels;
623 this.scaleColLabel = annotation.scaleColLabel;
624 this.showAllColLabels = annotation.showAllColLabels;
625 this.calcId = annotation.calcId;
626 if (this.hasScore = annotation.hasScore)
628 this.score = annotation.score;
630 if (annotation.threshold != null)
632 threshold = new GraphLine(annotation.threshold);
634 if (annotation.annotations != null)
636 Annotation[] ann = annotation.annotations;
637 this.annotations = new Annotation[ann.length];
638 for (int i = 0; i < ann.length; i++)
642 annotations[i] = new Annotation(ann[i]);
643 if (_linecolour != null)
645 _linecolour = annotations[i].colour;
650 if (annotation.sequenceRef != null)
652 this.sequenceRef = annotation.sequenceRef;
653 if (annotation.sequenceMapping != null)
656 sequenceMapping = new Hashtable();
657 Enumeration pos = annotation.sequenceMapping.keys();
658 while (pos.hasMoreElements())
660 // could optimise this!
661 p = (Integer) pos.nextElement();
662 Annotation a = (Annotation) annotation.sequenceMapping.get(p);
667 for (int i = 0; i < ann.length; i++)
671 sequenceMapping.put(p, annotations[i]);
678 this.sequenceMapping = null;
682 // TODO: check if we need to do this: JAL-952
683 // if (this.isrna=annotation.isrna)
685 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
687 validateRangeAndDisplay(); // construct hashcodes, etc.
691 * clip the annotation to the columns given by startRes and endRes (inclusive)
692 * and prune any existing sequenceMapping to just those columns.
697 public void restrict(int startRes, int endRes)
699 if (annotations == null)
706 if (startRes >= annotations.length)
707 startRes = annotations.length - 1;
708 if (endRes >= annotations.length)
709 endRes = annotations.length - 1;
710 if (annotations == null)
712 Annotation[] temp = new Annotation[endRes - startRes + 1];
713 if (startRes < annotations.length)
715 System.arraycopy(annotations, startRes, temp, 0, endRes - startRes
718 if (sequenceRef != null)
720 // Clip the mapping, if it exists.
721 int spos = sequenceRef.findPosition(startRes);
722 int epos = sequenceRef.findPosition(endRes);
723 if (sequenceMapping != null)
725 Hashtable newmapping = new Hashtable();
726 Enumeration e = sequenceMapping.keys();
727 while (e.hasMoreElements())
729 Integer pos = (Integer) e.nextElement();
730 if (pos.intValue() >= spos && pos.intValue() <= epos)
732 newmapping.put(pos, sequenceMapping.get(pos));
735 sequenceMapping.clear();
736 sequenceMapping = newmapping;
743 * set the annotation row to be at least length Annotations
746 * minimum number of columns required in the annotation row
747 * @return false if the annotation row is greater than length
749 public boolean padAnnotation(int length)
751 if (annotations == null)
753 return true; // annotation row is correct - null == not visible and
756 if (annotations.length < length)
758 Annotation[] na = new Annotation[length];
759 System.arraycopy(annotations, 0, na, 0, annotations.length);
763 return annotations.length > length;
770 * @return DOCUMENT ME!
772 public String toString()
774 StringBuffer buffer = new StringBuffer();
776 for (int i = 0; i < annotations.length; i++)
778 if (annotations[i] != null)
782 buffer.append(annotations[i].value);
786 buffer.append(annotations[i].secondaryStructure);
790 buffer.append(annotations[i].displayCharacter);
796 // TODO: remove disgusting hack for 'special' treatment of consensus line.
797 if (label.indexOf("Consensus") == 0)
801 for (int i = 0; i < annotations.length; i++)
803 if (annotations[i] != null)
805 buffer.append(annotations[i].description);
812 return buffer.toString();
815 public void setThreshold(GraphLine line)
820 public GraphLine getThreshold()
826 * Attach the annotation to seqRef, starting from startRes position. If
827 * alreadyMapped is true then the indices of the annotation[] array are
828 * sequence positions rather than alignment column positions.
832 * @param alreadyMapped
834 public void createSequenceMapping(SequenceI seqRef, int startRes,
835 boolean alreadyMapped)
842 sequenceRef = seqRef;
843 if (annotations == null)
847 sequenceMapping = new java.util.Hashtable();
851 for (int i = 0; i < annotations.length; i++)
853 if (annotations[i] != null)
857 seqPos = seqRef.findPosition(i);
861 seqPos = i + startRes;
864 sequenceMapping.put(new Integer(seqPos), annotations[i]);
870 public void adjustForAlignment()
872 if (sequenceRef == null)
875 if (annotations == null)
880 int a = 0, aSize = sequenceRef.getLength();
889 Annotation[] temp = new Annotation[aSize];
892 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
894 index = new Integer(a);
895 if (sequenceMapping.containsKey(index))
897 position = sequenceRef.findIndex(a) - 1;
899 temp[position] = (Annotation) sequenceMapping.get(index);
907 * remove any null entries in annotation row and return the number of non-null
908 * annotation elements.
912 public int compactAnnotationArray()
914 int i = 0, iSize = annotations.length;
917 if (annotations[i] == null)
920 System.arraycopy(annotations, i + 1, annotations, i, iSize - i
929 Annotation[] ann = annotations;
930 annotations = new Annotation[i];
931 System.arraycopy(ann, 0, annotations, 0, i);
937 * Associate this annotion with the aligned residues of a particular sequence.
938 * sequenceMapping will be updated in the following way: null sequenceI -
939 * existing mapping will be discarded but annotations left in mapped
940 * positions. valid sequenceI not equal to current sequenceRef: mapping is
941 * discarded and rebuilt assuming 1:1 correspondence TODO: overload with
942 * parameter to specify correspondence between current and new sequenceRef
946 public void setSequenceRef(SequenceI sequenceI)
948 if (sequenceI != null)
950 if (sequenceRef != null)
952 boolean rIsDs=sequenceRef.getDatasetSequence()==null,tIsDs=sequenceI.getDatasetSequence()==null;
953 if (sequenceRef != sequenceI
954 && (rIsDs && !tIsDs && sequenceRef != sequenceI
955 .getDatasetSequence())
956 && (!rIsDs && tIsDs && sequenceRef.getDatasetSequence() != sequenceI)
957 && (!rIsDs && !tIsDs && sequenceRef.getDatasetSequence() != sequenceI
958 .getDatasetSequence())
959 && !sequenceRef.equals(sequenceI))
961 // if sequenceRef isn't intersecting with sequenceI
962 // throw away old mapping and reconstruct.
964 if (sequenceMapping != null)
966 sequenceMapping = null;
967 // compactAnnotationArray();
969 createSequenceMapping(sequenceI, 1, true);
970 adjustForAlignment();
974 // Mapping carried over
975 sequenceRef = sequenceI;
981 createSequenceMapping(sequenceI, 1, true);
982 adjustForAlignment();
987 // throw away the mapping without compacting.
988 sequenceMapping = null;
996 public double getScore()
1005 public void setScore(double score)
1013 * @return true if annotation has an associated score
1015 public boolean hasScore()
1017 return hasScore || !Double.isNaN(score);
1021 * Score only annotation
1024 * @param description
1027 public AlignmentAnnotation(String label, String description, double score)
1029 this(label, description, null);
1034 * copy constructor with edit based on the hidden columns marked in colSel
1036 * @param alignmentAnnotation
1039 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1040 ColumnSelection colSel)
1042 this(alignmentAnnotation);
1043 if (annotations == null)
1047 colSel.makeVisibleAnnotation(this);
1050 public void setPadGaps(boolean padgaps, char gapchar)
1052 this.padGaps = padgaps;
1056 for (int i = 0; i < annotations.length; i++)
1058 if (annotations[i] == null)
1059 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1061 else if (annotations[i].displayCharacter == null
1062 || annotations[i].displayCharacter.equals(" "))
1063 annotations[i].displayCharacter = String.valueOf(gapchar);
1069 * format description string for display
1072 * @return Get the annotation description string optionally prefixed by
1073 * associated sequence name (if any)
1075 public String getDescription(boolean seqname)
1077 if (seqname && this.sequenceRef != null)
1079 int i = description.toLowerCase().indexOf("<html>");
1082 // move the html tag to before the sequence reference.
1083 return "<html>" + sequenceRef.getName() + " : "
1084 + description.substring(i + 6);
1086 return sequenceRef.getName() + " : " + description;
1091 public boolean isValidStruc()
1093 return invalidrnastruc == -1;
1096 public long getInvalidStrucPos()
1098 return invalidrnastruc;
1102 * machine readable ID string indicating what generated this annotation
1104 protected String calcId = "";
1107 * base colour for line graphs. If null, will be set automatically by
1108 * searching the alignment annotation
1110 public java.awt.Color _linecolour;
1112 public String getCalcId()
1117 public void setCalcId(String calcId)
1119 this.calcId = calcId;
1122 public boolean isRNA()
1128 * transfer annotation to the given sequence using the given mapping from the
1129 * current positions or an existing sequence mapping
1133 * map involving sq as To or From
1135 public void liftOver(SequenceI sq, Mapping sp2sq)
1137 boolean mapIsTo = (sp2sq != null) ? (sp2sq.getTo() == sq || sp2sq
1138 .getTo() == sq.getDatasetSequence()) : false;
1140 // TODO build a better annotation element map and get rid of annotations[]
1141 Hashtable<Integer, Annotation> mapForsq = new Hashtable();
1142 if (sequenceMapping != null)
1146 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1148 Integer mpos = Integer.valueOf(mapIsTo ? sp2sq
1149 .getMappedPosition(ie.getKey()) : sp2sq.getPosition(ie
1151 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1153 mapForsq.put(mpos, ie.getValue());
1156 sequenceMapping = mapForsq;
1158 adjustForAlignment();