2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import jalview.datamodel.*;
24 import jalview.util.*;
32 public class MSFfile extends AlignFile
36 * Creates a new MSFfile object.
43 * Creates a new MSFfile object.
53 public MSFfile(String inFile, String type) throws IOException
58 public MSFfile(FileParse source) throws IOException
64 // TODO Auto-generated constructor stub
70 public void parse() throws IOException
73 boolean seqFlag = false;
74 String key = new String();
75 Vector headers = new Vector();
76 Hashtable seqhash = new Hashtable();
81 while ((line = nextLine()) != null)
83 StringTokenizer str = new StringTokenizer(line);
85 while (str.hasMoreTokens())
87 String inStr = str.nextToken();
89 // If line has header information add to the headers vector
90 if (inStr.indexOf("Name:") != -1)
92 key = str.nextToken();
93 headers.addElement(key);
96 // if line has // set SeqFlag to 1 so we know sequences are coming
97 if (inStr.indexOf("//") != -1)
102 // Process lines as sequence lines if seqFlag is set
103 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
105 // seqeunce id is the first field
108 StringBuffer tempseq;
110 // Get sequence from hash if it exists
111 if (seqhash.containsKey(key))
113 tempseq = (StringBuffer) seqhash.get(key);
117 tempseq = new StringBuffer();
118 seqhash.put(key, tempseq);
121 // loop through the rest of the words
122 while (str.hasMoreTokens())
124 // append the word to the sequence
125 tempseq.append(str.nextToken());
130 } catch (IOException e)
132 System.err.println("Exception parsing MSFFile " + e);
136 this.noSeqs = headers.size();
138 // Add sequences to the hash
139 for (i = 0; i < headers.size(); i++)
141 if (seqhash.get(headers.elementAt(i)) != null)
143 String head = headers.elementAt(i).toString();
144 String seq = seqhash.get(head).toString();
146 if (maxLength < head.length())
148 maxLength = head.length();
151 // Replace ~ with a sensible gap character
152 seq = seq.replace('~', '-');
154 Sequence newSeq = parseId(head);
156 newSeq.setSequence(seq);
158 seqs.addElement(newSeq);
162 System.err.println("MSFFile Parser: Can't find sequence for "
163 + headers.elementAt(i));
174 * @return DOCUMENT ME!
176 public int checkSum(String seq)
179 String sequence = seq.toUpperCase();
181 for (int i = 0; i < sequence.length(); i++)
186 int value = sequence.charAt(i);
189 check += (i % 57 + 1) * value;
191 } catch (Exception e)
193 System.err.println("Exception during MSF Checksum calculation");
198 return check % 10000;
209 * @return DOCUMENT ME!
211 public String print(SequenceI[] seqs)
214 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
216 SequenceI[] s = new SequenceI[seqs.length];
218 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
219 + "_MULTIPLE_ALIGNMENT 1.0\n\n"); // TODO: JBPNote : Jalview doesn't
220 // remember NA or AA yet.
226 while ((i < seqs.length) && (seqs[i] != null))
228 // Replace all internal gaps with . and external spaces with ~
229 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
232 StringBuffer sb = new StringBuffer();
233 sb.append(s[i].getSequence());
235 for (int ii = 0; ii < sb.length(); ii++)
237 if (sb.charAt(ii) == '.')
239 sb.setCharAt(ii, '~');
247 for (int ii = sb.length() - 1; ii > 0; ii--)
249 if (sb.charAt(ii) == '.')
251 sb.setCharAt(ii, '~');
259 s[i].setSequence(sb.toString());
261 if (s[i].getSequence().length > max)
263 max = s[i].getSequence().length;
269 Format maxLenpad = new Format("%" + (new String("" + max)).length()
271 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
276 int[] checksums = new int[s.length];
279 checksums[i] = checkSum(s[i].getSequenceAsString());
280 bigChecksum += checksums[i];
285 out.append(" MSF: " + s[0].getSequence().length + " Type: "
286 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
289 String[] nameBlock = new String[s.length];
290 String[] idBlock = new String[s.length];
293 while ((i < s.length) && (s[i] != null))
296 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
298 idBlock[i] = new String("Len: "
299 + maxLenpad.form(s[i].getSequence().length) + " Check: "
300 + maxChkpad.form(checksums[i]) + " Weight: 1.00\n");
302 if (s[i].getName().length() > maxid)
304 maxid = s[i].getName().length();
307 if (nameBlock[i].length() > maxNB)
309 maxNB = nameBlock[i].length();
325 Format nbFormat = new Format("%-" + maxNB + "s");
327 for (i = 0; (i < s.length) && (s[i] != null); i++)
329 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
333 out.append("\n\n//\n\n");
337 int nochunks = (max / len) + 1;
339 if ((max % len) == 0)
344 for (i = 0; i < nochunks; i++)
348 while ((j < s.length) && (s[j] != null))
350 String name = printId(s[j]);
352 out.append(new Format("%-" + maxid + "s").form(name + " "));
354 for (int k = 0; k < 5; k++)
356 int start = (i * 50) + (k * 10);
357 int end = start + 10;
359 if ((end < s[j].getSequence().length)
360 && (start < s[j].getSequence().length))
362 out.append(s[j].getSequence(start, end));
375 if (start < s[j].getSequence().length)
377 out.append(s[j].getSequenceAsString().substring(start));
396 return out.toString();
402 * @return DOCUMENT ME!
404 public String print()
406 return print(getSeqsAsArray());