2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.Format;
28 import java.io.IOException;
29 import java.util.Hashtable;
30 import java.util.StringTokenizer;
31 import java.util.Vector;
33 public class ClustalFile extends AlignFile
40 public ClustalFile(String inFile, DataSourceType sourceType)
43 super(inFile, sourceType);
46 public ClustalFile(FileParse source) throws IOException
52 public void initData()
58 public void parse() throws IOException
64 StringBuffer pssecstr = new StringBuffer(),
65 consstr = new StringBuffer();
66 Vector headers = new Vector();
67 Hashtable seqhash = new Hashtable();
74 while ((line = nextLine()) != null)
76 if (line.length() == 0)
80 if (line.indexOf(" ") != 0)
82 str = new StringTokenizer(line, " ");
84 if (str.hasMoreTokens())
88 if (id.equalsIgnoreCase("CLUSTAL"))
96 if (seqhash.containsKey(id))
98 tempseq = (StringBuffer) seqhash.get(id);
102 tempseq = new StringBuffer();
103 seqhash.put(id, tempseq);
106 if (!(headers.contains(id)))
108 headers.addElement(id);
111 if (str.hasMoreTokens())
113 tempseq.append(str.nextToken());
126 if (line.matches("\\s+(-|\\.|\\(|\\[|\\]|\\))+"))
130 pssecstr.append(line.trim());
134 consstr.append(line.trim());
139 } catch (IOException e)
141 System.err.println("Exception parsing clustal file " + e);
147 this.noSeqs = headers.size();
149 // Add sequences to the hash
150 for (i = 0; i < headers.size(); i++)
152 if (seqhash.get(headers.elementAt(i)) != null)
154 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
157 maxLength = seqhash.get(headers.elementAt(i)).toString()
161 Sequence newSeq = parseId(headers.elementAt(i).toString());
163 seqhash.get(headers.elementAt(i).toString()).toString());
165 seqs.addElement(newSeq);
169 System.err.println("Clustal File Reader: Can't find sequence for "
170 + headers.elementAt(i));
173 AlignmentAnnotation lastssa = null;
174 if (pssecstr.length() == maxLength)
176 Vector ss = new Vector();
177 AlignmentAnnotation ssa = lastssa = StockholmFile
178 .parseAnnotationRow(ss, "secondary structure",
179 pssecstr.toString());
180 ssa.label = "Secondary Structure";
181 annotations.addElement(ssa);
183 if (consstr.length() == maxLength)
185 Vector ss = new Vector();
186 AlignmentAnnotation ssa = StockholmFile.parseAnnotationRow(ss,
187 "secondary structure", consstr.toString());
188 ssa.label = "Consensus Secondary Structure";
189 if (lastssa == null || !lastssa.getRNAStruc()
190 .equals(ssa.getRNAStruc().replace('-', '.')))
192 annotations.addElement(ssa);
199 public String print(SequenceI[] s, boolean jvsuffix)
201 StringBuffer out = new StringBuffer("CLUSTAL" + newline + newline);
208 while ((i < s.length) && (s[i] != null))
210 String tmp = printId(s[i], jvsuffix);
212 max = Math.max(max, s[i].getLength());
214 if (tmp.length() > maxid)
216 maxid = tmp.length();
230 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
232 for (i = 0; i < nochunks; i++)
236 while ((j < s.length) && (s[j] != null))
238 out.append(new Format("%-" + maxid + "s")
239 .form(printId(s[j], jvsuffix) + " "));
242 int end = start + len;
244 int length = s[j].getLength();
245 if ((end < length) && (start < length))
247 out.append(s[j].getSequenceAsString(start, end));
253 out.append(s[j].getSequenceAsString().substring(start));
264 return out.toString();