2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import jalview.datamodel.*;
32 public class FastaFile extends AlignFile
35 * Length of a sequence line
42 * Creates a new FastaFile object.
49 * Creates a new FastaFile object.
59 public FastaFile(String inFile, String type) throws IOException
64 public FastaFile(FileParse source) throws IOException
75 public void parse() throws IOException
77 StringBuffer sb = new StringBuffer();
78 boolean firstLine = true;
83 boolean annotation = false;
85 while ((uline = nextLine()) != null)
88 if (line.length() > 0)
90 if (line.charAt(0) == '>')
92 if (line.startsWith(">#_"))
96 annotations.addElement(makeAnnotation(seq, sb));
106 seq.setSequence(sb.toString());
110 seqs.addElement(seq);
114 seq = parseId(line.substring(1));
117 sb = new StringBuffer();
119 if (line.startsWith(">#_"))
126 sb.append(annotation ? uline : line);
133 annotations.addElement(makeAnnotation(seq, sb));
138 seq.setSequence(sb.toString());
139 seqs.addElement(seq);
142 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
144 Annotation[] anots = new Annotation[sb.length()];
146 for (int i=0;i<anots.length;i++)
148 char cn = sb.charAt(i);
151 anots[i] = new Annotation(""+cn, null,
155 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
156 .substring(2), seq.getDescription(), anots);
160 * called by AppletFormatAdapter to generate an annotated alignment, rather
161 * than bare sequences.
165 public void addAnnotations(Alignment al)
168 for (int i = 0; i < annotations.size(); i++)
170 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
172 aa.setPadGaps(true, al.getGapCharacter());
173 al.addAnnotation(aa);
189 * @return DOCUMENT ME!
191 public String print(SequenceI[] s)
193 out = new StringBuffer();
196 while ((i < s.length) && (s[i] != null))
198 out.append(">" + printId(s[i]));
199 if (s[i].getDescription() != null)
201 out.append(" " + s[i].getDescription());
206 int nochunks = (s[i].getLength() / len) + 1;
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();
233 * @return DOCUMENT ME!
235 public String print()
237 return print(getSeqsAsArray());