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.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
32 import java.util.StringTokenizer;
40 public class MSFfile extends AlignFile
44 * Creates a new MSFfile object.
51 * Creates a new MSFfile object.
61 public MSFfile(String inFile, DataSourceType type) throws IOException
66 public MSFfile(FileParse source) throws IOException
72 * Read and parse MSF sequence data
75 public void parse() throws IOException
77 boolean seqFlag = false;
78 List<String> headers = new ArrayList<String>();
79 Hashtable<String, StringBuilder> seqhash = new Hashtable<String, StringBuilder>();
84 while ((line = nextLine()) != null)
86 StringTokenizer str = new StringTokenizer(line);
89 while (str.hasMoreTokens())
91 String inStr = str.nextToken();
93 // If line has header information add to the headers vector
94 if (inStr.indexOf("Name:") != -1)
96 key = str.nextToken();
100 // if line has // set SeqFlag so we know sequences are coming
101 if (inStr.indexOf("//") != -1)
106 // Process lines as sequence lines if seqFlag is set
107 if ((inStr.indexOf("//") == -1) && seqFlag)
109 // sequence id is the first field
112 StringBuilder tempseq;
114 // Get sequence from hash if it exists
115 if (seqhash.containsKey(key))
117 tempseq = seqhash.get(key);
121 tempseq = new StringBuilder(64);
122 seqhash.put(key, tempseq);
125 // loop through the rest of the words
126 while (str.hasMoreTokens())
128 // append the word to the sequence
129 String sequenceBlock = str.nextToken();
130 tempseq.append(sequenceBlock);
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 (int i = 0; i < headers.size(); i++)
146 if (seqhash.get(headers.get(i)) != null)
148 String head = headers.get(i);
149 String seq = seqhash.get(head).toString();
151 if (maxLength < head.length())
153 maxLength = head.length();
157 * replace ~ (leading/trailing positions) with the gap character;
158 * use '.' as this is the internal gap character required by MSF
160 seq = seq.replace('~', '.');
162 Sequence newSeq = parseId(head);
164 newSeq.setSequence(seq);
166 seqs.addElement(newSeq);
170 System.err.println("MSFFile Parser: Can't find sequence for "
182 * @return DOCUMENT ME!
184 public int checkSum(String seq)
187 String sequence = seq.toUpperCase();
189 for (int i = 0; i < sequence.length(); i++)
194 int value = sequence.charAt(i);
197 check += (i % 57 + 1) * value;
199 } catch (Exception e)
201 System.err.println("Exception during MSF Checksum calculation");
206 return check % 10000;
217 * @return DOCUMENT ME!
220 public String print(SequenceI[] sqs, boolean jvSuffix)
223 boolean is_NA = Comparison.isNucleotide(sqs);
225 SequenceI[] s = new SequenceI[sqs.length];
227 StringBuilder out = new StringBuilder(256);
228 out.append("!!").append(is_NA ? "NA" : "AA")
229 .append("_MULTIPLE_ALIGNMENT 1.0");
230 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
237 while ((i < sqs.length) && (sqs[i] != null))
240 * modify to MSF format: uses '.' for internal gaps,
241 * and '~' for leading or trailing gaps
243 String seqString = sqs[i].getSequenceAsString().replace('-', '.');
245 StringBuilder sb = new StringBuilder(seqString);
247 for (int ii = 0; ii < sb.length(); ii++)
249 if (sb.charAt(ii) == '.')
251 sb.setCharAt(ii, '~');
259 for (int ii = sb.length() - 1; ii > 0; ii--)
261 if (sb.charAt(ii) == '.')
263 sb.setCharAt(ii, '~');
270 s[i] = new Sequence(sqs[i].getName(), sb.toString(),
271 sqs[i].getStart(), sqs[i].getEnd());
273 if (sb.length() > max)
281 Format maxLenpad = new Format(
282 "%" + (new String("" + max)).length() + "d");
283 Format maxChkpad = new Format(
284 "%" + (new String("1" + max)).length() + "d");
288 int[] checksums = new int[s.length];
291 checksums[i] = checkSum(s[i].getSequenceAsString());
292 bigChecksum += checksums[i];
297 out.append(" MSF: " + s[0].getLength() + " Type: "
298 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
304 String[] nameBlock = new String[s.length];
305 String[] idBlock = new String[s.length];
308 while ((i < s.length) && (s[i] != null))
311 nameBlock[i] = new String(" Name: " + printId(s[i], jvSuffix) + " ");
313 idBlock[i] = new String("Len: " + maxLenpad.form(s[i].getLength())
314 + " Check: " + maxChkpad.form(checksums[i])
315 + " Weight: 1.00" + newline);
317 if (s[i].getName().length() > maxid)
319 maxid = s[i].getName().length();
322 if (nameBlock[i].length() > maxNB)
324 maxNB = nameBlock[i].length();
340 Format nbFormat = new Format("%-" + maxNB + "s");
342 for (i = 0; (i < s.length) && (s[i] != null); i++)
344 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
355 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
357 for (i = 0; i < nochunks; i++)
361 while ((j < s.length) && (s[j] != null))
363 String name = printId(s[j], jvSuffix);
365 out.append(new Format("%-" + maxid + "s").form(name + " "));
367 for (int k = 0; k < 5; k++)
369 int start = (i * 50) + (k * 10);
370 int end = start + 10;
372 int length = s[j].getLength();
376 out.append(s[j].getSequence(start, end));
391 out.append(s[j].getSequenceAsString().substring(start));
410 return out.toString();