2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
24 * <p>Description: </p>
\r
26 * Read and write PileUp style MSF Files.
\r
27 * This used to be the MSFFile class, and was written according to the EBI's idea
\r
28 * of a subset of the MSF alignment format. But, that was updated to reflect current
\r
29 * GCG style IO fashion, as found in Emboss (thanks David Martin!)
\r
35 import jalview.datamodel.*;
\r
36 import jalview.util.*;
\r
38 public class PileUpfile
\r
45 public PileUpfile(String inStr)
\r
50 public PileUpfile(String inFile, String type)
\r
53 super(inFile, type);
\r
59 boolean seqFlag = false;
\r
60 String key = new String();
\r
61 Vector headers = new Vector();
\r
62 Hashtable seqhash = new Hashtable();
\r
67 while ( (line = nextLine()) != null)
\r
69 StringTokenizer str = new StringTokenizer(line);
\r
71 while (str.hasMoreTokens())
\r
73 String inStr = str.nextToken();
\r
75 //If line has header information add to the headers vector
\r
76 if (inStr.indexOf("Name:") != -1)
\r
78 key = str.nextToken();
\r
79 headers.addElement(key);
\r
82 //if line has // set SeqFlag to 1 so we know sequences are coming
\r
83 if (inStr.indexOf("//") != -1)
\r
88 //Process lines as sequence lines if seqFlag is set
\r
89 if ( (inStr.indexOf("//") == -1) && (seqFlag == true))
\r
91 //seqeunce id is the first field
\r
94 StringBuffer tempseq;
\r
96 //Get sequence from hash if it exists
\r
97 if (seqhash.containsKey(key))
\r
99 tempseq = (StringBuffer) seqhash.get(key);
\r
103 tempseq = new StringBuffer();
\r
104 seqhash.put(key, tempseq);
\r
107 //loop through the rest of the words
\r
108 while (str.hasMoreTokens())
\r
110 //append the word to the sequence
\r
111 tempseq.append(str.nextToken());
\r
117 catch (IOException e)
\r
119 System.err.println("Exception parsing PileUpfile " + e);
\r
120 e.printStackTrace();
\r
123 this.noSeqs = headers.size();
\r
125 //Add sequences to the hash
\r
126 for (i = 0; i < headers.size(); i++)
\r
128 if (seqhash.get(headers.elementAt(i)) != null)
\r
130 String head = headers.elementAt(i).toString();
\r
131 String seq = seqhash.get(head).toString();
\r
136 if (maxLength < head.length())
\r
138 maxLength = head.length();
\r
141 if (head.indexOf("/") > 0)
\r
143 StringTokenizer st = new StringTokenizer(head, "/");
\r
145 if (st.countTokens() == 2)
\r
147 head = st.nextToken();
\r
149 String tmp = st.nextToken();
\r
150 st = new StringTokenizer(tmp, "-");
\r
152 if (st.countTokens() == 2)
\r
154 start = Integer.valueOf(st.nextToken()).intValue();
\r
155 end = Integer.valueOf(st.nextToken()).intValue();
\r
160 Sequence newSeq = new Sequence(head, seq, start, end);
\r
162 seqs.addElement(newSeq);
\r
166 System.err.println(
\r
167 "PileUpfile Parser: Can't find sequence for " +
\r
168 headers.elementAt(i));
\r
173 public static int checkSum(String seq)
\r
175 //String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";
\r
178 String sequence = seq.toUpperCase();
\r
180 String index = "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";
\r
181 index += "--------------------------------------------------------------------------------------------------------------------------------";
\r
183 for (int i = 0; i < sequence.length(); i++)
\r
187 if (i < sequence.length())
\r
189 int pos = index.indexOf(sequence.charAt(i));
\r
191 if (index.charAt(pos)!='_')
\r
193 check += ( ( (i % 57) + 1) * pos);
\r
197 catch (Exception e)
\r
199 System.err.println("Exception during MSF Checksum calculation");
\r
200 e.printStackTrace();
\r
204 return check % 10000;
\r
207 public static String print(SequenceI[] s)
\r
209 StringBuffer out = new StringBuffer("PileUp\n\n");
\r
217 while ( (i < s.length) && (s[i] != null))
\r
219 big += s[i].getSequence();
\r
225 int bigcheck = checkSum(big);
\r
227 out.append(" MSF: " + s[0].getSequence().length() +
\r
228 " Type: P Check: " + bigcheck + " ..\n\n\n");
\r
230 while ( (i < s.length) && (s[i] != null))
\r
232 String seq = s[i].getSequence();
\r
233 String name = s[i].getName() + "/" + s[i].getStart() + "-" +
\r
235 int check = checkSum(s[i].getSequence());
\r
236 out.append(" Name: " + name + " oo Len: " +
\r
237 s[i].getSequence().length() + " Check: " + check +
\r
238 " Weight: 1.00\n");
\r
240 if (seq.length() > max)
\r
242 max = seq.length();
\r
245 if (name.length() > maxid)
\r
247 maxid = name.length();
\r
259 out.append("\n\n//\n\n");
\r
263 int nochunks = (max / len) + 1;
\r
265 if ( (max % len) == 0)
\r
270 for (i = 0; i < nochunks; i++)
\r
274 while ( (j < s.length) && (s[j] != null))
\r
276 String name = s[j].getName();
\r
277 out.append(new Format("%-" + maxid + "s").form(name + "/" +
\r
278 s[j].getStart() + "-" + s[j].getEnd()) + " ");
\r
280 for (int k = 0; k < 5; k++)
\r
282 int start = (i * 50) + (k * 10);
\r
283 int end = start + 10;
\r
285 if ( (end < s[j].getSequence().length()) &&
\r
286 (start < s[j].getSequence().length()))
\r
288 out.append(s[j].getSequence().substring(start, end));
\r
301 if (start < s[j].getSequence().length())
\r
303 out.append(s[j].getSequence().substring(start));
\r
322 return out.toString();
\r
325 public String print()
\r
327 return print(getSeqsAsArray());
\r