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