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