2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import jalview.datamodel.*;
\r
25 import jalview.schemes.UserColourScheme;
\r
32 * @version $Revision$
\r
34 public class FeaturesFile extends AlignFile
\r
37 * Creates a new FeaturesFile object.
\r
39 public FeaturesFile()
\r
45 * Creates a new FeaturesFile object.
\r
47 * @param inFile DOCUMENT ME!
\r
48 * @param type DOCUMENT ME!
\r
50 * @throws IOException DOCUMENT ME!
\r
52 public FeaturesFile(String inFile, String type)
\r
55 super(inFile, type);
\r
59 * The Application can render HTML, but the applet will
\r
60 * remove HTML tags and replace links with %LINK%
\r
61 * Both need to read links in HTML however
\r
63 * @throws IOException DOCUMENT ME!
\r
65 public boolean parse(AlignmentI align,
\r
69 return parse(align, colours, null, removeHTML);
\r
72 * The Application can render HTML, but the applet will
\r
73 * remove HTML tags and replace links with %LINK%
\r
74 * Both need to read links in HTML however
\r
76 * @throws IOException DOCUMENT ME!
\r
78 public boolean parse(AlignmentI align,
\r
80 Hashtable featureLink,
\r
86 SequenceI seq = null;
\r
87 String type, desc, token=null;
\r
89 int index, start, end;
\r
93 String featureGroup = null, groupLink = null;
\r
94 Hashtable typeLink = new Hashtable();
\r
96 boolean GFFFile = true;
\r
98 while ( (line = nextLine()) != null)
\r
100 if(line.startsWith("#"))
\r
103 st = new StringTokenizer(line, "\t");
\r
104 if (st.countTokens()>1 && st.countTokens() < 4 )
\r
107 type = st.nextToken();
\r
108 if (type.equalsIgnoreCase("startgroup"))
\r
110 featureGroup = st.nextToken();
\r
111 if (st.hasMoreElements())
\r
113 groupLink = st.nextToken();
\r
114 featureLink.put(featureGroup, groupLink);
\r
117 else if (type.equalsIgnoreCase("endgroup"))
\r
119 //We should check whether this is the current group,
\r
120 //but at present theres no way of showing more than 1 group
\r
122 featureGroup = null;
\r
127 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
128 colours.put(type, ucs.findColour("A"));
\r
129 if (st.hasMoreElements())
\r
131 String link = st.nextToken();
\r
132 typeLink.put(type, link);
\r
133 if(featureLink==null)
\r
134 featureLink = new Hashtable();
\r
135 featureLink.put(type, link);
\r
142 while (st.hasMoreElements())
\r
147 // Still possible this is an old Jalview file,
\r
148 // which does not have type colours at the beginning
\r
149 token = st.nextToken();
\r
150 seq = align.findName(token);
\r
153 desc = st.nextToken();
\r
154 type = st.nextToken();
\r
155 start = Integer.parseInt(st.nextToken());
\r
156 end = Integer.parseInt(st.nextToken());
\r
159 score = new Float(st.nextToken()).floatValue();
\r
161 catch (NumberFormatException ex)
\r
166 sf = new SequenceFeature(type, desc, start, end, score, null);
\r
170 sf.setValue("STRAND", st.nextToken());
\r
171 sf.setValue("FRAME", st.nextToken());
\r
173 catch (Exception ex)
\r
176 seq.getDatasetSequence().addSequenceFeature(sf);
\r
182 if(GFFFile && seq==null)
\r
187 desc = st.nextToken();
\r
190 token = st.nextToken();
\r
191 if (!token.equals("ID_NOT_SPECIFIED"))
\r
193 seq = align.findName(token);
\r
199 index = Integer.parseInt(st.nextToken());
\r
200 seq = align.getSequenceAt(index);
\r
202 catch(NumberFormatException ex)
\r
210 System.out.println("Sequence not found: "+line);
\r
214 start = Integer.parseInt(st.nextToken());
\r
215 end = Integer.parseInt(st.nextToken());
\r
217 type = st.nextToken();
\r
219 if (!colours.containsKey(type))
\r
221 // Probably the old style groups file
\r
222 UserColourScheme ucs = new UserColourScheme(type);
\r
223 colours.put(type, ucs.findColour("A"));
\r
226 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
228 seq.addSequenceFeature(sf);
\r
230 if(groupLink!=null && removeHTML)
\r
232 sf.addLink(groupLink);
\r
233 sf.description += "%LINK%";
\r
235 if(typeLink.containsKey(type) && removeHTML)
\r
237 sf.addLink(typeLink.get(type).toString());
\r
238 sf.description += "%LINK%";
\r
241 parseDescriptionHTML(sf, removeHTML);
\r
243 //If we got here, its not a GFFFile
\r
248 catch (Exception ex)
\r
250 System.out.println(line);
\r
251 ex.printStackTrace();
\r
252 System.out.println("Error parsing feature file: " + ex +"\n"+line);
\r
259 void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
261 StringBuffer sb = new StringBuffer();
\r
262 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
263 String token, link;
\r
264 while(st.hasMoreElements())
\r
266 token = st.nextToken("<>");
\r
267 if(token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
270 if(token.startsWith("a href="))
\r
272 link = token.substring(token.indexOf("\"")+1, token.length()-1);
\r
273 String label = st.nextToken("<>");
\r
274 sf.addLink(label+"|"+link);
\r
275 sb.append(label+"%LINK%");
\r
277 else if(token.equalsIgnoreCase("br"))
\r
284 sf.description = sb.toString();
\r
290 * @param s DOCUMENT ME!
\r
291 * @param len DOCUMENT ME!
\r
292 * @param gaps DOCUMENT ME!
\r
293 * @param displayId DOCUMENT ME!
\r
295 * @return DOCUMENT ME!
\r
297 public String printJalviewFormat(SequenceI [] seqs,
\r
300 StringBuffer out = new StringBuffer();
\r
301 SequenceFeature [] next;
\r
303 if(visible==null || visible.size()<1)
\r
304 return "No Features Visible";
\r
306 Enumeration en = visible.keys();
\r
309 while( en.hasMoreElements() )
\r
311 type = en.nextElement().toString();
\r
312 color = Integer.parseInt( visible.get(type).toString() );
\r
313 out.append(type + "\t"
\r
314 + jalview.util.Format.getHexString(
\r
315 new java.awt.Color(color) )
\r
319 //Work out which groups are both present and visible
\r
320 Vector groups = new Vector();
\r
321 int groupIndex = 0;
\r
323 for(int i=0; i<seqs.length; i++)
\r
325 next = seqs[i].getSequenceFeatures();
\r
328 for(int j=0; j<next.length; j++)
\r
330 if (!visible.containsKey(next[j].type))
\r
333 if ( next[j].featureGroup != null
\r
334 && !groups.contains(next[j].featureGroup))
\r
335 groups.addElement(next[j].featureGroup);
\r
340 String group = null;
\r
346 if (groups.size() > 0 && groupIndex < groups.size())
\r
348 group = groups.elementAt(groupIndex).toString();
\r
349 out.append("\nSTARTGROUP\t" + group + "\n");
\r
354 for (int i = 0; i < seqs.length; i++)
\r
356 next = seqs[i].getSequenceFeatures();
\r
359 for (int j = 0; j < next.length; j++)
\r
361 if (!visible.containsKey(next[j].type))
\r
365 && (next[j].featureGroup==null
\r
366 || !next[j].featureGroup.equals(group))
\r
370 if(group==null && next[j].featureGroup!=null)
\r
373 if(next[j].description==null || next[j].description.equals(""))
\r
374 out.append(next[j].type+"\t");
\r
376 out.append(next[j].description + "\t");
\r
378 out.append( seqs[i].getName() + "\t-1\t"
\r
379 + next[j].begin + "\t"
\r
380 + next[j].end + "\t"
\r
381 + next[j].type + "\n"
\r
389 out.append("ENDGROUP\t"+group+"\n");
\r
396 while(groupIndex < groups.size()+1);
\r
399 return out.toString();
\r
402 public String printGFFFormat(SequenceI [] seqs, Hashtable visible)
\r
404 StringBuffer out = new StringBuffer();
\r
405 SequenceFeature [] next;
\r
407 for(int i=0; i<seqs.length; i++)
\r
409 if(seqs[i].getSequenceFeatures()!=null)
\r
411 next = seqs[i].getSequenceFeatures();
\r
412 for(int j=0; j<next.length; j++)
\r
414 if(!visible.containsKey(next[j].type))
\r
417 out.append(seqs[i].getName() + "\t"
\r
418 + next[j].description + "\t"
\r
419 + next[j].type + "\t"
\r
420 + next[j].begin + "\t"
\r
421 + next[j].end + "\t"
\r
422 + next[j].score + "\t"
\r
425 if(next[j].getValue("STRAND")!=null)
\r
426 out.append(next[j].getValue("STRAND")+"\t");
\r
429 if(next[j].getValue("FRAME")!=null)
\r
430 out.append(next[j].getValue("FRAME")+"\n");
\r
438 return out.toString();
\r
441 public void parse()
\r
449 * @return DOCUMENT ME!
\r
451 public String print()
\r
453 return "USE printGFFFormat() or printJalviewFormat()";
\r