updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / BLCFile.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 import java.util.*;
26
27
28 /**
29  * DOCUMENT ME!
30  *
31  * @author $author$
32  * @version $Revision$
33  */
34 public class BLCFile extends AlignFile
35 {
36     Vector titles;
37
38     /**
39      * Creates a new BLCFile object.
40      */
41     public BLCFile()
42     {
43     }
44
45
46     /**
47      * Creates a new BLCFile object.
48      *
49      * @param inFile DOCUMENT ME!
50      * @param type DOCUMENT ME!
51      *
52      * @throws IOException DOCUMENT ME!
53      */
54     public BLCFile(String inFile, String type) throws IOException
55     {
56         super(inFile, type);
57     }
58
59     /**
60      * DOCUMENT ME!
61      */
62     public void initData()
63     {
64         super.initData();
65         titles = new Vector();
66     }
67
68     /**
69      * DOCUMENT ME!
70      */
71     public void parse() throws IOException
72     {
73         boolean idsFound = false;
74         Vector ids = new Vector();
75         StringBuffer[] seqstrings;
76         Vector starts = new Vector();
77         Vector ends = new Vector();
78
79         String line = null;
80
81             do
82             {
83                 line = nextLine();
84
85                 // seek end of ids
86                 if (line.indexOf("*") > -1)
87                 {
88                     idsFound = true;
89
90                     break;
91                 }
92
93                 int abracket = line.indexOf(">");
94
95                 if (abracket > -1)
96                 {
97                   if (line.indexOf(" ") > -1) //
98                   {
99                     line = line.substring(abracket + 1,
100                                           line.indexOf(" ", abracket + 1));
101                   }
102                   else
103                     line = line.substring(abracket+1);
104
105
106                   Sequence seq = parseId(line);
107                   ids.addElement(seq.getName());
108                   starts.addElement(seq.getStart() + "");
109                   ends.addElement(seq.getEnd() + "");
110                 }
111             }
112             while (!idsFound);
113
114             int starCol = line.indexOf("*");
115             seqstrings = new StringBuffer[ids.size()];
116
117             for (int i = 0; i < ids.size(); i++)
118             {
119                 if (seqstrings[i] == null)
120                 {
121                     seqstrings[i] = new StringBuffer();
122                 }
123             }
124
125             while ((line = nextLine()).indexOf("*") == -1)
126             {
127                 for (int i = 0; i < ids.size(); i++)
128                 {
129                     if (line.length() > (i + starCol))
130                     {
131                         seqstrings[i].append(line.charAt(i + starCol));
132                     }
133                 }
134             }
135
136             for (int i = 0; i < ids.size(); i++)
137             {
138               Sequence newSeq = new Sequence(ids.elementAt(i).toString(),
139                                              seqstrings[i].toString(),
140                                              Integer.parseInt(starts.elementAt(i).
141                                                               toString()),
142                                              Integer.parseInt(ends.elementAt(i).toString()));
143
144               if (!isValidProteinSequence(newSeq.getSequence()))
145               {
146                 throw new IOException(AppletFormatAdapter.INVALID_CHARACTERS
147                                +" : "+ newSeq.getName()
148                                +" : "+invalidCharacter);
149
150               }
151
152                 seqs.addElement(newSeq);
153             }
154
155     }
156
157     /**
158      * DOCUMENT ME!
159      *
160      * @return DOCUMENT ME!
161      */
162     public String print()
163     {
164         return print(getSeqsAsArray());
165     }
166
167     /**
168      * DOCUMENT ME!
169      *
170      * @param s DOCUMENT ME!
171      *
172      * @return DOCUMENT ME!
173      */
174     public String print(SequenceI[] s)
175     {
176         StringBuffer out = new StringBuffer();
177         /**
178          * A general parser for ids. Will look for dbrefs in
179          * Uniprot format source|id
180          * And also Jalview /start-end
181          *
182          * @String id Id to be parsed
183      */
184         int i = 0;
185         int max = -1;
186
187         while ((i < s.length) && (s[i] != null))
188         {
189             out.append(">" + printId(s[i]) +"\n");
190
191             if (s[i].getSequence().length() > max)
192             {
193                 max = s[i].getSequence().length();
194             }
195
196             i++;
197         }
198
199         out.append("* iteration 1\n");
200
201         for (int j = 0; j < max; j++)
202         {
203             i = 0;
204
205             while ((i < s.length) && (s[i] != null))
206             {
207                 if (s[i].getSequence().length() > j)
208                 {
209                     out.append(s[i].getSequence().substring(j, j + 1));
210                 }
211                 else
212                 {
213                     out.append("-");
214                 }
215
216                 i++;
217             }
218
219             out.append("\n");
220         }
221
222         out.append("*\n");
223
224         return out.toString();
225     }
226 }