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
75 public FastaFile(SequenceI[] seqs)
87 public void parse() throws IOException
89 StringBuffer sb = new StringBuffer();
90 boolean firstLine = true;
95 boolean annotation = false;
97 while ((uline = nextLine()) != null)
100 if (line.length() > 0)
102 if (line.charAt(0) == '>')
104 if (line.startsWith(">#_"))
108 annotations.addElement(makeAnnotation(seq, sb));
118 seq.setSequence(sb.toString());
122 seqs.addElement(seq);
126 seq = parseId(line.substring(1));
129 sb = new StringBuffer();
131 if (line.startsWith(">#_"))
138 sb.append(annotation ? uline : line);
145 annotations.addElement(makeAnnotation(seq, sb));
150 seq.setSequence(sb.toString());
151 seqs.addElement(seq);
155 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
157 Annotation[] anots = new Annotation[sb.length()];
159 for (int i = 0; i < anots.length; i++)
161 char cn = sb.charAt(i);
164 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
167 AlignmentAnnotation aa = new AlignmentAnnotation(
168 seq.getName().substring(2), seq.getDescription(), anots);
173 * called by AppletFormatAdapter to generate an annotated alignment, rather
174 * than bare sequences.
178 public void addAnnotations(Alignment al)
181 for (int i = 0; i < annotations.size(); i++)
183 AlignmentAnnotation aa = annotations.elementAt(i);
184 aa.setPadGaps(true, al.getGapCharacter());
185 al.addAnnotation(aa);
190 public String print(SequenceI[] s, boolean jvsuffix)
192 out = new StringBuffer();
195 while ((i < s.length) && (s[i] != null))
197 out.append(">" + printId(s[i], jvsuffix));
198 if (s[i].getDescription() != null)
200 out.append(" " + s[i].getDescription());
205 int nochunks = (s[i].getLength() / len)
206 + (s[i].getLength() % len > 0 ? 1 : 0);
208 for (int j = 0; j < nochunks; j++)
211 int end = start + len;
213 if (end < s[i].getLength())
215 out.append(s[i].getSequenceAsString(start, end) + newline);
217 else if (start < s[i].getLength())
219 out.append(s[i].getSequenceAsString(start, s[i].getLength())
227 return out.toString();