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