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/>.
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;
30 import jalview.datamodel.*;
38 public class FastaFile extends AlignFile
41 * Length of a sequence line
48 * Creates a new FastaFile object.
55 * Creates a new FastaFile object.
64 * @throws SAXException
65 * @throws ParserConfigurationException
66 * @throws ExceptionFileFormatOrSyntax
67 * @throws ExceptionLoadingFailed
68 * @throws ExceptionPermissionDenied
69 * @throws InterruptedException
71 public FastaFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
76 public FastaFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
87 public void parse() throws IOException
89 StringBuffer sb = new StringBuffer();
90 boolean firstLine = true;
95 boolean annotation = false;
97 while ((line = nextLine()) != null)
100 if (line.length() > 0)
102 if (line.charAt(0) == '>')
104 if (line.startsWith(">#_"))
108 Annotation[] anots = new Annotation[sb.length()];
109 String anotString = sb.toString();
110 for (int i = 0; i < sb.length(); i++)
112 anots[i] = new Annotation(anotString.substring(i, i + 1),
115 AlignmentAnnotation aa = new AlignmentAnnotation(seq
116 .getName().substring(2), seq.getDescription(), anots);
118 annotations.addElement(aa);
128 seq.setSequence(sb.toString());
132 seqs.addElement(seq);
136 seq = parseId(line.substring(1));
139 sb = new StringBuffer();
141 if (line.startsWith(">#_"))
155 Annotation[] anots = new Annotation[sb.length()];
156 String anotString = sb.toString();
157 for (int i = 0; i < sb.length(); i++)
159 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
162 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
163 .substring(2), seq.getDescription(), anots);
165 annotations.addElement(aa);
170 seq.setSequence(sb.toString());
171 seqs.addElement(seq);
176 * called by AppletFormatAdapter to generate an annotated alignment, rather
177 * than bare sequences.
181 public void addAnnotations(Alignment al)
184 for (int i = 0; i < annotations.size(); i++)
186 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
188 aa.setPadGaps(true, al.getGapCharacter());
189 al.addAnnotation(aa);
205 * @return DOCUMENT ME!
207 public String print(SequenceI[] s)
209 out = new StringBuffer();
212 while ((i < s.length) && (s[i] != null))
214 out.append(">" + printId(s[i]));
215 if (s[i].getDescription() != null)
217 out.append(" " + s[i].getDescription());
222 int nochunks = (s[i].getLength() / len) + 1;
224 for (int j = 0; j < nochunks; j++)
227 int end = start + len;
229 if (end < s[i].getLength())
231 out.append(s[i].getSequenceAsString(start, end) + newline);
233 else if (start < s[i].getLength())
235 out.append(s[i].getSequenceAsString(start, s[i].getLength())
243 return out.toString();
249 * @return DOCUMENT ME!
251 public String print()
253 return print(getSeqsAsArray());