v2
[jalview.git] / src / jalview / io / PileUpfile.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 /**
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
45 import jalview.datamodel.*;
46 import jalview.util.*;
47
48 public class PileUpfile extends MSFfile
49 {
50
51   /**
52    * Creates a new MSFfile object.
53    */
54   public PileUpfile()
55   {
56   }
57
58   /**
59    * Creates a new MSFfile object.
60    * 
61    * @param inFile
62    *          DOCUMENT ME!
63    * @param type
64    *          DOCUMENT ME!
65    * 
66    * @throws IOException
67    *           DOCUMENT ME!
68  * @throws SAXException 
69  * @throws ParserConfigurationException 
70  * @throws ExceptionFileFormatOrSyntax 
71  * @throws ExceptionLoadingFailed 
72  * @throws ExceptionPermissionDenied 
73  * @throws InterruptedException 
74    */
75   public PileUpfile(String inFile, String type) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
76   {
77     super(inFile, type);
78   }
79
80   public PileUpfile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
81   {
82     super(source);
83   }
84
85   /**
86    * DOCUMENT ME!
87    * 
88    * @return DOCUMENT ME!
89    */
90   public String print()
91   {
92     return print(getSeqsAsArray());
93   }
94
95   public String print(SequenceI[] s)
96   {
97     StringBuffer out = new StringBuffer("PileUp");
98     out.append(newline);
99     out.append(newline);
100     
101     int max = 0;
102     int maxid = 0;
103
104     int i = 0;
105     int bigChecksum = 0;
106     int[] checksums = new int[s.length];
107     while (i < s.length)
108     {
109       checksums[i] = checkSum(s[i].getSequenceAsString());
110       bigChecksum += checksums[i];
111       i++;
112     }
113
114     out.append("   MSF: " + s[0].getSequence().length
115             + "   Type: P    Check:  " + bigChecksum % 10000
116             + "   ..");
117     out.append(newline);
118     out.append(newline);
119     out.append(newline);
120
121     i = 0;
122     while ((i < s.length) && (s[i] != null))
123     {
124       String seq = s[i].getSequenceAsString();
125       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
126               + "  Check:  " + checksums[i] + "  Weight:  1.00");
127       out.append(newline);
128
129       if (seq.length() > max)
130       {
131         max = seq.length();
132       }
133
134       if (s[i].getName().length() > maxid)
135       {
136         maxid = s[i].getName().length();
137       }
138
139       i++;
140     }
141
142     if (maxid < 10)
143     {
144       maxid = 10;
145     }
146
147     maxid++;
148     out.append(newline);out.append(newline);out.append("//");
149     out.append(newline);out.append(newline);
150
151     int len = 50;
152
153     int nochunks = (max / len) + 1;
154
155     if ((max % len) == 0)
156     {
157       nochunks--;
158     }
159
160     for (i = 0; i < nochunks; i++)
161     {
162       int j = 0;
163
164       while ((j < s.length) && (s[j] != null))
165       {
166         String name = printId(s[j]);
167
168         out.append(new Format("%-" + maxid + "s").form(name + " "));
169
170         for (int k = 0; k < 5; k++)
171         {
172           int start = (i * 50) + (k * 10);
173           int end = start + 10;
174
175           if ((end < s[j].getSequence().length)
176                   && (start < s[j].getSequence().length))
177           {
178             out.append(s[j].getSequence(start, end));
179
180             if (k < 4)
181             {
182               out.append(" ");
183             }
184             else
185             {
186               out.append(newline);
187             }
188           }
189           else
190           {
191             if (start < s[j].getSequence().length)
192             {
193               out.append(s[j].getSequenceAsString().substring(start));
194               out.append(newline);
195             }
196             else
197             {
198               if (k == 0)
199               {
200                 out.append(newline);
201               }
202             }
203           }
204         }
205
206         j++;
207       }
208
209       out.append(newline);
210     }
211
212     return out.toString();
213   }
214 }