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