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