++ --
[jalview.git] / src / jalview / io / AnnotationReader.java
index 5ba1e26..cd7abfd 100755 (executable)
@@ -4,89 +4,43 @@ import java.io.*;
 import jalview.datamodel.*;\r
 import java.util.StringTokenizer;\r
 import jalview.schemes.UserColourScheme;\r
-import jalview.gui.FeatureRenderer;\r
+import java.net.URL;\r
 \r
 public class AnnotationReader\r
 {\r
-  public boolean readGroupsFile(FeatureRenderer fr, AlignmentI al, String file)\r
+  public boolean readAnnotationFile(AlignmentI al, String file)\r
   {\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
+      BufferedReader in = null;\r
+      java.io.InputStream is = getClass().getResourceAsStream("/" + file);\r
+      if (is != null)\r
       {\r
-        lineNo++;\r
-        st = new StringTokenizer(line, "\t");\r
-        if (st.countTokens() == 2)\r
+        in = new BufferedReader(new java.io.InputStreamReader(is));\r
+      }\r
+      else\r
+      {\r
+        try\r
         {\r
-          type = st.nextToken();\r
-          UserColourScheme ucs = new UserColourScheme(st.nextToken());\r
-          fr.setColour(type, ucs.findColour("A"));\r
-          continue;\r
+          URL url = new URL(file);\r
+          in = new BufferedReader(new InputStreamReader(url.openStream()));\r
         }\r
-\r
-        while (st.hasMoreElements())\r
+        catch (java.net.MalformedURLException ex)\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
+          in = new BufferedReader(new FileReader(file));\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
+      int refSeqIndex = 1;\r
+      int existingAnnotations = 0;\r
+      if(al.getAlignmentAnnotation()!=null)\r
+       existingAnnotations = al.getAlignmentAnnotation().length;\r
 \r
+      int alWidth = al.getWidth();\r
 \r
       StringTokenizer st;\r
       Annotation[] annotations;\r
@@ -146,30 +100,34 @@ public class AnnotationReader
           try{\r
             refSeqIndex = Integer.parseInt(st.nextToken());\r
           }\r
-          catch(NumberFormatException ex)\r
+          catch(Exception ex)\r
           {\r
-            refSeqIndex = 0;\r
+            refSeqIndex = 1;\r
           }\r
 \r
           continue;\r
         }\r
 \r
+\r
         graphStyle = AlignmentAnnotation.getGraphValueFromString(token);\r
         label = description = st.nextToken();\r
 \r
         line = st.nextToken();\r
+\r
         st = new StringTokenizer(line, "|", true);\r
-        annotations = new Annotation[st.countTokens()];\r
+        annotations = new Annotation[alWidth];\r
+\r
         index = 0;\r
         boolean emptyColumn = true;\r
-        while (st.hasMoreElements())\r
+\r
+\r
+        while (st.hasMoreElements() && index<alWidth)\r
         {\r
           token = st.nextToken().trim();\r
-\r
           if(token.equals("|"))\r
           {\r
             if(emptyColumn)\r
-              annotations[index++] = parseAnnotation("");\r
+              index++;\r
 \r
             emptyColumn = true;\r
           }\r
@@ -187,12 +145,21 @@ public class AnnotationReader
                                           0,\r
                                           graphStyle);\r
 \r
-       al.addAnnotation(annotation, refSeq, refSeqIndex);\r
+       if(refSeq!=null)\r
+       {\r
+         annotation.createSequenceMapping(refSeq, refSeqIndex);\r
+         refSeq.addAlignmentAnnotation(annotation);\r
+       }\r
 \r
+       al.addAnnotation(annotation);\r
+\r
+       al.setAnnotationIndex(annotation,  al.getAlignmentAnnotation().length - existingAnnotations-1);\r
       }\r
+\r
     }catch(Exception ex)\r
-    {ex.printStackTrace();\r
-    //  System.out.println("Problem reading annotation file: "+ex);\r
+    {\r
+      ex.printStackTrace();\r
+      System.out.println("Problem reading annotation file: "+ex);\r
       return false;\r
     }\r
     return true;\r
@@ -203,6 +170,7 @@ public class AnnotationReader
     String desc = "", displayChar="";\r
     char ss = ' '; // secondaryStructure\r
     float value = 0;\r
+    boolean parsedValue = false;\r
     StringTokenizer st = new StringTokenizer(string, ",");\r
     String token;\r
     while(st.hasMoreTokens())\r
@@ -211,29 +179,24 @@ public class AnnotationReader
       if(token.length()==0)\r
         continue;\r
 \r
-      if(value==0)\r
-      {  try{\r
-          value = Float.parseFloat(token);\r
+      if(!parsedValue)\r
+      {\r
+        try{\r
+          value = new Float(token).floatValue();\r
+          displayChar = token;\r
+          parsedValue = true;\r
         }catch(NumberFormatException ex){}\r
       }\r
 \r
-      if(token.length()==1)\r
+      if(token.equals("H") || token.equals("E"))\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
+        ss = token.charAt(0);\r
       }\r
       else if(desc.length()<1)\r
         desc = token;\r
-      else\r
-        displayChar = token;\r
+\r
     }\r
 \r
     return new Annotation(displayChar, desc, ss, value);\r
@@ -303,8 +266,7 @@ public class AnnotationReader
 \r
     if(annotation==null)\r
       return;\r
-\r
-    float value = Float.parseFloat(st.nextToken());\r
+    float value = new Float(st.nextToken()).floatValue();\r
     String label = st.hasMoreTokens() ?  st.nextToken() : null;\r
     java.awt.Color colour = null;\r
     if(st.hasMoreTokens())\r
@@ -313,7 +275,7 @@ public class AnnotationReader
       colour = ucs.findColour("A");\r
     }\r
 \r
-    annotation.addGraphLine(new GraphLine(value, label, colour));\r
+    annotation.setThreshold(new GraphLine(value, label, colour));\r
 \r
   }\r
 }\r