updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / BLCFile.java
index e0750d5..2176ec8 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
-package jalview.io;\r
-\r
-import jalview.datamodel.*;\r
-\r
-import java.io.*;\r
-\r
-import java.util.*;\r
-\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
-\r
-    public BLCFile(String inFile, String type) throws IOException {\r
-        super(inFile, type);\r
-    }\r
-\r
-    public void initData() {\r
-        super.initData();\r
-        titles = new Vector();\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
-                        if(line.indexOf("/")>-1)\r
-                        {\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
-                        else{\r
-                          ids.addElement(line.substring(abracket+1));\r
-                          starts.addElement("0");\r
-                          ends.addElement("0");\r
-                        }\r
-\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
-        }\r
-    }\r
-\r
-    public String print() {\r
-        return print(getSeqsAsArray());\r
-    }\r
-\r
-    public static String print(SequenceI[] s) {\r
-        StringBuffer out = new StringBuffer();\r
-\r
-        int i = 0;\r
-        int max = -1;\r
-\r
-        while ((i < s.length) && (s[i] != null)) {\r
-            out.append(">" + s[i].getName() + "/" + s[i].getStart() + "-" +\r
-                s[i].getEnd() + "\n");\r
-\r
-            if (s[i].getSequence().length() > max) {\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
-            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
-        }\r
-\r
-        out.append("*\n");\r
-\r
-        return out.toString();\r
-    }\r
-}\r
+/*
+* Jalview - A Sequence Alignment Editor and Viewer
+* Copyright (C) 2006 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
+* 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.
+*
+* 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
+*/
+package jalview.io;
+
+import jalview.datamodel.*;
+
+import java.io.*;
+
+import java.util.*;
+
+
+/**
+ * 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);
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void initData()
+    {
+        super.initData();
+        titles = new Vector();
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void parse() throws IOException
+    {
+        boolean idsFound = false;
+        Vector ids = new Vector();
+        StringBuffer[] seqstrings;
+        Vector starts = new Vector();
+        Vector ends = new Vector();
+
+        String line = null;
+
+            do
+            {
+                line = nextLine();
+
+                // seek end of ids
+                if (line.indexOf("*") > -1)
+                {
+                    idsFound = true;
+
+                    break;
+                }
+
+                int abracket = line.indexOf(">");
+
+                if (abracket > -1)
+                {
+                  if (line.indexOf(" ") > -1) //
+                  {
+                    line = line.substring(abracket + 1,
+                                          line.indexOf(" ", abracket + 1));
+                  }
+                  else
+                    line = line.substring(abracket+1);
+
+
+                  Sequence seq = parseId(line);
+                  ids.addElement(seq.getName());
+                  starts.addElement(seq.getStart() + "");
+                  ends.addElement(seq.getEnd() + "");
+                }
+            }
+            while (!idsFound);
+
+            int starCol = line.indexOf("*");
+            seqstrings = new StringBuffer[ids.size()];
+
+            for (int i = 0; i < ids.size(); i++)
+            {
+                if (seqstrings[i] == null)
+                {
+                    seqstrings[i] = new StringBuffer();
+                }
+            }
+
+            while ((line = nextLine()).indexOf("*") == -1)
+            {
+                for (int i = 0; i < ids.size(); i++)
+                {
+                    if (line.length() > (i + starCol))
+                    {
+                        seqstrings[i].append(line.charAt(i + starCol));
+                    }
+                }
+            }
+
+            for (int i = 0; i < ids.size(); i++)
+            {
+              Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
+                                             seqstrings[i].toString(),
+                                             Integer.parseInt(starts.elementAt(i).
+                                                              toString()),
+                                             Integer.parseInt(ends.elementAt(i).toString()));
+
+              if (!isValidProteinSequence(newSeq.getSequence()))
+              {
+                throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
+                               +" : "+ newSeq.getName()
+                               +" : "+invalidCharacter);
+
+              }
+
+                seqs.addElement(newSeq);
+            }
+
+    }
+
+    /**
+     * 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]) +"\n");
+
+            if (s[i].getSequence().length() > max)
+            {
+                max = s[i].getSequence().length();
+            }
+
+            i++;
+        }
+
+        out.append("* iteration 1\n");
+
+        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].getSequence().substring(j, j + 1));
+                }
+                else
+                {
+                    out.append("-");
+                }
+
+                i++;
+            }
+
+            out.append("\n");
+        }
+
+        out.append("*\n");
+
+        return out.toString();
+    }
+}