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