2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceI;
25 import jalview.util.Format;
27 import java.io.IOException;
28 import java.util.Hashtable;
29 import java.util.StringTokenizer;
30 import java.util.Vector;
38 public class MSFfile extends AlignFile
42 * Creates a new MSFfile object.
49 * Creates a new MSFfile object.
59 public MSFfile(String inFile, String type) throws IOException
64 public MSFfile(FileParse source) throws IOException
72 public void parse() throws IOException
75 boolean seqFlag = false;
76 String key = new String();
77 Vector headers = new Vector();
78 Hashtable seqhash = new Hashtable();
83 while ((line = nextLine()) != null)
85 StringTokenizer str = new StringTokenizer(line);
87 while (str.hasMoreTokens())
89 String inStr = str.nextToken();
91 // If line has header information add to the headers vector
92 if (inStr.indexOf("Name:") != -1)
94 key = str.nextToken();
95 headers.addElement(key);
98 // if line has // set SeqFlag to 1 so we know sequences are coming
99 if (inStr.indexOf("//") != -1)
104 // Process lines as sequence lines if seqFlag is set
105 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
107 // seqeunce id is the first field
110 StringBuffer tempseq;
112 // Get sequence from hash if it exists
113 if (seqhash.containsKey(key))
115 tempseq = (StringBuffer) seqhash.get(key);
119 tempseq = new StringBuffer();
120 seqhash.put(key, tempseq);
123 // loop through the rest of the words
124 while (str.hasMoreTokens())
126 // append the word to the sequence
127 tempseq.append(str.nextToken());
132 } catch (IOException e)
134 System.err.println("Exception parsing MSFFile " + e);
138 this.noSeqs = headers.size();
140 // Add sequences to the hash
141 for (i = 0; i < headers.size(); i++)
143 if (seqhash.get(headers.elementAt(i)) != null)
145 String head = headers.elementAt(i).toString();
146 String seq = seqhash.get(head).toString();
148 if (maxLength < head.length())
150 maxLength = head.length();
153 // Replace ~ with a sensible gap character
154 seq = seq.replace('~', '-');
156 Sequence newSeq = parseId(head);
158 newSeq.setSequence(seq);
160 seqs.addElement(newSeq);
164 System.err.println("MSFFile Parser: Can't find sequence for "
165 + headers.elementAt(i));
176 * @return DOCUMENT ME!
178 public int checkSum(String seq)
181 String sequence = seq.toUpperCase();
183 for (int i = 0; i < sequence.length(); i++)
188 int value = sequence.charAt(i);
191 check += (i % 57 + 1) * value;
193 } catch (Exception e)
195 System.err.println("Exception during MSF Checksum calculation");
200 return check % 10000;
211 * @return DOCUMENT ME!
213 public String print(SequenceI[] seqs)
216 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
218 SequenceI[] s = new SequenceI[seqs.length];
220 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
221 + "_MULTIPLE_ALIGNMENT 1.0");
222 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
229 while ((i < seqs.length) && (seqs[i] != null))
231 // Replace all internal gaps with . and external spaces with ~
232 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
233 .replace('-', '.'), seqs[i].getStart(), seqs[i].getEnd());
235 StringBuffer sb = new StringBuffer();
236 sb.append(s[i].getSequence());
238 for (int ii = 0; ii < sb.length(); ii++)
240 if (sb.charAt(ii) == '.')
242 sb.setCharAt(ii, '~');
250 for (int ii = sb.length() - 1; ii > 0; ii--)
252 if (sb.charAt(ii) == '.')
254 sb.setCharAt(ii, '~');
262 s[i].setSequence(sb.toString());
264 if (s[i].getSequence().length > max)
266 max = s[i].getSequence().length;
272 Format maxLenpad = new Format("%" + (new String("" + max)).length()
274 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
279 int[] checksums = new int[s.length];
282 checksums[i] = checkSum(s[i].getSequenceAsString());
283 bigChecksum += checksums[i];
288 out.append(" MSF: " + s[0].getSequence().length + " Type: "
289 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
295 String[] nameBlock = new String[s.length];
296 String[] idBlock = new String[s.length];
299 while ((i < s.length) && (s[i] != null))
302 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
304 idBlock[i] = new String("Len: "
305 + maxLenpad.form(s[i].getSequence().length) + " Check: "
306 + maxChkpad.form(checksums[i]) + " Weight: 1.00" + newline);
308 if (s[i].getName().length() > maxid)
310 maxid = s[i].getName().length();
313 if (nameBlock[i].length() > maxNB)
315 maxNB = nameBlock[i].length();
331 Format nbFormat = new Format("%-" + maxNB + "s");
333 for (i = 0; (i < s.length) && (s[i] != null); i++)
335 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
346 int nochunks = (max / len) + 1;
348 if ((max % len) == 0)
353 for (i = 0; i < nochunks; i++)
357 while ((j < s.length) && (s[j] != null))
359 String name = printId(s[j]);
361 out.append(new Format("%-" + maxid + "s").form(name + " "));
363 for (int k = 0; k < 5; k++)
365 int start = (i * 50) + (k * 10);
366 int end = start + 10;
368 if ((end < s[j].getSequence().length)
369 && (start < s[j].getSequence().length))
371 out.append(s[j].getSequence(start, end));
384 if (start < s[j].getSequence().length)
386 out.append(s[j].getSequenceAsString().substring(start));
405 return out.toString();
411 * @return DOCUMENT ME!
413 public String print()
415 return print(getSeqsAsArray());