07ba3f5337e6c3a6cdad244d322645292ce63085
[jalview.git] / src / jalview / io / PfamFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceI;
25 import jalview.util.Format;
26 import jalview.util.MessageManager;
27
28 import java.io.IOException;
29 import java.util.Hashtable;
30 import java.util.StringTokenizer;
31 import java.util.Vector;
32
33 public class PfamFile extends AlignFile
34 {
35
36   public PfamFile()
37   {
38   }
39
40   public PfamFile(String inFile, String type) throws IOException
41   {
42     super(inFile, type);
43   }
44
45   public PfamFile(FileParse source) throws IOException
46   {
47     super(source);
48   }
49
50   public void initData()
51   {
52     super.initData();
53   }
54
55   public void parse() throws IOException
56   {
57     int i = 0;
58     String line;
59
60     Hashtable seqhash = new Hashtable();
61     Vector headers = new Vector();
62
63     while ((line = nextLine()) != null)
64     {
65       if (line.indexOf(" ") != 0)
66       {
67         if (line.indexOf("#") != 0)
68         {
69           // TODO: verify pfam format requires spaces and not tab characters -
70           // if not upgrade to use stevesoft regex and look for whitespace.
71           StringTokenizer str = new StringTokenizer(line, " ");
72           String id = "";
73
74           if (str.hasMoreTokens())
75           {
76             id = str.nextToken();
77
78             StringBuffer tempseq;
79
80             if (seqhash.containsKey(id))
81             {
82               tempseq = (StringBuffer) seqhash.get(id);
83             }
84             else
85             {
86               tempseq = new StringBuffer();
87               seqhash.put(id, tempseq);
88             }
89
90             if (!(headers.contains(id)))
91             {
92               headers.addElement(id);
93             }
94             if (str.hasMoreTokens())
95             {
96               tempseq.append(str.nextToken());
97             }
98           }
99         }
100       }
101     }
102
103     this.noSeqs = headers.size();
104
105     if (noSeqs < 1)
106     {
107       throw new IOException(
108               MessageManager.getString("exception.pfam_no_sequences_found"));
109     }
110
111     for (i = 0; i < headers.size(); i++)
112     {
113       if (seqhash.get(headers.elementAt(i)) != null)
114       {
115         if (maxLength < seqhash.get(headers.elementAt(i)).toString()
116                 .length())
117         {
118           maxLength = seqhash.get(headers.elementAt(i)).toString().length();
119         }
120
121         Sequence newSeq = parseId(headers.elementAt(i).toString());
122         newSeq.setSequence(seqhash.get(headers.elementAt(i).toString())
123                 .toString());
124         seqs.addElement(newSeq);
125       }
126       else
127       {
128         System.err.println("PFAM File reader: Can't find sequence for "
129                 + headers.elementAt(i));
130       }
131     }
132   }
133
134   public String print(SequenceI[] s)
135   {
136     StringBuffer out = new StringBuffer("");
137
138     int max = 0;
139     int maxid = 0;
140
141     int i = 0;
142
143     while ((i < s.length) && (s[i] != null))
144     {
145       String tmp = printId(s[i]);
146
147       if (s[i].getSequence().length > max)
148       {
149         max = s[i].getSequence().length;
150       }
151
152       if (tmp.length() > maxid)
153       {
154         maxid = tmp.length();
155       }
156
157       i++;
158     }
159
160     if (maxid < 15)
161     {
162       maxid = 15;
163     }
164
165     int j = 0;
166
167     while ((j < s.length) && (s[j] != null))
168     {
169       out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " "));
170
171       out.append(s[j].getSequenceAsString());
172       out.append(newline);
173       j++;
174     }
175
176     out.append(newline);
177
178     return out.toString();
179   }
180
181   public String print()
182   {
183     return print(getSeqsAsArray());
184   }
185 }