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