Formatting changes
[jalview.git] / src / jalview / io / PileUpfile.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 /**\r
22  * <p>Title: </p>\r
23  *  PileUpfile\r
24  * <p>Description: </p>\r
25  *\r
26  *  Read and write PileUp style MSF Files.\r
27  *  This used to be the MSFFile class, and was written according to the EBI's idea\r
28  *  of a subset of the MSF alignment format. But, that was updated to reflect current\r
29  *  GCG style IO fashion, as found in Emboss (thanks David Martin!)\r
30  *\r
31  **/\r
32 import java.io.*;\r
33 import java.util.*;\r
34 \r
35 import jalview.datamodel.*;\r
36 import jalview.util.*;\r
37 \r
38 public class PileUpfile\r
39     extends AlignFile\r
40 {\r
41   public PileUpfile()\r
42   {\r
43   }\r
44 \r
45   public PileUpfile(String inStr)\r
46   {\r
47     super(inStr);\r
48   }\r
49 \r
50   public PileUpfile(String inFile, String type)\r
51       throws IOException\r
52   {\r
53     super(inFile, type);\r
54   }\r
55 \r
56   public void parse()\r
57   {\r
58     int i = 0;\r
59     boolean seqFlag = false;\r
60     String key = new String();\r
61     Vector headers = new Vector();\r
62     Hashtable seqhash = new Hashtable();\r
63     String line;\r
64 \r
65     try\r
66     {\r
67       while ( (line = nextLine()) != null)\r
68       {\r
69         StringTokenizer str = new StringTokenizer(line);\r
70 \r
71         while (str.hasMoreTokens())\r
72         {\r
73           String inStr = str.nextToken();\r
74 \r
75           //If line has header information add to the headers vector\r
76           if (inStr.indexOf("Name:") != -1)\r
77           {\r
78             key = str.nextToken();\r
79             headers.addElement(key);\r
80           }\r
81 \r
82           //if line has // set SeqFlag to 1 so we know sequences are coming\r
83           if (inStr.indexOf("//") != -1)\r
84           {\r
85             seqFlag = true;\r
86           }\r
87 \r
88           //Process lines as sequence lines if seqFlag is set\r
89           if ( (inStr.indexOf("//") == -1) && (seqFlag == true))\r
90           {\r
91             //seqeunce id is the first field\r
92             key = inStr;\r
93 \r
94             StringBuffer tempseq;\r
95 \r
96             //Get sequence from hash if it exists\r
97             if (seqhash.containsKey(key))\r
98             {\r
99               tempseq = (StringBuffer) seqhash.get(key);\r
100             }\r
101             else\r
102             {\r
103               tempseq = new StringBuffer();\r
104               seqhash.put(key, tempseq);\r
105             }\r
106 \r
107             //loop through the rest of the words\r
108             while (str.hasMoreTokens())\r
109             {\r
110               //append the word to the sequence\r
111               tempseq.append(str.nextToken());\r
112             }\r
113           }\r
114         }\r
115       }\r
116     }\r
117     catch (IOException e)\r
118     {\r
119       System.err.println("Exception parsing PileUpfile " + e);\r
120       e.printStackTrace();\r
121     }\r
122 \r
123     this.noSeqs = headers.size();\r
124 \r
125     //Add sequences to the hash\r
126     for (i = 0; i < headers.size(); i++)\r
127     {\r
128       if (seqhash.get(headers.elementAt(i)) != null)\r
129       {\r
130         String head = headers.elementAt(i).toString();\r
131         String seq = seqhash.get(head).toString();\r
132 \r
133         int start = 1;\r
134         int end = seq.length();\r
135 \r
136         if (maxLength < head.length())\r
137         {\r
138           maxLength = head.length();\r
139         }\r
140 \r
141         if (head.indexOf("/") > 0)\r
142         {\r
143           StringTokenizer st = new StringTokenizer(head, "/");\r
144 \r
145           if (st.countTokens() == 2)\r
146           {\r
147             head = st.nextToken();\r
148 \r
149             String tmp = st.nextToken();\r
150             st = new StringTokenizer(tmp, "-");\r
151 \r
152             if (st.countTokens() == 2)\r
153             {\r
154               start = Integer.valueOf(st.nextToken()).intValue();\r
155               end = Integer.valueOf(st.nextToken()).intValue();\r
156             }\r
157           }\r
158         }\r
159 \r
160         Sequence newSeq = new Sequence(head, seq, start, end);\r
161 \r
162         seqs.addElement(newSeq);\r
163       }\r
164       else\r
165       {\r
166         System.err.println(\r
167             "PileUpfile Parser: Can't find sequence for " +\r
168             headers.elementAt(i));\r
169       }\r
170     }\r
171   }\r
172 \r
173   public static int checkSum(String seq)\r
174   {\r
175     //String chars =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";\r
176     int check = 0;\r
177 \r
178     String index = "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";\r
179     index += "--------------------------------------------------------------------------------------------------------------------------------";\r
180 \r
181     for (int i = 0; i < seq.length(); i++)\r
182     {\r
183       try\r
184       {\r
185         if (i < seq.length())\r
186         {\r
187           int pos = index.indexOf(seq.substring(i, i + 1));\r
188 \r
189           if (!index.substring(pos, pos + 1).equals("_"))\r
190           {\r
191             check += ( ( (i % 57) + 1) * pos);\r
192           }\r
193         }\r
194       }\r
195       catch (Exception e)\r
196       {\r
197         System.err.println("Exception during MSF Checksum calculation");\r
198         e.printStackTrace();\r
199       }\r
200     }\r
201 \r
202     return check % 10000;\r
203   }\r
204 \r
205   public static String print(SequenceI[] s)\r
206   {\r
207     StringBuffer out = new StringBuffer("PileUp\n\n");\r
208 \r
209     int max = 0;\r
210     int maxid = 0;\r
211 \r
212     int i = 0;\r
213     String big = "";\r
214 \r
215     while ( (i < s.length) && (s[i] != null))\r
216     {\r
217       big += s[i].getSequence();\r
218       i++;\r
219     }\r
220 \r
221     i = 0;\r
222 \r
223     int bigcheck = checkSum(big);\r
224 \r
225     out.append("   MSF: " + s[0].getSequence().length() +\r
226                "   Type: P    Check:  " + bigcheck + "   ..\n\n\n");\r
227 \r
228     while ( (i < s.length) && (s[i] != null))\r
229     {\r
230       String seq = s[i].getSequence();\r
231       String name = s[i].getName() + "/" + s[i].getStart() + "-" +\r
232           s[i].getEnd();\r
233       int check = checkSum(s[i].getSequence());\r
234       out.append(" Name: " + name + " oo  Len:  " +\r
235                  s[i].getSequence().length() + "  Check:  " + check +\r
236                  "  Weight:  1.00\n");\r
237 \r
238       if (seq.length() > max)\r
239       {\r
240         max = seq.length();\r
241       }\r
242 \r
243       if (name.length() > maxid)\r
244       {\r
245         maxid = name.length();\r
246       }\r
247 \r
248       i++;\r
249     }\r
250 \r
251     if (maxid < 10)\r
252     {\r
253       maxid = 10;\r
254     }\r
255 \r
256     maxid++;\r
257     out.append("\n\n//\n\n");\r
258 \r
259     int len = 50;\r
260 \r
261     int nochunks = (max / len) + 1;\r
262 \r
263     if ( (max % len) == 0)\r
264     {\r
265       nochunks--;\r
266     }\r
267 \r
268     for (i = 0; i < nochunks; i++)\r
269     {\r
270       int j = 0;\r
271 \r
272       while ( (j < s.length) && (s[j] != null))\r
273       {\r
274         String name = s[j].getName();\r
275         out.append(new Format("%-" + maxid + "s").form(name + "/" +\r
276             s[j].getStart() + "-" + s[j].getEnd()) + " ");\r
277 \r
278         for (int k = 0; k < 5; k++)\r
279         {\r
280           int start = (i * 50) + (k * 10);\r
281           int end = start + 10;\r
282 \r
283           if ( (end < s[j].getSequence().length()) &&\r
284               (start < s[j].getSequence().length()))\r
285           {\r
286             out.append(s[j].getSequence().substring(start, end));\r
287 \r
288             if (k < 4)\r
289             {\r
290               out.append(" ");\r
291             }\r
292             else\r
293             {\r
294               out.append("\n");\r
295             }\r
296           }\r
297           else\r
298           {\r
299             if (start < s[j].getSequence().length())\r
300             {\r
301               out.append(s[j].getSequence().substring(start));\r
302               out.append("\n");\r
303             }\r
304             else\r
305             {\r
306               if (k == 0)\r
307               {\r
308                 out.append("\n");\r
309               }\r
310             }\r
311           }\r
312         }\r
313 \r
314         j++;\r
315       }\r
316 \r
317       out.append("\n");\r
318     }\r
319 \r
320     return out.toString();\r
321   }\r
322 \r
323   public String print()\r
324   {\r
325     return print(getSeqsAsArray());\r
326   }\r
327 }\r