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