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!)
16 import jalview.datamodel.*;
17 import jalview.util.*;
23 public class PileUpfile
30 public PileUpfile(String inStr) {
34 public PileUpfile(String inFile, String type) throws IOException {
41 boolean seqFlag = false;
42 String key = new String();
43 Vector headers = new Vector();
44 Hashtable seqhash = new Hashtable();
48 while ((line = nextLine()) != null) {
50 StringTokenizer str = new StringTokenizer(line);
52 while (str.hasMoreTokens()) {
54 String inStr = str.nextToken();
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);
62 //if line has // set SeqFlag to 1 so we know sequences are coming
63 if (inStr.indexOf("//") != -1) {
67 //Process lines as sequence lines if seqFlag is set
68 if (( inStr.indexOf("//") == -1) && (seqFlag == true)) {
69 //seqeunce id is the first field
73 //Get sequence from hash if it exists
74 if (seqhash.containsKey(key)) {
75 tempseq = (StringBuffer)seqhash.get(key);
77 tempseq = new StringBuffer();
78 seqhash.put(key,tempseq);
81 //loop through the rest of the words
82 while (str.hasMoreTokens()) {
83 //append the word to the sequence
84 tempseq.append(str.nextToken());
89 } catch (IOException e) {
90 System.err.println("Exception parsing PileUpfile " + e);
94 this.noSeqs = headers.size();
96 //Add sequences to the hash
97 for (i = 0; i < headers.size(); i++ ) {
99 if ( seqhash.get(headers.elementAt(i)) != null) {
100 String head = headers.elementAt(i).toString();
101 String seq = seqhash.get(head).toString();
104 int end = seq.length();
106 if (maxLength < head.length() ) {
107 maxLength = head.length();
110 if (head.indexOf("/") > 0 ) {
112 StringTokenizer st = new StringTokenizer(head,"/");
114 if (st.countTokens() == 2) {
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();
126 Sequence newSeq = new Sequence(head,seq,start,end);
128 seqs.addElement(newSeq);
131 System.err.println("PileUpfile Parser: Can't find sequence for " + headers.elementAt(i));
137 public static int checkSum(String seq) {
138 //String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.*~&@";
141 String index = "--------------------------------------&---*---.-----------------@ABCDEFGHIJKLMNOPQRSTUVWXYZ------ABCDEFGHIJKLMNOPQRSTUVWXYZ----@";
142 index += "--------------------------------------------------------------------------------------------------------------------------------";
144 for(int i = 0; i < seq.length(); i++) {
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;
152 } catch (Exception e) {
153 System.err.println("Exception during MSF Checksum calculation");
157 return check % 10000;
160 public static String print(SequenceI[] s) {
161 StringBuffer out = new StringBuffer("PileUp\n\n");
168 while (i < s.length && s[i] != null) {
169 big += s[i].getSequence();
173 int bigcheck = checkSum(big);
175 out.append(" MSF: " + s[0].getSequence().length() + " Type: P Check: " + bigcheck + " ..\n\n\n");
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) {
185 if (name.length() > maxid) {
186 maxid = name.length();
195 out.append( "\n\n//\n\n");
199 int nochunks = max / len + 1;
203 for (i = 0; i < nochunks; i++) {
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++) {
210 int start = i*50 + k*10;
211 int end = start + 10;
213 if (end < s[j].getSequence().length() && start < s[j].getSequence().length() ) {
214 out.append(s[j].getSequence().substring(start,end));
221 if (start < s[j].getSequence().length()) {
222 out.append(s[j].getSequence().substring(start));
236 return out.toString();
238 public String print() {
239 return print(getSeqsAsArray());