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.Locale;
33 import java.util.StringTokenizer;
41 public class MSFfile extends AlignFile
45 * Creates a new MSFfile object.
52 * Creates a new MSFfile object.
62 public MSFfile(String inFile, DataSourceType type) throws IOException
67 public MSFfile(FileParse source) throws IOException
73 * Read and parse MSF sequence data
76 public void parse() throws IOException
78 boolean seqFlag = false;
79 List<String> headers = new ArrayList<String>();
80 Hashtable<String, StringBuilder> seqhash = new Hashtable<String, StringBuilder>();
85 while ((line = nextLine()) != null)
87 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();
101 // if line has // set SeqFlag 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)
110 // sequence id is the first field
113 StringBuilder tempseq;
115 // Get sequence from hash if it exists
116 if (seqhash.containsKey(key))
118 tempseq = seqhash.get(key);
122 tempseq = new StringBuilder(64);
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 String sequenceBlock = str.nextToken();
131 tempseq.append(sequenceBlock);
136 } catch (IOException e)
138 System.err.println("Exception parsing MSFFile " + e);
142 this.noSeqs = headers.size();
144 // Add sequences to the hash
145 for (int i = 0; i < headers.size(); i++)
147 if (seqhash.get(headers.get(i)) != null)
149 String head = headers.get(i);
150 String seq = seqhash.get(head).toString();
152 if (maxLength < head.length())
154 maxLength = head.length();
158 * replace ~ (leading/trailing positions) with the gap character;
159 * use '.' as this is the internal gap character required by MSF
161 seq = seq.replace('~', '.');
163 Sequence newSeq = parseId(head);
165 newSeq.setSequence(seq);
167 seqs.addElement(newSeq);
171 System.err.println("MSFFile Parser: Can't find sequence for "
183 * @return DOCUMENT ME!
185 public int checkSum(String seq)
188 String sequence = seq.toUpperCase(Locale.ROOT);
190 for (int i = 0; i < sequence.length(); i++)
195 int value = sequence.charAt(i);
198 check += (i % 57 + 1) * value;
200 } catch (Exception e)
202 System.err.println("Exception during MSF Checksum calculation");
207 return check % 10000;
218 * @return DOCUMENT ME!
221 public String print(SequenceI[] sqs, boolean jvSuffix)
224 boolean is_NA = Comparison.isNucleotide(sqs);
226 SequenceI[] s = new SequenceI[sqs.length];
228 StringBuilder out = new StringBuilder(256);
229 out.append("!!").append(is_NA ? "NA" : "AA")
230 .append("_MULTIPLE_ALIGNMENT 1.0");
231 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
238 while ((i < sqs.length) && (sqs[i] != null))
241 * modify to MSF format: uses '.' for internal gaps,
242 * and '~' for leading or trailing gaps
244 String seqString = sqs[i].getSequenceAsString().replace('-', '.');
246 StringBuilder sb = new StringBuilder(seqString);
248 for (int ii = 0; ii < sb.length(); ii++)
250 if (sb.charAt(ii) == '.')
252 sb.setCharAt(ii, '~');
260 for (int ii = sb.length() - 1; ii > 0; ii--)
262 if (sb.charAt(ii) == '.')
264 sb.setCharAt(ii, '~');
271 s[i] = new Sequence(sqs[i].getName(), sb.toString(),
272 sqs[i].getStart(), sqs[i].getEnd());
274 if (sb.length() > max)
282 Format maxLenpad = new Format(
283 "%" + (new String("" + max)).length() + "d");
284 Format maxChkpad = new Format(
285 "%" + (new String("1" + max)).length() + "d");
289 int[] checksums = new int[s.length];
292 checksums[i] = checkSum(s[i].getSequenceAsString());
293 bigChecksum += checksums[i];
298 out.append(" MSF: " + s[0].getLength() + " Type: "
299 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
305 String[] nameBlock = new String[s.length];
306 String[] idBlock = new String[s.length];
309 while ((i < s.length) && (s[i] != null))
312 nameBlock[i] = new String(" Name: " + printId(s[i], jvSuffix) + " ");
314 idBlock[i] = new String("Len: " + maxLenpad.form(s[i].getLength())
315 + " Check: " + maxChkpad.form(checksums[i])
316 + " Weight: 1.00" + newline);
318 if (s[i].getName().length() > maxid)
320 maxid = s[i].getName().length();
323 if (nameBlock[i].length() > maxNB)
325 maxNB = nameBlock[i].length();
341 Format nbFormat = new Format("%-" + maxNB + "s");
343 for (i = 0; (i < s.length) && (s[i] != null); i++)
345 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
356 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
358 for (i = 0; i < nochunks; i++)
362 while ((j < s.length) && (s[j] != null))
364 String name = printId(s[j], jvSuffix);
366 out.append(new Format("%-" + maxid + "s").form(name + " "));
368 for (int k = 0; k < 5; k++)
370 int start = (i * 50) + (k * 10);
371 int end = start + 10;
373 int length = s[j].getLength();
377 out.append(s[j].getSequence(start, end));
392 out.append(s[j].getSequenceAsString().substring(start));
411 return out.toString();