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