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 java.io.IOException;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceI;
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
75 public FastaFile(FileParse source, boolean closeData) throws IOException
77 super(true, source, closeData);
80 public FastaFile(SequenceI[] seqs)
92 public void parse() throws IOException
94 StringBuffer sb = new StringBuffer();
95 boolean firstLine = true;
100 boolean annotation = false;
102 while ((uline = nextLine()) != null)
105 if (line.length() > 0)
107 if (line.charAt(0) == '>')
109 if (line.startsWith(">#_"))
113 annotations.addElement(makeAnnotation(seq, sb));
123 seq.setSequence(sb.toString());
127 seqs.addElement(seq);
131 seq = parseId(line.substring(1));
134 sb = new StringBuffer();
136 if (line.startsWith(">#_"))
143 sb.append(annotation ? uline : line);
150 annotations.addElement(makeAnnotation(seq, sb));
155 seq.setSequence(sb.toString());
156 seqs.addElement(seq);
160 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
162 Annotation[] anots = new Annotation[sb.length()];
164 for (int i = 0; i < anots.length; i++)
166 char cn = sb.charAt(i);
169 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
172 AlignmentAnnotation aa = new AlignmentAnnotation(
173 seq.getName().substring(2), seq.getDescription(), anots);
178 * called by AppletFormatAdapter to generate an annotated alignment, rather
179 * than bare sequences.
183 public void addAnnotations(Alignment al)
186 for (int i = 0; i < annotations.size(); i++)
188 AlignmentAnnotation aa = annotations.elementAt(i);
189 aa.setPadGaps(true, al.getGapCharacter());
190 al.addAnnotation(aa);
195 public String print(SequenceI[] s, boolean jvsuffix)
197 out = new StringBuffer();
200 while ((i < s.length) && (s[i] != null))
202 out.append(">" + printId(s[i], jvsuffix));
203 if (s[i].getDescription() != null)
205 out.append(" " + s[i].getDescription());
210 int nochunks = (s[i].getLength() / len)
211 + (s[i].getLength() % len > 0 ? 1 : 0);
213 for (int j = 0; j < nochunks; j++)
216 int end = start + len;
218 if (end < s[i].getLength())
220 out.append(s[i].getSequenceAsString(start, end) + newline);
222 else if (start < s[i].getLength())
224 out.append(s[i].getSequenceAsString(start, s[i].getLength())
232 return out.toString();