FileParse object can be re-used to read different files concatenated together
[jalview.git] / src / jalview / io / PileUpfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io;
20
21 /**
22  * <p>Title: </p>
23  *  PileUpfile
24  * <p>Description: </p>
25  *
26  *  Read and write PileUp style MSF Files.
27  *  This used to be the MSFFile class, and was written according to the EBI's idea
28  *  of a subset of the MSF alignment format. But, that was updated to reflect current
29  *  GCG style IO fashion, as found in Emboss (thanks David Martin!)
30  *
31  **/
32 import java.io.*;
33
34 import jalview.datamodel.*;
35 import jalview.util.*;
36
37 public class PileUpfile
38     extends MSFfile
39 {
40
41   /**
42    * Creates a new MSFfile object.
43    */
44   public PileUpfile()
45   {
46   }
47
48   /**
49    * Creates a new MSFfile object.
50    *
51    * @param inFile DOCUMENT ME!
52    * @param type DOCUMENT ME!
53    *
54    * @throws IOException DOCUMENT ME!
55    */
56   public PileUpfile(String inFile, String type)
57       throws IOException
58   {
59     super(inFile, type);
60   }
61   public PileUpfile(FileParse source) throws IOException
62   {
63     super(source);
64   }
65   /**
66    * DOCUMENT ME!
67    *
68    * @return DOCUMENT ME!
69    */
70   public String print()
71   {
72     return print(getSeqsAsArray());
73   }
74
75   public String print(SequenceI[] s)
76   {
77     StringBuffer out = new StringBuffer("PileUp\n\n");
78
79     int max = 0;
80     int maxid = 0;
81
82     int i = 0;
83     int bigChecksum = 0;
84     int[] checksums = new int[s.length];
85     while (i < s.length)
86     {
87       checksums[i] = checkSum(s[i].getSequenceAsString());
88       bigChecksum += checksums[i];
89       i++;
90     }
91
92     out.append("   MSF: " + s[0].getSequence().length +
93                "   Type: P    Check:  " + bigChecksum % 10000 + "   ..\n\n\n");
94
95     i = 0;
96     while ( (i < s.length) && (s[i] != null))
97     {
98       String seq = s[i].getSequenceAsString();
99       out.append(" Name: " + printId(s[i]) +
100                  " oo  Len:  " +
101                  seq.length() + "  Check:  " + checksums[i] +
102                  "  Weight:  1.00\n");
103
104       if (seq.length() > max)
105       {
106         max = seq.length();
107       }
108
109       if (s[i].getName().length() > maxid)
110       {
111         maxid = s[i].getName().length();
112       }
113
114       i++;
115     }
116
117     if (maxid < 10)
118     {
119       maxid = 10;
120     }
121
122     maxid++;
123     out.append("\n\n//\n\n");
124
125     int len = 50;
126
127     int nochunks = (max / len) + 1;
128
129     if ( (max % len) == 0)
130     {
131       nochunks--;
132     }
133
134     for (i = 0; i < nochunks; i++)
135     {
136       int j = 0;
137
138       while ( (j < s.length) && (s[j] != null))
139       {
140         String name = printId(s[j]);
141
142         out.append(new Format("%-" + maxid + "s").form(name + " "));
143
144         for (int k = 0; k < 5; k++)
145         {
146           int start = (i * 50) + (k * 10);
147           int end = start + 10;
148
149           if ( (end < s[j].getSequence().length) &&
150               (start < s[j].getSequence().length))
151           {
152             out.append(s[j].getSequence(start, end));
153
154             if (k < 4)
155             {
156               out.append(" ");
157             }
158             else
159             {
160               out.append("\n");
161             }
162           }
163           else
164           {
165             if (start < s[j].getSequence().length)
166             {
167               out.append(s[j].getSequenceAsString().substring(start));
168               out.append("\n");
169             }
170             else
171             {
172               if (k == 0)
173               {
174                 out.append("\n");
175               }
176             }
177           }
178         }
179
180         j++;
181       }
182
183       out.append("\n");
184     }
185
186     return out.toString();
187   }
188 }