File Parsing returns Alignments, not Sequence[]
[jalview.git] / src / jalview / io / FastaFile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\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
9 *\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
14 *\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
18 */\r
19 package jalview.io;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import java.io.*;\r
24 \r
25 import java.util.*;\r
26 \r
27 \r
28 /**\r
29  * DOCUMENT ME!\r
30  *\r
31  * @author $author$\r
32  * @version $Revision$\r
33  */\r
34 public class FastaFile extends AlignFile\r
35 {\r
36     /**\r
37      * Creates a new FastaFile object.\r
38      */\r
39     public FastaFile()\r
40     {\r
41     }\r
42 \r
43     /**\r
44      * Creates a new FastaFile object.\r
45      *\r
46      * @param inFile DOCUMENT ME!\r
47      * @param type DOCUMENT ME!\r
48      *\r
49      * @throws IOException DOCUMENT ME!\r
50      */\r
51     public FastaFile(String inFile, String type) throws IOException\r
52     {\r
53         super(inFile, type);\r
54     }\r
55 \r
56     /**\r
57      * DOCUMENT ME!\r
58      *\r
59      * @throws IOException DOCUMENT ME!\r
60      */\r
61     public void parse() throws IOException\r
62     {\r
63         StringBuffer sb = new StringBuffer();\r
64         boolean firstLine = true;\r
65 \r
66         String line;\r
67         Sequence seq = null;\r
68 \r
69         boolean annotation = false;\r
70 \r
71         while ((line = nextLine()) != null)\r
72         {\r
73             line = line.trim();\r
74             if (line.length() > 0)\r
75             {\r
76               if (line.charAt(0)=='>')\r
77                 {\r
78                   if (line.startsWith(">#_"))\r
79                   {\r
80                     if (annotation)\r
81                     {\r
82                       Annotation[] anots = new Annotation[sb.length()];\r
83                       for (int i = 0; i < sb.length(); i++)\r
84                       {\r
85                         anots[i] = new Annotation(sb.substring(i, i+1),\r
86                                                   null,\r
87                                                   ' ', 0);\r
88                       }\r
89                       AlignmentAnnotation aa = new AlignmentAnnotation(\r
90                           seq.getName().substring(2), seq.getDescription(),\r
91                           anots);\r
92 \r
93                       annotations.add(aa);\r
94                     }\r
95                     annotation = true;\r
96                   }\r
97                   else\r
98                     annotation = false;\r
99 \r
100                     if (!firstLine)\r
101                     {\r
102                       if (!annotation && !isValidProteinSequence(sb.toString().toCharArray()))\r
103                       {\r
104                         throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
105                                               +" : "+seq.getName()\r
106                                               +" : "+invalidCharacter);\r
107                       }\r
108 \r
109                        seq.setSequence(sb.toString());\r
110 \r
111                        if (!annotation)\r
112                          seqs.addElement(seq);\r
113                     }\r
114 \r
115                     seq = parseId(line.substring(1));\r
116                     firstLine = false;\r
117 \r
118                     sb = new StringBuffer();\r
119                 }\r
120                 else\r
121                 {\r
122                     sb.append(line);\r
123                 }\r
124             }\r
125         }\r
126 \r
127         if (annotation)\r
128         {\r
129           Annotation[] anots = new Annotation[sb.length()];\r
130           for (int i = 0; i < sb.length(); i++)\r
131           {\r
132             anots[i] = new Annotation(sb.substring(i, i + 1),\r
133                                       null,\r
134                                       ' ', 0);\r
135           }\r
136           AlignmentAnnotation aa = new AlignmentAnnotation(\r
137               seq.getName().substring(2), seq.getDescription(),\r
138               anots);\r
139 \r
140           annotations.add(aa);\r
141         }\r
142 \r
143         else if (!firstLine)\r
144         {\r
145 \r
146             if (!isValidProteinSequence(sb.toString().toCharArray()))\r
147             {\r
148                 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
149                                       +" : "+seq.getName()\r
150                                       +" : "+invalidCharacter);\r
151             }\r
152 \r
153             seq.setSequence(sb.toString());\r
154             seqs.addElement(seq);\r
155         }\r
156     }\r
157 \r
158 \r
159     /**\r
160      * DOCUMENT ME!\r
161      *\r
162      * @param s DOCUMENT ME!\r
163      * @param len DOCUMENT ME!\r
164      * @param gaps DOCUMENT ME!\r
165      * @param displayId DOCUMENT ME!\r
166      *\r
167      * @return DOCUMENT ME!\r
168      */\r
169     public String print(SequenceI[] s)\r
170     {\r
171         int len = 72;\r
172         StringBuffer out = new StringBuffer();\r
173         int i = 0;\r
174 \r
175         while ((i < s.length) && (s[i] != null))\r
176         {\r
177             out.append(">" + printId(s[i]));\r
178             if(s[i].getDescription()!=null)\r
179               out.append(" "+s[i].getDescription());\r
180 \r
181             out.append("\n");\r
182 \r
183             int nochunks = (s[i].getLength() / len) + 1;\r
184 \r
185             for (int j = 0; j < nochunks; j++)\r
186             {\r
187                 int start = j * len;\r
188                 int end = start + len;\r
189 \r
190                 if (end < s[i].getLength())\r
191                 {\r
192                     out.append(s[i].getSequenceAsString(start, end) + "\n");\r
193                 }\r
194                 else if (start < s[i].getLength())\r
195                 {\r
196                     out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");\r
197                 }\r
198             }\r
199 \r
200             i++;\r
201         }\r
202 \r
203         return out.toString();\r
204     }\r
205 \r
206     /**\r
207      * DOCUMENT ME!\r
208      *\r
209      * @return DOCUMENT ME!\r
210      */\r
211     public String print()\r
212     {\r
213         return print(getSeqsAsArray());\r
214     }\r
215 }\r