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