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 * 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 seqId = token = st.nextToken();
\r
160 seq = align.findName(seqId, 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 // 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
205 sf.setValue("ATTRIBUTES", attributes.toString());
\r
208 seq.addSequenceFeature(sf);
\r
209 while ((seq = align.findName(seq, seqId, true)) != null)
\r
211 seq.addSequenceFeature(new SequenceFeature(sf));
\r
217 if (GFFFile && seq == null)
\r
223 desc = st.nextToken();
\r
225 if (!st.hasMoreTokens())
\r
228 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
\r
229 // in all probability, this isn't a file we understand, so bail
\r
234 token = st.nextToken();
\r
236 if (!token.equals("ID_NOT_SPECIFIED"))
\r
238 seq = align.findName(seqId = token, true);
\r
246 index = Integer.parseInt(st.nextToken());
\r
247 seq = align.getSequenceAt(index);
\r
248 } catch (NumberFormatException ex)
\r
256 System.out.println("Sequence not found: " + line);
\r
260 start = Integer.parseInt(st.nextToken());
\r
261 end = Integer.parseInt(st.nextToken());
\r
263 type = st.nextToken();
\r
265 if (!colours.containsKey(type))
\r
267 // Probably the old style groups file
\r
268 UserColourScheme ucs = new UserColourScheme(type);
\r
269 colours.put(type, ucs.findColour('A'));
\r
272 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
274 if (groupLink != null && removeHTML)
\r
276 sf.addLink(groupLink);
\r
277 sf.description += "%LINK%";
\r
279 if (typeLink.containsKey(type) && removeHTML)
\r
281 sf.addLink(typeLink.get(type).toString());
\r
282 sf.description += "%LINK%";
\r
285 parseDescriptionHTML(sf, removeHTML);
\r
287 seq.addSequenceFeature(sf);
\r
289 while (seqId != null
\r
290 && (seq = align.findName(seq, seqId, false)) != null)
\r
292 seq.addSequenceFeature(new SequenceFeature(sf));
\r
294 // If we got here, its not a GFFFile
\r
298 } catch (Exception ex)
\r
300 System.out.println(line);
\r
301 System.out.println("Error parsing feature file: " + ex + "\n" + line);
\r
302 ex.printStackTrace(System.err);
\r
309 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
311 if (sf.getDescription() == null)
\r
317 && sf.getDescription().toUpperCase().indexOf("<HTML>") == -1)
\r
319 removeHTML = false;
\r
322 StringBuffer sb = new StringBuffer();
\r
323 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
324 String token, link;
\r
327 while (st.hasMoreElements())
\r
329 token = st.nextToken("&>");
\r
330 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
336 startTag = token.indexOf("<");
\r
340 tag = token.substring(startTag + 1);
\r
341 token = token.substring(0, startTag);
\r
344 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
\r
346 if (token.length() > 0)
\r
350 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
\r
351 String label = st.nextToken("<>");
\r
352 sf.addLink(label + "|" + link);
\r
353 sb.append(label + "%LINK%");
\r
355 else if (tag != null && tag.equalsIgnoreCase("br"))
\r
359 else if (token.startsWith("lt;"))
\r
361 sb.append("<" + token.substring(3));
\r
363 else if (token.startsWith("gt;"))
\r
365 sb.append(">" + token.substring(3));
\r
367 else if (token.startsWith("amp;"))
\r
369 sb.append("&" + token.substring(4));
\r
379 sf.description = sb.toString();
\r
385 * generate a features file for seqs
\r
388 * source of sequence features
\r
390 * hash of feature types and colours
\r
391 * @return features file contents
\r
393 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
\r
395 return printJalviewFormat(seqs, visible, true);
\r
399 * generate a features file for seqs with colours from visible (if any)
\r
402 * source of features
\r
404 * hash of Colours for each feature type
\r
406 * when true only feature types in 'visible' will be output
\r
407 * @return features file contents
\r
409 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
\r
412 StringBuffer out = new StringBuffer();
\r
413 SequenceFeature[] next;
\r
415 if (visOnly && (visible == null || visible.size() < 1))
\r
417 return "No Features Visible";
\r
419 if (visible != null && visOnly)
\r
421 // write feature colours only if we're given them and we are generating
\r
423 Enumeration en = visible.keys();
\r
426 while (en.hasMoreElements())
\r
428 type = en.nextElement().toString();
\r
429 color = Integer.parseInt(visible.get(type).toString());
\r
432 + jalview.util.Format
\r
433 .getHexString(new java.awt.Color(color)) + "\n");
\r
436 // Work out which groups are both present and visible
\r
437 Vector groups = new Vector();
\r
438 int groupIndex = 0;
\r
440 for (int i = 0; i < seqs.length; i++)
\r
442 next = seqs[i].getSequenceFeatures();
\r
445 for (int j = 0; j < next.length; j++)
\r
447 if (visOnly && !visible.containsKey(next[j].type))
\r
452 if (next[j].featureGroup != null
\r
453 && !groups.contains(next[j].featureGroup))
\r
455 groups.addElement(next[j].featureGroup);
\r
461 String group = null;
\r
466 if (groups.size() > 0 && groupIndex < groups.size())
\r
468 group = groups.elementAt(groupIndex).toString();
\r
469 out.append("\nSTARTGROUP\t" + group + "\n");
\r
476 for (int i = 0; i < seqs.length; i++)
\r
478 next = seqs[i].getSequenceFeatures();
\r
481 for (int j = 0; j < next.length; j++)
\r
483 if (visOnly && !visible.containsKey(next[j].type))
\r
489 && (next[j].featureGroup == null || !next[j].featureGroup
\r
495 if (group == null && next[j].featureGroup != null)
\r
500 if (next[j].description == null
\r
501 || next[j].description.equals(""))
\r
503 out.append(next[j].type + "\t");
\r
507 if (next[j].links != null
\r
508 && next[j].getDescription().indexOf("<html>") == -1)
\r
510 out.append("<html>");
\r
513 out.append(next[j].description + " ");
\r
514 if (next[j].links != null)
\r
516 for (int l = 0; l < next[j].links.size(); l++)
\r
518 String label = next[j].links.elementAt(l).toString();
\r
519 String href = label.substring(label.indexOf("|") + 1);
\r
520 label = label.substring(0, label.indexOf("|"));
\r
522 if (next[j].description.indexOf(href) == -1)
\r
525 .append("<a href=\"" + href + "\">" + label
\r
530 if (next[j].getDescription().indexOf("</html>") == -1)
\r
532 out.append("</html>");
\r
539 out.append(seqs[i].getName() + "\t-1\t" + next[j].begin + "\t"
\r
540 + next[j].end + "\t" + next[j].type + "\n");
\r
547 out.append("ENDGROUP\t" + group + "\n");
\r
555 } while (groupIndex < groups.size() + 1);
\r
557 return out.toString();
\r
560 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
\r
562 return printGFFFormat(seqs, visible, true);
\r
565 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
\r
568 StringBuffer out = new StringBuffer();
\r
569 SequenceFeature[] next;
\r
572 for (int i = 0; i < seqs.length; i++)
\r
574 if (seqs[i].getSequenceFeatures() != null)
\r
576 next = seqs[i].getSequenceFeatures();
\r
577 for (int j = 0; j < next.length; j++)
\r
579 if (visOnly && !visible.containsKey(next[j].type))
\r
584 source = next[j].featureGroup;
\r
585 if (source == null)
\r
587 source = next[j].getDescription();
\r
590 out.append(seqs[i].getName() + "\t" + source + "\t"
\r
591 + next[j].type + "\t" + next[j].begin + "\t"
\r
592 + next[j].end + "\t" + next[j].score + "\t");
\r
594 if (next[j].getValue("STRAND") != null)
\r
596 out.append(next[j].getValue("STRAND") + "\t");
\r
603 if (next[j].getValue("FRAME") != null)
\r
605 out.append(next[j].getValue("FRAME"));
\r
612 if (next[j].getValue("ATTRIBUTES") != null)
\r
614 out.append(next[j].getValue("ATTRIBUTES"));
\r
623 return out.toString();
\r
627 * this is only for the benefit of object polymorphism - method does nothing.
\r
629 public void parse()
\r
635 * this is only for the benefit of object polymorphism - method does nothing.
\r
637 * @return error message
\r
639 public String print()
\r
641 return "USE printGFFFormat() or printJalviewFormat()";
\r