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, DataSourceType sourceType)
63 super(inFile, sourceType);
66 public MSFfile(FileParse source) throws IOException
75 public void parse() throws IOException
78 boolean seqFlag = false;
79 String key = new String();
80 Vector headers = new Vector();
81 Hashtable seqhash = new Hashtable();
86 while ((line = nextLine()) != null)
88 StringTokenizer str = new StringTokenizer(line);
90 while (str.hasMoreTokens())
92 String inStr = str.nextToken();
94 // If line has header information add to the headers vector
95 if (inStr.indexOf("Name:") != -1)
97 key = str.nextToken();
98 headers.addElement(key);
101 // if line has // set SeqFlag to 1 so we know sequences are coming
102 if (inStr.indexOf("//") != -1)
107 // Process lines as sequence lines if seqFlag is set
108 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
110 // seqeunce id is the first field
113 StringBuffer tempseq;
115 // Get sequence from hash if it exists
116 if (seqhash.containsKey(key))
118 tempseq = (StringBuffer) seqhash.get(key);
122 tempseq = new StringBuffer();
123 seqhash.put(key, tempseq);
126 // loop through the rest of the words
127 while (str.hasMoreTokens())
129 // append the word to the sequence
130 tempseq.append(str.nextToken());
135 } catch (IOException e)
137 System.err.println("Exception parsing MSFFile " + e);
141 this.noSeqs = headers.size();
143 // Add sequences to the hash
144 for (i = 0; i < headers.size(); i++)
146 if (seqhash.get(headers.elementAt(i)) != null)
148 String head = headers.elementAt(i).toString();
149 String seq = seqhash.get(head).toString();
151 if (maxLength < head.length())
153 maxLength = head.length();
156 // Replace ~ with a sensible gap character
157 seq = seq.replace('~', '-');
159 Sequence newSeq = parseId(head);
161 newSeq.setSequence(seq);
163 seqs.addElement(newSeq);
167 System.err.println("MSFFile Parser: Can't find sequence for "
168 + headers.elementAt(i));
179 * @return DOCUMENT ME!
181 public int checkSum(String seq)
184 String sequence = seq.toUpperCase();
186 for (int i = 0; i < sequence.length(); i++)
191 int value = sequence.charAt(i);
194 check += (i % 57 + 1) * value;
196 } catch (Exception e)
198 System.err.println("Exception during MSF Checksum calculation");
203 return check % 10000;
207 public String print(SequenceI[] sqs, boolean jvsuffix)
210 boolean is_NA = Comparison.isNucleotide(sqs);
212 SequenceI[] s = new SequenceI[sqs.length];
214 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
215 + "_MULTIPLE_ALIGNMENT 1.0");
216 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
223 while ((i < sqs.length) && (sqs[i] != null))
225 // Replace all internal gaps with . and external spaces with ~
226 s[i] = new Sequence(sqs[i].getName(), sqs[i].getSequenceAsString()
227 .replace('-', '.'), sqs[i].getStart(), sqs[i].getEnd());
229 StringBuffer sb = new StringBuffer();
230 sb.append(s[i].getSequence());
232 for (int ii = 0; ii < sb.length(); ii++)
234 if (sb.charAt(ii) == '.')
236 sb.setCharAt(ii, '~');
244 for (int ii = sb.length() - 1; ii > 0; ii--)
246 if (sb.charAt(ii) == '.')
248 sb.setCharAt(ii, '~');
256 s[i].setSequence(sb.toString());
258 if (s[i].getSequence().length > max)
260 max = s[i].getSequence().length;
266 Format maxLenpad = new Format("%" + (new String("" + max)).length()
268 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
273 int[] checksums = new int[s.length];
276 checksums[i] = checkSum(s[i].getSequenceAsString());
277 bigChecksum += checksums[i];
282 out.append(" MSF: " + s[0].getSequence().length + " Type: "
283 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
289 String[] nameBlock = new String[s.length];
290 String[] idBlock = new String[s.length];
293 while ((i < s.length) && (s[i] != null))
296 nameBlock[i] = new String(" Name: " + printId(s[i], jvsuffix) + " ");
298 idBlock[i] = new String("Len: "
299 + maxLenpad.form(s[i].getSequence().length) + " Check: "
300 + maxChkpad.form(checksums[i]) + " Weight: 1.00" + newline);
302 if (s[i].getName().length() > maxid)
304 maxid = s[i].getName().length();
307 if (nameBlock[i].length() > maxNB)
309 maxNB = nameBlock[i].length();
325 Format nbFormat = new Format("%-" + maxNB + "s");
327 for (i = 0; (i < s.length) && (s[i] != null); i++)
329 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
340 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
342 for (i = 0; i < nochunks; i++)
346 while ((j < s.length) && (s[j] != null))
348 String name = printId(s[j], jvsuffix);
350 out.append(new Format("%-" + maxid + "s").form(name + " "));
352 for (int k = 0; k < 5; k++)
354 int start = (i * 50) + (k * 10);
355 int end = start + 10;
357 if ((end < s[j].getSequence().length)
358 && (start < s[j].getSequence().length))
360 out.append(s[j].getSequence(start, end));
373 if (start < s[j].getSequence().length)
375 out.append(s[j].getSequenceAsString().substring(start));
394 return out.toString();