2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceI;
29 import java.io.IOException;
37 public class FastaFile extends AlignFile
40 * Length of a sequence line
47 * Creates a new FastaFile object.
54 * Creates a new FastaFile object.
64 public FastaFile(String inFile, DataSourceType sourceType)
67 super(inFile, sourceType);
70 public FastaFile(FileParse source) throws IOException
82 public void parse() throws IOException
84 StringBuffer sb = new StringBuffer();
85 boolean firstLine = true;
90 boolean annotation = false;
92 while ((uline = nextLine()) != null)
95 if (line.length() > 0)
97 if (line.charAt(0) == '>')
99 if (line.startsWith(">#_"))
103 annotations.addElement(makeAnnotation(seq, sb));
113 seq.setSequence(sb.toString());
117 seqs.addElement(seq);
121 seq = parseId(line.substring(1));
124 sb = new StringBuffer();
126 if (line.startsWith(">#_"))
133 sb.append(annotation ? uline : line);
140 annotations.addElement(makeAnnotation(seq, sb));
145 seq.setSequence(sb.toString());
146 seqs.addElement(seq);
150 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
152 Annotation[] anots = new Annotation[sb.length()];
154 for (int i = 0; i < anots.length; i++)
156 char cn = sb.charAt(i);
159 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
162 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
163 .substring(2), seq.getDescription(), anots);
168 * called by AppletFormatAdapter to generate an annotated alignment, rather
169 * than bare sequences.
173 public void addAnnotations(Alignment al)
176 for (int i = 0; i < annotations.size(); i++)
178 AlignmentAnnotation aa = annotations
180 aa.setPadGaps(true, al.getGapCharacter());
181 al.addAnnotation(aa);
197 * @return DOCUMENT ME!
199 public String print(SequenceI[] s)
201 out = new StringBuffer();
204 while ((i < s.length) && (s[i] != null))
206 out.append(">" + printId(s[i]));
207 if (s[i].getDescription() != null)
209 out.append(" " + s[i].getDescription());
214 int nochunks = (s[i].getLength() / len)
215 + (s[i].getLength() % len > 0 ? 1 : 0);
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!
245 public String print()
247 return print(getSeqsAsArray());