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, DataSourceType sourceType)
62 super(inFile, sourceType);
65 public MSFfile(FileParse source) throws IOException
74 public void parse() throws IOException
77 boolean seqFlag = false;
78 String key = new String();
79 Vector headers = new Vector();
80 Hashtable seqhash = new Hashtable();
85 while ((line = nextLine()) != null)
87 StringTokenizer str = new StringTokenizer(line);
89 while (str.hasMoreTokens())
91 String inStr = str.nextToken();
93 // If line has header information add to the headers vector
94 if (inStr.indexOf("Name:") != -1)
96 key = str.nextToken();
97 headers.addElement(key);
100 // if line has // set SeqFlag to 1 so we know sequences are coming
101 if (inStr.indexOf("//") != -1)
106 // Process lines as sequence lines if seqFlag is set
107 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
109 // seqeunce id is the first field
112 StringBuffer tempseq;
114 // Get sequence from hash if it exists
115 if (seqhash.containsKey(key))
117 tempseq = (StringBuffer) seqhash.get(key);
121 tempseq = new StringBuffer();
122 seqhash.put(key, tempseq);
125 // loop through the rest of the words
126 while (str.hasMoreTokens())
128 // append the word to the sequence
129 tempseq.append(str.nextToken());
134 } catch (IOException e)
136 System.err.println("Exception parsing MSFFile " + e);
140 this.noSeqs = headers.size();
142 // Add sequences to the hash
143 for (i = 0; i < headers.size(); i++)
145 if (seqhash.get(headers.elementAt(i)) != null)
147 String head = headers.elementAt(i).toString();
148 String seq = seqhash.get(head).toString();
150 if (maxLength < head.length())
152 maxLength = head.length();
155 // Replace ~ with a sensible gap character
156 seq = seq.replace('~', '-');
158 Sequence newSeq = parseId(head);
160 newSeq.setSequence(seq);
162 seqs.addElement(newSeq);
166 System.err.println("MSFFile Parser: Can't find sequence for "
167 + headers.elementAt(i));
178 * @return DOCUMENT ME!
180 public int checkSum(String seq)
183 String sequence = seq.toUpperCase();
185 for (int i = 0; i < sequence.length(); i++)
190 int value = sequence.charAt(i);
193 check += (i % 57 + 1) * value;
195 } catch (Exception e)
197 System.err.println("Exception during MSF Checksum calculation");
202 return check % 10000;
213 * @return DOCUMENT ME!
215 public String print(SequenceI[] seqs)
218 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
220 SequenceI[] s = new SequenceI[seqs.length];
222 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
223 + "_MULTIPLE_ALIGNMENT 1.0");
224 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
231 while ((i < seqs.length) && (seqs[i] != null))
233 // Replace all internal gaps with . and external spaces with ~
234 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
235 .replace('-', '.'), seqs[i].getStart(), seqs[i].getEnd());
237 StringBuffer sb = new StringBuffer();
238 sb.append(s[i].getSequence());
240 for (int ii = 0; ii < sb.length(); ii++)
242 if (sb.charAt(ii) == '.')
244 sb.setCharAt(ii, '~');
252 for (int ii = sb.length() - 1; ii > 0; ii--)
254 if (sb.charAt(ii) == '.')
256 sb.setCharAt(ii, '~');
264 s[i].setSequence(sb.toString());
266 if (s[i].getSequence().length > max)
268 max = s[i].getSequence().length;
274 Format maxLenpad = new Format("%" + (new String("" + max)).length()
276 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
281 int[] checksums = new int[s.length];
284 checksums[i] = checkSum(s[i].getSequenceAsString());
285 bigChecksum += checksums[i];
290 out.append(" MSF: " + s[0].getSequence().length + " Type: "
291 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
297 String[] nameBlock = new String[s.length];
298 String[] idBlock = new String[s.length];
301 while ((i < s.length) && (s[i] != null))
304 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
306 idBlock[i] = new String("Len: "
307 + maxLenpad.form(s[i].getSequence().length) + " Check: "
308 + maxChkpad.form(checksums[i]) + " Weight: 1.00" + newline);
310 if (s[i].getName().length() > maxid)
312 maxid = s[i].getName().length();
315 if (nameBlock[i].length() > maxNB)
317 maxNB = nameBlock[i].length();
333 Format nbFormat = new Format("%-" + maxNB + "s");
335 for (i = 0; (i < s.length) && (s[i] != null); i++)
337 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
348 int nochunks = (max / len) + (max % len > 0 ? 1 : 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!
411 public String print()
413 return print(getSeqsAsArray());