apply gpl development license
[jalview.git] / src / jalview / io / PileUpfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 /**
22  * <p>
23  * Title:
24  * </p>
25  * PileUpfile
26  * <p>
27  * Description:
28  * </p>
29  * 
30  * Read and write PileUp style MSF Files. This used to be the MSFFile class, and
31  * was written according to the EBI's idea of a subset of the MSF alignment
32  * format. But, that was updated to reflect current GCG style IO fashion, as
33  * found in Emboss (thanks David Martin!)
34  * 
35  */
36 import java.io.*;
37
38 import jalview.datamodel.*;
39 import jalview.util.*;
40
41 public class PileUpfile extends MSFfile
42 {
43
44   /**
45    * Creates a new MSFfile object.
46    */
47   public PileUpfile()
48   {
49   }
50
51   /**
52    * Creates a new MSFfile object.
53    * 
54    * @param inFile
55    *                DOCUMENT ME!
56    * @param type
57    *                DOCUMENT ME!
58    * 
59    * @throws IOException
60    *                 DOCUMENT ME!
61    */
62   public PileUpfile(String inFile, String type) throws IOException
63   {
64     super(inFile, type);
65   }
66
67   public PileUpfile(FileParse source) throws IOException
68   {
69     super(source);
70   }
71
72   /**
73    * DOCUMENT ME!
74    * 
75    * @return DOCUMENT ME!
76    */
77   public String print()
78   {
79     return print(getSeqsAsArray());
80   }
81
82   public String print(SequenceI[] s)
83   {
84     StringBuffer out = new StringBuffer("PileUp\n\n");
85
86     int max = 0;
87     int maxid = 0;
88
89     int i = 0;
90     int bigChecksum = 0;
91     int[] checksums = new int[s.length];
92     while (i < s.length)
93     {
94       checksums[i] = checkSum(s[i].getSequenceAsString());
95       bigChecksum += checksums[i];
96       i++;
97     }
98
99     out.append("   MSF: " + s[0].getSequence().length
100             + "   Type: P    Check:  " + bigChecksum % 10000
101             + "   ..\n\n\n");
102
103     i = 0;
104     while ((i < s.length) && (s[i] != null))
105     {
106       String seq = s[i].getSequenceAsString();
107       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
108               + "  Check:  " + checksums[i] + "  Weight:  1.00\n");
109
110       if (seq.length() > max)
111       {
112         max = seq.length();
113       }
114
115       if (s[i].getName().length() > maxid)
116       {
117         maxid = s[i].getName().length();
118       }
119
120       i++;
121     }
122
123     if (maxid < 10)
124     {
125       maxid = 10;
126     }
127
128     maxid++;
129     out.append("\n\n//\n\n");
130
131     int len = 50;
132
133     int nochunks = (max / len) + 1;
134
135     if ((max % len) == 0)
136     {
137       nochunks--;
138     }
139
140     for (i = 0; i < nochunks; i++)
141     {
142       int j = 0;
143
144       while ((j < s.length) && (s[j] != null))
145       {
146         String name = printId(s[j]);
147
148         out.append(new Format("%-" + maxid + "s").form(name + " "));
149
150         for (int k = 0; k < 5; k++)
151         {
152           int start = (i * 50) + (k * 10);
153           int end = start + 10;
154
155           if ((end < s[j].getSequence().length)
156                   && (start < s[j].getSequence().length))
157           {
158             out.append(s[j].getSequence(start, end));
159
160             if (k < 4)
161             {
162               out.append(" ");
163             }
164             else
165             {
166               out.append("\n");
167             }
168           }
169           else
170           {
171             if (start < s[j].getSequence().length)
172             {
173               out.append(s[j].getSequenceAsString().substring(start));
174               out.append("\n");
175             }
176             else
177             {
178               if (k == 0)
179               {
180                 out.append("\n");
181               }
182             }
183           }
184         }
185
186         j++;
187       }
188
189       out.append("\n");
190     }
191
192     return out.toString();
193   }
194 }