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