2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
21 import jalview.datamodel.*;
\r
32 * @version $Revision$
\r
34 public class FastaFile extends AlignFile
\r
37 * Length of a sequence line
\r
44 * Creates a new FastaFile object.
\r
51 * Creates a new FastaFile object.
\r
53 * @param inFile DOCUMENT ME!
\r
54 * @param type DOCUMENT ME!
\r
56 * @throws IOException DOCUMENT ME!
\r
58 public FastaFile(String inFile, String type) throws IOException
\r
60 super(inFile, type);
\r
66 * @throws IOException DOCUMENT ME!
\r
68 public void parse() throws IOException
\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
105 annotation = false;
\r
109 if (!annotation && !isValidProteinSequence(sb.toString().toCharArray()))
\r
111 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
\r
112 +" : "+seq.getName()
\r
113 +" : "+invalidCharacter);
\r
116 seq.setSequence(sb.toString());
\r
119 seqs.addElement(seq);
\r
122 seq = parseId(line.substring(1));
\r
125 sb = new StringBuffer();
\r
127 if (line.startsWith(">#_"))
\r
139 Annotation[] anots = new Annotation[sb.length()];
\r
140 String anotString = sb.toString();
\r
141 for (int i = 0; i < sb.length(); i++)
\r
143 anots[i] = new Annotation(anotString.substring(i, i + 1),
\r
147 AlignmentAnnotation aa = new AlignmentAnnotation(
\r
148 seq.getName().substring(2), seq.getDescription(),
\r
151 annotations.addElement(aa);
\r
154 else if (!firstLine)
\r
157 if (!isValidProteinSequence(sb.toString().toCharArray()))
\r
159 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
\r
160 +" : "+seq.getName()
\r
161 +" : "+invalidCharacter);
\r
164 seq.setSequence(sb.toString());
\r
165 seqs.addElement(seq);
\r
172 * @param s DOCUMENT ME!
\r
173 * @param len DOCUMENT ME!
\r
174 * @param gaps DOCUMENT ME!
\r
175 * @param displayId DOCUMENT ME!
\r
177 * @return DOCUMENT ME!
\r
179 public String print(SequenceI[] s)
\r
181 out = new StringBuffer();
\r
184 while ((i < s.length) && (s[i] != null))
\r
186 out.append(">" + printId(s[i]));
\r
187 if(s[i].getDescription()!=null)
\r
188 out.append(" "+s[i].getDescription());
\r
192 int nochunks = (s[i].getLength() / len) + 1;
\r
194 for (int j = 0; j < nochunks; j++)
\r
196 int start = j * len;
\r
197 int end = start + len;
\r
199 if (end < s[i].getLength())
\r
201 out.append(s[i].getSequenceAsString(start, end) + "\n");
\r
203 else if (start < s[i].getLength())
\r
205 out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");
\r
212 return out.toString();
\r
218 * @return DOCUMENT ME!
\r
220 public String print()
\r
222 return print(getSeqsAsArray());
\r