2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
24 import jalview.datamodel.*;
25 import jalview.util.*;
27 public class ClustalFile
35 public ClustalFile(String inFile, String type)
41 public void initData()
46 public void parse() throws IOException
51 Vector headers = new Vector();
52 Hashtable seqhash = new Hashtable();
59 while ( (line = nextLine()) != null)
61 if (line.indexOf(" ") != 0)
63 str = new StringTokenizer(line, " ");
65 if (str.hasMoreTokens())
69 if (id.equalsIgnoreCase("CLUSTAL"))
77 if (seqhash.containsKey(id))
79 tempseq = (StringBuffer) seqhash.get(id);
83 tempseq = new StringBuffer();
84 seqhash.put(id, tempseq);
87 if (! (headers.contains(id)))
89 headers.addElement(id);
92 if (str.hasMoreTokens())
94 tempseq.append(str.nextToken());
104 catch (IOException e)
106 System.err.println("Exception parsing clustal file " + e);
112 this.noSeqs = headers.size();
114 //Add sequences to the hash
115 for (i = 0; i < headers.size(); i++)
117 if (seqhash.get(headers.elementAt(i)) != null)
119 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
122 maxLength = seqhash.get(headers.elementAt(i)).toString()
126 Sequence newSeq = parseId(headers.elementAt(i).toString());
127 newSeq.setSequence( seqhash.get(headers.elementAt(i).toString()).toString() );
129 if (!isValidProteinSequence(newSeq.getSequence()))
131 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
132 + " : " + newSeq.getName()
133 + " : " + invalidCharacter);
137 seqs.addElement(newSeq);
142 "Clustal File Reader: Can't find sequence for " +
143 headers.elementAt(i));
149 public String print()
151 return print(getSeqsAsArray());
154 public String print(SequenceI[] s)
156 StringBuffer out = new StringBuffer("CLUSTAL\n\n");
163 while ( (i < s.length) && (s[i] != null))
165 String tmp = printId(s[i]);
167 if (s[i].getSequence().length() > max)
169 max = s[i].getSequence().length();
172 if (tmp.length() > maxid)
174 maxid = tmp.length();
188 int nochunks = (max / len) + 1;
190 for (i = 0; i < nochunks; i++)
194 while ( (j < s.length) && (s[j] != null))
196 out.append(new Format("%-" + maxid + "s").form( printId(s[j]) + " "));
199 int end = start + len;
201 if ( (end < s[j].getSequence().length()) &&
202 (start < s[j].getSequence().length()))
204 out.append(s[j].getSequence().substring(start, end));
208 if (start < s[j].getSequence().length())
210 out.append(s[j].getSequence().substring(start));
221 return out.toString();