AMSA input set padgaps true
[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    * called by AppletFormatAdapter to generate\r
162    * an annotated alignment, rather than bare\r
163    * sequences.\r
164    * @param al\r
165    */\r
166   public void addAnnotations(Alignment al)\r
167   {\r
168     addProperties(al);\r
169     for (int i = 0; i < annotations.size(); i++)\r
170     {\r
171       AlignmentAnnotation aa = (AlignmentAnnotation) annotations.elementAt(i);\r
172       aa.setPadGaps(true, al.getGapCharacter());\r
173       al.addAnnotation( aa );\r
174     }\r
175   }\r
176 \r
177 \r
178   /**\r
179    * DOCUMENT ME!\r
180    *\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
185    *\r
186    * @return DOCUMENT ME!\r
187    */\r
188   public String print(SequenceI[] s)\r
189   {\r
190     out = new StringBuffer();\r
191     int i = 0;\r
192 \r
193     while ( (i < s.length) && (s[i] != null))\r
194     {\r
195       out.append(">" + printId(s[i]));\r
196       if (s[i].getDescription() != null)\r
197       {\r
198         out.append(" " + s[i].getDescription());\r
199       }\r
200 \r
201       out.append("\n");\r
202 \r
203       int nochunks = (s[i].getLength() / len) + 1;\r
204 \r
205       for (int j = 0; j < nochunks; j++)\r
206       {\r
207         int start = j * len;\r
208         int end = start + len;\r
209 \r
210         if (end < s[i].getLength())\r
211         {\r
212           out.append(s[i].getSequenceAsString(start, end) + "\n");\r
213         }\r
214         else if (start < s[i].getLength())\r
215         {\r
216           out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");\r
217         }\r
218       }\r
219 \r
220       i++;\r
221     }\r
222 \r
223     return out.toString();\r
224   }\r
225 \r
226   /**\r
227    * DOCUMENT ME!\r
228    *\r
229    * @return DOCUMENT ME!\r
230    */\r
231   public String print()\r
232   {\r
233     return print(getSeqsAsArray());\r
234   }\r
235 }\r