2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.datamodel.*;
23 import jalview.util.*;
36 public class MSFfile extends AlignFile
41 * Creates a new MSFfile object.
49 * Creates a new MSFfile object.
51 * @param inFile DOCUMENT ME!
52 * @param type DOCUMENT ME!
54 * @throws IOException DOCUMENT ME!
56 public MSFfile(String inFile, String type) throws IOException
64 public void parse() throws IOException
67 boolean seqFlag = false;
68 String key = new String();
69 Vector headers = new Vector();
70 Hashtable seqhash = new Hashtable();
75 while ((line = nextLine()) != null)
77 StringTokenizer str = new StringTokenizer(line);
79 while (str.hasMoreTokens())
81 String inStr = str.nextToken();
83 //If line has header information add to the headers vector
84 if (inStr.indexOf("Name:") != -1)
86 key = str.nextToken();
87 headers.addElement(key);
90 //if line has // set SeqFlag to 1 so we know sequences are coming
91 if (inStr.indexOf("//") != -1)
96 //Process lines as sequence lines if seqFlag is set
97 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
99 //seqeunce id is the first field
102 StringBuffer tempseq;
104 //Get sequence from hash if it exists
105 if (seqhash.containsKey(key))
107 tempseq = (StringBuffer) seqhash.get(key);
111 tempseq = new StringBuffer();
112 seqhash.put(key, tempseq);
115 //loop through the rest of the words
116 while (str.hasMoreTokens())
118 //append the word to the sequence
119 tempseq.append(str.nextToken());
125 catch (IOException e)
127 System.err.println("Exception parsing MSFFile " + e);
131 this.noSeqs = headers.size();
133 //Add sequences to the hash
134 for (i = 0; i < headers.size(); i++)
136 if (seqhash.get(headers.elementAt(i)) != null)
138 String head = headers.elementAt(i).toString();
139 String seq = seqhash.get(head).toString();
141 if (maxLength < head.length())
143 maxLength = head.length();
146 // Replace ~ with a sensible gap character
147 seq = seq.replace('~', '-');
148 if (!isValidProteinSequence(seq))
150 throw new IOException(AppletFormatAdapter.
153 + " : " + invalidCharacter);
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));
174 * @param seq DOCUMENT ME!
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;
196 System.err.println("Exception during MSF Checksum calculation");
201 return check % 10000;
208 * @param s DOCUMENT ME!
209 * @param is_NA DOCUMENT ME!
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\n\n"); // 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].getSequence().replace('-', '.'));
232 StringBuffer sb = new StringBuffer(s[i].getSequence());
233 for (int ii = 0; ii < sb.length(); ii++)
235 if (sb.charAt(ii) == '.')
237 sb.setCharAt(ii, '~');
243 for (int ii = sb.length() - 1; ii > 0; ii--)
245 if (sb.charAt(ii) == '.')
247 sb.setCharAt(ii,'~');
253 s[i].setSequence(sb.toString());
255 if (s[i].getSequence().length() > max)
257 max = s[i].getSequence().length();
263 Format maxLenpad = new Format("%" + (new String("" + max)).length() +
265 Format maxChkpad = new Format("%" + (new String("1" + max)).length() +
270 int [] checksums = new int[s.length];
271 while ( i < s.length )
273 checksums[i] = checkSum(s[i].getSequence());
274 bigChecksum += checksums[i];
279 out.append(" MSF: " + s[0].getSequence().length() + " Type: " +
280 (is_NA ? "N" : "P") + " Check: " + (bigChecksum%10000) + " ..\n\n\n");
282 String[] nameBlock = new String[s.length];
283 String[] idBlock = new String[s.length];
286 while ((i < s.length) && (s[i] != null))
289 nameBlock[i] = new String(" Name: " + printId(s[i])+" ");
291 idBlock[i] = new String("Len: " +
292 maxLenpad.form(s[i].getSequence().length()) + " Check: " +
293 maxChkpad.form(checksums[i]) + " Weight: 1.00\n");
295 if (s[i].getName().length() > maxid)
297 maxid = s[i].getName().length();
300 if (nameBlock[i].length() > maxNB)
302 maxNB = nameBlock[i].length();
318 Format nbFormat = new Format("%-" + maxNB + "s");
320 for (i = 0; (i < s.length) && (s[i] != null); i++)
322 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
326 out.append("\n\n//\n\n");
330 int nochunks = (max / len) + 1;
332 if ((max % len) == 0)
337 for (i = 0; i < nochunks; i++)
341 while ((j < s.length) && (s[j] != null))
343 String name = printId( s[j] );
345 out.append(new Format("%-" + maxid + "s").form(name+" "));
348 for (int k = 0; k < 5; k++)
350 int start = (i * 50) + (k * 10);
351 int end = start + 10;
353 if ((end < s[j].getSequence().length()) &&
354 (start < s[j].getSequence().length()))
356 out.append(s[j].getSequence().substring(start, end));
369 if (start < s[j].getSequence().length())
371 out.append(s[j].getSequence().substring(start));
390 return out.toString();
396 * @return DOCUMENT ME!
398 public String print()
400 return print(getSeqsAsArray());