2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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;
31 import jalview.datamodel.*;
32 import jalview.util.*;
40 public class MSFfile extends AlignFile
44 * Creates a new MSFfile object.
51 * Creates a new MSFfile object.
60 * @throws SAXException
61 * @throws ParserConfigurationException
62 * @throws ExceptionFileFormatOrSyntax
63 * @throws ExceptionLoadingFailed
64 * @throws ExceptionPermissionDenied
66 public MSFfile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
71 public MSFfile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
77 // TODO Auto-generated constructor stub
83 public void parse() throws IOException
86 boolean seqFlag = false;
87 String key = new String();
88 Vector headers = new Vector();
89 Hashtable seqhash = new Hashtable();
94 while ((line = nextLine()) != null)
96 StringTokenizer str = new StringTokenizer(line);
98 while (str.hasMoreTokens())
100 String inStr = str.nextToken();
102 // If line has header information add to the headers vector
103 if (inStr.indexOf("Name:") != -1)
105 key = str.nextToken();
106 headers.addElement(key);
109 // if line has // set SeqFlag to 1 so we know sequences are coming
110 if (inStr.indexOf("//") != -1)
115 // Process lines as sequence lines if seqFlag is set
116 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
118 // seqeunce id is the first field
121 StringBuffer tempseq;
123 // Get sequence from hash if it exists
124 if (seqhash.containsKey(key))
126 tempseq = (StringBuffer) seqhash.get(key);
130 tempseq = new StringBuffer();
131 seqhash.put(key, tempseq);
134 // loop through the rest of the words
135 while (str.hasMoreTokens())
137 // append the word to the sequence
138 tempseq.append(str.nextToken());
143 } catch (IOException e)
145 System.err.println("Exception parsing MSFFile " + e);
149 this.noSeqs = headers.size();
151 // Add sequences to the hash
152 for (i = 0; i < headers.size(); i++)
154 if (seqhash.get(headers.elementAt(i)) != null)
156 String head = headers.elementAt(i).toString();
157 String seq = seqhash.get(head).toString();
159 if (maxLength < head.length())
161 maxLength = head.length();
164 // Replace ~ with a sensible gap character
165 seq = seq.replace('~', '-');
167 Sequence newSeq = parseId(head);
169 newSeq.setSequence(seq);
171 seqs.addElement(newSeq);
175 System.err.println("MSFFile Parser: Can't find sequence for "
176 + headers.elementAt(i));
187 * @return DOCUMENT ME!
189 public int checkSum(String seq)
192 String sequence = seq.toUpperCase();
194 for (int i = 0; i < sequence.length(); i++)
199 int value = sequence.charAt(i);
202 check += (i % 57 + 1) * value;
204 } catch (Exception e)
206 System.err.println("Exception during MSF Checksum calculation");
211 return check % 10000;
222 * @return DOCUMENT ME!
224 public String print(SequenceI[] seqs)
227 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
229 SequenceI[] s = new SequenceI[seqs.length];
231 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
232 + "_MULTIPLE_ALIGNMENT 1.0");
233 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
240 while ((i < seqs.length) && (seqs[i] != null))
242 // Replace all internal gaps with . and external spaces with ~
243 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
244 .replace('-', '.'), seqs[i].getStart(),seqs[i].getEnd());
246 StringBuffer sb = new StringBuffer();
247 sb.append(s[i].getSequence());
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, '~');
273 s[i].setSequence(sb.toString());
275 if (s[i].getSequence().length > max)
277 max = s[i].getSequence().length;
283 Format maxLenpad = new Format("%" + (new String("" + max)).length()
285 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
290 int[] checksums = new int[s.length];
293 checksums[i] = checkSum(s[i].getSequenceAsString());
294 bigChecksum += checksums[i];
299 out.append(" MSF: " + s[0].getSequence().length + " 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]) + " ");
315 idBlock[i] = new String("Len: "
316 + maxLenpad.form(s[i].getSequence().length) + " Check: "
317 + maxChkpad.form(checksums[i]) + " 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]);
351 out.append(newline);out.append("//");
356 int nochunks = (max / len) + 1;
358 if ((max % len) == 0)
363 for (i = 0; i < nochunks; i++)
367 while ((j < s.length) && (s[j] != null))
369 String name = printId(s[j]);
371 out.append(new Format("%-" + maxid + "s").form(name + " "));
373 for (int k = 0; k < 5; k++)
375 int start = (i * 50) + (k * 10);
376 int end = start + 10;
378 if ((end < s[j].getSequence().length)
379 && (start < s[j].getSequence().length))
381 out.append(s[j].getSequence(start, end));
394 if (start < s[j].getSequence().length)
396 out.append(s[j].getSequenceAsString().substring(start));
415 return out.toString();
421 * @return DOCUMENT ME!
423 public String print()
425 return print(getSeqsAsArray());