update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / BLCFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.io;
19
20 import java.io.*;
21 import java.util.*;
22
23 import jalview.datamodel.*;
24
25 /**
26  * DOCUMENT ME!
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class BLCFile extends AlignFile
32 {
33   Vector titles;
34
35   /**
36    * Creates a new BLCFile object.
37    */
38   public BLCFile()
39   {
40   }
41
42   /**
43    * Creates a new BLCFile object.
44    * 
45    * @param inFile
46    *          DOCUMENT ME!
47    * @param type
48    *          DOCUMENT ME!
49    * 
50    * @throws IOException
51    *           DOCUMENT ME!
52    */
53   public BLCFile(String inFile, String type) throws IOException
54   {
55     super(inFile, type);
56   }
57
58   public BLCFile(FileParse source) throws IOException
59   {
60     super(source);
61   }
62
63   /**
64    * DOCUMENT ME!
65    */
66   public void initData()
67   {
68     super.initData();
69     titles = new Vector();
70   }
71
72   /**
73    * Control the number of block iterations to skip before returning. set to 0
74    * to read first block file entry only set to -1 to read last block file entry
75    * only set to greater than zero to skip at most that many entries before
76    * parsing
77    */
78   int iterationSkips = 0;
79
80   /**
81    * The iteration number for the alignment actually parsed from the blc file
82    */
83   int iterationCount = 0;
84
85   /**
86    * DOCUMENT ME!
87    */
88   public void parse() throws IOException
89   {
90     StringBuffer headerLines = new StringBuffer();
91     int numHeaderLines = 0; // number of lines appended.
92     StringBuffer[] seqstrings = null;
93     if (suffix != null)
94     {
95       try
96       {
97         iterationSkips = Integer.parseInt(suffix);
98       } catch (NumberFormatException e)
99       {
100         iterationSkips = 0; // first
101       }
102     }
103
104     String line = null;
105
106     do
107     {
108       boolean idsFound = false;
109       boolean newids = false;
110       // search for ID header.
111       do
112       {
113         line = nextLine();
114         if (line == null)
115           break;
116         // seek end of ids
117         if (line.indexOf("*") > -1)
118         {
119           idsFound = true;
120
121           break;
122         }
123
124         int abracket = line.indexOf(">");
125
126         if (abracket > -1)
127         {
128
129           if (iterationCount > 0 && !newids)
130           {
131             // we have a new set of IDs to record.
132             newids = true;
133             seqs.removeAllElements();
134           }
135
136           line = line.substring(abracket + 1);
137
138           Sequence seq = parseId(line);
139           seqs.addElement(seq);
140         }
141         else
142         {
143           // header lines - keep them for the alignment comments.
144           headerLines.append(line);
145           headerLines.append(newline);
146           numHeaderLines++;
147         }
148       } while (!idsFound);
149       if (line == null)
150         break; // end of file.
151       int starCol = line.indexOf("*");
152       seqstrings = new StringBuffer[seqs.size()];
153
154       for (int i = 0; i < seqs.size(); i++)
155       {
156         if (seqstrings[i] == null)
157         {
158           seqstrings[i] = new StringBuffer();
159         }
160       }
161
162       try
163       {
164         line = nextLine();
165         while (line != null && line.indexOf("*") == -1)
166         {
167           for (int i = 0; i < seqs.size(); i++)
168           {
169             if (line.length() > (i + starCol))
170             {
171               seqstrings[i].append(line.charAt(i + starCol));
172             }
173           }
174           line = nextLine();
175         }
176       } catch (IOException e)
177       {
178         if (iterationCount == 0)
179         {
180           throw (e); // otherwise we've just run out of iterations.
181         }
182         else
183         {
184           iterationSkips = 0;
185         }
186       }
187       iterationCount++;
188     } while (--iterationSkips != -1);
189
190     for (int i = 0; i < seqs.size(); i++)
191     {
192       Sequence newSeq = (Sequence) seqs.elementAt(i);
193
194       newSeq.setSequence(seqstrings[i].toString());
195     }
196     if (seqs.size() > 0)
197     {
198       if (headerLines.length() > 1 + numHeaderLines) // could see if buffer is
199         // just whitespace or not.
200         setAlignmentProperty("Comments", headerLines.toString());
201       setAlignmentProperty("iteration", "" + iterationCount);
202     }
203   }
204
205   /**
206    * DOCUMENT ME!
207    * 
208    * @return DOCUMENT ME!
209    */
210   public String print()
211   {
212     return print(getSeqsAsArray());
213   }
214
215   /**
216    * DOCUMENT ME!
217    * 
218    * @param s
219    *          DOCUMENT ME!
220    * 
221    * @return DOCUMENT ME!
222    */
223   public String print(SequenceI[] s)
224   {
225     StringBuffer out = new StringBuffer();
226     /**
227      * A general parser for ids. Will look for dbrefs in Uniprot format
228      * source|id And also Jalview /start-end
229      * 
230      * @String id Id to be parsed
231      */
232     int i = 0;
233     int max = -1;
234
235     while ((i < s.length) && (s[i] != null))
236     {
237       out.append(">" + printId(s[i]));
238       if (s[i].getDescription() != null)
239       {
240         out.append(" " + s[i].getDescription());
241       }
242
243       out.append(newline);
244
245       if (s[i].getSequence().length > max)
246       {
247         max = s[i].getSequence().length;
248       }
249
250       i++;
251     }
252
253     out.append("* iteration 1");
254     out.append(newline);
255
256     for (int j = 0; j < max; j++)
257     {
258       i = 0;
259
260       while ((i < s.length) && (s[i] != null))
261       {
262         if (s[i].getSequence().length > j)
263         {
264           out.append(s[i].getSequenceAsString(j, j + 1));
265         }
266         else
267         {
268           out.append("-");
269         }
270
271         i++;
272       }
273
274       out.append(newline);
275     }
276
277     out.append("*");
278     out.append(newline);
279
280     return out.toString();
281   }
282 }