JAL-1620 version bump and release notes
[jalview.git] / src / jalview / io / FastaFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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 java.io.*;
24
25 import jalview.datamodel.*;
26
27 /**
28  * DOCUMENT ME!
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class FastaFile extends AlignFile
34 {
35   /**
36    * Length of a sequence line
37    */
38   int len = 72;
39
40   StringBuffer out;
41
42   /**
43    * Creates a new FastaFile object.
44    */
45   public FastaFile()
46   {
47   }
48
49   /**
50    * Creates a new FastaFile object.
51    * 
52    * @param inFile
53    *          DOCUMENT ME!
54    * @param type
55    *          DOCUMENT ME!
56    * 
57    * @throws IOException
58    *           DOCUMENT ME!
59    */
60   public FastaFile(String inFile, String type) throws IOException
61   {
62     super(inFile, type);
63   }
64
65   public FastaFile(FileParse source) throws IOException
66   {
67     super(source);
68   }
69
70   /**
71    * DOCUMENT ME!
72    * 
73    * @throws IOException
74    *           DOCUMENT ME!
75    */
76   public void parse() throws IOException
77   {
78     StringBuffer sb = new StringBuffer();
79     boolean firstLine = true;
80
81     String line, uline;
82     Sequence seq = null;
83
84     boolean annotation = false;
85
86     while ((uline = nextLine()) != null)
87     {
88       line = uline.trim();
89       if (line.length() > 0)
90       {
91         if (line.charAt(0) == '>')
92         {
93           if (line.startsWith(">#_"))
94           {
95             if (annotation)
96             {
97               annotations.addElement(makeAnnotation(seq, sb));
98             }
99           }
100           else
101           {
102             annotation = false;
103           }
104
105           if (!firstLine)
106           {
107             seq.setSequence(sb.toString());
108
109             if (!annotation)
110             {
111               seqs.addElement(seq);
112             }
113           }
114
115           seq = parseId(line.substring(1));
116           firstLine = false;
117
118           sb = new StringBuffer();
119
120           if (line.startsWith(">#_"))
121           {
122             annotation = true;
123           }
124         }
125         else
126         {
127           sb.append(annotation ? uline : line);
128         }
129       }
130     }
131
132     if (annotation)
133     {
134       annotations.addElement(makeAnnotation(seq, sb));
135     }
136
137     else if (!firstLine)
138     {
139       seq.setSequence(sb.toString());
140       seqs.addElement(seq);
141     }
142   }
143
144   private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
145   {
146     Annotation[] anots = new Annotation[sb.length()];
147     char cb;
148     for (int i = 0; i < anots.length; i++)
149     {
150       char cn = sb.charAt(i);
151       if (cn != ' ')
152       {
153         anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
154       }
155     }
156     AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
157             .substring(2), seq.getDescription(), anots);
158     return aa;
159   }
160
161   /**
162    * called by AppletFormatAdapter to generate an annotated alignment, rather
163    * than bare sequences.
164    * 
165    * @param al
166    */
167   public void addAnnotations(Alignment al)
168   {
169     addProperties(al);
170     for (int i = 0; i < annotations.size(); i++)
171     {
172       AlignmentAnnotation aa = (AlignmentAnnotation) annotations
173               .elementAt(i);
174       aa.setPadGaps(true, al.getGapCharacter());
175       al.addAnnotation(aa);
176     }
177   }
178
179   /**
180    * DOCUMENT ME!
181    * 
182    * @param s
183    *          DOCUMENT ME!
184    * @param len
185    *          DOCUMENT ME!
186    * @param gaps
187    *          DOCUMENT ME!
188    * @param displayId
189    *          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(newline);
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) + newline);
218         }
219         else if (start < s[i].getLength())
220         {
221           out.append(s[i].getSequenceAsString(start, s[i].getLength())
222                   + newline);
223         }
224       }
225
226       i++;
227     }
228
229     return out.toString();
230   }
231
232   /**
233    * DOCUMENT ME!
234    * 
235    * @return DOCUMENT ME!
236    */
237   public String print()
238   {
239     return print(getSeqsAsArray());
240   }
241 }