2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
\r
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
24 import jalview.analysis.*;
\r
25 import jalview.datamodel.*;
\r
26 import jalview.schemes.*;
\r
28 public class AnnotationFile
\r
30 StringBuffer text = new StringBuffer("JALVIEW_ANNOTATION\n"
\r
31 + "# Created: " + new java.util.Date() + "\n\n");
\r
34 * convenience method for pre-2.4 feature files which have no view, hidden
\r
35 * columns or hidden row keywords.
\r
37 * @param annotations
\r
40 * @return feature file as a string.
\r
42 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
43 Vector groups, Hashtable properties)
\r
45 return printAnnotations(annotations, groups, properties, null);
\r
50 * hold all the information about a particular view definition read from or
\r
51 * written out in an annotations file.
\r
53 public class ViewDef
\r
55 public String viewname;
\r
57 public HiddenSequences hidseqs;
\r
59 public ColumnSelection hiddencols;
\r
61 public Vector visibleGroups;
\r
63 public Hashtable hiddenRepSeqs;
\r
65 public ViewDef(String viewname, HiddenSequences hidseqs,
\r
66 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
\r
68 this.viewname = viewname;
\r
69 this.hidseqs = hidseqs;
\r
70 this.hiddencols = hiddencols;
\r
71 this.hiddenRepSeqs = hiddenRepSeqs;
\r
76 * Prepare an annotation file given a set of annotations, groups, alignment
\r
77 * properties and views.
\r
79 * @param annotations
\r
83 * @return annotation file
\r
85 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
86 Vector groups, Hashtable properties, ViewDef[] views)
\r
88 // TODO: resolve views issue : annotationFile could contain visible region,
\r
89 // or full data + hidden region specifications for a view.
\r
90 if (annotations != null)
\r
92 boolean oneColour = true;
\r
93 AlignmentAnnotation row;
\r
95 SequenceI refSeq = null;
\r
96 SequenceGroup refGroup = null;
\r
98 StringBuffer colours = new StringBuffer();
\r
99 StringBuffer graphLine = new StringBuffer();
\r
100 StringBuffer rowprops = new StringBuffer();
\r
101 Hashtable graphGroup = new Hashtable();
\r
103 java.awt.Color color;
\r
105 for (int i = 0; i < annotations.length; i++)
\r
107 row = annotations[i];
\r
109 if (!row.visible && !row.hasScore())
\r
117 if (row.sequenceRef == null)
\r
119 if (refSeq != null)
\r
121 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
129 if (refSeq == null || refSeq != row.sequenceRef)
\r
131 refSeq = row.sequenceRef;
\r
132 text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");
\r
135 // mark any group references for the row
\r
136 if (row.groupRef == null)
\r
139 if (refGroup != null)
\r
141 text.append("\nGROUP_REF\tALIGNMENT\n");
\r
148 if (refGroup == null || refGroup != row.groupRef)
\r
150 refGroup = row.groupRef;
\r
151 text.append("\nGROUP_REF\t" + refGroup.getName() + "\n");
\r
155 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
\r
156 // lookahead to check what the annotation row object actually contains.
\r
157 for (int j = 0; row.annotations != null
\r
158 && j < row.annotations.length
\r
159 && (!hasGlyphs || !hasLabels || !hasValues); j++)
\r
161 if (row.annotations[j] != null)
\r
163 hasLabels |= (row.annotations[j].displayCharacter != null
\r
164 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
\r
166 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
\r
167 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
\r
170 hasText |= (row.annotations[j].description != null && row.annotations[j].description
\r
175 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
177 text.append("NO_GRAPH\t");
\r
178 hasValues = false; // only secondary structure
\r
179 // hasLabels = false; // and annotation description string.
\r
183 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
185 text.append("BAR_GRAPH\t");
\r
186 hasGlyphs = false; // no secondary structure
\r
189 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
191 hasGlyphs = false; // no secondary structure
\r
192 text.append("LINE_GRAPH\t");
\r
195 if (row.getThreshold() != null)
\r
198 .append("GRAPHLINE\t"
\r
201 + row.getThreshold().value
\r
203 + row.getThreshold().label
\r
205 + jalview.util.Format.getHexString(row
\r
206 .getThreshold().colour) + "\n");
\r
209 if (row.graphGroup > -1)
\r
211 String key = String.valueOf(row.graphGroup);
\r
212 if (graphGroup.containsKey(key))
\r
214 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
\r
218 graphGroup.put(key, row.label);
\r
223 text.append(row.label + "\t");
\r
224 if (row.description != null)
\r
226 text.append(row.description + "\t");
\r
228 for (int j = 0; row.annotations != null
\r
229 && j < row.annotations.length; j++)
\r
232 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
\r
237 if (row.annotations[j] != null)
\r
240 if (hasGlyphs) // could be also hasGlyphs || ...
\r
243 text.append(comma);
\r
244 if (row.annotations[j].secondaryStructure != ' ')
\r
246 // only write out the field if its not whitespace.
\r
247 text.append(row.annotations[j].secondaryStructure);
\r
253 if (row.annotations[j].value != Float.NaN)
\r
255 text.append(comma + row.annotations[j].value);
\r
259 System.err.println("Skipping NaN - not valid value.");
\r
260 text.append(comma + 0f);// row.annotations[j].value);
\r
266 // TODO: labels are emitted after values for bar graphs.
\r
267 if // empty labels are allowed, so
\r
268 (row.annotations[j].displayCharacter != null
\r
269 && row.annotations[j].displayCharacter.length() > 0
\r
270 && !row.annotations[j].displayCharacter.equals(" "))
\r
272 text.append(comma + row.annotations[j].displayCharacter);
\r
278 if (row.annotations[j].description != null
\r
279 && row.annotations[j].description.length() > 0
\r
280 && !row.annotations[j].description
\r
281 .equals(row.annotations[j].displayCharacter))
\r
283 text.append(comma + row.annotations[j].description);
\r
287 if (color != null && !color.equals(row.annotations[j].colour))
\r
292 color = row.annotations[j].colour;
\r
294 if (row.annotations[j].colour != null
\r
295 && row.annotations[j].colour != java.awt.Color.black)
\r
299 + jalview.util.Format
\r
300 .getHexString(row.annotations[j].colour)
\r
308 if (row.hasScore())
\r
309 text.append("\t" + row.score);
\r
313 if (color != null && color != java.awt.Color.black && oneColour)
\r
315 colours.append("COLOUR\t" + row.label + "\t"
\r
316 + jalview.util.Format.getHexString(color) + "\n");
\r
318 if (row.scaleColLabel || row.showAllColLabels
\r
319 || row.centreColLabels)
\r
321 rowprops.append("ROWPROPERTIES\t" + row.label);
\r
322 rowprops.append("\tscaletofit=" + row.scaleColLabel);
\r
323 rowprops.append("\tshowalllabs=" + row.showAllColLabels);
\r
324 rowprops.append("\tcentrelabs=" + row.centreColLabels);
\r
325 rowprops.append("\n");
\r
331 text.append(colours.toString());
\r
332 text.append(graphLine.toString());
\r
333 if (graphGroup.size() > 0)
\r
335 text.append("COMBINE\t");
\r
336 Enumeration en = graphGroup.elements();
\r
337 while (en.hasMoreElements())
\r
339 text.append(en.nextElement() + "\n");
\r
342 text.append(rowprops.toString());
\r
345 if (groups != null)
\r
347 printGroups(groups);
\r
350 if (properties != null)
\r
352 text.append("\n\nALIGNMENT");
\r
353 Enumeration en = properties.keys();
\r
354 while (en.hasMoreElements())
\r
356 String key = en.nextElement().toString();
\r
357 text.append("\t" + key + "=" + properties.get(key));
\r
359 // TODO: output alignment visualization settings here if required
\r
363 return text.toString();
\r
366 public void printGroups(Vector sequenceGroups)
\r
369 SequenceI seqrep = null;
\r
370 for (int i = 0; i < sequenceGroups.size(); i++)
\r
372 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
373 if (!sg.hasSeqrep())
\r
375 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
\r
376 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
\r
382 seqrep = sg.getSeqrep();
\r
383 text.append("SEQUENCE_REF\t" + seqrep.getName() + "\n");
\r
384 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
\r
385 + (seqrep.findPosition(sg.getStartRes())) + "\t"
\r
386 + (seqrep.findPosition(sg.getEndRes())) + "\t" + "-1\t");
\r
388 for (int s = 0; s < sg.getSize(); s++)
\r
390 text.append(sg.getSequenceAt(s).getName() + "\t");
\r
393 text.append("\nPROPERTIES\t" + sg.getName() + "\t");
\r
395 if (sg.getDescription() != null)
\r
397 text.append("description=" + sg.getDescription() + "\t");
\r
401 text.append("colour=" + ColourSchemeProperty.getColourName(sg.cs)
\r
403 if (sg.cs.getThreshold() != 0)
\r
405 text.append("pidThreshold=" + sg.cs.getThreshold());
\r
407 if (sg.cs.conservationApplied())
\r
409 text.append("consThreshold=" + sg.cs.getConservationInc() + "\t");
\r
412 text.append("outlineColour="
\r
413 + jalview.util.Format.getHexString(sg.getOutlineColour())
\r
416 text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");
\r
417 text.append("displayText=" + sg.getDisplayText() + "\t");
\r
418 text.append("colourText=" + sg.getColourText() + "\t");
\r
419 text.append("showUnconserved=" + sg.getShowNonconserved() + "\t");
\r
420 if (sg.textColour != java.awt.Color.black)
\r
422 text.append("textCol1="
\r
423 + jalview.util.Format.getHexString(sg.textColour) + "\t");
\r
425 if (sg.textColour2 != java.awt.Color.white)
\r
427 text.append("textCol2="
\r
428 + jalview.util.Format.getHexString(sg.textColour2) + "\t");
\r
430 if (sg.thresholdTextColour != 0)
\r
432 text.append("textColThreshold=" + sg.thresholdTextColour + "\t");
\r
434 if (sg.idColour != null)
\r
436 text.append("idColour="
\r
437 + jalview.util.Format.getHexString(sg.idColour) + "\t");
\r
439 if (sg.isHidereps())
\r
441 text.append("hide=true\t");
\r
443 if (sg.isHideCols())
\r
445 text.append("hidecols=true\t");
\r
447 if (seqrep != null)
\r
449 // terminate the last line and clear the sequence ref for the group
\r
450 text.append("\nSEQUENCE_REF");
\r
452 text.append("\n\n");
\r
457 SequenceI refSeq = null;
\r
459 String refSeqId = null;
\r
461 public boolean readAnnotationFile(AlignmentI al, String file,
\r
464 BufferedReader in = null;
\r
467 if (protocol.equals(AppletFormatAdapter.FILE))
\r
469 in = new BufferedReader(new FileReader(file));
\r
471 else if (protocol.equals(AppletFormatAdapter.URL))
\r
473 URL url = new URL(file);
\r
474 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
476 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
478 in = new BufferedReader(new StringReader(file));
\r
480 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
482 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
485 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
490 return parseAnnotationFrom(al, in);
\r
493 } catch (Exception ex)
\r
495 ex.printStackTrace();
\r
496 System.out.println("Problem reading annotation file: " + ex);
\r
502 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
\r
505 String groupRef = null;
\r
506 Hashtable groupRefRows = new Hashtable();
\r
508 Hashtable autoAnnots = new Hashtable();
\r
510 String line, label, description, token;
\r
511 int graphStyle, index;
\r
512 int refSeqIndex = 1;
\r
513 int existingAnnotations = 0;
\r
514 // when true - will add new rows regardless of whether they are duplicate
\r
515 // auto-annotation like consensus or conservation graphs
\r
516 boolean overrideAutoAnnot = false;
\r
517 if (al.getAlignmentAnnotation() != null)
\r
519 existingAnnotations = al.getAlignmentAnnotation().length;
\r
520 if (existingAnnotations > 0)
\r
522 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
\r
523 for (int aai = 0; aai < aa.length; aai++)
\r
525 if (aa[aai].autoCalculated)
\r
527 // make a note of the name and description
\r
533 + aa[aai].description
\r
535 + (aa[aai].sequenceRef != null ? aa[aai].sequenceRef
\r
536 .getDisplayId(true) : ""),
\r
543 int alWidth = al.getWidth();
\r
545 StringTokenizer st;
\r
546 Annotation[] annotations;
\r
547 AlignmentAnnotation annotation = null;
\r
549 // First confirm this is an Annotation file
\r
550 boolean jvAnnotationFile = false;
\r
551 while ((line = in.readLine()) != null)
\r
553 if (line.indexOf("#") == 0)
\r
558 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
560 jvAnnotationFile = true;
\r
565 if (!jvAnnotationFile)
\r
571 while ((line = in.readLine()) != null)
\r
573 if (line.indexOf("#") == 0
\r
574 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
575 || line.length() == 0)
\r
580 st = new StringTokenizer(line, "\t");
\r
581 token = st.nextToken();
\r
582 if (token.equalsIgnoreCase("COLOUR"))
\r
584 // TODO: use graduated colour def'n here too
\r
585 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
589 else if (token.equalsIgnoreCase("COMBINE"))
\r
591 combineAnnotations(al, st);
\r
594 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
\r
596 addRowProperties(al, st);
\r
599 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
605 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
607 if (st.hasMoreTokens())
\r
609 refSeq = al.findName(refSeqId = st.nextToken());
\r
610 if (refSeq == null)
\r
616 refSeqIndex = Integer.parseInt(st.nextToken());
\r
617 if (refSeqIndex < 1)
\r
621 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
623 } catch (Exception ex)
\r
635 else if (token.equalsIgnoreCase("GROUP_REF"))
\r
637 // Group references could be forward or backwards, so they are
\r
638 // resolved after the whole file is read in
\r
640 if (st.hasMoreTokens())
\r
642 groupRef = st.nextToken();
\r
643 if (groupRef.length() < 1)
\r
645 groupRef = null; // empty string
\r
649 if (groupRefRows.get(groupRef) == null)
\r
651 groupRefRows.put(groupRef, new Vector());
\r
657 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
663 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
665 addProperties(al, st);
\r
669 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
\r
671 setBelowAlignment(al, st);
\r
674 else if (token.equalsIgnoreCase("ALIGNMENT"))
\r
676 addAlignmentDetails(al, st);
\r
680 // Parse out the annotation row
\r
681 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
682 label = st.nextToken();
\r
685 annotations = new Annotation[alWidth];
\r
686 description = null;
\r
687 float score = Float.NaN;
\r
689 if (st.hasMoreTokens())
\r
691 line = st.nextToken();
\r
693 if (line.indexOf("|") == -1)
\r
695 description = line;
\r
696 if (st.hasMoreTokens())
\r
697 line = st.nextToken();
\r
700 if (st.hasMoreTokens())
\r
702 // This must be the score
\r
703 score = Float.valueOf(st.nextToken()).floatValue();
\r
706 st = new StringTokenizer(line, "|", true);
\r
708 boolean emptyColumn = true;
\r
709 boolean onlyOneElement = (st.countTokens() == 1);
\r
711 while (st.hasMoreElements() && index < alWidth)
\r
713 token = st.nextToken().trim();
\r
715 if (onlyOneElement)
\r
719 score = Float.valueOf(token).floatValue();
\r
721 } catch (NumberFormatException ex)
\r
726 if (token.equals("|"))
\r
733 emptyColumn = true;
\r
737 annotations[index++] = parseAnnotation(token, graphStyle);
\r
738 emptyColumn = false;
\r
744 annotation = new AlignmentAnnotation(label, description,
\r
745 (index == 0) ? null : annotations, 0, 0, graphStyle);
\r
747 annotation.score = score;
\r
748 if (!overrideAutoAnnot
\r
750 .containsKey(annotation.graph
\r
754 + annotation.description
\r
756 + (refSeq != null ? refSeq
\r
757 .getDisplayId(true) : "")))
\r
759 // skip - we've already got an automatic annotation of this type.
\r
762 // otherwise add it!
\r
763 if (refSeq != null)
\r
766 annotation.belowAlignment = false;
\r
767 // make a copy of refSeq so we can find other matches in the alignment
\r
768 SequenceI referedSeq = refSeq;
\r
771 // copy before we do any mapping business.
\r
772 // TODO: verify that undo/redo with 1:many sequence associated
\r
773 // annotations can be undone correctly
\r
774 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
\r
776 .createSequenceMapping(referedSeq, refSeqIndex, false);
\r
777 annotation.adjustForAlignment();
\r
778 referedSeq.addAlignmentAnnotation(annotation);
\r
779 al.addAnnotation(annotation);
\r
780 al.setAnnotationIndex(annotation,
\r
781 al.getAlignmentAnnotation().length
\r
782 - existingAnnotations - 1);
\r
783 if (groupRef != null)
\r
785 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
\r
787 // and recover our virgin copy to use again if necessary.
\r
790 } while (refSeqId != null
\r
791 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
\r
795 al.addAnnotation(annotation);
\r
796 al.setAnnotationIndex(annotation,
\r
797 al.getAlignmentAnnotation().length - existingAnnotations
\r
799 if (groupRef != null)
\r
801 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
\r
805 // Finally, resolve the groupRefs
\r
806 Enumeration en = groupRefRows.keys();
\r
807 SequenceGroup theGroup = null;
\r
809 while (en.hasMoreElements())
\r
811 groupRef = (String) en.nextElement();
\r
812 boolean matched = false;
\r
813 // Resolve group: TODO: add a getGroupByName method to alignments
\r
814 Vector grps = al.getGroups();
\r
815 for (int g = 0, gSize = grps.size(); g < gSize; g++)
\r
817 theGroup = (SequenceGroup) grps.elementAt(g);
\r
818 if (theGroup.getName().equals(groupRef))
\r
822 // TODO: specify and implement duplication of alignment annotation
\r
823 // for multiple group references.
\r
825 .println("Ignoring 1:many group reference mappings for group name '"
\r
831 Vector rowset = (Vector) groupRefRows.get(groupRef);
\r
832 if (rowset != null && rowset.size() > 0)
\r
834 AlignmentAnnotation alan = null;
\r
835 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
\r
837 alan = (AlignmentAnnotation) rowset.elementAt(elm);
\r
838 alan.groupRef = theGroup;
\r
844 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
\r
850 Annotation parseAnnotation(String string, int graphStyle)
\r
852 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
\r
861 String desc = null, displayChar = null;
\r
862 char ss = ' '; // secondaryStructure
\r
864 boolean parsedValue = false, dcset = false;
\r
866 // find colour here
\r
867 java.awt.Color colour = null;
\r
868 int i = string.indexOf("[");
\r
869 int j = string.indexOf("]");
\r
870 if (i > -1 && j > -1)
\r
872 UserColourScheme ucs = new UserColourScheme();
\r
874 colour = ucs.getColourFromString(string.substring(i + 1, j));
\r
875 if (i > 0 && string.charAt(i - 1) == ',')
\r
877 // clip the preceding comma as well
\r
880 string = string.substring(0, i) + string.substring(j + 1);
\r
883 StringTokenizer st = new StringTokenizer(string, ",", true);
\r
885 boolean seenContent = false;
\r
887 while (st.hasMoreTokens())
\r
890 token = st.nextToken().trim();
\r
891 if (token.equals(","))
\r
893 if (!seenContent && parsedValue && !dcset)
\r
895 // allow the value below the bar/line to be empty
\r
899 seenContent = false;
\r
904 seenContent = true;
\r
911 displayChar = token;
\r
913 value = new Float(token).floatValue();
\r
914 parsedValue = true;
\r
916 } catch (NumberFormatException ex)
\r
922 if (token.length() == 1)
\r
924 displayChar = token;
\r
928 && (token.equals("H") || token.equals("E") || token
\r
931 // Either this character represents a helix or sheet
\r
932 // or an integer which can be displayed
\r
933 ss = token.charAt(0);
\r
934 if (displayChar.equals(token.substring(0, 1)))
\r
939 else if (desc == null || (parsedValue && pass > 2))
\r
945 // if (!dcset && string.charAt(string.length() - 1) == ',')
\r
947 // displayChar = " "; // empty display char symbol.
\r
949 if (displayChar != null && desc != null && desc.length() == 1)
\r
951 if (displayChar.length() > 1)
\r
953 // switch desc and displayChar - legacy support
\r
954 String tmp = displayChar;
\r
955 displayChar = desc;
\r
960 if (displayChar.equals(desc))
\r
962 // duplicate label - hangover from the 'robust parser' above
\r
967 Annotation anot = new Annotation(displayChar, desc, ss, value);
\r
969 anot.colour = colour;
\r
974 void colourAnnotations(AlignmentI al, String label, String colour)
\r
976 UserColourScheme ucs = new UserColourScheme(colour);
\r
977 Annotation[] annotations;
\r
978 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
980 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
982 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
983 for (int j = 0; j < annotations.length; j++)
\r
985 if (annotations[j] != null)
\r
987 annotations[j].colour = ucs.findColour('A');
\r
994 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
996 int graphGroup = -1;
\r
997 String group = st.nextToken();
\r
998 // First make sure we are not overwriting the graphIndex
\r
999 if (al.getAlignmentAnnotation() != null)
\r
1001 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1003 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
1005 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
1006 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
1011 // Now update groups
\r
1012 while (st.hasMoreTokens())
\r
1014 group = st.nextToken();
\r
1015 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1017 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
1019 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
1028 .println("Couldn't combine annotations. None are added to alignment yet!");
\r
1032 void addLine(AlignmentI al, StringTokenizer st)
\r
1034 String group = st.nextToken();
\r
1035 AlignmentAnnotation annotation = null, alannot[] = al
\r
1036 .getAlignmentAnnotation();
\r
1037 if (alannot != null)
\r
1039 for (int i = 0; i < alannot.length; i++)
\r
1041 if (alannot[i].label.equalsIgnoreCase(group))
\r
1043 annotation = alannot[i];
\r
1048 if (annotation == null)
\r
1052 float value = new Float(st.nextToken()).floatValue();
\r
1053 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
1054 java.awt.Color colour = null;
\r
1055 if (st.hasMoreTokens())
\r
1057 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
1058 colour = ucs.findColour('A');
\r
1061 annotation.setThreshold(new GraphLine(value, label, colour));
\r
1064 void addGroup(AlignmentI al, StringTokenizer st)
\r
1066 SequenceGroup sg = new SequenceGroup();
\r
1067 sg.setName(st.nextToken());
\r
1071 rng = st.nextToken();
\r
1072 if (rng.length() > 0 && !rng.startsWith("*"))
\r
1074 sg.setStartRes(Integer.parseInt(rng) - 1);
\r
1078 sg.setStartRes(0);
\r
1080 rng = st.nextToken();
\r
1081 if (rng.length() > 0 && !rng.startsWith("*"))
\r
1083 sg.setEndRes(Integer.parseInt(rng) - 1);
\r
1087 sg.setEndRes(al.getWidth() - 1);
\r
1089 } catch (Exception e)
\r
1092 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
\r
1093 + rng + "' - assuming alignment width for group.");
\r
1094 // assume group is full width
\r
1095 sg.setStartRes(0);
\r
1096 sg.setEndRes(al.getWidth() - 1);
\r
1099 String index = st.nextToken();
\r
1100 if (index.equals("-1"))
\r
1102 while (st.hasMoreElements())
\r
1104 sg.addSequence(al.findName(st.nextToken()), false);
\r
1109 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
1111 while (st2.hasMoreTokens())
\r
1113 String tmp = st2.nextToken();
\r
1114 if (tmp.equals("*"))
\r
1116 for (int i = 0; i < al.getHeight(); i++)
\r
1118 sg.addSequence(al.getSequenceAt(i), false);
\r
1121 else if (tmp.indexOf("-") >= 0)
\r
1123 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
1125 int start = (Integer.parseInt(st3.nextToken()));
\r
1126 int end = (Integer.parseInt(st3.nextToken()));
\r
1130 for (int i = start; i <= end; i++)
\r
1132 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
1138 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
1143 if (refSeq != null)
\r
1145 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
1146 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
1147 sg.setSeqrep(refSeq);
\r
1150 if (sg.getSize() > 0)
\r
1156 void addRowProperties(AlignmentI al, StringTokenizer st)
\r
1158 String label = st.nextToken(), keyValue, key, value;
\r
1159 boolean scaletofit = false, centerlab = false, showalllabs = false;
\r
1160 while (st.hasMoreTokens())
\r
1162 keyValue = st.nextToken();
\r
1163 key = keyValue.substring(0, keyValue.indexOf("="));
\r
1164 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
1165 if (key.equalsIgnoreCase("scaletofit"))
\r
1167 scaletofit = Boolean.valueOf(value).booleanValue();
\r
1169 if (key.equalsIgnoreCase("showalllabs"))
\r
1171 showalllabs = Boolean.valueOf(value).booleanValue();
\r
1173 if (key.equalsIgnoreCase("centrelabs"))
\r
1175 centerlab = Boolean.valueOf(value).booleanValue();
\r
1177 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
\r
1180 for (int i = 0; i < alr.length; i++)
\r
1182 if (alr[i].label.equalsIgnoreCase(label))
\r
1184 alr[i].centreColLabels = centerlab;
\r
1185 alr[i].scaleColLabel = scaletofit;
\r
1186 alr[i].showAllColLabels = showalllabs;
\r
1193 void addProperties(AlignmentI al, StringTokenizer st)
\r
1196 // So far we have only added groups to the annotationHash,
\r
1197 // the idea is in the future properties can be added to
\r
1198 // alignments, other annotations etc
\r
1199 if (al.getGroups() == null)
\r
1203 SequenceGroup sg = null;
\r
1205 String name = st.nextToken();
\r
1207 Vector groups = al.getGroups();
\r
1208 for (int i = 0; i < groups.size(); i++)
\r
1210 sg = (SequenceGroup) groups.elementAt(i);
\r
1211 if (sg.getName().equals(name))
\r
1223 String keyValue, key, value;
\r
1224 ColourSchemeI def = sg.cs;
\r
1226 while (st.hasMoreTokens())
\r
1228 keyValue = st.nextToken();
\r
1229 key = keyValue.substring(0, keyValue.indexOf("="));
\r
1230 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
1232 if (key.equalsIgnoreCase("description"))
\r
1234 sg.setDescription(value);
\r
1236 else if (key.equalsIgnoreCase("colour"))
\r
1238 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
1240 else if (key.equalsIgnoreCase("pidThreshold"))
\r
1242 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
1245 else if (key.equalsIgnoreCase("consThreshold"))
\r
1247 sg.cs.setConservationInc(Integer.parseInt(value));
\r
1248 Conservation c = new Conservation("Group",
\r
1249 ResidueProperties.propHash, 3, sg.getSequences(null),
\r
1250 sg.getStartRes(), sg.getEndRes() + 1);
\r
1253 c.verdict(false, 25);
\r
1255 sg.cs.setConservation(c);
\r
1258 else if (key.equalsIgnoreCase("outlineColour"))
\r
1260 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
1262 else if (key.equalsIgnoreCase("displayBoxes"))
\r
1264 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
1266 else if (key.equalsIgnoreCase("showUnconserved"))
\r
1268 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
\r
1270 else if (key.equalsIgnoreCase("displayText"))
\r
1272 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
1274 else if (key.equalsIgnoreCase("colourText"))
\r
1276 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
1278 else if (key.equalsIgnoreCase("textCol1"))
\r
1280 sg.textColour = new UserColourScheme(value).findColour('A');
\r
1282 else if (key.equalsIgnoreCase("textCol2"))
\r
1284 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
1286 else if (key.equalsIgnoreCase("textColThreshold"))
\r
1288 sg.thresholdTextColour = Integer.parseInt(value);
\r
1290 else if (key.equalsIgnoreCase("idColour"))
\r
1292 // consider warning if colour doesn't resolve to a real colour
\r
1293 sg.setIdColour((def = new UserColourScheme(value))
\r
1294 .findColour('A'));
\r
1296 else if (key.equalsIgnoreCase("hide"))
\r
1298 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
\r
1299 sg.setHidereps(true);
\r
1301 else if (key.equalsIgnoreCase("hidecols"))
\r
1303 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
\r
1304 sg.setHideCols(true);
\r
1306 sg.recalcConservation();
\r
1308 if (sg.cs == null)
\r
1315 void setBelowAlignment(AlignmentI al, StringTokenizer st)
\r
1318 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
\r
1322 .print("Warning - no annotation to set below for sequence associated annotation:");
\r
1324 while (st.hasMoreTokens())
\r
1326 token = st.nextToken();
\r
1329 System.err.print(" " + token);
\r
1333 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1335 aa = al.getAlignmentAnnotation()[i];
\r
1336 if (aa.sequenceRef == refSeq && aa.label.equals(token))
\r
1338 aa.belowAlignment = true;
\r
1345 System.err.print("\n");
\r
1349 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
\r
1351 String keyValue, key, value;
\r
1352 while (st.hasMoreTokens())
\r
1354 keyValue = st.nextToken();
\r
1355 key = keyValue.substring(0, keyValue.indexOf("="));
\r
1356 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
1357 al.setProperty(key, value);
\r
1362 * Write annotations as a CSV file of the form 'label, value, value, ...' for
\r
1365 * @param annotations
\r
1366 * @return CSV file as a string.
\r
1368 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
\r
1370 StringBuffer sp = new StringBuffer();
\r
1371 for (int i = 0; i < annotations.length; i++)
\r
1373 String atos = annotations[i].toString();
\r
1377 int cp = atos.indexOf("\n", p);
\r
1378 sp.append(annotations[i].label);
\r
1382 sp.append(atos.substring(p, cp + 1));
\r
1386 sp.append(atos.substring(p));
\r
1392 return sp.toString();
\r