updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / FastaFile.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package jalview.io;
20
21 import jalview.datamodel.*;
22
23 import java.io.*;
24
25
26 /**
27  * DOCUMENT ME!
28  *
29  * @author $author$
30  * @version $Revision$
31  */
32 public class FastaFile extends AlignFile
33 {
34     /**
35      * Creates a new FastaFile object.
36      */
37     public FastaFile()
38     {
39     }
40
41     /**
42      * Creates a new FastaFile object.
43      *
44      * @param inFile DOCUMENT ME!
45      * @param type DOCUMENT ME!
46      *
47      * @throws IOException DOCUMENT ME!
48      */
49     public FastaFile(String inFile, String type) throws IOException
50     {
51         super(inFile, type);
52     }
53
54     /**
55      * DOCUMENT ME!
56      *
57      * @throws IOException DOCUMENT ME!
58      */
59     public void parse() throws IOException
60     {
61         StringBuffer sb = new StringBuffer();
62         int count = 0;
63
64         String line;
65         Sequence seq = null;
66
67         while ((line = nextLine()) != null)
68         {
69             line = line.trim();
70             if (line.length() > 0)
71             {
72                 if (line.charAt(0)=='>')
73                 {
74                     if (count != 0)
75                     {
76                       if (!isValidProteinSequence(sb.toString()))
77                       {
78                         throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
79                                               +" : "+seq.getName()
80                                               +" : "+invalidCharacter);
81                       }
82
83                        seq.setSequence(sb.toString());
84                        seqs.addElement(seq);
85                     }
86
87                     seq = parseId(line.substring(1));
88
89                     count++;
90                     sb = new StringBuffer();
91                 }
92                 else
93                 {
94                     sb.append(line);
95                 }
96             }
97         }
98
99         if (count > 0)
100         {
101             if (!isValidProteinSequence(sb.toString()))
102             {
103                 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
104                                       +" : "+seq.getName()
105                                       +" : "+invalidCharacter);
106             }
107
108             seq.setSequence(sb.toString());
109             seqs.addElement(seq);
110         }
111     }
112
113
114     /**
115      * DOCUMENT ME!
116      *
117      * @param s DOCUMENT ME!
118      * @param len DOCUMENT ME!
119      * @param gaps DOCUMENT ME!
120      * @param displayId DOCUMENT ME!
121      *
122      * @return DOCUMENT ME!
123      */
124     public String print(SequenceI[] s)
125     {
126         int len = 72;
127         StringBuffer out = new StringBuffer();
128         int i = 0;
129
130         while ((i < s.length) && (s[i] != null))
131         {
132             out.append(">" + printId(s[i]));
133             if(s[i].getDescription()!=null)
134               out.append(" "+s[i].getDescription());
135
136             out.append("\n");
137
138             int nochunks = (s[i].getLength() / len) + 1;
139
140             for (int j = 0; j < nochunks; j++)
141             {
142                 int start = j * len;
143                 int end = start + len;
144
145                 if (end < s[i].getLength())
146                 {
147                     out.append(s[i].getSequence(start, end) + "\n");
148                 }
149                 else if (start < s[i].getLength())
150                 {
151                     out.append(s[i].getSequence(start, s[i].getLength()) + "\n");
152                 }
153             }
154
155             i++;
156         }
157
158         return out.toString();
159     }
160
161     /**
162      * DOCUMENT ME!
163      *
164      * @return DOCUMENT ME!
165      */
166     public String print()
167     {
168         return print(getSeqsAsArray());
169     }
170 }