2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.Map.Entry;
43 public class AlignmentAnnotation
45 private static final String ANNOTATION_ID_PREFIX = "ann";
48 * Identifers for different types of profile data
50 public static final int SEQUENCE_PROFILE = 0;
52 public static final int STRUCTURE_PROFILE = 1;
54 public static final int CDNA_PROFILE = 2;
56 private static long counter = 0;
59 * If true, this annotations is calculated every edit, eg consensus, quality
60 * or conservation graphs
62 public boolean autoCalculated = false;
65 * unique ID for this annotation, used to match up the same annotation row
66 * shown in multiple views and alignments
68 public String annotationId;
71 * the sequence this annotation is associated with (or null)
73 public SequenceI sequenceRef;
75 /** label shown in dropdown menus and in the annotation label area */
78 /** longer description text shown as a tooltip */
79 public String description;
81 /** Array of annotations placed in the current coordinate system */
82 public Annotation[] annotations;
84 public List<SimpleBP> bps = null;
87 * RNA secondary structure contact positions
89 public SequenceFeature[] _rnasecstr = null;
92 * position of annotation resulting in invalid WUSS parsing or -1. -2 means
93 * there was no RNA structure in this annotation
95 private long invalidrnastruc = -2;
97 private double bitScore;
99 private double eValue;
102 * Updates the _rnasecstr field Determines the positions that base pair and
103 * the positions of helices based on secondary structure from a Stockholm file
105 * @param rnaAnnotation
107 private void _updateRnaSecStr(CharSequence rnaAnnotation)
111 _rnasecstr = Rna.getHelixMap(rnaAnnotation);
112 invalidrnastruc = -1;
113 } catch (WUSSParseException px)
115 // DEBUG System.out.println(px);
116 invalidrnastruc = px.getProblemPos();
118 if (invalidrnastruc > -1)
123 if (_rnasecstr != null && _rnasecstr.length > 0)
125 // show all the RNA secondary structure annotation symbols.
127 showAllColLabels = true;
128 scaleColLabel = true;
131 // System.out.println("featuregroup " + _rnasecstr[0].getFeatureGroup());
135 private void _markRnaHelices()
138 // Figure out number of helices
139 // Length of rnasecstr is the number of pairs of positions that base pair
140 // with each other in the secondary structure
141 for (int x = 0; x < _rnasecstr.length; x++)
145 * System.out.println(this.annotation._rnasecstr[x] + " Begin" +
146 * this.annotation._rnasecstr[x].getBegin());
148 // System.out.println(this.annotation._rnasecstr[x].getFeatureGroup());
152 val = Integer.valueOf(_rnasecstr[x].getFeatureGroup());
157 } catch (NumberFormatException q)
162 annotations[_rnasecstr[x].getBegin()].value = val;
163 annotations[_rnasecstr[x].getEnd()].value = val;
165 // annotations[_rnasecstr[x].getBegin()].displayCharacter = "" + val;
166 // annotations[_rnasecstr[x].getEnd()].displayCharacter = "" + val;
172 * Get the RNA Secondary Structure SequenceFeature Array if present
174 public SequenceFeature[] getRnaSecondaryStructure()
176 return this._rnasecstr;
180 * Check the RNA Secondary Structure is equivalent to one in given
181 * AlignmentAnnotation param
183 public boolean rnaSecondaryStructureEquivalent(AlignmentAnnotation that)
185 return rnaSecondaryStructureEquivalent(that, true);
188 public boolean rnaSecondaryStructureEquivalent(AlignmentAnnotation that, boolean compareType)
190 SequenceFeature[] thisSfArray = this.getRnaSecondaryStructure();
191 SequenceFeature[] thatSfArray = that.getRnaSecondaryStructure();
192 if (thisSfArray == null || thatSfArray == null)
194 return thisSfArray == null && thatSfArray == null;
196 if (thisSfArray.length != thatSfArray.length)
200 Arrays.sort(thisSfArray, new SFSortByEnd()); // probably already sorted
202 Arrays.sort(thatSfArray, new SFSortByEnd()); // probably already sorted
204 for (int i=0; i < thisSfArray.length; i++) {
205 SequenceFeature thisSf = thisSfArray[i];
206 SequenceFeature thatSf = thatSfArray[i];
208 if (thisSf.getType() == null || thatSf.getType() == null) {
209 if (thisSf.getType() == null && thatSf.getType() == null) {
215 if (! thisSf.getType().equals(thatSf.getType())) {
219 if (!(thisSf.getBegin() == thatSf.getBegin()
220 && thisSf.getEnd() == thatSf.getEnd()))
230 * map of positions in the associated annotation
232 private Map<Integer, Annotation> sequenceMapping;
235 * lower range for quantitative data
237 public float graphMin;
240 * Upper range for quantitative data
242 public float graphMax;
245 * Score associated with label and description.
247 public double score = Double.NaN;
250 * flag indicating if annotation has a score.
252 public boolean hasScore = false;
254 public GraphLine threshold;
256 // Graphical hints and tips
258 /** Can this row be edited by the user ? */
259 public boolean editable = false;
261 /** Indicates if annotation has a graphical symbol track */
262 public boolean hasIcons; //
264 /** Indicates if annotation has a text character label */
265 public boolean hasText;
267 /** is the row visible */
268 public boolean visible = true;
270 public int graphGroup = -1;
272 /** Displayed height of row in pixels */
273 public int height = 0;
275 public int graph = 0;
277 public int graphHeight = 40;
279 public boolean padGaps = false;
281 public static final int NO_GRAPH = 0;
283 public static final int BAR_GRAPH = 1;
285 public static final int LINE_GRAPH = 2;
287 public boolean belowAlignment = true;
289 public SequenceGroup groupRef = null;
292 * display every column label, even if there is a row of identical labels
294 public boolean showAllColLabels = false;
297 * scale the column label to fit within the alignment column.
299 public boolean scaleColLabel = false;
302 * centre the column labels relative to the alignment column
304 public boolean centreColLabels = false;
306 private boolean isrna;
308 public static int getGraphValueFromString(String string)
310 if (string.equalsIgnoreCase("BAR_GRAPH"))
314 else if (string.equalsIgnoreCase("LINE_GRAPH"))
325 * Creates a new AlignmentAnnotation object.
328 * short label shown under sequence labels
330 * text displayed on mouseover
332 * set of positional annotation elements
334 public AlignmentAnnotation(String label, String description,
335 Annotation[] annotations)
341 this.description = description;
342 this.annotations = annotations;
344 validateRangeAndDisplay();
348 * Checks if annotation labels represent secondary structures
351 void areLabelsSecondaryStructure()
353 boolean nonSSLabel = false;
355 StringBuffer rnastring = new StringBuffer();
358 for (int i = 0; i < annotations.length; i++)
360 // DEBUG System.out.println(i + ": " + annotations[i]);
361 if (annotations[i] == null)
365 if (annotations[i].secondaryStructure == 'H'
366 || annotations[i].secondaryStructure == 'E')
368 // DEBUG System.out.println( "/H|E/ '" +
369 // annotations[i].secondaryStructure + "'");
373 // Check for RNA secondary structure
375 // DEBUG System.out.println( "/else/ '" +
376 // annotations[i].secondaryStructure + "'");
377 // TODO: 2.8.2 should this ss symbol validation check be a function in
378 // RNA/ResidueProperties ?
379 if (annotations[i].secondaryStructure == '('
380 || annotations[i].secondaryStructure == '['
381 || annotations[i].secondaryStructure == '<'
382 || annotations[i].secondaryStructure == '{'
383 || annotations[i].secondaryStructure == 'A'
384 || annotations[i].secondaryStructure == 'B'
385 || annotations[i].secondaryStructure == 'C'
386 || annotations[i].secondaryStructure == 'D'
387 // || annotations[i].secondaryStructure == 'E' // ambiguous on
388 // its own -- already checked above
389 || annotations[i].secondaryStructure == 'F'
390 || annotations[i].secondaryStructure == 'G'
391 // || annotations[i].secondaryStructure == 'H' // ambiguous on
392 // its own -- already checked above
393 || annotations[i].secondaryStructure == 'I'
394 || annotations[i].secondaryStructure == 'J'
395 || annotations[i].secondaryStructure == 'K'
396 || annotations[i].secondaryStructure == 'L'
397 || annotations[i].secondaryStructure == 'M'
398 || annotations[i].secondaryStructure == 'N'
399 || annotations[i].secondaryStructure == 'O'
400 || annotations[i].secondaryStructure == 'P'
401 || annotations[i].secondaryStructure == 'Q'
402 || annotations[i].secondaryStructure == 'R'
403 || annotations[i].secondaryStructure == 'S'
404 || annotations[i].secondaryStructure == 'T'
405 || annotations[i].secondaryStructure == 'U'
406 || annotations[i].secondaryStructure == 'V'
407 || annotations[i].secondaryStructure == 'W'
408 || annotations[i].secondaryStructure == 'X'
409 || annotations[i].secondaryStructure == 'Y'
410 || annotations[i].secondaryStructure == 'Z')
417 // System.out.println("displaychar " + annotations[i].displayCharacter);
419 if (annotations[i].displayCharacter == null
420 || annotations[i].displayCharacter.length() == 0)
422 rnastring.append('.');
425 if (annotations[i].displayCharacter.length() == 1)
427 firstChar = annotations[i].displayCharacter.charAt(0);
428 // check to see if it looks like a sequence or is secondary structure
430 if (annotations[i].secondaryStructure != ' ' && !hasIcons &&
431 // Uncomment to only catch case where
432 // displayCharacter==secondary
434 // to correctly redisplay SS annotation imported from Stockholm,
435 // exported to JalviewXML and read back in again.
437 // annotations[i].displayCharacter.charAt(0)==annotations[i].secondaryStructure
438 firstChar != ' ' && firstChar != '$' && firstChar != 0xCE
439 && firstChar != '(' && firstChar != '[' && firstChar != '<'
440 && firstChar != '{' && firstChar != 'A' && firstChar != 'B'
441 && firstChar != 'C' && firstChar != 'D' && firstChar != 'E'
442 && firstChar != 'F' && firstChar != 'G' && firstChar != 'H'
443 && firstChar != 'I' && firstChar != 'J' && firstChar != 'K'
444 && firstChar != 'L' && firstChar != 'M' && firstChar != 'N'
445 && firstChar != 'O' && firstChar != 'P' && firstChar != 'Q'
446 && firstChar != 'R' && firstChar != 'S' && firstChar != 'T'
447 && firstChar != 'U' && firstChar != 'V' && firstChar != 'W'
448 && firstChar != 'X' && firstChar != 'Y' && firstChar != 'Z'
450 && firstChar < jalview.schemes.ResidueProperties.aaIndex.length)
452 if (jalview.schemes.ResidueProperties.aaIndex[firstChar] < 23) // TODO:
465 rnastring.append(annotations[i].displayCharacter.charAt(1));
468 if (annotations[i].displayCharacter.length() > 0)
477 for (int j = 0; j < annotations.length; j++)
479 if (annotations[j] != null
480 && annotations[j].secondaryStructure != ' ')
482 annotations[j].displayCharacter = String
483 .valueOf(annotations[j].secondaryStructure);
484 annotations[j].secondaryStructure = ' ';
493 _updateRnaSecStr(new AnnotCharSequence());
499 * flyweight access to positions in the alignment annotation row for RNA
505 private class AnnotCharSequence implements CharSequence
511 public AnnotCharSequence()
513 this(0, annotations.length);
516 AnnotCharSequence(int start, int end)
523 public CharSequence subSequence(int start, int end)
525 return new AnnotCharSequence(offset + start, offset + end);
535 public char charAt(int index)
537 return ((index + offset < 0) || (index + offset) >= max
538 || annotations[index + offset] == null
539 || (annotations[index + offset].secondaryStructure <= ' ')
541 : annotations[index + offset].displayCharacter == null
543 + offset].displayCharacter
546 + offset].secondaryStructure
548 + offset].displayCharacter
553 public String toString()
555 char[] string = new char[max - offset];
556 int mx = annotations.length;
558 for (int i = offset; i < mx; i++)
560 string[i] = (annotations[i] == null
561 || (annotations[i].secondaryStructure <= 32))
563 : (annotations[i].displayCharacter == null
564 || annotations[i].displayCharacter
566 ? annotations[i].secondaryStructure
567 : annotations[i].displayCharacter
570 return new String(string);
574 private long _lastrnaannot = -1;
576 public String getRNAStruc()
580 String rnastruc = new AnnotCharSequence().toString();
581 if (_lastrnaannot != rnastruc.hashCode())
583 // ensure rna structure contacts are up to date
584 _lastrnaannot = rnastruc.hashCode();
585 _updateRnaSecStr(rnastruc);
593 * Creates a new AlignmentAnnotation object.
608 public AlignmentAnnotation(String label, String description,
609 Annotation[] annotations, float min, float max, int graphType)
612 // graphs are not editable
613 editable = graphType == 0;
616 this.description = description;
617 this.annotations = annotations;
621 validateRangeAndDisplay();
625 * checks graphMin and graphMax, secondary structure symbols, sets graphType
626 * appropriately, sets null labels to the empty string if appropriate.
628 public void validateRangeAndDisplay()
631 if (annotations == null)
633 visible = false; // try to prevent renderer from displaying.
634 invalidrnastruc = -1;
635 return; // this is a non-annotation row annotation - ie a sequence score.
638 int graphType = graph;
639 float min = graphMin;
640 float max = graphMax;
641 boolean drawValues = true;
646 for (int i = 0; i < annotations.length; i++)
648 if (annotations[i] == null)
653 if (drawValues && annotations[i].displayCharacter != null
654 && annotations[i].displayCharacter.length() > 1)
659 if (annotations[i].value > max)
661 max = annotations[i].value;
664 if (annotations[i].value < min)
666 min = annotations[i].value;
668 if (_linecolour == null && annotations[i].colour != null)
670 _linecolour = annotations[i].colour;
673 // ensure zero is origin for min/max ranges on only one side of zero
690 areLabelsSecondaryStructure();
692 if (!drawValues && graphType != NO_GRAPH)
694 for (int i = 0; i < annotations.length; i++)
696 if (annotations[i] != null)
698 annotations[i].displayCharacter = "";
705 * Copy constructor creates a new independent annotation row with the same
706 * associated sequenceRef
710 public AlignmentAnnotation(AlignmentAnnotation annotation)
713 this.label = new String(annotation.label);
714 if (annotation.description != null)
716 this.description = new String(annotation.description);
718 this.graphMin = annotation.graphMin;
719 this.graphMax = annotation.graphMax;
720 this.graph = annotation.graph;
721 this.graphHeight = annotation.graphHeight;
722 this.graphGroup = annotation.graphGroup;
723 this.groupRef = annotation.groupRef;
724 this.editable = annotation.editable;
725 this.autoCalculated = annotation.autoCalculated;
726 this.hasIcons = annotation.hasIcons;
727 this.hasText = annotation.hasText;
728 this.height = annotation.height;
729 this.label = annotation.label;
730 this.padGaps = annotation.padGaps;
731 this.visible = annotation.visible;
732 this.centreColLabels = annotation.centreColLabels;
733 this.scaleColLabel = annotation.scaleColLabel;
734 this.showAllColLabels = annotation.showAllColLabels;
735 this.calcId = annotation.calcId;
736 this.bitScore = annotation.bitScore;
737 this.eValue = annotation.eValue;
739 if (annotation.properties != null)
741 properties = new HashMap<>();
742 for (Map.Entry<String, String> val : annotation.properties.entrySet())
744 properties.put(val.getKey(), val.getValue());
747 if (this.hasScore = annotation.hasScore)
749 this.score = annotation.score;
751 if (annotation.threshold != null)
753 threshold = new GraphLine(annotation.threshold);
755 Annotation[] ann = annotation.annotations;
756 if (annotation.annotations != null)
758 this.annotations = new Annotation[ann.length];
759 for (int i = 0; i < ann.length; i++)
763 annotations[i] = new Annotation(ann[i]);
764 if (_linecolour != null)
766 _linecolour = annotations[i].colour;
771 if (annotation.sequenceRef != null)
773 this.sequenceRef = annotation.sequenceRef;
774 if (annotation.sequenceMapping != null)
777 sequenceMapping = new HashMap<>();
778 Iterator<Integer> pos = annotation.sequenceMapping.keySet()
780 while (pos.hasNext())
782 // could optimise this!
784 Annotation a = annotation.sequenceMapping.get(p);
791 for (int i = 0; i < ann.length; i++)
795 sequenceMapping.put(p, annotations[i]);
803 this.sequenceMapping = null;
806 // TODO: check if we need to do this: JAL-952
807 // if (this.isrna=annotation.isrna)
809 // _rnasecstr=new SequenceFeature[annotation._rnasecstr];
811 validateRangeAndDisplay(); // construct hashcodes, etc.
815 * clip the annotation to the columns given by startRes and endRes (inclusive)
816 * and prune any existing sequenceMapping to just those columns.
821 public void restrict(int startRes, int endRes)
823 if (annotations == null)
832 if (startRes >= annotations.length)
834 startRes = annotations.length - 1;
836 if (endRes >= annotations.length)
838 endRes = annotations.length - 1;
840 if (annotations == null)
844 Annotation[] temp = new Annotation[endRes - startRes + 1];
845 if (startRes < annotations.length)
847 System.arraycopy(annotations, startRes, temp, 0,
848 endRes - startRes + 1);
850 if (sequenceRef != null)
852 // Clip the mapping, if it exists.
853 int spos = sequenceRef.findPosition(startRes);
854 int epos = sequenceRef.findPosition(endRes);
855 if (sequenceMapping != null)
857 Map<Integer, Annotation> newmapping = new HashMap<>();
858 Iterator<Integer> e = sequenceMapping.keySet().iterator();
861 Integer pos = e.next();
862 if (pos.intValue() >= spos && pos.intValue() <= epos)
864 newmapping.put(pos, sequenceMapping.get(pos));
867 sequenceMapping.clear();
868 sequenceMapping = newmapping;
875 * set the annotation row to be at least length Annotations
878 * minimum number of columns required in the annotation row
879 * @return false if the annotation row is greater than length
881 public boolean padAnnotation(int length)
883 if (annotations == null)
885 return true; // annotation row is correct - null == not visible and
888 if (annotations.length < length)
890 Annotation[] na = new Annotation[length];
891 System.arraycopy(annotations, 0, na, 0, annotations.length);
895 return annotations.length > length;
902 * @return DOCUMENT ME!
905 public String toString()
907 if (annotations == null)
911 StringBuilder buffer = new StringBuilder(256);
913 for (int i = 0; i < annotations.length; i++)
915 if (annotations[i] != null)
919 buffer.append(annotations[i].value);
923 buffer.append(annotations[i].secondaryStructure);
927 buffer.append(annotations[i].displayCharacter);
933 // TODO: remove disgusting hack for 'special' treatment of consensus line.
934 if (label.indexOf("Consensus") == 0)
938 for (int i = 0; i < annotations.length; i++)
940 if (annotations[i] != null)
942 buffer.append(annotations[i].description);
949 return buffer.toString();
952 public void setThreshold(GraphLine line)
957 public GraphLine getThreshold()
963 * Attach the annotation to seqRef, starting from startRes position. If
964 * alreadyMapped is true then the indices of the annotation[] array are
965 * sequence positions rather than alignment column positions.
969 * @param alreadyMapped
970 * - annotation are at aligned columns
972 public void createSequenceMapping(SequenceI seqRef, int startRes,
973 boolean alreadyMapped)
980 sequenceRef = seqRef;
981 if (annotations == null)
985 sequenceMapping = new HashMap<>();
989 for (int i = 0; i < annotations.length; i++)
991 if (annotations[i] != null)
995 seqPos = seqRef.findPosition(i);
999 seqPos = i + startRes;
1002 sequenceMapping.put(Integer.valueOf(seqPos), annotations[i]);
1009 * When positional annotation and a sequence reference is present, clears and
1010 * resizes the annotations array to the current alignment width, and adds
1011 * annotation according to aligned positions of the sequenceRef given by
1014 public void adjustForAlignment()
1016 if (sequenceRef == null)
1021 if (annotations == null)
1026 int a = 0, aSize = sequenceRef.getLength();
1035 Annotation[] temp = new Annotation[aSize];
1037 if (sequenceMapping != null)
1039 for (a = sequenceRef.getStart(); a <= sequenceRef.getEnd(); a++)
1041 index = Integer.valueOf(a);
1042 Annotation annot = sequenceMapping.get(index);
1045 position = sequenceRef.findIndex(a) - 1;
1047 temp[position] = annot;
1055 * remove any null entries in annotation row and return the number of non-null
1056 * annotation elements.
1060 public int compactAnnotationArray()
1062 int i = 0, iSize = annotations.length;
1065 if (annotations[i] == null)
1069 System.arraycopy(annotations, i + 1, annotations, i,
1079 Annotation[] ann = annotations;
1080 annotations = new Annotation[i];
1081 System.arraycopy(ann, 0, annotations, 0, i);
1087 * Associate this annotation with the aligned residues of a particular
1088 * sequence. sequenceMapping will be updated in the following way: null
1089 * sequenceI - existing mapping will be discarded but annotations left in
1090 * mapped positions. valid sequenceI not equal to current sequenceRef: mapping
1091 * is discarded and rebuilt assuming 1:1 correspondence TODO: overload with
1092 * parameter to specify correspondence between current and new sequenceRef
1096 public void setSequenceRef(SequenceI sequenceI)
1098 if (sequenceI != null)
1100 if (sequenceRef != null)
1102 boolean rIsDs = sequenceRef.getDatasetSequence() == null,
1103 tIsDs = sequenceI.getDatasetSequence() == null;
1104 if (sequenceRef != sequenceI
1106 && sequenceRef != sequenceI.getDatasetSequence())
1108 && sequenceRef.getDatasetSequence() != sequenceI)
1109 && (!rIsDs && !tIsDs
1110 && sequenceRef.getDatasetSequence() != sequenceI
1111 .getDatasetSequence())
1112 && !sequenceRef.equals(sequenceI))
1114 // if sequenceRef isn't intersecting with sequenceI
1115 // throw away old mapping and reconstruct.
1117 if (sequenceMapping != null)
1119 sequenceMapping = null;
1120 // compactAnnotationArray();
1122 createSequenceMapping(sequenceI, 1, true);
1123 adjustForAlignment();
1127 // Mapping carried over
1128 sequenceRef = sequenceI;
1133 // No mapping exists
1134 createSequenceMapping(sequenceI, 1, true);
1135 adjustForAlignment();
1140 // throw away the mapping without compacting.
1141 sequenceMapping = null;
1149 public double getScore()
1158 public void setScore(double score)
1166 * @return true if annotation has an associated score
1168 public boolean hasScore()
1170 return hasScore || !Double.isNaN(score);
1174 * Score only annotation
1177 * @param description
1180 public AlignmentAnnotation(String label, String description, double score)
1182 this(label, description, null);
1187 * copy constructor with edit based on the hidden columns marked in colSel
1189 * @param alignmentAnnotation
1192 public AlignmentAnnotation(AlignmentAnnotation alignmentAnnotation,
1193 HiddenColumns hidden)
1195 this(alignmentAnnotation);
1196 if (annotations == null)
1200 makeVisibleAnnotation(hidden);
1204 public void setPadGaps(boolean padgaps, char gapchar)
1206 this.padGaps = padgaps;
1210 for (int i = 0; i < annotations.length; i++)
1212 if (annotations[i] == null)
1214 annotations[i] = new Annotation(String.valueOf(gapchar), null,
1217 else if (annotations[i].displayCharacter == null
1218 || annotations[i].displayCharacter.equals(" "))
1220 annotations[i].displayCharacter = String.valueOf(gapchar);
1227 * format description string for display
1230 * @return Get the annotation description string optionally prefixed by
1231 * associated sequence name (if any)
1233 public String getDescription(boolean seqname)
1235 if (seqname && this.sequenceRef != null)
1237 int i = description.toLowerCase().indexOf("<html>");
1240 // move the html tag to before the sequence reference.
1241 return "<html>" + sequenceRef.getName() + " : "
1242 + description.substring(i + 6);
1244 return sequenceRef.getName() + " : " + description;
1249 public boolean isValidStruc()
1251 return invalidrnastruc == -1;
1254 public long getInvalidStrucPos()
1256 return invalidrnastruc;
1260 * machine readable ID string indicating what generated this annotation
1262 protected String calcId = "";
1265 * properties associated with the calcId
1267 protected Map<String, String> properties = new HashMap<>();
1270 * base colour for line graphs. If null, will be set automatically by
1271 * searching the alignment annotation
1273 public java.awt.Color _linecolour;
1275 public String getCalcId()
1280 public void setCalcId(String calcId)
1282 this.calcId = calcId;
1285 public boolean isRNA()
1291 * transfer annotation to the given sequence using the given mapping from the
1292 * current positions or an existing sequence mapping
1296 * map involving sq as To or From
1298 public void liftOver(SequenceI sq, Mapping sp2sq)
1300 if (sp2sq.getMappedWidth() != sp2sq.getWidth())
1302 // TODO: employ getWord/MappedWord to transfer annotation between cDNA and
1303 // Protein reference frames
1305 "liftOver currently not implemented for transfer of annotation between different types of seqeunce");
1307 boolean mapIsTo = (sp2sq != null)
1308 ? (sp2sq.getTo() == sq
1309 || sp2sq.getTo() == sq.getDatasetSequence())
1312 // TODO build a better annotation element map and get rid of annotations[]
1313 Map<Integer, Annotation> mapForsq = new HashMap<>();
1314 if (sequenceMapping != null)
1318 for (Entry<Integer, Annotation> ie : sequenceMapping.entrySet())
1320 Integer mpos = Integer
1321 .valueOf(mapIsTo ? sp2sq.getMappedPosition(ie.getKey())
1322 : sp2sq.getPosition(ie.getKey()));
1323 if (mpos >= sq.getStart() && mpos <= sq.getEnd())
1325 mapForsq.put(mpos, ie.getValue());
1328 sequenceMapping = mapForsq;
1330 adjustForAlignment();
1340 * like liftOver but more general.
1342 * Takes an array of int pairs that will be used to update the internal
1343 * sequenceMapping and so shuffle the annotated positions
1346 * - new sequence reference for the annotation row - if null,
1347 * sequenceRef is left unchanged
1349 * array of ints containing corresponding positions
1351 * - column for current coordinate system (-1 for index+1)
1353 * - column for destination coordinate system (-1 for index+1)
1355 * - offset added to index when referencing either coordinate system
1356 * @note no checks are made as to whether from and/or to are sensible
1357 * @note caller should add the remapped annotation to newref if they have not
1360 public void remap(SequenceI newref, HashMap<Integer, int[]> mapping,
1361 int from, int to, int idxoffset)
1363 if (mapping != null)
1365 Map<Integer, Annotation> old = sequenceMapping;
1366 Map<Integer, Annotation> remap = new HashMap<>();
1368 for (int mp[] : mapping.values())
1374 Annotation ann = null;
1377 ann = sequenceMapping.get(Integer.valueOf(idxoffset + index));
1381 if (mp != null && mp.length > from)
1383 ann = sequenceMapping.get(Integer.valueOf(mp[from]));
1390 remap.put(Integer.valueOf(idxoffset + index), ann);
1394 if (to > -1 && to < mp.length)
1396 remap.put(Integer.valueOf(mp[to]), ann);
1401 sequenceMapping = remap;
1405 sequenceRef = newref;
1407 adjustForAlignment();
1411 public String getProperty(String property)
1413 if (properties == null)
1417 return properties.get(property);
1420 public void setProperty(String property, String value)
1422 if (properties == null)
1424 properties = new HashMap<>();
1426 properties.put(property, value);
1429 public boolean hasProperties()
1431 return properties != null && properties.size() > 0;
1434 public Collection<String> getProperties()
1436 if (properties == null)
1438 return Collections.emptyList();
1440 return properties.keySet();
1444 * Returns the Annotation for the given sequence position (base 1) if any,
1450 public Annotation getAnnotationForPosition(int position)
1452 return sequenceMapping == null ? null : sequenceMapping.get(position);
1457 * Set the id to "ann" followed by a counter that increments so as to be
1458 * unique for the lifetime of the JVM
1460 protected final void setAnnotationId()
1462 this.annotationId = ANNOTATION_ID_PREFIX + Long.toString(nextId());
1466 * Returns the match for the last unmatched opening RNA helix pair symbol
1467 * preceding the given column, or '(' if nothing found to match.
1472 public String getDefaultRnaHelixSymbol(int column)
1474 String result = "(";
1475 if (annotations == null)
1481 * for each preceding column, if it contains an open bracket,
1482 * count whether it is still unmatched at column, if so return its pair
1483 * (likely faster than the fancy alternative using stacks)
1485 for (int col = column - 1; col >= 0; col--)
1487 Annotation annotation = annotations[col];
1488 if (annotation == null)
1492 String displayed = annotation.displayCharacter;
1493 if (displayed == null || displayed.length() != 1)
1497 char symbol = displayed.charAt(0);
1498 if (!Rna.isOpeningParenthesis(symbol))
1504 * found an opening bracket symbol
1505 * count (closing-opening) symbols of this type that follow it,
1506 * up to and excluding the target column; if the count is less
1507 * than 1, the opening bracket is unmatched, so return its match
1509 String closer = String
1510 .valueOf(Rna.getMatchingClosingParenthesis(symbol));
1511 String opener = String.valueOf(symbol);
1513 for (int j = col + 1; j < column; j++)
1515 if (annotations[j] != null)
1517 String s = annotations[j].displayCharacter;
1518 if (closer.equals(s))
1522 else if (opener.equals(s))
1536 protected static synchronized long nextId()
1543 * @return true for rows that have a range of values in their annotation set
1545 public boolean isQuantitative()
1547 return graphMin < graphMax;
1551 * delete any columns in alignmentAnnotation that are hidden (including
1552 * sequence associated annotation).
1554 * @param hiddenColumns
1555 * the set of hidden columns
1557 public void makeVisibleAnnotation(HiddenColumns hiddenColumns)
1559 if (annotations != null)
1561 makeVisibleAnnotation(0, annotations.length, hiddenColumns);
1566 * delete any columns in alignmentAnnotation that are hidden (including
1567 * sequence associated annotation).
1570 * remove any annotation to the right of this column
1572 * remove any annotation to the left of this column
1573 * @param hiddenColumns
1574 * the set of hidden columns
1576 public void makeVisibleAnnotation(int start, int end,
1577 HiddenColumns hiddenColumns)
1579 if (annotations != null)
1581 if (hiddenColumns.hasHiddenColumns())
1583 removeHiddenAnnotation(start, end, hiddenColumns);
1587 restrict(start, end);
1593 * The actual implementation of deleting hidden annotation columns
1596 * remove any annotation to the right of this column
1598 * remove any annotation to the left of this column
1599 * @param hiddenColumns
1600 * the set of hidden columns
1602 private void removeHiddenAnnotation(int start, int end,
1603 HiddenColumns hiddenColumns)
1605 // mangle the alignmentAnnotation annotation array
1606 ArrayList<Annotation[]> annels = new ArrayList<>();
1607 Annotation[] els = null;
1611 Iterator<int[]> blocks = hiddenColumns.getVisContigsIterator(start,
1615 int annotationLength;
1616 while (blocks.hasNext())
1618 int[] block = blocks.next();
1619 annotationLength = block[1] - block[0] + 1;
1621 if (blocks.hasNext())
1623 // copy just the visible segment of the annotation row
1624 copylength = annotationLength;
1628 if (annotationLength + block[0] <= annotations.length)
1630 // copy just the visible segment of the annotation row
1631 copylength = annotationLength;
1635 // copy to the end of the annotation row
1636 copylength = annotations.length - block[0];
1640 els = new Annotation[annotationLength];
1642 System.arraycopy(annotations, block[0], els, 0, copylength);
1643 w += annotationLength;
1648 annotations = new Annotation[w];
1651 for (Annotation[] chnk : annels)
1653 System.arraycopy(chnk, 0, annotations, w, chnk.length);
1659 public static Iterable<AlignmentAnnotation> findAnnotations(
1660 Iterable<AlignmentAnnotation> list, SequenceI seq, String calcId,
1663 ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
1664 for (AlignmentAnnotation ann : list)
1666 if ((calcId == null || (ann.getCalcId() != null
1667 && ann.getCalcId().equals(calcId)))
1668 && (seq == null || (ann.sequenceRef != null
1669 && ann.sequenceRef == seq))
1671 || (ann.label != null && ann.label.equals(label))))
1680 * Answer true if any annotation matches the calcId passed in (if not null).
1683 * annotation to search
1687 public static boolean hasAnnotation(List<AlignmentAnnotation> list,
1691 if (calcId != null && !"".equals(calcId))
1693 for (AlignmentAnnotation a : list)
1695 if (a.getCalcId() == calcId)
1704 public static Iterable<AlignmentAnnotation> findAnnotation(
1705 List<AlignmentAnnotation> list, String calcId)
1707 List<AlignmentAnnotation> aa = new ArrayList<>();
1712 for (AlignmentAnnotation a : list)
1715 if (a.getCalcId() == calcId || (a.getCalcId() != null
1716 && calcId != null && a.getCalcId().equals(calcId)))
1724 public double getBitScore()
1729 public void setBitScore(double bitScore)
1731 this.bitScore = bitScore;
1734 public double getEValue()
1739 public void setEValue(double eValue)
1741 this.eValue = eValue;