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