2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
23 import jalview.datamodel.*;
\r
29 * @version $Revision$
\r
31 public class FastaFile
\r
35 * Length of a sequence line
\r
42 * Creates a new FastaFile object.
\r
49 * Creates a new FastaFile object.
\r
51 * @param inFile DOCUMENT ME!
\r
52 * @param type DOCUMENT ME!
\r
54 * @throws IOException DOCUMENT ME!
\r
56 public FastaFile(String inFile, String type)
\r
59 super(inFile, type);
\r
65 * @throws IOException DOCUMENT ME!
\r
70 StringBuffer sb = new StringBuffer();
\r
71 boolean firstLine = true;
\r
74 Sequence seq = null;
\r
76 boolean annotation = false;
\r
78 while ( (line = nextLine()) != null)
\r
81 if (line.length() > 0)
\r
83 if (line.charAt(0) == '>')
\r
85 if (line.startsWith(">#_"))
\r
89 Annotation[] anots = new Annotation[sb.length()];
\r
90 String anotString = sb.toString();
\r
91 for (int i = 0; i < sb.length(); i++)
\r
93 anots[i] = new Annotation(anotString.substring(i, i + 1),
\r
97 AlignmentAnnotation aa = new AlignmentAnnotation(
\r
98 seq.getName().substring(2), seq.getDescription(),
\r
101 annotations.addElement(aa);
\r
106 annotation = false;
\r
111 seq.setSequence(sb.toString());
\r
115 seqs.addElement(seq);
\r
119 seq = parseId(line.substring(1));
\r
122 sb = new StringBuffer();
\r
124 if (line.startsWith(">#_"))
\r
138 Annotation[] anots = new Annotation[sb.length()];
\r
139 String anotString = sb.toString();
\r
140 for (int i = 0; i < sb.length(); i++)
\r
142 anots[i] = new Annotation(anotString.substring(i, i + 1),
\r
146 AlignmentAnnotation aa = new AlignmentAnnotation(
\r
147 seq.getName().substring(2), seq.getDescription(),
\r
150 annotations.addElement(aa);
\r
153 else if (!firstLine)
\r
155 seq.setSequence(sb.toString());
\r
156 seqs.addElement(seq);
\r
161 * called by AppletFormatAdapter to generate
\r
162 * an annotated alignment, rather than bare
\r
166 public void addAnnotations(Alignment al)
\r
169 for (int i = 0; i < annotations.size(); i++)
\r
171 AlignmentAnnotation aa = (AlignmentAnnotation) annotations.elementAt(i);
\r
172 aa.setPadGaps(true, al.getGapCharacter());
\r
173 al.addAnnotation( aa );
\r
181 * @param s DOCUMENT ME!
\r
182 * @param len DOCUMENT ME!
\r
183 * @param gaps DOCUMENT ME!
\r
184 * @param displayId DOCUMENT ME!
\r
186 * @return DOCUMENT ME!
\r
188 public String print(SequenceI[] s)
\r
190 out = new StringBuffer();
\r
193 while ( (i < s.length) && (s[i] != null))
\r
195 out.append(">" + printId(s[i]));
\r
196 if (s[i].getDescription() != null)
\r
198 out.append(" " + s[i].getDescription());
\r
203 int nochunks = (s[i].getLength() / len) + 1;
\r
205 for (int j = 0; j < nochunks; j++)
\r
207 int start = j * len;
\r
208 int end = start + len;
\r
210 if (end < s[i].getLength())
\r
212 out.append(s[i].getSequenceAsString(start, end) + "\n");
\r
214 else if (start < s[i].getLength())
\r
216 out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");
\r
223 return out.toString();
\r
229 * @return DOCUMENT ME!
\r
231 public String print()
\r
233 return print(getSeqsAsArray());
\r