2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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/>.
22 import jalview.datamodel.*;
30 public class FastaFile extends AlignFile
33 * Length of a sequence line
40 * Creates a new FastaFile object.
47 * Creates a new FastaFile object.
57 public FastaFile(String inFile, String type) throws IOException
62 public FastaFile(FileParse source) throws IOException
73 public void parse() throws IOException
75 StringBuffer sb = new StringBuffer();
76 boolean firstLine = true;
81 boolean annotation = false;
83 while ((line = nextLine()) != null)
86 if (line.length() > 0)
88 if (line.charAt(0) == '>')
90 if (line.startsWith(">#_"))
94 Annotation[] anots = new Annotation[sb.length()];
95 String anotString = sb.toString();
96 for (int i = 0; i < sb.length(); i++)
98 anots[i] = new Annotation(anotString.substring(i, i + 1),
101 AlignmentAnnotation aa = new AlignmentAnnotation(seq
102 .getName().substring(2), seq.getDescription(), anots);
104 annotations.addElement(aa);
114 seq.setSequence(sb.toString());
118 seqs.addElement(seq);
122 seq = parseId(line.substring(1));
125 sb = new StringBuffer();
127 if (line.startsWith(">#_"))
141 Annotation[] anots = new Annotation[sb.length()];
142 String anotString = sb.toString();
143 for (int i = 0; i < sb.length(); i++)
145 anots[i] = new Annotation(anotString.substring(i, i + 1), null,
148 AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
149 .substring(2), seq.getDescription(), anots);
151 annotations.addElement(aa);
156 seq.setSequence(sb.toString());
157 seqs.addElement(seq);
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) + "\n");
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());