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