f4d61bc43e03d5da32856a642d1e479bd168e028
[jalview.git] / src / jalview / io / PfamFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.io;
20
21 import java.io.*;
22 import java.util.*;
23
24
25 import jalview.datamodel.*;
26 import jalview.util.*;
27
28 public class PfamFile extends AlignFile
29 {
30
31   public PfamFile()
32   {
33   }
34
35   public PfamFile(String inFile, String type) throws IOException
36   {
37     super(inFile, type);
38   }
39
40   public PfamFile(FileParse source) throws IOException
41   {
42     super(source);
43   }
44
45   public void initData()
46   {
47     super.initData();
48   }
49
50   public void parse() 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           // TODO: verify pfam format requires spaces and not tab characters -
65           // if not upgrade to use stevesoft regex and look for whitespace.
66           StringTokenizer str = new StringTokenizer(line, " ");
67           String id = "";
68
69           if (str.hasMoreTokens())
70           {
71             id = str.nextToken();
72
73             StringBuffer tempseq;
74
75             if (seqhash.containsKey(id))
76             {
77               tempseq = (StringBuffer) seqhash.get(id);
78             }
79             else
80             {
81               tempseq = new StringBuffer();
82               seqhash.put(id, tempseq);
83             }
84
85             if (!(headers.contains(id)))
86             {
87               headers.addElement(id);
88             }
89             if (str.hasMoreTokens())
90             {
91               tempseq.append(str.nextToken());
92             }
93           }
94         }
95       }
96     }
97
98     this.noSeqs = headers.size();
99
100     if (noSeqs < 1)
101     {
102       throw new IOException("No sequences found (PFAM input)");
103     }
104
105     for (i = 0; i < headers.size(); i++)
106     {
107       if (seqhash.get(headers.elementAt(i)) != null)
108       {
109         if (maxLength < seqhash.get(headers.elementAt(i)).toString()
110                 .length())
111         {
112           maxLength = seqhash.get(headers.elementAt(i)).toString().length();
113         }
114
115         Sequence newSeq = parseId(headers.elementAt(i).toString());
116         newSeq.setSequence(seqhash.get(headers.elementAt(i).toString())
117                 .toString());
118         seqs.addElement(newSeq);
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].getSequenceAsString());
166       out.append(newline);
167       j++;
168     }
169
170     out.append(newline);
171
172     return out.toString();
173   }
174
175   public String print()
176   {
177     return print(getSeqsAsArray());
178   }
179 }