2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import jalview.datamodel.*;
31 public class FastaFile extends AlignFile
34 * Length of a sequence line
41 * Creates a new FastaFile object.
48 * Creates a new FastaFile object.
58 public FastaFile(String inFile, String type) throws IOException
63 public FastaFile(FileParse source) throws IOException
74 public void parse() throws IOException
76 StringBuffer sb = new StringBuffer();
77 boolean firstLine = true;
82 boolean annotation = false;
84 while ((line = nextLine()) != null)
87 if (line.length() > 0)
89 if (line.charAt(0) == '>')
91 if (line.startsWith(">#_"))
95 Annotation[] anots = new Annotation[sb.length()];
96 String anotString = sb.toString();
97 for (int i = 0; i < sb.length(); i++)
99 anots[i] = new Annotation(anotString.substring(i, i + 1),
102 AlignmentAnnotation aa = new AlignmentAnnotation(seq
103 .getName().substring(2), seq.getDescription(), anots);
105 annotations.addElement(aa);
115 seq.setSequence(sb.toString());
119 seqs.addElement(seq);
123 seq = parseId(line.substring(1));
126 sb = new StringBuffer();
128 if (line.startsWith(">#_"))
142 Annotation[] anots = new Annotation[sb.length()];
143 String anotString = sb.toString();
144 for (int i = 0; i < sb.length(); i++)
146 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
149 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
150 .substring(2), seq.getDescription(), anots);
152 annotations.addElement(aa);
157 seq.setSequence(sb.toString());
158 seqs.addElement(seq);
163 * called by AppletFormatAdapter to generate an annotated alignment, rather
164 * than bare sequences.
168 public void addAnnotations(Alignment al)
171 for (int i = 0; i < annotations.size(); i++)
173 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
175 aa.setPadGaps(true, al.getGapCharacter());
176 al.addAnnotation(aa);
192 * @return DOCUMENT ME!
194 public String print(SequenceI[] s)
196 out = new StringBuffer();
199 while ((i < s.length) && (s[i] != null))
201 out.append(">" + printId(s[i]));
202 if (s[i].getDescription() != null)
204 out.append(" " + s[i].getDescription());
209 int nochunks = (s[i].getLength() / len) + 1;
211 for (int j = 0; j < nochunks; j++)
214 int end = start + len;
216 if (end < s[i].getLength())
218 out.append(s[i].getSequenceAsString(start, end) + "\n");
220 else if (start < s[i].getLength())
222 out.append(s[i].getSequenceAsString(start, s[i].getLength())
230 return out.toString();
236 * @return DOCUMENT ME!
238 public String print()
240 return print(getSeqsAsArray());