updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / PileUpfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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  extends MSFfile
38 {
39
40     /**
41      * Creates a new MSFfile object.
42      */
43     public PileUpfile()
44     {
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) throws IOException
57     {
58         super(inFile, type);
59     }
60    /**
61    * DOCUMENT ME!
62    *
63    * @return DOCUMENT ME!
64    */
65   public String print()
66   {
67       return print(getSeqsAsArray());
68   }
69
70
71   public  String print(SequenceI[] s)
72   {
73     StringBuffer out = new StringBuffer("PileUp\n\n");
74
75     int max = 0;
76     int maxid = 0;
77
78     int i = 0;
79     int bigChecksum = 0;
80     int[] checksums = new int[s.length];
81     while (i < s.length)
82     {
83       checksums[i] = checkSum(s[i].getSequence());
84       bigChecksum += checksums[i];
85       i++;
86     }
87
88     out.append("   MSF: " + s[0].getSequence().length() +
89                "   Type: P    Check:  " + bigChecksum%10000 + "   ..\n\n\n");
90
91     i=0;
92     while ( (i < s.length) && (s[i] != null))
93     {
94       String seq = s[i].getSequence();
95       out.append(" Name: " + printId(s[i]) +
96                  " oo  Len:  " +
97                  s[i].getSequence().length() + "  Check:  " + checksums[i] +
98                  "  Weight:  1.00\n");
99
100       if (seq.length() > max)
101       {
102         max = seq.length();
103       }
104
105       if (s[i].getName().length() > maxid)
106       {
107         maxid = s[i].getName().length();
108       }
109
110       i++;
111     }
112
113     if (maxid < 10)
114     {
115       maxid = 10;
116     }
117
118     maxid++;
119     out.append("\n\n//\n\n");
120
121     int len = 50;
122
123     int nochunks = (max / len) + 1;
124
125     if ( (max % len) == 0)
126     {
127       nochunks--;
128     }
129
130     for (i = 0; i < nochunks; i++)
131     {
132       int j = 0;
133
134       while ( (j < s.length) && (s[j] != null))
135       {
136         String name = printId(s[j]);
137
138          out.append(new Format("%-" + maxid + "s").form(name + " "));
139
140         for (int k = 0; k < 5; k++)
141         {
142           int start = (i * 50) + (k * 10);
143           int end = start + 10;
144
145           if ( (end < s[j].getSequence().length()) &&
146               (start < s[j].getSequence().length()))
147           {
148             out.append(s[j].getSequence().substring(start, end));
149
150             if (k < 4)
151             {
152               out.append(" ");
153             }
154             else
155             {
156               out.append("\n");
157             }
158           }
159           else
160           {
161             if (start < s[j].getSequence().length())
162             {
163               out.append(s[j].getSequence().substring(start));
164               out.append("\n");
165             }
166             else
167             {
168               if (k == 0)
169               {
170                 out.append("\n");
171               }
172             }
173           }
174         }
175
176         j++;
177       }
178
179       out.append("\n");
180     }
181
182     return out.toString();
183   }
184 }