JAL-1432 updated copyright notices
[jalview.git] / src / jalview / io / MSFfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.util.*;
23
24 import jalview.datamodel.*;
25 import jalview.util.*;
26
27 /**
28  * DOCUMENT ME!
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class MSFfile extends AlignFile
34 {
35
36   /**
37    * Creates a new MSFfile object.
38    */
39   public MSFfile()
40   {
41   }
42
43   /**
44    * Creates a new MSFfile object.
45    * 
46    * @param inFile
47    *          DOCUMENT ME!
48    * @param type
49    *          DOCUMENT ME!
50    * 
51    * @throws IOException
52    *           DOCUMENT ME!
53    */
54   public MSFfile(String inFile, String type) throws IOException
55   {
56     super(inFile, type);
57   }
58
59   public MSFfile(FileParse source) throws IOException
60   {
61     super(source);
62   }
63
64   {
65     // TODO Auto-generated constructor stub
66   }
67
68   /**
69    * DOCUMENT ME!
70    */
71   public void parse() throws IOException
72   {
73     int i = 0;
74     boolean seqFlag = false;
75     String key = new String();
76     Vector headers = new Vector();
77     Hashtable seqhash = new Hashtable();
78     String line;
79
80     try
81     {
82       while ((line = nextLine()) != null)
83       {
84         StringTokenizer str = new StringTokenizer(line);
85
86         while (str.hasMoreTokens())
87         {
88           String inStr = str.nextToken();
89
90           // If line has header information add to the headers vector
91           if (inStr.indexOf("Name:") != -1)
92           {
93             key = str.nextToken();
94             headers.addElement(key);
95           }
96
97           // if line has // set SeqFlag to 1 so we know sequences are coming
98           if (inStr.indexOf("//") != -1)
99           {
100             seqFlag = true;
101           }
102
103           // Process lines as sequence lines if seqFlag is set
104           if ((inStr.indexOf("//") == -1) && (seqFlag == true))
105           {
106             // seqeunce id is the first field
107             key = inStr;
108
109             StringBuffer tempseq;
110
111             // Get sequence from hash if it exists
112             if (seqhash.containsKey(key))
113             {
114               tempseq = (StringBuffer) seqhash.get(key);
115             }
116             else
117             {
118               tempseq = new StringBuffer();
119               seqhash.put(key, tempseq);
120             }
121
122             // loop through the rest of the words
123             while (str.hasMoreTokens())
124             {
125               // append the word to the sequence
126               tempseq.append(str.nextToken());
127             }
128           }
129         }
130       }
131     } catch (IOException e)
132     {
133       System.err.println("Exception parsing MSFFile " + e);
134       e.printStackTrace();
135     }
136
137     this.noSeqs = headers.size();
138
139     // Add sequences to the hash
140     for (i = 0; i < headers.size(); i++)
141     {
142       if (seqhash.get(headers.elementAt(i)) != null)
143       {
144         String head = headers.elementAt(i).toString();
145         String seq = seqhash.get(head).toString();
146
147         if (maxLength < head.length())
148         {
149           maxLength = head.length();
150         }
151
152         // Replace ~ with a sensible gap character
153         seq = seq.replace('~', '-');
154
155         Sequence newSeq = parseId(head);
156
157         newSeq.setSequence(seq);
158
159         seqs.addElement(newSeq);
160       }
161       else
162       {
163         System.err.println("MSFFile Parser: Can't find sequence for "
164                 + headers.elementAt(i));
165       }
166     }
167   }
168
169   /**
170    * DOCUMENT ME!
171    * 
172    * @param seq
173    *          DOCUMENT ME!
174    * 
175    * @return DOCUMENT ME!
176    */
177   public int checkSum(String seq)
178   {
179     int check = 0;
180     String sequence = seq.toUpperCase();
181
182     for (int i = 0; i < sequence.length(); i++)
183     {
184       try
185       {
186
187         int value = sequence.charAt(i);
188         if (value != -1)
189         {
190           check += (i % 57 + 1) * value;
191         }
192       } catch (Exception e)
193       {
194         System.err.println("Exception during MSF Checksum calculation");
195         e.printStackTrace();
196       }
197     }
198
199     return check % 10000;
200   }
201
202   /**
203    * DOCUMENT ME!
204    * 
205    * @param s
206    *          DOCUMENT ME!
207    * @param is_NA
208    *          DOCUMENT ME!
209    * 
210    * @return DOCUMENT ME!
211    */
212   public String print(SequenceI[] seqs)
213   {
214
215     boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
216
217     SequenceI[] s = new SequenceI[seqs.length];
218
219     StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
220             + "_MULTIPLE_ALIGNMENT 1.0");
221     // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
222     out.append(newline);
223     out.append(newline);
224     int max = 0;
225     int maxid = 0;
226     int i = 0;
227
228     while ((i < seqs.length) && (seqs[i] != null))
229     {
230       // Replace all internal gaps with . and external spaces with ~
231       s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
232               .replace('-', '.'), seqs[i].getStart(), seqs[i].getEnd());
233
234       StringBuffer sb = new StringBuffer();
235       sb.append(s[i].getSequence());
236
237       for (int ii = 0; ii < sb.length(); ii++)
238       {
239         if (sb.charAt(ii) == '.')
240         {
241           sb.setCharAt(ii, '~');
242         }
243         else
244         {
245           break;
246         }
247       }
248
249       for (int ii = sb.length() - 1; ii > 0; ii--)
250       {
251         if (sb.charAt(ii) == '.')
252         {
253           sb.setCharAt(ii, '~');
254         }
255         else
256         {
257           break;
258         }
259       }
260
261       s[i].setSequence(sb.toString());
262
263       if (s[i].getSequence().length > max)
264       {
265         max = s[i].getSequence().length;
266       }
267
268       i++;
269     }
270
271     Format maxLenpad = new Format("%" + (new String("" + max)).length()
272             + "d");
273     Format maxChkpad = new Format("%" + (new String("1" + max)).length()
274             + "d");
275     i = 0;
276
277     int bigChecksum = 0;
278     int[] checksums = new int[s.length];
279     while (i < s.length)
280     {
281       checksums[i] = checkSum(s[i].getSequenceAsString());
282       bigChecksum += checksums[i];
283       i++;
284     }
285
286     long maxNB = 0;
287     out.append("   MSF: " + s[0].getSequence().length + "   Type: "
288             + (is_NA ? "N" : "P") + "    Check:  " + (bigChecksum % 10000)
289             + "   ..");
290     out.append(newline);
291     out.append(newline);
292     out.append(newline);
293
294     String[] nameBlock = new String[s.length];
295     String[] idBlock = new String[s.length];
296
297     i = 0;
298     while ((i < s.length) && (s[i] != null))
299     {
300
301       nameBlock[i] = new String("  Name: " + printId(s[i]) + " ");
302
303       idBlock[i] = new String("Len: "
304               + maxLenpad.form(s[i].getSequence().length) + "  Check: "
305               + maxChkpad.form(checksums[i]) + "  Weight: 1.00" + newline);
306
307       if (s[i].getName().length() > maxid)
308       {
309         maxid = s[i].getName().length();
310       }
311
312       if (nameBlock[i].length() > maxNB)
313       {
314         maxNB = nameBlock[i].length();
315       }
316
317       i++;
318     }
319
320     if (maxid < 10)
321     {
322       maxid = 10;
323     }
324
325     if (maxNB < 15)
326     {
327       maxNB = 15;
328     }
329
330     Format nbFormat = new Format("%-" + maxNB + "s");
331
332     for (i = 0; (i < s.length) && (s[i] != null); i++)
333     {
334       out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
335     }
336
337     maxid++;
338     out.append(newline);
339     out.append(newline);
340     out.append("//");
341     out.append(newline);
342     out.append(newline);
343     int len = 50;
344
345     int nochunks = (max / len) + 1;
346
347     if ((max % len) == 0)
348     {
349       nochunks--;
350     }
351
352     for (i = 0; i < nochunks; i++)
353     {
354       int j = 0;
355
356       while ((j < s.length) && (s[j] != null))
357       {
358         String name = printId(s[j]);
359
360         out.append(new Format("%-" + maxid + "s").form(name + " "));
361
362         for (int k = 0; k < 5; k++)
363         {
364           int start = (i * 50) + (k * 10);
365           int end = start + 10;
366
367           if ((end < s[j].getSequence().length)
368                   && (start < s[j].getSequence().length))
369           {
370             out.append(s[j].getSequence(start, end));
371
372             if (k < 4)
373             {
374               out.append(" ");
375             }
376             else
377             {
378               out.append(newline);
379             }
380           }
381           else
382           {
383             if (start < s[j].getSequence().length)
384             {
385               out.append(s[j].getSequenceAsString().substring(start));
386               out.append(newline);
387             }
388             else
389             {
390               if (k == 0)
391               {
392                 out.append(newline);
393               }
394             }
395           }
396         }
397
398         j++;
399       }
400
401       out.append(newline);
402     }
403
404     return out.toString();
405   }
406
407   /**
408    * DOCUMENT ME!
409    * 
410    * @return DOCUMENT ME!
411    */
412   public String print()
413   {
414     return print(getSeqsAsArray());
415   }
416 }