2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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 public AnnotationFile()
\r
36 * character used to write newlines
\r
38 protected String newline = System.getProperty("line.separator");
\r
41 * set new line string and reset the output buffer
\r
45 public void setNewlineString(String nl)
\r
51 public String getNewlineString()
\r
60 text = new StringBuffer("JALVIEW_ANNOTATION"+newline + "# Created: "
\r
61 + new java.util.Date() + newline + newline);
\r
67 * convenience method for pre-2.4 feature files which have no view, hidden
\r
68 * columns or hidden row keywords.
\r
70 * @param annotations
\r
73 * @return feature file as a string.
\r
75 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
76 Vector groups, Hashtable properties)
\r
78 return printAnnotations(annotations, groups, properties, null);
\r
83 * hold all the information about a particular view definition read from or
\r
84 * written out in an annotations file.
\r
86 public class ViewDef
\r
88 public String viewname;
\r
90 public HiddenSequences hidseqs;
\r
92 public ColumnSelection hiddencols;
\r
94 public Vector visibleGroups;
\r
96 public Hashtable hiddenRepSeqs;
\r
98 public ViewDef(String viewname, HiddenSequences hidseqs,
\r
99 ColumnSelection hiddencols, Hashtable hiddenRepSeqs)
\r
101 this.viewname = viewname;
\r
102 this.hidseqs = hidseqs;
\r
103 this.hiddencols = hiddencols;
\r
104 this.hiddenRepSeqs = hiddenRepSeqs;
\r
109 * Prepare an annotation file given a set of annotations, groups, alignment
\r
110 * properties and views.
\r
112 * @param annotations
\r
114 * @param properties
\r
116 * @return annotation file
\r
118 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
119 Vector groups, Hashtable properties, ViewDef[] views)
\r
121 // TODO: resolve views issue : annotationFile could contain visible region,
\r
122 // or full data + hidden region specifications for a view.
\r
123 if (annotations != null)
\r
125 boolean oneColour = true;
\r
126 AlignmentAnnotation row;
\r
128 SequenceI refSeq = null;
\r
129 SequenceGroup refGroup = null;
\r
131 StringBuffer colours = new StringBuffer();
\r
132 StringBuffer graphLine = new StringBuffer();
\r
133 StringBuffer rowprops = new StringBuffer();
\r
134 Hashtable graphGroup = new Hashtable();
\r
136 java.awt.Color color;
\r
138 for (int i = 0; i < annotations.length; i++)
\r
140 row = annotations[i];
\r
142 if (!row.visible && !row.hasScore())
\r
150 if (row.sequenceRef == null)
\r
152 if (refSeq != null)
\r
154 text.append(newline);
\r
155 text.append("SEQUENCE_REF\tALIGNMENT");
\r
156 text.append(newline);
\r
164 if (refSeq == null || refSeq != row.sequenceRef)
\r
166 refSeq = row.sequenceRef;
\r
167 text.append(newline);
\r
168 text.append("SEQUENCE_REF\t");
\r
169 text.append(refSeq.getName());
\r
170 text.append(newline);
\r
173 // mark any group references for the row
\r
174 if (row.groupRef == null)
\r
177 if (refGroup != null)
\r
179 text.append(newline);
\r
180 text.append("GROUP_REF\tALIGNMENT");
\r
181 text.append(newline);
\r
188 if (refGroup == null || refGroup != row.groupRef)
\r
190 refGroup = row.groupRef;
\r
191 text.append(newline);
\r
192 text.append("GROUP_REF\t");
\r
193 text.append(refGroup.getName());
\r
194 text.append(newline);
\r
198 boolean hasGlyphs = row.hasIcons, hasLabels = row.hasText, hasValues = row.hasScore, hasText = false;
\r
199 // lookahead to check what the annotation row object actually contains.
\r
200 for (int j = 0; row.annotations != null
\r
201 && j < row.annotations.length
\r
202 && (!hasGlyphs || !hasLabels || !hasValues); j++)
\r
204 if (row.annotations[j] != null)
\r
206 hasLabels |= (row.annotations[j].displayCharacter != null
\r
207 && row.annotations[j].displayCharacter.length() > 0 && !row.annotations[j].displayCharacter
\r
209 hasGlyphs |= (row.annotations[j].secondaryStructure != 0 && row.annotations[j].secondaryStructure != ' ');
\r
210 hasValues |= (row.annotations[j].value != Float.NaN); // NaNs can't
\r
213 hasText |= (row.annotations[j].description != null && row.annotations[j].description
\r
218 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
220 text.append("NO_GRAPH\t");
\r
221 hasValues = false; // only secondary structure
\r
222 // hasLabels = false; // and annotation description string.
\r
226 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
228 text.append("BAR_GRAPH\t");
\r
229 hasGlyphs = false; // no secondary structure
\r
232 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
234 hasGlyphs = false; // no secondary structure
\r
235 text.append("LINE_GRAPH\t");
\r
238 if (row.getThreshold() != null)
\r
240 graphLine.append("GRAPHLINE\t");
\r
241 graphLine.append(row.label);
\r
242 graphLine.append("\t");
\r
243 graphLine.append(row.getThreshold().value);
\r
244 graphLine.append("\t");
\r
245 graphLine.append(row.getThreshold().label);
\r
246 graphLine.append("\t");
\r
247 graphLine.append(jalview.util.Format.getHexString(row
\r
248 .getThreshold().colour));
\r
249 graphLine.append(newline);
\r
252 if (row.graphGroup > -1)
\r
254 String key = String.valueOf(row.graphGroup);
\r
255 if (graphGroup.containsKey(key))
\r
257 graphGroup.put(key, graphGroup.get(key) + "\t" + row.label);
\r
261 graphGroup.put(key, row.label);
\r
266 text.append(row.label + "\t");
\r
267 if (row.description != null)
\r
269 text.append(row.description + "\t");
\r
271 for (int j = 0; row.annotations != null
\r
272 && j < row.annotations.length; j++)
\r
275 && jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
\r
280 if (row.annotations[j] != null)
\r
283 if (hasGlyphs) // could be also hasGlyphs || ...
\r
286 text.append(comma);
\r
287 if (row.annotations[j].secondaryStructure != ' ')
\r
289 // only write out the field if its not whitespace.
\r
290 text.append(row.annotations[j].secondaryStructure);
\r
296 if (row.annotations[j].value != Float.NaN)
\r
298 text.append(comma + row.annotations[j].value);
\r
302 System.err.println("Skipping NaN - not valid value.");
\r
303 text.append(comma + 0f);// row.annotations[j].value);
\r
309 // TODO: labels are emitted after values for bar graphs.
\r
310 if // empty labels are allowed, so
\r
311 (row.annotations[j].displayCharacter != null
\r
312 && row.annotations[j].displayCharacter.length() > 0
\r
313 && !row.annotations[j].displayCharacter.equals(" "))
\r
315 text.append(comma + row.annotations[j].displayCharacter);
\r
321 if (row.annotations[j].description != null
\r
322 && row.annotations[j].description.length() > 0
\r
323 && !row.annotations[j].description
\r
324 .equals(row.annotations[j].displayCharacter))
\r
326 text.append(comma + row.annotations[j].description);
\r
330 if (color != null && !color.equals(row.annotations[j].colour))
\r
335 color = row.annotations[j].colour;
\r
337 if (row.annotations[j].colour != null
\r
338 && row.annotations[j].colour != java.awt.Color.black)
\r
342 + jalview.util.Format
\r
343 .getHexString(row.annotations[j].colour)
\r
351 if (row.hasScore())
\r
352 text.append("\t" + row.score);
\r
354 text.append(newline);
\r
356 if (color != null && color != java.awt.Color.black && oneColour)
\r
358 colours.append("COLOUR\t");
\r
359 colours.append(row.label);
\r
360 colours.append("\t");
\r
361 colours.append(jalview.util.Format.getHexString(color));
\r
362 colours.append(newline);
\r
364 if (row.scaleColLabel || row.showAllColLabels
\r
365 || row.centreColLabels)
\r
367 rowprops.append("ROWPROPERTIES\t");
\r
368 rowprops.append(row.label);
\r
369 rowprops.append("\tscaletofit=");
\r
370 rowprops.append(row.scaleColLabel);
\r
371 rowprops.append("\tshowalllabs=");
\r
372 rowprops.append(row.showAllColLabels);
\r
373 rowprops.append("\tcentrelabs=");
\r
374 rowprops.append(row.centreColLabels);
\r
375 rowprops.append(newline);
\r
379 text.append(newline);
\r
381 text.append(colours.toString());
\r
382 text.append(graphLine.toString());
\r
383 if (graphGroup.size() > 0)
\r
385 text.append("COMBINE\t");
\r
386 Enumeration en = graphGroup.elements();
\r
387 while (en.hasMoreElements())
\r
389 text.append(en.nextElement());
\r
390 text.append(newline);
\r
393 text.append(rowprops.toString());
\r
396 if (groups != null)
\r
398 printGroups(groups);
\r
401 if (properties != null)
\r
403 text.append(newline);
\r
404 text.append(newline);
\r
405 text.append("ALIGNMENT");
\r
406 Enumeration en = properties.keys();
\r
407 while (en.hasMoreElements())
\r
409 String key = en.nextElement().toString();
\r
413 text.append(properties.get(key));
\r
415 // TODO: output alignment visualization settings here if required
\r
419 return text.toString();
\r
422 public void printGroups(Vector sequenceGroups)
\r
425 SequenceI seqrep = null;
\r
426 for (int i = 0; i < sequenceGroups.size(); i++)
\r
428 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
429 if (!sg.hasSeqrep())
\r
431 text.append("SEQUENCE_GROUP\t" + sg.getName() + "\t"
\r
432 + (sg.getStartRes() + 1) + "\t" + (sg.getEndRes() + 1)
\r
438 seqrep = sg.getSeqrep();
\r
439 text.append("SEQUENCE_REF\t");
\r
440 text.append(seqrep.getName());
\r
441 text.append(newline);
\r
442 text.append("SEQUENCE_GROUP\t");
\r
443 text.append(sg.getName());
\r
445 text.append((seqrep.findPosition(sg.getStartRes())));
\r
447 text.append((seqrep.findPosition(sg.getEndRes())));
\r
449 text.append("-1\t");
\r
451 for (int s = 0; s < sg.getSize(); s++)
\r
453 text.append(sg.getSequenceAt(s).getName());
\r
456 text.append(newline);
\r
457 text.append("PROPERTIES\t");
\r
458 text.append(sg.getName());
\r
461 if (sg.getDescription() != null)
\r
463 text.append("description=");
\r
464 text.append(sg.getDescription());
\r
469 text.append("colour=");
\r
470 text.append(ColourSchemeProperty.getColourName(sg.cs));
\r
472 if (sg.cs.getThreshold() != 0)
\r
474 text.append("pidThreshold=");
\r
475 text.append(sg.cs.getThreshold());
\r
477 if (sg.cs.conservationApplied())
\r
479 text.append("consThreshold=");
\r
480 text.append(sg.cs.getConservationInc());
\r
484 text.append("outlineColour=");
\r
485 text.append(jalview.util.Format.getHexString(sg.getOutlineColour()));
\r
488 text.append("displayBoxes=");
\r
489 text.append(sg.getDisplayBoxes());
\r
491 text.append("displayText=");
\r
492 text.append(sg.getDisplayText());
\r
494 text.append("colourText=");
\r
495 text.append(sg.getColourText());
\r
497 text.append("showUnconserved=");
\r
498 text.append(sg.getShowNonconserved());
\r
500 if (sg.textColour != java.awt.Color.black)
\r
502 text.append("textCol1=");
\r
503 text.append(jalview.util.Format.getHexString(sg.textColour));
\r
506 if (sg.textColour2 != java.awt.Color.white)
\r
508 text.append("textCol2=");
\r
509 text.append(jalview.util.Format.getHexString(sg.textColour2));
\r
512 if (sg.thresholdTextColour != 0)
\r
514 text.append("textColThreshold=");
\r
515 text.append(sg.thresholdTextColour);
\r
518 if (sg.idColour != null)
\r
520 text.append("idColour=");
\r
521 text.append(jalview.util.Format.getHexString(sg.idColour));
\r
524 if (sg.isHidereps())
\r
526 text.append("hide=true\t");
\r
528 if (sg.isHideCols())
\r
530 text.append("hidecols=true\t");
\r
532 if (seqrep != null)
\r
534 // terminate the last line and clear the sequence ref for the group
\r
535 text.append(newline);
\r
536 text.append("SEQUENCE_REF");
\r
538 text.append(newline);
\r
539 text.append(newline);
\r
544 SequenceI refSeq = null;
\r
546 String refSeqId = null;
\r
548 public boolean readAnnotationFile(AlignmentI al, String file,
\r
551 BufferedReader in = null;
\r
554 if (protocol.equals(AppletFormatAdapter.FILE))
\r
556 in = new BufferedReader(new FileReader(file));
\r
558 else if (protocol.equals(AppletFormatAdapter.URL))
\r
560 URL url = new URL(file);
\r
561 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
563 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
565 in = new BufferedReader(new StringReader(file));
\r
567 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
569 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
572 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
577 return parseAnnotationFrom(al, in);
\r
580 } catch (Exception ex)
\r
582 ex.printStackTrace();
\r
583 System.out.println("Problem reading annotation file: " + ex);
\r
589 public boolean parseAnnotationFrom(AlignmentI al, BufferedReader in)
\r
592 boolean modified = false;
\r
593 String groupRef = null;
\r
594 Hashtable groupRefRows = new Hashtable();
\r
596 Hashtable autoAnnots = new Hashtable();
\r
598 String line, label, description, token;
\r
599 int graphStyle, index;
\r
600 int refSeqIndex = 1;
\r
601 int existingAnnotations = 0;
\r
602 // when true - will add new rows regardless of whether they are duplicate
\r
603 // auto-annotation like consensus or conservation graphs
\r
604 boolean overrideAutoAnnot = false;
\r
605 if (al.getAlignmentAnnotation() != null)
\r
607 existingAnnotations = al.getAlignmentAnnotation().length;
\r
608 if (existingAnnotations > 0)
\r
610 AlignmentAnnotation[] aa = al.getAlignmentAnnotation();
\r
611 for (int aai = 0; aai < aa.length; aai++)
\r
613 if (aa[aai].autoCalculated)
\r
615 // make a note of the name and description
\r
617 autoAnnotsKey(aa[aai], aa[aai].sequenceRef,
\r
618 (aa[aai].groupRef == null ? null
\r
619 : aa[aai].groupRef.getName())),
\r
626 int alWidth = al.getWidth();
\r
628 StringTokenizer st;
\r
629 Annotation[] annotations;
\r
630 AlignmentAnnotation annotation = null;
\r
632 // First confirm this is an Annotation file
\r
633 boolean jvAnnotationFile = false;
\r
634 while ((line = in.readLine()) != null)
\r
636 if (line.indexOf("#") == 0)
\r
641 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
643 jvAnnotationFile = true;
\r
648 if (!jvAnnotationFile)
\r
654 while ((line = in.readLine()) != null)
\r
656 if (line.indexOf("#") == 0
\r
657 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
658 || line.length() == 0)
\r
663 st = new StringTokenizer(line, "\t");
\r
664 token = st.nextToken();
\r
665 if (token.equalsIgnoreCase("COLOUR"))
\r
667 // TODO: use graduated colour def'n here too
\r
668 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
673 else if (token.equalsIgnoreCase("COMBINE"))
\r
675 combineAnnotations(al, st);
\r
679 else if (token.equalsIgnoreCase("ROWPROPERTIES"))
\r
681 addRowProperties(al, st);
\r
685 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
692 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
694 if (st.hasMoreTokens())
\r
696 refSeq = al.findName(refSeqId = st.nextToken());
\r
697 if (refSeq == null)
\r
703 refSeqIndex = Integer.parseInt(st.nextToken());
\r
704 if (refSeqIndex < 1)
\r
708 .println("WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
710 } catch (Exception ex)
\r
722 else if (token.equalsIgnoreCase("GROUP_REF"))
\r
724 // Group references could be forward or backwards, so they are
\r
725 // resolved after the whole file is read in
\r
727 if (st.hasMoreTokens())
\r
729 groupRef = st.nextToken();
\r
730 if (groupRef.length() < 1)
\r
732 groupRef = null; // empty string
\r
736 if (groupRefRows.get(groupRef) == null)
\r
738 groupRefRows.put(groupRef, new Vector());
\r
744 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
750 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
752 addProperties(al, st);
\r
757 else if (token.equalsIgnoreCase("BELOW_ALIGNMENT"))
\r
759 setBelowAlignment(al, st);
\r
763 else if (token.equalsIgnoreCase("ALIGNMENT"))
\r
765 addAlignmentDetails(al, st);
\r
770 // Parse out the annotation row
\r
771 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
772 label = st.nextToken();
\r
775 annotations = new Annotation[alWidth];
\r
776 description = null;
\r
777 float score = Float.NaN;
\r
779 if (st.hasMoreTokens())
\r
781 line = st.nextToken();
\r
783 if (line.indexOf("|") == -1)
\r
785 description = line;
\r
786 if (st.hasMoreTokens())
\r
787 line = st.nextToken();
\r
790 if (st.hasMoreTokens())
\r
792 // This must be the score
\r
793 score = Float.valueOf(st.nextToken()).floatValue();
\r
796 st = new StringTokenizer(line, "|", true);
\r
798 boolean emptyColumn = true;
\r
799 boolean onlyOneElement = (st.countTokens() == 1);
\r
801 while (st.hasMoreElements() && index < alWidth)
\r
803 token = st.nextToken().trim();
\r
805 if (onlyOneElement)
\r
809 score = Float.valueOf(token).floatValue();
\r
811 } catch (NumberFormatException ex)
\r
816 if (token.equals("|"))
\r
823 emptyColumn = true;
\r
827 annotations[index++] = parseAnnotation(token, graphStyle);
\r
828 emptyColumn = false;
\r
834 annotation = new AlignmentAnnotation(label, description,
\r
835 (index == 0) ? null : annotations, 0, 0, graphStyle);
\r
837 annotation.score = score;
\r
838 if (!overrideAutoAnnot
\r
839 && autoAnnots.containsKey(autoAnnotsKey(annotation, refSeq,
\r
842 // skip - we've already got an automatic annotation of this type.
\r
845 // otherwise add it!
\r
846 if (refSeq != null)
\r
849 annotation.belowAlignment = false;
\r
850 // make a copy of refSeq so we can find other matches in the alignment
\r
851 SequenceI referedSeq = refSeq;
\r
854 // copy before we do any mapping business.
\r
855 // TODO: verify that undo/redo with 1:many sequence associated
\r
856 // annotations can be undone correctly
\r
857 AlignmentAnnotation ann = new AlignmentAnnotation(annotation);
\r
859 .createSequenceMapping(referedSeq, refSeqIndex, false);
\r
860 annotation.adjustForAlignment();
\r
861 referedSeq.addAlignmentAnnotation(annotation);
\r
862 al.addAnnotation(annotation);
\r
863 al.setAnnotationIndex(annotation,
\r
864 al.getAlignmentAnnotation().length
\r
865 - existingAnnotations - 1);
\r
866 if (groupRef != null)
\r
868 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
\r
870 // and recover our virgin copy to use again if necessary.
\r
873 } while (refSeqId != null
\r
874 && (referedSeq = al.findName(referedSeq, refSeqId, true)) != null);
\r
878 al.addAnnotation(annotation);
\r
879 al.setAnnotationIndex(annotation,
\r
880 al.getAlignmentAnnotation().length - existingAnnotations
\r
882 if (groupRef != null)
\r
884 ((Vector) groupRefRows.get(groupRef)).addElement(annotation);
\r
887 // and set modification flag
\r
890 // Finally, resolve the groupRefs
\r
891 Enumeration en = groupRefRows.keys();
\r
892 SequenceGroup theGroup = null;
\r
894 while (en.hasMoreElements())
\r
896 groupRef = (String) en.nextElement();
\r
897 boolean matched = false;
\r
898 // Resolve group: TODO: add a getGroupByName method to alignments
\r
899 Vector grps = al.getGroups();
\r
900 for (int g = 0, gSize = grps.size(); g < gSize; g++)
\r
902 theGroup = (SequenceGroup) grps.elementAt(g);
\r
903 if (theGroup.getName().equals(groupRef))
\r
907 // TODO: specify and implement duplication of alignment annotation
\r
908 // for multiple group references.
\r
910 .println("Ignoring 1:many group reference mappings for group name '"
\r
916 Vector rowset = (Vector) groupRefRows.get(groupRef);
\r
917 if (rowset != null && rowset.size() > 0)
\r
919 AlignmentAnnotation alan = null;
\r
920 for (int elm = 0, elmSize = rowset.size(); elm < elmSize; elm++)
\r
922 alan = (AlignmentAnnotation) rowset.elementAt(elm);
\r
923 alan.groupRef = theGroup;
\r
929 ((Vector) groupRefRows.get(groupRef)).removeAllElements();
\r
935 private Object autoAnnotsKey(AlignmentAnnotation annotation,
\r
936 SequenceI refSeq, String groupRef)
\r
938 return annotation.graph + "\t" + annotation.label + "\t"
\r
939 + annotation.description + "\t"
\r
940 + (refSeq != null ? refSeq.getDisplayId(true) : "");
\r
943 Annotation parseAnnotation(String string, int graphStyle)
\r
945 boolean hasSymbols = (graphStyle == AlignmentAnnotation.NO_GRAPH); // don't
\r
954 String desc = null, displayChar = null;
\r
955 char ss = ' '; // secondaryStructure
\r
957 boolean parsedValue = false, dcset = false;
\r
959 // find colour here
\r
960 java.awt.Color colour = null;
\r
961 int i = string.indexOf("[");
\r
962 int j = string.indexOf("]");
\r
963 if (i > -1 && j > -1)
\r
965 UserColourScheme ucs = new UserColourScheme();
\r
967 colour = ucs.getColourFromString(string.substring(i + 1, j));
\r
968 if (i > 0 && string.charAt(i - 1) == ',')
\r
970 // clip the preceding comma as well
\r
973 string = string.substring(0, i) + string.substring(j + 1);
\r
976 StringTokenizer st = new StringTokenizer(string, ",", true);
\r
978 boolean seenContent = false;
\r
980 while (st.hasMoreTokens())
\r
983 token = st.nextToken().trim();
\r
984 if (token.equals(","))
\r
986 if (!seenContent && parsedValue && !dcset)
\r
988 // allow the value below the bar/line to be empty
\r
992 seenContent = false;
\r
997 seenContent = true;
\r
1004 displayChar = token;
\r
1006 value = new Float(token).floatValue();
\r
1007 parsedValue = true;
\r
1009 } catch (NumberFormatException ex)
\r
1015 if (token.length() == 1)
\r
1017 displayChar = token;
\r
1021 && (token.equals("H") || token.equals("E") || token.equals("S") || token
\r
1024 // Either this character represents a helix or sheet
\r
1025 // or an integer which can be displayed
\r
1026 ss = token.charAt(0);
\r
1027 if (displayChar.equals(token.substring(0, 1)))
\r
1032 else if (desc == null || (parsedValue && pass > 2))
\r
1038 // if (!dcset && string.charAt(string.length() - 1) == ',')
\r
1040 // displayChar = " "; // empty display char symbol.
\r
1042 if (displayChar != null && desc != null && desc.length() == 1)
\r
1044 if (displayChar.length() > 1)
\r
1046 // switch desc and displayChar - legacy support
\r
1047 String tmp = displayChar;
\r
1048 displayChar = desc;
\r
1053 if (displayChar.equals(desc))
\r
1055 // duplicate label - hangover from the 'robust parser' above
\r
1060 Annotation anot = new Annotation(displayChar, desc, ss, value);
\r
1062 anot.colour = colour;
\r
1067 void colourAnnotations(AlignmentI al, String label, String colour)
\r
1069 UserColourScheme ucs = new UserColourScheme(colour);
\r
1070 Annotation[] annotations;
\r
1071 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1073 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
1075 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
1076 for (int j = 0; j < annotations.length; j++)
\r
1078 if (annotations[j] != null)
\r
1080 annotations[j].colour = ucs.findColour('A');
\r
1087 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
1089 int graphGroup = -1;
\r
1090 String group = st.nextToken();
\r
1091 // First make sure we are not overwriting the graphIndex
\r
1092 if (al.getAlignmentAnnotation() != null)
\r
1094 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1096 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
1098 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
1099 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
1104 // Now update groups
\r
1105 while (st.hasMoreTokens())
\r
1107 group = st.nextToken();
\r
1108 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1110 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
1112 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
1121 .println("Couldn't combine annotations. None are added to alignment yet!");
\r
1125 void addLine(AlignmentI al, StringTokenizer st)
\r
1127 String group = st.nextToken();
\r
1128 AlignmentAnnotation annotation = null, alannot[] = al
\r
1129 .getAlignmentAnnotation();
\r
1130 if (alannot != null)
\r
1132 for (int i = 0; i < alannot.length; i++)
\r
1134 if (alannot[i].label.equalsIgnoreCase(group))
\r
1136 annotation = alannot[i];
\r
1141 if (annotation == null)
\r
1145 float value = new Float(st.nextToken()).floatValue();
\r
1146 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
1147 java.awt.Color colour = null;
\r
1148 if (st.hasMoreTokens())
\r
1150 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
1151 colour = ucs.findColour('A');
\r
1154 annotation.setThreshold(new GraphLine(value, label, colour));
\r
1157 void addGroup(AlignmentI al, StringTokenizer st)
\r
1159 SequenceGroup sg = new SequenceGroup();
\r
1160 sg.setName(st.nextToken());
\r
1164 rng = st.nextToken();
\r
1165 if (rng.length() > 0 && !rng.startsWith("*"))
\r
1167 sg.setStartRes(Integer.parseInt(rng) - 1);
\r
1171 sg.setStartRes(0);
\r
1173 rng = st.nextToken();
\r
1174 if (rng.length() > 0 && !rng.startsWith("*"))
\r
1176 sg.setEndRes(Integer.parseInt(rng) - 1);
\r
1180 sg.setEndRes(al.getWidth() - 1);
\r
1182 } catch (Exception e)
\r
1185 .println("Couldn't parse Group Start or End Field as '*' or a valid column or sequence index: '"
\r
1186 + rng + "' - assuming alignment width for group.");
\r
1187 // assume group is full width
\r
1188 sg.setStartRes(0);
\r
1189 sg.setEndRes(al.getWidth() - 1);
\r
1192 String index = st.nextToken();
\r
1193 if (index.equals("-1"))
\r
1195 while (st.hasMoreElements())
\r
1197 sg.addSequence(al.findName(st.nextToken()), false);
\r
1202 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
1204 while (st2.hasMoreTokens())
\r
1206 String tmp = st2.nextToken();
\r
1207 if (tmp.equals("*"))
\r
1209 for (int i = 0; i < al.getHeight(); i++)
\r
1211 sg.addSequence(al.getSequenceAt(i), false);
\r
1214 else if (tmp.indexOf("-") >= 0)
\r
1216 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
1218 int start = (Integer.parseInt(st3.nextToken()));
\r
1219 int end = (Integer.parseInt(st3.nextToken()));
\r
1223 for (int i = start; i <= end; i++)
\r
1225 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
1231 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
1236 if (refSeq != null)
\r
1238 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
1239 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
1240 sg.setSeqrep(refSeq);
\r
1243 if (sg.getSize() > 0)
\r
1249 void addRowProperties(AlignmentI al, StringTokenizer st)
\r
1251 String label = st.nextToken(), keyValue, key, value;
\r
1252 boolean scaletofit = false, centerlab = false, showalllabs = false;
\r
1253 while (st.hasMoreTokens())
\r
1255 keyValue = st.nextToken();
\r
1256 key = keyValue.substring(0, keyValue.indexOf("="));
\r
1257 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
1258 if (key.equalsIgnoreCase("scaletofit"))
\r
1260 scaletofit = Boolean.valueOf(value).booleanValue();
\r
1262 if (key.equalsIgnoreCase("showalllabs"))
\r
1264 showalllabs = Boolean.valueOf(value).booleanValue();
\r
1266 if (key.equalsIgnoreCase("centrelabs"))
\r
1268 centerlab = Boolean.valueOf(value).booleanValue();
\r
1270 AlignmentAnnotation[] alr = al.getAlignmentAnnotation();
\r
1273 for (int i = 0; i < alr.length; i++)
\r
1275 if (alr[i].label.equalsIgnoreCase(label))
\r
1277 alr[i].centreColLabels = centerlab;
\r
1278 alr[i].scaleColLabel = scaletofit;
\r
1279 alr[i].showAllColLabels = showalllabs;
\r
1286 void addProperties(AlignmentI al, StringTokenizer st)
\r
1289 // So far we have only added groups to the annotationHash,
\r
1290 // the idea is in the future properties can be added to
\r
1291 // alignments, other annotations etc
\r
1292 if (al.getGroups() == null)
\r
1296 SequenceGroup sg = null;
\r
1298 String name = st.nextToken();
\r
1300 Vector groups = al.getGroups();
\r
1301 for (int i = 0; i < groups.size(); i++)
\r
1303 sg = (SequenceGroup) groups.elementAt(i);
\r
1304 if (sg.getName().equals(name))
\r
1316 String keyValue, key, value;
\r
1317 ColourSchemeI def = sg.cs;
\r
1319 while (st.hasMoreTokens())
\r
1321 keyValue = st.nextToken();
\r
1322 key = keyValue.substring(0, keyValue.indexOf("="));
\r
1323 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
1325 if (key.equalsIgnoreCase("description"))
\r
1327 sg.setDescription(value);
\r
1329 else if (key.equalsIgnoreCase("colour"))
\r
1331 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
1333 else if (key.equalsIgnoreCase("pidThreshold"))
\r
1335 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
1338 else if (key.equalsIgnoreCase("consThreshold"))
\r
1340 sg.cs.setConservationInc(Integer.parseInt(value));
\r
1341 Conservation c = new Conservation("Group",
\r
1342 ResidueProperties.propHash, 3, sg.getSequences(null),
\r
1343 sg.getStartRes(), sg.getEndRes() + 1);
\r
1346 c.verdict(false, 25); // TODO: refer to conservation percent threshold
\r
1348 sg.cs.setConservation(c);
\r
1351 else if (key.equalsIgnoreCase("outlineColour"))
\r
1353 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
1355 else if (key.equalsIgnoreCase("displayBoxes"))
\r
1357 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
1359 else if (key.equalsIgnoreCase("showUnconserved"))
\r
1361 sg.setShowNonconserved(Boolean.valueOf(value).booleanValue());
\r
1363 else if (key.equalsIgnoreCase("displayText"))
\r
1365 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
1367 else if (key.equalsIgnoreCase("colourText"))
\r
1369 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
1371 else if (key.equalsIgnoreCase("textCol1"))
\r
1373 sg.textColour = new UserColourScheme(value).findColour('A');
\r
1375 else if (key.equalsIgnoreCase("textCol2"))
\r
1377 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
1379 else if (key.equalsIgnoreCase("textColThreshold"))
\r
1381 sg.thresholdTextColour = Integer.parseInt(value);
\r
1383 else if (key.equalsIgnoreCase("idColour"))
\r
1385 // consider warning if colour doesn't resolve to a real colour
\r
1386 sg.setIdColour((def = new UserColourScheme(value))
\r
1387 .findColour('A'));
\r
1389 else if (key.equalsIgnoreCase("hide"))
\r
1391 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
\r
1392 sg.setHidereps(true);
\r
1394 else if (key.equalsIgnoreCase("hidecols"))
\r
1396 // see bug https://mantis.lifesci.dundee.ac.uk/view.php?id=25847
\r
1397 sg.setHideCols(true);
\r
1399 sg.recalcConservation();
\r
1401 if (sg.cs == null)
\r
1408 void setBelowAlignment(AlignmentI al, StringTokenizer st)
\r
1411 AlignmentAnnotation aa, ala[] = al.getAlignmentAnnotation();
\r
1415 .print("Warning - no annotation to set below for sequence associated annotation:");
\r
1417 while (st.hasMoreTokens())
\r
1419 token = st.nextToken();
\r
1422 System.err.print(" " + token);
\r
1426 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
1428 aa = al.getAlignmentAnnotation()[i];
\r
1429 if (aa.sequenceRef == refSeq && aa.label.equals(token))
\r
1431 aa.belowAlignment = true;
\r
1438 System.err.print("\n");
\r
1442 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
\r
1444 String keyValue, key, value;
\r
1445 while (st.hasMoreTokens())
\r
1447 keyValue = st.nextToken();
\r
1448 key = keyValue.substring(0, keyValue.indexOf("="));
\r
1449 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
1450 al.setProperty(key, value);
\r
1455 * Write annotations as a CSV file of the form 'label, value, value, ...' for
\r
1458 * @param annotations
\r
1459 * @return CSV file as a string.
\r
1461 public String printCSVAnnotations(AlignmentAnnotation[] annotations)
\r
1463 StringBuffer sp = new StringBuffer();
\r
1464 for (int i = 0; i < annotations.length; i++)
\r
1466 String atos = annotations[i].toString();
\r
1470 int cp = atos.indexOf("\n", p);
\r
1471 sp.append(annotations[i].label);
\r
1475 sp.append(atos.substring(p, cp + 1));
\r
1479 sp.append(atos.substring(p));
\r
1480 sp.append(newline);
\r
1485 return sp.toString();
\r