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 java.util.Locale;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Comparison;
28 import jalview.util.Format;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Hashtable;
33 import java.util.List;
34 import java.util.StringTokenizer;
42 public class MSFfile extends AlignFile
46 * Creates a new MSFfile object.
53 * Creates a new MSFfile object.
63 public MSFfile(String inFile, DataSourceType type) throws IOException
68 public MSFfile(FileParse source) throws IOException
74 * Read and parse MSF sequence data
77 public void parse() throws IOException
79 boolean seqFlag = false;
80 List<String> headers = new ArrayList<String>();
81 Hashtable<String, StringBuilder> seqhash = new Hashtable<String, StringBuilder>();
86 while ((line = nextLine()) != null)
88 StringTokenizer str = new StringTokenizer(line);
91 while (str.hasMoreTokens())
93 String inStr = str.nextToken();
95 // If line has header information add to the headers vector
96 if (inStr.indexOf("Name:") != -1)
98 key = str.nextToken();
102 // if line has // set SeqFlag so we know sequences are coming
103 if (inStr.indexOf("//") != -1)
108 // Process lines as sequence lines if seqFlag is set
109 if ((inStr.indexOf("//") == -1) && seqFlag)
111 // sequence id is the first field
114 StringBuilder tempseq;
116 // Get sequence from hash if it exists
117 if (seqhash.containsKey(key))
119 tempseq = seqhash.get(key);
123 tempseq = new StringBuilder(64);
124 seqhash.put(key, tempseq);
127 // loop through the rest of the words
128 while (str.hasMoreTokens())
130 // append the word to the sequence
131 String sequenceBlock = str.nextToken();
132 tempseq.append(sequenceBlock);
137 } catch (IOException e)
139 System.err.println("Exception parsing MSFFile " + e);
143 this.noSeqs = headers.size();
145 // Add sequences to the hash
146 for (int i = 0; i < headers.size(); i++)
148 if (seqhash.get(headers.get(i)) != null)
150 String head = headers.get(i);
151 String seq = seqhash.get(head).toString();
153 if (maxLength < head.length())
155 maxLength = head.length();
159 * replace ~ (leading/trailing positions) with the gap character;
160 * use '.' as this is the internal gap character required by MSF
162 seq = seq.replace('~', '.');
164 Sequence newSeq = parseId(head);
166 newSeq.setSequence(seq);
168 seqs.addElement(newSeq);
172 System.err.println("MSFFile Parser: Can't find sequence for "
184 * @return DOCUMENT ME!
186 public int checkSum(String seq)
189 String sequence = seq.toUpperCase(Locale.ROOT);
191 for (int i = 0; i < sequence.length(); i++)
196 int value = sequence.charAt(i);
199 check += (i % 57 + 1) * value;
201 } catch (Exception e)
203 System.err.println("Exception during MSF Checksum calculation");
208 return check % 10000;
219 * @return DOCUMENT ME!
222 public String print(SequenceI[] sqs, boolean jvSuffix)
225 boolean is_NA = Comparison.isNucleotide(sqs);
227 SequenceI[] s = new SequenceI[sqs.length];
229 StringBuilder out = new StringBuilder(256);
230 out.append("!!").append(is_NA ? "NA" : "AA")
231 .append("_MULTIPLE_ALIGNMENT 1.0");
232 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
239 while ((i < sqs.length) && (sqs[i] != null))
242 * modify to MSF format: uses '.' for internal gaps,
243 * and '~' for leading or trailing gaps
245 String seqString = sqs[i].getSequenceAsString().replace('-', '.');
247 StringBuilder sb = new StringBuilder(seqString);
249 for (int ii = 0; ii < sb.length(); ii++)
251 if (sb.charAt(ii) == '.')
253 sb.setCharAt(ii, '~');
261 for (int ii = sb.length() - 1; ii > 0; ii--)
263 if (sb.charAt(ii) == '.')
265 sb.setCharAt(ii, '~');
272 s[i] = new Sequence(sqs[i].getName(), sb.toString(),
273 sqs[i].getStart(), sqs[i].getEnd());
275 if (sb.length() > max)
283 Format maxLenpad = new Format(
284 "%" + (new String("" + max)).length() + "d");
285 Format maxChkpad = new Format(
286 "%" + (new String("1" + max)).length() + "d");
290 int[] checksums = new int[s.length];
293 checksums[i] = checkSum(s[i].getSequenceAsString());
294 bigChecksum += checksums[i];
299 out.append(" MSF: " + s[0].getLength() + " Type: "
300 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
306 String[] nameBlock = new String[s.length];
307 String[] idBlock = new String[s.length];
310 while ((i < s.length) && (s[i] != null))
313 nameBlock[i] = new String(" Name: " + printId(s[i], jvSuffix) + " ");
315 idBlock[i] = new String("Len: " + maxLenpad.form(s[i].getLength())
316 + " Check: " + maxChkpad.form(checksums[i])
317 + " Weight: 1.00" + newline);
319 if (s[i].getName().length() > maxid)
321 maxid = s[i].getName().length();
324 if (nameBlock[i].length() > maxNB)
326 maxNB = nameBlock[i].length();
342 Format nbFormat = new Format("%-" + maxNB + "s");
344 for (i = 0; (i < s.length) && (s[i] != null); i++)
346 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
357 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
359 for (i = 0; i < nochunks; i++)
363 while ((j < s.length) && (s[j] != null))
365 String name = printId(s[j], jvSuffix);
367 out.append(new Format("%-" + maxid + "s").form(name + " "));
369 for (int k = 0; k < 5; k++)
371 int start = (i * 50) + (k * 10);
372 int end = start + 10;
374 int length = s[j].getLength();
375 if ((end < length) && (start < length))
377 out.append(s[j].getSequence(start, end));
392 out.append(s[j].getSequenceAsString().substring(start));
411 return out.toString();