2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
24 import jalview.analysis.*;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
28 public class AnnotationFile
30 public AnnotationFile()
36 * character used to write newlines
38 protected String newline = System.getProperty("line.separator");
41 * set new line string and reset the output buffer
45 public void setNewlineString(String nl)
51 public String getNewlineString()
60 text = new StringBuffer("JALVIEW_ANNOTATION" + newline + "# Created: "
61 + new java.util.Date() + newline + newline);
67 * convenience method for pre-2.4 feature files which have no view, hidden
68 * columns or hidden row keywords.
73 * @return feature file as a string.
75 public String printAnnotations(AlignmentAnnotation[] annotations,
76 List<SequenceGroup> list, Hashtable properties)
78 return printAnnotations(annotations, list, properties, null);
83 * hold all the information about a particular view definition read from or
84 * written out in an annotations file.
88 public String viewname;
90 public HiddenSequences hidseqs;
92 public ColumnSelection hiddencols;
94 public Vector visibleGroups;
96 public Hashtable hiddenRepSeqs;
98 public ViewDef(String viewname, HiddenSequences hidseqs,
99 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
101 this.viewname = viewname;
102 this.hidseqs = hidseqs;
103 this.hiddencols = hiddencols;
104 this.hiddenRepSeqs = hiddenRepSeqs;
109 * Prepare an annotation file given a set of annotations, groups, alignment
110 * properties and views.
116 * @return annotation file
118 public String printAnnotations(AlignmentAnnotation[] annotations,
119 List<SequenceGroup> list, Hashtable properties, ViewDef[] views)
121 // TODO: resolve views issue : annotationFile could contain visible region,
122 // or full data + hidden region specifications for a view.
123 if (annotations != null)
125 boolean oneColour = true;
126 AlignmentAnnotation row;
128 SequenceI refSeq = null;
129 SequenceGroup refGroup = null;
131 StringBuffer colours = new StringBuffer();
132 StringBuffer graphLine = new StringBuffer();
133 StringBuffer rowprops = new StringBuffer();
134 Hashtable<Integer,String> graphGroup = new Hashtable<Integer,String>();
135 Hashtable<Integer, Object[]> graphGroup_refs = new Hashtable<Integer,Object[]>();
136 BitSet graphGroupSeen = new BitSet();
138 java.awt.Color color;
140 for (int i = 0; i < annotations.length; i++)
142 row = annotations[i];
144 if (!row.visible && !row.hasScore() && !(row.graphGroup>-1 && graphGroupSeen.get(row.graphGroup)))
152 // mark any sequence references for the row
153 writeSequence_Ref(refSeq ,row.sequenceRef);
154 refSeq = row.sequenceRef;
155 // mark any group references for the row
156 writeGroup_Ref(refGroup, row.groupRef);
157 refGroup = row.groupRef;
159 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
160 // lookahead to check what the annotation row object actually contains.
161 for (int j = 0; row.annotations != null
162 && j < row.annotations.length
163 && (!hasGlyphs || !hasLabels || !hasValues); j++)
165 if (row.annotations[j] != null)
167 hasLabels |= (row.annotations[j].displayCharacter != null
168 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
170 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
171 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
174 hasText |= (row.annotations[j].description != null && row.annotations[j].description
179 if (row.graph == AlignmentAnnotation.NO_GRAPH)
181 text.append("NO_GRAPH\t");
182 hasValues = false; // only secondary structure
183 // hasLabels = false; // and annotation description string.
187 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
189 text.append("BAR_GRAPH\t");
190 hasGlyphs = false; // no secondary structure
193 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
195 hasGlyphs = false; // no secondary structure
196 text.append("LINE_GRAPH\t");
199 if (row.getThreshold() != null)
201 graphLine.append("GRAPHLINE\t");
202 graphLine.append(row.label);
203 graphLine.append("\t");
204 graphLine.append(row.getThreshold().value);
205 graphLine.append("\t");
206 graphLine.append(row.getThreshold().label);
207 graphLine.append("\t");
208 graphLine.append(jalview.util.Format.getHexString(row
209 .getThreshold().colour));
210 graphLine.append(newline);
213 if (row.graphGroup > -1)
215 graphGroupSeen.set(row.graphGroup);
216 Integer key = new Integer(row.graphGroup);
217 if (graphGroup.containsKey(key))
219 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
224 graphGroup_refs.put(key, new Object[] { refSeq, refGroup});
225 graphGroup.put(key, row.label);
230 text.append(row.label + "\t");
231 if (row.description != null)
233 text.append(row.description + "\t");
235 for (int j = 0; row.annotations != null
236 && j < row.annotations.length; j++)
239 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
244 if (row.annotations[j] != null)
247 if (hasGlyphs) // could be also hasGlyphs || ...
251 if (row.annotations[j].secondaryStructure != ' ')
253 // only write out the field if its not whitespace.
254 text.append(row.annotations[j].secondaryStructure);
260 if (row.annotations[j].value != Float.NaN)
262 text.append(comma + row.annotations[j].value);
266 System.err.println("Skipping NaN - not valid value.");
267 text.append(comma + 0f);// row.annotations[j].value);
273 // TODO: labels are emitted after values for bar graphs.
274 if // empty labels are allowed, so
275 (row.annotations[j].displayCharacter != null
276 && row.annotations[j].displayCharacter.length() > 0
277 && !row.annotations[j].displayCharacter.equals(" "))
279 text.append(comma + row.annotations[j].displayCharacter);
285 if (row.annotations[j].description != null
286 && row.annotations[j].description.length() > 0
287 && !row.annotations[j].description
288 .equals(row.annotations[j].displayCharacter))
290 text.append(comma + row.annotations[j].description);
294 if (color != null && !color.equals(row.annotations[j].colour))
299 color = row.annotations[j].colour;
301 if (row.annotations[j].colour != null
302 && row.annotations[j].colour != java.awt.Color.black)
306 + jalview.util.Format
307 .getHexString(row.annotations[j].colour)
316 text.append("\t" + row.score);
318 text.append(newline);
320 if (color != null && color != java.awt.Color.black && oneColour)
322 colours.append("COLOUR\t");
323 colours.append(row.label);
324 colours.append("\t");
325 colours.append(jalview.util.Format.getHexString(color));
326 colours.append(newline);
328 if (row.scaleColLabel || row.showAllColLabels
329 || row.centreColLabels)
331 rowprops.append("ROWPROPERTIES\t");
332 rowprops.append(row.label);
333 rowprops.append("\tscaletofit=");
334 rowprops.append(row.scaleColLabel);
335 rowprops.append("\tshowalllabs=");
336 rowprops.append(row.showAllColLabels);
337 rowprops.append("\tcentrelabs=");
338 rowprops.append(row.centreColLabels);
339 rowprops.append(newline);
341 if (graphLine.length()>0) {
342 text.append(graphLine.toString());
343 graphLine.setLength(0);
347 text.append(newline);
349 text.append(colours.toString());
350 if (graphGroup.size() > 0)
352 SequenceI oldRefSeq = refSeq;
353 SequenceGroup oldRefGroup = refGroup;
354 for (Map.Entry<Integer, String> combine_statement:graphGroup.entrySet())
356 Object[] seqRefAndGroup=graphGroup_refs.get(combine_statement.getKey());
358 writeSequence_Ref(refSeq, (SequenceI)seqRefAndGroup[0]);
359 refSeq = (SequenceI)seqRefAndGroup[0];
361 writeGroup_Ref(refGroup, (SequenceGroup)seqRefAndGroup[1]);
362 refGroup = (SequenceGroup)seqRefAndGroup[1];
363 text.append("COMBINE\t");
364 text.append(combine_statement.getValue());
365 text.append(newline);
367 writeSequence_Ref(refSeq, oldRefSeq);
370 writeGroup_Ref(refGroup, oldRefGroup);
371 refGroup = oldRefGroup;
373 text.append(rowprops.toString());
381 if (properties != null)
383 text.append(newline);
384 text.append(newline);
385 text.append("ALIGNMENT");
386 Enumeration en = properties.keys();
387 while (en.hasMoreElements())
389 String key = en.nextElement().toString();
393 text.append(properties.get(key));
395 // TODO: output alignment visualization settings here if required
399 return text.toString();
402 private Object writeGroup_Ref(SequenceGroup refGroup, SequenceGroup next_refGroup)
404 if (next_refGroup == null)
407 if (refGroup != null)
409 text.append(newline);
410 text.append("GROUP_REF\t");
411 text.append("ALIGNMENT");
412 text.append(newline);
418 if (refGroup == null || refGroup != next_refGroup)
420 text.append(newline);
421 text.append("GROUP_REF\t");
422 text.append(next_refGroup.getName());
423 text.append(newline);
430 private boolean writeSequence_Ref(SequenceI refSeq, SequenceI next_refSeq)
433 if (next_refSeq==null)
437 text.append(newline);
438 text.append("SEQUENCE_REF\t");
439 text.append("ALIGNMENT");
440 text.append(newline);
446 if (refSeq == null || refSeq != next_refSeq)
448 text.append(newline);
449 text.append("SEQUENCE_REF\t");
450 text.append(next_refSeq.getName());
451 text.append(newline);
458 public void printGroups(List<SequenceGroup> list)
460 SequenceI seqrep = null;
461 for (SequenceGroup sg : list)
465 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
466 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
472 seqrep = sg.getSeqrep();
473 text.append("SEQUENCE_REF\t");
474 text.append(seqrep.getName());
475 text.append(newline);
476 text.append("SEQUENCE_GROUP\t");
477 text.append(sg.getName());
479 text.append((seqrep.findPosition(sg.getStartRes())));
481 text.append((seqrep.findPosition(sg.getEndRes())));
485 for (int s = 0; s < sg.getSize(); s++)
487 text.append(sg.getSequenceAt(s).getName());
490 text.append(newline);
491 text.append("PROPERTIES\t");
492 text.append(sg.getName());
495 if (sg.getDescription() != null)
497 text.append("description=");
498 text.append(sg.getDescription());
503 text.append("colour=");
504 text.append(ColourSchemeProperty.getColourName(sg.cs));
506 if (sg.cs.getThreshold() != 0)
508 text.append("pidThreshold=");
509 text.append(sg.cs.getThreshold());
511 if (sg.cs.conservationApplied())
513 text.append("consThreshold=");
514 text.append(sg.cs.getConservationInc());
518 text.append("outlineColour=");
519 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
522 text.append("displayBoxes=");
523 text.append(sg.getDisplayBoxes());
525 text.append("displayText=");
526 text.append(sg.getDisplayText());
528 text.append("colourText=");
529 text.append(sg.getColourText());
531 text.append("showUnconserved=");
532 text.append(sg.getShowNonconserved());
534 if (sg.textColour != java.awt.Color.black)
536 text.append("textCol1=");
537 text.append(jalview.util.Format.getHexString(sg.textColour));
540 if (sg.textColour2 != java.awt.Color.white)
542 text.append("textCol2=");
543 text.append(jalview.util.Format.getHexString(sg.textColour2));
546 if (sg.thresholdTextColour != 0)
548 text.append("textColThreshold=");
549 text.append(sg.thresholdTextColour);
552 if (sg.idColour != null)
554 text.append("idColour=");
555 text.append(jalview.util.Format.getHexString(sg.idColour));
560 text.append("hide=true\t");
564 text.append("hidecols=true\t");
568 // terminate the last line and clear the sequence ref for the group
569 text.append(newline);
570 text.append("SEQUENCE_REF");
572 text.append(newline);
573 text.append(newline);
578 SequenceI refSeq = null;
580 String refSeqId = null;
582 public boolean readAnnotationFile(AlignmentI al, String file,
585 BufferedReader in = null;
588 if (protocol.equals(AppletFormatAdapter.FILE))
590 in = new BufferedReader(new FileReader(file));
592 else if (protocol.equals(AppletFormatAdapter.URL))
594 URL url = new URL(file);
595 in = new BufferedReader(new InputStreamReader(url.openStream()));
597 else if (protocol.equals(AppletFormatAdapter.PASTE))
599 in = new BufferedReader(new StringReader(file));
601 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
603 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
606 in = new BufferedReader(new java.io.InputStreamReader(is));
611 return parseAnnotationFrom(al, in);
614 } catch (Exception ex)
616 ex.printStackTrace();
617 System.out.println("Problem reading annotation file: " + ex);
619 System.out.println("Last read line "+nlinesread+": '"+lastread+"' (first 80 chars) ...");
627 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
631 ArrayList<Object[]> combineAnnotation_calls = new ArrayList<Object[]>();
632 boolean modified = false;
633 String groupRef = null;
634 Hashtable groupRefRows = new Hashtable();
636 Hashtable autoAnnots = new Hashtable();
638 String line, label, description, token;
639 int graphStyle, index;
641 int existingAnnotations = 0;
642 // when true - will add new rows regardless of whether they are duplicate
643 // auto-annotation like consensus or conservation graphs
644 boolean overrideAutoAnnot = false;
645 if (al.getAlignmentAnnotation() != null)
647 existingAnnotations = al.getAlignmentAnnotation().length;
648 if (existingAnnotations > 0)
650 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
651 for (int aai = 0; aai < aa.length; aai++)
653 if (aa[aai].autoCalculated)
655 // make a note of the name and description
657 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
658 (aa[aai].groupRef == null ? null
659 : aa[aai].groupRef.getName())),
666 int alWidth = al.getWidth();
669 Annotation[] annotations;
670 AlignmentAnnotation annotation = null;
672 // First confirm this is an Annotation file
673 boolean jvAnnotationFile = false;
674 while ((line = in.readLine()) != null)
676 nlinesread++;lastread = new String(line);
677 if (line.indexOf("#") == 0)
682 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
684 jvAnnotationFile = true;
689 if (!jvAnnotationFile)
695 while ((line = in.readLine()) != null)
697 nlinesread++;lastread = new String(line);
698 if (line.indexOf("#") == 0
699 || line.indexOf("JALVIEW_ANNOTATION") > -1
700 || line.length() == 0)
705 st = new StringTokenizer(line, "\t");
706 token = st.nextToken();
707 if (token.equalsIgnoreCase("COLOUR"))
709 // TODO: use graduated colour def'n here too
710 colourAnnotations(al, st.nextToken(), st.nextToken());
715 else if (token.equalsIgnoreCase("COMBINE"))
717 // keep a record of current state and resolve groupRef at end
718 combineAnnotation_calls.add(new Object[] { st, refSeq, groupRef});
722 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
724 addRowProperties(al, st);
728 else if (token.equalsIgnoreCase("GRAPHLINE"))
735 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
737 if (st.hasMoreTokens())
739 refSeq = al.findName(refSeqId = st.nextToken());
746 refSeqIndex = Integer.parseInt(st.nextToken());
751 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
753 } catch (Exception ex)
765 else if (token.equalsIgnoreCase("GROUP_REF"))
767 // Group references could be forward or backwards, so they are
768 // resolved after the whole file is read in
770 if (st.hasMoreTokens())
772 groupRef = st.nextToken();
773 if (groupRef.length() < 1)
775 groupRef = null; // empty string
779 if (groupRefRows.get(groupRef) == null)
781 groupRefRows.put(groupRef, new Vector());
787 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
793 else if (token.equalsIgnoreCase("PROPERTIES"))
795 addProperties(al, st);
800 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
802 setBelowAlignment(al, st);
806 else if (token.equalsIgnoreCase("ALIGNMENT"))
808 addAlignmentDetails(al, st);
813 // Parse out the annotation row
814 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
815 label = st.nextToken();
818 annotations = new Annotation[alWidth];
820 float score = Float.NaN;
822 if (st.hasMoreTokens())
824 line = st.nextToken();
826 if (line.indexOf("|") == -1)
829 if (st.hasMoreTokens())
830 line = st.nextToken();
833 if (st.hasMoreTokens())
835 // This must be the score
836 score = Float.valueOf(st.nextToken()).floatValue();
839 st = new StringTokenizer(line, "|", true);
841 boolean emptyColumn = true;
842 boolean onlyOneElement = (st.countTokens() == 1);
844 while (st.hasMoreElements() && index < alWidth)
846 token = st.nextToken().trim();
852 score = Float.valueOf(token).floatValue();
854 } catch (NumberFormatException ex)
859 if (token.equals("|"))
870 annotations[index++] = parseAnnotation(token, graphStyle);
877 annotation = new AlignmentAnnotation(label, description,
878 (index == 0) ? null : annotations, 0, 0, graphStyle);
880 annotation.score = score;
881 if (!overrideAutoAnnot
882 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
885 // skip - we've already got an automatic annotation of this type.
892 annotation.belowAlignment = false;
893 // make a copy of refSeq so we can find other matches in the alignment
894 SequenceI referedSeq = refSeq;
897 // copy before we do any mapping business.
898 // TODO: verify that undo/redo with 1:many sequence associated
899 // annotations can be undone correctly
900 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
902 .createSequenceMapping(referedSeq, refSeqIndex, false);
903 annotation.adjustForAlignment();
904 referedSeq.addAlignmentAnnotation(annotation);
905 al.addAnnotation(annotation);
906 al.setAnnotationIndex(annotation,
907 al.getAlignmentAnnotation().length
908 - existingAnnotations - 1);
909 if (groupRef != null)
911 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
913 // and recover our virgin copy to use again if necessary.
916 } while (refSeqId != null
917 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
921 al.addAnnotation(annotation);
922 al.setAnnotationIndex(annotation,
923 al.getAlignmentAnnotation().length - existingAnnotations
925 if (groupRef != null)
927 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
930 // and set modification flag
933 // Resolve the groupRefs
934 Hashtable <String,SequenceGroup> groupRefLookup=new Hashtable<String,SequenceGroup>();
935 Enumeration en = groupRefRows.keys();
937 while (en.hasMoreElements())
939 groupRef = (String) en.nextElement();
940 boolean matched = false;
941 // Resolve group: TODO: add a getGroupByName method to alignments
942 for (SequenceGroup theGroup : al.getGroups())
944 if (theGroup.getName().equals(groupRef))
948 // TODO: specify and implement duplication of alignment annotation
949 // for multiple group references.
951 .println("Ignoring 1:many group reference mappings for group name '"
957 Vector rowset = (Vector) groupRefRows.get(groupRef);
958 groupRefLookup.put(groupRef, theGroup);
959 if (rowset != null && rowset.size() > 0)
961 AlignmentAnnotation alan = null;
962 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
964 alan = (AlignmentAnnotation) rowset.elementAt(elm);
965 alan.groupRef = theGroup;
971 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
973 // finally, combine all the annotation rows within each context.
975 * number of combine statements in this annotation file. Used to create new groups for combined annotation graphs without disturbing existing ones
977 int combinecount = 0;
978 for (Object[] _combine_args:combineAnnotation_calls) {
979 combineAnnotations(al,
981 (StringTokenizer) _combine_args[0], // st
982 (SequenceI) _combine_args[1], // refSeq
983 (_combine_args[2]==null) ? null : groupRefLookup.get((String)_combine_args[2]) // the reference group, or null
991 private Object autoAnnotsKey(AlignmentAnnotation annotation,
992 SequenceI refSeq, String groupRef)
994 return annotation.graph + "\t" + annotation.label + "\t"
995 + annotation.description + "\t"
996 + (refSeq != null ? refSeq.getDisplayId(true) : "");
999 Annotation parseAnnotation(String string, int graphStyle)
1001 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
1010 String desc = null, displayChar = null;
1011 char ss = ' '; // secondaryStructure
1013 boolean parsedValue = false, dcset = false;
1016 java.awt.Color colour = null;
1017 int i = string.indexOf("[");
1018 int j = string.indexOf("]");
1019 if (i > -1 && j > -1)
1021 UserColourScheme ucs = new UserColourScheme();
1023 colour = ucs.getColourFromString(string.substring(i + 1, j));
1024 if (i > 0 && string.charAt(i - 1) == ',')
1026 // clip the preceding comma as well
1029 string = string.substring(0, i) + string.substring(j + 1);
1032 StringTokenizer st = new StringTokenizer(string, ",", true);
1034 boolean seenContent = false;
1036 while (st.hasMoreTokens())
1039 token = st.nextToken().trim();
1040 if (token.equals(","))
1042 if (!seenContent && parsedValue && !dcset)
1044 // allow the value below the bar/line to be empty
1048 seenContent = false;
1060 displayChar = token;
1062 value = new Float(token).floatValue();
1065 } catch (NumberFormatException ex)
1071 if (token.length() == 1)
1073 displayChar = token;
1077 && (token.equals("H") || token.equals("E")
1078 || token.equals("S") || token.equals(" ")))
1080 // Either this character represents a helix or sheet
1081 // or an integer which can be displayed
1082 ss = token.charAt(0);
1083 if (displayChar.equals(token.substring(0, 1)))
1088 else if (desc == null || (parsedValue && pass > 2))
1094 // if (!dcset && string.charAt(string.length() - 1) == ',')
1096 // displayChar = " "; // empty display char symbol.
1098 if (displayChar != null && desc != null && desc.length() == 1)
1100 if (displayChar.length() > 1)
1102 // switch desc and displayChar - legacy support
1103 String tmp = displayChar;
1109 if (displayChar.equals(desc))
1111 // duplicate label - hangover from the 'robust parser' above
1116 Annotation anot = new Annotation(displayChar, desc, ss, value);
1118 anot.colour = colour;
1123 void colourAnnotations(AlignmentI al, String label, String colour)
1125 UserColourScheme ucs = new UserColourScheme(colour);
1126 Annotation[] annotations;
1127 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1129 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1131 annotations = al.getAlignmentAnnotation()[i].annotations;
1132 for (int j = 0; j < annotations.length; j++)
1134 if (annotations[j] != null)
1136 annotations[j].colour = ucs.findColour('A');
1143 void combineAnnotations(AlignmentI al, int combineCount, StringTokenizer st, SequenceI seqRef, SequenceGroup groupRef)
1145 String group = st.nextToken();
1146 // First make sure we are not overwriting the graphIndex
1148 if (al.getAlignmentAnnotation() != null)
1150 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1152 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1154 if (aa.graphGroup>graphGroup)
1156 // try to number graphGroups in order of occurence.
1157 graphGroup=aa.graphGroup+1;
1159 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1161 if (aa.graphGroup>-1)
1163 graphGroup = aa.graphGroup;
1165 if (graphGroup <= combineCount)
1167 graphGroup=combineCount+1;
1169 aa.graphGroup = graphGroup;
1175 // Now update groups
1176 while (st.hasMoreTokens())
1178 group = st.nextToken();
1179 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1181 AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
1182 if (aa.sequenceRef==seqRef && aa.groupRef==groupRef && aa.label.equalsIgnoreCase(group))
1184 aa.graphGroup = graphGroup;
1193 .println("Couldn't combine annotations. None are added to alignment yet!");
1197 void addLine(AlignmentI al, StringTokenizer st)
1199 String group = st.nextToken();
1200 AlignmentAnnotation annotation = null, alannot[] = al
1201 .getAlignmentAnnotation();
1202 if (alannot != null)
1204 for (int i = 0; i < alannot.length; i++)
1206 if (alannot[i].label.equalsIgnoreCase(group))
1208 annotation = alannot[i];
1213 if (annotation == null)
1217 float value = new Float(st.nextToken()).floatValue();
1218 String label = st.hasMoreTokens() ? st.nextToken() : null;
1219 java.awt.Color colour = null;
1220 if (st.hasMoreTokens())
1222 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1223 colour = ucs.findColour('A');
1226 annotation.setThreshold(new GraphLine(value, label, colour));
1229 void addGroup(AlignmentI al, StringTokenizer st)
1231 SequenceGroup sg = new SequenceGroup();
1232 sg.setName(st.nextToken());
1236 rng = st.nextToken();
1237 if (rng.length() > 0 && !rng.startsWith("*"))
1239 sg.setStartRes(Integer.parseInt(rng) - 1);
1245 rng = st.nextToken();
1246 if (rng.length() > 0 && !rng.startsWith("*"))
1248 sg.setEndRes(Integer.parseInt(rng) - 1);
1252 sg.setEndRes(al.getWidth() - 1);
1254 } catch (Exception e)
1257 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1258 + rng + "' - assuming alignment width for group.");
1259 // assume group is full width
1261 sg.setEndRes(al.getWidth() - 1);
1264 String index = st.nextToken();
1265 if (index.equals("-1"))
1267 while (st.hasMoreElements())
1269 sg.addSequence(al.findName(st.nextToken()), false);
1274 StringTokenizer st2 = new StringTokenizer(index, ",");
1276 while (st2.hasMoreTokens())
1278 String tmp = st2.nextToken();
1279 if (tmp.equals("*"))
1281 for (int i = 0; i < al.getHeight(); i++)
1283 sg.addSequence(al.getSequenceAt(i), false);
1286 else if (tmp.indexOf("-") >= 0)
1288 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1290 int start = (Integer.parseInt(st3.nextToken()));
1291 int end = (Integer.parseInt(st3.nextToken()));
1295 for (int i = start; i <= end; i++)
1297 sg.addSequence(al.getSequenceAt(i - 1), false);
1303 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1310 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1311 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1312 sg.setSeqrep(refSeq);
1315 if (sg.getSize() > 0)
1321 void addRowProperties(AlignmentI al, StringTokenizer st)
1323 String label = st.nextToken(), keyValue, key, value;
1324 boolean scaletofit = false, centerlab = false, showalllabs = false;
1325 while (st.hasMoreTokens())
1327 keyValue = st.nextToken();
1328 key = keyValue.substring(0, keyValue.indexOf("="));
1329 value = keyValue.substring(keyValue.indexOf("=") + 1);
1330 if (key.equalsIgnoreCase("scaletofit"))
1332 scaletofit = Boolean.valueOf(value).booleanValue();
1334 if (key.equalsIgnoreCase("showalllabs"))
1336 showalllabs = Boolean.valueOf(value).booleanValue();
1338 if (key.equalsIgnoreCase("centrelabs"))
1340 centerlab = Boolean.valueOf(value).booleanValue();
1342 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1345 for (int i = 0; i < alr.length; i++)
1347 if (alr[i].label.equalsIgnoreCase(label))
1349 alr[i].centreColLabels = centerlab;
1350 alr[i].scaleColLabel = scaletofit;
1351 alr[i].showAllColLabels = showalllabs;
1358 void addProperties(AlignmentI al, StringTokenizer st)
1361 // So far we have only added groups to the annotationHash,
1362 // the idea is in the future properties can be added to
1363 // alignments, other annotations etc
1364 if (al.getGroups() == null)
1369 String name = st.nextToken();
1370 SequenceGroup sg = null;
1371 for (SequenceGroup _sg : al.getGroups())
1373 if ((sg = _sg).getName().equals(name))
1385 String keyValue, key, value;
1386 ColourSchemeI def = sg.cs;
1388 while (st.hasMoreTokens())
1390 keyValue = st.nextToken();
1391 key = keyValue.substring(0, keyValue.indexOf("="));
1392 value = keyValue.substring(keyValue.indexOf("=") + 1);
1394 if (key.equalsIgnoreCase("description"))
1396 sg.setDescription(value);
1398 else if (key.equalsIgnoreCase("colour"))
1400 sg.cs = ColourSchemeProperty.getColour(al, value);
1402 else if (key.equalsIgnoreCase("pidThreshold"))
1404 sg.cs.setThreshold(Integer.parseInt(value), true);
1407 else if (key.equalsIgnoreCase("consThreshold"))
1409 sg.cs.setConservationInc(Integer.parseInt(value));
1410 Conservation c = new Conservation("Group",
1411 ResidueProperties.propHash, 3, sg.getSequences(null),
1412 sg.getStartRes(), sg.getEndRes() + 1);
1415 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1417 sg.cs.setConservation(c);
1420 else if (key.equalsIgnoreCase("outlineColour"))
1422 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1424 else if (key.equalsIgnoreCase("displayBoxes"))
1426 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1428 else if (key.equalsIgnoreCase("showUnconserved"))
1430 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1432 else if (key.equalsIgnoreCase("displayText"))
1434 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1436 else if (key.equalsIgnoreCase("colourText"))
1438 sg.setColourText(Boolean.valueOf(value).booleanValue());
1440 else if (key.equalsIgnoreCase("textCol1"))
1442 sg.textColour = new UserColourScheme(value).findColour('A');
1444 else if (key.equalsIgnoreCase("textCol2"))
1446 sg.textColour2 = new UserColourScheme(value).findColour('A');
1448 else if (key.equalsIgnoreCase("textColThreshold"))
1450 sg.thresholdTextColour = Integer.parseInt(value);
1452 else if (key.equalsIgnoreCase("idColour"))
1454 // consider warning if colour doesn't resolve to a real colour
1455 sg.setIdColour((def = new UserColourScheme(value))
1458 else if (key.equalsIgnoreCase("hide"))
1460 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1461 sg.setHidereps(true);
1463 else if (key.equalsIgnoreCase("hidecols"))
1465 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1466 sg.setHideCols(true);
1468 sg.recalcConservation();
1477 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1480 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1484 .print("Warning - no annotation to set below for sequence associated annotation:");
1486 while (st.hasMoreTokens())
1488 token = st.nextToken();
1491 System.err.print(" " + token);
1495 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1497 aa = al.getAlignmentAnnotation()[i];
1498 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1500 aa.belowAlignment = true;
1507 System.err.print("\n");
1511 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1513 String keyValue, key, value;
1514 while (st.hasMoreTokens())
1516 keyValue = st.nextToken();
1517 key = keyValue.substring(0, keyValue.indexOf("="));
1518 value = keyValue.substring(keyValue.indexOf("=") + 1);
1519 al.setProperty(key, value);
1524 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1527 * @param annotations
1528 * @return CSV file as a string.
1530 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1532 StringBuffer sp = new StringBuffer();
1533 for (int i = 0; i < annotations.length; i++)
1535 String atos = annotations[i].toString();
1539 int cp = atos.indexOf("\n", p);
1540 sp.append(annotations[i].label);
1544 sp.append(atos.substring(p, cp + 1));
1548 sp.append(atos.substring(p));
1554 return sp.toString();