2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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))
\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].getSequence().replace('-', '.'));
\r
232 StringBuffer sb = new StringBuffer(s[i].getSequence());
\r
233 for (int ii = 0; ii < sb.length(); ii++)
\r
235 if (sb.charAt(ii) == '.')
\r
237 sb.setCharAt(ii, '~');
\r
243 for (int ii = sb.length() - 1; ii > 0; ii--)
\r
245 if (sb.charAt(ii) == '.')
\r
247 sb.setCharAt(ii,'~');
\r
253 s[i].setSequence(sb.toString());
\r
255 if (s[i].getSequence().length() > max)
\r
257 max = s[i].getSequence().length();
\r
263 Format maxLenpad = new Format("%" + (new String("" + max)).length() +
\r
265 Format maxChkpad = new Format("%" + (new String("1" + max)).length() +
\r
269 int bigChecksum = 0;
\r
270 int [] checksums = new int[s.length];
\r
271 while ( i < s.length )
\r
273 checksums[i] = checkSum(s[i].getSequence());
\r
274 bigChecksum += checksums[i];
\r
279 out.append(" MSF: " + s[0].getSequence().length() + " Type: " +
\r
280 (is_NA ? "N" : "P") + " Check: " + (bigChecksum%10000) + " ..\n\n\n");
\r
282 String[] nameBlock = new String[s.length];
\r
283 String[] idBlock = new String[s.length];
\r
286 while ((i < s.length) && (s[i] != null))
\r
289 nameBlock[i] = new String(" Name: " + printId(s[i])+" ");
\r
291 idBlock[i] = new String("Len: " +
\r
292 maxLenpad.form(s[i].getSequence().length()) + " Check: " +
\r
293 maxChkpad.form(checksums[i]) + " Weight: 1.00\n");
\r
295 if (s[i].getName().length() > maxid)
\r
297 maxid = s[i].getName().length();
\r
300 if (nameBlock[i].length() > maxNB)
\r
302 maxNB = nameBlock[i].length();
\r
318 Format nbFormat = new Format("%-" + maxNB + "s");
\r
320 for (i = 0; (i < s.length) && (s[i] != null); i++)
\r
322 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
\r
326 out.append("\n\n//\n\n");
\r
330 int nochunks = (max / len) + 1;
\r
332 if ((max % len) == 0)
\r
337 for (i = 0; i < nochunks; i++)
\r
341 while ((j < s.length) && (s[j] != null))
\r
343 String name = printId( s[j] );
\r
345 out.append(new Format("%-" + maxid + "s").form(name+" "));
\r
348 for (int k = 0; k < 5; k++)
\r
350 int start = (i * 50) + (k * 10);
\r
351 int end = start + 10;
\r
353 if ((end < s[j].getSequence().length()) &&
\r
354 (start < s[j].getSequence().length()))
\r
356 out.append(s[j].getSequence().substring(start, end));
\r
369 if (start < s[j].getSequence().length())
\r
371 out.append(s[j].getSequence().substring(start));
\r
390 return out.toString();
\r
396 * @return DOCUMENT ME!
\r
398 public String print()
\r
400 return print(getSeqsAsArray());
\r