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
65 * @throws InterruptedException
67 public MSFfile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
72 public MSFfile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
78 // TODO Auto-generated constructor stub
84 public void parse() throws IOException
87 boolean seqFlag = false;
88 String key = new String();
89 Vector headers = new Vector();
90 Hashtable seqhash = new Hashtable();
95 while ((line = nextLine()) != null)
97 StringTokenizer str = new StringTokenizer(line);
99 while (str.hasMoreTokens())
101 String inStr = str.nextToken();
103 // If line has header information add to the headers vector
104 if (inStr.indexOf("Name:") != -1)
106 key = str.nextToken();
107 headers.addElement(key);
110 // if line has // set SeqFlag to 1 so we know sequences are coming
111 if (inStr.indexOf("//") != -1)
116 // Process lines as sequence lines if seqFlag is set
117 if ((inStr.indexOf("//") == -1) && (seqFlag == true))
119 // seqeunce id is the first field
122 StringBuffer tempseq;
124 // Get sequence from hash if it exists
125 if (seqhash.containsKey(key))
127 tempseq = (StringBuffer) seqhash.get(key);
131 tempseq = new StringBuffer();
132 seqhash.put(key, tempseq);
135 // loop through the rest of the words
136 while (str.hasMoreTokens())
138 // append the word to the sequence
139 tempseq.append(str.nextToken());
144 } catch (IOException e)
146 System.err.println("Exception parsing MSFFile " + e);
150 this.noSeqs = headers.size();
152 // Add sequences to the hash
153 for (i = 0; i < headers.size(); i++)
155 if (seqhash.get(headers.elementAt(i)) != null)
157 String head = headers.elementAt(i).toString();
158 String seq = seqhash.get(head).toString();
160 if (maxLength < head.length())
162 maxLength = head.length();
165 // Replace ~ with a sensible gap character
166 seq = seq.replace('~', '-');
168 Sequence newSeq = parseId(head);
170 newSeq.setSequence(seq);
172 seqs.addElement(newSeq);
176 System.err.println("MSFFile Parser: Can't find sequence for "
177 + headers.elementAt(i));
188 * @return DOCUMENT ME!
190 public int checkSum(String seq)
193 String sequence = seq.toUpperCase();
195 for (int i = 0; i < sequence.length(); i++)
200 int value = sequence.charAt(i);
203 check += (i % 57 + 1) * value;
205 } catch (Exception e)
207 System.err.println("Exception during MSF Checksum calculation");
212 return check % 10000;
223 * @return DOCUMENT ME!
225 public String print(SequenceI[] seqs)
228 boolean is_NA = jalview.util.Comparison.isNucleotide(seqs);
230 SequenceI[] s = new SequenceI[seqs.length];
232 StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA")
233 + "_MULTIPLE_ALIGNMENT 1.0");
234 // TODO: JBPNote : Jalview doesn't remember NA or AA yet.
241 while ((i < seqs.length) && (seqs[i] != null))
243 // Replace all internal gaps with . and external spaces with ~
244 s[i] = new Sequence(seqs[i].getName(), seqs[i].getSequenceAsString()
245 .replace('-', '.'), seqs[i].getStart(),seqs[i].getEnd());
247 StringBuffer sb = new StringBuffer();
248 sb.append(s[i].getSequence());
250 for (int ii = 0; ii < sb.length(); ii++)
252 if (sb.charAt(ii) == '.')
254 sb.setCharAt(ii, '~');
262 for (int ii = sb.length() - 1; ii > 0; ii--)
264 if (sb.charAt(ii) == '.')
266 sb.setCharAt(ii, '~');
274 s[i].setSequence(sb.toString());
276 if (s[i].getSequence().length > max)
278 max = s[i].getSequence().length;
284 Format maxLenpad = new Format("%" + (new String("" + max)).length()
286 Format maxChkpad = new Format("%" + (new String("1" + max)).length()
291 int[] checksums = new int[s.length];
294 checksums[i] = checkSum(s[i].getSequenceAsString());
295 bigChecksum += checksums[i];
300 out.append(" MSF: " + s[0].getSequence().length + " Type: "
301 + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000)
307 String[] nameBlock = new String[s.length];
308 String[] idBlock = new String[s.length];
311 while ((i < s.length) && (s[i] != null))
314 nameBlock[i] = new String(" Name: " + printId(s[i]) + " ");
316 idBlock[i] = new String("Len: "
317 + maxLenpad.form(s[i].getSequence().length) + " Check: "
318 + maxChkpad.form(checksums[i]) + " Weight: 1.00"+newline);
320 if (s[i].getName().length() > maxid)
322 maxid = s[i].getName().length();
325 if (nameBlock[i].length() > maxNB)
327 maxNB = nameBlock[i].length();
343 Format nbFormat = new Format("%-" + maxNB + "s");
345 for (i = 0; (i < s.length) && (s[i] != null); i++)
347 out.append(nbFormat.form(nameBlock[i]) + idBlock[i]);
352 out.append(newline);out.append("//");
357 int nochunks = (max / len) + 1;
359 if ((max % len) == 0)
364 for (i = 0; i < nochunks; i++)
368 while ((j < s.length) && (s[j] != null))
370 String name = printId(s[j]);
372 out.append(new Format("%-" + maxid + "s").form(name + " "));
374 for (int k = 0; k < 5; k++)
376 int start = (i * 50) + (k * 10);
377 int end = start + 10;
379 if ((end < s[j].getSequence().length)
380 && (start < s[j].getSequence().length))
382 out.append(s[j].getSequence(start, end));
395 if (start < s[j].getSequence().length)
397 out.append(s[j].getSequenceAsString().substring(start));
416 return out.toString();
422 * @return DOCUMENT ME!
424 public String print()
426 return print(getSeqsAsArray());