Merge branch 'develop' into develop_m2_8_1_2
[jalview.git] / src / jalview / io / StockholmFile.java
index 579aff4..4d8f136 100644 (file)
@@ -22,6 +22,7 @@ package jalview.io;
 \r
 import jalview.datamodel.AlignmentI;\r
 import jalview.datamodel.AlignmentAnnotation;\r
+import jalview.datamodel.AlignmentI;\r
 import jalview.datamodel.Annotation;\r
 import jalview.datamodel.DBRefEntry;\r
 import jalview.datamodel.Mapping;\r
@@ -57,6 +58,9 @@ import fr.orsay.lri.varna.models.rna.RNA;
  * into Jalview's local representation.\r
  * \r
  * @author bsb at sanger.ac.uk\r
+ * @author Natasha Shersnev (Dundee, UK) (Stockholm file writer)\r
+ * @author Lauren Lui (UCSC, USA) (RNA secondary structure annotation import as stockholm)\r
+ * @author Anne Menard (Paris, FR) (VARNA parsing of Stockholm file data)\r
  * @version 0.3 + jalview mods\r
  * \r
  */\r
@@ -64,10 +68,9 @@ public class StockholmFile extends AlignFile
 {\r
   // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");\r
   protected ArrayList<RNA> result;\r
-  \r
-  StringBuffer out; // output buffer\r
-\r
-  AlignmentI al;\r
+  StringBuffer out; // output buffer
+
+  AlignmentI al;
 \r
   public StockholmFile()\r
   {\r
@@ -95,7 +98,72 @@ public class StockholmFile extends AlignFile
   {\r
     super.initData();\r
   }\r
+  /**\r
+   * Parse a file in Stockholm format into Jalview's data model using VARNA\r
+   * \r
+   * @throws IOException\r
+   *           If there is an error with the input file\r
+   */\r
+  public void parse_with_VARNA(java.io.File inFile) throws IOException\r
+  {\r
+    FileReader fr = null;\r
+    fr = new FileReader(inFile);\r
+\r
+    BufferedReader r = new BufferedReader(fr);\r
+    result = null;\r
+    try\r
+    {\r
+      result = RNAFactory.loadSecStrStockholm(r);\r
+    } catch (ExceptionUnmatchedClosingParentheses umcp)\r
+    {\r
+      errormessage = "Unmatched parentheses in annotation. Aborting ("\r
+              + umcp.getMessage() + ")";\r
+      throw new IOException(umcp);\r
+    }\r
+    // DEBUG System.out.println("this is the secondary scructure:"\r
+    // +result.size());\r
+    SequenceI[] seqs = new SequenceI[result.size()];\r
+    String id=null;\r
+    for (int i = 0; i < result.size(); i++)\r
+    {\r
+      // DEBUG System.err.println("Processing i'th sequence in Stockholm file")\r
+      RNA current = result.get(i);\r
 \r
+      String seq = current.getSeq();\r
+      String rna = current.getStructDBN(true);\r
+      // DEBUG System.out.println(seq);\r
+      // DEBUG System.err.println(rna);\r
+      int begin = 0;\r
+      int end = seq.length() - 1;\r
+      id = safeName(getDataName());\r
+      seqs[i] = new Sequence(id, seq, begin, end);\r
+      String[] annot = new String[rna.length()];\r
+      Annotation[] ann = new Annotation[rna.length()];\r
+      for (int j = 0; j < rna.length(); j++)\r
+      {\r
+        annot[j] = rna.substring(j, j + 1);\r
+\r
+      }\r
+\r
+      for (int k = 0; k < rna.length(); k++)\r
+      {\r
+        ann[k] = new Annotation(annot[k], "",\r
+                jalview.schemes.ResidueProperties.getRNASecStrucState(\r
+                        annot[k]).charAt(0), 0f);\r
+\r
+      }\r
+      AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",\r
+              current.getID(), ann);\r
+\r
+      seqs[i].addAlignmentAnnotation(align);\r
+      seqs[i].setRNA(result.get(i));\r
+      this.annotations.addElement(align);\r
+    }\r
+    this.setSeqs(seqs);\r
+\r
+  }\r
+\r
+  \r
   /**\r
    * Parse a file in Stockholm format into Jalview's data model. The file has to\r
    * be passed at construction time\r
@@ -110,7 +178,7 @@ public class StockholmFile extends AlignFile
       // --------------- Variable Definitions -------------------\r
       String line;\r
       String version;\r
-    // String id;
+    // String id;\r
       Hashtable seqAnn = new Hashtable(); // Sequence related annotations\r
       Hashtable seqs = new Hashtable();\r
       Regex p, r, rend, s, x;\r
@@ -167,24 +235,24 @@ public class StockholmFile extends AlignFile
              if (rend.search(line))\r
              {\r
                // End of the alignment, pass stuff back\r
-        this.noSeqs = seqs.size();
+        this.noSeqs = seqs.size();\r
        \r
-        String seqdb,dbsource = null;
-        Regex pf = new Regex("PF[0-9]{5}(.*)"); // Finds AC for Pfam
-        Regex rf = new Regex("RF[0-9]{5}(.*)"); // Finds AC for Rfam
-        if (getAlignmentProperty("AC") != null)
-        {
-          String dbType = getAlignmentProperty("AC").toString();
-          if (pf.search(dbType))
-          {
-            // PFAM Alignment - so references are typically from Uniprot
-            dbsource = "PFAM";
-          }
-          else if (rf.search(dbType))
-          {
-            dbsource = "RFAM";
-          }
-        }
+        String seqdb,dbsource = null;\r
+        Regex pf = new Regex("PF[0-9]{5}(.*)"); // Finds AC for Pfam\r
+        Regex rf = new Regex("RF[0-9]{5}(.*)"); // Finds AC for Rfam\r
+        if (getAlignmentProperty("AC") != null)\r
+        {\r
+          String dbType = getAlignmentProperty("AC").toString();\r
+          if (pf.search(dbType))\r
+          {\r
+            // PFAM Alignment - so references are typically from Uniprot\r
+            dbsource = "PFAM";\r
+          }\r
+          else if (rf.search(dbType))\r
+          {\r
+            dbsource = "RFAM";\r
+          }\r
+        }\r
                // logger.debug("Number of sequences: " + this.noSeqs);\r
                Enumeration accs = seqs.keys();\r
                while (accs.hasMoreElements())\r
@@ -200,8 +268,8 @@ public class StockholmFile extends AlignFile
                  int end = -1;\r
                  String sid = acc;\r
                  /*\r
-           * Retrieve hash of annotations for this accession Associate
-           * Annotation with accession
+           * Retrieve hash of annotations for this accession Associate\r
+           * Annotation with accession\r
                   */\r
                  Hashtable accAnnotations = null;\r
        \r
@@ -238,21 +306,21 @@ public class StockholmFile extends AlignFile
                      jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);\r
                    }\r
                  }        \r
-
-          if (accAnnotations != null && accAnnotations.containsKey("AC"))
-          {
-            if (dbsource != null)
-            {
-              String dbr = (String) accAnnotations.get("AC");
-              if (dbr != null)
+\r
+          if (accAnnotations != null && accAnnotations.containsKey("AC"))\r
+          {\r
+            if (dbsource != null)\r
+            {\r
+              String dbr = (String) accAnnotations.get("AC");\r
+              if (dbr != null)\r
                  {\r
-                // we could get very clever here - but for now - just try to guess accession type from source of alignment plus structure of accession
-                guessDatabaseFor(seqO, dbr, dbsource);
+                // we could get very clever here - but for now - just try to guess accession type from source of alignment plus structure of accession\r
+                guessDatabaseFor(seqO, dbr, dbsource);\r
                          \r
                            }\r
                  }\r
-            // else - do what ?  add the data anyway and prompt the user to specify what references these are ?
-          }
+            // else - do what ?  add the data anyway and prompt the user to specify what references these are ?\r
+          }\r
                \r
                  Hashtable features = null;\r
                  // We need to adjust the positions of all features to account for gaps\r
@@ -278,24 +346,24 @@ public class StockholmFile extends AlignFile
                      // TODO: map coding region to core jalview feature types\r
                      String type = i.nextElement().toString();\r
                      Hashtable content = (Hashtable) features.remove(type);\r
-
-              // add alignment annotation for this feature
-              String key = type2id(type);
-              if (key != null)
-              {
-                if (accAnnotations != null
-                        && accAnnotations.containsKey(key))
-                {
-                  Vector vv = (Vector) accAnnotations.get(key);
-                  for (int ii = 0; ii < vv.size(); ii++)
-                  {
-                    AlignmentAnnotation an = (AlignmentAnnotation) vv
-                            .elementAt(ii);
-                    seqO.addAlignmentAnnotation(an);
-                  }
-                }
-              }
-
+\r
+              // add alignment annotation for this feature\r
+              String key = type2id(type);\r
+              if (key != null)\r
+              {\r
+                if (accAnnotations != null\r
+                        && accAnnotations.containsKey(key))\r
+                {\r
+                  Vector vv = (Vector) accAnnotations.get(key);\r
+                  for (int ii = 0; ii < vv.size(); ii++)\r
+                  {\r
+                    AlignmentAnnotation an = (AlignmentAnnotation) vv\r
+                            .elementAt(ii);\r
+                    seqO.addAlignmentAnnotation(an);\r
+                  }\r
+                }\r
+              }\r
+\r
                      Enumeration j = content.keys();\r
                      while (j.hasMoreElements())\r
                      {\r
@@ -513,8 +581,8 @@ public class StockholmFile extends AlignFile
                      ann = new Hashtable();\r
                      seqAnn.put(acc, ann);\r
                    }\r
-            // TODO test structure, call parseAnnotationRow with vector from
-            // hashtable for specific sequence
+            // TODO test structure, call parseAnnotationRow with vector from\r
+            // hashtable for specific sequence\r
                    Hashtable features;\r
                    // Get an object with all the content for an annotation\r
                    if (ann.containsKey("features"))\r
@@ -549,7 +617,7 @@ public class StockholmFile extends AlignFile
                    }\r
                    ns += seq;\r
                    content.put(description, ns);\r
-       
+       \r
 //                 if(type.equals("SS")){\r
                        Hashtable strucAnn;\r
                        if (seqAnn.containsKey(acc))\r
@@ -682,72 +750,6 @@ public class StockholmFile extends AlignFile
     }\r
   }\r
 \r
-\r
-  /**\r
-   * Parse a file in Stockholm format into Jalview's data model using VARNA\r
-   * \r
-   * @throws IOException\r
-   *           If there is an error with the input file\r
-   */\r
-  public void parse_with_VARNA() throws IOException\r
-  {\r
-    FileReader fr = null;\r
-    fr = new FileReader(inFile);\r
-\r
-    BufferedReader r = new BufferedReader(fr);\r
-    result = null;\r
-    try\r
-    {\r
-      result = RNAFactory.loadSecStrStockholm(r);\r
-    } catch (ExceptionUnmatchedClosingParentheses umcp)\r
-    {\r
-      errormessage = "Unmatched parentheses in annotation. Aborting ("\r
-              + umcp.getMessage() + ")";\r
-      throw new IOException(umcp);\r
-    }\r
-    // DEBUG System.out.println("this is the secondary scructure:"\r
-    // +result.size());\r
-    SequenceI[] seqs = new SequenceI[result.size()];\r
-    String id=null;\r
-    for (int i = 0; i < result.size(); i++)\r
-    {\r
-      // DEBUG System.err.println("Processing i'th sequence in Stockholm file")\r
-      RNA current = result.get(i);\r
-\r
-      String seq = current.getSeq();\r
-      String rna = current.getStructDBN(true);\r
-      // DEBUG System.out.println(seq);\r
-      // DEBUG System.err.println(rna);\r
-      int begin = 0;\r
-      int end = seq.length() - 1;\r
-      id = safeName(getDataName());\r
-      seqs[i] = new Sequence(id, seq, begin, end);\r
-      String[] annot = new String[rna.length()];\r
-      Annotation[] ann = new Annotation[rna.length()];\r
-      for (int j = 0; j < rna.length(); j++)\r
-      {\r
-        annot[j] = rna.substring(j, j + 1);\r
-\r
-      }\r
-\r
-      for (int k = 0; k < rna.length(); k++)\r
-      {\r
-        ann[k] = new Annotation(annot[k], "",\r
-                jalview.schemes.ResidueProperties.getRNASecStrucState(\r
-                        annot[k]).charAt(0), 0f);\r
-\r
-      }\r
-      AlignmentAnnotation align = new AlignmentAnnotation("Sec. str.",\r
-              current.getID(), ann);\r
-\r
-      seqs[i].addAlignmentAnnotation(align);\r
-      seqs[i].setRNA(result.get(i));\r
-      this.annotations.addElement(align);\r
-    }\r
-    this.setSeqs(seqs);\r
-\r
-  }\r
-\r
   protected static AlignmentAnnotation parseAnnotationRow(\r
           Vector annotation, String label, String annots)\r
   {\r
@@ -1078,27 +1080,27 @@ public class StockholmFile extends AlignFile
             + id);\r
     return id;\r
   }\r
-
-  protected static String type2id(String type)
-  {
-    String key = null;
-    Enumeration e = typeIds.keys();
-    while (e.hasMoreElements())
-    {
-      Object ll = e.nextElement();
-      if (typeIds.get(ll).toString().equals(type))
-      {
-        key = (String) ll;
-        break;
-      }
-    }
-    if (key != null)
-    {
-      return (String) key;
-    }
-    System.err.println("Warning : Unknown Stockholm annotation type: "
-            + type);
-    return key;
+\r
+  protected static String type2id(String type)\r
+  {\r
+    String key = null;\r
+    Enumeration e = typeIds.keys();\r
+    while (e.hasMoreElements())\r
+    {\r
+      Object ll = e.nextElement();\r
+      if (typeIds.get(ll).toString().equals(type))\r
+      {\r
+        key = (String) ll;\r
+        break;\r
+      }\r
+    }\r
+    if (key != null)\r
+    {\r
+      return (String) key;\r
+    }\r
+    System.err.println("Warning : Unknown Stockholm annotation type: "\r
+            + type);\r
+    return key;\r
   }
   /**\r
    * make a friendly ID string.\r