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 sf.setValue("ATTRIBUTES", attributes.toString());
\r
207 seq.addSequenceFeature(sf);
\r
208 while ((seq = align.findName(seq, seqId, true)) != null)
\r
210 seq.addSequenceFeature(new SequenceFeature(sf));
\r
216 if (GFFFile && seq == null)
\r
222 desc = st.nextToken();
\r
224 if (!st.hasMoreTokens())
\r
227 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
\r
228 // in all probability, this isn't a file we understand, so bail
\r
233 token = st.nextToken();
\r
235 if (!token.equals("ID_NOT_SPECIFIED"))
\r
237 seq = align.findName(seqId = token, true);
\r
245 index = Integer.parseInt(st.nextToken());
\r
246 seq = align.getSequenceAt(index);
\r
247 } catch (NumberFormatException ex)
\r
255 System.out.println("Sequence not found: " + line);
\r
259 start = Integer.parseInt(st.nextToken());
\r
260 end = Integer.parseInt(st.nextToken());
\r
262 type = st.nextToken();
\r
264 if (!colours.containsKey(type))
\r
266 // Probably the old style groups file
\r
267 UserColourScheme ucs = new UserColourScheme(type);
\r
268 colours.put(type, ucs.findColour('A'));
\r
271 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
273 if (groupLink != null && removeHTML)
\r
275 sf.addLink(groupLink);
\r
276 sf.description += "%LINK%";
\r
278 if (typeLink.containsKey(type) && removeHTML)
\r
280 sf.addLink(typeLink.get(type).toString());
\r
281 sf.description += "%LINK%";
\r
284 parseDescriptionHTML(sf, removeHTML);
\r
286 seq.addSequenceFeature(sf);
\r
288 while (seqId != null
\r
289 && (seq = align.findName(seq, seqId, false)) != null)
\r
291 seq.addSequenceFeature(new SequenceFeature(sf));
\r
293 // If we got here, its not a GFFFile
\r
297 } catch (Exception ex)
\r
299 System.out.println(line);
\r
300 System.out.println("Error parsing feature file: " + ex + "\n" + line);
\r
301 ex.printStackTrace(System.err);
\r
308 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
310 if (sf.getDescription() == null)
\r
316 && sf.getDescription().toUpperCase().indexOf("<HTML>") == -1)
\r
318 removeHTML = false;
\r
321 StringBuffer sb = new StringBuffer();
\r
322 StringTokenizer st = new StringTokenizer(sf.getDescription(), "<");
\r
323 String token, link;
\r
326 while (st.hasMoreElements())
\r
328 token = st.nextToken("&>");
\r
329 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
\r
335 startTag = token.indexOf("<");
\r
339 tag = token.substring(startTag + 1);
\r
340 token = token.substring(0, startTag);
\r
343 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
\r
345 if (token.length() > 0)
\r
349 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
\r
350 String label = st.nextToken("<>");
\r
351 sf.addLink(label + "|" + link);
\r
352 sb.append(label + "%LINK%");
\r
354 else if (tag != null && tag.equalsIgnoreCase("br"))
\r
358 else if (token.startsWith("lt;"))
\r
360 sb.append("<" + token.substring(3));
\r
362 else if (token.startsWith("gt;"))
\r
364 sb.append(">" + token.substring(3));
\r
366 else if (token.startsWith("amp;"))
\r
368 sb.append("&" + token.substring(4));
\r
378 sf.description = sb.toString();
\r
384 * generate a features file for seqs
\r
387 * source of sequence features
\r
389 * hash of feature types and colours
\r
390 * @return features file contents
\r
392 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
\r
394 return printJalviewFormat(seqs, visible, true);
\r
398 * generate a features file for seqs with colours from visible (if any)
\r
401 * source of features
\r
403 * hash of Colours for each feature type
\r
405 * when true only feature types in 'visible' will be output
\r
406 * @return features file contents
\r
408 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
\r
411 StringBuffer out = new StringBuffer();
\r
412 SequenceFeature[] next;
\r
414 if (visOnly && (visible == null || visible.size() < 1))
\r
416 return "No Features Visible";
\r
418 if (visible != null && visOnly)
\r
420 // write feature colours only if we're given them and we are generating
\r
422 Enumeration en = visible.keys();
\r
425 while (en.hasMoreElements())
\r
427 type = en.nextElement().toString();
\r
428 color = Integer.parseInt(visible.get(type).toString());
\r
431 + jalview.util.Format
\r
432 .getHexString(new java.awt.Color(color)) + "\n");
\r
435 // Work out which groups are both present and visible
\r
436 Vector groups = new Vector();
\r
437 int groupIndex = 0;
\r
439 for (int i = 0; i < seqs.length; i++)
\r
441 next = seqs[i].getSequenceFeatures();
\r
444 for (int j = 0; j < next.length; j++)
\r
446 if (visOnly && !visible.containsKey(next[j].type))
\r
451 if (next[j].featureGroup != null
\r
452 && !groups.contains(next[j].featureGroup))
\r
454 groups.addElement(next[j].featureGroup);
\r
460 String group = null;
\r
465 if (groups.size() > 0 && groupIndex < groups.size())
\r
467 group = groups.elementAt(groupIndex).toString();
\r
468 out.append("\nSTARTGROUP\t" + group + "\n");
\r
475 for (int i = 0; i < seqs.length; i++)
\r
477 next = seqs[i].getSequenceFeatures();
\r
480 for (int j = 0; j < next.length; j++)
\r
482 if (visOnly && !visible.containsKey(next[j].type))
\r
488 && (next[j].featureGroup == null || !next[j].featureGroup
\r
494 if (group == null && next[j].featureGroup != null)
\r
499 if (next[j].description == null
\r
500 || next[j].description.equals(""))
\r
502 out.append(next[j].type + "\t");
\r
506 if (next[j].links != null
\r
507 && next[j].getDescription().indexOf("<html>") == -1)
\r
509 out.append("<html>");
\r
512 out.append(next[j].description + " ");
\r
513 if (next[j].links != null)
\r
515 for (int l = 0; l < next[j].links.size(); l++)
\r
517 String label = next[j].links.elementAt(l).toString();
\r
518 String href = label.substring(label.indexOf("|") + 1);
\r
519 label = label.substring(0, label.indexOf("|"));
\r
521 if (next[j].description.indexOf(href) == -1)
\r
524 .append("<a href=\"" + href + "\">" + label
\r
529 if (next[j].getDescription().indexOf("</html>") == -1)
\r
531 out.append("</html>");
\r
538 out.append(seqs[i].getName() + "\t-1\t" + next[j].begin + "\t"
\r
539 + next[j].end + "\t" + next[j].type + "\n");
\r
546 out.append("ENDGROUP\t" + group + "\n");
\r
554 } while (groupIndex < groups.size() + 1);
\r
556 return out.toString();
\r
559 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
\r
561 return printGFFFormat(seqs, visible, true);
\r
564 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
\r
567 StringBuffer out = new StringBuffer();
\r
568 SequenceFeature[] next;
\r
571 for (int i = 0; i < seqs.length; i++)
\r
573 if (seqs[i].getSequenceFeatures() != null)
\r
575 next = seqs[i].getSequenceFeatures();
\r
576 for (int j = 0; j < next.length; j++)
\r
578 if (visOnly && !visible.containsKey(next[j].type))
\r
583 source = next[j].featureGroup;
\r
584 if (source == null)
\r
586 source = next[j].getDescription();
\r
589 out.append(seqs[i].getName() + "\t" + source + "\t"
\r
590 + next[j].type + "\t" + next[j].begin + "\t"
\r
591 + next[j].end + "\t" + next[j].score + "\t");
\r
593 if (next[j].getValue("STRAND") != null)
\r
595 out.append(next[j].getValue("STRAND") + "\t");
\r
602 if (next[j].getValue("FRAME") != null)
\r
604 out.append(next[j].getValue("FRAME"));
\r
611 if (next[j].getValue("ATTRIBUTES") != null)
\r
613 out.append(next[j].getValue("ATTRIBUTES"));
\r
622 return out.toString();
\r
626 * this is only for the benefit of object polymorphism - method does nothing.
\r
628 public void parse()
\r
634 * this is only for the benefit of object polymorphism - method does nothing.
\r
636 * @return error message
\r
638 public String print()
\r
640 return "USE printGFFFormat() or printJalviewFormat()";
\r