stockholm file output code developed by Natasha Sherstneva
authorNatasha <cherstn@main-home.(none)>
Fri, 23 Nov 2012 07:49:49 +0000 (07:49 +0000)
committerNatasha <cherstn@main-home.(none)>
Fri, 23 Nov 2012 07:49:49 +0000 (07:49 +0000)
src/jalview/io/AppletFormatAdapter.java
src/jalview/io/StockholmFile.java

index 11f5adb..a848555 100755 (executable)
@@ -45,21 +45,21 @@ public class AppletFormatAdapter
    * method
    */
   public static final String[] WRITEABLE_FORMATS = new String[]
-  { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA" };
+  { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "AMSA" };
 
   /**
    * List of extensions corresponding to file format types in WRITABLE_FNAMES
    * that are writable by the application.
    */
   public static final String[] WRITABLE_EXTENSIONS = new String[]
-  { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar" };
+  { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar", "sto,stk" };
 
   /**
    * List of writable formats by the application. Order must correspond with the
    * WRITABLE_EXTENSIONS list of formats.
    */
   public static final String[] WRITABLE_FNAMES = new String[]
-  { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
+  { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview", "STH"};
 
   /**
    * List of readable format file extensions by application in order
@@ -445,7 +445,7 @@ public class AppletFormatAdapter
       }
       else if (format.equalsIgnoreCase("STH"))
       {
-        afile = new StockholmFile();
+        afile = new StockholmFile(alignment);
       }
       else if (format.equalsIgnoreCase("AMSA"))
       {
index 669181a..62fec79 100644 (file)
@@ -25,6 +25,7 @@ import java.util.*;
 
 import com.stevesoft.pat.*;
 import jalview.datamodel.*;
+import jalview.util.Format;
 
 // import org.apache.log4j.*;
 
@@ -43,11 +44,21 @@ import jalview.datamodel.*;
 public class StockholmFile extends AlignFile
 {
   // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
-
+  StringBuffer out;
+  AlignmentI al;
+  
   public StockholmFile()
   {
   }
 
+  /**
+  * Creates a new StockholmFile object for output.
+  */
+  public StockholmFile(AlignmentI al)
+  {
+    this.al = al;
+  }
+  
   public StockholmFile(String inFile, String type) throws IOException
   {
     super(inFile, type);
@@ -187,18 +198,7 @@ public class StockholmFile extends AlignFile
               jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
               // seqO.addDBRef(dbref);
             }
-          }
-          if (accAnnotations != null && accAnnotations.containsKey("SS"))
-          {
-            Vector v = (Vector) accAnnotations.get("SS");
-
-            for (int i = 0; i < v.size(); i++)
-            {
-              AlignmentAnnotation an = (AlignmentAnnotation) v.elementAt(i);
-              seqO.addAlignmentAnnotation(an);
-              // annotations.add(an);
-            }
-          }
+          }  
 
           Hashtable features = null;
           // We need to adjust the positions of all features to account for gaps
@@ -224,6 +224,22 @@ public class StockholmFile extends AlignFile
               // TODO: map coding region to core jalview feature types
               String type = i.nextElement().toString();
               Hashtable content = (Hashtable) features.remove(type);
+             
+              // 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);           
+                  }        
+                }
+              }
+              
               Enumeration j = content.keys();
               while (j.hasMoreElements())
               {
@@ -477,25 +493,20 @@ public class StockholmFile extends AlignFile
             }
             ns += seq;
             content.put(description, ns);
-
-            if (type.equals("SS"))
+            Hashtable strucAnn;
+            if (seqAnn.containsKey(acc))
             {
-              Hashtable strucAnn;
-              if (seqAnn.containsKey(acc))
-              {
-                strucAnn = (Hashtable) seqAnn.get(acc);
-              }
-              else
-              {
-                strucAnn = new Hashtable();
-              }
-
-              Vector newStruc = new Vector();
-              parseAnnotationRow(newStruc, type, ns);
-
-              strucAnn.put(type, newStruc);
-              seqAnn.put(acc, strucAnn);
+              strucAnn = (Hashtable) seqAnn.get(acc);
+            }
+            else
+            {
+              strucAnn = new Hashtable();
             }
+
+            Vector newStruc = new Vector();
+            parseAnnotationRow(newStruc, type, ns);
+            strucAnn.put(type, newStruc);
+            seqAnn.put(acc, strucAnn);
           }
           else
           {
@@ -606,14 +617,141 @@ public class StockholmFile extends AlignFile
     return annot;
   }
 
-  public static String print(SequenceI[] s)
+  public String print(SequenceI[] s)
   {
-    return "not yet implemented";
+         // find max length of id
+           int max = 0;
+           int maxid = 0;
+           int in = 0;
+           while ((in < s.length) && (s[in] != null))
+           {
+             String tmp = printId(s[in]);
+             if (s[in].getSequence().length > max)
+             {
+               max = s[in].getSequence().length;
+             }
+
+             if (tmp.length() > maxid)
+             {
+               maxid = tmp.length();
+             }
+
+             in++;
+           }
+           maxid += 9;
+           int i = 0;
+           
+           while (i < s.length && s[i] != null) 
+           {
+             if (s[i].getDatasetSequence() != null) 
+             {
+               SequenceI ds = s[i].getDatasetSequence();
+                       AlignmentAnnotation[] alAnot;
+                   Annotation[] ann;
+                   Annotation annot;  
+                   alAnot = s[i].getAnnotation();
+                   String feature = "";
+               if (alAnot != null) 
+               {
+                         for (int j = 0; j < alAnot.length; j++) 
+                         {     
+                           if (ds.getSequenceFeatures() != null) 
+                   {
+                                 feature = ds.getSequenceFeatures()[0].type;
+                           }   
+                           String key = type2id(feature);
+                           
+                         // output annotations
+                       if (key == null)
+                           continue;
+                       
+                         //  out.append("#=GR ");
+                           out.append(new Format("%-" + maxid + "s").form("#=GR " + printId(s[i]) + " " + key + " "));
+                           ann = alAnot[j].annotations;
+                           String seq = "";
+                               for (int k = 0; k < ann.length; k++) 
+                               {         
+                             annot = ann[k]; 
+                             String ch = (annot == null) ? Character.toString(s[i].getCharAt(k)) : annot.displayCharacter;
+                             if (ch.length() == 0)
+                         {
+                           if (key.equals("SS")) {
+                             char ll = annot.secondaryStructure;
+                                 seq = (Character.toString(ll).equals(" ")) ? seq + "C" : seq + ll;    
+                               } else {
+                                 seq += ".";  
+                                 }
+                               } else if (ch.length() == 1) {
+                                     seq += ch;
+                                   } else if (ch.length() > 1) {
+                                     seq += ch.charAt(1) ;
+                               }   
+                             }
+                             out.append(seq);
+                             out.append(newline);
+                         }
+               }
+                 }
+           
+             out.append(new Format("%-" + maxid + "s").form(printId(s[i])+" "));
+                 out.append(s[i].getSequenceAsString());
+                 out.append(newline);  
+             i++;
+           } 
+           
+           // alignment annotation
+           AlignmentAnnotation aa;
+           if (al.getAlignmentAnnotation() != null) 
+           {
+             for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++)
+             {
+               aa = al.getAlignmentAnnotation()[ia];
+               if (aa.autoCalculated || !aa.visible)
+               {
+                 continue;
+               }
+               String seq = "";
+               String label = type2id(aa.label);
+
+               if (label == null) 
+                 label = aa.label;     
+               
+               label += "_cons";
+               
+               out.append(new Format("%-" + maxid + "s").form("#=GC " + label+" "));
+               for (int j = 0; j < aa.annotations.length; j++) 
+               {
+                 String ch = (aa.annotations[j] == null) ? "-" : aa.annotations[j].displayCharacter ;
+                 if (ch.length() == 0) 
+                 {
+                   char ll = aa.annotations[j].secondaryStructure;
+                           if (Character.toString(ll).equals(" "))
+                             seq += "C";
+                               else 
+                             seq += ll;  
+                 } else if (ch.length() == 1) {
+                   seq += ch;
+                 } else if (ch.length() > 1) {
+                   seq += ch.charAt(1) ;
+                 }   
+               }
+                   out.append(seq);
+               out.append(newline);
+             }
+           }
+               return out.toString();
   }
 
   public String print()
   {
-    return print(getSeqsAsArray());
+       out = new StringBuffer();
+       out.append("# STOCKHOLM 1.0");
+       out.append(newline); 
+    print(getSeqsAsArray());
+           
+    out.append("//");
+    out.append(newline);  
+    return out.toString();
   }
 
   private static Hashtable typeIds = null;
@@ -651,6 +789,28 @@ public class StockholmFile extends AlignFile
             + id);
     return id;
   }
+  
+  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;
+  }
   /**
    * //ssline is complete secondary structure line private AlignmentAnnotation
    * addHelices(Vector annotation, String label, String ssline) {