FileParse object can be re-used to read different files concatenated together
[jalview.git] / src / jalview / io / FastaFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 import java.io.*;
22
23 import jalview.datamodel.*;
24
25 /**
26  * DOCUMENT ME!
27  *
28  * @author $author$
29  * @version $Revision$
30  */
31 public class FastaFile
32     extends AlignFile
33 {
34   /**
35    * Length of a sequence line
36    */
37   int len = 72;
38
39   StringBuffer out;
40
41   /**
42    * Creates a new FastaFile object.
43    */
44   public FastaFile()
45   {
46   }
47
48   /**
49    * Creates a new FastaFile object.
50    *
51    * @param inFile DOCUMENT ME!
52    * @param type DOCUMENT ME!
53    *
54    * @throws IOException DOCUMENT ME!
55    */
56   public FastaFile(String inFile, String type)
57       throws IOException
58   {
59     super(inFile, type);
60   }
61
62   public FastaFile(FileParse source) throws IOException
63   {
64     super(source);
65   }
66
67   /**
68    * DOCUMENT ME!
69    *
70    * @throws IOException DOCUMENT ME!
71    */
72   public void parse()
73       throws IOException
74   {
75     StringBuffer sb = new StringBuffer();
76     boolean firstLine = true;
77
78     String line;
79     Sequence seq = null;
80
81     boolean annotation = false;
82
83     while ( (line = nextLine()) != null)
84     {
85       line = line.trim();
86       if (line.length() > 0)
87       {
88         if (line.charAt(0) == '>')
89         {
90           if (line.startsWith(">#_"))
91           {
92             if (annotation)
93             {
94               Annotation[] anots = new Annotation[sb.length()];
95               String anotString = sb.toString();
96               for (int i = 0; i < sb.length(); i++)
97               {
98                 anots[i] = new Annotation(anotString.substring(i, i + 1),
99                                           null,
100                                           ' ', 0);
101               }
102               AlignmentAnnotation aa = new AlignmentAnnotation(
103                   seq.getName().substring(2), seq.getDescription(),
104                   anots);
105
106               annotations.addElement(aa);
107             }
108           }
109           else
110           {
111             annotation = false;
112           }
113
114           if (!firstLine)
115           {
116             seq.setSequence(sb.toString());
117
118             if (!annotation)
119             {
120               seqs.addElement(seq);
121             }
122           }
123
124           seq = parseId(line.substring(1));
125           firstLine = false;
126
127           sb = new StringBuffer();
128
129           if (line.startsWith(">#_"))
130           {
131             annotation = true;
132           }
133         }
134         else
135         {
136           sb.append(line);
137         }
138       }
139     }
140
141     if (annotation)
142     {
143       Annotation[] anots = new Annotation[sb.length()];
144       String anotString = sb.toString();
145       for (int i = 0; i < sb.length(); i++)
146       {
147         anots[i] = new Annotation(anotString.substring(i, i + 1),
148                                   null,
149                                   ' ', 0);
150       }
151       AlignmentAnnotation aa = new AlignmentAnnotation(
152           seq.getName().substring(2), seq.getDescription(),
153           anots);
154
155       annotations.addElement(aa);
156     }
157
158     else if (!firstLine)
159     {
160       seq.setSequence(sb.toString());
161       seqs.addElement(seq);
162     }
163   }
164
165   /**
166    * called by AppletFormatAdapter to generate
167    * an annotated alignment, rather than bare
168    * sequences.
169    * @param al
170    */
171   public void addAnnotations(Alignment al)
172   {
173     addProperties(al);
174     for (int i = 0; i < annotations.size(); i++)
175     {
176       AlignmentAnnotation aa = (AlignmentAnnotation) annotations.elementAt(i);
177       aa.setPadGaps(true, al.getGapCharacter());
178       al.addAnnotation( aa );
179     }
180   }
181
182
183   /**
184    * DOCUMENT ME!
185    *
186    * @param s DOCUMENT ME!
187    * @param len DOCUMENT ME!
188    * @param gaps DOCUMENT ME!
189    * @param displayId DOCUMENT ME!
190    *
191    * @return DOCUMENT ME!
192    */
193   public String print(SequenceI[] s)
194   {
195     out = new StringBuffer();
196     int i = 0;
197
198     while ( (i < s.length) && (s[i] != null))
199     {
200       out.append(">" + printId(s[i]));
201       if (s[i].getDescription() != null)
202       {
203         out.append(" " + s[i].getDescription());
204       }
205
206       out.append("\n");
207
208       int nochunks = (s[i].getLength() / len) + 1;
209
210       for (int j = 0; j < nochunks; j++)
211       {
212         int start = j * len;
213         int end = start + len;
214
215         if (end < s[i].getLength())
216         {
217           out.append(s[i].getSequenceAsString(start, end) + "\n");
218         }
219         else if (start < s[i].getLength())
220         {
221           out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n");
222         }
223       }
224
225       i++;
226     }
227
228     return out.toString();
229   }
230
231   /**
232    * DOCUMENT ME!
233    *
234    * @return DOCUMENT ME!
235    */
236   public String print()
237   {
238     return print(getSeqsAsArray());
239   }
240 }