Formatting
[jalview.git] / src / jalview / io / FastaFile.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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 java.io.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 /**\r
26  * DOCUMENT ME!\r
27  *\r
28  * @author $author$\r
29  * @version $Revision$\r
30  */\r
31 public class FastaFile\r
32     extends AlignFile\r
33 {\r
34   /**\r
35    * Length of a sequence line\r
36    */\r
37   int len = 72;\r
38 \r
39   StringBuffer out;\r
40 \r
41   /**\r
42    * Creates a new FastaFile object.\r
43    */\r
44   public FastaFile()\r
45   {\r
46   }\r
47 \r
48   /**\r
49    * Creates a new FastaFile object.\r
50    *\r
51    * @param inFile DOCUMENT ME!\r
52    * @param type DOCUMENT ME!\r
53    *\r
54    * @throws IOException DOCUMENT ME!\r
55    */\r
56   public FastaFile(String inFile, String type)\r
57       throws IOException\r
58   {\r
59     super(inFile, type);\r
60   }\r
61 \r
62   /**\r
63    * DOCUMENT ME!\r
64    *\r
65    * @throws IOException DOCUMENT ME!\r
66    */\r
67   public void parse()\r
68       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           {\r
106             annotation = false;\r
107           }\r
108 \r
109           if (!firstLine)\r
110           {\r
111             seq.setSequence(sb.toString());\r
112 \r
113             if (!annotation)\r
114             {\r
115               seqs.addElement(seq);\r
116             }\r
117           }\r
118 \r
119           seq = parseId(line.substring(1));\r
120           firstLine = false;\r
121 \r
122           sb = new StringBuffer();\r
123 \r
124           if (line.startsWith(">#_"))\r
125           {\r
126             annotation = true;\r
127           }\r
128         }\r
129         else\r
130         {\r
131           sb.append(line);\r
132         }\r
133       }\r
134     }\r
135 \r
136     if (annotation)\r
137     {\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
141       {\r
142         anots[i] = new Annotation(anotString.substring(i, i + 1),\r
143                                   null,\r
144                                   ' ', 0);\r
145       }\r
146       AlignmentAnnotation aa = new AlignmentAnnotation(\r
147           seq.getName().substring(2), seq.getDescription(),\r
148           anots);\r
149 \r
150       annotations.addElement(aa);\r
151     }\r
152 \r
153     else if (!firstLine)\r
154     {\r
155       seq.setSequence(sb.toString());\r
156       seqs.addElement(seq);\r
157     }\r
158   }\r
159 \r
160   /**\r
161    * DOCUMENT ME!\r
162    *\r
163    * @param s DOCUMENT ME!\r
164    * @param len DOCUMENT ME!\r
165    * @param gaps DOCUMENT ME!\r
166    * @param displayId DOCUMENT ME!\r
167    *\r
168    * @return DOCUMENT ME!\r
169    */\r
170   public String print(SequenceI[] s)\r
171   {\r
172     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       {\r
180         out.append(" " + s[i].getDescription());\r
181       }\r
182 \r
183       out.append("\n");\r
184 \r
185       int nochunks = (s[i].getLength() / len) + 1;\r
186 \r
187       for (int j = 0; j < nochunks; j++)\r
188       {\r
189         int start = j * len;\r
190         int end = start + len;\r
191 \r
192         if (end < s[i].getLength())\r
193         {\r
194           out.append(s[i].getSequenceAsString(start, end) + "\n");\r
195         }\r
196         else if (start < s[i].getLength())\r
197         {\r
198           out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");\r
199         }\r
200       }\r
201 \r
202       i++;\r
203     }\r
204 \r
205     return out.toString();\r
206   }\r
207 \r
208   /**\r
209    * DOCUMENT ME!\r
210    *\r
211    * @return DOCUMENT ME!\r
212    */\r
213   public String print()\r
214   {\r
215     return print(getSeqsAsArray());\r
216   }\r
217 }\r