1e16348a2506db0d6716f04b79b8236f6dec3dd9
[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, DataSourceType sourceType)
40           throws IOException
41   {
42     super(inFile, sourceType);
43   }
44
45   public PfamFile(FileParse source) throws IOException
46   {
47     super(source);
48   }
49
50   @Override
51   public void initData()
52   {
53     super.initData();
54   }
55
56   @Override
57   public void parse() throws IOException
58   {
59     int i = 0;
60     String line;
61
62     HashMap<String, StringBuffer> seqhash = new HashMap<String, StringBuffer>();
63     ArrayList<String> headers = new ArrayList<String>();
64     boolean useTabs = false;
65     int spces;
66     while ((line = nextLine()) != null)
67     {
68       if (line.indexOf("#") == 0)
69       {
70         // skip comment lines
71         continue;
72       }
73       // locate first space or (if already checked), tab
74       if (useTabs)
75       {
76         spces = line.indexOf("\t");
77       }
78       else
79       {
80         spces = line.indexOf(" ");
81         // check to see if we ought to split on tabs instead.
82         if (!useTabs && spces == -1)
83         {
84           useTabs = true;
85           spces = line.indexOf("\t");
86         }
87       }
88       if (spces <= 0)
89       {
90         // no sequence data to split on
91         continue;
92       }
93       String id = line.substring(0, spces);
94       StringBuffer tempseq;
95
96       if (seqhash.containsKey(id))
97       {
98         tempseq = seqhash.get(id);
99       }
100       else
101       {
102         tempseq = new StringBuffer();
103         seqhash.put(id, tempseq);
104       }
105
106       if (!(headers.contains(id)))
107       {
108         headers.add(id);
109       }
110       if (spces + 1 < line.length())
111       {
112         tempseq.append(line.substring(spces + 1).trim());
113       }
114     }
115
116     this.noSeqs = headers.size();
117
118     if (noSeqs < 1)
119     {
120       throw new IOException(MessageManager
121               .getString("exception.pfam_no_sequences_found"));
122     }
123
124     for (i = 0; i < headers.size(); i++)
125     {
126       if (seqhash.get(headers.get(i)) != null)
127       {
128         if (maxLength < seqhash.get(headers.get(i)).toString().length())
129         {
130           maxLength = seqhash.get(headers.get(i)).toString().length();
131         }
132
133         Sequence newSeq = parseId(headers.get(i).toString());
134         newSeq.setSequence(
135                 seqhash.get(headers.get(i).toString()).toString());
136         seqs.addElement(newSeq);
137       }
138       else
139       {
140         jalview.bin.Console
141                 .errPrintln("PFAM File reader: Can't find sequence for "
142                         + headers.get(i));
143       }
144     }
145   }
146
147   @Override
148   public String print(SequenceI[] s, boolean jvsuffix)
149   {
150     StringBuffer out = new StringBuffer("");
151
152     int max = 0;
153     int maxid = 0;
154
155     int i = 0;
156
157     while ((i < s.length) && (s[i] != null))
158     {
159       String tmp = printId(s[i], jvsuffix);
160
161       max = Math.max(max, s[i].getLength());
162
163       if (tmp.length() > maxid)
164       {
165         maxid = tmp.length();
166       }
167
168       i++;
169     }
170
171     if (maxid < 15)
172     {
173       maxid = 15;
174     }
175
176     int j = 0;
177
178     while ((j < s.length) && (s[j] != null))
179     {
180       out.append(new Format("%-" + maxid + "s")
181               .form(printId(s[j], jvsuffix) + " "));
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 }