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