2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 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 * work around for GFF interpretation bug where source string becomes description rather than a group
\r
41 private boolean doGffSource = true;
\r
44 * Creates a new FeaturesFile object.
\r
46 public FeaturesFile()
\r
51 * Creates a new FeaturesFile object.
\r
58 * @throws IOException
\r
61 public FeaturesFile(String inFile, String type) throws IOException
\r
63 super(inFile, type);
\r
66 public FeaturesFile(FileParse source) throws IOException
\r
72 * The Application can render HTML, but the applet will remove HTML tags and
\r
73 * replace links with %LINK% Both need to read links in HTML however
\r
75 * @throws IOException
\r
78 public boolean parse(AlignmentI align, Hashtable colours,
\r
81 return parse(align, colours, null, removeHTML);
\r
85 * The Application can render HTML, but the applet will remove HTML tags and
\r
86 * replace links with %LINK% Both need to read links in HTML however
\r
88 * @throws IOException
\r
91 public boolean parse(AlignmentI align, Hashtable colours,
\r
92 Hashtable featureLink, boolean removeHTML)
\r
97 SequenceI seq = null;
\r
98 String type, desc, token = null;
\r
100 int index, start, end;
\r
102 StringTokenizer st;
\r
103 SequenceFeature sf;
\r
104 String featureGroup = null, groupLink = null;
\r
105 Hashtable typeLink = new Hashtable();
\r
107 boolean GFFFile = true;
\r
109 while ((line = nextLine()) != null)
\r
111 if (line.startsWith("#"))
\r
116 st = new StringTokenizer(line, "\t");
\r
117 if (st.countTokens() > 1 && st.countTokens() < 4)
\r
120 type = st.nextToken();
\r
121 if (type.equalsIgnoreCase("startgroup"))
\r
123 featureGroup = st.nextToken();
\r
124 if (st.hasMoreElements())
\r
126 groupLink = st.nextToken();
\r
127 featureLink.put(featureGroup, groupLink);
\r
130 else if (type.equalsIgnoreCase("endgroup"))
\r
132 // We should check whether this is the current group,
\r
133 // but at present theres no way of showing more than 1 group
\r
135 featureGroup = null;
\r
140 UserColourScheme ucs = new UserColourScheme(st.nextToken());
\r
141 colours.put(type, ucs.findColour('A'));
\r
142 if (st.hasMoreElements())
\r
144 String link = st.nextToken();
\r
145 typeLink.put(type, link);
\r
146 if (featureLink == null)
\r
148 featureLink = new Hashtable();
\r
150 featureLink.put(type, link);
\r
157 while (st.hasMoreElements())
\r
162 // Still possible this is an old Jalview file,
\r
163 // which does not have type colours at the beginning
\r
164 seqId = token = st.nextToken();
\r
165 seq = align.findName(seqId, true);
\r
168 desc = st.nextToken();
\r
170 if (doGffSource && desc.indexOf(' ')==-1) {
\r
171 // could also be a source term rather than description line
\r
172 group = new String(desc);
\r
174 type = st.nextToken();
\r
177 String stt = st.nextToken();
\r
178 if (stt.length()==0 || stt.equals("-"))
\r
182 start = Integer.parseInt(stt);
\r
184 } catch (NumberFormatException ex)
\r
190 String stt = st.nextToken();
\r
191 if (stt.length()==0 || stt.equals("-"))
\r
195 end = Integer.parseInt(stt);
\r
197 } catch (NumberFormatException ex)
\r
203 score = new Float(st.nextToken()).floatValue();
\r
204 } catch (NumberFormatException ex)
\r
209 sf = new SequenceFeature(type, desc, start, end, score, group);
\r
213 sf.setValue("STRAND", st.nextToken());
\r
214 sf.setValue("FRAME", st.nextToken());
\r
215 } catch (Exception ex)
\r
219 if (st.hasMoreTokens())
\r
221 StringBuffer attributes = new StringBuffer();
\r
222 while (st.hasMoreTokens())
\r
224 attributes.append("\t" + st.nextElement());
\r
226 // TODO validate and split GFF2 attributes field ? parse out ([A-Za-z][A-Za-z0-9_]*) <value> ; and add as sf.setValue(attrib, val);
\r
227 sf.setValue("ATTRIBUTES", attributes.toString());
\r
230 seq.addSequenceFeature(sf);
\r
231 while ((seq = align.findName(seq, seqId, true)) != null)
\r
233 seq.addSequenceFeature(new SequenceFeature(sf));
\r
239 if (GFFFile && seq == null)
\r
245 desc = st.nextToken();
\r
247 if (!st.hasMoreTokens())
\r
250 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
\r
251 // in all probability, this isn't a file we understand, so bail
\r
256 token = st.nextToken();
\r
258 if (!token.equals("ID_NOT_SPECIFIED"))
\r
260 seq = align.findName(seqId = token, true);
\r
268 index = Integer.parseInt(st.nextToken());
\r
269 seq = align.getSequenceAt(index);
\r
270 } catch (NumberFormatException ex)
\r
278 System.out.println("Sequence not found: " + line);
\r
282 start = Integer.parseInt(st.nextToken());
\r
283 end = Integer.parseInt(st.nextToken());
\r
285 type = st.nextToken();
\r
287 if (!colours.containsKey(type))
\r
289 // Probably the old style groups file
\r
290 UserColourScheme ucs = new UserColourScheme(type);
\r
291 colours.put(type, ucs.findColour('A'));
\r
294 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
296 if (groupLink != null && removeHTML)
\r
298 sf.addLink(groupLink);
\r
299 sf.description += "%LINK%";
\r
301 if (typeLink.containsKey(type) && removeHTML)
\r
303 sf.addLink(typeLink.get(type).toString());
\r
304 sf.description += "%LINK%";
\r
307 parseDescriptionHTML(sf, removeHTML);
\r
309 seq.addSequenceFeature(sf);
\r
311 while (seqId != null
\r
312 && (seq = align.findName(seq, seqId, false)) != null)
\r
314 seq.addSequenceFeature(new SequenceFeature(sf));
\r
316 // If we got here, its not a GFFFile
\r
320 } catch (Exception ex)
\r
322 System.out.println(line);
\r
323 System.out.println("Error parsing feature file: " + ex + "\n" + line);
\r
324 ex.printStackTrace(System.err);
\r
331 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
333 if (sf.getDescription() == null)
\r
339 && sf.getDescription().toUpperCase().indexOf("<HTML>") == -1)
\r
341 removeHTML = false;
\r
344 StringBuffer sb = new StringBuffer();
\r
345 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
346 String token, link;
\r
349 while (st.hasMoreElements())
\r
351 token = st.nextToken("&>");
\r
352 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
358 startTag = token.indexOf("<");
\r
362 tag = token.substring(startTag + 1);
\r
363 token = token.substring(0, startTag);
\r
366 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
\r
368 if (token.length() > 0)
\r
372 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
\r
373 String label = st.nextToken("<>");
\r
374 sf.addLink(label + "|" + link);
\r
375 sb.append(label + "%LINK%");
\r
377 else if (tag != null && tag.equalsIgnoreCase("br"))
\r
381 else if (token.startsWith("lt;"))
\r
383 sb.append("<" + token.substring(3));
\r
385 else if (token.startsWith("gt;"))
\r
387 sb.append(">" + token.substring(3));
\r
389 else if (token.startsWith("amp;"))
\r
391 sb.append("&" + token.substring(4));
\r
401 sf.description = sb.toString();
\r
407 * generate a features file for seqs
\r
410 * source of sequence features
\r
412 * hash of feature types and colours
\r
413 * @return features file contents
\r
415 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
\r
417 return printJalviewFormat(seqs, visible, true);
\r
421 * generate a features file for seqs with colours from visible (if any)
\r
424 * source of features
\r
426 * hash of Colours for each feature type
\r
428 * when true only feature types in 'visible' will be output
\r
429 * @return features file contents
\r
431 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
\r
434 StringBuffer out = new StringBuffer();
\r
435 SequenceFeature[] next;
\r
437 if (visOnly && (visible == null || visible.size() < 1))
\r
439 return "No Features Visible";
\r
441 if (visible != null && visOnly)
\r
443 // write feature colours only if we're given them and we are generating
\r
445 Enumeration en = visible.keys();
\r
448 while (en.hasMoreElements())
\r
450 type = en.nextElement().toString();
\r
451 color = Integer.parseInt(visible.get(type).toString());
\r
454 + jalview.util.Format
\r
455 .getHexString(new java.awt.Color(color)) + "\n");
\r
458 // Work out which groups are both present and visible
\r
459 Vector groups = new Vector();
\r
460 int groupIndex = 0;
\r
462 for (int i = 0; i < seqs.length; i++)
\r
464 next = seqs[i].getSequenceFeatures();
\r
467 for (int j = 0; j < next.length; j++)
\r
469 if (visOnly && !visible.containsKey(next[j].type))
\r
474 if (next[j].featureGroup != null
\r
475 && !groups.contains(next[j].featureGroup))
\r
477 groups.addElement(next[j].featureGroup);
\r
483 String group = null;
\r
488 if (groups.size() > 0 && groupIndex < groups.size())
\r
490 group = groups.elementAt(groupIndex).toString();
\r
491 out.append("\nSTARTGROUP\t" + group + "\n");
\r
498 for (int i = 0; i < seqs.length; i++)
\r
500 next = seqs[i].getSequenceFeatures();
\r
503 for (int j = 0; j < next.length; j++)
\r
505 if (visOnly && !visible.containsKey(next[j].type))
\r
511 && (next[j].featureGroup == null || !next[j].featureGroup
\r
517 if (group == null && next[j].featureGroup != null)
\r
522 if (next[j].description == null
\r
523 || next[j].description.equals(""))
\r
525 out.append(next[j].type + "\t");
\r
529 if (next[j].links != null
\r
530 && next[j].getDescription().indexOf("<html>") == -1)
\r
532 out.append("<html>");
\r
535 out.append(next[j].description + " ");
\r
536 if (next[j].links != null)
\r
538 for (int l = 0; l < next[j].links.size(); l++)
\r
540 String label = next[j].links.elementAt(l).toString();
\r
541 String href = label.substring(label.indexOf("|") + 1);
\r
542 label = label.substring(0, label.indexOf("|"));
\r
544 if (next[j].description.indexOf(href) == -1)
\r
547 .append("<a href=\"" + href + "\">" + label
\r
552 if (next[j].getDescription().indexOf("</html>") == -1)
\r
554 out.append("</html>");
\r
561 out.append(seqs[i].getName() + "\t-1\t" + next[j].begin + "\t"
\r
562 + next[j].end + "\t" + next[j].type + "\n");
\r
569 out.append("ENDGROUP\t" + group + "\n");
\r
577 } while (groupIndex < groups.size() + 1);
\r
579 return out.toString();
\r
582 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
\r
584 return printGFFFormat(seqs, visible, true);
\r
587 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
\r
590 StringBuffer out = new StringBuffer();
\r
591 SequenceFeature[] next;
\r
594 for (int i = 0; i < seqs.length; i++)
\r
596 if (seqs[i].getSequenceFeatures() != null)
\r
598 next = seqs[i].getSequenceFeatures();
\r
599 for (int j = 0; j < next.length; j++)
\r
601 if (visOnly && !visible.containsKey(next[j].type))
\r
606 source = next[j].featureGroup;
\r
607 if (source == null)
\r
609 source = next[j].getDescription();
\r
612 out.append(seqs[i].getName() + "\t" + source + "\t"
\r
613 + next[j].type + "\t" + next[j].begin + "\t"
\r
614 + next[j].end + "\t" + next[j].score + "\t");
\r
616 if (next[j].getValue("STRAND") != null)
\r
618 out.append(next[j].getValue("STRAND") + "\t");
\r
625 if (next[j].getValue("FRAME") != null)
\r
627 out.append(next[j].getValue("FRAME"));
\r
634 if (next[j].getValue("ATTRIBUTES") != null)
\r
636 out.append(next[j].getValue("ATTRIBUTES"));
\r
645 return out.toString();
\r
649 * this is only for the benefit of object polymorphism - method does nothing.
\r
651 public void parse()
\r
657 * this is only for the benefit of object polymorphism - method does nothing.
\r
659 * @return error message
\r
661 public String print()
\r
663 return "USE printGFFFormat() or printJalviewFormat()";
\r