file format enum wip changes
[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 sourceType
59    *          DOCUMENT ME!
60    * 
61    * @throws IOException
62    *           DOCUMENT ME!
63    */
64   public FastaFile(String inFile, DataSourceType sourceType)
65           throws IOException
66   {
67     super(inFile, sourceType);
68   }
69
70   public FastaFile(FileParse source) throws IOException
71   {
72     super(source);
73   }
74
75   /**
76    * DOCUMENT ME!
77    * 
78    * @throws IOException
79    *           DOCUMENT ME!
80    */
81   @Override
82   public void parse() throws IOException
83   {
84     StringBuffer sb = new StringBuffer();
85     boolean firstLine = true;
86
87     String line, uline;
88     Sequence seq = null;
89
90     boolean annotation = false;
91
92     while ((uline = nextLine()) != null)
93     {
94       line = uline.trim();
95       if (line.length() > 0)
96       {
97         if (line.charAt(0) == '>')
98         {
99           if (line.startsWith(">#_"))
100           {
101             if (annotation)
102             {
103               annotations.addElement(makeAnnotation(seq, sb));
104             }
105           }
106           else
107           {
108             annotation = false;
109           }
110
111           if (!firstLine)
112           {
113             seq.setSequence(sb.toString());
114
115             if (!annotation)
116             {
117               seqs.addElement(seq);
118             }
119           }
120
121           seq = parseId(line.substring(1));
122           firstLine = false;
123
124           sb = new StringBuffer();
125
126           if (line.startsWith(">#_"))
127           {
128             annotation = true;
129           }
130         }
131         else
132         {
133           sb.append(annotation ? uline : line);
134         }
135       }
136     }
137
138     if (annotation)
139     {
140       annotations.addElement(makeAnnotation(seq, sb));
141     }
142
143     else if (!firstLine)
144     {
145       seq.setSequence(sb.toString());
146       seqs.addElement(seq);
147     }
148   }
149
150   private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
151   {
152     Annotation[] anots = new Annotation[sb.length()];
153     char cb;
154     for (int i = 0; i < anots.length; i++)
155     {
156       char cn = sb.charAt(i);
157       if (cn != ' ')
158       {
159         anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
160       }
161     }
162     AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
163             .substring(2), seq.getDescription(), anots);
164     return aa;
165   }
166
167   /**
168    * called by AppletFormatAdapter to generate an annotated alignment, rather
169    * than bare sequences.
170    * 
171    * @param al
172    */
173   public void addAnnotations(Alignment al)
174   {
175     addProperties(al);
176     for (int i = 0; i < annotations.size(); i++)
177     {
178       AlignmentAnnotation aa = annotations
179               .elementAt(i);
180       aa.setPadGaps(true, al.getGapCharacter());
181       al.addAnnotation(aa);
182     }
183   }
184
185   /**
186    * DOCUMENT ME!
187    * 
188    * @param s
189    *          DOCUMENT ME!
190    * @param len
191    *          DOCUMENT ME!
192    * @param gaps
193    *          DOCUMENT ME!
194    * @param displayId
195    *          DOCUMENT ME!
196    * 
197    * @return DOCUMENT ME!
198    */
199   public String print(SequenceI[] s)
200   {
201     out = new StringBuffer();
202     int i = 0;
203
204     while ((i < s.length) && (s[i] != null))
205     {
206       out.append(">" + printId(s[i]));
207       if (s[i].getDescription() != null)
208       {
209         out.append(" " + s[i].getDescription());
210       }
211
212       out.append(newline);
213
214       int nochunks = (s[i].getLength() / len)
215               + (s[i].getLength() % len > 0 ? 1 : 0);
216
217       for (int j = 0; j < nochunks; j++)
218       {
219         int start = j * len;
220         int end = start + len;
221
222         if (end < s[i].getLength())
223         {
224           out.append(s[i].getSequenceAsString(start, end) + newline);
225         }
226         else if (start < s[i].getLength())
227         {
228           out.append(s[i].getSequenceAsString(start, s[i].getLength())
229                   + newline);
230         }
231       }
232
233       i++;
234     }
235
236     return out.toString();
237   }
238
239   /**
240    * DOCUMENT ME!
241    * 
242    * @return DOCUMENT ME!
243    */
244   @Override
245   public String print()
246   {
247     return print(getSeqsAsArray());
248   }
249 }