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
178 String sequence = seq.toUpperCase();
\r
180 for (int i = 0; i < sequence.length(); i++)
\r
182 if (i < sequence.length())
\r
184 int value = sequence.charAt(i);
\r
187 check += (i % 57 + 1) * value;
\r
193 return check % 10000;
\r
196 public static String print(SequenceI[] s)
\r
198 StringBuffer out = new StringBuffer("PileUp\n\n");
\r
204 int bigChecksum = 0;
\r
205 int[] checksums = new int[s.length];
\r
206 while (i < s.length)
\r
208 checksums[i] = checkSum(s[i].getSequence());
\r
209 bigChecksum += checksums[i];
\r
213 out.append(" MSF: " + s[0].getSequence().length() +
\r
214 " Type: P Check: " + bigChecksum%10000 + " ..\n\n\n");
\r
217 while ( (i < s.length) && (s[i] != null))
\r
219 String seq = s[i].getSequence();
\r
220 out.append(" Name: " + s[i].getName() + " oo Len: " +
\r
221 s[i].getSequence().length() + " Check: " + checksums[i] +
\r
222 " Weight: 1.00\n");
\r
224 if (seq.length() > max)
\r
226 max = seq.length();
\r
229 if (s[i].getName().length() > maxid)
\r
231 maxid = s[i].getName().length();
\r
243 out.append("\n\n//\n\n");
\r
247 int nochunks = (max / len) + 1;
\r
249 if ( (max % len) == 0)
\r
254 for (i = 0; i < nochunks; i++)
\r
258 while ( (j < s.length) && (s[j] != null))
\r
260 String name = s[j].getName();
\r
261 out.append(new Format("%-" + maxid + "s").form(name) + " ");
\r
263 for (int k = 0; k < 5; k++)
\r
265 int start = (i * 50) + (k * 10);
\r
266 int end = start + 10;
\r
268 if ( (end < s[j].getSequence().length()) &&
\r
269 (start < s[j].getSequence().length()))
\r
271 out.append(s[j].getSequence().substring(start, end));
\r
284 if (start < s[j].getSequence().length())
\r
286 out.append(s[j].getSequence().substring(start));
\r
305 return out.toString();
\r
308 public String print()
\r
310 return print(getSeqsAsArray());
\r