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