2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import jalview.analysis.SequenceIdMatcher;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27 import jalview.util.Format;
30 * Parse and create Jalview Features files Detects GFF format features files and
31 * parses. Does not implement standard print() - call specific printFeatures or
32 * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object
33 * for the features annotation - this normally works on an exact match.
38 public class FeaturesFile extends AlignFile
41 * work around for GFF interpretation bug where source string becomes
42 * description rather than a group
44 private boolean doGffSource = true;
47 * Creates a new FeaturesFile object.
54 * Creates a new FeaturesFile object.
64 public FeaturesFile(String inFile, String type) throws IOException
69 public FeaturesFile(FileParse source) throws IOException
75 * Parse GFF or sequence features file using case-independent matching,
79 * - alignment/dataset containing sequences that are to be annotated
81 * - hashtable to store feature colour definitions
83 * - process html strings into plain text
84 * @return true if features were added
86 public boolean parse(AlignmentI align, Hashtable colours,
89 return parse(align, colours, null, removeHTML, false);
93 * Parse GFF or sequence features file optionally using case-independent
94 * matching, discarding URLs
97 * - alignment/dataset containing sequences that are to be annotated
99 * - hashtable to store feature colour definitions
101 * - process html strings into plain text
102 * @param relaxedIdmatching
103 * - when true, ID matches to compound sequence IDs are allowed
104 * @return true if features were added
106 public boolean parse(AlignmentI align, Map colours, boolean removeHTML,
107 boolean relaxedIdMatching)
109 return parse(align, colours, null, removeHTML, relaxedIdMatching);
113 * Parse GFF or sequence features file optionally using case-independent
117 * - alignment/dataset containing sequences that are to be annotated
119 * - hashtable to store feature colour definitions
121 * - hashtable to store associated URLs
123 * - process html strings into plain text
124 * @return true if features were added
126 public boolean parse(AlignmentI align, Map colours, Map featureLink,
129 return parse(align, colours, featureLink, removeHTML, false);
133 * /** Parse GFF or sequence features file
136 * - alignment/dataset containing sequences that are to be annotated
138 * - hashtable to store feature colour definitions
140 * - hashtable to store associated URLs
142 * - process html strings into plain text
143 * @param relaxedIdmatching
144 * - when true, ID matches to compound sequence IDs are allowed
145 * @return true if features were added
147 public boolean parse(AlignmentI align, Map colours, Map featureLink,
148 boolean removeHTML, boolean relaxedIdmatching)
154 SequenceI seq = null;
155 String type, desc, token = null;
157 int index, start, end;
161 String featureGroup = null, groupLink = null;
162 Map typeLink = new Hashtable();
164 * when true, assume GFF style features rather than Jalview style.
166 boolean GFFFile = true;
167 while ((line = nextLine()) != null)
169 if (line.startsWith("#"))
174 st = new StringTokenizer(line, "\t");
175 if (st.countTokens() == 1)
177 if (line.trim().equalsIgnoreCase("GFF"))
179 // Start parsing file as if it might be GFF again.
184 if (st.countTokens() > 1 && st.countTokens() < 4)
187 type = st.nextToken();
188 if (type.equalsIgnoreCase("startgroup"))
190 featureGroup = st.nextToken();
191 if (st.hasMoreElements())
193 groupLink = st.nextToken();
194 featureLink.put(featureGroup, groupLink);
197 else if (type.equalsIgnoreCase("endgroup"))
199 // We should check whether this is the current group,
200 // but at present theres no way of showing more than 1 group
207 Object colour = null;
208 String colscheme = st.nextToken();
209 if (colscheme.indexOf("|") > -1
210 || colscheme.trim().equalsIgnoreCase("label"))
212 // Parse '|' separated graduated colourscheme fields:
213 // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
214 // can either provide 'label' only, first is optional, next two
215 // colors are required (but may be
216 // left blank), next is optional, nxt two min/max are required.
217 // first is either 'label'
218 // first/second and third are both hexadecimal or word equivalent
220 // next two are values parsed as floats.
221 // fifth is either 'above','below', or 'none'.
222 // sixth is a float value and only required when fifth is either
223 // 'above' or 'below'.
224 StringTokenizer gcol = new StringTokenizer(colscheme, "|",
227 int threshtype = AnnotationColourGradient.NO_THRESHOLD;
228 float min = Float.MIN_VALUE, max = Float.MAX_VALUE, threshval = Float.NaN;
229 boolean labelCol = false;
231 String mincol = gcol.nextToken();
235 .println("Expected either 'label' or a colour specification in the line: "
239 String maxcol = null;
240 if (mincol.toLowerCase().indexOf("label") == 0)
243 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip
245 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
247 String abso = null, minval, maxval;
250 // at least four more tokens
251 if (mincol.equals("|"))
257 gcol.nextToken(); // skip next '|'
259 // continue parsing rest of line
260 maxcol = gcol.nextToken();
261 if (maxcol.equals("|"))
267 gcol.nextToken(); // skip next '|'
269 abso = gcol.nextToken();
270 gcol.nextToken(); // skip next '|'
271 if (abso.toLowerCase().indexOf("abso") != 0)
278 minval = gcol.nextToken();
279 gcol.nextToken(); // skip next '|'
281 maxval = gcol.nextToken();
282 if (gcol.hasMoreTokens())
284 gcol.nextToken(); // skip next '|'
288 if (minval.length() > 0)
290 min = new Float(minval).floatValue();
292 } catch (Exception e)
295 .println("Couldn't parse the minimum value for graduated colour for type ("
297 + ") - did you misspell 'auto' for the optional automatic colour switch ?");
302 if (maxval.length() > 0)
304 max = new Float(maxval).floatValue();
306 } catch (Exception e)
309 .println("Couldn't parse the maximum value for graduated colour for type ("
316 // add in some dummy min/max colours for the label-only
323 colour = new jalview.schemes.GraduatedColor(
324 new UserColourScheme(mincol).findColour('A'),
325 new UserColourScheme(maxcol).findColour('A'), min,
327 } catch (Exception e)
330 .println("Couldn't parse the graduated colour scheme ("
336 ((jalview.schemes.GraduatedColor) colour)
337 .setColourByLabel(labelCol);
338 ((jalview.schemes.GraduatedColor) colour)
339 .setAutoScaled(abso == null);
340 // add in any additional parameters
341 String ttype = null, tval = null;
342 if (gcol.hasMoreTokens())
344 // threshold type and possibly a threshold value
345 ttype = gcol.nextToken();
346 if (ttype.toLowerCase().startsWith("below"))
348 ((jalview.schemes.GraduatedColor) colour)
349 .setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
351 else if (ttype.toLowerCase().startsWith("above"))
353 ((jalview.schemes.GraduatedColor) colour)
354 .setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
358 ((jalview.schemes.GraduatedColor) colour)
359 .setThreshType(AnnotationColourGradient.NO_THRESHOLD);
360 if (!ttype.toLowerCase().startsWith("no"))
363 .println("Ignoring unrecognised threshold type : "
368 if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
373 tval = gcol.nextToken();
374 ((jalview.schemes.GraduatedColor) colour)
375 .setThresh(new Float(tval).floatValue());
376 } catch (Exception e)
379 .println("Couldn't parse threshold value as a float: ("
384 // parse the thresh-is-min token ?
385 if (gcol.hasMoreTokens())
388 .println("Ignoring additional tokens in parameters in graduated colour specification\n");
389 while (gcol.hasMoreTokens())
391 System.err.println("|" + gcol.nextToken());
393 System.err.println("\n");
399 UserColourScheme ucs = new UserColourScheme(colscheme);
400 colour = ucs.findColour('A');
404 colours.put(type, colour);
406 if (st.hasMoreElements())
408 String link = st.nextToken();
409 typeLink.put(type, link);
410 if (featureLink == null)
412 featureLink = new Hashtable();
414 featureLink.put(type, link);
420 while (st.hasMoreElements())
425 // Still possible this is an old Jalview file,
426 // which does not have type colours at the beginning
427 seqId = token = st.nextToken();
428 seq = findName(align, seqId, relaxedIdmatching);
431 desc = st.nextToken();
433 if (doGffSource && desc.indexOf(' ') == -1)
435 // could also be a source term rather than description line
436 group = new String(desc);
438 type = st.nextToken();
441 String stt = st.nextToken();
442 if (stt.length() == 0 || stt.equals("-"))
448 start = Integer.parseInt(stt);
450 } catch (NumberFormatException ex)
456 String stt = st.nextToken();
457 if (stt.length() == 0 || stt.equals("-"))
463 end = Integer.parseInt(stt);
465 } catch (NumberFormatException ex)
469 // TODO: decide if non positional feature assertion for input data
470 // where end==0 is generally valid
473 // treat as non-positional feature, regardless.
478 score = new Float(st.nextToken()).floatValue();
479 } catch (NumberFormatException ex)
484 sf = new SequenceFeature(type, desc, start, end, score, group);
488 sf.setValue("STRAND", st.nextToken());
489 sf.setValue("FRAME", st.nextToken());
490 } catch (Exception ex)
494 if (st.hasMoreTokens())
496 StringBuffer attributes = new StringBuffer();
497 while (st.hasMoreTokens())
499 attributes.append("\t" + st.nextElement());
501 // TODO validate and split GFF2 attributes field ? parse out
502 // ([A-Za-z][A-Za-z0-9_]*) <value> ; and add as
503 // sf.setValue(attrib, val);
504 sf.setValue("ATTRIBUTES", attributes.toString());
507 seq.addSequenceFeature(sf);
508 while ((seq = align.findName(seq, seqId, true)) != null)
510 seq.addSequenceFeature(new SequenceFeature(sf));
516 if (GFFFile && seq == null)
522 desc = st.nextToken();
524 if (!st.hasMoreTokens())
527 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
528 // in all probability, this isn't a file we understand, so bail
533 token = st.nextToken();
535 if (!token.equals("ID_NOT_SPECIFIED"))
537 seq = findName(align, seqId = token, relaxedIdmatching);
545 index = Integer.parseInt(st.nextToken());
546 seq = align.getSequenceAt(index);
547 } catch (NumberFormatException ex)
555 System.out.println("Sequence not found: " + line);
559 start = Integer.parseInt(st.nextToken());
560 end = Integer.parseInt(st.nextToken());
562 type = st.nextToken();
564 if (!colours.containsKey(type))
566 // Probably the old style groups file
567 UserColourScheme ucs = new UserColourScheme(type);
568 colours.put(type, ucs.findColour('A'));
570 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
571 if (st.hasMoreTokens())
575 score = new Float(st.nextToken()).floatValue();
576 // update colourgradient bounds if allowed to
577 } catch (NumberFormatException ex)
583 if (groupLink != null && removeHTML)
585 sf.addLink(groupLink);
586 sf.description += "%LINK%";
588 if (typeLink.containsKey(type) && removeHTML)
590 sf.addLink(typeLink.get(type).toString());
591 sf.description += "%LINK%";
594 parseDescriptionHTML(sf, removeHTML);
596 seq.addSequenceFeature(sf);
599 && (seq = align.findName(seq, seqId, false)) != null)
601 seq.addSequenceFeature(new SequenceFeature(sf));
603 // If we got here, its not a GFFFile
608 } catch (Exception ex)
610 System.out.println(line);
611 System.out.println("Error parsing feature file: " + ex + "\n" + line);
612 ex.printStackTrace(System.err);
620 private AlignmentI lastmatchedAl = null;
622 private SequenceIdMatcher matcher = null;
625 * clear any temporary handles used to speed up ID matching
627 private void resetMatcher()
629 lastmatchedAl = null;
633 private SequenceI findName(AlignmentI align, String seqId,
634 boolean relaxedIdMatching)
636 SequenceI match = null;
637 if (relaxedIdMatching)
639 if (lastmatchedAl != align)
641 matcher = new SequenceIdMatcher(
642 (lastmatchedAl = align).getSequencesArray());
644 match = matcher.findIdMatch(seqId);
648 match = align.findName(seqId, true);
653 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
655 if (sf.getDescription() == null)
659 jalview.util.ParseHtmlBodyAndLinks parsed = new jalview.util.ParseHtmlBodyAndLinks(
660 sf.getDescription(), removeHTML, newline);
662 sf.description = (removeHTML) ? parsed.getNonHtmlContent()
664 for (String link : parsed.getLinks())
672 * generate a features file for seqs includes non-pos features by default.
675 * source of sequence features
677 * hash of feature types and colours
678 * @return features file contents
680 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
682 return printJalviewFormat(seqs, visible, true, true);
686 * generate a features file for seqs with colours from visible (if any)
691 * hash of Colours for each feature type
693 * when true only feature types in 'visible' will be output
695 * indicates if non-positional features should be output (regardless
697 * @return features file contents
699 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
700 boolean visOnly, boolean nonpos)
702 StringBuffer out = new StringBuffer();
703 SequenceFeature[] next;
704 boolean featuresGen = false;
705 if (visOnly && !nonpos && (visible == null || visible.size() < 1))
707 // no point continuing.
708 return "No Features Visible";
711 if (visible != null && visOnly)
713 // write feature colours only if we're given them and we are generating
715 // TODO: decide if feature links should also be written here ?
716 Enumeration en = visible.keys();
718 while (en.hasMoreElements())
720 type = en.nextElement().toString();
722 if (visible.get(type) instanceof GraduatedColor)
724 GraduatedColor gc = (GraduatedColor) visible.get(type);
725 color = (gc.isColourByLabel() ? "label|" : "")
726 + Format.getHexString(gc.getMinColor()) + "|"
727 + Format.getHexString(gc.getMaxColor())
728 + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
730 if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
732 if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
738 if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
740 System.err.println("WARNING: Unsupported threshold type ("
741 + gc.getThreshType() + ") : Assuming 'above'");
746 color += "|" + gc.getThresh();
753 else if (visible.get(type) instanceof java.awt.Color)
755 color = Format.getHexString((java.awt.Color) visible.get(type));
759 // legacy support for integer objects containing colour triplet values
760 color = Format.getHexString(new java.awt.Color(Integer
761 .parseInt(visible.get(type).toString())));
769 // Work out which groups are both present and visible
770 Vector groups = new Vector();
772 boolean isnonpos = false;
774 for (int i = 0; i < seqs.length; i++)
776 next = seqs[i].getSequenceFeatures();
779 for (int j = 0; j < next.length; j++)
781 isnonpos = next[j].begin == 0 && next[j].end == 0;
782 if ((!nonpos && isnonpos)
783 || (!isnonpos && visOnly && !visible
784 .containsKey(next[j].type)))
789 if (next[j].featureGroup != null
790 && !groups.contains(next[j].featureGroup))
792 groups.addElement(next[j].featureGroup);
802 if (groups.size() > 0 && groupIndex < groups.size())
804 group = groups.elementAt(groupIndex).toString();
806 out.append("STARTGROUP\t");
815 for (int i = 0; i < seqs.length; i++)
817 next = seqs[i].getSequenceFeatures();
820 for (int j = 0; j < next.length; j++)
822 isnonpos = next[j].begin == 0 && next[j].end == 0;
823 if ((!nonpos && isnonpos)
824 || (!isnonpos && visOnly && !visible
825 .containsKey(next[j].type)))
827 // skip if feature is nonpos and we ignore them or if we only
828 // output visible and it isn't non-pos and it's not visible
833 && (next[j].featureGroup == null || !next[j].featureGroup
839 if (group == null && next[j].featureGroup != null)
843 // we have features to output
845 if (next[j].description == null
846 || next[j].description.equals(""))
848 out.append(next[j].type + "\t");
852 if (next[j].links != null
853 && next[j].getDescription().indexOf("<html>") == -1)
855 out.append("<html>");
858 out.append(next[j].description + " ");
859 if (next[j].links != null)
861 for (int l = 0; l < next[j].links.size(); l++)
863 String label = next[j].links.elementAt(l).toString();
864 String href = label.substring(label.indexOf("|") + 1);
865 label = label.substring(0, label.indexOf("|"));
867 if (next[j].description.indexOf(href) == -1)
869 out.append("<a href=\"" + href + "\">" + label + "</a>");
873 if (next[j].getDescription().indexOf("</html>") == -1)
875 out.append("</html>");
881 out.append(seqs[i].getName());
882 out.append("\t-1\t");
883 out.append(next[j].begin);
885 out.append(next[j].end);
887 out.append(next[j].type);
888 if (next[j].score != Float.NaN)
891 out.append(next[j].score);
900 out.append("ENDGROUP\t");
910 } while (groupIndex < groups.size() + 1);
914 return "No Features Visible";
917 return out.toString();
921 * generate a gff file for sequence features includes non-pos features by
928 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
930 return printGFFFormat(seqs, visible, true, true);
933 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
934 boolean visOnly, boolean nonpos)
936 StringBuffer out = new StringBuffer();
937 SequenceFeature[] next;
940 for (int i = 0; i < seqs.length; i++)
942 if (seqs[i].getSequenceFeatures() != null)
944 next = seqs[i].getSequenceFeatures();
945 for (int j = 0; j < next.length; j++)
947 isnonpos = next[j].begin == 0 && next[j].end == 0;
948 if ((!nonpos && isnonpos)
949 || (!isnonpos && visOnly && !visible
950 .containsKey(next[j].type)))
955 source = next[j].featureGroup;
958 source = next[j].getDescription();
961 out.append(seqs[i].getName());
965 out.append(next[j].type);
967 out.append(next[j].begin);
969 out.append(next[j].end);
971 out.append(next[j].score);
974 if (next[j].getValue("STRAND") != null)
976 out.append(next[j].getValue("STRAND"));
984 if (next[j].getValue("FRAME") != null)
986 out.append(next[j].getValue("FRAME"));
992 // TODO: verify/check GFF - should there be a /t here before attribute
995 if (next[j].getValue("ATTRIBUTES") != null)
997 out.append(next[j].getValue("ATTRIBUTES"));
1000 out.append(newline);
1006 return out.toString();
1010 * this is only for the benefit of object polymorphism - method does nothing.
1018 * this is only for the benefit of object polymorphism - method does nothing.
1020 * @return error message
1022 public String print()
1024 return "USE printGFFFormat() or printJalviewFormat()";