JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / io / FastaFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceI;
28
29 import java.io.IOException;
30
31 /**
32  * DOCUMENT ME!
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class FastaFile extends AlignFile
38 {
39   /**
40    * Length of a sequence line
41    */
42   int len = 72;
43
44   StringBuffer out;
45
46   /**
47    * Creates a new FastaFile object.
48    */
49   public FastaFile()
50   {
51   }
52
53   /**
54    * Creates a new FastaFile object.
55    * 
56    * @param inFile
57    *          DOCUMENT ME!
58    * @param type
59    *          DOCUMENT ME!
60    * 
61    * @throws IOException
62    *           DOCUMENT ME!
63    */
64   public FastaFile(String inFile, String type) throws IOException
65   {
66     super(inFile, type);
67   }
68
69   public FastaFile(FileParse source) throws IOException
70   {
71     super(source);
72   }
73
74   /**
75    * DOCUMENT ME!
76    * 
77    * @throws IOException
78    *           DOCUMENT ME!
79    */
80   @Override
81   public void parse() throws IOException
82   {
83     StringBuffer sb = new StringBuffer();
84     boolean firstLine = true;
85
86     String line, uline;
87     Sequence seq = null;
88
89     boolean annotation = false;
90
91     while ((uline = nextLine()) != null)
92     {
93       line = uline.trim();
94       if (line.length() > 0)
95       {
96         if (line.charAt(0) == '>')
97         {
98           if (line.startsWith(">#_"))
99           {
100             if (annotation)
101             {
102               annotations.addElement(makeAnnotation(seq, sb));
103             }
104           }
105           else
106           {
107             annotation = false;
108           }
109
110           if (!firstLine)
111           {
112             seq.setSequence(sb.toString());
113
114             if (!annotation)
115             {
116               seqs.addElement(seq);
117             }
118           }
119
120           seq = parseId(line.substring(1));
121           firstLine = false;
122
123           sb = new StringBuffer();
124
125           if (line.startsWith(">#_"))
126           {
127             annotation = true;
128           }
129         }
130         else
131         {
132           sb.append(annotation ? uline : line);
133         }
134       }
135     }
136
137     if (annotation)
138     {
139       annotations.addElement(makeAnnotation(seq, sb));
140     }
141
142     else if (!firstLine)
143     {
144       seq.setSequence(sb.toString());
145       seqs.addElement(seq);
146     }
147   }
148
149   private AlignmentAnnotation makeAnnotation(SequenceI seq, StringBuffer sb)
150   {
151     Annotation[] anots = new Annotation[sb.length()];
152     char cb;
153     for (int i = 0; i < anots.length; i++)
154     {
155       char cn = sb.charAt(i);
156       if (cn != ' ')
157       {
158         anots[i] = new Annotation("" + cn, null, ' ', Float.NaN);
159       }
160     }
161     AlignmentAnnotation aa = new AlignmentAnnotation(seq.getName()
162             .substring(2), seq.getDescription(), anots);
163     return aa;
164   }
165
166   /**
167    * called by AppletFormatAdapter to generate an annotated alignment, rather
168    * than bare sequences.
169    * 
170    * @param al
171    */
172   public void addAnnotations(Alignment al)
173   {
174     addProperties(al);
175     for (int i = 0; i < annotations.size(); i++)
176     {
177       AlignmentAnnotation aa = annotations.elementAt(i);
178       aa.setPadGaps(true, al.getGapCharacter());
179       al.addAnnotation(aa);
180     }
181   }
182
183   /**
184    * DOCUMENT ME!
185    * 
186    * @param s
187    *          DOCUMENT ME!
188    * @param len
189    *          DOCUMENT ME!
190    * @param gaps
191    *          DOCUMENT ME!
192    * @param displayId
193    *          DOCUMENT ME!
194    * 
195    * @return DOCUMENT ME!
196    */
197   public String print(SequenceI[] s)
198   {
199     out = new StringBuffer();
200     int i = 0;
201
202     while ((i < s.length) && (s[i] != null))
203     {
204       out.append(">" + printId(s[i]));
205       if (s[i].getDescription() != null)
206       {
207         out.append(" " + s[i].getDescription());
208       }
209
210       out.append(newline);
211
212       int nochunks = (s[i].getLength() / len)
213               + (s[i].getLength() % len > 0 ? 1 : 0);
214
215       for (int j = 0; j < nochunks; j++)
216       {
217         int start = j * len;
218         int end = start + len;
219
220         if (end < s[i].getLength())
221         {
222           out.append(s[i].getSequenceAsString(start, end) + newline);
223         }
224         else if (start < s[i].getLength())
225         {
226           out.append(s[i].getSequenceAsString(start, s[i].getLength())
227                   + newline);
228         }
229       }
230
231       i++;
232     }
233
234     return out.toString();
235   }
236
237   /**
238    * DOCUMENT ME!
239    * 
240    * @return DOCUMENT ME!
241    */
242   @Override
243   public String print()
244   {
245     return print(getSeqsAsArray());
246   }
247 }