2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
26 import jalview.analysis.*;
\r
27 import jalview.datamodel.*;
\r
28 import jalview.schemes.*;
\r
30 public class AnnotationFile
\r
32 StringBuffer text = new StringBuffer(
\r
33 "JALVIEW_ANNOTATION\n"
\r
35 + new java.util.Date() + "\n\n");
\r
37 public String printAnnotations(AlignmentAnnotation[] annotations,
\r
40 if (annotations != null)
\r
42 AlignmentAnnotation row;
\r
44 SequenceI seqref = null;
\r
46 StringBuffer colours = new StringBuffer();
\r
47 StringBuffer graphLine = new StringBuffer();
\r
49 Hashtable graphGroup = new Hashtable();
\r
51 java.awt.Color color;
\r
53 for (int i = 0; i < annotations.length; i++)
\r
55 row = annotations[i];
\r
64 if (row.sequenceRef == null)
\r
68 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
74 else if (seqref == null || seqref != row.sequenceRef)
\r
76 seqref = row.sequenceRef;
\r
77 text.append("\nSEQUENCE_REF\t" + seqref.getName() + "\n");
\r
80 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
82 text.append("NO_GRAPH\t");
\r
86 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
88 text.append("BAR_GRAPH\t");
\r
90 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
92 text.append("LINE_GRAPH\t");
\r
95 if (row.getThreshold() != null)
\r
97 graphLine.append("GRAPHLINE\t"
\r
99 + row.getThreshold().value + "\t"
\r
100 + row.getThreshold().label + "\t"
\r
101 + jalview.util.Format.getHexString(
\r
102 row.getThreshold().colour) + "\n"
\r
106 if (row.graphGroup > -1)
\r
108 String key = String.valueOf(row.graphGroup);
\r
109 if (graphGroup.containsKey(key))
\r
111 graphGroup.put(key, graphGroup.get(key)
\r
112 + "\t" + row.label);
\r
116 graphGroup.put(key, row.label);
\r
121 text.append(row.label + "\t");
\r
122 if (row.description != null)
\r
124 text.append(row.description + "\t");
\r
127 for (int j = 0; j < row.annotations.length; j++)
\r
129 if (seqref != null &&
\r
130 jalview.util.Comparison.isGap(seqref.getCharAt(j)))
\r
135 if (row.annotations[j] != null)
\r
138 if (row.annotations[j].secondaryStructure != ' ')
\r
140 text.append(comma + row.annotations[j].secondaryStructure);
\r
143 if (row.annotations[j].displayCharacter.length() > 0
\r
144 && !row.annotations[j].displayCharacter.equals(" "))
\r
146 text.append(comma + row.annotations[j].displayCharacter);
\r
150 if (row.annotations[j] != null)
\r
152 color = row.annotations[j].colour;
\r
153 if (row.annotations[j].value != 0f)
\r
155 text.append(comma + row.annotations[j].value);
\r
164 if (color != null && color != java.awt.Color.black)
\r
166 colours.append("COLOUR\t"
\r
168 + jalview.util.Format.getHexString(color) + "\n");
\r
175 text.append(colours.toString());
\r
176 text.append(graphLine.toString());
\r
177 if (graphGroup.size() > 0)
\r
179 text.append("COMBINE\t");
\r
180 Enumeration en = graphGroup.elements();
\r
181 while (en.hasMoreElements())
\r
183 text.append(en.nextElement() + "\n");
\r
188 if (groups != null)
\r
190 printGroups(groups);
\r
193 return text.toString();
\r
196 public void printGroups(Vector sequenceGroups)
\r
199 for (int i = 0; i < sequenceGroups.size(); i++)
\r
201 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
202 text.append("SEQUENCE_GROUP\t"
\r
203 + sg.getName() + "\t"
\r
204 + (sg.getStartRes() + 1) + "\t"
\r
205 + (sg.getEndRes() + 1) + "\t" + "-1\t");
\r
206 for (int s = 0; s < sg.getSize(); s++)
\r
208 text.append(sg.getSequenceAt(s).getName() + "\t");
\r
211 text.append("\nPROPERTIES\t" + sg.getName() + "\t");
\r
213 if (sg.getDescription() != null)
\r
215 text.append("description=" + sg.getDescription() + "\t");
\r
219 text.append("colour=" + ColourSchemeProperty.getColourName(sg.cs) +
\r
221 if (sg.cs.getThreshold() != 0)
\r
223 text.append("pidThreshold=" + sg.cs.getThreshold());
\r
225 if (sg.cs.conservationApplied())
\r
227 text.append("consThreshold=" + sg.cs.getConservationInc() + "\t");
\r
230 text.append("outlineColour=" +
\r
231 jalview.util.Format.getHexString(sg.getOutlineColour()) +
\r
234 text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");
\r
235 text.append("displayText=" + sg.getDisplayText() + "\t");
\r
236 text.append("colourText=" + sg.getColourText() + "\t");
\r
238 if (sg.textColour != java.awt.Color.black)
\r
240 text.append("textCol1=" +
\r
241 jalview.util.Format.getHexString(sg.textColour) + "\t");
\r
243 if (sg.textColour2 != java.awt.Color.white)
\r
245 text.append("textCol2=" +
\r
246 jalview.util.Format.getHexString(sg.textColour2) + "\t");
\r
248 if (sg.thresholdTextColour != 0)
\r
250 text.append("textColThreshold=" + sg.thresholdTextColour);
\r
253 text.append("\n\n");
\r
258 SequenceI refSeq = null;
\r
259 public boolean readAnnotationFile(AlignmentI al,
\r
265 BufferedReader in = null;
\r
266 if (protocol.equals(AppletFormatAdapter.FILE))
\r
268 in = new BufferedReader(new FileReader(file));
\r
270 else if (protocol.equals(AppletFormatAdapter.URL))
\r
272 URL url = new URL(file);
\r
273 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
275 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
277 in = new BufferedReader(new StringReader(file));
\r
279 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
281 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
284 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
288 String line, label, description, token;
\r
289 int graphStyle, index;
\r
290 int refSeqIndex = 1;
\r
291 int existingAnnotations = 0;
\r
292 if (al.getAlignmentAnnotation() != null)
\r
294 existingAnnotations = al.getAlignmentAnnotation().length;
\r
297 int alWidth = al.getWidth();
\r
299 StringTokenizer st;
\r
300 Annotation[] annotations;
\r
301 AlignmentAnnotation annotation = null;
\r
303 // First confirm this is an Annotation file
\r
304 boolean jvAnnotationFile = false;
\r
305 while ( (line = in.readLine()) != null)
\r
307 if (line.indexOf("#") == 0)
\r
312 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
314 jvAnnotationFile = true;
\r
319 if (!jvAnnotationFile)
\r
325 while ( (line = in.readLine()) != null)
\r
327 if (line.indexOf("#") == 0
\r
328 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
329 || line.length() == 0)
\r
334 st = new StringTokenizer(line, "\t");
\r
335 token = st.nextToken();
\r
336 if (token.equalsIgnoreCase("COLOUR"))
\r
338 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
342 else if (token.equalsIgnoreCase("COMBINE"))
\r
344 combineAnnotations(al, st);
\r
348 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
354 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
356 refSeq = al.findName(st.nextToken());
\r
359 refSeqIndex = Integer.parseInt(st.nextToken());
\r
360 if (refSeqIndex < 1)
\r
363 System.out.println(
\r
364 "WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
367 catch (Exception ex)
\r
375 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
381 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
383 addProperties(al, st);
\r
387 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
388 label = st.nextToken();
\r
390 if (st.countTokens() > 1)
\r
392 description = st.nextToken();
\r
396 description = null;
\r
399 line = st.nextToken();
\r
401 st = new StringTokenizer(line, "|", true);
\r
402 annotations = new Annotation[alWidth];
\r
405 boolean emptyColumn = true;
\r
407 while (st.hasMoreElements() && index < alWidth)
\r
409 token = st.nextToken().trim();
\r
410 if (token.equals("|"))
\r
417 emptyColumn = true;
\r
421 annotations[index++] = parseAnnotation(token);
\r
422 emptyColumn = false;
\r
426 annotation = new AlignmentAnnotation(label,
\r
433 if (refSeq != null)
\r
435 System.out.println(refSeq.getName()+" "+refSeqIndex);
\r
436 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
437 annotation.adjustForAlignment();
\r
438 refSeq.addAlignmentAnnotation(annotation);
\r
441 al.addAnnotation(annotation);
\r
443 al.setAnnotationIndex(annotation,
\r
444 al.getAlignmentAnnotation().length - existingAnnotations -
\r
449 catch (Exception ex)
\r
451 ex.printStackTrace();
\r
452 System.out.println("Problem reading annotation file: " + ex);
\r
458 Annotation parseAnnotation(String string)
\r
460 String desc = null, displayChar = "";
\r
461 char ss = ' '; // secondaryStructure
\r
463 boolean parsedValue = false;
\r
464 StringTokenizer st = new StringTokenizer(string, ",");
\r
466 while (st.hasMoreTokens())
\r
468 token = st.nextToken().trim();
\r
469 if (token.length() == 0)
\r
478 displayChar = token;
\r
479 value = new Float(token).floatValue();
\r
480 parsedValue = true;
\r
483 catch (NumberFormatException ex)
\r
487 if (token.equals("H") || token.equals("E"))
\r
489 // Either this character represents a helix or sheet
\r
490 // or an integer which can be displayed
\r
491 ss = token.charAt(0);
\r
492 if (displayChar.equals(token.substring(0, 1)))
\r
497 else if (desc == null)
\r
509 if (displayChar.length() > 1 && desc.length() == 1)
\r
511 String tmp = displayChar;
\r
512 displayChar = desc;
\r
516 return new Annotation(displayChar, desc, ss, value);
\r
519 void colourAnnotations(AlignmentI al, String label, String colour)
\r
521 UserColourScheme ucs = new UserColourScheme(colour);
\r
522 Annotation[] annotations;
\r
523 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
525 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
527 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
528 for (int j = 0; j < annotations.length; j++)
\r
530 if (annotations[j] != null)
\r
532 annotations[j].colour = ucs.findColour('A');
\r
539 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
541 int graphGroup = -1;
\r
542 String group = st.nextToken();
\r
543 //First make sure we are not overwriting the graphIndex
\r
544 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
546 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
548 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
549 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
554 //Now update groups
\r
555 while (st.hasMoreTokens())
\r
557 group = st.nextToken();
\r
558 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
560 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
562 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
569 void addLine(AlignmentI al, StringTokenizer st)
\r
571 String group = st.nextToken();
\r
572 AlignmentAnnotation annotation = null;
\r
574 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
576 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
578 annotation = al.getAlignmentAnnotation()[i];
\r
583 if (annotation == null)
\r
587 float value = new Float(st.nextToken()).floatValue();
\r
588 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
589 java.awt.Color colour = null;
\r
590 if (st.hasMoreTokens())
\r
592 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
593 colour = ucs.findColour('A');
\r
596 annotation.setThreshold(new GraphLine(value, label, colour));
\r
599 void addGroup(AlignmentI al, StringTokenizer st)
\r
601 SequenceGroup sg = new SequenceGroup();
\r
602 sg.setName(st.nextToken());
\r
603 sg.setStartRes(Integer.parseInt(st.nextToken()) - 1);
\r
604 sg.setEndRes(Integer.parseInt(st.nextToken()) - 1);
\r
606 String index = st.nextToken();
\r
607 if (index.equals("-1"))
\r
609 while (st.hasMoreElements())
\r
611 sg.addSequence(al.findName(st.nextToken()), false);
\r
616 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
618 while (st2.hasMoreTokens())
\r
620 String tmp = st2.nextToken();
\r
621 if (tmp.equals("*"))
\r
623 for (int i = 0; i < al.getHeight(); i++)
\r
625 sg.addSequence(al.getSequenceAt(i), false);
\r
628 else if (tmp.indexOf("-") >= 0)
\r
630 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
632 int start = (Integer.parseInt(st3.nextToken()));
\r
633 int end = (Integer.parseInt(st3.nextToken()));
\r
637 for (int i = start; i <= end; i++)
\r
639 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
645 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
650 if (refSeq != null)
\r
652 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
653 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
656 if (sg.getSize() > 0)
\r
662 void addProperties(AlignmentI al, StringTokenizer st)
\r
665 //So far we have only added groups to the annotationHash,
\r
666 //the idea is in the future properties can be added to
\r
667 //alignments, other annotations etc
\r
668 if (al.getGroups() == null)
\r
672 SequenceGroup sg = null;
\r
674 String name = st.nextToken();
\r
676 Vector groups = al.getGroups();
\r
677 for (int i = 0; i < groups.size(); i++)
\r
679 sg = (SequenceGroup) groups.elementAt(i);
\r
680 if (sg.getName().equals(name))
\r
692 String keyValue, key, value;
\r
693 while (st.hasMoreTokens())
\r
695 keyValue = st.nextToken();
\r
696 key = keyValue.substring(0, keyValue.indexOf("="));
\r
697 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
699 if (key.equalsIgnoreCase("description"))
\r
701 sg.setDescription(value);
\r
703 else if (key.equalsIgnoreCase("colour"))
\r
705 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
707 else if (key.equalsIgnoreCase("pidThreshold"))
\r
709 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
712 else if (key.equalsIgnoreCase("consThreshold"))
\r
714 sg.cs.setConservationInc(Integer.parseInt(value));
\r
715 Conservation c = new Conservation("Group",
\r
716 ResidueProperties.propHash, 3,
\r
717 sg.getSequences(null),
\r
719 sg.getEndRes() + 1);
\r
722 c.verdict(false, 25);
\r
724 sg.cs.setConservation(c);
\r
727 else if (key.equalsIgnoreCase("outlineColour"))
\r
729 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
731 else if (key.equalsIgnoreCase("displayBoxes"))
\r
733 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
735 else if (key.equalsIgnoreCase("displayText"))
\r
737 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
739 else if (key.equalsIgnoreCase("colourText"))
\r
741 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
743 else if (key.equalsIgnoreCase("textCol1"))
\r
745 sg.textColour = new UserColourScheme(value).findColour('A');
\r
747 else if (key.equalsIgnoreCase("textCol2"))
\r
749 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
751 else if (key.equalsIgnoreCase("textColThreshold"))
\r
753 sg.thresholdTextColour = Integer.parseInt(value);
\r
756 sg.recalcConservation();
\r