Output accession number for file in stockholm format
authorNatasha <cherstn@main-home.(none)>
Wed, 12 Dec 2012 21:02:44 +0000 (21:02 +0000)
committerNatasha <cherstn@main-home.(none)>
Wed, 12 Dec 2012 21:02:44 +0000 (21:02 +0000)
src/jalview/io/StockholmFile.java

index 62fec79..c6a3ce4 100644 (file)
@@ -44,7 +44,7 @@ import jalview.util.Format;
 public class StockholmFile extends AlignFile
 {
   // static Logger logger = Logger.getLogger("jalview.io.StockholmFile");
-  StringBuffer out;
+  StringBuffer out; // output buffer
   AlignmentI al;
   
   public StockholmFile()
@@ -143,8 +143,23 @@ public class StockholmFile extends AlignFile
       if (rend.search(line))
       {
         // End of the alignment, pass stuff back
-
         this.noSeqs = seqs.size();
+        
+        String propety = 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))
+          {
+            propety = "PFAM";
+          }
+          else if (rf.search(dbType))
+          {
+               propety = "RFAM";
+          }
+        }
         // logger.debug("Number of sequences: " + this.noSeqs);
         Enumeration accs = seqs.keys();
         while (accs.hasMoreElements())
@@ -187,6 +202,9 @@ public class StockholmFile extends AlignFile
             String desc = (String) accAnnotations.get("DE");
             seqO.setDescription((desc == null) ? "" : desc);
           }
+          
+         
+            
           // Add DB References (if any)
           if (accAnnotations != null && accAnnotations.containsKey("DR"))
           {
@@ -196,10 +214,21 @@ public class StockholmFile extends AlignFile
               String src = dbr.substring(0, dbr.indexOf(";"));
               String acn = dbr.substring(dbr.indexOf(";") + 1);
               jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
-              // seqO.addDBRef(dbref);
-            }
-          }  
-
+            }            
+          }
+          
+          if (accAnnotations != null && accAnnotations.containsKey("AC") && propety != null)
+          {
+            String dbr = (String) accAnnotations.get("AC");
+            if (dbr != null)
+            {
+              String src = propety;
+              String acn = dbr.toString();
+              jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn);
+            }            
+          }
+          
+      
           Hashtable features = null;
           // We need to adjust the positions of all features to account for gaps
           try
@@ -548,9 +577,12 @@ public class StockholmFile extends AlignFile
     convert1 = openparen.replaceAll(annots);
     convert2 = closeparen.replaceAll(convert1);
     annots = convert2;
-
-    String type = (label.indexOf("_cons") == label.length() - 5) ? label
-            .substring(0, label.length() - 5) : label;
+    
+    String type = label;
+    if (label.contains("_cons")) {
+       type = (label.indexOf("_cons") == label.length() - 5) ? label
+                .substring(0, label.length() - 5) : label;     
+    }     
     boolean ss = false;
     type = id2type(type);
     if (type.equals("secondary structure"))
@@ -616,13 +648,14 @@ public class StockholmFile extends AlignFile
     }
     return annot;
   }
-
+  
   public String print(SequenceI[] s)
   {
          // find max length of id
            int max = 0;
            int maxid = 0;
            int in = 0;
+           Hashtable dataRef = null;
            while ((in < s.length) && (s[in] != null))
            {
              String tmp = printId(s[in]);
@@ -635,17 +668,64 @@ public class StockholmFile extends AlignFile
              {
                maxid = tmp.length();
              }
-
+             if (s[in].getDBRef() != null)
+             {  
+               for (int idb = 0; idb < s[in].getDBRef().length; idb++)
+               {               
+                 if (dataRef == null) 
+                       dataRef = new Hashtable();
+  
+                 String datAs1 = s[in].getDBRef()[idb].getSource().toString() + " ; " +s[in].getDBRef()[idb].getAccessionId().toString();
+                 dataRef.put(tmp, datAs1);
+            }
+          }
              in++;
            }
            maxid += 9;
            int i = 0;
            
+           // output database type
+           if (al.getProperties() != null)
+           {
+             if (!al.getProperties().isEmpty())
+             {
+               Enumeration key = al.getProperties().keys();
+               Enumeration val = al.getProperties().elements();
+               while (key.hasMoreElements())
+               {  
+                  out.append("#=GF " + key.nextElement() + " " + val.nextElement());
+                  out.append(newline);
+               }
+             }  
+           }
+           
+           // output  database accessions 
+           if (dataRef != null)
+           {
+             Enumeration en = dataRef.keys();    
+             while (en.hasMoreElements())
+             {
+                Object idd = en.nextElement();
+                String type = (String) dataRef.remove(idd);
+                out.append(new Format("%-" + (maxid - 2) + "s").form("#=GS " +idd.toString() + " "));
+                if (type.contains("PFAM") || type.contains("RFAM") )
+                {
+               
+                       out.append(" AC " + type.substring(type.indexOf(";") + 1));
+                } else
+                {
+                   out.append(" DR " + type + " ");
+                }
+                out.append(newline);
+             } 
+           }
+           
+           // output annotations
            while (i < s.length && s[i] != null) 
            {
              if (s[i].getDatasetSequence() != null) 
              {
-               SequenceI ds = s[i].getDatasetSequence();
+               SequenceI ds = s[i].getDatasetSequence();       
                        AlignmentAnnotation[] alAnot;
                    Annotation[] ann;
                    Annotation annot;  
@@ -661,7 +741,7 @@ public class StockholmFile extends AlignFile
                            }   
                            String key = type2id(feature);
                            
-                         // output annotations
+                      
                        if (key == null)
                            continue;
                        
@@ -711,13 +791,16 @@ public class StockholmFile extends AlignFile
                  continue;
                }
                String seq = "";
-               String label = type2id(aa.label);
+               String label;
+               
+               if (aa.label.equals("seq"))
+                 label = "seq_cons";
+               else
+                 label = type2id(aa.label.toLowerCase())+"_cons";
 
                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++) 
                {
@@ -803,7 +886,7 @@ public class StockholmFile extends AlignFile
           break;
         }
       }  
-      if (key != null)
+      if (key != null) 
       {
          return (String) key;
       }