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