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.HashMap;
31 import java.util.StringTokenizer;
32 import java.util.Vector;
34 public class ClustalFile extends AlignFile
41 public ClustalFile(String inFile, DataSourceType sourceType)
44 super(inFile, sourceType);
47 public ClustalFile(FileParse source) throws IOException
53 public void initData()
59 public void parse() throws IOException
64 StringBuffer pssecstr = new StringBuffer();
65 StringBuffer consstr = new StringBuffer();
66 Vector<String> headers = new Vector<>();
67 Map<String, StringBuffer> seqhash = new HashMap<>();
74 while ((line = nextLine()) != null)
76 if (line.length() == 0)
80 boolean isConservation = line.startsWith(SPACE)
81 || line.startsWith(TAB);
84 str = new StringTokenizer(line);
86 if (str.hasMoreTokens())
90 if (id.equalsIgnoreCase("CLUSTAL"))
98 if (seqhash.containsKey(id))
100 tempseq = seqhash.get(id);
104 tempseq = new StringBuffer();
105 seqhash.put(id, tempseq);
108 if (!(headers.contains(id)))
110 headers.addElement(id);
113 if (str.hasMoreTokens())
115 tempseq.append(str.nextToken());
128 if (line.matches("\\s+(-|\\.|\\(|\\[|\\]|\\))+"))
132 pssecstr.append(line.trim());
136 consstr.append(line.trim());
141 } catch (IOException e)
143 jalview.bin.Console.errPrintln("Exception parsing clustal file " + e);
149 this.noSeqs = headers.size();
151 // Add sequences to the hash
152 for (i = 0; i < headers.size(); i++)
154 if (seqhash.get(headers.elementAt(i)) != null)
156 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
159 maxLength = seqhash.get(headers.elementAt(i)).toString()
163 Sequence newSeq = parseId(headers.elementAt(i).toString());
165 seqhash.get(headers.elementAt(i).toString()).toString());
167 seqs.addElement(newSeq);
171 jalview.bin.Console.errPrintln(
172 "Clustal File Reader: Can't find sequence for "
173 + headers.elementAt(i));
176 AlignmentAnnotation lastssa = null;
177 if (pssecstr.length() == maxLength)
179 Vector<AlignmentAnnotation> ss = new Vector<>();
180 AlignmentAnnotation ssa = lastssa = StockholmFile
181 .parseAnnotationRow(ss, "secondary structure",
182 pssecstr.toString());
183 ssa.label = "Secondary Structure";
184 annotations.addElement(ssa);
186 if (consstr.length() == maxLength)
188 Vector<AlignmentAnnotation> ss = new Vector<>();
189 AlignmentAnnotation ssa = StockholmFile.parseAnnotationRow(ss,
190 "secondary structure", consstr.toString());
191 ssa.label = "Consensus Secondary Structure";
192 if (lastssa == null || !lastssa.getRNAStruc()
193 .equals(ssa.getRNAStruc().replace('-', '.')))
195 annotations.addElement(ssa);
202 public String print(SequenceI[] s, boolean jvsuffix)
204 StringBuffer out = new StringBuffer("CLUSTAL" + newline + newline);
211 while ((i < s.length) && (s[i] != null))
213 String tmp = printId(s[i], jvsuffix);
215 max = Math.max(max, s[i].getLength());
217 if (tmp.length() > maxid)
219 maxid = tmp.length();
233 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
235 for (i = 0; i < nochunks; i++)
239 while ((j < s.length) && (s[j] != null))
241 out.append(new Format("%-" + maxid + "s")
242 .form(printId(s[j], jvsuffix) + " "));
244 int chunkStart = i * len;
245 int chunkEnd = chunkStart + len;
247 int length = s[j].getLength();
248 if ((chunkEnd < length) && (chunkStart < length))
250 out.append(s[j].getSequenceAsString(chunkStart, chunkEnd));
254 if (chunkStart < length)
256 out.append(s[j].getSequenceAsString().substring(chunkStart));
267 return out.toString();