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