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!)
35 import jalview.datamodel.*;
36 import jalview.util.*;
42 public class PileUpfile
49 public PileUpfile(String inStr) {
53 public PileUpfile(String inFile, String type) throws IOException {
60 boolean seqFlag = false;
61 String key = new String();
62 Vector headers = new Vector();
63 Hashtable seqhash = new Hashtable();
67 while ((line = nextLine()) != null) {
69 StringTokenizer str = new StringTokenizer(line);
71 while (str.hasMoreTokens()) {
73 String inStr = str.nextToken();
75 //If line has header information add to the headers vector
76 if (inStr.indexOf("Name:") != -1) {
77 key = str.nextToken();
78 headers.addElement(key);
81 //if line has // set SeqFlag to 1 so we know sequences are coming
82 if (inStr.indexOf("//") != -1) {
86 //Process lines as sequence lines if seqFlag is set
87 if (( inStr.indexOf("//") == -1) && (seqFlag == true)) {
88 //seqeunce id is the first field
92 //Get sequence from hash if it exists
93 if (seqhash.containsKey(key)) {
94 tempseq = (StringBuffer)seqhash.get(key);
96 tempseq = new StringBuffer();
97 seqhash.put(key,tempseq);
100 //loop through the rest of the words
101 while (str.hasMoreTokens()) {
102 //append the word to the sequence
103 tempseq.append(str.nextToken());
108 } catch (IOException e) {
109 System.err.println("Exception parsing PileUpfile " + e);
113 this.noSeqs = headers.size();
115 //Add sequences to the hash
116 for (i = 0; i < headers.size(); i++ ) {
118 if ( seqhash.get(headers.elementAt(i)) != null) {
119 String head = headers.elementAt(i).toString();
120 String seq = seqhash.get(head).toString();
123 int end = seq.length();
125 if (maxLength < head.length() ) {
126 maxLength = head.length();
129 if (head.indexOf("/") > 0 ) {
131 StringTokenizer st = new StringTokenizer(head,"/");
133 if (st.countTokens() == 2) {
135 head = st.nextToken();
136 String tmp = st.nextToken();
137 st = new StringTokenizer(tmp,"-");
138 if (st.countTokens() == 2) {
139 start = Integer.valueOf(st.nextToken()).intValue();
140 end = Integer.valueOf(st.nextToken()).intValue();
145 Sequence newSeq = new Sequence(head,seq,start,end);
147 seqs.addElement(newSeq);
150 System.err.println("PileUpfile Parser: Can't find sequence for " + headers.elementAt(i));
156 public static int checkSum(String seq) {
157 //String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";
160 String index = "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";
161 index += "--------------------------------------------------------------------------------------------------------------------------------";
163 for(int i = 0; i < seq.length(); i++) {
165 if (i <seq.length()) {
166 int pos = index.indexOf(seq.substring(i,i+1));
167 if (!index.substring(pos,pos+1).equals("_")) {
168 check += ((i % 57) + 1) * pos;
171 } catch (Exception e) {
172 System.err.println("Exception during MSF Checksum calculation");
176 return check % 10000;
179 public static String print(SequenceI[] s) {
180 StringBuffer out = new StringBuffer("PileUp\n\n");
187 while (i < s.length && s[i] != null) {
188 big += s[i].getSequence();
192 int bigcheck = checkSum(big);
194 out.append(" MSF: " + s[0].getSequence().length() + " Type: P Check: " + bigcheck + " ..\n\n\n");
196 while (i < s.length && s[i] != null) {
197 String seq = s[i].getSequence();
198 String name = s[i].getName()+ "/" + s[i].getStart() + "-" + s[i].getEnd();
199 int check = checkSum(s[i].getSequence());
200 out.append(" Name: " + name + " oo Len: " + s[i].getSequence().length() + " Check: " + check + " Weight: 1.00\n");
201 if (seq.length() > max) {
204 if (name.length() > maxid) {
205 maxid = name.length();
214 out.append( "\n\n//\n\n");
218 int nochunks = max / len + 1;
222 for (i = 0; i < nochunks; i++) {
224 while (j < s.length && s[j] != null) {
225 String name = s[j].getName();
226 out.append( new Format("%-" + maxid + "s").form(name + "/" + s[j].getStart() + "-" + s[j].getEnd()) + " ");
227 for (int k = 0; k < 5; k++) {
229 int start = i*50 + k*10;
230 int end = start + 10;
232 if (end < s[j].getSequence().length() && start < s[j].getSequence().length() ) {
233 out.append(s[j].getSequence().substring(start,end));
240 if (start < s[j].getSequence().length()) {
241 out.append(s[j].getSequence().substring(start));
255 return out.toString();
257 public String print() {
258 return print(getSeqsAsArray());