use 1 & -1 if start/end not specified
[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 = -1;\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           }\r
137           else\r
138           {\r
139             ids.addElement(headers.elementAt(i));\r
140           }\r
141         }\r
142         else\r
143         {\r
144           ids.addElement(headers.elementAt(i));\r
145         }\r
146 \r
147         Sequence newSeq = null;\r
148 \r
149         newSeq = new Sequence(ids.elementAt(i).toString(),\r
150                               seqhash.get(headers.elementAt(i).toString())\r
151                               .toString(), start, end);\r
152         seqs.addElement(newSeq);\r
153 \r
154 \r
155         if (!isValidProteinSequence(newSeq.getSequence()))\r
156         {\r
157           throw new IOException(\r
158               "Not a valid protein sequence - (PFAM input)");\r
159         }\r
160       }\r
161       else\r
162       {\r
163         System.err.println("PFAM File reader: Can't find sequence for " +\r
164                            headers.elementAt(i));\r
165       }\r
166     }\r
167   }\r
168 \r
169   public static String print(SequenceI[] s)\r
170   {\r
171     StringBuffer out = new StringBuffer("");\r
172 \r
173     int max = 0;\r
174     int maxid = 0;\r
175 \r
176     int i = 0;\r
177 \r
178     while ( (i < s.length) && (s[i] != null))\r
179     {\r
180       String tmp = s[i].getName() + "/" + s[i].getStart() + "-" +\r
181           s[i].getEnd();\r
182 \r
183       if (s[i].getSequence().length() > max)\r
184       {\r
185         max = s[i].getSequence().length();\r
186       }\r
187 \r
188       if (tmp.length() > maxid)\r
189       {\r
190         maxid = tmp.length();\r
191       }\r
192 \r
193       i++;\r
194     }\r
195 \r
196     if (maxid < 15)\r
197     {\r
198       maxid = 15;\r
199     }\r
200 \r
201     int j = 0;\r
202 \r
203     while ( (j < s.length) && (s[j] != null))\r
204     {\r
205       out.append(new Format("%-" + maxid + "s").form(s[j].getName() +\r
206           "/" + s[j].getStart() + "-" + s[j].getEnd()) + " ");\r
207 \r
208       out.append(s[j].getSequence() + "\n");\r
209       j++;\r
210     }\r
211 \r
212     out.append("\n");\r
213 \r
214     return out.toString();\r
215   }\r
216 \r
217   public String print()\r
218   {\r
219     return print(getSeqsAsArray());\r
220   }\r
221 }\r