applied copyright 2008
[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
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   public PfamFile(FileParse source) throws IOException
41   {
42     super(source);
43   }
44   public void initData()
45   {
46     super.initData();
47   }
48
49   public void parse()
50       throws IOException
51   {
52     int i = 0;
53     String line;
54
55     Hashtable seqhash = new Hashtable();
56     Vector headers = new Vector();
57
58     while ( (line = nextLine()) != null)
59     {
60       if (line.indexOf(" ") != 0)
61       {
62         if (line.indexOf("#") != 0)
63         {
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
88             tempseq.append(str.nextToken());
89           }
90         }
91       }
92     }
93
94     this.noSeqs = headers.size();
95
96     if (noSeqs < 1)
97     {
98       throw new IOException("No sequences found (PFAM input)");
99     }
100
101     for (i = 0; i < headers.size(); i++)
102     {
103       if (seqhash.get(headers.elementAt(i)) != null)
104       {
105         if (maxLength < seqhash.get(headers.elementAt(i)).toString()
106             .length())
107         {
108           maxLength = seqhash.get(headers.elementAt(i)).toString()
109               .length();
110         }
111
112         Sequence newSeq = parseId(headers.elementAt(i).toString());
113         newSeq.setSequence(seqhash.get(headers.elementAt(i).toString()).
114                            toString());
115         seqs.addElement(newSeq);
116       }
117       else
118       {
119         System.err.println("PFAM File reader: Can't find sequence for " +
120                            headers.elementAt(i));
121       }
122     }
123   }
124
125   public String print(SequenceI[] s)
126   {
127     StringBuffer out = new StringBuffer("");
128
129     int max = 0;
130     int maxid = 0;
131
132     int i = 0;
133
134     while ( (i < s.length) && (s[i] != null))
135     {
136       String tmp = printId(s[i]);
137
138       if (s[i].getSequence().length > max)
139       {
140         max = s[i].getSequence().length;
141       }
142
143       if (tmp.length() > maxid)
144       {
145         maxid = tmp.length();
146       }
147
148       i++;
149     }
150
151     if (maxid < 15)
152     {
153       maxid = 15;
154     }
155
156     int j = 0;
157
158     while ( (j < s.length) && (s[j] != null))
159     {
160       out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " "));
161
162       out.append(s[j].getSequenceAsString() + "\n");
163       j++;
164     }
165
166     out.append("\n");
167
168     return out.toString();
169   }
170
171   public String print()
172   {
173     return print(getSeqsAsArray());
174   }
175 }