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