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);
587 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
590 boolean modified = false;
591 String groupRef = null;
592 Hashtable groupRefRows = new Hashtable();
594 Hashtable autoAnnots = new Hashtable();
596 String line, label, description, token;
597 int graphStyle, index;
599 int existingAnnotations = 0;
600 // when true - will add new rows regardless of whether they are duplicate
601 // auto-annotation like consensus or conservation graphs
602 boolean overrideAutoAnnot = false;
603 if (al.getAlignmentAnnotation() != null)
605 existingAnnotations = al.getAlignmentAnnotation().length;
606 if (existingAnnotations > 0)
608 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
609 for (int aai = 0; aai < aa.length; aai++)
611 if (aa[aai].autoCalculated)
613 // make a note of the name and description
615 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
616 (aa[aai].groupRef == null ? null
617 : aa[aai].groupRef.getName())),
624 int alWidth = al.getWidth();
627 Annotation[] annotations;
628 AlignmentAnnotation annotation = null;
630 // First confirm this is an Annotation file
631 boolean jvAnnotationFile = false;
632 while ((line = in.readLine()) != null)
634 if (line.indexOf("#") == 0)
639 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
641 jvAnnotationFile = true;
646 if (!jvAnnotationFile)
652 while ((line = in.readLine()) != null)
654 if (line.indexOf("#") == 0
655 || line.indexOf("JALVIEW_ANNOTATION") > -1
656 || line.length() == 0)
661 st = new StringTokenizer(line, "\t");
662 token = st.nextToken();
663 if (token.equalsIgnoreCase("COLOUR"))
665 // TODO: use graduated colour def'n here too
666 colourAnnotations(al, st.nextToken(), st.nextToken());
671 else if (token.equalsIgnoreCase("COMBINE"))
673 combineAnnotations(al, st);
677 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
679 addRowProperties(al, st);
683 else if (token.equalsIgnoreCase("GRAPHLINE"))
690 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
692 if (st.hasMoreTokens())
694 refSeq = al.findName(refSeqId = st.nextToken());
701 refSeqIndex = Integer.parseInt(st.nextToken());
706 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
708 } catch (Exception ex)
720 else if (token.equalsIgnoreCase("GROUP_REF"))
722 // Group references could be forward or backwards, so they are
723 // resolved after the whole file is read in
725 if (st.hasMoreTokens())
727 groupRef = st.nextToken();
728 if (groupRef.length() < 1)
730 groupRef = null; // empty string
734 if (groupRefRows.get(groupRef) == null)
736 groupRefRows.put(groupRef, new Vector());
742 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
748 else if (token.equalsIgnoreCase("PROPERTIES"))
750 addProperties(al, st);
755 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
757 setBelowAlignment(al, st);
761 else if (token.equalsIgnoreCase("ALIGNMENT"))
763 addAlignmentDetails(al, st);
768 // Parse out the annotation row
769 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
770 label = st.nextToken();
773 annotations = new Annotation[alWidth];
775 float score = Float.NaN;
777 if (st.hasMoreTokens())
779 line = st.nextToken();
781 if (line.indexOf("|") == -1)
784 if (st.hasMoreTokens())
785 line = st.nextToken();
788 if (st.hasMoreTokens())
790 // This must be the score
791 score = Float.valueOf(st.nextToken()).floatValue();
794 st = new StringTokenizer(line, "|", true);
796 boolean emptyColumn = true;
797 boolean onlyOneElement = (st.countTokens() == 1);
799 while (st.hasMoreElements() && index < alWidth)
801 token = st.nextToken().trim();
807 score = Float.valueOf(token).floatValue();
809 } catch (NumberFormatException ex)
814 if (token.equals("|"))
825 annotations[index++] = parseAnnotation(token, graphStyle);
832 annotation = new AlignmentAnnotation(label, description,
833 (index == 0) ? null : annotations, 0, 0, graphStyle);
835 annotation.score = score;
836 if (!overrideAutoAnnot
837 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
840 // skip - we've already got an automatic annotation of this type.
847 annotation.belowAlignment = false;
848 // make a copy of refSeq so we can find other matches in the alignment
849 SequenceI referedSeq = refSeq;
852 // copy before we do any mapping business.
853 // TODO: verify that undo/redo with 1:many sequence associated
854 // annotations can be undone correctly
855 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
857 .createSequenceMapping(referedSeq, refSeqIndex, false);
858 annotation.adjustForAlignment();
859 referedSeq.addAlignmentAnnotation(annotation);
860 al.addAnnotation(annotation);
861 al.setAnnotationIndex(annotation,
862 al.getAlignmentAnnotation().length
863 - existingAnnotations - 1);
864 if (groupRef != null)
866 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
868 // and recover our virgin copy to use again if necessary.
871 } while (refSeqId != null
872 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
876 al.addAnnotation(annotation);
877 al.setAnnotationIndex(annotation,
878 al.getAlignmentAnnotation().length - existingAnnotations
880 if (groupRef != null)
882 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
885 // and set modification flag
888 // Finally, resolve the groupRefs
889 Enumeration en = groupRefRows.keys();
891 while (en.hasMoreElements())
893 groupRef = (String) en.nextElement();
894 boolean matched = false;
895 // Resolve group: TODO: add a getGroupByName method to alignments
896 for (SequenceGroup theGroup : al.getGroups())
898 if (theGroup.getName().equals(groupRef))
902 // TODO: specify and implement duplication of alignment annotation
903 // for multiple group references.
905 .println("Ignoring 1:many group reference mappings for group name '"
911 Vector rowset = (Vector) groupRefRows.get(groupRef);
912 if (rowset != null && rowset.size() > 0)
914 AlignmentAnnotation alan = null;
915 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
917 alan = (AlignmentAnnotation) rowset.elementAt(elm);
918 alan.groupRef = theGroup;
924 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
930 private Object autoAnnotsKey(AlignmentAnnotation annotation,
931 SequenceI refSeq, String groupRef)
933 return annotation.graph + "\t" + annotation.label + "\t"
934 + annotation.description + "\t"
935 + (refSeq != null ? refSeq.getDisplayId(true) : "");
938 Annotation parseAnnotation(String string, int graphStyle)
940 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
949 String desc = null, displayChar = null;
950 char ss = ' '; // secondaryStructure
952 boolean parsedValue = false, dcset = false;
955 java.awt.Color colour = null;
956 int i = string.indexOf("[");
957 int j = string.indexOf("]");
958 if (i > -1 && j > -1)
960 UserColourScheme ucs = new UserColourScheme();
962 colour = ucs.getColourFromString(string.substring(i + 1, j));
963 if (i > 0 && string.charAt(i - 1) == ',')
965 // clip the preceding comma as well
968 string = string.substring(0, i) + string.substring(j + 1);
971 StringTokenizer st = new StringTokenizer(string, ",", true);
973 boolean seenContent = false;
975 while (st.hasMoreTokens())
978 token = st.nextToken().trim();
979 if (token.equals(","))
981 if (!seenContent && parsedValue && !dcset)
983 // allow the value below the bar/line to be empty
1001 value = new Float(token).floatValue();
1004 } catch (NumberFormatException ex)
1010 if (token.length() == 1)
1012 displayChar = token;
1016 && (token.equals("H") || token.equals("E")
1017 || token.equals("S") || token.equals(" ")))
1019 // Either this character represents a helix or sheet
1020 // or an integer which can be displayed
1021 ss = token.charAt(0);
1022 if (displayChar.equals(token.substring(0, 1)))
1027 else if (desc == null || (parsedValue && pass > 2))
1033 // if (!dcset && string.charAt(string.length() - 1) == ',')
1035 // displayChar = " "; // empty display char symbol.
1037 if (displayChar != null && desc != null && desc.length() == 1)
1039 if (displayChar.length() > 1)
1041 // switch desc and displayChar - legacy support
1042 String tmp = displayChar;
1048 if (displayChar.equals(desc))
1050 // duplicate label - hangover from the 'robust parser' above
1055 Annotation anot = new Annotation(displayChar, desc, ss, value);
1057 anot.colour = colour;
1062 void colourAnnotations(AlignmentI al, String label, String colour)
1064 UserColourScheme ucs = new UserColourScheme(colour);
1065 Annotation[] annotations;
1066 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1068 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
1070 annotations = al.getAlignmentAnnotation()[i].annotations;
1071 for (int j = 0; j < annotations.length; j++)
1073 if (annotations[j] != null)
1075 annotations[j].colour = ucs.findColour('A');
1082 void combineAnnotations(AlignmentI al, StringTokenizer st)
1084 int graphGroup = -1;
1085 String group = st.nextToken();
1086 // First make sure we are not overwriting the graphIndex
1087 if (al.getAlignmentAnnotation() != null)
1089 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1091 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
1093 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
1094 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
1099 // Now update groups
1100 while (st.hasMoreTokens())
1102 group = st.nextToken();
1103 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1105 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
1107 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
1116 .println("Couldn't combine annotations. None are added to alignment yet!");
1120 void addLine(AlignmentI al, StringTokenizer st)
1122 String group = st.nextToken();
1123 AlignmentAnnotation annotation = null, alannot[] = al
1124 .getAlignmentAnnotation();
1125 if (alannot != null)
1127 for (int i = 0; i < alannot.length; i++)
1129 if (alannot[i].label.equalsIgnoreCase(group))
1131 annotation = alannot[i];
1136 if (annotation == null)
1140 float value = new Float(st.nextToken()).floatValue();
1141 String label = st.hasMoreTokens() ? st.nextToken() : null;
1142 java.awt.Color colour = null;
1143 if (st.hasMoreTokens())
1145 UserColourScheme ucs = new UserColourScheme(st.nextToken());
1146 colour = ucs.findColour('A');
1149 annotation.setThreshold(new GraphLine(value, label, colour));
1152 void addGroup(AlignmentI al, StringTokenizer st)
1154 SequenceGroup sg = new SequenceGroup();
1155 sg.setName(st.nextToken());
1159 rng = st.nextToken();
1160 if (rng.length() > 0 && !rng.startsWith("*"))
1162 sg.setStartRes(Integer.parseInt(rng) - 1);
1168 rng = st.nextToken();
1169 if (rng.length() > 0 && !rng.startsWith("*"))
1171 sg.setEndRes(Integer.parseInt(rng) - 1);
1175 sg.setEndRes(al.getWidth() - 1);
1177 } catch (Exception e)
1180 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
1181 + rng + "' - assuming alignment width for group.");
1182 // assume group is full width
1184 sg.setEndRes(al.getWidth() - 1);
1187 String index = st.nextToken();
1188 if (index.equals("-1"))
1190 while (st.hasMoreElements())
1192 sg.addSequence(al.findName(st.nextToken()), false);
1197 StringTokenizer st2 = new StringTokenizer(index, ",");
1199 while (st2.hasMoreTokens())
1201 String tmp = st2.nextToken();
1202 if (tmp.equals("*"))
1204 for (int i = 0; i < al.getHeight(); i++)
1206 sg.addSequence(al.getSequenceAt(i), false);
1209 else if (tmp.indexOf("-") >= 0)
1211 StringTokenizer st3 = new StringTokenizer(tmp, "-");
1213 int start = (Integer.parseInt(st3.nextToken()));
1214 int end = (Integer.parseInt(st3.nextToken()));
1218 for (int i = start; i <= end; i++)
1220 sg.addSequence(al.getSequenceAt(i - 1), false);
1226 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
1233 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
1234 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
1235 sg.setSeqrep(refSeq);
1238 if (sg.getSize() > 0)
1244 void addRowProperties(AlignmentI al, StringTokenizer st)
1246 String label = st.nextToken(), keyValue, key, value;
1247 boolean scaletofit = false, centerlab = false, showalllabs = false;
1248 while (st.hasMoreTokens())
1250 keyValue = st.nextToken();
1251 key = keyValue.substring(0, keyValue.indexOf("="));
1252 value = keyValue.substring(keyValue.indexOf("=") + 1);
1253 if (key.equalsIgnoreCase("scaletofit"))
1255 scaletofit = Boolean.valueOf(value).booleanValue();
1257 if (key.equalsIgnoreCase("showalllabs"))
1259 showalllabs = Boolean.valueOf(value).booleanValue();
1261 if (key.equalsIgnoreCase("centrelabs"))
1263 centerlab = Boolean.valueOf(value).booleanValue();
1265 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
1268 for (int i = 0; i < alr.length; i++)
1270 if (alr[i].label.equalsIgnoreCase(label))
1272 alr[i].centreColLabels = centerlab;
1273 alr[i].scaleColLabel = scaletofit;
1274 alr[i].showAllColLabels = showalllabs;
1281 void addProperties(AlignmentI al, StringTokenizer st)
1284 // So far we have only added groups to the annotationHash,
1285 // the idea is in the future properties can be added to
1286 // alignments, other annotations etc
1287 if (al.getGroups() == null)
1292 String name = st.nextToken();
1293 SequenceGroup sg = null;
1294 for (SequenceGroup _sg : al.getGroups())
1296 if ((sg = _sg).getName().equals(name))
1308 String keyValue, key, value;
1309 ColourSchemeI def = sg.cs;
1311 while (st.hasMoreTokens())
1313 keyValue = st.nextToken();
1314 key = keyValue.substring(0, keyValue.indexOf("="));
1315 value = keyValue.substring(keyValue.indexOf("=") + 1);
1317 if (key.equalsIgnoreCase("description"))
1319 sg.setDescription(value);
1321 else if (key.equalsIgnoreCase("colour"))
1323 sg.cs = ColourSchemeProperty.getColour(al, value);
1325 else if (key.equalsIgnoreCase("pidThreshold"))
1327 sg.cs.setThreshold(Integer.parseInt(value), true);
1330 else if (key.equalsIgnoreCase("consThreshold"))
1332 sg.cs.setConservationInc(Integer.parseInt(value));
1333 Conservation c = new Conservation("Group",
1334 ResidueProperties.propHash, 3, sg.getSequences(null),
1335 sg.getStartRes(), sg.getEndRes() + 1);
1338 c.verdict(false, 25); // TODO: refer to conservation percent threshold
1340 sg.cs.setConservation(c);
1343 else if (key.equalsIgnoreCase("outlineColour"))
1345 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
1347 else if (key.equalsIgnoreCase("displayBoxes"))
1349 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
1351 else if (key.equalsIgnoreCase("showUnconserved"))
1353 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
1355 else if (key.equalsIgnoreCase("displayText"))
1357 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
1359 else if (key.equalsIgnoreCase("colourText"))
1361 sg.setColourText(Boolean.valueOf(value).booleanValue());
1363 else if (key.equalsIgnoreCase("textCol1"))
1365 sg.textColour = new UserColourScheme(value).findColour('A');
1367 else if (key.equalsIgnoreCase("textCol2"))
1369 sg.textColour2 = new UserColourScheme(value).findColour('A');
1371 else if (key.equalsIgnoreCase("textColThreshold"))
1373 sg.thresholdTextColour = Integer.parseInt(value);
1375 else if (key.equalsIgnoreCase("idColour"))
1377 // consider warning if colour doesn't resolve to a real colour
1378 sg.setIdColour((def = new UserColourScheme(value))
1381 else if (key.equalsIgnoreCase("hide"))
1383 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1384 sg.setHidereps(true);
1386 else if (key.equalsIgnoreCase("hidecols"))
1388 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
1389 sg.setHideCols(true);
1391 sg.recalcConservation();
1400 void setBelowAlignment(AlignmentI al, StringTokenizer st)
1403 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
1407 .print("Warning - no annotation to set below for sequence associated annotation:");
1409 while (st.hasMoreTokens())
1411 token = st.nextToken();
1414 System.err.print(" " + token);
1418 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
1420 aa = al.getAlignmentAnnotation()[i];
1421 if (aa.sequenceRef == refSeq && aa.label.equals(token))
1423 aa.belowAlignment = true;
1430 System.err.print("\n");
1434 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
1436 String keyValue, key, value;
1437 while (st.hasMoreTokens())
1439 keyValue = st.nextToken();
1440 key = keyValue.substring(0, keyValue.indexOf("="));
1441 value = keyValue.substring(keyValue.indexOf("=") + 1);
1442 al.setProperty(key, value);
1447 * Write annotations as a CSV file of the form 'label, value, value, ...' for
1450 * @param annotations
1451 * @return CSV file as a string.
1453 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
1455 StringBuffer sp = new StringBuffer();
1456 for (int i = 0; i < annotations.length; i++)
1458 String atos = annotations[i].toString();
1462 int cp = atos.indexOf("\n", p);
1463 sp.append(annotations[i].label);
1467 sp.append(atos.substring(p, cp + 1));
1471 sp.append(atos.substring(p));
1477 return sp.toString();