update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / FastaFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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  */
18 package jalview.io;
19
20 import java.io.*;
21
22 import jalview.datamodel.*;
23
24 /**
25  * DOCUMENT ME!
26  * 
27  * @author $author$
28  * @version $Revision$
29  */
30 public class FastaFile extends AlignFile
31 {
32   /**
33    * Length of a sequence line
34    */
35   int len = 72;
36
37   StringBuffer out;
38
39   /**
40    * Creates a new FastaFile object.
41    */
42   public FastaFile()
43   {
44   }
45
46   /**
47    * Creates a new FastaFile object.
48    * 
49    * @param inFile
50    *          DOCUMENT ME!
51    * @param type
52    *          DOCUMENT ME!
53    * 
54    * @throws IOException
55    *           DOCUMENT ME!
56    */
57   public FastaFile(String inFile, String type) throws IOException
58   {
59     super(inFile, type);
60   }
61
62   public FastaFile(FileParse source) throws IOException
63   {
64     super(source);
65   }
66
67   /**
68    * DOCUMENT ME!
69    * 
70    * @throws IOException
71    *           DOCUMENT ME!
72    */
73   public void parse() throws IOException
74   {
75     StringBuffer sb = new StringBuffer();
76     boolean firstLine = true;
77
78     String line;
79     Sequence seq = null;
80
81     boolean annotation = false;
82
83     while ((line = nextLine()) != null)
84     {
85       line = line.trim();
86       if (line.length() > 0)
87       {
88         if (line.charAt(0) == '>')
89         {
90           if (line.startsWith(">#_"))
91           {
92             if (annotation)
93             {
94               Annotation[] anots = new Annotation[sb.length()];
95               String anotString = sb.toString();
96               for (int i = 0; i < sb.length(); i++)
97               {
98                 anots[i] = new Annotation(anotString.substring(i, i + 1),
99                         null, ' ', 0);
100               }
101               AlignmentAnnotation aa = new AlignmentAnnotation(seq
102                       .getName().substring(2), seq.getDescription(), anots);
103
104               annotations.addElement(aa);
105             }
106           }
107           else
108           {
109             annotation = false;
110           }
111
112           if (!firstLine)
113           {
114             seq.setSequence(sb.toString());
115
116             if (!annotation)
117             {
118               seqs.addElement(seq);
119             }
120           }
121
122           seq = parseId(line.substring(1));
123           firstLine = false;
124
125           sb = new StringBuffer();
126
127           if (line.startsWith(">#_"))
128           {
129             annotation = true;
130           }
131         }
132         else
133         {
134           sb.append(line);
135         }
136       }
137     }
138
139     if (annotation)
140     {
141       Annotation[] anots = new Annotation[sb.length()];
142       String anotString = sb.toString();
143       for (int i = 0; i < sb.length(); i++)
144       {
145         anots[i] = new Annotation(anotString.substring(i, i + 1), null,
146                 ' ', 0);
147       }
148       AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
149               .substring(2), seq.getDescription(), anots);
150
151       annotations.addElement(aa);
152     }
153
154     else if (!firstLine)
155     {
156       seq.setSequence(sb.toString());
157       seqs.addElement(seq);
158     }
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 }