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