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