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
81 public void parse() throws IOException
83 StringBuffer sb = new StringBuffer();
84 boolean firstLine = true;
89 boolean annotation = false;
91 while ((uline = nextLine()) != null)
94 if (line.length() > 0)
96 if (line.charAt(0) == '>')
98 if (line.startsWith(">#_"))
102 annotations.addElement(makeAnnotation(seq, sb));
112 seq.setSequence(sb.toString());
116 seqs.addElement(seq);
120 seq = parseId(line.substring(1));
123 sb = new StringBuffer();
125 if (line.startsWith(">#_"))
132 sb.append(annotation ? uline : line);
139 annotations.addElement(makeAnnotation(seq, sb));
144 seq.setSequence(sb.toString());
145 seqs.addElement(seq);
149 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
151 Annotation[] anots = new Annotation[sb.length()];
153 for (int i = 0; i < anots.length; i++)
155 char cn = sb.charAt(i);
158 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
161 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
162 .substring(2), seq.getDescription(), anots);
167 * called by AppletFormatAdapter to generate an annotated alignment, rather
168 * than bare sequences.
172 public void addAnnotations(Alignment al)
175 for (int i = 0; i < annotations.size(); i++)
177 AlignmentAnnotation aa = annotations
179 aa.setPadGaps(true, al.getGapCharacter());
180 al.addAnnotation(aa);
196 * @return DOCUMENT ME!
198 public String print(SequenceI[] s)
200 out = new StringBuffer();
203 while ((i < s.length) && (s[i] != null))
205 out.append(">" + printId(s[i]));
206 if (s[i].getDescription() != null)
208 out.append(" " + s[i].getDescription());
213 int nochunks = (s[i].getLength() / len)
214 + (s[i].getLength() % len > 0 ? 1 : 0);
216 for (int j = 0; j < nochunks; j++)
219 int end = start + len;
221 if (end < s[i].getLength())
223 out.append(s[i].getSequenceAsString(start, end) + newline);
225 else if (start < s[i].getLength())
227 out.append(s[i].getSequenceAsString(start, s[i].getLength())
235 return out.toString();
241 * @return DOCUMENT ME!
244 public String print()
246 return print(getSeqsAsArray());