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
22 import jalview.datamodel.*;
\r
23 import jalview.util.*;
\r
28 public class ClustalFile extends AlignFile {
\r
32 public ClustalFile()
\r
35 public ClustalFile(String inStr) {
\r
40 public void initData() {
\r
45 public ClustalFile(String inFile, String type) throws IOException {
\r
49 public void parse() {
\r
51 boolean flag = false;
\r
53 Vector headers = new Vector();
\r
54 Hashtable seqhash = new Hashtable();
\r
59 while ((line = nextLine()) != null) {
\r
60 if (line.indexOf(" ") != 0) {
\r
61 StringTokenizer str = new StringTokenizer(line," ");
\r
64 if (str.hasMoreTokens()) {
\r
65 id = str.nextToken();
\r
66 if (id.equals("CLUSTAL")) {
\r
70 StringBuffer tempseq;
\r
71 if (seqhash.containsKey(id)) {
\r
72 tempseq = (StringBuffer)seqhash.get(id);
\r
74 tempseq = new StringBuffer();
\r
75 seqhash.put(id,tempseq);
\r
78 if (!(headers.contains(id))) {
\r
79 headers.addElement(id);
\r
82 if(str.hasMoreTokens())
\r
83 tempseq.append(str.nextToken());
\r
89 } catch (IOException e) {
\r
90 System.err.println("Exception parsing clustal file " + e);
\r
91 e.printStackTrace();
\r
95 this.noSeqs = headers.size();
\r
97 //Add sequences to the hash
\r
98 for (i = 0; i < headers.size(); i++ ) {
\r
102 if ( seqhash.get(headers.elementAt(i)) != null) {
\r
103 if (maxLength < seqhash.get(headers.elementAt(i)).toString().length() ) {
\r
104 maxLength = seqhash.get(headers.elementAt(i)).toString().length();
\r
106 String head = headers.elementAt(i).toString();
\r
108 end = seqhash.get(headers.elementAt(i)).toString().length();
\r
110 if (head.indexOf("/") > 0 ) {
\r
111 StringTokenizer st = new StringTokenizer(head,"/");
\r
112 if (st.countTokens() == 2) {
\r
114 ids.addElement(st.nextToken());
\r
116 String tmp = st.nextToken();
\r
117 st = new StringTokenizer(tmp,"-");
\r
118 if (st.countTokens() == 2) {
\r
119 start = Integer.valueOf(st.nextToken()).intValue();
\r
120 end = Integer.valueOf(st.nextToken()).intValue();
\r
123 ids.addElement(headers.elementAt(i));
\r
126 ids.addElement(headers.elementAt(i));
\r
129 Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
\r
130 seqhash.get(headers.elementAt(i).toString()).toString(),start,end);
\r
132 seqs.addElement(newSeq);
\r
135 System.err.println("Clustal File Reader: Can't find sequence for " + headers.elementAt(i));
\r
142 public String print() {
\r
143 return print(getSeqsAsArray());
\r
145 public static String print(SequenceI[] s) {
\r
147 StringBuffer out = new StringBuffer("CLUSTAL\n\n");
\r
154 while (i < s.length && s[i] != null) {
\r
155 String tmp = s[i].getName() + "/" + s[i].getStart() + "-" + s[i].getEnd();
\r
157 if (s[i].getSequence().length() > max) {
\r
158 max = s[i].getSequence().length();
\r
160 if (tmp.length() > maxid) {
\r
161 maxid = tmp.length();
\r
171 int nochunks = max / len + 1;
\r
173 for (i = 0; i < nochunks; i++) {
\r
175 while ( j < s.length && s[j] != null) {
\r
176 out.append( new Format("%-" + maxid + "s").form(s[j].getName() + "/" + s[j].getStart() + "-" + s[j].getEnd()) + " ");
\r
178 int end = start + len;
\r
180 if (end < s[j].getSequence().length() && start < s[j].getSequence().length() )
\r
181 out.append(s[j].getSequence().substring(start,end));
\r
184 if (start < s[j].getSequence().length())
\r
185 out.append(s[j].getSequence().substring(start));
\r
194 return out.toString();
\r