+package jalview.io;\r
+\r
+import java.io.*;\r
+import jalview.datamodel.*;\r
+import java.util.StringTokenizer;\r
+import jalview.schemes.UserColourScheme;\r
+import jalview.gui.FeatureRenderer;\r
+\r
+public class AnnotationReader\r
+{\r
+ public boolean readGroupsFile(FeatureRenderer fr, AlignmentI al, String file)\r
+ {\r
+ try\r
+ {\r
+ BufferedReader in = new BufferedReader(new FileReader(file));\r
+ SequenceI seq = null;\r
+ String line, type, desc, token;\r
+\r
+ int index, start, end;\r
+ StringTokenizer st;\r
+ SequenceFeature sf;\r
+ int lineNo = 0;\r
+ while ( (line = in.readLine()) != null)\r
+ {\r
+ lineNo++;\r
+ st = new StringTokenizer(line, "\t");\r
+ if (st.countTokens() == 2)\r
+ {\r
+ type = st.nextToken();\r
+ UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
+ fr.setColour(type, ucs.findColour("A"));\r
+ continue;\r
+ }\r
+\r
+ while (st.hasMoreElements())\r
+ {\r
+ desc = st.nextToken();\r
+ token = st.nextToken();\r
+ if (!token.equals("ID_NOT_SPECIFIED"))\r
+ {\r
+ index = al.findIndex(al.findName(token));\r
+ st.nextToken();\r
+ }\r
+ else\r
+ {\r
+ index = Integer.parseInt(st.nextToken());\r
+ }\r
+\r
+ start = Integer.parseInt(st.nextToken());\r
+ end = Integer.parseInt(st.nextToken());\r
+\r
+ seq = al.getSequenceAt(index);\r
+ start = seq.findIndex(start) - 1;\r
+ end = seq.findIndex(end) - 1;\r
+\r
+ type = st.nextToken();\r
+\r
+ if (fr.getColour(type) == null)\r
+ {\r
+ // Probably the old style groups file\r
+ UserColourScheme ucs = new UserColourScheme(type);\r
+ fr.setColour(type, ucs.findColour("A"));\r
+ }\r
+\r
+ sf = new SequenceFeature(type, desc, "", start, end);\r
+\r
+ seq.getDatasetSequence().addSequenceFeature(sf);\r
+ }\r
+ }\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ System.out.println("Error parsing groups file: " + ex);\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ public boolean readAnnotationFile(AlignmentI al, String file)\r
+ {\r
+ try\r
+ {\r
+ BufferedReader in = new BufferedReader(new FileReader(file));\r
+ String line, label, description, token;\r
+ int graphStyle, index;\r
+ SequenceI refSeq = null;\r
+ int refSeqIndex = -1;\r
+\r
+\r
+ StringTokenizer st;\r
+ Annotation[] annotations;\r
+ AlignmentAnnotation annotation = null;\r
+\r
+ // First confirm this is an Annotation file\r
+ boolean jvAnnotationFile = false;\r
+ while ( (line = in.readLine()) != null)\r
+ {\r
+ if (line.indexOf("#") == 0 )\r
+ continue;\r
+\r
+ if (line.indexOf("JALVIEW_ANNOTATION") > -1)\r
+ {\r
+ jvAnnotationFile = true;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if(!jvAnnotationFile)\r
+ {\r
+ in.close();\r
+ return false;\r
+ }\r
+\r
+ while ( (line = in.readLine()) != null)\r
+ {\r
+ if(line.indexOf("#")==0\r
+ || line.indexOf("JALVIEW_ANNOTATION")>-1\r
+ || line.length()==0)\r
+ continue;\r
+\r
+ st = new StringTokenizer(line, "\t");\r
+ token = st.nextToken();\r
+ if(token.equalsIgnoreCase("COLOUR"))\r
+ {\r
+ colourAnnotations(al, st.nextToken(), st.nextToken());\r
+ continue;\r
+ }\r
+\r
+ if(token.equalsIgnoreCase("COMBINE") )\r
+ {\r
+ combineAnnotations(al, st);\r
+ continue;\r
+ }\r
+\r
+ if (token.equalsIgnoreCase("GRAPHLINE"))\r
+ {\r
+ addLine(al, st);\r
+ continue;\r
+ }\r
+\r
+\r
+ if(token.equalsIgnoreCase("SEQUENCE_REF") )\r
+ {\r
+ refSeq = al.findName(st.nextToken());\r
+ try{\r
+ refSeqIndex = Integer.parseInt(st.nextToken());\r
+ }\r
+ catch(NumberFormatException ex)\r
+ {\r
+ refSeqIndex = 0;\r
+ }\r
+\r
+ continue;\r
+ }\r
+\r
+ graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
+ label = description = st.nextToken();\r
+\r
+ line = st.nextToken();\r
+ st = new StringTokenizer(line, "|", true);\r
+ annotations = new Annotation[st.countTokens()];\r
+ index = 0;\r
+ boolean emptyColumn = true;\r
+ while (st.hasMoreElements())\r
+ {\r
+ token = st.nextToken().trim();\r
+\r
+ if(token.equals("|"))\r
+ {\r
+ if(emptyColumn)\r
+ annotations[index++] = parseAnnotation("");\r
+\r
+ emptyColumn = true;\r
+ }\r
+ else\r
+ {\r
+ annotations[index++] = parseAnnotation(token);\r
+ emptyColumn = false;\r
+ }\r
+ }\r
+\r
+ annotation = new AlignmentAnnotation(label,\r
+ description,\r
+ annotations,\r
+ 0,\r
+ 0,\r
+ graphStyle);\r
+\r
+ al.addAnnotation(annotation, refSeq, refSeqIndex);\r
+\r
+ }\r
+ }catch(Exception ex)\r
+ {ex.printStackTrace();\r
+ // System.out.println("Problem reading annotation file: "+ex);\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ Annotation parseAnnotation(String string)\r
+ {\r
+ String desc = "", displayChar="";\r
+ char ss = ' '; // secondaryStructure\r
+ float value = 0;\r
+ StringTokenizer st = new StringTokenizer(string, ",");\r
+ String token;\r
+ while(st.hasMoreTokens())\r
+ {\r
+ token = st.nextToken().trim();\r
+ if(token.length()==0)\r
+ continue;\r
+\r
+ if(value==0)\r
+ { try{\r
+ value = Float.parseFloat(token);\r
+ }catch(NumberFormatException ex){}\r
+ }\r
+\r
+ if(token.length()==1)\r
+ {\r
+ // Either this character represents a helix or sheet\r
+ // or an integer which can be displayed\r
+ if(token.equals("H") || token.equals("E"))\r
+ {\r
+ ss = token.charAt(0);\r
+ }\r
+ else //if(value!=0)\r
+ {\r
+ displayChar = token;\r
+ }\r
+ }\r
+ else if(desc.length()<1)\r
+ desc = token;\r
+ else\r
+ displayChar = token;\r
+ }\r
+\r
+ return new Annotation(displayChar, desc, ss, value);\r
+ }\r
+\r
+ void colourAnnotations(AlignmentI al, String label, String colour)\r
+ {\r
+ UserColourScheme ucs = new UserColourScheme(colour);\r
+ Annotation[] annotations;\r
+ for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+ {\r
+ if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(label))\r
+ {\r
+ annotations = al.getAlignmentAnnotation()[i].annotations;\r
+ for(int j=0; j<annotations.length; j++)\r
+ {\r
+ if(annotations[j]!=null)\r
+ annotations[j].colour = ucs.findColour("A");\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ void combineAnnotations(AlignmentI al, StringTokenizer st)\r
+ {\r
+ int graphGroup = -1;\r
+ String group = st.nextToken();\r
+ //First make sure we are not overwriting the graphIndex\r
+ for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+ {\r
+ if(al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
+ {\r
+ graphGroup = al.getAlignmentAnnotation()[i].graphGroup +1;\r
+ al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
+ break;\r
+ }\r
+ }\r
+\r
+ //Now update groups\r
+ while(st.hasMoreTokens())\r
+ {\r
+ group = st.nextToken();\r
+ for(int i=0; i<al.getAlignmentAnnotation().length; i++)\r
+ {\r
+ if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
+ {\r
+ al.getAlignmentAnnotation()[i].graphGroup = graphGroup;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ void addLine(AlignmentI al, StringTokenizer st)\r
+ {\r
+ String group = st.nextToken();\r
+ AlignmentAnnotation annotation = null;\r
+\r
+ for (int i = 0; i < al.getAlignmentAnnotation().length; i++)\r
+ {\r
+ if (al.getAlignmentAnnotation()[i].label.equalsIgnoreCase(group))\r
+ {\r
+ annotation = al.getAlignmentAnnotation()[i];\r
+ break;\r
+ }\r
+ }\r
+\r
+ if(annotation==null)\r
+ return;\r
+\r
+ float value = Float.parseFloat(st.nextToken());\r
+ String label = st.hasMoreTokens() ? st.nextToken() : null;\r
+ java.awt.Color colour = null;\r
+ if(st.hasMoreTokens())\r
+ {\r
+ UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
+ colour = ucs.findColour("A");\r
+ }\r
+\r
+ annotation.addGraphLine(new GraphLine(value, label, colour));\r
+\r
+ }\r
+}\r