file format enum wip changes
[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 sourceType
59    *          DOCUMENT ME!
60    * 
61    * @throws IOException
62    *           DOCUMENT ME!
63    */
64   public PileUpfile(String inFile, DataSourceType sourceType)
65           throws IOException
66   {
67     super(inFile, sourceType);
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   @Override
81   public String print()
82   {
83     return print(getSeqsAsArray());
84   }
85
86   @Override
87   public String print(SequenceI[] s)
88   {
89     StringBuffer out = new StringBuffer("PileUp");
90     out.append(newline);
91     out.append(newline);
92
93     int max = 0;
94     int maxid = 0;
95
96     int i = 0;
97     int bigChecksum = 0;
98     int[] checksums = new int[s.length];
99     while (i < s.length)
100     {
101       checksums[i] = checkSum(s[i].getSequenceAsString());
102       bigChecksum += checksums[i];
103       i++;
104     }
105
106     out.append("   MSF: " + s[0].getSequence().length
107             + "   Type: P    Check:  " + bigChecksum % 10000 + "   ..");
108     out.append(newline);
109     out.append(newline);
110     out.append(newline);
111
112     i = 0;
113     while ((i < s.length) && (s[i] != null))
114     {
115       String seq = s[i].getSequenceAsString();
116       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
117               + "  Check:  " + checksums[i] + "  Weight:  1.00");
118       out.append(newline);
119
120       if (seq.length() > max)
121       {
122         max = seq.length();
123       }
124
125       if (s[i].getName().length() > maxid)
126       {
127         maxid = s[i].getName().length();
128       }
129
130       i++;
131     }
132
133     if (maxid < 10)
134     {
135       maxid = 10;
136     }
137
138     maxid++;
139     out.append(newline);
140     out.append(newline);
141     out.append("//");
142     out.append(newline);
143     out.append(newline);
144
145     int len = 50;
146
147     int nochunks = (max / len) + (max % len > 0 ? 1 : 0);
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 }