2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
24 import jalview.datamodel.*;
\r
25 import jalview.util.*;
\r
27 public class ClustalFile
\r
31 public ClustalFile()
\r
35 public ClustalFile(String inStr)
\r
40 public ClustalFile(String inFile, String type)
\r
43 super(inFile, type);
\r
46 public void initData()
\r
51 public void parse() throws IOException
\r
54 boolean flag = false;
\r
56 Vector headers = new Vector();
\r
57 Hashtable seqhash = new Hashtable();
\r
58 StringBuffer tempseq;
\r
60 StringTokenizer str;
\r
64 while ( (line = nextLine()) != null)
\r
66 if (line.indexOf(" ") != 0)
\r
68 str = new StringTokenizer(line, " ");
\r
70 if (str.hasMoreTokens())
\r
72 id = str.nextToken();
\r
74 if (id.equalsIgnoreCase("CLUSTAL"))
\r
82 if (seqhash.containsKey(id))
\r
84 tempseq = (StringBuffer) seqhash.get(id);
\r
88 tempseq = new StringBuffer();
\r
89 seqhash.put(id, tempseq);
\r
92 if (! (headers.contains(id)))
\r
94 headers.addElement(id);
\r
97 if (str.hasMoreTokens())
\r
99 tempseq.append(str.nextToken());
\r
109 catch (IOException e)
\r
111 System.err.println("Exception parsing clustal file " + e);
\r
112 e.printStackTrace();
\r
117 this.noSeqs = headers.size();
\r
119 //Add sequences to the hash
\r
120 for (i = 0; i < headers.size(); i++)
\r
122 if (seqhash.get(headers.elementAt(i)) != null)
\r
124 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
\r
127 maxLength = seqhash.get(headers.elementAt(i)).toString()
\r
131 Sequence newSeq = parseId(headers.elementAt(i).toString());
\r
132 newSeq.setSequence( seqhash.get(headers.elementAt(i).toString()).toString() );
\r
134 if (!isValidProteinSequence(newSeq.getSequence()))
\r
136 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
\r
137 + " : " + newSeq.getName()
\r
138 + " : " + invalidCharacter);
\r
142 seqs.addElement(newSeq);
\r
146 System.err.println(
\r
147 "Clustal File Reader: Can't find sequence for " +
\r
148 headers.elementAt(i));
\r
154 public String print()
\r
156 return print(getSeqsAsArray());
\r
159 public String print(SequenceI[] s)
\r
161 StringBuffer out = new StringBuffer("CLUSTAL\n\n");
\r
168 while ( (i < s.length) && (s[i] != null))
\r
170 String tmp = printId(s[i]);
\r
172 if (s[i].getSequence().length() > max)
\r
174 max = s[i].getSequence().length();
\r
177 if (tmp.length() > maxid)
\r
179 maxid = tmp.length();
\r
193 int nochunks = (max / len) + 1;
\r
195 for (i = 0; i < nochunks; i++)
\r
199 while ( (j < s.length) && (s[j] != null))
\r
201 out.append(new Format("%-" + maxid + "s").form( printId(s[j]) + " "));
\r
203 int start = i * len;
\r
204 int end = start + len;
\r
206 if ( (end < s[j].getSequence().length()) &&
\r
207 (start < s[j].getSequence().length()))
\r
209 out.append(s[j].getSequence().substring(start, end));
\r
213 if (start < s[j].getSequence().length())
\r
215 out.append(s[j].getSequence().substring(start));
\r
226 return out.toString();
\r