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, String type) throws IOException
69 public FastaFile(FileParse source) throws IOException
80 public void parse() throws IOException
82 StringBuffer sb = new StringBuffer();
83 boolean firstLine = true;
88 boolean annotation = false;
90 while ((uline = nextLine()) != null)
93 if (line.length() > 0)
95 if (line.charAt(0) == '>')
97 if (line.startsWith(">#_"))
101 annotations.addElement(makeAnnotation(seq, sb));
111 seq.setSequence(sb.toString());
115 seqs.addElement(seq);
119 seq = parseId(line.substring(1));
122 sb = new StringBuffer();
124 if (line.startsWith(">#_"))
131 sb.append(annotation ? uline : line);
138 annotations.addElement(makeAnnotation(seq, sb));
143 seq.setSequence(sb.toString());
144 seqs.addElement(seq);
148 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
150 Annotation[] anots = new Annotation[sb.length()];
152 for (int i = 0; i < anots.length; i++)
154 char cn = sb.charAt(i);
157 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
160 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
161 .substring(2), seq.getDescription(), anots);
166 * called by AppletFormatAdapter to generate an annotated alignment, rather
167 * than bare sequences.
171 public void addAnnotations(Alignment al)
174 for (int i = 0; i < annotations.size(); i++)
176 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
178 aa.setPadGaps(true, al.getGapCharacter());
179 al.addAnnotation(aa);
195 * @return DOCUMENT ME!
197 public String print(SequenceI[] s)
199 out = new StringBuffer();
202 while ((i < s.length) && (s[i] != null))
204 out.append(">" + printId(s[i]));
205 if (s[i].getDescription() != null)
207 out.append(" " + s[i].getDescription());
212 int nochunks = (s[i].getLength() / len) + 1;
214 for (int j = 0; j < nochunks; j++)
217 int end = start + len;
219 if (end < s[i].getLength())
221 out.append(s[i].getSequenceAsString(start, end) + newline);
223 else if (start < s[i].getLength())
225 out.append(s[i].getSequenceAsString(start, s[i].getLength())
233 return out.toString();
239 * @return DOCUMENT ME!
241 public String print()
243 return print(getSeqsAsArray());