2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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 if(st.hasMoreTokens())
\r
178 StringBuffer attributes = new StringBuffer();
\r
179 while (st.hasMoreTokens())
\r
181 attributes.append("\t"+st.nextElement());
\r
183 sf.setValue("ATTRIBUTES", attributes.toString());
\r
186 seq.addSequenceFeature(sf);
\r
192 if(GFFFile && seq==null)
\r
197 desc = st.nextToken();
\r
200 token = st.nextToken();
\r
201 if (!token.equals("ID_NOT_SPECIFIED"))
\r
203 seq = align.findName(token);
\r
209 index = Integer.parseInt(st.nextToken());
\r
210 seq = align.getSequenceAt(index);
\r
212 catch(NumberFormatException ex)
\r
220 System.out.println("Sequence not found: "+line);
\r
224 start = Integer.parseInt(st.nextToken());
\r
225 end = Integer.parseInt(st.nextToken());
\r
227 type = st.nextToken();
\r
229 if (!colours.containsKey(type))
\r
231 // Probably the old style groups file
\r
232 UserColourScheme ucs = new UserColourScheme(type);
\r
233 colours.put(type, ucs.findColour('A'));
\r
236 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
238 seq.addSequenceFeature(sf);
\r
240 if(groupLink!=null && removeHTML)
\r
242 sf.addLink(groupLink);
\r
243 sf.description += "%LINK%";
\r
245 if(typeLink.containsKey(type) && removeHTML)
\r
247 sf.addLink(typeLink.get(type).toString());
\r
248 sf.description += "%LINK%";
\r
251 parseDescriptionHTML(sf, removeHTML);
\r
253 //If we got here, its not a GFFFile
\r
258 catch (Exception ex)
\r
260 System.out.println(line);
\r
261 System.out.println("Error parsing feature file: " + ex +"\n"+line);
\r
268 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
270 if(sf.getDescription()==null)
\r
273 if(removeHTML && sf.getDescription().toUpperCase().indexOf("<HTML>")==-1)
\r
274 removeHTML = false;
\r
276 StringBuffer sb = new StringBuffer();
\r
277 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
278 String token, link;
\r
281 while(st.hasMoreElements())
\r
283 token = st.nextToken("&>");
\r
284 if(token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
290 startTag = token.indexOf("<");
\r
294 tag = token.substring(startTag+1);
\r
295 token = token.substring(0, startTag);
\r
298 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
\r
300 if(token.length()>0)
\r
302 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
\r
303 String label = st.nextToken("<>");
\r
304 sf.addLink(label + "|" + link);
\r
305 sb.append(label + "%LINK%");
\r
307 else if (tag != null && tag.equalsIgnoreCase("br"))
\r
309 else if (token.startsWith("lt;"))
\r
310 sb.append("<" + token.substring(3));
\r
311 else if (token.startsWith("gt;"))
\r
312 sb.append(">" + token.substring(3));
\r
313 else if (token.startsWith("amp;"))
\r
314 sb.append("&" + token.substring(4));
\r
322 sf.description = sb.toString();
\r
329 * @param s DOCUMENT ME!
\r
330 * @param len DOCUMENT ME!
\r
331 * @param gaps DOCUMENT ME!
\r
332 * @param displayId DOCUMENT ME!
\r
334 * @return DOCUMENT ME!
\r
336 public String printJalviewFormat(SequenceI [] seqs,
\r
339 StringBuffer out = new StringBuffer();
\r
340 SequenceFeature [] next;
\r
342 if(visible==null || visible.size()<1)
\r
343 return "No Features Visible";
\r
345 Enumeration en = visible.keys();
\r
348 while( en.hasMoreElements() )
\r
350 type = en.nextElement().toString();
\r
351 color = Integer.parseInt( visible.get(type).toString() );
\r
352 out.append(type + "\t"
\r
353 + jalview.util.Format.getHexString(
\r
354 new java.awt.Color(color) )
\r
358 //Work out which groups are both present and visible
\r
359 Vector groups = new Vector();
\r
360 int groupIndex = 0;
\r
362 for(int i=0; i<seqs.length; i++)
\r
364 next = seqs[i].getSequenceFeatures();
\r
367 for(int j=0; j<next.length; j++)
\r
369 if (!visible.containsKey(next[j].type))
\r
372 if ( next[j].featureGroup != null
\r
373 && !groups.contains(next[j].featureGroup))
\r
374 groups.addElement(next[j].featureGroup);
\r
379 String group = null;
\r
385 if (groups.size() > 0 && groupIndex < groups.size())
\r
387 group = groups.elementAt(groupIndex).toString();
\r
388 out.append("\nSTARTGROUP\t" + group + "\n");
\r
393 for (int i = 0; i < seqs.length; i++)
\r
395 next = seqs[i].getSequenceFeatures();
\r
398 for (int j = 0; j < next.length; j++)
\r
400 if (!visible.containsKey(next[j].type))
\r
404 && (next[j].featureGroup==null
\r
405 || !next[j].featureGroup.equals(group))
\r
409 if(group==null && next[j].featureGroup!=null)
\r
412 if(next[j].description==null || next[j].description.equals(""))
\r
413 out.append(next[j].type+"\t");
\r
416 if(next[j].links!=null
\r
417 && next[j].getDescription().indexOf("<html>")==-1)
\r
418 out.append("<html>");
\r
420 out.append(next[j].description+" ");
\r
421 if(next[j].links!=null)
\r
423 for(int l=0; l<next[j].links.size(); l++)
\r
425 String label = next[j].links.elementAt(l).toString();
\r
426 String href = label.substring(label.indexOf("|")+1);
\r
427 label = label.substring(0, label.indexOf("|"));
\r
429 if(next[j].description.indexOf(href)==-1)
\r
431 out.append("<a href=\""
\r
439 if (next[j].getDescription().indexOf("</html>") == -1)
\r
440 out.append("</html>");
\r
448 out.append( seqs[i].getName() + "\t-1\t"
\r
449 + next[j].begin + "\t"
\r
450 + next[j].end + "\t"
\r
451 + next[j].type + "\n"
\r
459 out.append("ENDGROUP\t"+group+"\n");
\r
466 while(groupIndex < groups.size()+1);
\r
469 return out.toString();
\r
472 public String printGFFFormat(SequenceI [] seqs, Hashtable visible)
\r
474 StringBuffer out = new StringBuffer();
\r
475 SequenceFeature [] next;
\r
478 for(int i=0; i<seqs.length; i++)
\r
480 if(seqs[i].getSequenceFeatures()!=null)
\r
482 next = seqs[i].getSequenceFeatures();
\r
483 for(int j=0; j<next.length; j++)
\r
485 if(!visible.containsKey(next[j].type))
\r
488 source = next[j].featureGroup;
\r
490 source = next[j].getDescription();
\r
492 out.append(seqs[i].getName() + "\t"
\r
494 + next[j].type + "\t"
\r
495 + next[j].begin + "\t"
\r
496 + next[j].end + "\t"
\r
497 + next[j].score + "\t"
\r
500 if(next[j].getValue("STRAND")!=null)
\r
501 out.append(next[j].getValue("STRAND")+"\t");
\r
505 if(next[j].getValue("FRAME")!=null)
\r
506 out.append(next[j].getValue("FRAME"));
\r
510 if(next[j].getValue("ATTRIBUTES")!=null)
\r
511 out.append(next[j].getValue("ATTRIBUTES"));
\r
519 return out.toString();
\r
522 public void parse()
\r
530 * @return DOCUMENT ME!
\r
532 public String print()
\r
534 return "USE printGFFFormat() or printJalviewFormat()";
\r