update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / BLCFile.java
index 04867a2..03b2edf 100755 (executable)
-/*\r
-* Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
-*\r
-* This program is free software; you can redistribute it and/or\r
-* modify it under the terms of the GNU General Public License\r
-* as published by the Free Software Foundation; either version 2\r
-* of the License, or (at your option) any later version.\r
-*\r
-* This program is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with this program; if not, write to the Free Software\r
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
-*/\r
-package jalview.io;\r
-\r
-import jalview.datamodel.*;\r
-\r
-import java.io.*;\r
-\r
-import java.util.*;\r
-\r
-\r
-/**\r
- * DOCUMENT ME!\r
- *\r
- * @author $author$\r
- * @version $Revision$\r
- */\r
-public class BLCFile extends AlignFile\r
-{\r
-    Vector titles;\r
-\r
-    /**\r
-     * Creates a new BLCFile object.\r
-     */\r
-    public BLCFile()\r
-    {\r
-    }\r
-\r
-\r
-    /**\r
-     * Creates a new BLCFile object.\r
-     *\r
-     * @param inFile DOCUMENT ME!\r
-     * @param type DOCUMENT ME!\r
-     *\r
-     * @throws IOException DOCUMENT ME!\r
-     */\r
-    public BLCFile(String inFile, String type) throws IOException\r
-    {\r
-        super(inFile, type);\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     */\r
-    public void initData()\r
-    {\r
-        super.initData();\r
-        titles = new Vector();\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     */\r
-    public void parse() throws IOException\r
-    {\r
-        boolean idsFound = false;\r
-        StringBuffer[] seqstrings;\r
-\r
-        String line = null;\r
-\r
-            do\r
-            {\r
-                line = nextLine();\r
-\r
-                // seek end of ids\r
-                if (line.indexOf("*") > -1)\r
-                {\r
-                    idsFound = true;\r
-\r
-                    break;\r
-                }\r
-\r
-                int abracket = line.indexOf(">");\r
-\r
-                if (abracket > -1)\r
-                {\r
-                  line = line.substring(abracket+1);\r
-\r
-                  Sequence seq = parseId(line);\r
-                  seqs.addElement(seq);\r
-                }\r
-            }\r
-            while (!idsFound);\r
-\r
-            int starCol = line.indexOf("*");\r
-            seqstrings = new StringBuffer[seqs.size()];\r
-\r
-            for (int i = 0; i < seqs.size(); i++)\r
-            {\r
-                if (seqstrings[i] == null)\r
-                {\r
-                    seqstrings[i] = new StringBuffer();\r
-                }\r
-            }\r
-\r
-            while ((line = nextLine()).indexOf("*") == -1)\r
-            {\r
-                for (int i = 0; i < seqs.size(); i++)\r
-                {\r
-                    if (line.length() > (i + starCol))\r
-                    {\r
-                        seqstrings[i].append(line.charAt(i + starCol));\r
-                    }\r
-                }\r
-            }\r
-\r
-            for (int i = 0; i < seqs.size(); i++)\r
-            {\r
-              Sequence newSeq = (Sequence) seqs.elementAt(i);\r
-\r
-              if (!isValidProteinSequence(newSeq.getSequence()))\r
-              {\r
-                throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
-                               +" : "+ newSeq.getName()\r
-                               +" : "+invalidCharacter);\r
-              }\r
-\r
-              newSeq.setSequence(seqstrings[i].toString());\r
-            }\r
-\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @return DOCUMENT ME!\r
-     */\r
-    public String print()\r
-    {\r
-        return print(getSeqsAsArray());\r
-    }\r
-\r
-    /**\r
-     * DOCUMENT ME!\r
-     *\r
-     * @param s DOCUMENT ME!\r
-     *\r
-     * @return DOCUMENT ME!\r
-     */\r
-    public String print(SequenceI[] s)\r
-    {\r
-        StringBuffer out = new StringBuffer();\r
-        /**\r
-         * A general parser for ids. Will look for dbrefs in\r
-         * Uniprot format source|id\r
-         * And also Jalview /start-end\r
-         *\r
-         * @String id Id to be parsed\r
-     */\r
-        int i = 0;\r
-        int max = -1;\r
-\r
-        while ((i < s.length) && (s[i] != null))\r
-        {\r
-            out.append(">" + printId(s[i]));\r
-            if(s[i].getDescription()!=null)\r
-              out.append(" "+s[i].getDescription());\r
-\r
-            out.append("\n");\r
-\r
-            if (s[i].getSequence().length > max)\r
-            {\r
-                max = s[i].getSequence().length;\r
-            }\r
-\r
-            i++;\r
-        }\r
-\r
-        out.append("* iteration 1\n");\r
-\r
-        for (int j = 0; j < max; j++)\r
-        {\r
-            i = 0;\r
-\r
-            while ((i < s.length) && (s[i] != null))\r
-            {\r
-                if (s[i].getSequence().length > j)\r
-                {\r
-                    out.append(s[i].getSequenceAsString(j, j + 1));\r
-                }\r
-                else\r
-                {\r
-                    out.append("-");\r
-                }\r
-\r
-                i++;\r
-            }\r
-\r
-            out.append("\n");\r
-        }\r
-\r
-        out.append("*\n");\r
-\r
-        return out.toString();\r
-    }\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * 
+ * 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.
+ * 
+ * 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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package jalview.io;
+
+import java.io.*;
+import java.util.*;
+
+import jalview.datamodel.*;
+
+/**
+ * DOCUMENT ME!
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class BLCFile extends AlignFile
+{
+  Vector titles;
+
+  /**
+   * Creates a new BLCFile object.
+   */
+  public BLCFile()
+  {
+  }
+
+  /**
+   * Creates a new BLCFile object.
+   * 
+   * @param inFile
+   *          DOCUMENT ME!
+   * @param type
+   *          DOCUMENT ME!
+   * 
+   * @throws IOException
+   *           DOCUMENT ME!
+   */
+  public BLCFile(String inFile, String type) throws IOException
+  {
+    super(inFile, type);
+  }
+
+  public BLCFile(FileParse source) throws IOException
+  {
+    super(source);
+  }
+
+  /**
+   * DOCUMENT ME!
+   */
+  public void initData()
+  {
+    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
+   */
+  int iterationSkips = 0;
+
+  /**
+   * The iteration number for the alignment actually parsed from the blc file
+   */
+  int iterationCount = 0;
+
+  /**
+   * DOCUMENT ME!
+   */
+  public void parse() throws IOException
+  {
+    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
+    {
+      boolean idsFound = false;
+      boolean newids = false;
+      // search for ID header.
+      do
+      {
+        line = nextLine();
+        if (line == null)
+          break;
+        // seek end of ids
+        if (line.indexOf("*") > -1)
+        {
+          idsFound = true;
+
+          break;
+        }
+
+        int abracket = line.indexOf(">");
+
+        if (abracket > -1)
+        {
+
+          if (iterationCount > 0 && !newids)
+          {
+            // we have a new set of IDs to record.
+            newids = true;
+            seqs.removeAllElements();
+          }
+
+          line = line.substring(abracket + 1);
+
+          Sequence seq = parseId(line);
+          seqs.addElement(seq);
+        }
+        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()];
+
+      for (int i = 0; i < seqs.size(); i++)
+      {
+        if (seqstrings[i] == null)
+        {
+          seqstrings[i] = new StringBuffer();
+        }
+      }
+
+      try
+      {
+        line = nextLine();
+        while (line != null && line.indexOf("*") == -1)
+        {
+          for (int i = 0; i < seqs.size(); i++)
+          {
+            if (line.length() > (i + starCol))
+            {
+              seqstrings[i].append(line.charAt(i + starCol));
+            }
+          }
+          line = nextLine();
+        }
+      } catch (IOException e)
+      {
+        if (iterationCount == 0)
+        {
+          throw (e); // otherwise we've just run out of iterations.
+        }
+        else
+        {
+          iterationSkips = 0;
+        }
+      }
+      iterationCount++;
+    } 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) // could see if buffer is
+        // just whitespace or not.
+        setAlignmentProperty("Comments", headerLines.toString());
+      setAlignmentProperty("iteration", "" + iterationCount);
+    }
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @return DOCUMENT ME!
+   */
+  public String print()
+  {
+    return print(getSeqsAsArray());
+  }
+
+  /**
+   * 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
+     * 
+     * @String id Id to be parsed
+     */
+    int i = 0;
+    int max = -1;
+
+    while ((i < s.length) && (s[i] != null))
+    {
+      out.append(">" + printId(s[i]));
+      if (s[i].getDescription() != null)
+      {
+        out.append(" " + s[i].getDescription());
+      }
+
+      out.append(newline);
+
+      if (s[i].getSequence().length > max)
+      {
+        max = s[i].getSequence().length;
+      }
+
+      i++;
+    }
+
+    out.append("* iteration 1");
+    out.append(newline);
+
+    for (int j = 0; j < max; j++)
+    {
+      i = 0;
+
+      while ((i < s.length) && (s[i] != null))
+      {
+        if (s[i].getSequence().length > j)
+        {
+          out.append(s[i].getSequenceAsString(j, j + 1));
+        }
+        else
+        {
+          out.append("-");
+        }
+
+        i++;
+      }
+
+      out.append(newline);
+    }
+
+    out.append("*");
+    out.append(newline);
+
+    return out.toString();
+  }
+}