Old MSF Format writer now renamed.
[jalview.git] / src / jalview / io / PileUpfile.java
1 package jalview.io;
2
3 /**
4  * <p>Title: </p>
5  *  PileUpfile
6  * <p>Description: </p>
7  *
8  *  Read and write PileUp style MSF Files.
9  *  This used to be the MSFFile class, and was written according to the EBI's idea
10  *  of a subset of the MSF alignment format. But, that was updated to reflect current
11  *  GCG style IO fashion, as found in Emboss (thanks David Martin!)
12  *
13  **/
14
15
16 import jalview.datamodel.*;
17 import jalview.util.*;
18
19 import java.io.*;
20 import java.util.*;
21
22
23 public class PileUpfile
24     extends AlignFile
25 {
26
27 public PileUpfile()
28 {}
29
30 public PileUpfile(String inStr) {
31   super(inStr);
32 }
33
34 public PileUpfile(String inFile, String type) throws IOException {
35   super(inFile,type);
36 }
37
38 public void parse() {
39
40   int       i       = 0;
41   boolean   seqFlag = false;
42   String    key     = new String();
43   Vector    headers = new Vector();
44   Hashtable seqhash = new Hashtable();
45   String    line;
46
47   try {
48   while ((line = nextLine()) != null) {
49
50     StringTokenizer str = new StringTokenizer(line);
51
52     while (str.hasMoreTokens()) {
53
54       String inStr = str.nextToken();
55
56       //If line has header information add to the headers vector
57       if (inStr.indexOf("Name:") != -1) {
58         key = str.nextToken();
59         headers.addElement(key);
60       }
61
62       //if line has // set SeqFlag to 1 so we know sequences are coming
63       if (inStr.indexOf("//") != -1) {
64         seqFlag = true;
65       }
66
67       //Process lines as sequence lines if seqFlag is set
68       if (( inStr.indexOf("//") == -1) && (seqFlag == true)) {
69         //seqeunce id is the first field
70         key = inStr;
71         StringBuffer tempseq;
72
73         //Get sequence from hash if it exists
74         if (seqhash.containsKey(key)) {
75           tempseq = (StringBuffer)seqhash.get(key);
76         } else {
77           tempseq = new StringBuffer();
78           seqhash.put(key,tempseq);
79         }
80
81         //loop through the rest of the words
82         while (str.hasMoreTokens()) {
83           //append the word to the sequence
84           tempseq.append(str.nextToken());
85         }
86       }
87     }
88   }
89   } catch (IOException e) {
90     System.err.println("Exception parsing PileUpfile " + e);
91     e.printStackTrace();
92   }
93
94   this.noSeqs = headers.size();
95
96   //Add sequences to the hash
97   for (i = 0; i < headers.size(); i++ ) {
98
99     if ( seqhash.get(headers.elementAt(i)) != null) {
100       String head =  headers.elementAt(i).toString();
101       String seq  =  seqhash.get(head).toString();
102
103       int start = 1;
104       int end = seq.length();
105
106       if (maxLength <  head.length() ) {
107         maxLength =  head.length();
108       }
109
110       if (head.indexOf("/") > 0 ) {
111
112         StringTokenizer st = new StringTokenizer(head,"/");
113
114         if (st.countTokens() == 2) {
115
116           head = st.nextToken();
117           String tmp = st.nextToken();
118           st = new StringTokenizer(tmp,"-");
119           if (st.countTokens() == 2) {
120             start = Integer.valueOf(st.nextToken()).intValue();
121             end = Integer.valueOf(st.nextToken()).intValue();
122           }
123         }
124       }
125
126       Sequence newSeq = new Sequence(head,seq,start,end);
127
128       seqs.addElement(newSeq);
129
130     } else {
131       System.err.println("PileUpfile Parser: Can't find sequence for " + headers.elementAt(i));
132     }
133   }
134
135 }
136
137 public static int checkSum(String seq) {
138   //String chars =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";
139   int check = 0;
140
141   String index =  "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";
142   index += "--------------------------------------------------------------------------------------------------------------------------------";
143
144   for(int i = 0; i < seq.length(); i++) {
145     try {
146       if (i <seq.length()) {
147         int pos = index.indexOf(seq.substring(i,i+1));
148         if (!index.substring(pos,pos+1).equals("_")) {
149           check += ((i % 57) + 1) * pos;
150         }
151       }
152     } catch (Exception e) {
153       System.err.println("Exception during MSF Checksum calculation");
154       e.printStackTrace();
155     }
156   }
157   return check % 10000;
158 }
159
160 public static String print(SequenceI[] s) {
161   StringBuffer out = new StringBuffer("PileUp\n\n");
162
163   int max = 0;
164   int maxid = 0;
165
166   int i = 0;
167   String big = "";
168   while (i < s.length && s[i] != null) {
169     big += s[i].getSequence();
170     i++;
171   }
172   i = 0;
173   int bigcheck = checkSum(big);
174
175   out.append("   MSF: " + s[0].getSequence().length() + "   Type: P    Check:  " + bigcheck + "   ..\n\n\n");
176
177   while (i < s.length && s[i] != null) {
178     String seq = s[i].getSequence();
179     String name =  s[i].getName()+ "/" + s[i].getStart() + "-" + s[i].getEnd();
180     int check = checkSum(s[i].getSequence());
181     out.append(" Name: " + name + " oo  Len:  " + s[i].getSequence().length() + "  Check:  " + check + "  Weight:  1.00\n");
182     if (seq.length() > max) {
183       max = seq.length();
184     }
185     if (name.length() > maxid) {
186       maxid = name.length();
187     }
188     i++;
189   }
190
191   if (maxid < 10) {
192     maxid = 10;
193   }
194   maxid++;
195   out.append( "\n\n//\n\n");
196
197   int len = 50;
198
199   int nochunks =  max / len + 1;
200   if (max%len == 0) {
201     nochunks--;
202   }
203   for (i = 0; i < nochunks; i++) {
204     int j = 0;
205     while (j < s.length && s[j] != null) {
206       String name =  s[j].getName();
207       out.append( new Format("%-" + maxid + "s").form(name + "/" + s[j].getStart() + "-" + s[j].getEnd()) + " ");
208       for (int k = 0; k < 5; k++) {
209
210         int start = i*50 + k*10;
211         int end = start + 10;
212
213         if (end < s[j].getSequence().length() && start < s[j].getSequence().length() ) {
214           out.append(s[j].getSequence().substring(start,end));
215           if (k < 4) {
216             out.append(" ");
217           } else {
218             out.append("\n");
219           }
220         } else {
221           if (start < s[j].getSequence().length()) {
222             out.append(s[j].getSequence().substring(start));
223             out.append("\n");
224           } else {
225             if (k == 0) {
226               out.append("\n");
227             }
228           }
229         }
230       }
231       j++;
232     }
233     out.append("\n");
234
235   }
236   return out.toString();
237 }
238 public String print() {
239   return print(getSeqsAsArray());
240 }
241 }
242
243
244
245
246
247
248