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