java 1.1 compliance
[jalview.git] / src / jalview / io / BLCFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 java.io.*;
22 import java.util.*;
23
24 import jalview.datamodel.*;
25
26 /**
27  * DOCUMENT ME!
28  *
29  * @author $author$
30  * @version $Revision$
31  */
32 public class BLCFile
33 extends AlignFile
34 {
35   Vector titles;
36
37   /**
38    * Creates a new BLCFile object.
39    */
40   public BLCFile()
41   {
42   }
43
44   /**
45    * Creates a new BLCFile object.
46    *
47    * @param inFile DOCUMENT ME!
48    * @param type DOCUMENT ME!
49    *
50    * @throws IOException DOCUMENT ME!
51    */
52   public BLCFile(String inFile, String type)
53   throws IOException
54   {
55     super(inFile, type);
56   }
57
58   /**
59    * DOCUMENT ME!
60    */
61   public void initData()
62   {
63     super.initData();
64     titles = new Vector();
65   }
66   /**
67    * Control the number of block iterations to skip before returning.
68    * set to 0 to read first block file entry only
69    * set to -1 to read last block file entry only
70    * set to greater than zero to skip at most that many entries before parsing
71    */
72   int iterationSkips=0;
73   /**
74    * The iteration number for the alignment actually parsed from the blc file 
75    */
76   int iterationCount=0;
77
78   /**
79    * DOCUMENT ME!
80    */
81   public void parse()
82   throws IOException
83   {
84     StringBuffer[] seqstrings=null;
85     if (suffix!=null) {
86       try {
87         iterationSkips = Integer.parseInt(suffix); 
88       } catch (NumberFormatException e) {
89         iterationSkips = 0; // first
90       }
91     }
92
93     String line = null;
94
95     do {
96       boolean idsFound = false;
97       boolean newids = false;
98         // search for ID header.
99       do
100       {
101         line = nextLine();
102         if (line==null)
103           break;
104         // seek end of ids
105         if (line.indexOf("*") > -1)
106         {
107           idsFound = true;
108
109           break;
110         }
111         
112         int abracket = line.indexOf(">");
113
114         if (abracket > -1)
115         {
116
117           if (iterationCount>0 && !newids) {
118             // we have a new set of IDs to record.
119             newids = true;
120             seqs.removeAllElements(); 
121           }
122
123           line = line.substring(abracket + 1);
124
125           Sequence seq = parseId(line);
126           seqs.addElement(seq);
127         }
128       }
129       while (!idsFound);
130       if (line==null)
131         break; // end of file.
132       int starCol = line.indexOf("*");
133       seqstrings = new StringBuffer[seqs.size()];
134
135       for (int i = 0; i < seqs.size(); i++)
136       {
137         if (seqstrings[i] == null)
138         {
139           seqstrings[i] = new StringBuffer();
140         }
141       }
142
143       try {
144         line = nextLine();
145         while (line!=null && line.indexOf("*") == -1)
146         {
147           for (int i = 0; i < seqs.size(); i++)
148           {
149             if (line.length() > (i + starCol))
150             {
151               seqstrings[i].append(line.charAt(i + starCol));
152             }
153           }
154           line = nextLine();
155         }
156       } catch (IOException e) {
157         if (iterationCount==0) {
158           throw(e); // otherwise we've just run out of iterations.
159         } else {
160           iterationSkips=0;
161         }
162       }
163       iterationCount++;
164     } while (--iterationSkips!=-1);
165     
166     for (int i = 0; i < seqs.size(); i++)
167     {
168       Sequence newSeq = (Sequence) seqs.elementAt(i);
169
170       newSeq.setSequence(seqstrings[i].toString());
171     }
172
173   }
174
175   /**
176    * DOCUMENT ME!
177    *
178    * @return DOCUMENT ME!
179    */
180   public String print()
181   {
182     return print(getSeqsAsArray());
183   }
184
185   /**
186    * DOCUMENT ME!
187    *
188    * @param s DOCUMENT ME!
189    *
190    * @return DOCUMENT ME!
191    */
192   public String print(SequenceI[] s)
193   {
194     StringBuffer out = new StringBuffer();
195     /**
196      * A general parser for ids. Will look for dbrefs in
197      * Uniprot format source|id
198      * And also Jalview /start-end
199      *
200      * @String id Id to be parsed
201      */
202     int i = 0;
203     int max = -1;
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("\n");
214
215       if (s[i].getSequence().length > max)
216       {
217         max = s[i].getSequence().length;
218       }
219
220       i++;
221     }
222
223     out.append("* iteration 1\n");
224
225     for (int j = 0; j < max; j++)
226     {
227       i = 0;
228
229       while ( (i < s.length) && (s[i] != null))
230       {
231         if (s[i].getSequence().length > j)
232         {
233           out.append(s[i].getSequenceAsString(j, j + 1));
234         }
235         else
236         {
237           out.append("-");
238         }
239
240         i++;
241       }
242
243       out.append("\n");
244     }
245
246     out.append("*\n");
247
248     return out.toString();
249   }
250 }