2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import jalview.datamodel.*;
25 import jalview.util.*;
33 public class MSFfile extends AlignFile
37 * Creates a new MSFfile object.
44 * Creates a new MSFfile object.
54 public MSFfile(String inFile, String type) throws IOException
59 public MSFfile(FileParse source) throws IOException
65 // TODO Auto-generated constructor stub
71 public void parse() throws IOException
74 boolean seqFlag = false;
75 String key = new String();
76 Vector headers = new Vector();
77 Hashtable seqhash = new Hashtable();
82 while ((line = nextLine()) != null)
84 StringTokenizer str = new StringTokenizer(line);
86 while (str.hasMoreTokens())
88 String inStr = str.nextToken();
90 // If line has header information add to the headers vector
91 if (inStr.indexOf("Name:") != -1)
93 key = str.nextToken();
94 headers.addElement(key);
97 // if line has // set SeqFlag to 1 so we know sequences are coming
98 if (inStr.indexOf("//") != -1)
103 // Process lines as sequence lines if seqFlag is set
104 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
106 // seqeunce id is the first field
109 StringBuffer tempseq;
111 // Get sequence from hash if it exists
112 if (seqhash.containsKey(key))
114 tempseq = (StringBuffer) seqhash.get(key);
118 tempseq = new StringBuffer();
119 seqhash.put(key, tempseq);
122 // loop through the rest of the words
123 while (str.hasMoreTokens())
125 // append the word to the sequence
126 tempseq.append(str.nextToken());
131 } catch (IOException e)
133 System.err.println("Exception parsing MSFFile " + e);
137 this.noSeqs = headers.size();
139 // Add sequences to the hash
140 for (i = 0; i < headers.size(); i++)
142 if (seqhash.get(headers.elementAt(i)) != null)
144 String head = headers.elementAt(i).toString();
145 String seq = seqhash.get(head).toString();
147 if (maxLength < head.length())
149 maxLength = head.length();
152 // Replace ~ with a sensible gap character
153 seq = seq.replace('~', '-');
155 Sequence newSeq = parseId(head);
157 newSeq.setSequence(seq);
159 seqs.addElement(newSeq);
163 System.err.println("MSFFile Parser: Can't find sequence for "
164 + headers.elementAt(i));
175 * @return DOCUMENT ME!
177 public int checkSum(String seq)
180 String sequence = seq.toUpperCase();
182 for (int i = 0; i < sequence.length(); i++)
187 int value = sequence.charAt(i);
190 check += (i % 57 + 1) * value;
192 } catch (Exception e)
194 System.err.println("Exception during MSF Checksum calculation");
199 return check % 10000;
210 * @return DOCUMENT ME!
212 public String print(SequenceI[] seqs)
215 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
217 SequenceI[] s = new SequenceI[seqs.length];
219 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
220 + "_MULTIPLE_ALIGNMENT 1.0\n\n"); // TODO: JBPNote : Jalview doesn't
221 // remember NA or AA yet.
227 while ((i < seqs.length) && (seqs[i] != null))
229 // Replace all internal gaps with . and external spaces with ~
230 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
233 StringBuffer sb = new StringBuffer();
234 sb.append(s[i].getSequence());
236 for (int ii = 0; ii < sb.length(); ii++)
238 if (sb.charAt(ii) == '.')
240 sb.setCharAt(ii, '~');
248 for (int ii = sb.length() - 1; ii > 0; ii--)
250 if (sb.charAt(ii) == '.')
252 sb.setCharAt(ii, '~');
260 s[i].setSequence(sb.toString());
262 if (s[i].getSequence().length > max)
264 max = s[i].getSequence().length;
270 Format maxLenpad = new Format("%" + (new String("" + max)).length()
272 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
277 int[] checksums = new int[s.length];
280 checksums[i] = checkSum(s[i].getSequenceAsString());
281 bigChecksum += checksums[i];
286 out.append(" MSF: " + s[0].getSequence().length + " Type: "
287 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
290 String[] nameBlock = new String[s.length];
291 String[] idBlock = new String[s.length];
294 while ((i < s.length) && (s[i] != null))
297 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
299 idBlock[i] = new String("Len: "
300 + maxLenpad.form(s[i].getSequence().length) + " Check: "
301 + maxChkpad.form(checksums[i]) + " Weight: 1.00\n");
303 if (s[i].getName().length() > maxid)
305 maxid = s[i].getName().length();
308 if (nameBlock[i].length() > maxNB)
310 maxNB = nameBlock[i].length();
326 Format nbFormat = new Format("%-" + maxNB + "s");
328 for (i = 0; (i < s.length) && (s[i] != null); i++)
330 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
334 out.append("\n\n//\n\n");
338 int nochunks = (max / len) + 1;
340 if ((max % len) == 0)
345 for (i = 0; i < nochunks; i++)
349 while ((j < s.length) && (s[j] != null))
351 String name = printId(s[j]);
353 out.append(new Format("%-" + maxid + "s").form(name + " "));
355 for (int k = 0; k < 5; k++)
357 int start = (i * 50) + (k * 10);
358 int end = start + 10;
360 if ((end < s[j].getSequence().length)
361 && (start < s[j].getSequence().length))
363 out.append(s[j].getSequence(start, end));
376 if (start < s[j].getSequence().length)
378 out.append(s[j].getSequenceAsString().substring(start));
397 return out.toString();
403 * @return DOCUMENT ME!
405 public String print()
407 return print(getSeqsAsArray());