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