v2
[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 javax.xml.parsers.ParserConfigurationException;
23
24 import org.xml.sax.SAXException;
25
26 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
27 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
28 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
29
30 import jalview.datamodel.*;
31
32 /**
33  * DOCUMENT ME!
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 public class FastaFile extends AlignFile
39 {
40   /**
41    * Length of a sequence line
42    */
43   int len = 72;
44
45   StringBuffer out;
46
47   /**
48    * Creates a new FastaFile object.
49    */
50   public FastaFile()
51   {
52   }
53
54   /**
55    * Creates a new FastaFile object.
56    * 
57    * @param inFile
58    *          DOCUMENT ME!
59    * @param type
60    *          DOCUMENT ME!
61    * 
62    * @throws IOException
63    *           DOCUMENT ME!
64  * @throws SAXException 
65  * @throws ParserConfigurationException 
66  * @throws ExceptionFileFormatOrSyntax 
67  * @throws ExceptionLoadingFailed 
68  * @throws ExceptionPermissionDenied 
69  * @throws InterruptedException 
70    */
71   public FastaFile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
72   {
73     super(inFile, type);
74   }
75
76   public FastaFile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
77   {
78     super(source);
79   }
80
81   /**
82    * DOCUMENT ME!
83    * 
84    * @throws IOException
85    *           DOCUMENT ME!
86    */
87   public void parse() throws IOException
88   {
89     StringBuffer sb = new StringBuffer();
90     boolean firstLine = true;
91
92     String line;
93     Sequence seq = null;
94
95     boolean annotation = false;
96
97     while ((line = nextLine()) != null)
98     {
99       line = line.trim();
100       if (line.length() > 0)
101       {
102         if (line.charAt(0) == '>')
103         {
104           if (line.startsWith(">#_"))
105           {
106             if (annotation)
107             {
108               Annotation[] anots = new Annotation[sb.length()];
109               String anotString = sb.toString();
110               for (int i = 0; i < sb.length(); i++)
111               {
112                 anots[i] = new Annotation(anotString.substring(i, i + 1),
113                         null, ' ', 0);
114               }
115               AlignmentAnnotation aa = new AlignmentAnnotation(seq
116                       .getName().substring(2), seq.getDescription(), anots);
117
118               annotations.addElement(aa);
119             }
120           }
121           else
122           {
123             annotation = false;
124           }
125
126           if (!firstLine)
127           {
128             seq.setSequence(sb.toString());
129
130             if (!annotation)
131             {
132               seqs.addElement(seq);
133             }
134           }
135
136           seq = parseId(line.substring(1));
137           firstLine = false;
138
139           sb = new StringBuffer();
140
141           if (line.startsWith(">#_"))
142           {
143             annotation = true;
144           }
145         }
146         else
147         {
148           sb.append(line);
149         }
150       }
151     }
152
153     if (annotation)
154     {
155       Annotation[] anots = new Annotation[sb.length()];
156       String anotString = sb.toString();
157       for (int i = 0; i < sb.length(); i++)
158       {
159         anots[i] = new Annotation(anotString.substring(i, i + 1), null,
160                 ' ', 0);
161       }
162       AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
163               .substring(2), seq.getDescription(), anots);
164
165       annotations.addElement(aa);
166     }
167
168     else if (!firstLine)
169     {
170       seq.setSequence(sb.toString());
171       seqs.addElement(seq);
172     }
173   }
174
175   /**
176    * called by AppletFormatAdapter to generate an annotated alignment, rather
177    * than bare sequences.
178    * 
179    * @param al
180    */
181   public void addAnnotations(Alignment al)
182   {
183     addProperties(al);
184     for (int i = 0; i < annotations.size(); i++)
185     {
186       AlignmentAnnotation aa = (AlignmentAnnotation) annotations
187               .elementAt(i);
188       aa.setPadGaps(true, al.getGapCharacter());
189       al.addAnnotation(aa);
190     }
191   }
192
193   /**
194    * DOCUMENT ME!
195    * 
196    * @param s
197    *          DOCUMENT ME!
198    * @param len
199    *          DOCUMENT ME!
200    * @param gaps
201    *          DOCUMENT ME!
202    * @param displayId
203    *          DOCUMENT ME!
204    * 
205    * @return DOCUMENT ME!
206    */
207   public String print(SequenceI[] s)
208   {
209     out = new StringBuffer();
210     int i = 0;
211
212     while ((i < s.length) && (s[i] != null))
213     {
214       out.append(">" + printId(s[i]));
215       if (s[i].getDescription() != null)
216       {
217         out.append(" " + s[i].getDescription());
218       }
219
220       out.append(newline);
221
222       int nochunks = (s[i].getLength() / len) + 1;
223
224       for (int j = 0; j < nochunks; j++)
225       {
226         int start = j * len;
227         int end = start + len;
228
229         if (end < s[i].getLength())
230         {
231           out.append(s[i].getSequenceAsString(start, end) + newline);
232         }
233         else if (start < s[i].getLength())
234         {
235           out.append(s[i].getSequenceAsString(start, s[i].getLength())
236                   + newline);
237         }
238       }
239
240       i++;
241     }
242
243     return out.toString();
244   }
245
246   /**
247    * DOCUMENT ME!
248    * 
249    * @return DOCUMENT ME!
250    */
251   public String print()
252   {
253     return print(getSeqsAsArray());
254   }
255 }