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
48 * Creates a new MSFfile object.
\r
50 * @param inStr DOCUMENT ME!
\r
52 public MSFfile(String inStr)
\r
58 * Creates a new MSFfile object.
\r
60 * @param inFile DOCUMENT ME!
\r
61 * @param type DOCUMENT ME!
\r
63 * @throws IOException DOCUMENT ME!
\r
65 public MSFfile(String inFile, String type) throws IOException
\r
67 super(inFile, type);
\r
73 public void parse() throws IOException
\r
76 boolean seqFlag = false;
\r
77 String key = new String();
\r
78 Vector headers = new Vector();
\r
79 Hashtable seqhash = new Hashtable();
\r
84 while ((line = nextLine()) != null)
\r
86 StringTokenizer str = new StringTokenizer(line);
\r
88 while (str.hasMoreTokens())
\r
90 String inStr = str.nextToken();
\r
92 //If line has header information add to the headers vector
\r
93 if (inStr.indexOf("Name:") != -1)
\r
95 key = str.nextToken();
\r
96 headers.addElement(key);
\r
99 //if line has // set SeqFlag to 1 so we know sequences are coming
\r
100 if (inStr.indexOf("//") != -1)
\r
105 //Process lines as sequence lines if seqFlag is set
\r
106 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
\r
108 //seqeunce id is the first field
\r
111 StringBuffer tempseq;
\r
113 //Get sequence from hash if it exists
\r
114 if (seqhash.containsKey(key))
\r
116 tempseq = (StringBuffer) seqhash.get(key);
\r
120 tempseq = new StringBuffer();
\r
121 seqhash.put(key, tempseq);
\r
124 //loop through the rest of the words
\r
125 while (str.hasMoreTokens())
\r
127 //append the word to the sequence
\r
128 tempseq.append(str.nextToken());
\r
134 catch (IOException e)
\r
136 System.err.println("Exception parsing MSFFile " + e);
\r
137 e.printStackTrace();
\r
140 this.noSeqs = headers.size();
\r
142 //Add sequences to the hash
\r
143 for (i = 0; i < headers.size(); i++)
\r
145 if (seqhash.get(headers.elementAt(i)) != null)
\r
147 String head = headers.elementAt(i).toString();
\r
148 String seq = seqhash.get(head).toString();
\r
150 if (maxLength < head.length())
\r
152 maxLength = head.length();
\r
155 // Replace ~ with a sensible gap character
\r
156 seq = seq.replace('~', '-');
\r
157 if (!isValidProteinSequence(seq))
\r
159 throw new IOException(AppletFormatAdapter.
\r
162 + " : " + invalidCharacter);
\r
166 Sequence newSeq = parseId(head);
\r
168 newSeq.setSequence(seq);
\r
170 seqs.addElement(newSeq);
\r
174 System.err.println("MSFFile Parser: Can't find sequence for " +
\r
175 headers.elementAt(i));
\r
183 * @param seq DOCUMENT ME!
\r
185 * @return DOCUMENT ME!
\r
187 public int checkSum(String seq)
\r
190 String sequence = seq.toUpperCase();
\r
192 for (int i = 0; i < sequence.length(); i++)
\r
197 int value = sequence.charAt(i);
\r
200 check += (i % 57 +1) * value;
\r
203 catch (Exception e)
\r
205 System.err.println("Exception during MSF Checksum calculation");
\r
206 e.printStackTrace();
\r
210 return check % 10000;
\r
217 * @param s DOCUMENT ME!
\r
218 * @param is_NA DOCUMENT ME!
\r
220 * @return DOCUMENT ME!
\r
222 public String print(SequenceI[] seqs)
\r
225 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
\r
227 SequenceI [] s = new SequenceI[seqs.length];
\r
229 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA") +
\r
230 "_MULTIPLE_ALIGNMENT 1.0\n\n"); // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
\r
236 while ((i < seqs.length) && (seqs[i] != null))
\r
238 // Replace all internal gaps with . and external spaces with ~
\r
239 s[i] =new Sequence(seqs[i].getName(),seqs[i].getSequence().replace('-', '.'));
\r
241 StringBuffer sb = new StringBuffer(s[i].getSequence());
\r
242 for (int ii = 0; ii < sb.length(); ii++)
\r
244 if (sb.charAt(ii) == '.')
\r
246 sb.setCharAt(ii, '~');
\r
252 for (int ii = sb.length() - 1; ii > 0; ii--)
\r
254 if (sb.charAt(ii) == '.')
\r
256 sb.setCharAt(ii,'~');
\r
262 s[i].setSequence(sb.toString());
\r
264 if (s[i].getSequence().length() > max)
\r
266 max = s[i].getSequence().length();
\r
272 Format maxLenpad = new Format("%" + (new String("" + max)).length() +
\r
274 Format maxChkpad = new Format("%" + (new String("1" + max)).length() +
\r
278 int bigChecksum = 0;
\r
279 int [] checksums = new int[s.length];
\r
280 while ( i < s.length )
\r
282 checksums[i] = checkSum(s[i].getSequence());
\r
283 bigChecksum += checksums[i];
\r
288 out.append(" MSF: " + s[0].getSequence().length() + " Type: " +
\r
289 (is_NA ? "N" : "P") + " Check: " + (bigChecksum%10000) + " ..\n\n\n");
\r
291 String[] nameBlock = new String[s.length];
\r
292 String[] idBlock = new String[s.length];
\r
295 while ((i < s.length) && (s[i] != null))
\r
298 nameBlock[i] = new String(" Name: " + printId(s[i])+" ");
\r
300 idBlock[i] = new String("Len: " +
\r
301 maxLenpad.form(s[i].getSequence().length()) + " Check: " +
\r
302 maxChkpad.form(checksums[i]) + " Weight: 1.00\n");
\r
304 if (s[i].getName().length() > maxid)
\r
306 maxid = s[i].getName().length();
\r
309 if (nameBlock[i].length() > maxNB)
\r
311 maxNB = nameBlock[i].length();
\r
327 Format nbFormat = new Format("%-" + maxNB + "s");
\r
329 for (i = 0; (i < s.length) && (s[i] != null); i++)
\r
331 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
\r
335 out.append("\n\n//\n\n");
\r
339 int nochunks = (max / len) + 1;
\r
341 if ((max % len) == 0)
\r
346 for (i = 0; i < nochunks; i++)
\r
350 while ((j < s.length) && (s[j] != null))
\r
352 String name = printId( s[j] );
\r
354 out.append(new Format("%-" + maxid + "s").form(name+" "));
\r
357 for (int k = 0; k < 5; k++)
\r
359 int start = (i * 50) + (k * 10);
\r
360 int end = start + 10;
\r
362 if ((end < s[j].getSequence().length()) &&
\r
363 (start < s[j].getSequence().length()))
\r
365 out.append(s[j].getSequence().substring(start, end));
\r
378 if (start < s[j].getSequence().length())
\r
380 out.append(s[j].getSequence().substring(start));
\r
399 return out.toString();
\r
405 * @return DOCUMENT ME!
\r
407 public String print()
\r
409 return print(getSeqsAsArray());
\r