JAL-1432 updated copyright notices
[jalview.git] / src / jalview / io / PileUpfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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");
85     out.append(newline);
86     out.append(newline);
87
88     int max = 0;
89     int maxid = 0;
90
91     int i = 0;
92     int bigChecksum = 0;
93     int[] checksums = new int[s.length];
94     while (i < s.length)
95     {
96       checksums[i] = checkSum(s[i].getSequenceAsString());
97       bigChecksum += checksums[i];
98       i++;
99     }
100
101     out.append("   MSF: " + s[0].getSequence().length
102             + "   Type: P    Check:  " + bigChecksum % 10000 + "   ..");
103     out.append(newline);
104     out.append(newline);
105     out.append(newline);
106
107     i = 0;
108     while ((i < s.length) && (s[i] != null))
109     {
110       String seq = s[i].getSequenceAsString();
111       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
112               + "  Check:  " + checksums[i] + "  Weight:  1.00");
113       out.append(newline);
114
115       if (seq.length() > max)
116       {
117         max = seq.length();
118       }
119
120       if (s[i].getName().length() > maxid)
121       {
122         maxid = s[i].getName().length();
123       }
124
125       i++;
126     }
127
128     if (maxid < 10)
129     {
130       maxid = 10;
131     }
132
133     maxid++;
134     out.append(newline);
135     out.append(newline);
136     out.append("//");
137     out.append(newline);
138     out.append(newline);
139
140     int len = 50;
141
142     int nochunks = (max / len) + 1;
143
144     if ((max % len) == 0)
145     {
146       nochunks--;
147     }
148
149     for (i = 0; i < nochunks; i++)
150     {
151       int j = 0;
152
153       while ((j < s.length) && (s[j] != null))
154       {
155         String name = printId(s[j]);
156
157         out.append(new Format("%-" + maxid + "s").form(name + " "));
158
159         for (int k = 0; k < 5; k++)
160         {
161           int start = (i * 50) + (k * 10);
162           int end = start + 10;
163
164           if ((end < s[j].getSequence().length)
165                   && (start < s[j].getSequence().length))
166           {
167             out.append(s[j].getSequence(start, end));
168
169             if (k < 4)
170             {
171               out.append(" ");
172             }
173             else
174             {
175               out.append(newline);
176             }
177           }
178           else
179           {
180             if (start < s[j].getSequence().length)
181             {
182               out.append(s[j].getSequenceAsString().substring(start));
183               out.append(newline);
184             }
185             else
186             {
187               if (k == 0)
188               {
189                 out.append(newline);
190               }
191             }
192           }
193         }
194
195         j++;
196       }
197
198       out.append(newline);
199     }
200
201     return out.toString();
202   }
203 }