Merge branch 'jims_annotate3d_update' into menard_finalsep2012
[jalview.git] / src / jalview / io / StockholmFile.java
index 71cc5c5..a9b8d37 100644 (file)
@@ -26,24 +26,21 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;\r
 import jalview.datamodel.SequenceI;\r
 \r
-import java.io.*;\r
-import java.util.*;\r
-\r
-\r
-import javax.xml.parsers.ParserConfigurationException;\r
-\r
-import org.xml.sax.SAXException;\r
+import java.io.BufferedReader;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+import java.util.StringTokenizer;\r
+import java.util.Vector;\r
 \r
 import com.stevesoft.pat.Regex;\r
 \r
-import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;\r
-import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;\r
-import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;\r
 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;\r
+import fr.orsay.lri.varna.factories.RNAFactory;\r
 import fr.orsay.lri.varna.models.rna.RNA;\r
 \r
-\r
-\r
 // import org.apache.log4j.*;\r
 \r
 /**\r
@@ -62,18 +59,19 @@ public class StockholmFile extends AlignFile
 {\r
   // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");\r
   protected ArrayList<RNA> result;\r
+\r
   public String id;\r
-  \r
+\r
   public StockholmFile()\r
   {\r
   }\r
 \r
-  public StockholmFile(String inFile, String type) throws Exception \r
+  public StockholmFile(String inFile, String type) throws IOException\r
   {\r
     super(inFile, type);\r
   }\r
 \r
-  public StockholmFile(FileParse source) throws Exception \r
+  public StockholmFile(FileParse source) throws IOException\r
   {\r
     super(source);\r
   }\r
@@ -89,11 +87,9 @@ public class StockholmFile extends AlignFile
    * \r
    * @throws IOException\r
    *           If there is an error with the input file\r
- * @throws ExceptionUnmatchedClosingParentheses \r
    */\r
   public void parse() throws IOException\r
   {\r
-       \r
       StringBuffer treeString = new StringBuffer();\r
       String treeName = null;\r
       // --------------- Variable Definitions -------------------\r
@@ -544,8 +540,73 @@ public class StockholmFile extends AlignFile
                }\r
        }\r
 \r
-  protected static AlignmentAnnotation parseAnnotationRow(Vector annotation,\r
-          String label, String annots)\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
+    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
     String convert1, convert2 = null;\r
 \r
@@ -553,7 +614,7 @@ public class StockholmFile extends AlignFile
     Regex openparen = new Regex("(<|\\[)", "(");\r
     Regex closeparen = new Regex("(>|\\])", ")");\r
 \r
-   // Detect if file is RNA by looking for bracket types\r
+    // Detect if file is RNA by looking for bracket types\r
     Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");\r
 \r
     convert1 = openparen.replaceAll(annots);\r
@@ -581,7 +642,7 @@ public class StockholmFile extends AlignFile
         if (detectbrackets.search(pos))\r
         {\r
           ann.secondaryStructure = jalview.schemes.ResidueProperties\r
-                 .getRNASecStrucState(pos).charAt(0);\r
+                  .getRNASecStrucState(pos).charAt(0);\r
         }\r
         else\r
         {\r
@@ -594,14 +655,14 @@ public class StockholmFile extends AlignFile
           ann.displayCharacter = ""; // null; // " ";\r
         }\r
         else\r
-       {\r
+        {\r
           ann.displayCharacter = " " + ann.displayCharacter;\r
         }\r
       }\r
 \r
       els[i] = ann;\r
     }\r
-   AlignmentAnnotation annot = null;\r
+    AlignmentAnnotation annot = null;\r
     Enumeration e = annotation.elements();\r
     while (e.hasMoreElements())\r
     {\r
@@ -616,16 +677,16 @@ public class StockholmFile extends AlignFile
       annotation.addElement(annot);\r
     }\r
     else\r
-   {\r
+    {\r
       Annotation[] anns = new Annotation[annot.annotations.length\r
               + els.length];\r
-     System.arraycopy(annot.annotations, 0, anns, 0,\r
-             annot.annotations.length);\r
+      System.arraycopy(annot.annotations, 0, anns, 0,\r
+              annot.annotations.length);\r
       System.arraycopy(els, 0, anns, annot.annotations.length, els.length);\r
       annot.annotations = anns;\r
-      //System.out.println("else: ");\r
-   }\r
-   return annot;\r
+      // System.out.println("else: ");\r
+    }\r
+    return annot;\r
   }\r
 \r
   public static String print(SequenceI[] s)\r
@@ -674,41 +735,6 @@ public class StockholmFile extends AlignFile
     return id;\r
   }\r
   /**\r
-   * //ssline is complete secondary structure line private AlignmentAnnotation\r
-   * addHelices(Vector annotation, String label, String ssline) {\r
-   * \r
-   * // decide on secondary structure or not. Annotation[] els = new\r
-   * Annotation[ssline.length()]; for (int i = 0; i < ssline.length(); i++) {\r
-   * String pos = ssline.substring(i, i + 1); Annotation ann; ann = new\r
-   * Annotation(pos, "", ' ', 0f); // 0f is 'valid' null - will not\r
-   * \r
-   * ann.secondaryStructure =\r
-   * jalview.schemes.ResidueProperties.getRNAssState(pos).charAt(0);\r
-   * \r
-   * ann.displayCharacter = "x" + ann.displayCharacter;\r
-   * \r
-   * System.out.println(ann.displayCharacter);\r
-   * \r
-   * els[i] = ann; } AlignmentAnnotation helicesAnnot = null; Enumeration e =\r
-   * annotation.elements(); while (e.hasMoreElements()) { helicesAnnot =\r
-   * (AlignmentAnnotation) e.nextElement(); if (helicesAnnot.label.equals(type))\r
-   * break; helicesAnnot = null; } if (helicesAnnot == null) { helicesAnnot =\r
-   * new AlignmentAnnotation(type, type, els);\r
-   * annotation.addElement(helicesAnnot); } else { Annotation[] anns = new\r
-   * Annotation[helicesAnnot.annotations.length + els.length];\r
-   * System.arraycopy(helicesAnnot.annotations, 0, anns, 0,\r
-   * helicesAnnot.annotations.length); System.arraycopy(els, 0, anns,\r
-   * helicesAnnot.annotations.length, els.length); helicesAnnot.annotations =\r
-   * anns; }\r
-   * \r
-   * helicesAnnot.features = Rna.GetBasePairs(ssline);\r
-   * Rna.HelixMap(helicesAnnot.features);\r
-   * \r
-   * \r
-   * return helicesAnnot; }\r
-   * \r
-   */\r
-  /**\r
    * make a friendly ID string.\r
    * \r
    * @param dataName\r
@@ -717,13 +743,13 @@ public class StockholmFile extends AlignFile
   private String safeName(String dataName)\r
   {\r
     int b = 0;\r
-    while ((b = dataName.indexOf("/")) > -1 && b < dataName.length() )\r
-    {             \r
-       dataName = dataName.substring(b + 1).trim();\r
-       \r
+    while ((b = dataName.indexOf("/")) > -1 && b < dataName.length())\r
+    {\r
+      dataName = dataName.substring(b + 1).trim();\r
+\r
     }\r
-    int e = (dataName.length() - dataName.indexOf("."))+1;\r
-    dataName = dataName.substring(1,e).trim();\r
+    int e = (dataName.length() - dataName.indexOf(".")) + 1;\r
+    dataName = dataName.substring(1, e).trim();\r
     return dataName;\r
   }\r
 }\r