2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import jalview.analysis.SequenceIdMatcher;
24 import jalview.datamodel.*;
25 import jalview.schemes.*;
26 import jalview.util.Format;
29 * Parse and create Jalview Features files Detects GFF format features files and
30 * parses. Does not implement standard print() - call specific printFeatures or
31 * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object
32 * for the features annotation - this normally works on an exact match.
37 public class FeaturesFile extends AlignFile
40 * work around for GFF interpretation bug where source string becomes
41 * description rather than a group
43 private boolean doGffSource = true;
46 * Creates a new FeaturesFile object.
53 * Creates a new FeaturesFile object.
63 public FeaturesFile(String inFile, String type) throws IOException
68 public FeaturesFile(FileParse source) throws IOException
74 * Parse GFF or sequence features file using case-independent matching,
78 * - alignment/dataset containing sequences that are to be annotated
80 * - hashtable to store feature colour definitions
82 * - process html strings into plain text
83 * @return true if features were added
85 public boolean parse(AlignmentI align, Hashtable colours,
88 return parse(align, colours, null, removeHTML, false);
92 * Parse GFF or sequence features file optionally using case-independent
93 * matching, discarding URLs
96 * - alignment/dataset containing sequences that are to be annotated
98 * - hashtable to store feature colour definitions
100 * - process html strings into plain text
101 * @param relaxedIdmatching
102 * - when true, ID matches to compound sequence IDs are allowed
103 * @return true if features were added
105 public boolean parse(AlignmentI align, Map colours, boolean removeHTML,
106 boolean relaxedIdMatching)
108 return parse(align, colours, null, removeHTML, relaxedIdMatching);
112 * Parse GFF or sequence features file optionally using case-independent
116 * - alignment/dataset containing sequences that are to be annotated
118 * - hashtable to store feature colour definitions
120 * - hashtable to store associated URLs
122 * - process html strings into plain text
123 * @return true if features were added
125 public boolean parse(AlignmentI align, Map colours, Map featureLink,
128 return parse(align, colours, featureLink, removeHTML, false);
132 * Parse GFF or sequence features file
135 * - alignment/dataset containing sequences that are to be annotated
137 * - hashtable to store feature colour definitions
139 * - hashtable to store associated URLs
141 * - process html strings into plain text
142 * @param relaxedIdmatching
143 * - when true, ID matches to compound sequence IDs are allowed
144 * @return true if features were added
146 public boolean parse(AlignmentI align, Map colours, Map featureLink,
147 boolean removeHTML, boolean relaxedIdmatching)
153 SequenceI seq = null;
154 String type, desc, token = null;
156 int index, start, end;
160 String featureGroup = null, groupLink = null;
161 Map typeLink = new Hashtable();
163 * when true, assume GFF style features rather than Jalview style.
165 boolean GFFFile = true;
166 while ((line = nextLine()) != null)
168 if (line.startsWith("#"))
173 st = new StringTokenizer(line, "\t");
174 if (st.countTokens() == 1)
176 if (line.trim().equalsIgnoreCase("GFF"))
178 // Start parsing file as if it might be GFF again.
183 if (st.countTokens() > 1 && st.countTokens() < 4)
186 type = st.nextToken();
187 if (type.equalsIgnoreCase("startgroup"))
189 featureGroup = st.nextToken();
190 if (st.hasMoreElements())
192 groupLink = st.nextToken();
193 featureLink.put(featureGroup, groupLink);
196 else if (type.equalsIgnoreCase("endgroup"))
198 // We should check whether this is the current group,
199 // but at present theres no way of showing more than 1 group
206 Object colour = null;
207 String colscheme = st.nextToken();
208 if (colscheme.indexOf("|") > -1
209 || colscheme.trim().equalsIgnoreCase("label"))
211 // Parse '|' separated graduated colourscheme fields:
212 // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
213 // can either provide 'label' only, first is optional, next two
214 // colors are required (but may be
215 // left blank), next is optional, nxt two min/max are required.
216 // first is either 'label'
217 // first/second and third are both hexadecimal or word equivalent
219 // next two are values parsed as floats.
220 // fifth is either 'above','below', or 'none'.
221 // sixth is a float value and only required when fifth is either
222 // 'above' or 'below'.
223 StringTokenizer gcol = new StringTokenizer(colscheme, "|",
226 int threshtype = AnnotationColourGradient.NO_THRESHOLD;
227 float min = Float.MIN_VALUE, max = Float.MAX_VALUE, threshval = Float.NaN;
228 boolean labelCol = false;
230 String mincol = gcol.nextToken();
234 .println("Expected either 'label' or a colour specification in the line: "
238 String maxcol = null;
239 if (mincol.toLowerCase().indexOf("label") == 0)
242 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip
244 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
246 String abso = null, minval, maxval;
249 // at least four more tokens
250 if (mincol.equals("|"))
256 gcol.nextToken(); // skip next '|'
258 // continue parsing rest of line
259 maxcol = gcol.nextToken();
260 if (maxcol.equals("|"))
266 gcol.nextToken(); // skip next '|'
268 abso = gcol.nextToken();
269 gcol.nextToken(); // skip next '|'
270 if (abso.toLowerCase().indexOf("abso") != 0)
277 minval = gcol.nextToken();
278 gcol.nextToken(); // skip next '|'
280 maxval = gcol.nextToken();
281 if (gcol.hasMoreTokens())
283 gcol.nextToken(); // skip next '|'
287 if (minval.length() > 0)
289 min = new Float(minval).floatValue();
291 } catch (Exception e)
294 .println("Couldn't parse the minimum value for graduated colour for type ("
296 + ") - did you misspell 'auto' for the optional automatic colour switch ?");
301 if (maxval.length() > 0)
303 max = new Float(maxval).floatValue();
305 } catch (Exception e)
308 .println("Couldn't parse the maximum value for graduated colour for type ("
315 // add in some dummy min/max colours for the label-only
322 colour = new jalview.schemes.GraduatedColor(
323 new UserColourScheme(mincol).findColour('A'),
324 new UserColourScheme(maxcol).findColour('A'), min,
326 } catch (Exception e)
329 .println("Couldn't parse the graduated colour scheme ("
335 ((jalview.schemes.GraduatedColor) colour)
336 .setColourByLabel(labelCol);
337 ((jalview.schemes.GraduatedColor) colour)
338 .setAutoScaled(abso == null);
339 // add in any additional parameters
340 String ttype = null, tval = null;
341 if (gcol.hasMoreTokens())
343 // threshold type and possibly a threshold value
344 ttype = gcol.nextToken();
345 if (ttype.toLowerCase().startsWith("below"))
347 ((jalview.schemes.GraduatedColor) colour)
348 .setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
350 else if (ttype.toLowerCase().startsWith("above"))
352 ((jalview.schemes.GraduatedColor) colour)
353 .setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
357 ((jalview.schemes.GraduatedColor) colour)
358 .setThreshType(AnnotationColourGradient.NO_THRESHOLD);
359 if (!ttype.toLowerCase().startsWith("no"))
362 .println("Ignoring unrecognised threshold type : "
367 if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
372 tval = gcol.nextToken();
373 ((jalview.schemes.GraduatedColor) colour)
374 .setThresh(new Float(tval).floatValue());
375 } catch (Exception e)
378 .println("Couldn't parse threshold value as a float: ("
383 // parse the thresh-is-min token ?
384 if (gcol.hasMoreTokens())
387 .println("Ignoring additional tokens in parameters in graduated colour specification\n");
388 while (gcol.hasMoreTokens())
390 System.err.println("|" + gcol.nextToken());
392 System.err.println("\n");
398 UserColourScheme ucs = new UserColourScheme(colscheme);
399 colour = ucs.findColour('A');
403 colours.put(type, colour);
405 if (st.hasMoreElements())
407 String link = st.nextToken();
408 typeLink.put(type, link);
409 if (featureLink == null)
411 featureLink = new Hashtable();
413 featureLink.put(type, link);
419 while (st.hasMoreElements())
424 // Still possible this is an old Jalview file,
425 // which does not have type colours at the beginning
426 seqId = token = st.nextToken();
427 seq = findName(align, seqId, relaxedIdmatching);
430 desc = st.nextToken();
432 if (doGffSource && desc.indexOf(' ') == -1)
434 // could also be a source term rather than description line
435 group = new String(desc);
437 type = st.nextToken();
440 String stt = st.nextToken();
441 if (stt.length() == 0 || stt.equals("-"))
447 start = Integer.parseInt(stt);
449 } catch (NumberFormatException ex)
455 String stt = st.nextToken();
456 if (stt.length() == 0 || stt.equals("-"))
462 end = Integer.parseInt(stt);
464 } catch (NumberFormatException ex)
468 // TODO: decide if non positional feature assertion for input data
469 // where end==0 is generally valid
472 // treat as non-positional feature, regardless.
477 score = new Float(st.nextToken()).floatValue();
478 } catch (NumberFormatException ex)
483 sf = new SequenceFeature(type, desc, start, end, score, group);
487 sf.setValue("STRAND", st.nextToken());
488 sf.setValue("FRAME", st.nextToken());
489 } catch (Exception ex)
493 if (st.hasMoreTokens())
495 StringBuffer attributes = new StringBuffer();
496 while (st.hasMoreTokens())
498 attributes.append("\t" + st.nextElement());
500 // TODO validate and split GFF2 attributes field ? parse out
501 // ([A-Za-z][A-Za-z0-9_]*) <value> ; and add as
502 // sf.setValue(attrib, val);
503 sf.setValue("ATTRIBUTES", attributes.toString());
506 seq.addSequenceFeature(sf);
507 while ((seq = align.findName(seq, seqId, true)) != null)
509 seq.addSequenceFeature(new SequenceFeature(sf));
515 if (GFFFile && seq == null)
521 desc = st.nextToken();
523 if (!st.hasMoreTokens())
526 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
527 // in all probability, this isn't a file we understand, so bail
532 token = st.nextToken();
534 if (!token.equals("ID_NOT_SPECIFIED"))
536 seq = findName(align, seqId = token, relaxedIdmatching);
544 index = Integer.parseInt(st.nextToken());
545 seq = align.getSequenceAt(index);
546 } catch (NumberFormatException ex)
554 System.out.println("Sequence not found: " + line);
558 start = Integer.parseInt(st.nextToken());
559 end = Integer.parseInt(st.nextToken());
561 type = st.nextToken();
563 if (!colours.containsKey(type))
565 // Probably the old style groups file
566 UserColourScheme ucs = new UserColourScheme(type);
567 colours.put(type, ucs.findColour('A'));
569 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
570 if (st.hasMoreTokens())
574 score = new Float(st.nextToken()).floatValue();
575 // update colourgradient bounds if allowed to
576 } catch (NumberFormatException ex)
582 if (groupLink != null && removeHTML)
584 sf.addLink(groupLink);
585 sf.description += "%LINK%";
587 if (typeLink.containsKey(type) && removeHTML)
589 sf.addLink(typeLink.get(type).toString());
590 sf.description += "%LINK%";
593 parseDescriptionHTML(sf, removeHTML);
595 seq.addSequenceFeature(sf);
598 && (seq = align.findName(seq, seqId, false)) != null)
600 seq.addSequenceFeature(new SequenceFeature(sf));
602 // If we got here, its not a GFFFile
607 } catch (Exception ex)
609 System.out.println("Error parsing feature file: " + ex + "\n" + line);
610 ex.printStackTrace(System.err);
618 private AlignmentI lastmatchedAl = null;
620 private SequenceIdMatcher matcher = null;
623 * clear any temporary handles used to speed up ID matching
625 private void resetMatcher()
627 lastmatchedAl = null;
631 private SequenceI findName(AlignmentI align, String seqId,
632 boolean relaxedIdMatching)
634 SequenceI match = null;
635 if (relaxedIdMatching)
637 if (lastmatchedAl != align)
639 matcher = new SequenceIdMatcher(
640 (lastmatchedAl = align).getSequencesArray());
642 match = matcher.findIdMatch(seqId);
646 match = align.findName(seqId, true);
651 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
653 if (sf.getDescription() == null)
657 jalview.util.ParseHtmlBodyAndLinks parsed = new jalview.util.ParseHtmlBodyAndLinks(
658 sf.getDescription(), removeHTML, newline);
660 sf.description = (removeHTML) ? parsed.getNonHtmlContent()
662 for (String link : parsed.getLinks())
670 * generate a features file for seqs includes non-pos features by default.
673 * source of sequence features
675 * hash of feature types and colours
676 * @return features file contents
678 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
680 return printJalviewFormat(seqs, visible, true, true);
684 * generate a features file for seqs with colours from visible (if any)
689 * hash of Colours for each feature type
691 * when true only feature types in 'visible' will be output
693 * indicates if non-positional features should be output (regardless
695 * @return features file contents
697 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
698 boolean visOnly, boolean nonpos)
700 StringBuffer out = new StringBuffer();
701 SequenceFeature[] next;
702 boolean featuresGen = false;
703 if (visOnly && !nonpos && (visible == null || visible.size() < 1))
705 // no point continuing.
706 return "No Features Visible";
709 if (visible != null && visOnly)
711 // write feature colours only if we're given them and we are generating
713 // TODO: decide if feature links should also be written here ?
714 Enumeration en = visible.keys();
716 while (en.hasMoreElements())
718 type = en.nextElement().toString();
720 if (visible.get(type) instanceof GraduatedColor)
722 GraduatedColor gc = (GraduatedColor) visible.get(type);
723 color = (gc.isColourByLabel() ? "label|" : "")
724 + Format.getHexString(gc.getMinColor()) + "|"
725 + Format.getHexString(gc.getMaxColor())
726 + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
728 if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
730 if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
736 if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
738 System.err.println("WARNING: Unsupported threshold type ("
739 + gc.getThreshType() + ") : Assuming 'above'");
744 color += "|" + gc.getThresh();
751 else if (visible.get(type) instanceof java.awt.Color)
753 color = Format.getHexString((java.awt.Color) visible.get(type));
757 // legacy support for integer objects containing colour triplet values
758 color = Format.getHexString(new java.awt.Color(Integer
759 .parseInt(visible.get(type).toString())));
767 // Work out which groups are both present and visible
768 Vector groups = new Vector();
770 boolean isnonpos = false;
772 for (int i = 0; i < seqs.length; i++)
774 next = seqs[i].getSequenceFeatures();
777 for (int j = 0; j < next.length; j++)
779 isnonpos = next[j].begin == 0 && next[j].end == 0;
780 if ((!nonpos && isnonpos)
781 || (!isnonpos && visOnly && !visible
782 .containsKey(next[j].type)))
787 if (next[j].featureGroup != null
788 && !groups.contains(next[j].featureGroup))
790 groups.addElement(next[j].featureGroup);
800 if (groups.size() > 0 && groupIndex < groups.size())
802 group = groups.elementAt(groupIndex).toString();
804 out.append("STARTGROUP\t");
813 for (int i = 0; i < seqs.length; i++)
815 next = seqs[i].getSequenceFeatures();
818 for (int j = 0; j < next.length; j++)
820 isnonpos = next[j].begin == 0 && next[j].end == 0;
821 if ((!nonpos && isnonpos)
822 || (!isnonpos && visOnly && !visible
823 .containsKey(next[j].type)))
825 // skip if feature is nonpos and we ignore them or if we only
826 // output visible and it isn't non-pos and it's not visible
831 && (next[j].featureGroup == null || !next[j].featureGroup
837 if (group == null && next[j].featureGroup != null)
841 // we have features to output
843 if (next[j].description == null
844 || next[j].description.equals(""))
846 out.append(next[j].type + "\t");
850 if (next[j].links != null
851 && next[j].getDescription().indexOf("<html>") == -1)
853 out.append("<html>");
856 out.append(next[j].description + " ");
857 if (next[j].links != null)
859 for (int l = 0; l < next[j].links.size(); l++)
861 String label = next[j].links.elementAt(l).toString();
862 String href = label.substring(label.indexOf("|") + 1);
863 label = label.substring(0, label.indexOf("|"));
865 if (next[j].description.indexOf(href) == -1)
867 out.append("<a href=\"" + href + "\">" + label + "</a>");
871 if (next[j].getDescription().indexOf("</html>") == -1)
873 out.append("</html>");
879 out.append(seqs[i].getName());
880 out.append("\t-1\t");
881 out.append(next[j].begin);
883 out.append(next[j].end);
885 out.append(next[j].type);
886 if (next[j].score != Float.NaN)
889 out.append(next[j].score);
898 out.append("ENDGROUP\t");
908 } while (groupIndex < groups.size() + 1);
912 return "No Features Visible";
915 return out.toString();
919 * generate a gff file for sequence features includes non-pos features by
926 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
928 return printGFFFormat(seqs, visible, true, true);
931 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
932 boolean visOnly, boolean nonpos)
934 StringBuffer out = new StringBuffer();
935 SequenceFeature[] next;
938 for (int i = 0; i < seqs.length; i++)
940 if (seqs[i].getSequenceFeatures() != null)
942 next = seqs[i].getSequenceFeatures();
943 for (int j = 0; j < next.length; j++)
945 isnonpos = next[j].begin == 0 && next[j].end == 0;
946 if ((!nonpos && isnonpos)
947 || (!isnonpos && visOnly && !visible
948 .containsKey(next[j].type)))
953 source = next[j].featureGroup;
956 source = next[j].getDescription();
959 out.append(seqs[i].getName());
963 out.append(next[j].type);
965 out.append(next[j].begin);
967 out.append(next[j].end);
969 out.append(next[j].score);
972 if (next[j].getValue("STRAND") != null)
974 out.append(next[j].getValue("STRAND"));
982 if (next[j].getValue("FRAME") != null)
984 out.append(next[j].getValue("FRAME"));
990 // TODO: verify/check GFF - should there be a /t here before attribute
993 if (next[j].getValue("ATTRIBUTES") != null)
995 out.append(next[j].getValue("ATTRIBUTES"));
1004 return out.toString();
1008 * this is only for the benefit of object polymorphism - method does nothing.
1016 * this is only for the benefit of object polymorphism - method does nothing.
1018 * @return error message
1020 public String print()
1022 return "USE printGFFFormat() or printJalviewFormat()";