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