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