JAL-1432 updated copyright notices
[jalview.git] / src / jalview / io / FastaFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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;
80     Sequence seq = null;
81
82     boolean annotation = false;
83
84     while ((line = nextLine()) != null)
85     {
86       line = line.trim();
87       if (line.length() > 0)
88       {
89         if (line.charAt(0) == '>')
90         {
91           if (line.startsWith(">#_"))
92           {
93             if (annotation)
94             {
95               Annotation[] anots = new Annotation[sb.length()];
96               String anotString = sb.toString();
97               for (int i = 0; i < sb.length(); i++)
98               {
99                 anots[i] = new Annotation(anotString.substring(i, i + 1),
100                         null, ' ', 0);
101               }
102               AlignmentAnnotation aa = new AlignmentAnnotation(seq
103                       .getName().substring(2), seq.getDescription(), anots);
104
105               annotations.addElement(aa);
106             }
107           }
108           else
109           {
110             annotation = false;
111           }
112
113           if (!firstLine)
114           {
115             seq.setSequence(sb.toString());
116
117             if (!annotation)
118             {
119               seqs.addElement(seq);
120             }
121           }
122
123           seq = parseId(line.substring(1));
124           firstLine = false;
125
126           sb = new StringBuffer();
127
128           if (line.startsWith(">#_"))
129           {
130             annotation = true;
131           }
132         }
133         else
134         {
135           sb.append(line);
136         }
137       }
138     }
139
140     if (annotation)
141     {
142       Annotation[] anots = new Annotation[sb.length()];
143       String anotString = sb.toString();
144       for (int i = 0; i < sb.length(); i++)
145       {
146         anots[i] = new Annotation(anotString.substring(i, i + 1), null,
147                 ' ', 0);
148       }
149       AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
150               .substring(2), seq.getDescription(), anots);
151
152       annotations.addElement(aa);
153     }
154
155     else if (!firstLine)
156     {
157       seq.setSequence(sb.toString());
158       seqs.addElement(seq);
159     }
160   }
161
162   /**
163    * called by AppletFormatAdapter to generate an annotated alignment, rather
164    * than bare sequences.
165    * 
166    * @param al
167    */
168   public void addAnnotations(Alignment al)
169   {
170     addProperties(al);
171     for (int i = 0; i < annotations.size(); i++)
172     {
173       AlignmentAnnotation aa = (AlignmentAnnotation) annotations
174               .elementAt(i);
175       aa.setPadGaps(true, al.getGapCharacter());
176       al.addAnnotation(aa);
177     }
178   }
179
180   /**
181    * DOCUMENT ME!
182    * 
183    * @param s
184    *          DOCUMENT ME!
185    * @param len
186    *          DOCUMENT ME!
187    * @param gaps
188    *          DOCUMENT ME!
189    * @param displayId
190    *          DOCUMENT ME!
191    * 
192    * @return DOCUMENT ME!
193    */
194   public String print(SequenceI[] s)
195   {
196     out = new StringBuffer();
197     int i = 0;
198
199     while ((i < s.length) && (s[i] != null))
200     {
201       out.append(">" + printId(s[i]));
202       if (s[i].getDescription() != null)
203       {
204         out.append(" " + s[i].getDescription());
205       }
206
207       out.append(newline);
208
209       int nochunks = (s[i].getLength() / len) + 1;
210
211       for (int j = 0; j < nochunks; j++)
212       {
213         int start = j * len;
214         int end = start + len;
215
216         if (end < s[i].getLength())
217         {
218           out.append(s[i].getSequenceAsString(start, end) + newline);
219         }
220         else if (start < s[i].getLength())
221         {
222           out.append(s[i].getSequenceAsString(start, s[i].getLength())
223                   + newline);
224         }
225       }
226
227       i++;
228     }
229
230     return out.toString();
231   }
232
233   /**
234    * DOCUMENT ME!
235    * 
236    * @return DOCUMENT ME!
237    */
238   public String print()
239   {
240     return print(getSeqsAsArray());
241   }
242 }