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
39 Hashtable properties)
\r
41 if (annotations != null)
\r
43 AlignmentAnnotation row;
\r
45 SequenceI refSeq = null;
\r
47 StringBuffer colours = new StringBuffer();
\r
48 StringBuffer graphLine = new StringBuffer();
\r
50 Hashtable graphGroup = new Hashtable();
\r
52 java.awt.Color color;
\r
54 for (int i = 0; i < annotations.length; i++)
\r
56 row = annotations[i];
\r
65 if (row.sequenceRef == null)
\r
69 text.append("\nSEQUENCE_REF\tALIGNMENT\n");
\r
75 else if (refSeq == null || refSeq != row.sequenceRef)
\r
77 refSeq = row.sequenceRef;
\r
78 text.append("\nSEQUENCE_REF\t" + refSeq.getName() + "\n");
\r
81 if (row.graph == AlignmentAnnotation.NO_GRAPH)
\r
83 text.append("NO_GRAPH\t");
\r
87 if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
89 text.append("BAR_GRAPH\t");
\r
91 else if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
93 text.append("LINE_GRAPH\t");
\r
96 if (row.getThreshold() != null)
\r
98 graphLine.append("GRAPHLINE\t"
\r
100 + row.getThreshold().value + "\t"
\r
101 + row.getThreshold().label + "\t"
\r
102 + jalview.util.Format.getHexString(
\r
103 row.getThreshold().colour) + "\n"
\r
107 if (row.graphGroup > -1)
\r
109 String key = String.valueOf(row.graphGroup);
\r
110 if (graphGroup.containsKey(key))
\r
112 graphGroup.put(key, graphGroup.get(key)
\r
113 + "\t" + row.label);
\r
117 graphGroup.put(key, row.label);
\r
122 text.append(row.label + "\t");
\r
123 if (row.description != null)
\r
125 text.append(row.description + "\t");
\r
128 for (int j = 0; j < row.annotations.length; j++)
\r
130 if (refSeq != null &&
\r
131 jalview.util.Comparison.isGap(refSeq.getCharAt(j)))
\r
136 if (row.annotations[j] != null)
\r
139 if (row.annotations[j].secondaryStructure != ' ')
\r
141 text.append(comma + row.annotations[j].secondaryStructure);
\r
144 if (row.annotations[j].displayCharacter!=null
\r
145 && row.annotations[j].displayCharacter.length() > 0
\r
146 && !row.annotations[j].displayCharacter.equals(" "))
\r
148 text.append(comma + row.annotations[j].displayCharacter);
\r
152 if (row.annotations[j] != null)
\r
154 color = row.annotations[j].colour;
\r
155 if (row.annotations[j].value != 0f)
\r
157 text.append(comma + row.annotations[j].value);
\r
161 if(row.annotations[j].colour!=null
\r
162 && row.annotations[j].colour!=java.awt.Color.black)
\r
164 text.append(comma+"["+
\r
165 jalview.util.Format.getHexString(
\r
166 row.annotations[j].colour)+"]");
\r
172 if(!Float.isNaN(row.score))
\r
173 text.append("\t"+row.score);
\r
177 if (color != null && color != java.awt.Color.black)
\r
179 colours.append("COLOUR\t"
\r
181 + jalview.util.Format.getHexString(color) + "\n");
\r
188 text.append(colours.toString());
\r
189 text.append(graphLine.toString());
\r
190 if (graphGroup.size() > 0)
\r
192 text.append("COMBINE\t");
\r
193 Enumeration en = graphGroup.elements();
\r
194 while (en.hasMoreElements())
\r
196 text.append(en.nextElement() + "\n");
\r
201 if (groups != null)
\r
203 printGroups(groups);
\r
206 if(properties!=null)
\r
208 text.append("\n\nALIGNMENT");
\r
209 Enumeration en = properties.keys();
\r
210 while(en.hasMoreElements())
\r
212 String key = en.nextElement().toString();
\r
213 text.append("\t"+key+"="+properties.get(key));
\r
218 return text.toString();
\r
221 public void printGroups(Vector sequenceGroups)
\r
224 for (int i = 0; i < sequenceGroups.size(); i++)
\r
226 sg = (SequenceGroup) sequenceGroups.elementAt(i);
\r
227 text.append("SEQUENCE_GROUP\t"
\r
228 + sg.getName() + "\t"
\r
229 + (sg.getStartRes() + 1) + "\t"
\r
230 + (sg.getEndRes() + 1) + "\t" + "-1\t");
\r
231 for (int s = 0; s < sg.getSize(); s++)
\r
233 text.append(sg.getSequenceAt(s).getName() + "\t");
\r
236 text.append("\nPROPERTIES\t" + sg.getName() + "\t");
\r
238 if (sg.getDescription() != null)
\r
240 text.append("description=" + sg.getDescription() + "\t");
\r
244 text.append("colour=" + ColourSchemeProperty.getColourName(sg.cs) +
\r
246 if (sg.cs.getThreshold() != 0)
\r
248 text.append("pidThreshold=" + sg.cs.getThreshold());
\r
250 if (sg.cs.conservationApplied())
\r
252 text.append("consThreshold=" + sg.cs.getConservationInc() + "\t");
\r
255 text.append("outlineColour=" +
\r
256 jalview.util.Format.getHexString(sg.getOutlineColour()) +
\r
259 text.append("displayBoxes=" + sg.getDisplayBoxes() + "\t");
\r
260 text.append("displayText=" + sg.getDisplayText() + "\t");
\r
261 text.append("colourText=" + sg.getColourText() + "\t");
\r
263 if (sg.textColour != java.awt.Color.black)
\r
265 text.append("textCol1=" +
\r
266 jalview.util.Format.getHexString(sg.textColour) + "\t");
\r
268 if (sg.textColour2 != java.awt.Color.white)
\r
270 text.append("textCol2=" +
\r
271 jalview.util.Format.getHexString(sg.textColour2) + "\t");
\r
273 if (sg.thresholdTextColour != 0)
\r
275 text.append("textColThreshold=" + sg.thresholdTextColour);
\r
278 text.append("\n\n");
\r
283 SequenceI refSeq = null;
\r
284 public boolean readAnnotationFile(AlignmentI al,
\r
290 BufferedReader in = null;
\r
291 if (protocol.equals(AppletFormatAdapter.FILE))
\r
293 in = new BufferedReader(new FileReader(file));
\r
295 else if (protocol.equals(AppletFormatAdapter.URL))
\r
297 URL url = new URL(file);
\r
298 in = new BufferedReader(new InputStreamReader(url.openStream()));
\r
300 else if (protocol.equals(AppletFormatAdapter.PASTE))
\r
302 in = new BufferedReader(new StringReader(file));
\r
304 else if (protocol.equals(AppletFormatAdapter.CLASSLOADER))
\r
306 java.io.InputStream is = getClass().getResourceAsStream("/" + file);
\r
309 in = new BufferedReader(new java.io.InputStreamReader(is));
\r
313 String line, label, description, token;
\r
314 int graphStyle, index;
\r
315 int refSeqIndex = 1;
\r
316 int existingAnnotations = 0;
\r
317 if (al.getAlignmentAnnotation() != null)
\r
319 existingAnnotations = al.getAlignmentAnnotation().length;
\r
322 int alWidth = al.getWidth();
\r
324 StringTokenizer st;
\r
325 Annotation[] annotations;
\r
326 AlignmentAnnotation annotation = null;
\r
328 // First confirm this is an Annotation file
\r
329 boolean jvAnnotationFile = false;
\r
330 while ( (line = in.readLine()) != null)
\r
332 if (line.indexOf("#") == 0)
\r
337 if (line.indexOf("JALVIEW_ANNOTATION") > -1)
\r
339 jvAnnotationFile = true;
\r
344 if (!jvAnnotationFile)
\r
350 while ( (line = in.readLine()) != null)
\r
352 if (line.indexOf("#") == 0
\r
353 || line.indexOf("JALVIEW_ANNOTATION") > -1
\r
354 || line.length() == 0)
\r
359 st = new StringTokenizer(line, "\t");
\r
360 token = st.nextToken();
\r
361 if (token.equalsIgnoreCase("COLOUR"))
\r
363 colourAnnotations(al, st.nextToken(), st.nextToken());
\r
367 else if (token.equalsIgnoreCase("COMBINE"))
\r
369 combineAnnotations(al, st);
\r
373 else if (token.equalsIgnoreCase("GRAPHLINE"))
\r
379 else if (token.equalsIgnoreCase("SEQUENCE_REF"))
\r
381 refSeq = al.findName(st.nextToken());
\r
384 refSeqIndex = Integer.parseInt(st.nextToken());
\r
385 if (refSeqIndex < 1)
\r
388 System.out.println(
\r
389 "WARNING: SEQUENCE_REF index must be > 0 in AnnotationFile");
\r
392 catch (Exception ex)
\r
400 else if (token.equalsIgnoreCase("SEQUENCE_GROUP"))
\r
406 else if (token.equalsIgnoreCase("PROPERTIES"))
\r
408 addProperties(al, st);
\r
412 else if( token.equalsIgnoreCase("BELOW_ALIGNMENT"))
\r
414 setBelowAlignment(al, st);
\r
417 else if( token.equalsIgnoreCase("ALIGNMENT"))
\r
419 addAlignmentDetails(al, st);
\r
423 graphStyle = AlignmentAnnotation.getGraphValueFromString(token);
\r
424 label = st.nextToken();
\r
428 annotations = new Annotation[alWidth];
\r
429 description = null;
\r
430 float score = Float.NaN;
\r
432 if(st.hasMoreTokens())
\r
434 line = st.nextToken();
\r
436 if (line.indexOf("|") ==-1)
\r
438 description = line;
\r
439 if (st.hasMoreTokens())
\r
440 line = st.nextToken();
\r
443 if(st.hasMoreTokens())
\r
445 //This must be the score
\r
446 score = Float.valueOf(st.nextToken()).floatValue();
\r
449 st = new StringTokenizer(line, "|", true);
\r
452 boolean emptyColumn = true;
\r
453 boolean onlyOneElement = (st.countTokens()==1);
\r
455 while (st.hasMoreElements() && index < alWidth)
\r
457 token = st.nextToken().trim();
\r
463 score = Float.valueOf(token).floatValue();
\r
466 catch(NumberFormatException ex){}
\r
469 if (token.equals("|"))
\r
476 emptyColumn = true;
\r
480 annotations[index++] = parseAnnotation(token);
\r
481 emptyColumn = false;
\r
487 annotation = new AlignmentAnnotation(label,
\r
494 annotation.score = score;
\r
496 if (refSeq != null)
\r
498 annotation.belowAlignment=false;
\r
499 annotation.createSequenceMapping(refSeq, refSeqIndex, false);
\r
500 annotation.adjustForAlignment();
\r
501 refSeq.addAlignmentAnnotation(annotation);
\r
504 al.addAnnotation(annotation);
\r
506 al.setAnnotationIndex(annotation,
\r
507 al.getAlignmentAnnotation().length - existingAnnotations -
\r
512 catch (Exception ex)
\r
514 ex.printStackTrace();
\r
515 System.out.println("Problem reading annotation file: " + ex);
\r
521 Annotation parseAnnotation(String string)
\r
523 String desc = null, displayChar = null;
\r
524 char ss = ' '; // secondaryStructure
\r
526 boolean parsedValue = false;
\r
529 java.awt.Color colour = null;
\r
530 int i=string.indexOf("[");
\r
531 int j=string.indexOf("]");
\r
534 UserColourScheme ucs = new UserColourScheme();
\r
536 colour = ucs.getColourFromString(string.substring(i+1,j));
\r
538 string = string.substring(0,i)+string.substring(j+1);
\r
541 StringTokenizer st = new StringTokenizer(string, ",");
\r
543 while (st.hasMoreTokens())
\r
545 token = st.nextToken().trim();
\r
546 if (token.length() == 0)
\r
555 displayChar = token;
\r
556 value = new Float(token).floatValue();
\r
557 parsedValue = true;
\r
560 catch (NumberFormatException ex)
\r
564 if (token.equals("H") || token.equals("E"))
\r
566 // Either this character represents a helix or sheet
\r
567 // or an integer which can be displayed
\r
568 ss = token.charAt(0);
\r
569 if (displayChar.equals(token.substring(0, 1)))
\r
574 else if (desc == null)
\r
581 if (displayChar.length() > 1
\r
583 && desc.length() == 1)
\r
585 String tmp = displayChar;
\r
586 displayChar = desc;
\r
590 Annotation anot = new Annotation(displayChar, desc, ss, value);
\r
592 anot.colour = colour;
\r
597 void colourAnnotations(AlignmentI al, String label, String colour)
\r
599 UserColourScheme ucs = new UserColourScheme(colour);
\r
600 Annotation[] annotations;
\r
601 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
603 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))
\r
605 annotations = al.getAlignmentAnnotation()[i].annotations;
\r
606 for (int j = 0; j < annotations.length; j++)
\r
608 if (annotations[j] != null)
\r
610 annotations[j].colour = ucs.findColour('A');
\r
617 void combineAnnotations(AlignmentI al, StringTokenizer st)
\r
619 int graphGroup = -1;
\r
620 String group = st.nextToken();
\r
621 //First make sure we are not overwriting the graphIndex
\r
622 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
624 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
626 graphGroup = al.getAlignmentAnnotation()[i].graphGroup + 1;
\r
627 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
632 //Now update groups
\r
633 while (st.hasMoreTokens())
\r
635 group = st.nextToken();
\r
636 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
638 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
640 al.getAlignmentAnnotation()[i].graphGroup = graphGroup;
\r
647 void addLine(AlignmentI al, StringTokenizer st)
\r
649 String group = st.nextToken();
\r
650 AlignmentAnnotation annotation = null;
\r
652 for (int i = 0; i < al.getAlignmentAnnotation().length; i++)
\r
654 if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))
\r
656 annotation = al.getAlignmentAnnotation()[i];
\r
661 if (annotation == null)
\r
665 float value = new Float(st.nextToken()).floatValue();
\r
666 String label = st.hasMoreTokens() ? st.nextToken() : null;
\r
667 java.awt.Color colour = null;
\r
668 if (st.hasMoreTokens())
\r
670 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
671 colour = ucs.findColour('A');
\r
674 annotation.setThreshold(new GraphLine(value, label, colour));
\r
677 void addGroup(AlignmentI al, StringTokenizer st)
\r
679 SequenceGroup sg = new SequenceGroup();
\r
680 sg.setName(st.nextToken());
\r
681 sg.setStartRes(Integer.parseInt(st.nextToken()) - 1);
\r
682 sg.setEndRes(Integer.parseInt(st.nextToken()) - 1);
\r
684 String index = st.nextToken();
\r
685 if (index.equals("-1"))
\r
687 while (st.hasMoreElements())
\r
689 sg.addSequence(al.findName(st.nextToken()), false);
\r
694 StringTokenizer st2 = new StringTokenizer(index, ",");
\r
696 while (st2.hasMoreTokens())
\r
698 String tmp = st2.nextToken();
\r
699 if (tmp.equals("*"))
\r
701 for (int i = 0; i < al.getHeight(); i++)
\r
703 sg.addSequence(al.getSequenceAt(i), false);
\r
706 else if (tmp.indexOf("-") >= 0)
\r
708 StringTokenizer st3 = new StringTokenizer(tmp, "-");
\r
710 int start = (Integer.parseInt(st3.nextToken()));
\r
711 int end = (Integer.parseInt(st3.nextToken()));
\r
715 for (int i = start; i <= end; i++)
\r
717 sg.addSequence(al.getSequenceAt(i - 1), false);
\r
723 sg.addSequence(al.getSequenceAt(Integer.parseInt(tmp) - 1), false);
\r
730 if (refSeq != null)
\r
732 sg.setStartRes(refSeq.findIndex(sg.getStartRes() + 1) - 1);
\r
733 sg.setEndRes(refSeq.findIndex(sg.getEndRes() + 1) - 1);
\r
736 if (sg.getSize() > 0)
\r
742 void addProperties(AlignmentI al, StringTokenizer st)
\r
745 //So far we have only added groups to the annotationHash,
\r
746 //the idea is in the future properties can be added to
\r
747 //alignments, other annotations etc
\r
748 if (al.getGroups() == null)
\r
752 SequenceGroup sg = null;
\r
754 String name = st.nextToken();
\r
756 Vector groups = al.getGroups();
\r
757 for (int i = 0; i < groups.size(); i++)
\r
759 sg = (SequenceGroup) groups.elementAt(i);
\r
760 if (sg.getName().equals(name))
\r
772 String keyValue, key, value;
\r
773 while (st.hasMoreTokens())
\r
775 keyValue = st.nextToken();
\r
776 key = keyValue.substring(0, keyValue.indexOf("="));
\r
777 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
779 if (key.equalsIgnoreCase("description"))
\r
781 sg.setDescription(value);
\r
783 else if (key.equalsIgnoreCase("colour"))
\r
785 sg.cs = ColourSchemeProperty.getColour(al, value);
\r
787 else if (key.equalsIgnoreCase("pidThreshold"))
\r
789 sg.cs.setThreshold(Integer.parseInt(value), true);
\r
792 else if (key.equalsIgnoreCase("consThreshold"))
\r
794 sg.cs.setConservationInc(Integer.parseInt(value));
\r
795 Conservation c = new Conservation("Group",
\r
796 ResidueProperties.propHash, 3,
\r
797 sg.getSequences(null),
\r
799 sg.getEndRes() + 1);
\r
802 c.verdict(false, 25);
\r
804 sg.cs.setConservation(c);
\r
807 else if (key.equalsIgnoreCase("outlineColour"))
\r
809 sg.setOutlineColour(new UserColourScheme(value).findColour('A'));
\r
811 else if (key.equalsIgnoreCase("displayBoxes"))
\r
813 sg.setDisplayBoxes(Boolean.valueOf(value).booleanValue());
\r
815 else if (key.equalsIgnoreCase("displayText"))
\r
817 sg.setDisplayText(Boolean.valueOf(value).booleanValue());
\r
819 else if (key.equalsIgnoreCase("colourText"))
\r
821 sg.setColourText(Boolean.valueOf(value).booleanValue());
\r
823 else if (key.equalsIgnoreCase("textCol1"))
\r
825 sg.textColour = new UserColourScheme(value).findColour('A');
\r
827 else if (key.equalsIgnoreCase("textCol2"))
\r
829 sg.textColour2 = new UserColourScheme(value).findColour('A');
\r
831 else if (key.equalsIgnoreCase("textColThreshold"))
\r
833 sg.thresholdTextColour = Integer.parseInt(value);
\r
836 sg.recalcConservation();
\r
841 void setBelowAlignment(AlignmentI al, StringTokenizer st)
\r
844 AlignmentAnnotation aa;
\r
845 while(st.hasMoreTokens())
\r
847 token = st.nextToken();
\r
848 for(int i=0; i<al.getAlignmentAnnotation().length; i++)
\r
850 aa = al.getAlignmentAnnotation()[i];
\r
851 if(aa.sequenceRef==refSeq && aa.label.equals(token))
\r
853 aa.belowAlignment = true;
\r
859 void addAlignmentDetails(AlignmentI al, StringTokenizer st)
\r
861 String keyValue, key, value;
\r
862 while (st.hasMoreTokens())
\r
864 keyValue = st.nextToken();
\r
865 key = keyValue.substring(0, keyValue.indexOf("="));
\r
866 value = keyValue.substring(keyValue.indexOf("=") + 1);
\r
867 al.setProperty(key,value);
\r