GPL license added
[jalview.git] / src / jalview / io / PileUpfile.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
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.
9 *
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.
14 *
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
18 */
19
20 package jalview.io;
21
22 /**
23  * <p>Title: </p>
24  *  PileUpfile
25  * <p>Description: </p>
26  *
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!)
31  *
32  **/
33
34
35 import jalview.datamodel.*;
36 import jalview.util.*;
37
38 import java.io.*;
39 import java.util.*;
40
41
42 public class PileUpfile
43     extends AlignFile
44 {
45
46 public PileUpfile()
47 {}
48
49 public PileUpfile(String inStr) {
50   super(inStr);
51 }
52
53 public PileUpfile(String inFile, String type) throws IOException {
54   super(inFile,type);
55 }
56
57 public void parse() {
58
59   int       i       = 0;
60   boolean   seqFlag = false;
61   String    key     = new String();
62   Vector    headers = new Vector();
63   Hashtable seqhash = new Hashtable();
64   String    line;
65
66   try {
67   while ((line = nextLine()) != null) {
68
69     StringTokenizer str = new StringTokenizer(line);
70
71     while (str.hasMoreTokens()) {
72
73       String inStr = str.nextToken();
74
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);
79       }
80
81       //if line has // set SeqFlag to 1 so we know sequences are coming
82       if (inStr.indexOf("//") != -1) {
83         seqFlag = true;
84       }
85
86       //Process lines as sequence lines if seqFlag is set
87       if (( inStr.indexOf("//") == -1) && (seqFlag == true)) {
88         //seqeunce id is the first field
89         key = inStr;
90         StringBuffer tempseq;
91
92         //Get sequence from hash if it exists
93         if (seqhash.containsKey(key)) {
94           tempseq = (StringBuffer)seqhash.get(key);
95         } else {
96           tempseq = new StringBuffer();
97           seqhash.put(key,tempseq);
98         }
99
100         //loop through the rest of the words
101         while (str.hasMoreTokens()) {
102           //append the word to the sequence
103           tempseq.append(str.nextToken());
104         }
105       }
106     }
107   }
108   } catch (IOException e) {
109     System.err.println("Exception parsing PileUpfile " + e);
110     e.printStackTrace();
111   }
112
113   this.noSeqs = headers.size();
114
115   //Add sequences to the hash
116   for (i = 0; i < headers.size(); i++ ) {
117
118     if ( seqhash.get(headers.elementAt(i)) != null) {
119       String head =  headers.elementAt(i).toString();
120       String seq  =  seqhash.get(head).toString();
121
122       int start = 1;
123       int end = seq.length();
124
125       if (maxLength <  head.length() ) {
126         maxLength =  head.length();
127       }
128
129       if (head.indexOf("/") > 0 ) {
130
131         StringTokenizer st = new StringTokenizer(head,"/");
132
133         if (st.countTokens() == 2) {
134
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();
141           }
142         }
143       }
144
145       Sequence newSeq = new Sequence(head,seq,start,end);
146
147       seqs.addElement(newSeq);
148
149     } else {
150       System.err.println("PileUpfile Parser: Can't find sequence for " + headers.elementAt(i));
151     }
152   }
153
154 }
155
156 public static int checkSum(String seq) {
157   //String chars =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";
158   int check = 0;
159
160   String index =  "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";
161   index += "--------------------------------------------------------------------------------------------------------------------------------";
162
163   for(int i = 0; i < seq.length(); i++) {
164     try {
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;
169         }
170       }
171     } catch (Exception e) {
172       System.err.println("Exception during MSF Checksum calculation");
173       e.printStackTrace();
174     }
175   }
176   return check % 10000;
177 }
178
179 public static String print(SequenceI[] s) {
180   StringBuffer out = new StringBuffer("PileUp\n\n");
181
182   int max = 0;
183   int maxid = 0;
184
185   int i = 0;
186   String big = "";
187   while (i < s.length && s[i] != null) {
188     big += s[i].getSequence();
189     i++;
190   }
191   i = 0;
192   int bigcheck = checkSum(big);
193
194   out.append("   MSF: " + s[0].getSequence().length() + "   Type: P    Check:  " + bigcheck + "   ..\n\n\n");
195
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) {
202       max = seq.length();
203     }
204     if (name.length() > maxid) {
205       maxid = name.length();
206     }
207     i++;
208   }
209
210   if (maxid < 10) {
211     maxid = 10;
212   }
213   maxid++;
214   out.append( "\n\n//\n\n");
215
216   int len = 50;
217
218   int nochunks =  max / len + 1;
219   if (max%len == 0) {
220     nochunks--;
221   }
222   for (i = 0; i < nochunks; i++) {
223     int j = 0;
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++) {
228
229         int start = i*50 + k*10;
230         int end = start + 10;
231
232         if (end < s[j].getSequence().length() && start < s[j].getSequence().length() ) {
233           out.append(s[j].getSequence().substring(start,end));
234           if (k < 4) {
235             out.append(" ");
236           } else {
237             out.append("\n");
238           }
239         } else {
240           if (start < s[j].getSequence().length()) {
241             out.append(s[j].getSequence().substring(start));
242             out.append("\n");
243           } else {
244             if (k == 0) {
245               out.append("\n");
246             }
247           }
248         }
249       }
250       j++;
251     }
252     out.append("\n");
253
254   }
255   return out.toString();
256 }
257 public String print() {
258   return print(getSeqsAsArray());
259 }
260 }
261
262
263
264
265
266
267