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();
135 BitSet graphGroupSeen = new BitSet();
137 java.awt.Color color;
139 for (int i = 0; i < annotations.length; i++)
141 row = annotations[i];
143 if (!row.visible && !row.hasScore() && !(row.graphGroup>-1 && graphGroupSeen.get(row.graphGroup)))
151 if (row.sequenceRef == null)
155 text.append(newline);
156 text.append("SEQUENCE_REF\tALIGNMENT");
157 text.append(newline);
165 if (refSeq == null || refSeq != row.sequenceRef)
167 refSeq = row.sequenceRef;
168 text.append(newline);
169 text.append("SEQUENCE_REF\t");
170 text.append(refSeq.getName());
171 text.append(newline);
174 // mark any group references for the row
175 if (row.groupRef == null)
178 if (refGroup != null)
180 text.append(newline);
181 text.append("GROUP_REF\tALIGNMENT");
182 text.append(newline);
189 if (refGroup == null || refGroup != row.groupRef)
191 refGroup = row.groupRef;
192 text.append(newline);
193 text.append("GROUP_REF\t");
194 text.append(refGroup.getName());
195 text.append(newline);
199 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
200 // lookahead to check what the annotation row object actually contains.
201 for (int j = 0; row.annotations != null
202 && j < row.annotations.length
203 && (!hasGlyphs || !hasLabels || !hasValues); j++)
205 if (row.annotations[j] != null)
207 hasLabels |= (row.annotations[j].displayCharacter != null
208 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
210 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
211 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
214 hasText |= (row.annotations[j].description != null && row.annotations[j].description
219 if (row.graph == AlignmentAnnotation.NO_GRAPH)
221 text.append("NO_GRAPH\t");
222 hasValues = false; // only secondary structure
223 // hasLabels = false; // and annotation description string.
227 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
229 text.append("BAR_GRAPH\t");
230 hasGlyphs = false; // no secondary structure
233 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
235 hasGlyphs = false; // no secondary structure
236 text.append("LINE_GRAPH\t");
239 if (row.getThreshold() != null)
241 graphLine.append("GRAPHLINE\t");
242 graphLine.append(row.label);
243 graphLine.append("\t");
244 graphLine.append(row.getThreshold().value);
245 graphLine.append("\t");
246 graphLine.append(row.getThreshold().label);
247 graphLine.append("\t");
248 graphLine.append(jalview.util.Format.getHexString(row
249 .getThreshold().colour));
250 graphLine.append(newline);
253 if (row.graphGroup > -1)
255 graphGroupSeen.set(row.graphGroup);
256 String key = String.valueOf(row.graphGroup);
257 if (graphGroup.containsKey(key))
259 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
263 graphGroup.put(key, row.label);
268 text.append(row.label + "\t");
269 if (row.description != null)
271 text.append(row.description + "\t");
273 for (int j = 0; row.annotations != null
274 && j < row.annotations.length; j++)
277 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
282 if (row.annotations[j] != null)
285 if (hasGlyphs) // could be also hasGlyphs || ...
289 if (row.annotations[j].secondaryStructure != ' ')
291 // only write out the field if its not whitespace.
292 text.append(row.annotations[j].secondaryStructure);
298 if (row.annotations[j].value != Float.NaN)
300 text.append(comma + row.annotations[j].value);
304 System.err.println("Skipping NaN - not valid value.");
305 text.append(comma + 0f);// row.annotations[j].value);
311 // TODO: labels are emitted after values for bar graphs.
312 if // empty labels are allowed, so
313 (row.annotations[j].displayCharacter != null
314 && row.annotations[j].displayCharacter.length() > 0
315 && !row.annotations[j].displayCharacter.equals(" "))
317 text.append(comma + row.annotations[j].displayCharacter);
323 if (row.annotations[j].description != null
324 && row.annotations[j].description.length() > 0
325 && !row.annotations[j].description
326 .equals(row.annotations[j].displayCharacter))
328 text.append(comma + row.annotations[j].description);
332 if (color != null && !color.equals(row.annotations[j].colour))
337 color = row.annotations[j].colour;
339 if (row.annotations[j].colour != null
340 && row.annotations[j].colour != java.awt.Color.black)
344 + jalview.util.Format
345 .getHexString(row.annotations[j].colour)
354 text.append("\t" + row.score);
356 text.append(newline);
358 if (color != null && color != java.awt.Color.black && oneColour)
360 colours.append("COLOUR\t");
361 colours.append(row.label);
362 colours.append("\t");
363 colours.append(jalview.util.Format.getHexString(color));
364 colours.append(newline);
366 if (row.scaleColLabel || row.showAllColLabels
367 || row.centreColLabels)
369 rowprops.append("ROWPROPERTIES\t");
370 rowprops.append(row.label);
371 rowprops.append("\tscaletofit=");
372 rowprops.append(row.scaleColLabel);
373 rowprops.append("\tshowalllabs=");
374 rowprops.append(row.showAllColLabels);
375 rowprops.append("\tcentrelabs=");
376 rowprops.append(row.centreColLabels);
377 rowprops.append(newline);
381 text.append(newline);
383 text.append(colours.toString());
384 text.append(graphLine.toString());
385 if (graphGroup.size() > 0)
387 Enumeration en = graphGroup.elements();
388 while (en.hasMoreElements())
390 text.append("COMBINE\t");
391 text.append(en.nextElement());
392 text.append(newline);
395 text.append(rowprops.toString());
403 if (properties != null)
405 text.append(newline);
406 text.append(newline);
407 text.append("ALIGNMENT");
408 Enumeration en = properties.keys();
409 while (en.hasMoreElements())
411 String key = en.nextElement().toString();
415 text.append(properties.get(key));
417 // TODO: output alignment visualization settings here if required
421 return text.toString();
424 public void printGroups(List<SequenceGroup> list)
426 SequenceI seqrep = null;
427 for (SequenceGroup sg : list)
431 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
432 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
438 seqrep = sg.getSeqrep();
439 text.append("SEQUENCE_REF\t");
440 text.append(seqrep.getName());
441 text.append(newline);
442 text.append("SEQUENCE_GROUP\t");
443 text.append(sg.getName());
445 text.append((seqrep.findPosition(sg.getStartRes())));
447 text.append((seqrep.findPosition(sg.getEndRes())));
451 for (int s = 0; s < sg.getSize(); s++)
453 text.append(sg.getSequenceAt(s).getName());
456 text.append(newline);
457 text.append("PROPERTIES\t");
458 text.append(sg.getName());
461 if (sg.getDescription() != null)
463 text.append("description=");
464 text.append(sg.getDescription());
469 text.append("colour=");
470 text.append(ColourSchemeProperty.getColourName(sg.cs));
472 if (sg.cs.getThreshold() != 0)
474 text.append("pidThreshold=");
475 text.append(sg.cs.getThreshold());
477 if (sg.cs.conservationApplied())
479 text.append("consThreshold=");
480 text.append(sg.cs.getConservationInc());
484 text.append("outlineColour=");
485 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
488 text.append("displayBoxes=");
489 text.append(sg.getDisplayBoxes());
491 text.append("displayText=");
492 text.append(sg.getDisplayText());
494 text.append("colourText=");
495 text.append(sg.getColourText());
497 text.append("showUnconserved=");
498 text.append(sg.getShowNonconserved());
500 if (sg.textColour != java.awt.Color.black)
502 text.append("textCol1=");
503 text.append(jalview.util.Format.getHexString(sg.textColour));
506 if (sg.textColour2 != java.awt.Color.white)
508 text.append("textCol2=");
509 text.append(jalview.util.Format.getHexString(sg.textColour2));
512 if (sg.thresholdTextColour != 0)
514 text.append("textColThreshold=");
515 text.append(sg.thresholdTextColour);
518 if (sg.idColour != null)
520 text.append("idColour=");
521 text.append(jalview.util.Format.getHexString(sg.idColour));
526 text.append("hide=true\t");
530 text.append("hidecols=true\t");
534 // terminate the last line and clear the sequence ref for the group
535 text.append(newline);
536 text.append("SEQUENCE_REF");
538 text.append(newline);
539 text.append(newline);
544 SequenceI refSeq = null;
546 String refSeqId = null;
548 public boolean readAnnotationFile(AlignmentI al, String file,
551 BufferedReader in = null;
554 if (protocol.equals(AppletFormatAdapter.FILE))
556 in = new BufferedReader(new FileReader(file));
558 else if (protocol.equals(AppletFormatAdapter.URL))
560 URL url = new URL(file);
561 in = new BufferedReader(new InputStreamReader(url.openStream()));
563 else if (protocol.equals(AppletFormatAdapter.PASTE))
565 in = new BufferedReader(new StringReader(file));
567 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
569 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
572 in = new BufferedReader(new java.io.InputStreamReader(is));
577 return parseAnnotationFrom(al, in);
580 } catch (Exception ex)
582 ex.printStackTrace();
583 System.out.println("Problem reading annotation file: " + ex);
585 System.out.println("Last read line "+nlinesread+": '"+lastread+"' (first 80 chars) ...");
593 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
597 boolean modified = false;
598 String groupRef = null;
599 Hashtable groupRefRows = new Hashtable();
601 Hashtable autoAnnots = new Hashtable();
603 String line, label, description, token;
604 int graphStyle, index;
606 int existingAnnotations = 0;
607 // when true - will add new rows regardless of whether they are duplicate
608 // auto-annotation like consensus or conservation graphs
609 boolean overrideAutoAnnot = false;
610 if (al.getAlignmentAnnotation() != null)
612 existingAnnotations = al.getAlignmentAnnotation().length;
613 if (existingAnnotations > 0)
615 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
616 for (int aai = 0; aai < aa.length; aai++)
618 if (aa[aai].autoCalculated)
620 // make a note of the name and description
622 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
623 (aa[aai].groupRef == null ? null
624 : aa[aai].groupRef.getName())),
631 int alWidth = al.getWidth();
634 Annotation[] annotations;
635 AlignmentAnnotation annotation = null;
637 // First confirm this is an Annotation file
638 boolean jvAnnotationFile = false;
639 while ((line = in.readLine()) != null)
641 nlinesread++;lastread = new String(line);
642 if (line.indexOf("#") == 0)
647 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
649 jvAnnotationFile = true;
654 if (!jvAnnotationFile)
660 while ((line = in.readLine()) != null)
662 nlinesread++;lastread = new String(line);
663 if (line.indexOf("#") == 0
664 || line.indexOf("JALVIEW_ANNOTATION") > -1
665 || line.length() == 0)
670 st = new StringTokenizer(line, "\t");
671 token = st.nextToken();
672 if (token.equalsIgnoreCase("COLOUR"))
674 // TODO: use graduated colour def'n here too
675 colourAnnotations(al, st.nextToken(), st.nextToken());
680 else if (token.equalsIgnoreCase("COMBINE"))
682 combineAnnotations(al, st);
686 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
688 addRowProperties(al, st);
692 else if (token.equalsIgnoreCase("GRAPHLINE"))
699 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
701 if (st.hasMoreTokens())
703 refSeq = al.findName(refSeqId = st.nextToken());
710 refSeqIndex = Integer.parseInt(st.nextToken());
715 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
717 } catch (Exception ex)
729 else if (token.equalsIgnoreCase("GROUP_REF"))
731 // Group references could be forward or backwards, so they are
732 // resolved after the whole file is read in
734 if (st.hasMoreTokens())
736 groupRef = st.nextToken();
737 if (groupRef.length() < 1)
739 groupRef = null; // empty string
743 if (groupRefRows.get(groupRef) == null)
745 groupRefRows.put(groupRef, new Vector());
751 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
757 else if (token.equalsIgnoreCase("PROPERTIES"))
759 addProperties(al, st);
764 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
766 setBelowAlignment(al, st);
770 else if (token.equalsIgnoreCase("ALIGNMENT"))
772 addAlignmentDetails(al, st);
777 // Parse out the annotation row
778 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
779 label = st.nextToken();
782 annotations = new Annotation[alWidth];
784 float score = Float.NaN;
786 if (st.hasMoreTokens())
788 line = st.nextToken();
790 if (line.indexOf("|") == -1)
793 if (st.hasMoreTokens())
794 line = st.nextToken();
797 if (st.hasMoreTokens())
799 // This must be the score
800 score = Float.valueOf(st.nextToken()).floatValue();
803 st = new StringTokenizer(line, "|", true);
805 boolean emptyColumn = true;
806 boolean onlyOneElement = (st.countTokens() == 1);
808 while (st.hasMoreElements() && index < alWidth)
810 token = st.nextToken().trim();
816 score = Float.valueOf(token).floatValue();
818 } catch (NumberFormatException ex)
823 if (token.equals("|"))
834 annotations[index++] = parseAnnotation(token, graphStyle);
841 annotation = new AlignmentAnnotation(label, description,
842 (index == 0) ? null : annotations, 0, 0, graphStyle);
844 annotation.score = score;
845 if (!overrideAutoAnnot
846 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
849 // skip - we've already got an automatic annotation of this type.
856 annotation.belowAlignment = false;
857 // make a copy of refSeq so we can find other matches in the alignment
858 SequenceI referedSeq = refSeq;
861 // copy before we do any mapping business.
862 // TODO: verify that undo/redo with 1:many sequence associated
863 // annotations can be undone correctly
864 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
866 .createSequenceMapping(referedSeq, refSeqIndex, false);
867 annotation.adjustForAlignment();
868 referedSeq.addAlignmentAnnotation(annotation);
869 al.addAnnotation(annotation);
870 al.setAnnotationIndex(annotation,
871 al.getAlignmentAnnotation().length
872 - existingAnnotations - 1);
873 if (groupRef != null)
875 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
877 // and recover our virgin copy to use again if necessary.
880 } while (refSeqId != null
881 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
885 al.addAnnotation(annotation);
886 al.setAnnotationIndex(annotation,
887 al.getAlignmentAnnotation().length - existingAnnotations
889 if (groupRef != null)
891 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
894 // and set modification flag
897 // Finally, resolve the groupRefs
898 Enumeration en = groupRefRows.keys();
900 while (en.hasMoreElements())
902 groupRef = (String) en.nextElement();
903 boolean matched = false;
904 // Resolve group: TODO: add a getGroupByName method to alignments
905 for (SequenceGroup theGroup : al.getGroups())
907 if (theGroup.getName().equals(groupRef))
911 // TODO: specify and implement duplication of alignment annotation
912 // for multiple group references.
914 .println("Ignoring 1:many group reference mappings for group name '"
920 Vector rowset = (Vector) groupRefRows.get(groupRef);
921 if (rowset != null && rowset.size() > 0)
923 AlignmentAnnotation alan = null;
924 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
926 alan = (AlignmentAnnotation) rowset.elementAt(elm);
927 alan.groupRef = theGroup;
933 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
939 private Object autoAnnotsKey(AlignmentAnnotation annotation,
940 SequenceI refSeq, String groupRef)
942 return annotation.graph + "\t" + annotation.label + "\t"
943 + annotation.description + "\t"
944 + (refSeq != null ? refSeq.getDisplayId(true) : "");
947 Annotation parseAnnotation(String string, int graphStyle)
949 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
958 String desc = null, displayChar = null;
959 char ss = ' '; // secondaryStructure
961 boolean parsedValue = false, dcset = false;
964 java.awt.Color colour = null;
965 int i = string.indexOf("[");
966 int j = string.indexOf("]");
967 if (i > -1 && j > -1)
969 UserColourScheme ucs = new UserColourScheme();
971 colour = ucs.getColourFromString(string.substring(i + 1, j));
972 if (i > 0 && string.charAt(i - 1) == ',')
974 // clip the preceding comma as well
977 string = string.substring(0, i) + string.substring(j + 1);
980 StringTokenizer st = new StringTokenizer(string, ",", true);
982 boolean seenContent = false;
984 while (st.hasMoreTokens())
987 token = st.nextToken().trim();
988 if (token.equals(","))
990 if (!seenContent && parsedValue && !dcset)
992 // allow the value below the bar/line to be empty
1008 displayChar = token;
1010 value = new Float(token).floatValue();
1013 } catch (NumberFormatException ex)
1019 if (token.length() == 1)
1021 displayChar = token;
1025 && (token.equals("H") || token.equals("E")
1026 || token.equals("S") || token.equals(" ")))
1028 // Either this character represents a helix or sheet
1029 // or an integer which can be displayed
1030 ss = token.charAt(0);
1031 if (displayChar.equals(token.substring(0, 1)))
1036 else if (desc == null || (parsedValue && pass > 2))
1042 // if (!dcset && string.charAt(string.length() - 1) == ',')
1044 // displayChar = " "; // empty display char symbol.
1046 if (displayChar != null && desc != null && desc.length() == 1)
1048 if (displayChar.length() > 1)
1050 // switch desc and displayChar - legacy support
1051 String tmp = displayChar;
1057 if (displayChar.equals(desc))
1059 // duplicate label - hangover from the 'robust parser' above
1064 Annotation anot = new Annotation(displayChar, desc, ss, value);
1066 anot.colour = colour;
1071 void colourAnnotations(AlignmentI al, String label, String colour)
1073 UserColourScheme ucs = new UserColourScheme(colour);
1074 Annotation[] annotations;
1075 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1077 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1079 annotations = al.getAlignmentAnnotation()[i].annotations;
1080 for (int j = 0; j < annotations.length; j++)
1082 if (annotations[j] != null)
1084 annotations[j].colour = ucs.findColour('A');
1091 void combineAnnotations(AlignmentI al, StringTokenizer st)
1093 int graphGroup = -1;
1094 String group = st.nextToken();
1095 // First make sure we are not overwriting the graphIndex
1096 if (al.getAlignmentAnnotation() != null)
1098 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1100 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
1102 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
1103 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
1108 // Now update groups
1109 while (st.hasMoreTokens())
1111 group = st.nextToken();
1112 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1114 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
1116 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
1125 .println("Couldn't combine annotations. None are added to alignment yet!");
1129 void addLine(AlignmentI al, StringTokenizer st)
1131 String group = st.nextToken();
1132 AlignmentAnnotation annotation = null, alannot[] = al
1133 .getAlignmentAnnotation();
1134 if (alannot != null)
1136 for (int i = 0; i < alannot.length; i++)
1138 if (alannot[i].label.equalsIgnoreCase(group))
1140 annotation = alannot[i];
1145 if (annotation == null)
1149 float value = new Float(st.nextToken()).floatValue();
1150 String label = st.hasMoreTokens() ? st.nextToken() : null;
1151 java.awt.Color colour = null;
1152 if (st.hasMoreTokens())
1154 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1155 colour = ucs.findColour('A');
1158 annotation.setThreshold(new GraphLine(value, label, colour));
1161 void addGroup(AlignmentI al, StringTokenizer st)
1163 SequenceGroup sg = new SequenceGroup();
1164 sg.setName(st.nextToken());
1168 rng = st.nextToken();
1169 if (rng.length() > 0 && !rng.startsWith("*"))
1171 sg.setStartRes(Integer.parseInt(rng) - 1);
1177 rng = st.nextToken();
1178 if (rng.length() > 0 && !rng.startsWith("*"))
1180 sg.setEndRes(Integer.parseInt(rng) - 1);
1184 sg.setEndRes(al.getWidth() - 1);
1186 } catch (Exception e)
1189 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1190 + rng + "' - assuming alignment width for group.");
1191 // assume group is full width
1193 sg.setEndRes(al.getWidth() - 1);
1196 String index = st.nextToken();
1197 if (index.equals("-1"))
1199 while (st.hasMoreElements())
1201 sg.addSequence(al.findName(st.nextToken()), false);
1206 StringTokenizer st2 = new StringTokenizer(index, ",");
1208 while (st2.hasMoreTokens())
1210 String tmp = st2.nextToken();
1211 if (tmp.equals("*"))
1213 for (int i = 0; i < al.getHeight(); i++)
1215 sg.addSequence(al.getSequenceAt(i), false);
1218 else if (tmp.indexOf("-") >= 0)
1220 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1222 int start = (Integer.parseInt(st3.nextToken()));
1223 int end = (Integer.parseInt(st3.nextToken()));
1227 for (int i = start; i <= end; i++)
1229 sg.addSequence(al.getSequenceAt(i - 1), false);
1235 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1242 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1243 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1244 sg.setSeqrep(refSeq);
1247 if (sg.getSize() > 0)
1253 void addRowProperties(AlignmentI al, StringTokenizer st)
1255 String label = st.nextToken(), keyValue, key, value;
1256 boolean scaletofit = false, centerlab = false, showalllabs = false;
1257 while (st.hasMoreTokens())
1259 keyValue = st.nextToken();
1260 key = keyValue.substring(0, keyValue.indexOf("="));
1261 value = keyValue.substring(keyValue.indexOf("=") + 1);
1262 if (key.equalsIgnoreCase("scaletofit"))
1264 scaletofit = Boolean.valueOf(value).booleanValue();
1266 if (key.equalsIgnoreCase("showalllabs"))
1268 showalllabs = Boolean.valueOf(value).booleanValue();
1270 if (key.equalsIgnoreCase("centrelabs"))
1272 centerlab = Boolean.valueOf(value).booleanValue();
1274 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1277 for (int i = 0; i < alr.length; i++)
1279 if (alr[i].label.equalsIgnoreCase(label))
1281 alr[i].centreColLabels = centerlab;
1282 alr[i].scaleColLabel = scaletofit;
1283 alr[i].showAllColLabels = showalllabs;
1290 void addProperties(AlignmentI al, StringTokenizer st)
1293 // So far we have only added groups to the annotationHash,
1294 // the idea is in the future properties can be added to
1295 // alignments, other annotations etc
1296 if (al.getGroups() == null)
1301 String name = st.nextToken();
1302 SequenceGroup sg = null;
1303 for (SequenceGroup _sg : al.getGroups())
1305 if ((sg = _sg).getName().equals(name))
1317 String keyValue, key, value;
1318 ColourSchemeI def = sg.cs;
1320 while (st.hasMoreTokens())
1322 keyValue = st.nextToken();
1323 key = keyValue.substring(0, keyValue.indexOf("="));
1324 value = keyValue.substring(keyValue.indexOf("=") + 1);
1326 if (key.equalsIgnoreCase("description"))
1328 sg.setDescription(value);
1330 else if (key.equalsIgnoreCase("colour"))
1332 sg.cs = ColourSchemeProperty.getColour(al, value);
1334 else if (key.equalsIgnoreCase("pidThreshold"))
1336 sg.cs.setThreshold(Integer.parseInt(value), true);
1339 else if (key.equalsIgnoreCase("consThreshold"))
1341 sg.cs.setConservationInc(Integer.parseInt(value));
1342 Conservation c = new Conservation("Group",
1343 ResidueProperties.propHash, 3, sg.getSequences(null),
1344 sg.getStartRes(), sg.getEndRes() + 1);
1347 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1349 sg.cs.setConservation(c);
1352 else if (key.equalsIgnoreCase("outlineColour"))
1354 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1356 else if (key.equalsIgnoreCase("displayBoxes"))
1358 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1360 else if (key.equalsIgnoreCase("showUnconserved"))
1362 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1364 else if (key.equalsIgnoreCase("displayText"))
1366 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1368 else if (key.equalsIgnoreCase("colourText"))
1370 sg.setColourText(Boolean.valueOf(value).booleanValue());
1372 else if (key.equalsIgnoreCase("textCol1"))
1374 sg.textColour = new UserColourScheme(value).findColour('A');
1376 else if (key.equalsIgnoreCase("textCol2"))
1378 sg.textColour2 = new UserColourScheme(value).findColour('A');
1380 else if (key.equalsIgnoreCase("textColThreshold"))
1382 sg.thresholdTextColour = Integer.parseInt(value);
1384 else if (key.equalsIgnoreCase("idColour"))
1386 // consider warning if colour doesn't resolve to a real colour
1387 sg.setIdColour((def = new UserColourScheme(value))
1390 else if (key.equalsIgnoreCase("hide"))
1392 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1393 sg.setHidereps(true);
1395 else if (key.equalsIgnoreCase("hidecols"))
1397 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1398 sg.setHideCols(true);
1400 sg.recalcConservation();
1409 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1412 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1416 .print("Warning - no annotation to set below for sequence associated annotation:");
1418 while (st.hasMoreTokens())
1420 token = st.nextToken();
1423 System.err.print(" " + token);
1427 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1429 aa = al.getAlignmentAnnotation()[i];
1430 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1432 aa.belowAlignment = true;
1439 System.err.print("\n");
1443 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1445 String keyValue, key, value;
1446 while (st.hasMoreTokens())
1448 keyValue = st.nextToken();
1449 key = keyValue.substring(0, keyValue.indexOf("="));
1450 value = keyValue.substring(keyValue.indexOf("=") + 1);
1451 al.setProperty(key, value);
1456 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1459 * @param annotations
1460 * @return CSV file as a string.
1462 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1464 StringBuffer sp = new StringBuffer();
1465 for (int i = 0; i < annotations.length; i++)
1467 String atos = annotations[i].toString();
1471 int cp = atos.indexOf("\n", p);
1472 sp.append(annotations[i].label);
1476 sp.append(atos.substring(p, cp + 1));
1480 sp.append(atos.substring(p));
1486 return sp.toString();