removed Iterator usage.
[jalview.git] / src / jalview / io / BLCFile.java
index 687eef9..8958f7b 100755 (executable)
 /*\r
-* Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2005 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
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2007 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
+import jalview.datamodel.*;\r
 \r
-public class BLCFile extends AlignFile {\r
-    Vector titles;\r
-\r
-    public BLCFile() {\r
-    }\r
-\r
-    public BLCFile(String inStr) {\r
-        super(inStr);\r
+/**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @author $author$\r
+ * @version $Revision$\r
+ */\r
+public class BLCFile\r
+    extends AlignFile\r
+{\r
+  Vector titles;\r
+\r
+  /**\r
+   * Creates a new BLCFile object.\r
+   */\r
+  public BLCFile()\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)\r
+      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()\r
+      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
-    public BLCFile(String inFile, String type) throws IOException {\r
-        super(inFile, type);\r
-    }\r
+    int starCol = line.indexOf("*");\r
+    seqstrings = new StringBuffer[seqs.size()];\r
 \r
-    public void initData() {\r
-        super.initData();\r
-        titles = new Vector();\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
-    public void parse() {\r
-        boolean idsFound = false;\r
-        Vector ids = new Vector();\r
-        StringBuffer[] seqstrings;\r
-        Vector starts = new Vector();\r
-        Vector ends = new Vector();\r
-\r
-        String line = null;\r
-\r
-        try {\r
-            do {\r
-                line = nextLine();\r
-\r
-                // seek end of ids\r
-                if (line.indexOf("*") > -1) {\r
-                    idsFound = true;\r
-\r
-                    break;\r
-                }\r
-\r
-                int abracket = line.indexOf(">");\r
-\r
-                if (abracket > -1) {\r
-                    if (line.indexOf(" ") > -1) //\r
-                     {\r
-                        ///Colur it be this format?\r
-                        //>54402046         0             1   137   137:\r
-                        // or this??\r
-                        //     1   >L1H14       30539  343\r
-                        try {\r
-                            ids.addElement(line.substring(abracket + 1,\r
-                                    line.indexOf(" ", abracket + 1)));\r
-\r
-                            // remove p Value\r
-                            line = line.substring(abracket + 1);\r
-                            line = line.substring(line.indexOf(" ") + 1);\r
-                            line = line.trim();\r
-                            line = line.substring(line.indexOf(" ") + 1);\r
-                            line = line.trim();\r
-\r
-                            int value = Integer.parseInt(line.substring(0,\r
-                                        line.indexOf(" ")));\r
-                            starts.addElement(value + "");\r
-                            line = line.substring(line.indexOf(" ") + 1);\r
-                            line = line.trim();\r
-                            value = Integer.parseInt(line.substring(0,\r
-                                        line.indexOf(" ")));\r
-                            ends.addElement(value + "");\r
-                        } catch (Exception ex) {\r
-                            System.err.println("Error during blockfile read.");\r
-                            ex.printStackTrace();\r
-                            starts.addElement("0");\r
-                            ends.addElement("0");\r
-                        }\r
-                    } else {\r
-                        ids.addElement(line.substring(abracket + 1,\r
-                                line.indexOf("/")));\r
-                        line = line.substring(line.indexOf("/") + 1);\r
-                        starts.addElement(line.substring(0, line.indexOf("-")));\r
-                        ends.addElement(line.substring(line.indexOf("-") + 1));\r
-                    }\r
-                }\r
-            } while (!idsFound);\r
-\r
-            int starCol = line.indexOf("*");\r
-            seqstrings = new StringBuffer[ids.size()];\r
-\r
-            for (int i = 0; i < ids.size(); i++) {\r
-                if (seqstrings[i] == null) {\r
-                    seqstrings[i] = new StringBuffer();\r
-                }\r
-            }\r
-\r
-            while ((line = nextLine()).indexOf("*") == -1) {\r
-                for (int i = 0; i < ids.size(); i++) {\r
-                    if (line.length() > (i + starCol)) {\r
-                        seqstrings[i].append(line.charAt(i + starCol));\r
-                    }\r
-                }\r
-            }\r
-\r
-            for (int i = 0; i < ids.size(); i++) {\r
-                Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
-                        seqstrings[i].toString(),\r
-                        Integer.parseInt(starts.elementAt(i).toString()),\r
-                        Integer.parseInt(ends.elementAt(i).toString()));\r
-                seqs.addElement(newSeq);\r
-            }\r
-        } catch (Exception ex) {\r
-            ex.printStackTrace();\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
-    public String print() {\r
-        return print(getSeqsAsArray());\r
-    }\r
+    for (int i = 0; i < seqs.size(); i++)\r
+    {\r
+      Sequence newSeq = (Sequence) seqs.elementAt(i);\r
 \r
-    public static String print(SequenceI[] s) {\r
-        StringBuffer out = new StringBuffer();\r
+      newSeq.setSequence(seqstrings[i].toString());\r
+    }\r
 \r
-        int i = 0;\r
-        int max = -1;\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
+      {\r
+        out.append(" " + s[i].getDescription());\r
+      }\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
-        while ((i < s.length) && (s[i] != null)) {\r
-            out.append(">" + s[i].getName() + "/" + s[i].getStart() + "-" +\r
-                s[i].getEnd() + "\n");\r
+    out.append("* iteration 1\n");\r
 \r
-            if (s[i].getSequence().length() > max) {\r
-                max = s[i].getSequence().length();\r
-            }\r
+    for (int j = 0; j < max; j++)\r
+    {\r
+      i = 0;\r
 \r
-            i++;\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
-\r
-        out.append("* iteration 1\n");\r
-\r
-        for (int j = 0; j < max; j++) {\r
-            i = 0;\r
-\r
-            while ((i < s.length) && (s[i] != null)) {\r
-                if (s[i].getSequence().length() > j) {\r
-                    out.append(s[i].getSequence().substring(j, j + 1));\r
-                } else {\r
-                    out.append("-");\r
-                }\r
-\r
-                i++;\r
-            }\r
-\r
-            out.append("\n");\r
+        else\r
+        {\r
+          out.append("-");\r
         }\r
 \r
-        out.append("*\n");\r
+        i++;\r
+      }\r
 \r
-        return out.toString();\r
+      out.append("\n");\r
     }\r
+\r
+    out.append("*\n");\r
+\r
+    return out.toString();\r
+  }\r
 }\r