667da9fccd72ebc7f3fb3bc8a2bc531676806397
[jalview.git] / src / jalview / io / PfamFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.datamodel.Sequence;
24 import jalview.datamodel.SequenceI;
25 import jalview.util.Format;
26 import jalview.util.MessageManager;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31
32 public class PfamFile extends AlignFile
33 {
34
35   public PfamFile()
36   {
37   }
38
39   public PfamFile(String inFile, String type) throws IOException
40   {
41     super(inFile, type);
42   }
43
44   public PfamFile(FileParse source) throws IOException
45   {
46     super(source);
47   }
48
49   @Override
50   public void initData()
51   {
52     super.initData();
53   }
54
55   @Override
56   public void parse() throws IOException
57   {
58     int i = 0;
59     String line;
60
61     HashMap<String, StringBuffer> seqhash = new HashMap<String, StringBuffer>();
62     ArrayList<String> headers = new ArrayList<String>();
63     boolean useTabs = false;
64     int spces;
65     while ((line = nextLine()) != null)
66     {
67       if (line.indexOf("#") == 0)
68       {
69         // skip comment lines
70         continue;
71       }
72       // locate first space or (if already checked), tab
73       if (useTabs)
74       {
75         spces = line.indexOf("\t");
76       }
77       else
78       {
79         spces = line.indexOf(" ");
80         // check to see if we ought to split on tabs instead.
81         if (!useTabs && spces == -1)
82         {
83           useTabs = true;
84           spces = line.indexOf("\t");
85         }
86       }
87       if (spces <= 0)
88       {
89         // no sequence data to split on
90         continue;
91       }
92       String id = line.substring(0, spces);
93       StringBuffer tempseq;
94
95       if (seqhash.containsKey(id))
96       {
97         tempseq = seqhash.get(id);
98       }
99       else
100       {
101         tempseq = new StringBuffer();
102         seqhash.put(id, tempseq);
103       }
104
105       if (!(headers.contains(id)))
106       {
107         headers.add(id);
108       }
109       if (spces + 1 < line.length())
110       {
111         tempseq.append(line.substring(spces + 1).trim());
112       }
113     }
114
115     this.noSeqs = headers.size();
116
117     if (noSeqs < 1)
118     {
119       throw new IOException(
120               MessageManager.getString("exception.pfam_no_sequences_found"));
121     }
122
123     for (i = 0; i < headers.size(); i++)
124     {
125       if (seqhash.get(headers.get(i)) != null)
126       {
127         if (maxLength < seqhash.get(headers.get(i)).toString()
128                 .length())
129         {
130           maxLength = seqhash.get(headers.get(i)).toString().length();
131         }
132
133         Sequence newSeq = parseId(headers.get(i).toString());
134         newSeq.setSequence(seqhash.get(headers.get(i).toString())
135                 .toString());
136         seqs.addElement(newSeq);
137       }
138       else
139       {
140         System.err.println("PFAM File reader: Can't find sequence for "
141                 + headers.get(i));
142       }
143     }
144   }
145
146   public String print(SequenceI[] s)
147   {
148     StringBuffer out = new StringBuffer("");
149
150     int max = 0;
151     int maxid = 0;
152
153     int i = 0;
154
155     while ((i < s.length) && (s[i] != null))
156     {
157       String tmp = printId(s[i]);
158
159       if (s[i].getSequence().length > max)
160       {
161         max = s[i].getSequence().length;
162       }
163
164       if (tmp.length() > maxid)
165       {
166         maxid = tmp.length();
167       }
168
169       i++;
170     }
171
172     if (maxid < 15)
173     {
174       maxid = 15;
175     }
176
177     int j = 0;
178
179     while ((j < s.length) && (s[j] != null))
180     {
181       out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " "));
182
183       out.append(s[j].getSequenceAsString());
184       out.append(newline);
185       j++;
186     }
187
188     out.append(newline);
189
190     return out.toString();
191   }
192
193   @Override
194   public String print()
195   {
196     return print(getSeqsAsArray());
197   }
198 }