Formatting changes
[jalview.git] / src / jalview / io / PfamFile.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 import java.io.*;\r
22 import java.util.*;\r
23 \r
24 import jalview.datamodel.*;\r
25 import jalview.util.*;\r
26 \r
27 public class PfamFile\r
28     extends AlignFile\r
29 {\r
30   Vector ids;\r
31 \r
32   public PfamFile()\r
33   {\r
34   }\r
35 \r
36   public PfamFile(String inStr)\r
37   {\r
38     super(inStr);\r
39   }\r
40 \r
41   public PfamFile(String inFile, String type)\r
42       throws IOException\r
43   {\r
44     super(inFile, type);\r
45   }\r
46 \r
47   public void initData()\r
48   {\r
49     super.initData();\r
50     ids = new Vector();\r
51   }\r
52 \r
53   public void parse()\r
54       throws IOException\r
55   {\r
56     int i = 0;\r
57     String line;\r
58 \r
59     Hashtable seqhash = new Hashtable();\r
60     Vector headers = new Vector();\r
61 \r
62     while ( (line = nextLine()) != null)\r
63     {\r
64       if (line.indexOf(" ") != 0)\r
65       {\r
66         if (line.indexOf("#") != 0)\r
67         {\r
68           StringTokenizer str = new StringTokenizer(line, " ");\r
69           String id = "";\r
70 \r
71           if (str.hasMoreTokens())\r
72           {\r
73             id = str.nextToken();\r
74 \r
75             StringBuffer tempseq;\r
76 \r
77             if (seqhash.containsKey(id))\r
78             {\r
79               tempseq = (StringBuffer) seqhash.get(id);\r
80             }\r
81             else\r
82             {\r
83               tempseq = new StringBuffer();\r
84               seqhash.put(id, tempseq);\r
85             }\r
86 \r
87             if (! (headers.contains(id)))\r
88             {\r
89               headers.addElement(id);\r
90             }\r
91 \r
92             tempseq.append(str.nextToken());\r
93           }\r
94         }\r
95       }\r
96     }\r
97 \r
98     this.noSeqs = headers.size();\r
99 \r
100     if (noSeqs < 1)\r
101     {\r
102       throw new IOException("No sequences found (PFAM input)");\r
103     }\r
104 \r
105     for (i = 0; i < headers.size(); i++)\r
106     {\r
107       if (seqhash.get(headers.elementAt(i)) != null)\r
108       {\r
109         if (maxLength < seqhash.get(headers.elementAt(i)).toString()\r
110             .length())\r
111         {\r
112           maxLength = seqhash.get(headers.elementAt(i)).toString()\r
113               .length();\r
114         }\r
115 \r
116         String head = headers.elementAt(i).toString();\r
117         int start = 1;\r
118         int end = seqhash.get(headers.elementAt(i)).toString().length();\r
119 \r
120         if (head.indexOf("/") > 0)\r
121         {\r
122           StringTokenizer st = new StringTokenizer(head, "/");\r
123 \r
124           if (st.countTokens() == 2)\r
125           {\r
126             ids.addElement(st.nextToken());\r
127 \r
128             String tmp = st.nextToken();\r
129             st = new StringTokenizer(tmp, "-");\r
130 \r
131             if (st.countTokens() == 2)\r
132             {\r
133               start = Integer.valueOf(st.nextToken()).intValue();\r
134               end = Integer.valueOf(st.nextToken()).intValue();\r
135             }\r
136             else\r
137             {\r
138               start = -1;\r
139               end = -1;\r
140             }\r
141           }\r
142           else\r
143           {\r
144             ids.addElement(headers.elementAt(i));\r
145           }\r
146         }\r
147         else\r
148         {\r
149           ids.addElement(headers.elementAt(i));\r
150         }\r
151 \r
152         Sequence newSeq = null;\r
153 \r
154         if ( (start != -1) && (end != -1))\r
155         {\r
156           newSeq = new Sequence(ids.elementAt(i).toString(),\r
157                                 seqhash.get(headers.elementAt(i).toString())\r
158                                 .toString(), start, end);\r
159           seqs.addElement(newSeq);\r
160         }\r
161         else\r
162         {\r
163           newSeq = new Sequence(ids.elementAt(i).toString(),\r
164                                 seqhash.get(headers.elementAt(i).toString())\r
165                                 .toString(), 1,\r
166                                 seqhash.get(headers.elementAt(i).toString())\r
167                                 .toString().length());\r
168           seqs.addElement(newSeq);\r
169         }\r
170 \r
171         if (!isValidProteinSequence(newSeq.getSequence()))\r
172         {\r
173           throw new IOException(\r
174               "Not a valid protein sequence - (PFAM input)");\r
175         }\r
176       }\r
177       else\r
178       {\r
179         System.err.println("PFAM File reader: Can't find sequence for " +\r
180                            headers.elementAt(i));\r
181       }\r
182     }\r
183   }\r
184 \r
185   public static String print(SequenceI[] s)\r
186   {\r
187     StringBuffer out = new StringBuffer("");\r
188 \r
189     int max = 0;\r
190     int maxid = 0;\r
191 \r
192     int i = 0;\r
193 \r
194     while ( (i < s.length) && (s[i] != null))\r
195     {\r
196       String tmp = s[i].getName() + "/" + s[i].getStart() + "-" +\r
197           s[i].getEnd();\r
198 \r
199       if (s[i].getSequence().length() > max)\r
200       {\r
201         max = s[i].getSequence().length();\r
202       }\r
203 \r
204       if (tmp.length() > maxid)\r
205       {\r
206         maxid = tmp.length();\r
207       }\r
208 \r
209       i++;\r
210     }\r
211 \r
212     if (maxid < 15)\r
213     {\r
214       maxid = 15;\r
215     }\r
216 \r
217     int j = 0;\r
218 \r
219     while ( (j < s.length) && (s[j] != null))\r
220     {\r
221       out.append(new Format("%-" + maxid + "s").form(s[j].getName() +\r
222           "/" + s[j].getStart() + "-" + s[j].getEnd()) + " ");\r
223 \r
224       out.append(s[j].getSequence() + "\n");\r
225       j++;\r
226     }\r
227 \r
228     out.append("\n");\r
229 \r
230     return out.toString();\r
231   }\r
232 \r
233   public String print()\r
234   {\r
235     return print(getSeqsAsArray());\r
236   }\r
237 }\r