2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
22 import javax.xml.parsers.ParserConfigurationException;
24 import org.xml.sax.SAXException;
26 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
27 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
28 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
29 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
31 import jalview.datamodel.*;
39 public class FastaFile extends AlignFile
42 * Length of a sequence line
49 * Creates a new FastaFile object.
56 * Creates a new FastaFile object.
65 * @throws SAXException
66 * @throws ParserConfigurationException
67 * @throws ExceptionFileFormatOrSyntax
68 * @throws ExceptionLoadingFailed
69 * @throws ExceptionPermissionDenied
70 * @throws InterruptedException
71 * @throws ExceptionUnmatchedClosingParentheses
73 public FastaFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
78 public FastaFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
89 public void parse() throws IOException
91 StringBuffer sb = new StringBuffer();
92 boolean firstLine = true;
97 boolean annotation = false;
99 while ((line = nextLine()) != null)
102 if (line.length() > 0)
104 if (line.charAt(0) == '>')
106 if (line.startsWith(">#_"))
110 Annotation[] anots = new Annotation[sb.length()];
111 String anotString = sb.toString();
112 for (int i = 0; i < sb.length(); i++)
114 anots[i] = new Annotation(anotString.substring(i, i + 1),
117 AlignmentAnnotation aa = new AlignmentAnnotation(seq
118 .getName().substring(2), seq.getDescription(), anots);
120 annotations.addElement(aa);
130 seq.setSequence(sb.toString());
134 seqs.addElement(seq);
138 seq = parseId(line.substring(1));
141 sb = new StringBuffer();
143 if (line.startsWith(">#_"))
157 Annotation[] anots = new Annotation[sb.length()];
158 String anotString = sb.toString();
159 for (int i = 0; i < sb.length(); i++)
161 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
164 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
165 .substring(2), seq.getDescription(), anots);
167 annotations.addElement(aa);
172 seq.setSequence(sb.toString());
173 seqs.addElement(seq);
178 * called by AppletFormatAdapter to generate an annotated alignment, rather
179 * than bare sequences.
183 public void addAnnotations(Alignment al)
186 for (int i = 0; i < annotations.size(); i++)
188 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
190 aa.setPadGaps(true, al.getGapCharacter());
191 al.addAnnotation(aa);
207 * @return DOCUMENT ME!
209 public String print(SequenceI[] s)
211 out = new StringBuffer();
214 while ((i < s.length) && (s[i] != null))
216 out.append(">" + printId(s[i]));
217 if (s[i].getDescription() != null)
219 out.append(" " + s[i].getDescription());
224 int nochunks = (s[i].getLength() / len) + 1;
226 for (int j = 0; j < nochunks; j++)
229 int end = start + len;
231 if (end < s[i].getLength())
233 out.append(s[i].getSequenceAsString(start, end) + newline);
235 else if (start < s[i].getLength())
237 out.append(s[i].getSequenceAsString(start, s[i].getLength())
245 return out.toString();
251 * @return DOCUMENT ME!
253 public String print()
255 return print(getSeqsAsArray());