2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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.
23 import jalview.datamodel.*;
31 public class FastaFile extends AlignFile
34 * Length of a sequence line
41 * Creates a new FastaFile object.
48 * Creates a new FastaFile object.
58 public FastaFile(String inFile, String type) throws IOException
63 public FastaFile(FileParse source) throws IOException
74 public void parse() throws IOException
76 StringBuffer sb = new StringBuffer();
77 boolean firstLine = true;
82 boolean annotation = false;
84 while ((uline = nextLine()) != null)
87 if (line.length() > 0)
89 if (line.charAt(0) == '>')
91 if (line.startsWith(">#_"))
95 annotations.addElement(makeAnnotation(seq, sb));
105 seq.setSequence(sb.toString());
109 seqs.addElement(seq);
113 seq = parseId(line.substring(1));
116 sb = new StringBuffer();
118 if (line.startsWith(">#_"))
125 sb.append(annotation ? uline : line);
132 annotations.addElement(makeAnnotation(seq, sb));
137 seq.setSequence(sb.toString());
138 seqs.addElement(seq);
141 private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
143 Annotation[] anots = new Annotation[sb.length()];
145 for (int i=0;i<anots.length;i++)
147 char cn = sb.charAt(i);
150 anots[i] = new Annotation(""+cn, null,
154 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
155 .substring(2), seq.getDescription(), anots);
159 * called by AppletFormatAdapter to generate an annotated alignment, rather
160 * than bare sequences.
164 public void addAnnotations(Alignment al)
167 for (int i = 0; i < annotations.size(); i++)
169 AlignmentAnnotation aa = (AlignmentAnnotation) annotations
171 aa.setPadGaps(true, al.getGapCharacter());
172 al.addAnnotation(aa);
188 * @return DOCUMENT ME!
190 public String print(SequenceI[] s)
192 out = new StringBuffer();
195 while ((i < s.length) && (s[i] != null))
197 out.append(">" + printId(s[i]));
198 if (s[i].getDescription() != null)
200 out.append(" " + s[i].getDescription());
205 int nochunks = (s[i].getLength() / len) + 1;
207 for (int j = 0; j < nochunks; j++)
210 int end = start + len;
212 if (end < s[i].getLength())
214 out.append(s[i].getSequenceAsString(start, end) + newline);
216 else if (start < s[i].getLength())
218 out.append(s[i].getSequenceAsString(start, s[i].getLength())
226 return out.toString();
232 * @return DOCUMENT ME!
234 public String print()
236 return print(getSeqsAsArray());