tidied up system.out messages and moved many to stderr.
[jalview.git] / src / jalview / io / BLCFile.java
1 /* Jalview - a java multiple alignment editor\r
2  * Copyright (C) 1998  Michele Clamp\r
3  *\r
4  * This program is free software; you can redistribute it and/or\r
5  * modify it under the terms of the GNU General Public License\r
6  * as published by the Free Software Foundation; either version 2\r
7  * of the License, or (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program; if not, write to the Free Software\r
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
17  */\r
18 package jalview.io;\r
19 \r
20 import jalview.datamodel.*;\r
21 \r
22 import java.io.*;\r
23 import java.util.*;\r
24 \r
25 public class BLCFile extends AlignFile {\r
26 \r
27   Vector titles;\r
28 \r
29   public BLCFile()\r
30   {}\r
31 \r
32   public BLCFile(String inStr) {\r
33     super(inStr);\r
34   }\r
35 \r
36   public void initData() {\r
37     super.initData();\r
38     titles = new Vector();\r
39   }\r
40 \r
41   public BLCFile(String inFile, String type) throws IOException {\r
42     super(inFile,type);\r
43   }\r
44 \r
45 \r
46   public void parse()\r
47   {\r
48     boolean idsFound=false;\r
49     Vector ids = new Vector();\r
50     StringBuffer seqstrings [];\r
51     Vector starts = new Vector();\r
52     Vector ends = new Vector();\r
53 \r
54    String line=null;\r
55    try{\r
56      do{\r
57       line = nextLine();\r
58 \r
59       // seek end of ids\r
60       if (line.indexOf("*") > -1)\r
61       {\r
62         idsFound=true;\r
63         break;\r
64       }\r
65 \r
66       int abracket = line.indexOf(">");\r
67 \r
68       if (abracket > -1)\r
69       {\r
70 \r
71         if(line.indexOf(" ")>-1)//\r
72         {\r
73           ///Colur it be this format?\r
74           //>54402046         0             1   137   137:\r
75           // or this??\r
76           //     1   >L1H14       30539  343\r
77           try{\r
78             ids.addElement(line.substring(abracket + 1,\r
79                                           line.indexOf(" ", abracket + 1)));\r
80 \r
81 \r
82             // remove p Value\r
83             line = line.substring(abracket + 1);\r
84             line = line.substring(line.indexOf(" ") + 1);\r
85             line = line.trim();\r
86             line = line.substring(line.indexOf(" ") + 1);\r
87             line = line.trim();\r
88             int value = Integer.parseInt(line.substring(0, line.indexOf(" ")));\r
89             starts.addElement(value+"");\r
90             line = line.substring(line.indexOf(" ") + 1);\r
91             line = line.trim();\r
92             value = Integer.parseInt(line.substring(0, line.indexOf(" ")));\r
93             ends.addElement(value+"");\r
94 \r
95           }catch(Exception ex)\r
96           {\r
97             System.err.println("Error during blockfile read.");\r
98             ex.printStackTrace();\r
99             starts.addElement("0");\r
100             ends.addElement("0");\r
101           }\r
102 \r
103         }\r
104         else\r
105         {\r
106           ids.addElement( line.substring(abracket + 1, line.indexOf("/")));\r
107           line = line.substring(line.indexOf("/") + 1);\r
108           starts.addElement(line.substring(0, line.indexOf("-")));\r
109           ends.addElement(line.substring(line.indexOf("-") + 1));\r
110         }\r
111       }\r
112     }while(!idsFound);\r
113 \r
114     int starCol = line.indexOf("*");\r
115     seqstrings = new StringBuffer[ids.size()];\r
116     for(int i=0; i<ids.size(); i++)\r
117     {\r
118       if(seqstrings[i]==null)\r
119         seqstrings[i] = new StringBuffer();\r
120     }\r
121 \r
122     while ((line = nextLine()).indexOf("*")==-1)\r
123     {\r
124       for(int i=0; i<ids.size(); i++)\r
125       {\r
126         if(line.length()>i+starCol)\r
127           seqstrings[i].append(line.charAt(i + starCol));\r
128       }\r
129     }\r
130 \r
131     for(int i=0; i<ids.size(); i++)\r
132     {\r
133       Sequence newSeq = new Sequence(ids.elementAt(i).toString(),\r
134                                      seqstrings[i].toString(),\r
135                                      Integer.parseInt(starts.elementAt(i).toString()),\r
136                                      Integer.parseInt(ends.elementAt(i).toString()));\r
137       seqs.addElement(newSeq);\r
138     }\r
139 \r
140    }catch(Exception ex){ex.printStackTrace();}\r
141   }\r
142 \r
143   public String print() {\r
144     return print(getSeqsAsArray());\r
145   }\r
146   public static String print(SequenceI[] s) {\r
147     StringBuffer out = new StringBuffer();\r
148 \r
149     int i=0;\r
150     int max = -1;\r
151     while (i < s.length && s[i] != null) {\r
152       out.append(">" + s[i].getName() + "/" + s[i].getStart() + "-" + s[i].getEnd() + "\n");\r
153       if (s[i].getSequence().length() > max) { max = s[i].getSequence().length();}\r
154       i++;\r
155     }\r
156 \r
157       out.append("* iteration 1\n");\r
158       for (int j = 0; j < max; j++) {\r
159         i=0;\r
160         while (i < s.length && s[i] != null) {\r
161           if(s[i].getSequence().length()>j )\r
162             out.append(s[i].getSequence().substring(j,j+1));\r
163           else\r
164             out.append("-");\r
165           i++;\r
166         }\r
167         out.append("\n");\r
168       }\r
169     out.append("*\n");\r
170     return out.toString();\r
171 \r
172   }\r
173 }\r