2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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
24 import jalview.datamodel.*;
\r
25 import jalview.schemes.*;
\r
28 * Parse and create Jalview Features files Detects GFF format features files and
\r
29 * parses. Does not implement standard print() - call specific printFeatures or
\r
30 * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object
\r
31 * for the features annotation - this normally works on an exact match.
\r
34 * @version $Revision$
\r
36 public class FeaturesFile extends AlignFile
\r
39 * Creates a new FeaturesFile object.
\r
41 public FeaturesFile()
\r
46 * Creates a new FeaturesFile object.
\r
53 * @throws IOException
\r
56 public FeaturesFile(String inFile, String type) throws IOException
\r
58 super(inFile, type);
\r
61 public FeaturesFile(FileParse source) throws IOException
\r
67 * The Application can render HTML, but the applet will remove HTML tags and
\r
68 * replace links with %LINK% Both need to read links in HTML however
\r
70 * @throws IOException
\r
73 public boolean parse(AlignmentI align, Hashtable colours,
\r
76 return parse(align, colours, null, removeHTML);
\r
80 * The Application can render HTML, but the applet will remove HTML tags and
\r
81 * replace links with %LINK% Both need to read links in HTML however
\r
83 * @throws IOException
\r
86 public boolean parse(AlignmentI align, Hashtable colours,
\r
87 Hashtable featureLink, boolean removeHTML)
\r
92 SequenceI seq = null;
\r
93 String type, desc, token = null;
\r
95 int index, start, end;
\r
99 String featureGroup = null, groupLink = null;
\r
100 Hashtable typeLink = new Hashtable();
\r
102 boolean GFFFile = true;
\r
104 while ((line = nextLine()) != null)
\r
106 if (line.startsWith("#"))
\r
111 st = new StringTokenizer(line, "\t");
\r
112 if (st.countTokens() > 1 && st.countTokens() < 4)
\r
115 type = st.nextToken();
\r
116 if (type.equalsIgnoreCase("startgroup"))
\r
118 featureGroup = st.nextToken();
\r
119 if (st.hasMoreElements())
\r
121 groupLink = st.nextToken();
\r
122 featureLink.put(featureGroup, groupLink);
\r
125 else if (type.equalsIgnoreCase("endgroup"))
\r
127 // We should check whether this is the current group,
\r
128 // but at present theres no way of showing more than 1 group
\r
130 featureGroup = null;
\r
135 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
136 colours.put(type, ucs.findColour('A'));
\r
137 if (st.hasMoreElements())
\r
139 String link = st.nextToken();
\r
140 typeLink.put(type, link);
\r
141 if (featureLink == null)
\r
143 featureLink = new Hashtable();
\r
145 featureLink.put(type, link);
\r
152 while (st.hasMoreElements())
\r
157 // Still possible this is an old Jalview file,
\r
158 // which does not have type colours at the beginning
\r
159 token = st.nextToken();
\r
160 seq = align.findName(token, true);
\r
163 desc = st.nextToken();
\r
164 type = st.nextToken();
\r
167 start = Integer.parseInt(st.nextToken());
\r
168 } catch (NumberFormatException ex)
\r
174 end = Integer.parseInt(st.nextToken());
\r
175 } catch (NumberFormatException ex)
\r
181 score = new Float(st.nextToken()).floatValue();
\r
182 } catch (NumberFormatException ex)
\r
187 sf = new SequenceFeature(type, desc, start, end, score, null);
\r
191 sf.setValue("STRAND", st.nextToken());
\r
192 sf.setValue("FRAME", st.nextToken());
\r
193 } catch (Exception ex)
\r
197 if (st.hasMoreTokens())
\r
199 StringBuffer attributes = new StringBuffer();
\r
200 while (st.hasMoreTokens())
\r
202 attributes.append("\t" + st.nextElement());
\r
204 sf.setValue("ATTRIBUTES", attributes.toString());
\r
207 seq.addSequenceFeature(sf);
\r
213 if (GFFFile && seq == null)
\r
219 desc = st.nextToken();
\r
221 if (!st.hasMoreTokens())
\r
224 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
\r
225 // in all probability, this isn't a file we understand, so bail
\r
230 token = st.nextToken();
\r
232 if (!token.equals("ID_NOT_SPECIFIED"))
\r
234 seq = align.findName(token, true);
\r
241 index = Integer.parseInt(st.nextToken());
\r
242 seq = align.getSequenceAt(index);
\r
243 } catch (NumberFormatException ex)
\r
251 System.out.println("Sequence not found: " + line);
\r
255 start = Integer.parseInt(st.nextToken());
\r
256 end = Integer.parseInt(st.nextToken());
\r
258 type = st.nextToken();
\r
260 if (!colours.containsKey(type))
\r
262 // Probably the old style groups file
\r
263 UserColourScheme ucs = new UserColourScheme(type);
\r
264 colours.put(type, ucs.findColour('A'));
\r
267 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
269 seq.addSequenceFeature(sf);
\r
271 if (groupLink != null && removeHTML)
\r
273 sf.addLink(groupLink);
\r
274 sf.description += "%LINK%";
\r
276 if (typeLink.containsKey(type) && removeHTML)
\r
278 sf.addLink(typeLink.get(type).toString());
\r
279 sf.description += "%LINK%";
\r
282 parseDescriptionHTML(sf, removeHTML);
\r
284 // If we got here, its not a GFFFile
\r
288 } catch (Exception ex)
\r
290 System.out.println(line);
\r
291 System.out.println("Error parsing feature file: " + ex + "\n" + line);
\r
292 ex.printStackTrace(System.err);
\r
299 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
301 if (sf.getDescription() == null)
\r
307 && sf.getDescription().toUpperCase().indexOf("<HTML>") == -1)
\r
309 removeHTML = false;
\r
312 StringBuffer sb = new StringBuffer();
\r
313 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
314 String token, link;
\r
317 while (st.hasMoreElements())
\r
319 token = st.nextToken("&>");
\r
320 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
326 startTag = token.indexOf("<");
\r
330 tag = token.substring(startTag + 1);
\r
331 token = token.substring(0, startTag);
\r
334 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
\r
336 if (token.length() > 0)
\r
340 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
\r
341 String label = st.nextToken("<>");
\r
342 sf.addLink(label + "|" + link);
\r
343 sb.append(label + "%LINK%");
\r
345 else if (tag != null && tag.equalsIgnoreCase("br"))
\r
349 else if (token.startsWith("lt;"))
\r
351 sb.append("<" + token.substring(3));
\r
353 else if (token.startsWith("gt;"))
\r
355 sb.append(">" + token.substring(3));
\r
357 else if (token.startsWith("amp;"))
\r
359 sb.append("&" + token.substring(4));
\r
369 sf.description = sb.toString();
\r
375 * generate a features file for seqs
\r
376 * @param seqs source of sequence features
\r
377 * @param visible hash of feature types and colours
\r
378 * @return features file contents
\r
380 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
\r
382 return printJalviewFormat(seqs, visible, true);
\r
386 * generate a features file for seqs with colours from visible (if any)
\r
387 * @param seqs source of features
\r
388 * @param visible hash of Colours for each feature type
\r
389 * @param visOnly when true only feature types in 'visible' will be output
\r
390 * @return features file contents
\r
392 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
\r
395 StringBuffer out = new StringBuffer();
\r
396 SequenceFeature[] next;
\r
398 if (visOnly && (visible == null || visible.size() < 1))
\r
400 return "No Features Visible";
\r
402 if (visible != null && visOnly)
\r
404 // write feature colours only if we're given them and we are generating viewed features
\r
405 Enumeration en = visible.keys();
\r
408 while (en.hasMoreElements())
\r
410 type = en.nextElement().toString();
\r
411 color = Integer.parseInt(visible.get(type).toString());
\r
414 + jalview.util.Format
\r
415 .getHexString(new java.awt.Color(color)) + "\n");
\r
418 // Work out which groups are both present and visible
\r
419 Vector groups = new Vector();
\r
420 int groupIndex = 0;
\r
422 for (int i = 0; i < seqs.length; i++)
\r
424 next = seqs[i].getSequenceFeatures();
\r
427 for (int j = 0; j < next.length; j++)
\r
429 if (visOnly && !visible.containsKey(next[j].type))
\r
434 if (next[j].featureGroup != null
\r
435 && !groups.contains(next[j].featureGroup))
\r
437 groups.addElement(next[j].featureGroup);
\r
443 String group = null;
\r
448 if (groups.size() > 0 && groupIndex < groups.size())
\r
450 group = groups.elementAt(groupIndex).toString();
\r
451 out.append("\nSTARTGROUP\t" + group + "\n");
\r
458 for (int i = 0; i < seqs.length; i++)
\r
460 next = seqs[i].getSequenceFeatures();
\r
463 for (int j = 0; j < next.length; j++)
\r
465 if (visOnly && !visible.containsKey(next[j].type))
\r
471 && (next[j].featureGroup == null || !next[j].featureGroup
\r
477 if (group == null && next[j].featureGroup != null)
\r
482 if (next[j].description == null
\r
483 || next[j].description.equals(""))
\r
485 out.append(next[j].type + "\t");
\r
489 if (next[j].links != null
\r
490 && next[j].getDescription().indexOf("<html>") == -1)
\r
492 out.append("<html>");
\r
495 out.append(next[j].description + " ");
\r
496 if (next[j].links != null)
\r
498 for (int l = 0; l < next[j].links.size(); l++)
\r
500 String label = next[j].links.elementAt(l).toString();
\r
501 String href = label.substring(label.indexOf("|") + 1);
\r
502 label = label.substring(0, label.indexOf("|"));
\r
504 if (next[j].description.indexOf(href) == -1)
\r
507 .append("<a href=\"" + href + "\">" + label
\r
512 if (next[j].getDescription().indexOf("</html>") == -1)
\r
514 out.append("</html>");
\r
521 out.append(seqs[i].getName() + "\t-1\t" + next[j].begin + "\t"
\r
522 + next[j].end + "\t" + next[j].type + "\n");
\r
529 out.append("ENDGROUP\t" + group + "\n");
\r
537 } while (groupIndex < groups.size() + 1);
\r
539 return out.toString();
\r
542 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
\r
544 return printGFFFormat(seqs, visible, true);
\r
547 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
\r
550 StringBuffer out = new StringBuffer();
\r
551 SequenceFeature[] next;
\r
554 for (int i = 0; i < seqs.length; i++)
\r
556 if (seqs[i].getSequenceFeatures() != null)
\r
558 next = seqs[i].getSequenceFeatures();
\r
559 for (int j = 0; j < next.length; j++)
\r
561 if (visOnly && !visible.containsKey(next[j].type))
\r
566 source = next[j].featureGroup;
\r
567 if (source == null)
\r
569 source = next[j].getDescription();
\r
572 out.append(seqs[i].getName() + "\t" + source + "\t"
\r
573 + next[j].type + "\t" + next[j].begin + "\t"
\r
574 + next[j].end + "\t" + next[j].score + "\t");
\r
576 if (next[j].getValue("STRAND") != null)
\r
578 out.append(next[j].getValue("STRAND") + "\t");
\r
585 if (next[j].getValue("FRAME") != null)
\r
587 out.append(next[j].getValue("FRAME"));
\r
594 if (next[j].getValue("ATTRIBUTES") != null)
\r
596 out.append(next[j].getValue("ATTRIBUTES"));
\r
605 return out.toString();
\r
609 * this is only for the benefit of object polymorphism - method does nothing.
\r
611 public void parse()
\r
617 * this is only for the benefit of object polymorphism - method does nothing.
\r
619 * @return error message
\r
621 public String print()
\r
623 return "USE printGFFFormat() or printJalviewFormat()";
\r