2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import jalview.datamodel.*;
24 import jalview.util.*;
26 public class ClustalFile extends AlignFile
33 public ClustalFile(String inFile, String type) throws IOException
38 public ClustalFile(FileParse source) throws IOException
43 public void initData()
48 public void parse() throws IOException
53 Vector headers = new Vector();
54 Hashtable seqhash = new Hashtable();
61 while ((line = nextLine()) != null)
63 if (line.indexOf(" ") != 0)
65 str = new StringTokenizer(line, " ");
67 if (str.hasMoreTokens())
71 if (id.equalsIgnoreCase("CLUSTAL"))
79 if (seqhash.containsKey(id))
81 tempseq = (StringBuffer) seqhash.get(id);
85 tempseq = new StringBuffer();
86 seqhash.put(id, tempseq);
89 if (!(headers.contains(id)))
91 headers.addElement(id);
94 if (str.hasMoreTokens())
96 tempseq.append(str.nextToken());
107 } catch (IOException e)
109 System.err.println("Exception parsing clustal file " + e);
115 this.noSeqs = headers.size();
117 // Add sequences to the hash
118 for (i = 0; i < headers.size(); i++)
120 if (seqhash.get(headers.elementAt(i)) != null)
122 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
125 maxLength = seqhash.get(headers.elementAt(i)).toString()
129 Sequence newSeq = parseId(headers.elementAt(i).toString());
130 newSeq.setSequence(seqhash.get(headers.elementAt(i).toString())
133 seqs.addElement(newSeq);
138 .println("Clustal File Reader: Can't find sequence for "
139 + headers.elementAt(i));
145 public String print()
147 return print(getSeqsAsArray());
150 public String print(SequenceI[] s)
152 StringBuffer out = new StringBuffer("CLUSTAL"+newline+newline);
159 while ((i < s.length) && (s[i] != null))
161 String tmp = printId(s[i]);
163 if (s[i].getSequence().length > max)
165 max = s[i].getSequence().length;
168 if (tmp.length() > maxid)
170 maxid = tmp.length();
184 int nochunks = (max / len) + 1;
186 for (i = 0; i < nochunks; i++)
190 while ((j < s.length) && (s[j] != null))
192 out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " "));
195 int end = start + len;
197 if ((end < s[j].getSequence().length)
198 && (start < s[j].getSequence().length))
200 out.append(s[j].getSequenceAsString(start, end));
204 if (start < s[j].getSequence().length)
206 out.append(s[j].getSequenceAsString().substring(start));
217 return out.toString();