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.
64 public FastaFile(String inFile, String type) throws Exception
69 public FastaFile(FileParse source) throws Exception
80 public void parse() throws IOException
82 StringBuffer sb = new StringBuffer();
83 boolean firstLine = true;
88 boolean annotation = false;
90 while ((line = nextLine()) != null)
93 if (line.length() > 0)
95 if (line.charAt(0) == '>')
97 if (line.startsWith(">#_"))
101 Annotation[] anots = new Annotation[sb.length()];
102 String anotString = sb.toString();
103 for (int i = 0; i < sb.length(); i++)
105 anots[i] = new Annotation(anotString.substring(i, i + 1),
108 AlignmentAnnotation aa = new AlignmentAnnotation(seq
109 .getName().substring(2), seq.getDescription(), anots);
111 annotations.addElement(aa);
121 seq.setSequence(sb.toString());
125 seqs.addElement(seq);
129 seq = parseId(line.substring(1));
132 sb = new StringBuffer();
134 if (line.startsWith(">#_"))
148 Annotation[] anots = new Annotation[sb.length()];
149 String anotString = sb.toString();
150 for (int i = 0; i < sb.length(); i++)
152 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
155 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
156 .substring(2), seq.getDescription(), anots);
158 annotations.addElement(aa);
163 seq.setSequence(sb.toString());
164 seqs.addElement(seq);
169 * called by AppletFormatAdapter to generate an annotated alignment, rather
170 * than bare sequences.
174 public void addAnnotations(Alignment al)
177 for (int i = 0; i < annotations.size(); i++)
179 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
181 aa.setPadGaps(true, al.getGapCharacter());
182 al.addAnnotation(aa);
198 * @return DOCUMENT ME!
200 public String print(SequenceI[] s)
202 out = new StringBuffer();
205 while ((i < s.length) && (s[i] != null))
207 out.append(">" + printId(s[i]));
208 if (s[i].getDescription() != null)
210 out.append(" " + s[i].getDescription());
215 int nochunks = (s[i].getLength() / len) + 1;
217 for (int j = 0; j < nochunks; j++)
220 int end = start + len;
222 if (end < s[i].getLength())
224 out.append(s[i].getSequenceAsString(start, end) + newline);
226 else if (start < s[i].getLength())
228 out.append(s[i].getSequenceAsString(start, s[i].getLength())
236 return out.toString();
242 * @return DOCUMENT ME!
244 public String print()
246 return print(getSeqsAsArray());