proxy commit for Anne Menard <menard.annec@gmail.com> throw generic exceptions (just...
[jalview.git] / src / jalview / io / PileUpfile.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 /**
21  * <p>
22  * Title:
23  * </p>
24  * PileUpfile
25  * <p>
26  * Description:
27  * </p>
28  * 
29  * Read and write PileUp style MSF Files. This used to be the MSFFile class, and
30  * was written according to the EBI's idea of a subset of the MSF alignment
31  * format. But, that was updated to reflect current GCG style IO fashion, as
32  * found in Emboss (thanks David Martin!)
33  * 
34  */
35 import java.io.*;
36
37 import javax.xml.parsers.ParserConfigurationException;
38
39 import org.xml.sax.SAXException;
40
41 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
42 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
43 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
44 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
45
46 import jalview.datamodel.*;
47 import jalview.util.*;
48
49 public class PileUpfile extends MSFfile
50 {
51
52   /**
53    * Creates a new MSFfile object.
54    */
55   public PileUpfile()
56   {
57   }
58
59   /**
60    * Creates a new MSFfile object.
61    * 
62    * @param inFile
63    *          DOCUMENT ME!
64    * @param type
65    *          DOCUMENT ME!
66  * @throws Exception 
67    */
68   public PileUpfile(String inFile, String type) throws Exception
69   {
70     super(inFile, type);
71   }
72
73   public PileUpfile(FileParse source) throws Exception
74   {
75     super(source);
76   }
77
78   /**
79    * DOCUMENT ME!
80    * 
81    * @return DOCUMENT ME!
82    */
83   public String print()
84   {
85     return print(getSeqsAsArray());
86   }
87
88   public String print(SequenceI[] s)
89   {
90     StringBuffer out = new StringBuffer("PileUp");
91     out.append(newline);
92     out.append(newline);
93
94     int max = 0;
95     int maxid = 0;
96
97     int i = 0;
98     int bigChecksum = 0;
99     int[] checksums = new int[s.length];
100     while (i < s.length)
101     {
102       checksums[i] = checkSum(s[i].getSequenceAsString());
103       bigChecksum += checksums[i];
104       i++;
105     }
106
107     out.append("   MSF: " + s[0].getSequence().length
108             + "   Type: P    Check:  " + bigChecksum % 10000 + "   ..");
109     out.append(newline);
110     out.append(newline);
111     out.append(newline);
112
113     i = 0;
114     while ((i < s.length) && (s[i] != null))
115     {
116       String seq = s[i].getSequenceAsString();
117       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
118               + "  Check:  " + checksums[i] + "  Weight:  1.00");
119       out.append(newline);
120
121       if (seq.length() > max)
122       {
123         max = seq.length();
124       }
125
126       if (s[i].getName().length() > maxid)
127       {
128         maxid = s[i].getName().length();
129       }
130
131       i++;
132     }
133
134     if (maxid < 10)
135     {
136       maxid = 10;
137     }
138
139     maxid++;
140     out.append(newline);
141     out.append(newline);
142     out.append("//");
143     out.append(newline);
144     out.append(newline);
145
146     int len = 50;
147
148     int nochunks = (max / len) + 1;
149
150     if ((max % len) == 0)
151     {
152       nochunks--;
153     }
154
155     for (i = 0; i < nochunks; i++)
156     {
157       int j = 0;
158
159       while ((j < s.length) && (s[j] != null))
160       {
161         String name = printId(s[j]);
162
163         out.append(new Format("%-" + maxid + "s").form(name + " "));
164
165         for (int k = 0; k < 5; k++)
166         {
167           int start = (i * 50) + (k * 10);
168           int end = start + 10;
169
170           if ((end < s[j].getSequence().length)
171                   && (start < s[j].getSequence().length))
172           {
173             out.append(s[j].getSequence(start, end));
174
175             if (k < 4)
176             {
177               out.append(" ");
178             }
179             else
180             {
181               out.append(newline);
182             }
183           }
184           else
185           {
186             if (start < s[j].getSequence().length)
187             {
188               out.append(s[j].getSequenceAsString().substring(start));
189               out.append(newline);
190             }
191             else
192             {
193               if (k == 0)
194               {
195                 out.append(newline);
196               }
197             }
198           }
199         }
200
201         j++;
202       }
203
204       out.append(newline);
205     }
206
207     return out.toString();
208   }
209 }