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 System.err.println("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 System.err.println("Clustal File Reader: Can't find sequence for "
172 + headers.elementAt(i));
175 AlignmentAnnotation lastssa = null;
176 if (pssecstr.length() == maxLength)
178 Vector<AlignmentAnnotation> ss = new Vector<>();
179 AlignmentAnnotation ssa = lastssa = StockholmFile
180 .parseAnnotationRow(ss, "secondary structure",
181 pssecstr.toString());
182 ssa.label = "Secondary Structure";
183 annotations.addElement(ssa);
185 if (consstr.length() == maxLength)
187 Vector<AlignmentAnnotation> ss = new Vector<>();
188 AlignmentAnnotation ssa = StockholmFile.parseAnnotationRow(ss,
189 "secondary structure", consstr.toString());
190 ssa.label = "Consensus Secondary Structure";
191 if (lastssa == null || !lastssa.getRNAStruc()
192 .equals(ssa.getRNAStruc().replace('-', '.')))
194 annotations.addElement(ssa);
201 public String print(SequenceI[] s, boolean jvsuffix)
203 StringBuffer out = new StringBuffer("CLUSTAL" + newline + newline);
210 while ((i < s.length) && (s[i] != null))
212 String tmp = printId(s[i], jvsuffix);
214 max = Math.max(max, s[i].getLength());
216 if (tmp.length() > maxid)
218 maxid = tmp.length();
232 int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
234 for (i = 0; i < nochunks; i++)
238 while ((j < s.length) && (s[j] != null))
240 out.append(new Format("%-" + maxid + "s")
241 .form(printId(s[j], jvsuffix) + " "));
243 int chunkStart = i * len;
244 int chunkEnd = chunkStart + len;
246 int length = s[j].getLength();
247 if ((chunkEnd < length) && (chunkStart < length))
249 out.append(s[j].getSequenceAsString(chunkStart, chunkEnd));
253 if (chunkStart < length)
255 out.append(s[j].getSequenceAsString().substring(chunkStart));
266 return out.toString();