2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import javax.xml.parsers.ParserConfigurationException;
25 import org.xml.sax.SAXException;
27 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
28 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
29 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
30 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
32 import jalview.datamodel.*;
33 import jalview.util.*;
35 public class ClustalFile extends AlignFile
42 public ClustalFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
47 public ClustalFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
52 public void initData()
57 public void parse() throws IOException
63 StringBuffer pssecstr=new StringBuffer(),consstr=new StringBuffer();
64 Vector headers = new Vector();
65 Hashtable seqhash = new Hashtable();
72 while ((line = nextLine()) != null)
78 if (line.indexOf(" ") != 0)
80 str = new StringTokenizer(line, " ");
82 if (str.hasMoreTokens())
86 if (id.equalsIgnoreCase("CLUSTAL"))
94 if (seqhash.containsKey(id))
96 tempseq = (StringBuffer) seqhash.get(id);
100 tempseq = new StringBuffer();
101 seqhash.put(id, tempseq);
104 if (!(headers.contains(id)))
106 headers.addElement(id);
109 if (str.hasMoreTokens())
111 tempseq.append(str.nextToken());
122 if (line.matches("\\s+(-|\\.|\\(|\\[|\\]|\\))+"))
126 pssecstr.append(line.trim());
128 consstr.append(line.trim());
133 } catch (IOException e)
135 System.err.println("Exception parsing clustal file " + e);
141 this.noSeqs = headers.size();
143 // Add sequences to the hash
144 for (i = 0; i < headers.size(); i++)
146 if (seqhash.get(headers.elementAt(i)) != null)
148 if (maxLength < seqhash.get(headers.elementAt(i)).toString()
151 maxLength = seqhash.get(headers.elementAt(i)).toString()
155 Sequence newSeq = parseId(headers.elementAt(i).toString());
156 newSeq.setSequence(seqhash.get(headers.elementAt(i).toString())
159 seqs.addElement(newSeq);
164 .println("Clustal File Reader: Can't find sequence for "
165 + headers.elementAt(i));
168 AlignmentAnnotation lastssa=null;
169 if (pssecstr.length()==maxLength)
171 Vector ss=new Vector();
172 AlignmentAnnotation ssa=lastssa=StockholmFile.parseAnnotationRow(ss, "secondary structure", pssecstr.toString());
173 ssa.label="Secondary Structure";
174 annotations.addElement(ssa);
176 if (consstr.length()==maxLength)
178 Vector ss=new Vector();
179 AlignmentAnnotation ssa=StockholmFile.parseAnnotationRow(ss, "secondary structure", consstr.toString());
180 ssa.label="Consensus Secondary Structure";
181 if (lastssa==null || !lastssa.getRNAStruc().equals(ssa.getRNAStruc().replace('-', '.')))
183 annotations.addElement(ssa);
188 public String print()
190 return print(getSeqsAsArray());
191 // TODO: locaRNA style aln output
194 public String print(SequenceI[] s)
196 StringBuffer out = new StringBuffer("CLUSTAL"+newline+newline);
203 while ((i < s.length) && (s[i] != null))
205 String tmp = printId(s[i]);
207 if (s[i].getSequence().length > max)
209 max = s[i].getSequence().length;
212 if (tmp.length() > maxid)
214 maxid = tmp.length();
228 int nochunks = (max / len) + 1;
230 for (i = 0; i < nochunks; i++)
234 while ((j < s.length) && (s[j] != null))
236 out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " "));
239 int end = start + len;
241 if ((end < s[j].getSequence().length)
242 && (start < s[j].getSequence().length))
244 out.append(s[j].getSequenceAsString(start, end));
248 if (start < s[j].getSequence().length)
250 out.append(s[j].getSequenceAsString().substring(start));
261 return out.toString();