2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 import jalview.analysis.SequenceIdMatcher;
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29 import jalview.util.Format;
32 * Parse and create Jalview Features files Detects GFF format features files and
33 * parses. Does not implement standard print() - call specific printFeatures or
34 * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object
35 * for the features annotation - this normally works on an exact match.
40 public class FeaturesFile extends AlignFile
43 * work around for GFF interpretation bug where source string becomes
44 * description rather than a group
46 private boolean doGffSource = true;
49 * Creates a new FeaturesFile object.
56 * Creates a new FeaturesFile object.
66 public FeaturesFile(String inFile, String type) throws IOException
71 public FeaturesFile(FileParse source) throws IOException
77 * Parse GFF or sequence features file using case-independent matching,
81 * - alignment/dataset containing sequences that are to be annotated
83 * - hashtable to store feature colour definitions
85 * - process html strings into plain text
86 * @return true if features were added
88 public boolean parse(AlignmentI align, Hashtable colours,
91 return parse(align, colours, null, removeHTML, false);
95 * Parse GFF or sequence features file optionally using case-independent
96 * matching, discarding URLs
99 * - alignment/dataset containing sequences that are to be annotated
101 * - hashtable to store feature colour definitions
103 * - process html strings into plain text
104 * @param relaxedIdmatching
105 * - when true, ID matches to compound sequence IDs are allowed
106 * @return true if features were added
108 public boolean parse(AlignmentI align, Map colours, boolean removeHTML,
109 boolean relaxedIdMatching)
111 return parse(align, colours, null, removeHTML, relaxedIdMatching);
115 * Parse GFF or sequence features file optionally using case-independent
119 * - alignment/dataset containing sequences that are to be annotated
121 * - hashtable to store feature colour definitions
123 * - hashtable to store associated URLs
125 * - process html strings into plain text
126 * @return true if features were added
128 public boolean parse(AlignmentI align, Map colours, Map featureLink,
131 return parse(align, colours, featureLink, removeHTML, false);
135 * Parse GFF or sequence features file
138 * - alignment/dataset containing sequences that are to be annotated
140 * - hashtable to store feature colour definitions
142 * - hashtable to store associated URLs
144 * - process html strings into plain text
145 * @param relaxedIdmatching
146 * - when true, ID matches to compound sequence IDs are allowed
147 * @return true if features were added
149 public boolean parse(AlignmentI align, Map colours, Map featureLink,
150 boolean removeHTML, boolean relaxedIdmatching)
156 SequenceI seq = null;
157 String type, desc, token = null;
159 int index, start, end;
163 String featureGroup = null, groupLink = null;
164 Map typeLink = new Hashtable();
166 * when true, assume GFF style features rather than Jalview style.
168 boolean GFFFile = true;
169 while ((line = nextLine()) != null)
171 if (line.startsWith("#"))
176 st = new StringTokenizer(line, "\t");
177 if (st.countTokens() == 1)
179 if (line.trim().equalsIgnoreCase("GFF"))
181 // Start parsing file as if it might be GFF again.
186 if (st.countTokens() > 1 && st.countTokens() < 4)
189 type = st.nextToken();
190 if (type.equalsIgnoreCase("startgroup"))
192 featureGroup = st.nextToken();
193 if (st.hasMoreElements())
195 groupLink = st.nextToken();
196 featureLink.put(featureGroup, groupLink);
199 else if (type.equalsIgnoreCase("endgroup"))
201 // We should check whether this is the current group,
202 // but at present theres no way of showing more than 1 group
209 Object colour = null;
210 String colscheme = st.nextToken();
211 if (colscheme.indexOf("|") > -1
212 || colscheme.trim().equalsIgnoreCase("label"))
214 // Parse '|' separated graduated colourscheme fields:
215 // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
216 // can either provide 'label' only, first is optional, next two
217 // colors are required (but may be
218 // left blank), next is optional, nxt two min/max are required.
219 // first is either 'label'
220 // first/second and third are both hexadecimal or word equivalent
222 // next two are values parsed as floats.
223 // fifth is either 'above','below', or 'none'.
224 // sixth is a float value and only required when fifth is either
225 // 'above' or 'below'.
226 StringTokenizer gcol = new StringTokenizer(colscheme, "|",
229 int threshtype = AnnotationColourGradient.NO_THRESHOLD;
230 float min = Float.MIN_VALUE, max = Float.MAX_VALUE, threshval = Float.NaN;
231 boolean labelCol = false;
233 String mincol = gcol.nextToken();
237 .println("Expected either 'label' or a colour specification in the line: "
241 String maxcol = null;
242 if (mincol.toLowerCase().indexOf("label") == 0)
245 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip
247 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
249 String abso = null, minval, maxval;
252 // at least four more tokens
253 if (mincol.equals("|"))
259 gcol.nextToken(); // skip next '|'
261 // continue parsing rest of line
262 maxcol = gcol.nextToken();
263 if (maxcol.equals("|"))
269 gcol.nextToken(); // skip next '|'
271 abso = gcol.nextToken();
272 gcol.nextToken(); // skip next '|'
273 if (abso.toLowerCase().indexOf("abso") != 0)
280 minval = gcol.nextToken();
281 gcol.nextToken(); // skip next '|'
283 maxval = gcol.nextToken();
284 if (gcol.hasMoreTokens())
286 gcol.nextToken(); // skip next '|'
290 if (minval.length() > 0)
292 min = new Float(minval).floatValue();
294 } catch (Exception e)
297 .println("Couldn't parse the minimum value for graduated colour for type ("
299 + ") - did you misspell 'auto' for the optional automatic colour switch ?");
304 if (maxval.length() > 0)
306 max = new Float(maxval).floatValue();
308 } catch (Exception e)
311 .println("Couldn't parse the maximum value for graduated colour for type ("
318 // add in some dummy min/max colours for the label-only
325 colour = new jalview.schemes.GraduatedColor(
326 new UserColourScheme(mincol).findColour('A'),
327 new UserColourScheme(maxcol).findColour('A'), min,
329 } catch (Exception e)
332 .println("Couldn't parse the graduated colour scheme ("
338 ((jalview.schemes.GraduatedColor) colour)
339 .setColourByLabel(labelCol);
340 ((jalview.schemes.GraduatedColor) colour)
341 .setAutoScaled(abso == null);
342 // add in any additional parameters
343 String ttype = null, tval = null;
344 if (gcol.hasMoreTokens())
346 // threshold type and possibly a threshold value
347 ttype = gcol.nextToken();
348 if (ttype.toLowerCase().startsWith("below"))
350 ((jalview.schemes.GraduatedColor) colour)
351 .setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
353 else if (ttype.toLowerCase().startsWith("above"))
355 ((jalview.schemes.GraduatedColor) colour)
356 .setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
360 ((jalview.schemes.GraduatedColor) colour)
361 .setThreshType(AnnotationColourGradient.NO_THRESHOLD);
362 if (!ttype.toLowerCase().startsWith("no"))
365 .println("Ignoring unrecognised threshold type : "
370 if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
375 tval = gcol.nextToken();
376 ((jalview.schemes.GraduatedColor) colour)
377 .setThresh(new Float(tval).floatValue());
378 } catch (Exception e)
381 .println("Couldn't parse threshold value as a float: ("
386 // parse the thresh-is-min token ?
387 if (gcol.hasMoreTokens())
390 .println("Ignoring additional tokens in parameters in graduated colour specification\n");
391 while (gcol.hasMoreTokens())
393 System.err.println("|" + gcol.nextToken());
395 System.err.println("\n");
401 UserColourScheme ucs = new UserColourScheme(colscheme);
402 colour = ucs.findColour('A');
406 colours.put(type, colour);
408 if (st.hasMoreElements())
410 String link = st.nextToken();
411 typeLink.put(type, link);
412 if (featureLink == null)
414 featureLink = new Hashtable();
416 featureLink.put(type, link);
422 while (st.hasMoreElements())
427 // Still possible this is an old Jalview file,
428 // which does not have type colours at the beginning
429 seqId = token = st.nextToken();
430 seq = findName(align, seqId, relaxedIdmatching);
433 desc = st.nextToken();
435 if (doGffSource && desc.indexOf(' ') == -1)
437 // could also be a source term rather than description line
438 group = new String(desc);
440 type = st.nextToken();
443 String stt = st.nextToken();
444 if (stt.length() == 0 || stt.equals("-"))
450 start = Integer.parseInt(stt);
452 } catch (NumberFormatException ex)
458 String stt = st.nextToken();
459 if (stt.length() == 0 || stt.equals("-"))
465 end = Integer.parseInt(stt);
467 } catch (NumberFormatException ex)
471 // TODO: decide if non positional feature assertion for input data
472 // where end==0 is generally valid
475 // treat as non-positional feature, regardless.
480 score = new Float(st.nextToken()).floatValue();
481 } catch (NumberFormatException ex)
486 sf = new SequenceFeature(type, desc, start, end, score, group);
490 sf.setValue("STRAND", st.nextToken());
491 sf.setValue("FRAME", st.nextToken());
492 } catch (Exception ex)
496 if (st.hasMoreTokens())
498 StringBuffer attributes = new StringBuffer();
499 while (st.hasMoreTokens())
501 attributes.append("\t" + st.nextElement());
503 // TODO validate and split GFF2 attributes field ? parse out
504 // ([A-Za-z][A-Za-z0-9_]*) <value> ; and add as
505 // sf.setValue(attrib, val);
506 sf.setValue("ATTRIBUTES", attributes.toString());
509 seq.addSequenceFeature(sf);
510 while ((seq = align.findName(seq, seqId, true)) != null)
512 seq.addSequenceFeature(new SequenceFeature(sf));
518 if (GFFFile && seq == null)
524 desc = st.nextToken();
526 if (!st.hasMoreTokens())
529 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
530 // in all probability, this isn't a file we understand, so bail
535 token = st.nextToken();
537 if (!token.equals("ID_NOT_SPECIFIED"))
539 seq = findName(align, seqId = token, relaxedIdmatching);
547 index = Integer.parseInt(st.nextToken());
548 seq = align.getSequenceAt(index);
549 } catch (NumberFormatException ex)
557 System.out.println("Sequence not found: " + line);
561 start = Integer.parseInt(st.nextToken());
562 end = Integer.parseInt(st.nextToken());
564 type = st.nextToken();
566 if (!colours.containsKey(type))
568 // Probably the old style groups file
569 UserColourScheme ucs = new UserColourScheme(type);
570 colours.put(type, ucs.findColour('A'));
572 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
573 if (st.hasMoreTokens())
577 score = new Float(st.nextToken()).floatValue();
578 // update colourgradient bounds if allowed to
579 } catch (NumberFormatException ex)
585 if (groupLink != null && removeHTML)
587 sf.addLink(groupLink);
588 sf.description += "%LINK%";
590 if (typeLink.containsKey(type) && removeHTML)
592 sf.addLink(typeLink.get(type).toString());
593 sf.description += "%LINK%";
596 parseDescriptionHTML(sf, removeHTML);
598 seq.addSequenceFeature(sf);
601 && (seq = align.findName(seq, seqId, false)) != null)
603 seq.addSequenceFeature(new SequenceFeature(sf));
605 // If we got here, its not a GFFFile
610 } catch (Exception ex)
612 System.out.println("Error parsing feature file: " + ex + "\n" + line);
613 ex.printStackTrace(System.err);
621 private AlignmentI lastmatchedAl = null;
623 private SequenceIdMatcher matcher = null;
626 * clear any temporary handles used to speed up ID matching
628 private void resetMatcher()
630 lastmatchedAl = null;
634 private SequenceI findName(AlignmentI align, String seqId,
635 boolean relaxedIdMatching)
637 SequenceI match = null;
638 if (relaxedIdMatching)
640 if (lastmatchedAl != align)
642 matcher = new SequenceIdMatcher(
643 (lastmatchedAl = align).getSequencesArray());
645 match = matcher.findIdMatch(seqId);
649 match = align.findName(seqId, true);
654 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
656 if (sf.getDescription() == null)
660 jalview.util.ParseHtmlBodyAndLinks parsed = new jalview.util.ParseHtmlBodyAndLinks(
661 sf.getDescription(), removeHTML, newline);
663 sf.description = (removeHTML) ? parsed.getNonHtmlContent()
665 for (String link : parsed.getLinks())
673 * generate a features file for seqs includes non-pos features by default.
676 * source of sequence features
678 * hash of feature types and colours
679 * @return features file contents
681 public String printJalviewFormat(SequenceI[] seqs, Map<String,Object> visible)
683 return printJalviewFormat(seqs, visible, true, true);
687 * generate a features file for seqs with colours from visible (if any)
692 * hash of Colours for each feature type
694 * when true only feature types in 'visible' will be output
696 * indicates if non-positional features should be output (regardless
698 * @return features file contents
700 public String printJalviewFormat(SequenceI[] seqs, Map visible,
701 boolean visOnly, boolean nonpos)
703 StringBuffer out = new StringBuffer();
704 SequenceFeature[] next;
705 boolean featuresGen = false;
706 if (visOnly && !nonpos && (visible == null || visible.size() < 1))
708 // no point continuing.
709 return "No Features Visible";
712 if (visible != null && visOnly)
714 // write feature colours only if we're given them and we are generating
716 // TODO: decide if feature links should also be written here ?
717 Iterator en = visible.keySet().iterator();
721 type = en.next().toString();
723 if (visible.get(type) instanceof GraduatedColor)
725 GraduatedColor gc = (GraduatedColor) visible.get(type);
726 color = (gc.isColourByLabel() ? "label|" : "")
727 + Format.getHexString(gc.getMinColor()) + "|"
728 + Format.getHexString(gc.getMaxColor())
729 + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
731 if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
733 if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
739 if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
741 System.err.println("WARNING: Unsupported threshold type ("
742 + gc.getThreshType() + ") : Assuming 'above'");
747 color += "|" + gc.getThresh();
754 else if (visible.get(type) instanceof java.awt.Color)
756 color = Format.getHexString((java.awt.Color) visible.get(type));
760 // legacy support for integer objects containing colour triplet values
761 color = Format.getHexString(new java.awt.Color(Integer
762 .parseInt(visible.get(type).toString())));
770 // Work out which groups are both present and visible
771 Vector groups = new Vector();
773 boolean isnonpos = false;
775 for (int i = 0; i < seqs.length; i++)
777 next = seqs[i].getSequenceFeatures();
780 for (int j = 0; j < next.length; j++)
782 isnonpos = next[j].begin == 0 && next[j].end == 0;
783 if ((!nonpos && isnonpos)
784 || (!isnonpos && visOnly && !visible
785 .containsKey(next[j].type)))
790 if (next[j].featureGroup != null
791 && !groups.contains(next[j].featureGroup))
793 groups.addElement(next[j].featureGroup);
803 if (groups.size() > 0 && groupIndex < groups.size())
805 group = groups.elementAt(groupIndex).toString();
807 out.append("STARTGROUP\t");
816 for (int i = 0; i < seqs.length; i++)
818 next = seqs[i].getSequenceFeatures();
821 for (int j = 0; j < next.length; j++)
823 isnonpos = next[j].begin == 0 && next[j].end == 0;
824 if ((!nonpos && isnonpos)
825 || (!isnonpos && visOnly && !visible
826 .containsKey(next[j].type)))
828 // skip if feature is nonpos and we ignore them or if we only
829 // output visible and it isn't non-pos and it's not visible
834 && (next[j].featureGroup == null || !next[j].featureGroup
840 if (group == null && next[j].featureGroup != null)
844 // we have features to output
846 if (next[j].description == null
847 || next[j].description.equals(""))
849 out.append(next[j].type + "\t");
853 if (next[j].links != null
854 && next[j].getDescription().indexOf("<html>") == -1)
856 out.append("<html>");
859 out.append(next[j].description + " ");
860 if (next[j].links != null)
862 for (int l = 0; l < next[j].links.size(); l++)
864 String label = next[j].links.elementAt(l).toString();
865 String href = label.substring(label.indexOf("|") + 1);
866 label = label.substring(0, label.indexOf("|"));
868 if (next[j].description.indexOf(href) == -1)
870 out.append("<a href=\"" + href + "\">" + label + "</a>");
874 if (next[j].getDescription().indexOf("</html>") == -1)
876 out.append("</html>");
882 out.append(seqs[i].getName());
883 out.append("\t-1\t");
884 out.append(next[j].begin);
886 out.append(next[j].end);
888 out.append(next[j].type);
889 if (next[j].score != Float.NaN)
892 out.append(next[j].score);
901 out.append("ENDGROUP\t");
911 } while (groupIndex < groups.size() + 1);
915 return "No Features Visible";
918 return out.toString();
922 * generate a gff file for sequence features includes non-pos features by
929 public String printGFFFormat(SequenceI[] seqs, Map<String,Object> visible)
931 return printGFFFormat(seqs, visible, true, true);
934 public String printGFFFormat(SequenceI[] seqs, Map<String,Object> visible,
935 boolean visOnly, boolean nonpos)
937 StringBuffer out = new StringBuffer();
938 SequenceFeature[] next;
941 for (int i = 0; i < seqs.length; i++)
943 if (seqs[i].getSequenceFeatures() != null)
945 next = seqs[i].getSequenceFeatures();
946 for (int j = 0; j < next.length; j++)
948 isnonpos = next[j].begin == 0 && next[j].end == 0;
949 if ((!nonpos && isnonpos)
950 || (!isnonpos && visOnly && !visible
951 .containsKey(next[j].type)))
956 source = next[j].featureGroup;
959 source = next[j].getDescription();
962 out.append(seqs[i].getName());
966 out.append(next[j].type);
968 out.append(next[j].begin);
970 out.append(next[j].end);
972 out.append(next[j].score);
975 if (next[j].getValue("STRAND") != null)
977 out.append(next[j].getValue("STRAND"));
985 if (next[j].getValue("FRAME") != null)
987 out.append(next[j].getValue("FRAME"));
993 // TODO: verify/check GFF - should there be a /t here before attribute
996 if (next[j].getValue("ATTRIBUTES") != null)
998 out.append(next[j].getValue("ATTRIBUTES"));
1001 out.append(newline);
1007 return out.toString();
1011 * this is only for the benefit of object polymorphism - method does nothing.
1019 * this is only for the benefit of object polymorphism - method does nothing.
1021 * @return error message
1023 public String print()
1025 return "USE printGFFFormat() or printJalviewFormat()";