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