JAL-1236 TODO: fix up parser to cope with the currently defined custom header......
[jalview.git] / src / jalview / io / ClustalFile.java
index 6c35ca1..c558186 100755 (executable)
@@ -26,7 +26,8 @@ import jalview.datamodel.SequenceI;
 import jalview.util.Format;
 
 import java.io.IOException;
-import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -35,17 +36,28 @@ public class ClustalFile extends AlignFile
 
   public ClustalFile()
   {
+    _initHeader();
   }
 
   public ClustalFile(String inFile, DataSourceType sourceType)
           throws IOException
   {
     super(inFile, sourceType);
+    _initHeader();
   }
 
   public ClustalFile(FileParse source) throws IOException
   {
     super(source);
+    _initHeader();
+  }
+
+  private void _initHeader()
+  {
+    try {
+       clustalHeader = jalview.bin.Cache.getDefault("CLUSTAL_HEADER","CLUSTAL");
+    } catch (Error e) {};
+
   }
 
   @Override
@@ -59,12 +71,11 @@ public class ClustalFile extends AlignFile
   {
     int i = 0;
     boolean flag = false;
-    boolean rna = false;
     boolean top = false;
-    StringBuffer pssecstr = new StringBuffer(),
-            consstr = new StringBuffer();
-    Vector headers = new Vector();
-    Hashtable seqhash = new Hashtable();
+    StringBuffer pssecstr = new StringBuffer();
+    StringBuffer consstr = new StringBuffer();
+    Vector<String> headers = new Vector<>();
+    Map<String, StringBuffer> seqhash = new HashMap<>();
     StringBuffer tempseq;
     String line, id;
     StringTokenizer str;
@@ -77,14 +88,16 @@ public class ClustalFile extends AlignFile
         {
           top = true;
         }
-        if (line.indexOf(" ") != 0)
+        boolean isConservation = line.startsWith(SPACE)
+                || line.startsWith(TAB);
+        if (!isConservation)
         {
-          str = new StringTokenizer(line, " ");
+          str = new StringTokenizer(line);
 
           if (str.hasMoreTokens())
           {
             id = str.nextToken();
-
+           // TODO: JAL-1236 other tokens may be indicative of a header for Clustal format
             if (id.equalsIgnoreCase("CLUSTAL"))
             {
               flag = true;
@@ -95,7 +108,7 @@ public class ClustalFile extends AlignFile
               {
                 if (seqhash.containsKey(id))
                 {
-                  tempseq = (StringBuffer) seqhash.get(id);
+                  tempseq = seqhash.get(id);
                 }
                 else
                 {
@@ -173,7 +186,7 @@ public class ClustalFile extends AlignFile
       AlignmentAnnotation lastssa = null;
       if (pssecstr.length() == maxLength)
       {
-        Vector ss = new Vector();
+        Vector<AlignmentAnnotation> ss = new Vector<>();
         AlignmentAnnotation ssa = lastssa = StockholmFile
                 .parseAnnotationRow(ss, "secondary structure",
                         pssecstr.toString());
@@ -182,7 +195,7 @@ public class ClustalFile extends AlignFile
       }
       if (consstr.length() == maxLength)
       {
-        Vector ss = new Vector();
+        Vector<AlignmentAnnotation> ss = new Vector<>();
         AlignmentAnnotation ssa = StockholmFile.parseAnnotationRow(ss,
                 "secondary structure", consstr.toString());
         ssa.label = "Consensus Secondary Structure";
@@ -194,11 +207,15 @@ public class ClustalFile extends AlignFile
       }
     }
   }
-
+  /**
+   * clustal header - customise if needed
+   */
+  public String clustalHeader = "CLUSTAL";
+  
   @Override
   public String print(SequenceI[] s, boolean jvsuffix)
   {
-    StringBuffer out = new StringBuffer("CLUSTAL" + newline + newline);
+    StringBuffer out = new StringBuffer(clustalHeader + newline + newline);
 
     int max = 0;
     int maxid = 0;
@@ -209,10 +226,7 @@ public class ClustalFile extends AlignFile
     {
       String tmp = printId(s[i], jvsuffix);
 
-      if (s[i].getSequence().length > max)
-      {
-        max = s[i].getSequence().length;
-      }
+      max = Math.max(max, s[i].getLength());
 
       if (tmp.length() > maxid)
       {
@@ -241,19 +255,19 @@ public class ClustalFile extends AlignFile
         out.append(new Format("%-" + maxid + "s")
                 .form(printId(s[j], jvsuffix) + " "));
 
-        int start = i * len;
-        int end = start + len;
+        int chunkStart = i * len;
+        int chunkEnd = chunkStart + len;
 
-        if ((end < s[j].getSequence().length)
-                && (start < s[j].getSequence().length))
+        int length = s[j].getLength();
+        if ((chunkEnd < length) && (chunkStart < length))
         {
-          out.append(s[j].getSequenceAsString(start, end));
+          out.append(s[j].getSequenceAsString(chunkStart, chunkEnd));
         }
         else
         {
-          if (start < s[j].getSequence().length)
+          if (chunkStart < length)
           {
-            out.append(s[j].getSequenceAsString().substring(start));
+            out.append(s[j].getSequenceAsString().substring(chunkStart));
           }
         }