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