JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / io / BLCFile.java
index f485a49..a499f75 100755 (executable)
@@ -1,36 +1,38 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
  * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.io;
 
-import java.io.*;
-import java.util.*;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
 
-import jalview.datamodel.*;
+import java.io.IOException;
+import java.util.Vector;
 
 /**
  * DOCUMENT ME!
- *
+ * 
  * @author $author$
  * @version $Revision$
  */
-public class BLCFile
-extends AlignFile
+public class BLCFile extends AlignFile
 {
   Vector titles;
 
@@ -43,18 +45,25 @@ extends AlignFile
 
   /**
    * Creates a new BLCFile object.
-   *
-   * @param inFile DOCUMENT ME!
-   * @param type DOCUMENT ME!
-   *
-   * @throws IOException DOCUMENT ME!
+   * 
+   * @param inFile
+   *          DOCUMENT ME!
+   * @param type
+   *          DOCUMENT ME!
+   * 
+   * @throws IOException
+   *           DOCUMENT ME!
    */
-  public BLCFile(String inFile, String type)
-  throws IOException
+  public BLCFile(String inFile, String type) throws IOException
   {
     super(inFile, type);
   }
 
+  public BLCFile(FileParse source) throws IOException
+  {
+    super(source);
+  }
+
   /**
    * DOCUMENT ME!
    */
@@ -63,44 +72,53 @@ extends AlignFile
     super.initData();
     titles = new Vector();
   }
+
   /**
-   * Control the number of block iterations to skip before returning.
-   * set to 0 to read first block file entry only
-   * set to -1 to read last block file entry only
-   * set to greater than zero to skip at most that many entries before parsing
+   * Control the number of block iterations to skip before returning. set to 0
+   * to read first block file entry only set to -1 to read last block file entry
+   * only set to greater than zero to skip at most that many entries before
+   * parsing
    */
-  int iterationSkips=0;
+  int iterationSkips = 0;
+
   /**
-   * The iteration number for the alignment actually parsed from the blc file 
+   * The iteration number for the alignment actually parsed from the blc file
    */
-  int iterationCount=0;
+  int iterationCount = 0;
 
   /**
    * DOCUMENT ME!
    */
-  public void parse()
-  throws IOException
+  public void parse() throws IOException
   {
-    StringBuffer[] seqstrings=null;
-    if (suffix!=null) {
-      try {
-        iterationSkips = Integer.parseInt(suffix); 
-      } catch (NumberFormatException e) {
+    StringBuffer headerLines = new StringBuffer();
+    int numHeaderLines = 0; // number of lines appended.
+    StringBuffer[] seqstrings = null;
+    if (suffix != null)
+    {
+      try
+      {
+        iterationSkips = Integer.parseInt(suffix);
+      } catch (NumberFormatException e)
+      {
         iterationSkips = 0; // first
       }
     }
 
     String line = null;
 
-    do {
+    do
+    {
       boolean idsFound = false;
       boolean newids = false;
-        // search for ID header.
+      // search for ID header.
       do
       {
         line = nextLine();
-        if (line==null)
+        if (line == null)
+        {
           break;
+        }
         // seek end of ids
         if (line.indexOf("*") > -1)
         {
@@ -108,16 +126,17 @@ extends AlignFile
 
           break;
         }
-        
+
         int abracket = line.indexOf(">");
 
         if (abracket > -1)
         {
 
-          if (iterationCount>0 && !newids) {
+          if (iterationCount > 0 && !newids)
+          {
             // we have a new set of IDs to record.
             newids = true;
-            seqs.clear(); 
+            seqs.removeAllElements();
           }
 
           line = line.substring(abracket + 1);
@@ -125,10 +144,18 @@ extends AlignFile
           Sequence seq = parseId(line);
           seqs.addElement(seq);
         }
-      }
-      while (!idsFound);
-      if (line==null)
+        else
+        {
+          // header lines - keep them for the alignment comments.
+          headerLines.append(line);
+          headerLines.append(newline);
+          numHeaderLines++;
+        }
+      } while (!idsFound);
+      if (line == null)
+      {
         break; // end of file.
+      }
       int starCol = line.indexOf("*");
       seqstrings = new StringBuffer[seqs.size()];
 
@@ -140,9 +167,10 @@ extends AlignFile
         }
       }
 
-      try {
+      try
+      {
         line = nextLine();
-        while (line!=null && line.indexOf("*") == -1)
+        while (line != null && line.indexOf("*") == -1)
         {
           for (int i = 0; i < seqs.size(); i++)
           {
@@ -153,28 +181,40 @@ extends AlignFile
           }
           line = nextLine();
         }
-      } catch (IOException e) {
-        if (iterationCount==0) {
-          throw(e); // otherwise we've just run out of iterations.
-        } else {
-          iterationSkips=0;
+      } catch (IOException e)
+      {
+        if (iterationCount == 0)
+        {
+          throw (e); // otherwise we've just run out of iterations.
+        }
+        else
+        {
+          iterationSkips = 0;
         }
       }
       iterationCount++;
-    } while (--iterationSkips!=-1);
-    
+    } while (--iterationSkips != -1);
+
     for (int i = 0; i < seqs.size(); i++)
     {
       Sequence newSeq = (Sequence) seqs.elementAt(i);
 
       newSeq.setSequence(seqstrings[i].toString());
     }
-
+    if (seqs.size() > 0)
+    {
+      if (headerLines.length() > 1 + numHeaderLines)
+      {
+        // just whitespace or not.
+        setAlignmentProperty("Comments", headerLines.toString());
+      }
+      setAlignmentProperty("iteration", "" + iterationCount);
+    }
   }
 
   /**
    * DOCUMENT ME!
-   *
+   * 
    * @return DOCUMENT ME!
    */
   public String print()
@@ -184,25 +224,25 @@ extends AlignFile
 
   /**
    * DOCUMENT ME!
-   *
-   * @param s DOCUMENT ME!
-   *
+   * 
+   * @param s
+   *          DOCUMENT ME!
+   * 
    * @return DOCUMENT ME!
    */
   public String print(SequenceI[] s)
   {
     StringBuffer out = new StringBuffer();
     /**
-     * A general parser for ids. Will look for dbrefs in
-     * Uniprot format source|id
-     * And also Jalview /start-end
-     *
+     * A general parser for ids. Will look for dbrefs in Uniprot format
+     * source|id And also Jalview /start-end
+     * 
      * @String id Id to be parsed
      */
     int i = 0;
     int max = -1;
 
-    while ( (i < s.length) && (s[i] != null))
+    while ((i < s.length) && (s[i] != null))
     {
       out.append(">" + printId(s[i]));
       if (s[i].getDescription() != null)
@@ -210,7 +250,7 @@ extends AlignFile
         out.append(" " + s[i].getDescription());
       }
 
-      out.append("\n");
+      out.append(newline);
 
       if (s[i].getSequence().length > max)
       {
@@ -220,13 +260,14 @@ extends AlignFile
       i++;
     }
 
-    out.append("* iteration 1\n");
+    out.append("* iteration 1");
+    out.append(newline);
 
     for (int j = 0; j < max; j++)
     {
       i = 0;
 
-      while ( (i < s.length) && (s[i] != null))
+      while ((i < s.length) && (s[i] != null))
       {
         if (s[i].getSequence().length > j)
         {
@@ -240,10 +281,11 @@ extends AlignFile
         i++;
       }
 
-      out.append("\n");
+      out.append(newline);
     }
 
-    out.append("*\n");
+    out.append("*");
+    out.append(newline);
 
     return out.toString();
   }