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 graphGroup = new Hashtable();
136 java.awt.Color color;
138 for (int i = 0; i < annotations.length; i++)
140 row = annotations[i];
142 if (!row.visible && !row.hasScore())
150 if (row.sequenceRef == null)
154 text.append(newline);
155 text.append("SEQUENCE_REF\tALIGNMENT");
156 text.append(newline);
164 if (refSeq == null || refSeq != row.sequenceRef)
166 refSeq = row.sequenceRef;
167 text.append(newline);
168 text.append("SEQUENCE_REF\t");
169 text.append(refSeq.getName());
170 text.append(newline);
173 // mark any group references for the row
174 if (row.groupRef == null)
177 if (refGroup != null)
179 text.append(newline);
180 text.append("GROUP_REF\tALIGNMENT");
181 text.append(newline);
188 if (refGroup == null || refGroup != row.groupRef)
190 refGroup = row.groupRef;
191 text.append(newline);
192 text.append("GROUP_REF\t");
193 text.append(refGroup.getName());
194 text.append(newline);
198 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
199 // lookahead to check what the annotation row object actually contains.
200 for (int j = 0; row.annotations != null
201 && j < row.annotations.length
202 && (!hasGlyphs || !hasLabels || !hasValues); j++)
204 if (row.annotations[j] != null)
206 hasLabels |= (row.annotations[j].displayCharacter != null
207 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
209 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
210 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
213 hasText |= (row.annotations[j].description != null && row.annotations[j].description
218 if (row.graph == AlignmentAnnotation.NO_GRAPH)
220 text.append("NO_GRAPH\t");
221 hasValues = false; // only secondary structure
222 // hasLabels = false; // and annotation description string.
226 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
228 text.append("BAR_GRAPH\t");
229 hasGlyphs = false; // no secondary structure
232 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
234 hasGlyphs = false; // no secondary structure
235 text.append("LINE_GRAPH\t");
238 if (row.getThreshold() != null)
240 graphLine.append("GRAPHLINE\t");
241 graphLine.append(row.label);
242 graphLine.append("\t");
243 graphLine.append(row.getThreshold().value);
244 graphLine.append("\t");
245 graphLine.append(row.getThreshold().label);
246 graphLine.append("\t");
247 graphLine.append(jalview.util.Format.getHexString(row
248 .getThreshold().colour));
249 graphLine.append(newline);
252 if (row.graphGroup > -1)
254 String key = String.valueOf(row.graphGroup);
255 if (graphGroup.containsKey(key))
257 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
261 graphGroup.put(key, row.label);
266 text.append(row.label + "\t");
267 if (row.description != null)
269 text.append(row.description + "\t");
271 for (int j = 0; row.annotations != null
272 && j < row.annotations.length; j++)
275 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
280 if (row.annotations[j] != null)
283 if (hasGlyphs) // could be also hasGlyphs || ...
287 if (row.annotations[j].secondaryStructure != ' ')
289 // only write out the field if its not whitespace.
290 text.append(row.annotations[j].secondaryStructure);
296 if (row.annotations[j].value != Float.NaN)
298 text.append(comma + row.annotations[j].value);
302 System.err.println("Skipping NaN - not valid value.");
303 text.append(comma + 0f);// row.annotations[j].value);
309 // TODO: labels are emitted after values for bar graphs.
310 if // empty labels are allowed, so
311 (row.annotations[j].displayCharacter != null
312 && row.annotations[j].displayCharacter.length() > 0
313 && !row.annotations[j].displayCharacter.equals(" "))
315 text.append(comma + row.annotations[j].displayCharacter);
321 if (row.annotations[j].description != null
322 && row.annotations[j].description.length() > 0
323 && !row.annotations[j].description
324 .equals(row.annotations[j].displayCharacter))
326 text.append(comma + row.annotations[j].description);
330 if (color != null && !color.equals(row.annotations[j].colour))
335 color = row.annotations[j].colour;
337 if (row.annotations[j].colour != null
338 && row.annotations[j].colour != java.awt.Color.black)
342 + jalview.util.Format
343 .getHexString(row.annotations[j].colour)
352 text.append("\t" + row.score);
354 text.append(newline);
356 if (color != null && color != java.awt.Color.black && oneColour)
358 colours.append("COLOUR\t");
359 colours.append(row.label);
360 colours.append("\t");
361 colours.append(jalview.util.Format.getHexString(color));
362 colours.append(newline);
364 if (row.scaleColLabel || row.showAllColLabels
365 || row.centreColLabels)
367 rowprops.append("ROWPROPERTIES\t");
368 rowprops.append(row.label);
369 rowprops.append("\tscaletofit=");
370 rowprops.append(row.scaleColLabel);
371 rowprops.append("\tshowalllabs=");
372 rowprops.append(row.showAllColLabels);
373 rowprops.append("\tcentrelabs=");
374 rowprops.append(row.centreColLabels);
375 rowprops.append(newline);
379 text.append(newline);
381 text.append(colours.toString());
382 text.append(graphLine.toString());
383 if (graphGroup.size() > 0)
385 text.append("COMBINE\t");
386 Enumeration en = graphGroup.elements();
387 while (en.hasMoreElements())
389 text.append(en.nextElement());
390 text.append(newline);
393 text.append(rowprops.toString());
401 if (properties != null)
403 text.append(newline);
404 text.append(newline);
405 text.append("ALIGNMENT");
406 Enumeration en = properties.keys();
407 while (en.hasMoreElements())
409 String key = en.nextElement().toString();
413 text.append(properties.get(key));
415 // TODO: output alignment visualization settings here if required
419 return text.toString();
422 public void printGroups(List<SequenceGroup> list)
424 SequenceI seqrep = null;
425 for (SequenceGroup sg : list)
429 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
430 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
436 seqrep = sg.getSeqrep();
437 text.append("SEQUENCE_REF\t");
438 text.append(seqrep.getName());
439 text.append(newline);
440 text.append("SEQUENCE_GROUP\t");
441 text.append(sg.getName());
443 text.append((seqrep.findPosition(sg.getStartRes())));
445 text.append((seqrep.findPosition(sg.getEndRes())));
449 for (int s = 0; s < sg.getSize(); s++)
451 text.append(sg.getSequenceAt(s).getName());
454 text.append(newline);
455 text.append("PROPERTIES\t");
456 text.append(sg.getName());
459 if (sg.getDescription() != null)
461 text.append("description=");
462 text.append(sg.getDescription());
467 text.append("colour=");
468 text.append(ColourSchemeProperty.getColourName(sg.cs));
470 if (sg.cs.getThreshold() != 0)
472 text.append("pidThreshold=");
473 text.append(sg.cs.getThreshold());
475 if (sg.cs.conservationApplied())
477 text.append("consThreshold=");
478 text.append(sg.cs.getConservationInc());
482 text.append("outlineColour=");
483 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
486 text.append("displayBoxes=");
487 text.append(sg.getDisplayBoxes());
489 text.append("displayText=");
490 text.append(sg.getDisplayText());
492 text.append("colourText=");
493 text.append(sg.getColourText());
495 text.append("showUnconserved=");
496 text.append(sg.getShowNonconserved());
498 if (sg.textColour != java.awt.Color.black)
500 text.append("textCol1=");
501 text.append(jalview.util.Format.getHexString(sg.textColour));
504 if (sg.textColour2 != java.awt.Color.white)
506 text.append("textCol2=");
507 text.append(jalview.util.Format.getHexString(sg.textColour2));
510 if (sg.thresholdTextColour != 0)
512 text.append("textColThreshold=");
513 text.append(sg.thresholdTextColour);
516 if (sg.idColour != null)
518 text.append("idColour=");
519 text.append(jalview.util.Format.getHexString(sg.idColour));
524 text.append("hide=true\t");
528 text.append("hidecols=true\t");
532 // terminate the last line and clear the sequence ref for the group
533 text.append(newline);
534 text.append("SEQUENCE_REF");
536 text.append(newline);
537 text.append(newline);
542 SequenceI refSeq = null;
544 String refSeqId = null;
546 public boolean readAnnotationFile(AlignmentI al, String file,
549 BufferedReader in = null;
552 if (protocol.equals(AppletFormatAdapter.FILE))
554 in = new BufferedReader(new FileReader(file));
556 else if (protocol.equals(AppletFormatAdapter.URL))
558 URL url = new URL(file);
559 in = new BufferedReader(new InputStreamReader(url.openStream()));
561 else if (protocol.equals(AppletFormatAdapter.PASTE))
563 in = new BufferedReader(new StringReader(file));
565 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
567 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
570 in = new BufferedReader(new java.io.InputStreamReader(is));
575 return parseAnnotationFrom(al, in);
578 } catch (Exception ex)
580 ex.printStackTrace();
581 System.out.println("Problem reading annotation file: " + ex);
583 System.out.println("Last read line "+nlinesread+": '"+lastread+"' (first 80 chars) ...");
591 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
595 boolean modified = false;
596 String groupRef = null;
597 Hashtable groupRefRows = new Hashtable();
599 Hashtable autoAnnots = new Hashtable();
601 String line, label, description, token;
602 int graphStyle, index;
604 int existingAnnotations = 0;
605 // when true - will add new rows regardless of whether they are duplicate
606 // auto-annotation like consensus or conservation graphs
607 boolean overrideAutoAnnot = false;
608 if (al.getAlignmentAnnotation() != null)
610 existingAnnotations = al.getAlignmentAnnotation().length;
611 if (existingAnnotations > 0)
613 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
614 for (int aai = 0; aai < aa.length; aai++)
616 if (aa[aai].autoCalculated)
618 // make a note of the name and description
620 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
621 (aa[aai].groupRef == null ? null
622 : aa[aai].groupRef.getName())),
629 int alWidth = al.getWidth();
632 Annotation[] annotations;
633 AlignmentAnnotation annotation = null;
635 // First confirm this is an Annotation file
636 boolean jvAnnotationFile = false;
637 while ((line = in.readLine()) != null)
639 nlinesread++;lastread = new String(line);
640 if (line.indexOf("#") == 0)
645 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
647 jvAnnotationFile = true;
652 if (!jvAnnotationFile)
658 while ((line = in.readLine()) != null)
660 nlinesread++;lastread = new String(line);
661 if (line.indexOf("#") == 0
662 || line.indexOf("JALVIEW_ANNOTATION") > -1
663 || line.length() == 0)
668 st = new StringTokenizer(line, "\t");
669 token = st.nextToken();
670 if (token.equalsIgnoreCase("COLOUR"))
672 // TODO: use graduated colour def'n here too
673 colourAnnotations(al, st.nextToken(), st.nextToken());
678 else if (token.equalsIgnoreCase("COMBINE"))
680 combineAnnotations(al, st);
684 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
686 addRowProperties(al, st);
690 else if (token.equalsIgnoreCase("GRAPHLINE"))
697 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
699 if (st.hasMoreTokens())
701 refSeq = al.findName(refSeqId = st.nextToken());
708 refSeqIndex = Integer.parseInt(st.nextToken());
713 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
715 } catch (Exception ex)
727 else if (token.equalsIgnoreCase("GROUP_REF"))
729 // Group references could be forward or backwards, so they are
730 // resolved after the whole file is read in
732 if (st.hasMoreTokens())
734 groupRef = st.nextToken();
735 if (groupRef.length() < 1)
737 groupRef = null; // empty string
741 if (groupRefRows.get(groupRef) == null)
743 groupRefRows.put(groupRef, new Vector());
749 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
755 else if (token.equalsIgnoreCase("PROPERTIES"))
757 addProperties(al, st);
762 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
764 setBelowAlignment(al, st);
768 else if (token.equalsIgnoreCase("ALIGNMENT"))
770 addAlignmentDetails(al, st);
775 // Parse out the annotation row
776 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
777 label = st.nextToken();
780 annotations = new Annotation[alWidth];
782 float score = Float.NaN;
784 if (st.hasMoreTokens())
786 line = st.nextToken();
788 if (line.indexOf("|") == -1)
791 if (st.hasMoreTokens())
792 line = st.nextToken();
795 if (st.hasMoreTokens())
797 // This must be the score
798 score = Float.valueOf(st.nextToken()).floatValue();
801 st = new StringTokenizer(line, "|", true);
803 boolean emptyColumn = true;
804 boolean onlyOneElement = (st.countTokens() == 1);
806 while (st.hasMoreElements() && index < alWidth)
808 token = st.nextToken().trim();
814 score = Float.valueOf(token).floatValue();
816 } catch (NumberFormatException ex)
821 if (token.equals("|"))
832 annotations[index++] = parseAnnotation(token, graphStyle);
839 annotation = new AlignmentAnnotation(label, description,
840 (index == 0) ? null : annotations, 0, 0, graphStyle);
842 annotation.score = score;
843 if (!overrideAutoAnnot
844 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
847 // skip - we've already got an automatic annotation of this type.
854 annotation.belowAlignment = false;
855 // make a copy of refSeq so we can find other matches in the alignment
856 SequenceI referedSeq = refSeq;
859 // copy before we do any mapping business.
860 // TODO: verify that undo/redo with 1:many sequence associated
861 // annotations can be undone correctly
862 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
864 .createSequenceMapping(referedSeq, refSeqIndex, false);
865 annotation.adjustForAlignment();
866 referedSeq.addAlignmentAnnotation(annotation);
867 al.addAnnotation(annotation);
868 al.setAnnotationIndex(annotation,
869 al.getAlignmentAnnotation().length
870 - existingAnnotations - 1);
871 if (groupRef != null)
873 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
875 // and recover our virgin copy to use again if necessary.
878 } while (refSeqId != null
879 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
883 al.addAnnotation(annotation);
884 al.setAnnotationIndex(annotation,
885 al.getAlignmentAnnotation().length - existingAnnotations
887 if (groupRef != null)
889 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
892 // and set modification flag
895 // Finally, resolve the groupRefs
896 Enumeration en = groupRefRows.keys();
898 while (en.hasMoreElements())
900 groupRef = (String) en.nextElement();
901 boolean matched = false;
902 // Resolve group: TODO: add a getGroupByName method to alignments
903 for (SequenceGroup theGroup : al.getGroups())
905 if (theGroup.getName().equals(groupRef))
909 // TODO: specify and implement duplication of alignment annotation
910 // for multiple group references.
912 .println("Ignoring 1:many group reference mappings for group name '"
918 Vector rowset = (Vector) groupRefRows.get(groupRef);
919 if (rowset != null && rowset.size() > 0)
921 AlignmentAnnotation alan = null;
922 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
924 alan = (AlignmentAnnotation) rowset.elementAt(elm);
925 alan.groupRef = theGroup;
931 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
937 private Object autoAnnotsKey(AlignmentAnnotation annotation,
938 SequenceI refSeq, String groupRef)
940 return annotation.graph + "\t" + annotation.label + "\t"
941 + annotation.description + "\t"
942 + (refSeq != null ? refSeq.getDisplayId(true) : "");
945 Annotation parseAnnotation(String string, int graphStyle)
947 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
956 String desc = null, displayChar = null;
957 char ss = ' '; // secondaryStructure
959 boolean parsedValue = false, dcset = false;
962 java.awt.Color colour = null;
963 int i = string.indexOf("[");
964 int j = string.indexOf("]");
965 if (i > -1 && j > -1)
967 UserColourScheme ucs = new UserColourScheme();
969 colour = ucs.getColourFromString(string.substring(i + 1, j));
970 if (i > 0 && string.charAt(i - 1) == ',')
972 // clip the preceding comma as well
975 string = string.substring(0, i) + string.substring(j + 1);
978 StringTokenizer st = new StringTokenizer(string, ",", true);
980 boolean seenContent = false;
982 while (st.hasMoreTokens())
985 token = st.nextToken().trim();
986 if (token.equals(","))
988 if (!seenContent && parsedValue && !dcset)
990 // allow the value below the bar/line to be empty
1006 displayChar = token;
1008 value = new Float(token).floatValue();
1011 } catch (NumberFormatException ex)
1017 if (token.length() == 1)
1019 displayChar = token;
1023 && (token.equals("H") || token.equals("E")
1024 || token.equals("S") || token.equals(" ")))
1026 // Either this character represents a helix or sheet
1027 // or an integer which can be displayed
1028 ss = token.charAt(0);
1029 if (displayChar.equals(token.substring(0, 1)))
1034 else if (desc == null || (parsedValue && pass > 2))
1040 // if (!dcset && string.charAt(string.length() - 1) == ',')
1042 // displayChar = " "; // empty display char symbol.
1044 if (displayChar != null && desc != null && desc.length() == 1)
1046 if (displayChar.length() > 1)
1048 // switch desc and displayChar - legacy support
1049 String tmp = displayChar;
1055 if (displayChar.equals(desc))
1057 // duplicate label - hangover from the 'robust parser' above
1062 Annotation anot = new Annotation(displayChar, desc, ss, value);
1064 anot.colour = colour;
1069 void colourAnnotations(AlignmentI al, String label, String colour)
1071 UserColourScheme ucs = new UserColourScheme(colour);
1072 Annotation[] annotations;
1073 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1075 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1077 annotations = al.getAlignmentAnnotation()[i].annotations;
1078 for (int j = 0; j < annotations.length; j++)
1080 if (annotations[j] != null)
1082 annotations[j].colour = ucs.findColour('A');
1089 void combineAnnotations(AlignmentI al, StringTokenizer st)
1091 int graphGroup = -1;
1092 String group = st.nextToken();
1093 // First make sure we are not overwriting the graphIndex
1094 if (al.getAlignmentAnnotation() != null)
1096 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1098 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
1100 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
1101 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
1106 // Now update groups
1107 while (st.hasMoreTokens())
1109 group = st.nextToken();
1110 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1112 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
1114 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
1123 .println("Couldn't combine annotations. None are added to alignment yet!");
1127 void addLine(AlignmentI al, StringTokenizer st)
1129 String group = st.nextToken();
1130 AlignmentAnnotation annotation = null, alannot[] = al
1131 .getAlignmentAnnotation();
1132 if (alannot != null)
1134 for (int i = 0; i < alannot.length; i++)
1136 if (alannot[i].label.equalsIgnoreCase(group))
1138 annotation = alannot[i];
1143 if (annotation == null)
1147 float value = new Float(st.nextToken()).floatValue();
1148 String label = st.hasMoreTokens() ? st.nextToken() : null;
1149 java.awt.Color colour = null;
1150 if (st.hasMoreTokens())
1152 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1153 colour = ucs.findColour('A');
1156 annotation.setThreshold(new GraphLine(value, label, colour));
1159 void addGroup(AlignmentI al, StringTokenizer st)
1161 SequenceGroup sg = new SequenceGroup();
1162 sg.setName(st.nextToken());
1166 rng = st.nextToken();
1167 if (rng.length() > 0 && !rng.startsWith("*"))
1169 sg.setStartRes(Integer.parseInt(rng) - 1);
1175 rng = st.nextToken();
1176 if (rng.length() > 0 && !rng.startsWith("*"))
1178 sg.setEndRes(Integer.parseInt(rng) - 1);
1182 sg.setEndRes(al.getWidth() - 1);
1184 } catch (Exception e)
1187 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1188 + rng + "' - assuming alignment width for group.");
1189 // assume group is full width
1191 sg.setEndRes(al.getWidth() - 1);
1194 String index = st.nextToken();
1195 if (index.equals("-1"))
1197 while (st.hasMoreElements())
1199 sg.addSequence(al.findName(st.nextToken()), false);
1204 StringTokenizer st2 = new StringTokenizer(index, ",");
1206 while (st2.hasMoreTokens())
1208 String tmp = st2.nextToken();
1209 if (tmp.equals("*"))
1211 for (int i = 0; i < al.getHeight(); i++)
1213 sg.addSequence(al.getSequenceAt(i), false);
1216 else if (tmp.indexOf("-") >= 0)
1218 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1220 int start = (Integer.parseInt(st3.nextToken()));
1221 int end = (Integer.parseInt(st3.nextToken()));
1225 for (int i = start; i <= end; i++)
1227 sg.addSequence(al.getSequenceAt(i - 1), false);
1233 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1240 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1241 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1242 sg.setSeqrep(refSeq);
1245 if (sg.getSize() > 0)
1251 void addRowProperties(AlignmentI al, StringTokenizer st)
1253 String label = st.nextToken(), keyValue, key, value;
1254 boolean scaletofit = false, centerlab = false, showalllabs = false;
1255 while (st.hasMoreTokens())
1257 keyValue = st.nextToken();
1258 key = keyValue.substring(0, keyValue.indexOf("="));
1259 value = keyValue.substring(keyValue.indexOf("=") + 1);
1260 if (key.equalsIgnoreCase("scaletofit"))
1262 scaletofit = Boolean.valueOf(value).booleanValue();
1264 if (key.equalsIgnoreCase("showalllabs"))
1266 showalllabs = Boolean.valueOf(value).booleanValue();
1268 if (key.equalsIgnoreCase("centrelabs"))
1270 centerlab = Boolean.valueOf(value).booleanValue();
1272 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1275 for (int i = 0; i < alr.length; i++)
1277 if (alr[i].label.equalsIgnoreCase(label))
1279 alr[i].centreColLabels = centerlab;
1280 alr[i].scaleColLabel = scaletofit;
1281 alr[i].showAllColLabels = showalllabs;
1288 void addProperties(AlignmentI al, StringTokenizer st)
1291 // So far we have only added groups to the annotationHash,
1292 // the idea is in the future properties can be added to
1293 // alignments, other annotations etc
1294 if (al.getGroups() == null)
1299 String name = st.nextToken();
1300 SequenceGroup sg = null;
1301 for (SequenceGroup _sg : al.getGroups())
1303 if ((sg = _sg).getName().equals(name))
1315 String keyValue, key, value;
1316 ColourSchemeI def = sg.cs;
1318 while (st.hasMoreTokens())
1320 keyValue = st.nextToken();
1321 key = keyValue.substring(0, keyValue.indexOf("="));
1322 value = keyValue.substring(keyValue.indexOf("=") + 1);
1324 if (key.equalsIgnoreCase("description"))
1326 sg.setDescription(value);
1328 else if (key.equalsIgnoreCase("colour"))
1330 sg.cs = ColourSchemeProperty.getColour(al, value);
1332 else if (key.equalsIgnoreCase("pidThreshold"))
1334 sg.cs.setThreshold(Integer.parseInt(value), true);
1337 else if (key.equalsIgnoreCase("consThreshold"))
1339 sg.cs.setConservationInc(Integer.parseInt(value));
1340 Conservation c = new Conservation("Group",
1341 ResidueProperties.propHash, 3, sg.getSequences(null),
1342 sg.getStartRes(), sg.getEndRes() + 1);
1345 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1347 sg.cs.setConservation(c);
1350 else if (key.equalsIgnoreCase("outlineColour"))
1352 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1354 else if (key.equalsIgnoreCase("displayBoxes"))
1356 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1358 else if (key.equalsIgnoreCase("showUnconserved"))
1360 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1362 else if (key.equalsIgnoreCase("displayText"))
1364 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1366 else if (key.equalsIgnoreCase("colourText"))
1368 sg.setColourText(Boolean.valueOf(value).booleanValue());
1370 else if (key.equalsIgnoreCase("textCol1"))
1372 sg.textColour = new UserColourScheme(value).findColour('A');
1374 else if (key.equalsIgnoreCase("textCol2"))
1376 sg.textColour2 = new UserColourScheme(value).findColour('A');
1378 else if (key.equalsIgnoreCase("textColThreshold"))
1380 sg.thresholdTextColour = Integer.parseInt(value);
1382 else if (key.equalsIgnoreCase("idColour"))
1384 // consider warning if colour doesn't resolve to a real colour
1385 sg.setIdColour((def = new UserColourScheme(value))
1388 else if (key.equalsIgnoreCase("hide"))
1390 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1391 sg.setHidereps(true);
1393 else if (key.equalsIgnoreCase("hidecols"))
1395 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1396 sg.setHideCols(true);
1398 sg.recalcConservation();
1407 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1410 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1414 .print("Warning - no annotation to set below for sequence associated annotation:");
1416 while (st.hasMoreTokens())
1418 token = st.nextToken();
1421 System.err.print(" " + token);
1425 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1427 aa = al.getAlignmentAnnotation()[i];
1428 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1430 aa.belowAlignment = true;
1437 System.err.print("\n");
1441 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1443 String keyValue, key, value;
1444 while (st.hasMoreTokens())
1446 keyValue = st.nextToken();
1447 key = keyValue.substring(0, keyValue.indexOf("="));
1448 value = keyValue.substring(keyValue.indexOf("=") + 1);
1449 al.setProperty(key, value);
1454 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1457 * @param annotations
1458 * @return CSV file as a string.
1460 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1462 StringBuffer sp = new StringBuffer();
1463 for (int i = 0; i < annotations.length; i++)
1465 String atos = annotations[i].toString();
1469 int cp = atos.indexOf("\n", p);
1470 sp.append(annotations[i].label);
1474 sp.append(atos.substring(p, cp + 1));
1478 sp.append(atos.substring(p));
1484 return sp.toString();