2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
25 import jalview.datamodel.*;
33 public class FastaFile extends AlignFile
36 * Length of a sequence line
43 * Creates a new FastaFile object.
50 * Creates a new FastaFile object.
60 public FastaFile(String inFile, String type) throws IOException
65 public FastaFile(FileParse source) throws IOException
76 public void parse() throws IOException
78 StringBuffer sb = new StringBuffer();
79 boolean firstLine = true;
84 boolean annotation = false;
86 while ((uline = nextLine()) != null)
89 if (line.length() > 0)
91 if (line.charAt(0) == '>')
93 if (line.startsWith(">#_"))
97 annotations.addElement(makeAnnotation(seq, sb));
107 seq.setSequence(sb.toString());
111 seqs.addElement(seq);
115 seq = parseId(line.substring(1));
118 sb = new StringBuffer();
120 if (line.startsWith(">#_"))
127 sb.append(annotation ? uline : line);
134 annotations.addElement(makeAnnotation(seq, sb));
139 seq.setSequence(sb.toString());
140 seqs.addElement(seq);
144 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
146 Annotation[] anots = new Annotation[sb.length()];
148 for (int i = 0; i < anots.length; i++)
150 char cn = sb.charAt(i);
153 anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
156 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
157 .substring(2), seq.getDescription(), anots);
162 * called by AppletFormatAdapter to generate an annotated alignment, rather
163 * than bare sequences.
167 public void addAnnotations(Alignment al)
170 for (int i = 0; i < annotations.size(); i++)
172 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
174 aa.setPadGaps(true, al.getGapCharacter());
175 al.addAnnotation(aa);
191 * @return DOCUMENT ME!
193 public String print(SequenceI[] s)
195 out = new StringBuffer();
198 while ((i < s.length) && (s[i] != null))
200 out.append(">" + printId(s[i]));
201 if (s[i].getDescription() != null)
203 out.append(" " + s[i].getDescription());
208 int nochunks = (s[i].getLength() / len) + 1;
210 for (int j = 0; j < nochunks; j++)
213 int end = start + len;
215 if (end < s[i].getLength())
217 out.append(s[i].getSequenceAsString(start, end) + newline);
219 else if (start < s[i].getLength())
221 out.append(s[i].getSequenceAsString(start, s[i].getLength())
229 return out.toString();
235 * @return DOCUMENT ME!
237 public String print()
239 return print(getSeqsAsArray());