output now takes alignment, not just sequences
[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    * Length of a sequence line\r
38    */\r
39   int len = 72;\r
40 \r
41   StringBuffer out;\r
42 \r
43     /**\r
44      * Creates a new FastaFile object.\r
45      */\r
46     public FastaFile()\r
47     {\r
48     }\r
49 \r
50     /**\r
51      * Creates a new FastaFile object.\r
52      *\r
53      * @param inFile DOCUMENT ME!\r
54      * @param type DOCUMENT ME!\r
55      *\r
56      * @throws IOException DOCUMENT ME!\r
57      */\r
58     public FastaFile(String inFile, String type) throws IOException\r
59     {\r
60         super(inFile, type);\r
61     }\r
62 \r
63     /**\r
64      * DOCUMENT ME!\r
65      *\r
66      * @throws IOException DOCUMENT ME!\r
67      */\r
68     public void parse() throws IOException\r
69     {\r
70         StringBuffer sb = new StringBuffer();\r
71         boolean firstLine = true;\r
72 \r
73         String line;\r
74         Sequence seq = null;\r
75 \r
76         boolean annotation = false;\r
77 \r
78         while ((line = nextLine()) != null)\r
79         {\r
80             line = line.trim();\r
81             if (line.length() > 0)\r
82             {\r
83               if (line.charAt(0)=='>')\r
84                 {\r
85                   if (line.startsWith(">#_"))\r
86                   {\r
87                     if (annotation)\r
88                     {\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
92                       {\r
93                         anots[i] = new Annotation(anotString.substring(i, i+1),\r
94                                                   null,\r
95                                                   ' ', 0);\r
96                       }\r
97                       AlignmentAnnotation aa = new AlignmentAnnotation(\r
98                           seq.getName().substring(2), seq.getDescription(),\r
99                           anots);\r
100 \r
101                       annotations.addElement(aa);\r
102                     }\r
103                   }\r
104                   else\r
105                     annotation = false;\r
106 \r
107                     if (!firstLine)\r
108                     {\r
109                       if (!annotation && !isValidProteinSequence(sb.toString().toCharArray()))\r
110                       {\r
111                         throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
112                                               +" : "+seq.getName()\r
113                                               +" : "+invalidCharacter);\r
114                       }\r
115 \r
116                        seq.setSequence(sb.toString());\r
117 \r
118                        if (!annotation)\r
119                          seqs.addElement(seq);\r
120                     }\r
121 \r
122                     seq = parseId(line.substring(1));\r
123                     firstLine = false;\r
124 \r
125                     sb = new StringBuffer();\r
126 \r
127                     if (line.startsWith(">#_"))\r
128                       annotation = true;\r
129                 }\r
130                 else\r
131                 {\r
132                     sb.append(line);\r
133                 }\r
134             }\r
135         }\r
136 \r
137         if (annotation)\r
138         {\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
142           {\r
143             anots[i] = new Annotation(anotString.substring(i, i + 1),\r
144                                       null,\r
145                                       ' ', 0);\r
146           }\r
147           AlignmentAnnotation aa = new AlignmentAnnotation(\r
148               seq.getName().substring(2), seq.getDescription(),\r
149               anots);\r
150 \r
151           annotations.addElement(aa);\r
152         }\r
153 \r
154         else if (!firstLine)\r
155         {\r
156 \r
157             if (!isValidProteinSequence(sb.toString().toCharArray()))\r
158             {\r
159                 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS\r
160                                       +" : "+seq.getName()\r
161                                       +" : "+invalidCharacter);\r
162             }\r
163 \r
164             seq.setSequence(sb.toString());\r
165             seqs.addElement(seq);\r
166         }\r
167     }\r
168 \r
169     /**\r
170      * DOCUMENT ME!\r
171      *\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
176      *\r
177      * @return DOCUMENT ME!\r
178      */\r
179     public String print(SequenceI[] s)\r
180     {\r
181         out = new StringBuffer();\r
182         int i = 0;\r
183 \r
184         while ((i < s.length) && (s[i] != null))\r
185         {\r
186             out.append(">" + printId(s[i]));\r
187             if(s[i].getDescription()!=null)\r
188               out.append(" "+s[i].getDescription());\r
189 \r
190             out.append("\n");\r
191 \r
192             int nochunks = (s[i].getLength() / len) + 1;\r
193 \r
194             for (int j = 0; j < nochunks; j++)\r
195             {\r
196                 int start = j * len;\r
197                 int end = start + len;\r
198 \r
199                 if (end < s[i].getLength())\r
200                 {\r
201                     out.append(s[i].getSequenceAsString(start, end) + "\n");\r
202                 }\r
203                 else if (start < s[i].getLength())\r
204                 {\r
205                     out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");\r
206                 }\r
207             }\r
208 \r
209             i++;\r
210         }\r
211 \r
212         return out.toString();\r
213     }\r
214 \r
215     /**\r
216      * DOCUMENT ME!\r
217      *\r
218      * @return DOCUMENT ME!\r
219      */\r
220     public String print()\r
221     {\r
222         return print(getSeqsAsArray());\r
223     }\r
224 }\r