2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import jalview.datamodel.*;
\r
23 import jalview.util.*;
\r
34 * @version $Revision$
\r
36 public class MSFfile extends AlignFile
\r
41 * Creates a new MSFfile object.
\r
49 * Creates a new MSFfile object.
\r
51 * @param inFile DOCUMENT ME!
\r
52 * @param type DOCUMENT ME!
\r
54 * @throws IOException DOCUMENT ME!
\r
56 public MSFfile(String inFile, String type) throws IOException
\r
58 super(inFile, type);
\r
64 public void parse() throws IOException
\r
67 boolean seqFlag = false;
\r
68 String key = new String();
\r
69 Vector headers = new Vector();
\r
70 Hashtable seqhash = new Hashtable();
\r
75 while ((line = nextLine()) != null)
\r
77 StringTokenizer str = new StringTokenizer(line);
\r
79 while (str.hasMoreTokens())
\r
81 String inStr = str.nextToken();
\r
83 //If line has header information add to the headers vector
\r
84 if (inStr.indexOf("Name:") != -1)
\r
86 key = str.nextToken();
\r
87 headers.addElement(key);
\r
90 //if line has // set SeqFlag to 1 so we know sequences are coming
\r
91 if (inStr.indexOf("//") != -1)
\r
96 //Process lines as sequence lines if seqFlag is set
\r
97 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
\r
99 //seqeunce id is the first field
\r
102 StringBuffer tempseq;
\r
104 //Get sequence from hash if it exists
\r
105 if (seqhash.containsKey(key))
\r
107 tempseq = (StringBuffer) seqhash.get(key);
\r
111 tempseq = new StringBuffer();
\r
112 seqhash.put(key, tempseq);
\r
115 //loop through the rest of the words
\r
116 while (str.hasMoreTokens())
\r
118 //append the word to the sequence
\r
119 tempseq.append(str.nextToken());
\r
125 catch (IOException e)
\r
127 System.err.println("Exception parsing MSFFile " + e);
\r
128 e.printStackTrace();
\r
131 this.noSeqs = headers.size();
\r
133 //Add sequences to the hash
\r
134 for (i = 0; i < headers.size(); i++)
\r
136 if (seqhash.get(headers.elementAt(i)) != null)
\r
138 String head = headers.elementAt(i).toString();
\r
139 String seq = seqhash.get(head).toString();
\r
141 if (maxLength < head.length())
\r
143 maxLength = head.length();
\r
146 // Replace ~ with a sensible gap character
\r
147 seq = seq.replace('~', '-');
\r
148 if (!isValidProteinSequence(seq.toCharArray()))
\r
150 throw new IOException(AppletFormatAdapter.
\r
153 + " : " + invalidCharacter);
\r
157 Sequence newSeq = parseId(head);
\r
159 newSeq.setSequence(seq);
\r
161 seqs.addElement(newSeq);
\r
165 System.err.println("MSFFile Parser: Can't find sequence for " +
\r
166 headers.elementAt(i));
\r
174 * @param seq DOCUMENT ME!
\r
176 * @return DOCUMENT ME!
\r
178 public int checkSum(String seq)
\r
181 String sequence = seq.toUpperCase();
\r
183 for (int i = 0; i < sequence.length(); i++)
\r
188 int value = sequence.charAt(i);
\r
191 check += (i % 57 +1) * value;
\r
194 catch (Exception e)
\r
196 System.err.println("Exception during MSF Checksum calculation");
\r
197 e.printStackTrace();
\r
201 return check % 10000;
\r
208 * @param s DOCUMENT ME!
\r
209 * @param is_NA DOCUMENT ME!
\r
211 * @return DOCUMENT ME!
\r
213 public String print(SequenceI[] seqs)
\r
216 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
\r
218 SequenceI [] s = new SequenceI[seqs.length];
\r
220 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA") +
\r
221 "_MULTIPLE_ALIGNMENT 1.0\n\n"); // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
\r
227 while ((i < seqs.length) && (seqs[i] != null))
\r
229 // Replace all internal gaps with . and external spaces with ~
\r
230 s[i] =new Sequence(seqs[i].getName(),seqs[i].getSequenceAsString().replace('-', '.'));
\r
232 StringBuffer sb = new StringBuffer();
\r
233 sb.append(s[i].getSequence());
\r
235 for (int ii = 0; ii < sb.length(); ii++)
\r
237 if (sb.charAt(ii) == '.')
\r
239 sb.setCharAt(ii, '~');
\r
245 for (int ii = sb.length() - 1; ii > 0; ii--)
\r
247 if (sb.charAt(ii) == '.')
\r
249 sb.setCharAt(ii,'~');
\r
255 s[i].setSequence(sb.toString());
\r
257 if (s[i].getSequence().length > max)
\r
259 max = s[i].getSequence().length;
\r
265 Format maxLenpad = new Format("%" + (new String("" + max)).length() +
\r
267 Format maxChkpad = new Format("%" + (new String("1" + max)).length() +
\r
271 int bigChecksum = 0;
\r
272 int [] checksums = new int[s.length];
\r
273 while ( i < s.length )
\r
275 checksums[i] = checkSum(s[i].getSequenceAsString());
\r
276 bigChecksum += checksums[i];
\r
281 out.append(" MSF: " + s[0].getSequence().length + " Type: " +
\r
282 (is_NA ? "N" : "P") + " Check: " + (bigChecksum%10000) + " ..\n\n\n");
\r
284 String[] nameBlock = new String[s.length];
\r
285 String[] idBlock = new String[s.length];
\r
288 while ((i < s.length) && (s[i] != null))
\r
291 nameBlock[i] = new String(" Name: " + printId(s[i])+" ");
\r
293 idBlock[i] = new String("Len: " +
\r
294 maxLenpad.form(s[i].getSequence().length) + " Check: " +
\r
295 maxChkpad.form(checksums[i]) + " Weight: 1.00\n");
\r
297 if (s[i].getName().length() > maxid)
\r
299 maxid = s[i].getName().length();
\r
302 if (nameBlock[i].length() > maxNB)
\r
304 maxNB = nameBlock[i].length();
\r
320 Format nbFormat = new Format("%-" + maxNB + "s");
\r
322 for (i = 0; (i < s.length) && (s[i] != null); i++)
\r
324 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
\r
328 out.append("\n\n//\n\n");
\r
332 int nochunks = (max / len) + 1;
\r
334 if ((max % len) == 0)
\r
339 for (i = 0; i < nochunks; i++)
\r
343 while ((j < s.length) && (s[j] != null))
\r
345 String name = printId( s[j] );
\r
347 out.append(new Format("%-" + maxid + "s").form(name+" "));
\r
350 for (int k = 0; k < 5; k++)
\r
352 int start = (i * 50) + (k * 10);
\r
353 int end = start + 10;
\r
355 if ((end < s[j].getSequence().length) &&
\r
356 (start < s[j].getSequence().length))
\r
358 out.append(s[j].getSequence(start, end));
\r
371 if (start < s[j].getSequence().length)
\r
373 out.append(s[j].getSequenceAsString().substring(start));
\r
392 return out.toString();
\r
398 * @return DOCUMENT ME!
\r
400 public String print()
\r
402 return print(getSeqsAsArray());
\r