2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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 extends AlignFile
34 public ClustalFile(String inFile, String type) throws IOException
39 public ClustalFile(FileParse source) throws IOException
44 public void initData()
49 public void parse() throws IOException
54 Vector headers = new Vector();
55 Hashtable seqhash = new Hashtable();
62 while ((line = nextLine()) != null)
64 if (line.indexOf(" ") != 0)
66 str = new StringTokenizer(line, " ");
68 if (str.hasMoreTokens())
72 if (id.equalsIgnoreCase("CLUSTAL"))
80 if (seqhash.containsKey(id))
82 tempseq = (StringBuffer) seqhash.get(id);
86 tempseq = new StringBuffer();
87 seqhash.put(id, tempseq);
90 if (!(headers.contains(id)))
92 headers.addElement(id);
95 if (str.hasMoreTokens())
97 tempseq.append(str.nextToken());
108 } catch (IOException e)
110 System.err.println("Exception parsing clustal file " + e);
116 this.noSeqs = headers.size();
118 // Add sequences to the hash
119 for (i = 0; i < headers.size(); i++)
121 if (seqhash.get(headers.elementAt(i)) != null)
123 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
126 maxLength = seqhash.get(headers.elementAt(i)).toString()
130 Sequence newSeq = parseId(headers.elementAt(i).toString());
131 newSeq.setSequence(seqhash.get(headers.elementAt(i).toString())
134 seqs.addElement(newSeq);
139 .println("Clustal File Reader: Can't find sequence for "
140 + headers.elementAt(i));
146 public String print()
148 return print(getSeqsAsArray());
151 public String print(SequenceI[] s)
153 StringBuffer out = new StringBuffer("CLUSTAL\n\n");
160 while ((i < s.length) && (s[i] != null))
162 String tmp = printId(s[i]);
164 if (s[i].getSequence().length > max)
166 max = s[i].getSequence().length;
169 if (tmp.length() > maxid)
171 maxid = tmp.length();
185 int nochunks = (max / len) + 1;
187 for (i = 0; i < nochunks; i++)
191 while ((j < s.length) && (s[j] != null))
194 .append(new Format("%-" + maxid + "s").form(printId(s[j])
198 int end = start + len;
200 if ((end < s[j].getSequence().length)
201 && (start < s[j].getSequence().length))
203 out.append(s[j].getSequenceAsString(start, end));
207 if (start < s[j].getSequence().length)
209 out.append(s[j].getSequenceAsString().substring(start));
220 return out.toString();