2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import jalview.datamodel.*;
31 public class FastaFile
35 * Length of a sequence line
42 * Creates a new FastaFile object.
49 * Creates a new FastaFile object.
51 * @param inFile DOCUMENT ME!
52 * @param type DOCUMENT ME!
54 * @throws IOException DOCUMENT ME!
56 public FastaFile(String inFile, String type)
62 public FastaFile(FileParse source) throws IOException
70 * @throws IOException DOCUMENT ME!
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),
102 AlignmentAnnotation aa = new AlignmentAnnotation(
103 seq.getName().substring(2), seq.getDescription(),
106 annotations.addElement(aa);
116 seq.setSequence(sb.toString());
120 seqs.addElement(seq);
124 seq = parseId(line.substring(1));
127 sb = new StringBuffer();
129 if (line.startsWith(">#_"))
143 Annotation[] anots = new Annotation[sb.length()];
144 String anotString = sb.toString();
145 for (int i = 0; i < sb.length(); i++)
147 anots[i] = new Annotation(anotString.substring(i, i + 1),
151 AlignmentAnnotation aa = new AlignmentAnnotation(
152 seq.getName().substring(2), seq.getDescription(),
155 annotations.addElement(aa);
160 seq.setSequence(sb.toString());
161 seqs.addElement(seq);
166 * called by AppletFormatAdapter to generate
167 * an annotated alignment, rather than bare
171 public void addAnnotations(Alignment al)
174 for (int i = 0; i < annotations.size(); i++)
176 AlignmentAnnotation aa = (AlignmentAnnotation) annotations.elementAt(i);
177 aa.setPadGaps(true, al.getGapCharacter());
178 al.addAnnotation( aa );
186 * @param s DOCUMENT ME!
187 * @param len DOCUMENT ME!
188 * @param gaps DOCUMENT ME!
189 * @param displayId DOCUMENT ME!
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()) + "\n");
228 return out.toString();
234 * @return DOCUMENT ME!
236 public String print()
238 return print(getSeqsAsArray());