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