2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import javax.xml.parsers.ParserConfigurationException;
25 import org.xml.sax.SAXException;
27 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
28 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
29 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
30 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
32 import jalview.datamodel.*;
33 import jalview.util.*;
41 public class MSFfile extends AlignFile
45 * Creates a new MSFfile object.
52 * Creates a new MSFfile object.
60 public MSFfile(String inFile, String type) throws Exception
65 public MSFfile(FileParse source) throws Exception
71 // TODO Auto-generated constructor stub
77 public void parse() throws IOException
80 boolean seqFlag = false;
81 String key = new String();
82 Vector headers = new Vector();
83 Hashtable seqhash = new Hashtable();
88 while ((line = nextLine()) != null)
90 StringTokenizer str = new StringTokenizer(line);
92 while (str.hasMoreTokens())
94 String inStr = str.nextToken();
96 // If line has header information add to the headers vector
97 if (inStr.indexOf("Name:") != -1)
99 key = str.nextToken();
100 headers.addElement(key);
103 // if line has // set SeqFlag to 1 so we know sequences are coming
104 if (inStr.indexOf("//") != -1)
109 // Process lines as sequence lines if seqFlag is set
110 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
112 // seqeunce id is the first field
115 StringBuffer tempseq;
117 // Get sequence from hash if it exists
118 if (seqhash.containsKey(key))
120 tempseq = (StringBuffer) seqhash.get(key);
124 tempseq = new StringBuffer();
125 seqhash.put(key, tempseq);
128 // loop through the rest of the words
129 while (str.hasMoreTokens())
131 // append the word to the sequence
132 tempseq.append(str.nextToken());
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 (i = 0; i < headers.size(); i++)
148 if (seqhash.get(headers.elementAt(i)) != null)
150 String head = headers.elementAt(i).toString();
151 String seq = seqhash.get(head).toString();
153 if (maxLength < head.length())
155 maxLength = head.length();
158 // Replace ~ with a sensible gap character
159 seq = seq.replace('~', '-');
161 Sequence newSeq = parseId(head);
163 newSeq.setSequence(seq);
165 seqs.addElement(newSeq);
169 System.err.println("MSFFile Parser: Can't find sequence for "
170 + headers.elementAt(i));
181 * @return DOCUMENT ME!
183 public int checkSum(String seq)
186 String sequence = seq.toUpperCase();
188 for (int i = 0; i < sequence.length(); i++)
193 int value = sequence.charAt(i);
196 check += (i % 57 + 1) * value;
198 } catch (Exception e)
200 System.err.println("Exception during MSF Checksum calculation");
205 return check % 10000;
216 * @return DOCUMENT ME!
218 public String print(SequenceI[] seqs)
221 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
223 SequenceI[] s = new SequenceI[seqs.length];
225 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
226 + "_MULTIPLE_ALIGNMENT 1.0");
227 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
234 while ((i < seqs.length) && (seqs[i] != null))
236 // Replace all internal gaps with . and external spaces with ~
237 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
238 .replace('-', '.'), seqs[i].getStart(), seqs[i].getEnd());
240 StringBuffer sb = new StringBuffer();
241 sb.append(s[i].getSequence());
243 for (int ii = 0; ii < sb.length(); ii++)
245 if (sb.charAt(ii) == '.')
247 sb.setCharAt(ii, '~');
255 for (int ii = sb.length() - 1; ii > 0; ii--)
257 if (sb.charAt(ii) == '.')
259 sb.setCharAt(ii, '~');
267 s[i].setSequence(sb.toString());
269 if (s[i].getSequence().length > max)
271 max = s[i].getSequence().length;
277 Format maxLenpad = new Format("%" + (new String("" + max)).length()
279 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
284 int[] checksums = new int[s.length];
287 checksums[i] = checkSum(s[i].getSequenceAsString());
288 bigChecksum += checksums[i];
293 out.append(" MSF: " + s[0].getSequence().length + " Type: "
294 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
300 String[] nameBlock = new String[s.length];
301 String[] idBlock = new String[s.length];
304 while ((i < s.length) && (s[i] != null))
307 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
309 idBlock[i] = new String("Len: "
310 + maxLenpad.form(s[i].getSequence().length) + " Check: "
311 + maxChkpad.form(checksums[i]) + " Weight: 1.00" + newline);
313 if (s[i].getName().length() > maxid)
315 maxid = s[i].getName().length();
318 if (nameBlock[i].length() > maxNB)
320 maxNB = nameBlock[i].length();
336 Format nbFormat = new Format("%-" + maxNB + "s");
338 for (i = 0; (i < s.length) && (s[i] != null); i++)
340 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
351 int nochunks = (max / len) + 1;
353 if ((max % len) == 0)
358 for (i = 0; i < nochunks; i++)
362 while ((j < s.length) && (s[j] != null))
364 String name = printId(s[j]);
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 if ((end < s[j].getSequence().length)
374 && (start < s[j].getSequence().length))
376 out.append(s[j].getSequence(start, end));
389 if (start < s[j].getSequence().length)
391 out.append(s[j].getSequenceAsString().substring(start));
410 return out.toString();
416 * @return DOCUMENT ME!
418 public String print()
420 return print(getSeqsAsArray());