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