JAL-1517 update copyright to version 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 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    */
63   public PileUpfile(String inFile, String type) throws IOException
64   {
65     super(inFile, type);
66   }
67
68   public PileUpfile(FileParse source) throws IOException
69   {
70     super(source);
71   }
72
73   /**
74    * DOCUMENT ME!
75    * 
76    * @return DOCUMENT ME!
77    */
78   public String print()
79   {
80     return print(getSeqsAsArray());
81   }
82
83   public String print(SequenceI[] s)
84   {
85     StringBuffer out = new StringBuffer("PileUp");
86     out.append(newline);
87     out.append(newline);
88
89     int max = 0;
90     int maxid = 0;
91
92     int i = 0;
93     int bigChecksum = 0;
94     int[] checksums = new int[s.length];
95     while (i < s.length)
96     {
97       checksums[i] = checkSum(s[i].getSequenceAsString());
98       bigChecksum += checksums[i];
99       i++;
100     }
101
102     out.append("   MSF: " + s[0].getSequence().length
103             + "   Type: P    Check:  " + bigChecksum % 10000 + "   ..");
104     out.append(newline);
105     out.append(newline);
106     out.append(newline);
107
108     i = 0;
109     while ((i < s.length) && (s[i] != null))
110     {
111       String seq = s[i].getSequenceAsString();
112       out.append(" Name: " + printId(s[i]) + " oo  Len:  " + seq.length()
113               + "  Check:  " + checksums[i] + "  Weight:  1.00");
114       out.append(newline);
115
116       if (seq.length() > max)
117       {
118         max = seq.length();
119       }
120
121       if (s[i].getName().length() > maxid)
122       {
123         maxid = s[i].getName().length();
124       }
125
126       i++;
127     }
128
129     if (maxid < 10)
130     {
131       maxid = 10;
132     }
133
134     maxid++;
135     out.append(newline);
136     out.append(newline);
137     out.append("//");
138     out.append(newline);
139     out.append(newline);
140
141     int len = 50;
142
143     int nochunks = (max / len) + 1;
144
145     if ((max % len) == 0)
146     {
147       nochunks--;
148     }
149
150     for (i = 0; i < nochunks; i++)
151     {
152       int j = 0;
153
154       while ((j < s.length) && (s[j] != null))
155       {
156         String name = printId(s[j]);
157
158         out.append(new Format("%-" + maxid + "s").form(name + " "));
159
160         for (int k = 0; k < 5; k++)
161         {
162           int start = (i * 50) + (k * 10);
163           int end = start + 10;
164
165           if ((end < s[j].getSequence().length)
166                   && (start < s[j].getSequence().length))
167           {
168             out.append(s[j].getSequence(start, end));
169
170             if (k < 4)
171             {
172               out.append(" ");
173             }
174             else
175             {
176               out.append(newline);
177             }
178           }
179           else
180           {
181             if (start < s[j].getSequence().length)
182             {
183               out.append(s[j].getSequenceAsString().substring(start));
184               out.append(newline);
185             }
186             else
187             {
188               if (k == 0)
189               {
190                 out.append(newline);
191               }
192             }
193           }
194         }
195
196         j++;
197       }
198
199       out.append(newline);
200     }
201
202     return out.toString();
203   }
204 }