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