2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import jalview.datamodel.*;
32 public class FastaFile extends AlignFile
35 * Length of a sequence line
42 * Creates a new FastaFile object.
49 * Creates a new FastaFile object.
59 public FastaFile(String inFile, String type) throws IOException
64 public FastaFile(FileParse source) throws IOException
75 public void parse() throws IOException
77 StringBuffer sb = new StringBuffer();
78 boolean firstLine = true;
83 boolean annotation = false;
85 while ((line = nextLine()) != null)
88 if (line.length() > 0)
90 if (line.charAt(0) == '>')
92 if (line.startsWith(">#_"))
96 Annotation[] anots = new Annotation[sb.length()];
97 String anotString = sb.toString();
98 for (int i = 0; i < sb.length(); i++)
100 anots[i] = new Annotation(anotString.substring(i, i + 1),
103 AlignmentAnnotation aa = new AlignmentAnnotation(seq
104 .getName().substring(2), seq.getDescription(), anots);
106 annotations.addElement(aa);
116 seq.setSequence(sb.toString());
120 seqs.addElement(seq);
124 seq = parseId(line.substring(1));
127 sb = new StringBuffer();
129 if (line.startsWith(">#_"))
143 Annotation[] anots = new Annotation[sb.length()];
144 String anotString = sb.toString();
145 for (int i = 0; i < sb.length(); i++)
147 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
150 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
151 .substring(2), seq.getDescription(), anots);
153 annotations.addElement(aa);
158 seq.setSequence(sb.toString());
159 seqs.addElement(seq);
164 * called by AppletFormatAdapter to generate an annotated alignment, rather
165 * than bare sequences.
169 public void addAnnotations(Alignment al)
172 for (int i = 0; i < annotations.size(); i++)
174 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
176 aa.setPadGaps(true, al.getGapCharacter());
177 al.addAnnotation(aa);
193 * @return DOCUMENT ME!
195 public String print(SequenceI[] s)
197 out = new StringBuffer();
200 while ((i < s.length) && (s[i] != null))
202 out.append(">" + printId(s[i]));
203 if (s[i].getDescription() != null)
205 out.append(" " + s[i].getDescription());
210 int nochunks = (s[i].getLength() / len) + 1;
212 for (int j = 0; j < nochunks; j++)
215 int end = start + len;
217 if (end < s[i].getLength())
219 out.append(s[i].getSequenceAsString(start, end) + newline);
221 else if (start < s[i].getLength())
223 out.append(s[i].getSequenceAsString(start, s[i].getLength())
231 return out.toString();
237 * @return DOCUMENT ME!
239 public String print()
241 return print(getSeqsAsArray());