JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / io / PileUpfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.datamodel.SequenceI;
39 import jalview.util.Format;
40
41 import java.io.IOException;
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   public PileUpfile(String inFile, String type) throws IOException
65   {
66     super(inFile, type);
67   }
68
69   public PileUpfile(FileParse source) throws IOException
70   {
71     super(source);
72   }
73
74   /**
75    * DOCUMENT ME!
76    * 
77    * @return DOCUMENT ME!
78    */
79   @Override
80   public String print()
81   {
82     return print(getSeqsAsArray());
83   }
84
85   @Override
86   public String print(SequenceI[] s)
87   {
88     StringBuffer out = new StringBuffer("PileUp");
89     out.append(newline);
90     out.append(newline);
91
92     int max = 0;
93     int maxid = 0;
94
95     int i = 0;
96     int bigChecksum = 0;
97     int[] checksums = new int[s.length];
98     while (i < s.length)
99     {
100       checksums[i] = checkSum(s[i].getSequenceAsString());
101       bigChecksum += checksums[i];
102       i++;
103     }
104
105     out.append("   MSF: " + s[0].getSequence().length
106             + "   Type: P    Check:  " + bigChecksum % 10000 + "   ..");
107     out.append(newline);
108     out.append(newline);
109     out.append(newline);
110
111     i = 0;
112     while ((i < s.length) && (s[i] != null))
113     {
114       String seq = s[i].getSequenceAsString();
115       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
116               + "  Check:  " + checksums[i] + "  Weight:  1.00");
117       out.append(newline);
118
119       if (seq.length() > max)
120       {
121         max = seq.length();
122       }
123
124       if (s[i].getName().length() > maxid)
125       {
126         maxid = s[i].getName().length();
127       }
128
129       i++;
130     }
131
132     if (maxid < 10)
133     {
134       maxid = 10;
135     }
136
137     maxid++;
138     out.append(newline);
139     out.append(newline);
140     out.append("//");
141     out.append(newline);
142     out.append(newline);
143
144     int len = 50;
145
146     int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
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 }