2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 * <p>Description: </p>
27 * Read and write PileUp style MSF Files.
28 * This used to be the MSFFile class, and was written according to the EBI's idea
29 * of a subset of the MSF alignment format. But, that was updated to reflect current
30 * GCG style IO fashion, as found in Emboss (thanks David Martin!)
33 import jalview.datamodel.*;
\r
35 import jalview.util.*;
\r
42 public class PileUpfile extends AlignFile {
\r
43 public PileUpfile() {
\r
46 public PileUpfile(String inStr) {
\r
50 public PileUpfile(String inFile, String type) throws IOException {
\r
51 super(inFile, type);
\r
54 public void parse() {
\r
56 boolean seqFlag = false;
\r
57 String key = new String();
\r
58 Vector headers = new Vector();
\r
59 Hashtable seqhash = new Hashtable();
\r
63 while ((line = nextLine()) != null) {
\r
64 StringTokenizer str = new StringTokenizer(line);
\r
66 while (str.hasMoreTokens()) {
\r
67 String inStr = str.nextToken();
\r
69 //If line has header information add to the headers vector
\r
70 if (inStr.indexOf("Name:") != -1) {
\r
71 key = str.nextToken();
\r
72 headers.addElement(key);
\r
75 //if line has // set SeqFlag to 1 so we know sequences are coming
\r
76 if (inStr.indexOf("//") != -1) {
\r
80 //Process lines as sequence lines if seqFlag is set
\r
81 if ((inStr.indexOf("//") == -1) && (seqFlag == true)) {
\r
82 //seqeunce id is the first field
\r
85 StringBuffer tempseq;
\r
87 //Get sequence from hash if it exists
\r
88 if (seqhash.containsKey(key)) {
\r
89 tempseq = (StringBuffer) seqhash.get(key);
\r
91 tempseq = new StringBuffer();
\r
92 seqhash.put(key, tempseq);
\r
95 //loop through the rest of the words
\r
96 while (str.hasMoreTokens()) {
\r
97 //append the word to the sequence
\r
98 tempseq.append(str.nextToken());
\r
103 } catch (IOException e) {
\r
104 System.err.println("Exception parsing PileUpfile " + e);
\r
105 e.printStackTrace();
\r
108 this.noSeqs = headers.size();
\r
110 //Add sequences to the hash
\r
111 for (i = 0; i < headers.size(); i++) {
\r
112 if (seqhash.get(headers.elementAt(i)) != null) {
\r
113 String head = headers.elementAt(i).toString();
\r
114 String seq = seqhash.get(head).toString();
\r
117 int end = seq.length();
\r
119 if (maxLength < head.length()) {
\r
120 maxLength = head.length();
\r
123 if (head.indexOf("/") > 0) {
\r
124 StringTokenizer st = new StringTokenizer(head, "/");
\r
126 if (st.countTokens() == 2) {
\r
127 head = st.nextToken();
\r
129 String tmp = st.nextToken();
\r
130 st = new StringTokenizer(tmp, "-");
\r
132 if (st.countTokens() == 2) {
\r
133 start = Integer.valueOf(st.nextToken()).intValue();
\r
134 end = Integer.valueOf(st.nextToken()).intValue();
\r
139 Sequence newSeq = new Sequence(head, seq, start, end);
\r
141 seqs.addElement(newSeq);
\r
143 System.err.println(
\r
144 "PileUpfile Parser: Can't find sequence for " +
\r
145 headers.elementAt(i));
\r
150 public static int checkSum(String seq) {
\r
151 //String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";
\r
154 String index = "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";
\r
155 index += "--------------------------------------------------------------------------------------------------------------------------------";
\r
157 for (int i = 0; i < seq.length(); i++) {
\r
159 if (i < seq.length()) {
\r
160 int pos = index.indexOf(seq.substring(i, i + 1));
\r
162 if (!index.substring(pos, pos + 1).equals("_")) {
\r
163 check += (((i % 57) + 1) * pos);
\r
166 } catch (Exception e) {
\r
167 System.err.println("Exception during MSF Checksum calculation");
\r
168 e.printStackTrace();
\r
172 return check % 10000;
\r
175 public static String print(SequenceI[] s) {
\r
176 StringBuffer out = new StringBuffer("PileUp\n\n");
\r
184 while ((i < s.length) && (s[i] != null)) {
\r
185 big += s[i].getSequence();
\r
191 int bigcheck = checkSum(big);
\r
193 out.append(" MSF: " + s[0].getSequence().length() +
\r
194 " Type: P Check: " + bigcheck + " ..\n\n\n");
\r
196 while ((i < s.length) && (s[i] != null)) {
\r
197 String seq = s[i].getSequence();
\r
198 String name = s[i].getName() + "/" + s[i].getStart() + "-" +
\r
200 int check = checkSum(s[i].getSequence());
\r
201 out.append(" Name: " + name + " oo Len: " +
\r
202 s[i].getSequence().length() + " Check: " + check +
\r
203 " Weight: 1.00\n");
\r
205 if (seq.length() > max) {
\r
206 max = seq.length();
\r
209 if (name.length() > maxid) {
\r
210 maxid = name.length();
\r
221 out.append("\n\n//\n\n");
\r
225 int nochunks = (max / len) + 1;
\r
227 if ((max % len) == 0) {
\r
231 for (i = 0; i < nochunks; i++) {
\r
234 while ((j < s.length) && (s[j] != null)) {
\r
235 String name = s[j].getName();
\r
236 out.append(new Format("%-" + maxid + "s").form(name + "/" +
\r
237 s[j].getStart() + "-" + s[j].getEnd()) + " ");
\r
239 for (int k = 0; k < 5; k++) {
\r
240 int start = (i * 50) + (k * 10);
\r
241 int end = start + 10;
\r
243 if ((end < s[j].getSequence().length()) &&
\r
244 (start < s[j].getSequence().length())) {
\r
245 out.append(s[j].getSequence().substring(start, end));
\r
253 if (start < s[j].getSequence().length()) {
\r
254 out.append(s[j].getSequence().substring(start));
\r
270 return out.toString();
\r
273 public String print() {
\r
274 return print(getSeqsAsArray());
\r