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.Comparison;
26 import jalview.util.Format;
28 import java.io.IOException;
29 import java.util.Hashtable;
30 import java.util.StringTokenizer;
31 import java.util.Vector;
39 public class MSFfile extends AlignFile
43 * Creates a new MSFfile object.
50 * Creates a new MSFfile object.
60 public MSFfile(String inFile, String type) throws IOException
65 public MSFfile(FileParse source) throws IOException
73 public void parse() throws IOException
76 boolean seqFlag = false;
77 String key = new String();
78 Vector headers = new Vector();
79 Hashtable seqhash = new Hashtable();
84 while ((line = nextLine()) != null)
86 StringTokenizer str = new StringTokenizer(line);
88 while (str.hasMoreTokens())
90 String inStr = str.nextToken();
92 // If line has header information add to the headers vector
93 if (inStr.indexOf("Name:") != -1)
95 key = str.nextToken();
96 headers.addElement(key);
99 // if line has // set SeqFlag to 1 so we know sequences are coming
100 if (inStr.indexOf("//") != -1)
105 // Process lines as sequence lines if seqFlag is set
106 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
108 // seqeunce id is the first field
111 StringBuffer tempseq;
113 // Get sequence from hash if it exists
114 if (seqhash.containsKey(key))
116 tempseq = (StringBuffer) seqhash.get(key);
120 tempseq = new StringBuffer();
121 seqhash.put(key, tempseq);
124 // loop through the rest of the words
125 while (str.hasMoreTokens())
127 // append the word to the sequence
128 tempseq.append(str.nextToken());
133 } catch (IOException e)
135 System.err.println("Exception parsing MSFFile " + e);
139 this.noSeqs = headers.size();
141 // Add sequences to the hash
142 for (i = 0; i < headers.size(); i++)
144 if (seqhash.get(headers.elementAt(i)) != null)
146 String head = headers.elementAt(i).toString();
147 String seq = seqhash.get(head).toString();
149 if (maxLength < head.length())
151 maxLength = head.length();
154 // Replace ~ with a sensible gap character
155 seq = seq.replace('~', '-');
157 Sequence newSeq = parseId(head);
159 newSeq.setSequence(seq);
161 seqs.addElement(newSeq);
165 System.err.println("MSFFile Parser: Can't find sequence for "
166 + headers.elementAt(i));
177 * @return DOCUMENT ME!
179 public int checkSum(String seq)
182 String sequence = seq.toUpperCase();
184 for (int i = 0; i < sequence.length(); i++)
189 int value = sequence.charAt(i);
192 check += (i % 57 + 1) * value;
194 } catch (Exception e)
196 System.err.println("Exception during MSF Checksum calculation");
201 return check % 10000;
212 * @return DOCUMENT ME!
214 public String print(SequenceI[] seqs)
217 boolean is_NA = Comparison.isNucleotide(seqs);
219 SequenceI[] s = new SequenceI[seqs.length];
221 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
222 + "_MULTIPLE_ALIGNMENT 1.0");
223 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
230 while ((i < seqs.length) && (seqs[i] != null))
232 // Replace all internal gaps with . and external spaces with ~
233 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
234 .replace('-', '.'), seqs[i].getStart(), seqs[i].getEnd());
236 StringBuffer sb = new StringBuffer();
237 sb.append(s[i].getSequence());
239 for (int ii = 0; ii < sb.length(); ii++)
241 if (sb.charAt(ii) == '.')
243 sb.setCharAt(ii, '~');
251 for (int ii = sb.length() - 1; ii > 0; ii--)
253 if (sb.charAt(ii) == '.')
255 sb.setCharAt(ii, '~');
263 s[i].setSequence(sb.toString());
265 if (s[i].getSequence().length > max)
267 max = s[i].getSequence().length;
273 Format maxLenpad = new Format("%" + (new String("" + max)).length()
275 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
280 int[] checksums = new int[s.length];
283 checksums[i] = checkSum(s[i].getSequenceAsString());
284 bigChecksum += checksums[i];
289 out.append(" MSF: " + s[0].getSequence().length + " Type: "
290 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
296 String[] nameBlock = new String[s.length];
297 String[] idBlock = new String[s.length];
300 while ((i < s.length) && (s[i] != null))
303 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
305 idBlock[i] = new String("Len: "
306 + maxLenpad.formLong(s[i].getSequence().length) + " Check: "
307 + maxChkpad.formLong(checksums[i]) + " Weight: 1.00" + newline);
309 if (s[i].getName().length() > maxid)
311 maxid = s[i].getName().length();
314 if (nameBlock[i].length() > maxNB)
316 maxNB = nameBlock[i].length();
332 Format nbFormat = new Format("%-" + maxNB + "s");
334 for (i = 0; (i < s.length) && (s[i] != null); i++)
336 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
347 int nochunks = (max / len) + 1;
349 if ((max % len) == 0)
354 for (i = 0; i < nochunks; i++)
358 while ((j < s.length) && (s[j] != null))
360 String name = printId(s[j]);
362 out.append(new Format("%-" + maxid + "s").form(name + " "));
364 for (int k = 0; k < 5; k++)
366 int start = (i * 50) + (k * 10);
367 int end = start + 10;
369 if ((end < s[j].getSequence().length)
370 && (start < s[j].getSequence().length))
372 out.append(s[j].getSequence(start, end));
385 if (start < s[j].getSequence().length)
387 out.append(s[j].getSequenceAsString().substring(start));
406 return out.toString();
412 * @return DOCUMENT ME!
414 public String print()
416 return print(getSeqsAsArray());