Start merging of Laurens Code
[jalview.git] / src / jalview / io / StockholmFile.java
index e81c31a..2e0fea7 100644 (file)
@@ -25,6 +25,7 @@ import java.util.*;
 \r
 import com.stevesoft.pat.*;\r
 import jalview.datamodel.*;\r
+import jalview.analysis.Rna;\r
 \r
 // import org.apache.log4j.*;\r
 \r
@@ -82,6 +83,9 @@ public class StockholmFile extends AlignFile
     Hashtable seqs = new Hashtable();\r
     Regex p, r, rend, s, x;\r
 \r
+    // Temporary line for processing RNA annotation\r
+    // String RNAannot = "";\r
+\r
     // ------------------ Parsing File ----------------------\r
     // First, we have to check that this file has STOCKHOLM format, i.e. the\r
     // first line must match\r
@@ -105,11 +109,20 @@ public class StockholmFile extends AlignFile
     r = new Regex("#=(G[FSRC]?)\\s+(.*)"); // Finds any annotation line\r
     x = new Regex("(\\S+)\\s+(\\S+)"); // split id from sequence\r
 \r
+    // Convert all bracket types to parentheses (necessary for passing to VARNA)\r
+    Regex openparen = new Regex("(<|\\[)", "(");\r
+    Regex closeparen = new Regex("(>|\\])", ")");\r
+\r
+    // Detect if file is RNA by looking for bracket types\r
+    Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");\r
+\r
     rend.optimize();\r
     p.optimize();\r
     s.optimize();\r
     r.optimize();\r
     x.optimize();\r
+    openparen.optimize();\r
+    closeparen.optimize();\r
 \r
     while ((line = nextLine()) != null)\r
     {\r
@@ -477,6 +490,19 @@ public class StockholmFile extends AlignFile
   private AlignmentAnnotation parseAnnotationRow(Vector annotation,\r
           String label, String annots)\r
   {\r
+    String convert1, convert2 = null;\r
+\r
+    // Convert all bracket types to parentheses\r
+    Regex openparen = new Regex("(<|\\[)", "(");\r
+    Regex closeparen = new Regex("(>|\\])", ")");\r
+\r
+    // Detect if file is RNA by looking for bracket types\r
+    Regex detectbrackets = new Regex("(<|>|\\[|\\]|\\(|\\))");\r
+\r
+    convert1 = openparen.replaceAll(annots);\r
+    convert2 = closeparen.replaceAll(convert1);\r
+    annots = convert2;\r
+\r
     String type = (label.indexOf("_cons") == label.length() - 5) ? label\r
             .substring(0, label.length() - 5) : label;\r
     boolean ss = false;\r
@@ -495,8 +521,17 @@ public class StockholmFile extends AlignFile
       // be written out\r
       if (ss)\r
       {\r
-        ann.secondaryStructure = jalview.schemes.ResidueProperties\r
-                .getDssp3state(pos).charAt(0);\r
+        if (detectbrackets.search(pos))\r
+        {\r
+          ann.secondaryStructure = jalview.schemes.ResidueProperties\r
+                  .getRNASecStrucState(pos).charAt(0);\r
+        }\r
+        else\r
+        {\r
+          ann.secondaryStructure = jalview.schemes.ResidueProperties\r
+                  .getDssp3state(pos).charAt(0);\r
+        }\r
+\r
         if (ann.secondaryStructure == pos.charAt(0) || pos.charAt(0) == 'C')\r
         {\r
           ann.displayCharacter = ""; // null; // " ";\r
@@ -580,4 +615,38 @@ public class StockholmFile extends AlignFile
             + id);\r
     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