2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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");
220 // TODO: JBPNote : Jalview doesn't 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)
293 String[] nameBlock = new String[s.length];
294 String[] idBlock = new String[s.length];
297 while ((i < s.length) && (s[i] != null))
300 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
302 idBlock[i] = new String("Len: "
303 + maxLenpad.form(s[i].getSequence().length) + " Check: "
304 + maxChkpad.form(checksums[i]) + " Weight: 1.00"+newline);
306 if (s[i].getName().length() > maxid)
308 maxid = s[i].getName().length();
311 if (nameBlock[i].length() > maxNB)
313 maxNB = nameBlock[i].length();
329 Format nbFormat = new Format("%-" + maxNB + "s");
331 for (i = 0; (i < s.length) && (s[i] != null); i++)
333 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
338 out.append(newline);out.append("//");
343 int nochunks = (max / len) + 1;
345 if ((max % len) == 0)
350 for (i = 0; i < nochunks; i++)
354 while ((j < s.length) && (s[j] != null))
356 String name = printId(s[j]);
358 out.append(new Format("%-" + maxid + "s").form(name + " "));
360 for (int k = 0; k < 5; k++)
362 int start = (i * 50) + (k * 10);
363 int end = start + 10;
365 if ((end < s[j].getSequence().length)
366 && (start < s[j].getSequence().length))
368 out.append(s[j].getSequence(start, end));
381 if (start < s[j].getSequence().length)
383 out.append(s[j].getSequenceAsString().substring(start));
402 return out.toString();
408 * @return DOCUMENT ME!
410 public String print()
412 return print(getSeqsAsArray());