update author list in license for (JAL-826)
[jalview.git] / src / jalview / io / PfamFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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  */
18 package jalview.io;
19
20 import java.io.*;
21 import java.util.*;
22
23 import jalview.datamodel.*;
24 import jalview.util.*;
25
26 public class PfamFile extends AlignFile
27 {
28
29   public PfamFile()
30   {
31   }
32
33   public PfamFile(String inFile, String type) throws IOException
34   {
35     super(inFile, type);
36   }
37
38   public PfamFile(FileParse source) throws IOException
39   {
40     super(source);
41   }
42
43   public void initData()
44   {
45     super.initData();
46   }
47
48   public void parse() throws IOException
49   {
50     int i = 0;
51     String line;
52
53     Hashtable seqhash = new Hashtable();
54     Vector headers = new Vector();
55
56     while ((line = nextLine()) != null)
57     {
58       if (line.indexOf(" ") != 0)
59       {
60         if (line.indexOf("#") != 0)
61         {
62           // TODO: verify pfam format requires spaces and not tab characters -
63           // if not upgrade to use stevesoft regex and look for whitespace.
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             if (str.hasMoreTokens())
88             {
89               tempseq.append(str.nextToken());
90             }
91           }
92         }
93       }
94     }
95
96     this.noSeqs = headers.size();
97
98     if (noSeqs < 1)
99     {
100       throw new IOException("No sequences found (PFAM input)");
101     }
102
103     for (i = 0; i < headers.size(); i++)
104     {
105       if (seqhash.get(headers.elementAt(i)) != null)
106       {
107         if (maxLength < seqhash.get(headers.elementAt(i)).toString()
108                 .length())
109         {
110           maxLength = seqhash.get(headers.elementAt(i)).toString().length();
111         }
112
113         Sequence newSeq = parseId(headers.elementAt(i).toString());
114         newSeq.setSequence(seqhash.get(headers.elementAt(i).toString())
115                 .toString());
116         seqs.addElement(newSeq);
117       }
118       else
119       {
120         System.err.println("PFAM File reader: Can't find sequence for "
121                 + headers.elementAt(i));
122       }
123     }
124   }
125
126   public String print(SequenceI[] s)
127   {
128     StringBuffer out = new StringBuffer("");
129
130     int max = 0;
131     int maxid = 0;
132
133     int i = 0;
134
135     while ((i < s.length) && (s[i] != null))
136     {
137       String tmp = printId(s[i]);
138
139       if (s[i].getSequence().length > max)
140       {
141         max = s[i].getSequence().length;
142       }
143
144       if (tmp.length() > maxid)
145       {
146         maxid = tmp.length();
147       }
148
149       i++;
150     }
151
152     if (maxid < 15)
153     {
154       maxid = 15;
155     }
156
157     int j = 0;
158
159     while ((j < s.length) && (s[j] != null))
160     {
161       out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " "));
162
163       out.append(s[j].getSequenceAsString());
164       out.append(newline);
165       j++;
166     }
167
168     out.append(newline);
169
170     return out.toString();
171   }
172
173   public String print()
174   {
175     return print(getSeqsAsArray());
176   }
177 }