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