2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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)
40 public ClustalFile(FileParse source) throws IOException
44 public void initData()
55 Vector headers = new Vector();
56 Hashtable seqhash = new Hashtable();
63 while ( (line = nextLine()) != null)
65 if (line.indexOf(" ") != 0)
67 str = new StringTokenizer(line, " ");
69 if (str.hasMoreTokens())
73 if (id.equalsIgnoreCase("CLUSTAL"))
81 if (seqhash.containsKey(id))
83 tempseq = (StringBuffer) seqhash.get(id);
87 tempseq = new StringBuffer();
88 seqhash.put(id, tempseq);
91 if (! (headers.contains(id)))
93 headers.addElement(id);
96 if (str.hasMoreTokens())
98 tempseq.append(str.nextToken());
110 catch (IOException e)
112 System.err.println("Exception parsing clustal file " + e);
118 this.noSeqs = headers.size();
120 //Add sequences to the hash
121 for (i = 0; i < headers.size(); i++)
123 if (seqhash.get(headers.elementAt(i)) != null)
125 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
128 maxLength = seqhash.get(headers.elementAt(i)).toString()
132 Sequence newSeq = parseId(headers.elementAt(i).toString());
133 newSeq.setSequence(seqhash.get(headers.elementAt(i).toString()).
136 seqs.addElement(newSeq);
141 "Clustal File Reader: Can't find sequence for " +
142 headers.elementAt(i));
148 public String print()
150 return print(getSeqsAsArray());
153 public String print(SequenceI[] s)
155 StringBuffer out = new StringBuffer("CLUSTAL\n\n");
162 while ( (i < s.length) && (s[i] != null))
164 String tmp = printId(s[i]);
166 if (s[i].getSequence().length > max)
168 max = s[i].getSequence().length;
171 if (tmp.length() > maxid)
173 maxid = tmp.length();
187 int nochunks = (max / len) + 1;
189 for (i = 0; i < nochunks; i++)
193 while ( (j < s.length) && (s[j] != null))
195 out.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();