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
70 public FastaFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
75 public FastaFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed
86 public void parse() throws IOException
88 StringBuffer sb = new StringBuffer();
89 boolean firstLine = true;
94 boolean annotation = false;
96 while ((line = nextLine()) != null)
99 if (line.length() > 0)
101 if (line.charAt(0) == '>')
103 if (line.startsWith(">#_"))
107 Annotation[] anots = new Annotation[sb.length()];
108 String anotString = sb.toString();
109 for (int i = 0; i < sb.length(); i++)
111 anots[i] = new Annotation(anotString.substring(i, i + 1),
114 AlignmentAnnotation aa = new AlignmentAnnotation(seq
115 .getName().substring(2), seq.getDescription(), anots);
117 annotations.addElement(aa);
127 seq.setSequence(sb.toString());
131 seqs.addElement(seq);
135 seq = parseId(line.substring(1));
138 sb = new StringBuffer();
140 if (line.startsWith(">#_"))
154 Annotation[] anots = new Annotation[sb.length()];
155 String anotString = sb.toString();
156 for (int i = 0; i < sb.length(); i++)
158 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
161 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
162 .substring(2), seq.getDescription(), anots);
164 annotations.addElement(aa);
169 seq.setSequence(sb.toString());
170 seqs.addElement(seq);
175 * called by AppletFormatAdapter to generate an annotated alignment, rather
176 * than bare sequences.
180 public void addAnnotations(Alignment al)
183 for (int i = 0; i < annotations.size(); i++)
185 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
187 aa.setPadGaps(true, al.getGapCharacter());
188 al.addAnnotation(aa);
204 * @return DOCUMENT ME!
206 public String print(SequenceI[] s)
208 out = new StringBuffer();
211 while ((i < s.length) && (s[i] != null))
213 out.append(">" + printId(s[i]));
214 if (s[i].getDescription() != null)
216 out.append(" " + s[i].getDescription());
221 int nochunks = (s[i].getLength() / len) + 1;
223 for (int j = 0; j < nochunks; j++)
226 int end = start + len;
228 if (end < s[i].getLength())
230 out.append(s[i].getSequenceAsString(start, end) + newline);
232 else if (start < s[i].getLength())
234 out.append(s[i].getSequenceAsString(start, s[i].getLength())
242 return out.toString();
248 * @return DOCUMENT ME!
250 public String print()
252 return print(getSeqsAsArray());