2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
\r
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
23 import jalview.analysis.SequenceIdMatcher;
\r
24 import jalview.datamodel.*;
\r
25 import jalview.schemes.*;
\r
26 import jalview.util.Format;
\r
29 * Parse and create Jalview Features files Detects GFF format features files and
\r
30 * parses. Does not implement standard print() - call specific printFeatures or
\r
31 * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object
\r
32 * for the features annotation - this normally works on an exact match.
\r
35 * @version $Revision$
\r
37 public class FeaturesFile extends AlignFile
\r
40 * work around for GFF interpretation bug where source string becomes
\r
41 * description rather than a group
\r
43 private boolean doGffSource = true;
\r
46 * Creates a new FeaturesFile object.
\r
48 public FeaturesFile()
\r
53 * Creates a new FeaturesFile object.
\r
60 * @throws IOException
\r
63 public FeaturesFile(String inFile, String type) throws IOException
\r
65 super(inFile, type);
\r
68 public FeaturesFile(FileParse source) throws IOException
\r
74 * Parse GFF or sequence features file using case-independent matching, discarding URLs
\r
75 * @param align - alignment/dataset containing sequences that are to be annotated
\r
76 * @param colours - hashtable to store feature colour definitions
\r
77 * @param removeHTML - process html strings into plain text
\r
78 * @return true if features were added
\r
80 public boolean parse(AlignmentI align, Hashtable colours,
\r
83 return parse(align, colours, null, removeHTML, false);
\r
87 * Parse GFF or sequence features file optionally using case-independent matching, discarding URLs
\r
88 * @param align - alignment/dataset containing sequences that are to be annotated
\r
89 * @param colours - hashtable to store feature colour definitions
\r
90 * @param removeHTML - process html strings into plain text
\r
91 * @param relaxedIdmatching - when true, ID matches to compound sequence IDs are allowed
\r
92 * @return true if features were added
\r
94 public boolean parse(AlignmentI align,
\r
95 Map colours, boolean removeHTML, boolean relaxedIdMatching)
\r
97 return parse(align, colours, null, removeHTML, relaxedIdMatching);
\r
101 * Parse GFF or sequence features file optionally using case-independent matching
\r
102 * @param align - alignment/dataset containing sequences that are to be annotated
\r
103 * @param colours - hashtable to store feature colour definitions
\r
104 * @param featureLink - hashtable to store associated URLs
\r
105 * @param removeHTML - process html strings into plain text
\r
106 * @return true if features were added
\r
108 public boolean parse(AlignmentI align, Map colours,
\r
109 Map featureLink, boolean removeHTML)
\r
111 return parse(align, colours, featureLink, removeHTML, false);
\r
116 * Parse GFF or sequence features file
\r
117 * @param align - alignment/dataset containing sequences that are to be annotated
\r
118 * @param colours - hashtable to store feature colour definitions
\r
119 * @param featureLink - hashtable to store associated URLs
\r
120 * @param removeHTML - process html strings into plain text
\r
121 * @param relaxedIdmatching - when true, ID matches to compound sequence IDs are allowed
\r
122 * @return true if features were added
\r
124 public boolean parse(AlignmentI align,
\r
125 Map colours, Map featureLink, boolean removeHTML, boolean relaxedIdmatching)
\r
128 String line = null;
\r
131 SequenceI seq = null;
\r
132 String type, desc, token = null;
\r
134 int index, start, end;
\r
136 StringTokenizer st;
\r
137 SequenceFeature sf;
\r
138 String featureGroup = null, groupLink = null;
\r
139 Map typeLink = new Hashtable();
\r
141 * when true, assume GFF style features rather than Jalview style.
\r
143 boolean GFFFile = true;
\r
144 while ((line = nextLine()) != null)
\r
146 if (line.startsWith("#"))
\r
151 st = new StringTokenizer(line, "\t");
\r
152 if (st.countTokens() == 1)
\r
154 if (line.trim().equalsIgnoreCase("GFF"))
\r
156 // Start parsing file as if it might be GFF again.
\r
161 if (st.countTokens() > 1 && st.countTokens() < 4)
\r
164 type = st.nextToken();
\r
165 if (type.equalsIgnoreCase("startgroup"))
\r
167 featureGroup = st.nextToken();
\r
168 if (st.hasMoreElements())
\r
170 groupLink = st.nextToken();
\r
171 featureLink.put(featureGroup, groupLink);
\r
174 else if (type.equalsIgnoreCase("endgroup"))
\r
176 // We should check whether this is the current group,
\r
177 // but at present theres no way of showing more than 1 group
\r
179 featureGroup = null;
\r
184 Object colour = null;
\r
185 String colscheme = st.nextToken();
\r
186 if (colscheme.indexOf("|") > -1
\r
187 || colscheme.trim().equalsIgnoreCase("label"))
\r
189 // Parse '|' separated graduated colourscheme fields:
\r
190 // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
\r
191 // can either provide 'label' only, first is optional, next two
\r
192 // colors are required (but may be
\r
193 // left blank), next is optional, nxt two min/max are required.
\r
194 // first is either 'label'
\r
195 // first/second and third are both hexadecimal or word equivalent
\r
197 // next two are values parsed as floats.
\r
198 // fifth is either 'above','below', or 'none'.
\r
199 // sixth is a float value and only required when fifth is either
\r
200 // 'above' or 'below'.
\r
201 StringTokenizer gcol = new StringTokenizer(colscheme, "|",
\r
204 int threshtype = AnnotationColourGradient.NO_THRESHOLD;
\r
205 float min = Float.MIN_VALUE, max = Float.MAX_VALUE, threshval = Float.NaN;
\r
206 boolean labelCol = false;
\r
208 String mincol = gcol.nextToken();
\r
212 .println("Expected either 'label' or a colour specification in the line: "
\r
216 String maxcol = null;
\r
217 if (mincol.toLowerCase().indexOf("label") == 0)
\r
220 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip
\r
222 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
\r
224 String abso = null, minval, maxval;
\r
225 if (mincol != null)
\r
227 // at least four more tokens
\r
228 if (mincol.equals("|"))
\r
234 gcol.nextToken(); // skip next '|'
\r
236 // continue parsing rest of line
\r
237 maxcol = gcol.nextToken();
\r
238 if (maxcol.equals("|"))
\r
244 gcol.nextToken(); // skip next '|'
\r
246 abso = gcol.nextToken();
\r
247 gcol.nextToken(); // skip next '|'
\r
248 if (abso.toLowerCase().indexOf("abso") != 0)
\r
255 minval = gcol.nextToken();
\r
256 gcol.nextToken(); // skip next '|'
\r
258 maxval = gcol.nextToken();
\r
259 if (gcol.hasMoreTokens())
\r
261 gcol.nextToken(); // skip next '|'
\r
265 if (minval.length() > 0)
\r
267 min = new Float(minval).floatValue();
\r
269 } catch (Exception e)
\r
272 .println("Couldn't parse the minimum value for graduated colour for type ("
\r
274 + ") - did you misspell 'auto' for the optional automatic colour switch ?");
\r
275 e.printStackTrace();
\r
279 if (maxval.length() > 0)
\r
281 max = new Float(maxval).floatValue();
\r
283 } catch (Exception e)
\r
286 .println("Couldn't parse the maximum value for graduated colour for type ("
\r
287 + colscheme + ")");
\r
288 e.printStackTrace();
\r
293 // add in some dummy min/max colours for the label-only
\r
300 colour = new jalview.schemes.GraduatedColor(
\r
301 new UserColourScheme(mincol).findColour('A'),
\r
302 new UserColourScheme(maxcol).findColour('A'), min,
\r
304 } catch (Exception e)
\r
307 .println("Couldn't parse the graduated colour scheme ("
\r
308 + colscheme + ")");
\r
309 e.printStackTrace();
\r
311 if (colour != null)
\r
313 ((jalview.schemes.GraduatedColor) colour)
\r
314 .setColourByLabel(labelCol);
\r
315 ((jalview.schemes.GraduatedColor) colour)
\r
316 .setAutoScaled(abso == null);
\r
317 // add in any additional parameters
\r
318 String ttype = null, tval = null;
\r
319 if (gcol.hasMoreTokens())
\r
321 // threshold type and possibly a threshold value
\r
322 ttype = gcol.nextToken();
\r
323 if (ttype.toLowerCase().startsWith("below"))
\r
325 ((jalview.schemes.GraduatedColor) colour)
\r
326 .setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
\r
328 else if (ttype.toLowerCase().startsWith("above"))
\r
330 ((jalview.schemes.GraduatedColor) colour)
\r
331 .setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
\r
335 ((jalview.schemes.GraduatedColor) colour)
\r
336 .setThreshType(AnnotationColourGradient.NO_THRESHOLD);
\r
337 if (!ttype.toLowerCase().startsWith("no"))
\r
340 .println("Ignoring unrecognised threshold type : "
\r
345 if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
\r
350 tval = gcol.nextToken();
\r
351 ((jalview.schemes.GraduatedColor) colour)
\r
352 .setThresh(new Float(tval).floatValue());
\r
353 } catch (Exception e)
\r
356 .println("Couldn't parse threshold value as a float: ("
\r
358 e.printStackTrace();
\r
361 // parse the thresh-is-min token ?
\r
362 if (gcol.hasMoreTokens())
\r
365 .println("Ignoring additional tokens in parameters in graduated colour specification\n");
\r
366 while (gcol.hasMoreTokens())
\r
368 System.err.println("|" + gcol.nextToken());
\r
370 System.err.println("\n");
\r
376 UserColourScheme ucs = new UserColourScheme(colscheme);
\r
377 colour = ucs.findColour('A');
\r
379 if (colour != null)
\r
381 colours.put(type, colour);
\r
383 if (st.hasMoreElements())
\r
385 String link = st.nextToken();
\r
386 typeLink.put(type, link);
\r
387 if (featureLink == null)
\r
389 featureLink = new Hashtable();
\r
391 featureLink.put(type, link);
\r
397 while (st.hasMoreElements())
\r
402 // Still possible this is an old Jalview file,
\r
403 // which does not have type colours at the beginning
\r
404 seqId = token = st.nextToken();
\r
405 seq = findName(align, seqId, relaxedIdmatching);
\r
408 desc = st.nextToken();
\r
409 String group = null;
\r
410 if (doGffSource && desc.indexOf(' ') == -1)
\r
412 // could also be a source term rather than description line
\r
413 group = new String(desc);
\r
415 type = st.nextToken();
\r
418 String stt = st.nextToken();
\r
419 if (stt.length() == 0 || stt.equals("-"))
\r
425 start = Integer.parseInt(stt);
\r
427 } catch (NumberFormatException ex)
\r
433 String stt = st.nextToken();
\r
434 if (stt.length() == 0 || stt.equals("-"))
\r
440 end = Integer.parseInt(stt);
\r
442 } catch (NumberFormatException ex)
\r
446 // TODO: decide if non positional feature assertion for input data
\r
447 // where end==0 is generally valid
\r
450 // treat as non-positional feature, regardless.
\r
455 score = new Float(st.nextToken()).floatValue();
\r
456 } catch (NumberFormatException ex)
\r
461 sf = new SequenceFeature(type, desc, start, end, score, group);
\r
465 sf.setValue("STRAND", st.nextToken());
\r
466 sf.setValue("FRAME", st.nextToken());
\r
467 } catch (Exception ex)
\r
471 if (st.hasMoreTokens())
\r
473 StringBuffer attributes = new StringBuffer();
\r
474 while (st.hasMoreTokens())
\r
476 attributes.append("\t" + st.nextElement());
\r
478 // TODO validate and split GFF2 attributes field ? parse out
\r
479 // ([A-Za-z][A-Za-z0-9_]*) <value> ; and add as
\r
480 // sf.setValue(attrib, val);
\r
481 sf.setValue("ATTRIBUTES", attributes.toString());
\r
484 seq.addSequenceFeature(sf);
\r
485 while ((seq = align.findName(seq, seqId, true)) != null)
\r
487 seq.addSequenceFeature(new SequenceFeature(sf));
\r
493 if (GFFFile && seq == null)
\r
499 desc = st.nextToken();
\r
501 if (!st.hasMoreTokens())
\r
504 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
\r
505 // in all probability, this isn't a file we understand, so bail
\r
510 token = st.nextToken();
\r
512 if (!token.equals("ID_NOT_SPECIFIED"))
\r
514 seq = findName(align, seqId = token, relaxedIdmatching);
\r
522 index = Integer.parseInt(st.nextToken());
\r
523 seq = align.getSequenceAt(index);
\r
524 } catch (NumberFormatException ex)
\r
532 System.out.println("Sequence not found: " + line);
\r
536 start = Integer.parseInt(st.nextToken());
\r
537 end = Integer.parseInt(st.nextToken());
\r
539 type = st.nextToken();
\r
541 if (!colours.containsKey(type))
\r
543 // Probably the old style groups file
\r
544 UserColourScheme ucs = new UserColourScheme(type);
\r
545 colours.put(type, ucs.findColour('A'));
\r
547 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
\r
548 if (st.hasMoreTokens())
\r
552 score = new Float(st.nextToken()).floatValue();
\r
553 // update colourgradient bounds if allowed to
\r
554 } catch (NumberFormatException ex)
\r
558 sf.setScore(score);
\r
560 if (groupLink != null && removeHTML)
\r
562 sf.addLink(groupLink);
\r
563 sf.description += "%LINK%";
\r
565 if (typeLink.containsKey(type) && removeHTML)
\r
567 sf.addLink(typeLink.get(type).toString());
\r
568 sf.description += "%LINK%";
\r
571 parseDescriptionHTML(sf, removeHTML);
\r
573 seq.addSequenceFeature(sf);
\r
575 while (seqId != null
\r
576 && (seq = align.findName(seq, seqId, false)) != null)
\r
578 seq.addSequenceFeature(new SequenceFeature(sf));
\r
580 // If we got here, its not a GFFFile
\r
585 } catch (Exception ex)
\r
587 System.out.println(line);
\r
588 System.out.println("Error parsing feature file: " + ex + "\n" + line);
\r
589 ex.printStackTrace(System.err);
\r
597 private AlignmentI lastmatchedAl = null;
\r
599 private SequenceIdMatcher matcher = null;
\r
602 * clear any temporary handles used to speed up ID matching
\r
604 private void resetMatcher()
\r
606 lastmatchedAl = null;
\r
610 private SequenceI findName(AlignmentI align, String seqId,
\r
611 boolean relaxedIdMatching)
\r
613 SequenceI match = null;
\r
614 if (relaxedIdMatching)
\r
616 if (lastmatchedAl != align)
\r
618 matcher = new SequenceIdMatcher(
\r
619 (lastmatchedAl = align).getSequencesArray());
\r
621 match = matcher.findIdMatch(seqId);
\r
625 match = align.findName(seqId, true);
\r
630 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
\r
632 if (sf.getDescription() == null)
\r
636 jalview.util.ParseHtmlBodyAndLinks parsed = new jalview.util.ParseHtmlBodyAndLinks(sf.getDescription(), removeHTML, newline);
\r
638 sf.description = (removeHTML) ? parsed.getNonHtmlContent() : sf.description;
\r
639 for (String link:parsed.getLinks())
\r
647 * generate a features file for seqs includes non-pos features by default.
\r
650 * source of sequence features
\r
652 * hash of feature types and colours
\r
653 * @return features file contents
\r
655 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible)
\r
657 return printJalviewFormat(seqs, visible, true, true);
\r
661 * generate a features file for seqs with colours from visible (if any)
\r
664 * source of features
\r
666 * hash of Colours for each feature type
\r
668 * when true only feature types in 'visible' will be output
\r
670 * indicates if non-positional features should be output (regardless
\r
671 * of group or type)
\r
672 * @return features file contents
\r
674 public String printJalviewFormat(SequenceI[] seqs, Hashtable visible,
\r
675 boolean visOnly, boolean nonpos)
\r
677 StringBuffer out = new StringBuffer();
\r
678 SequenceFeature[] next;
\r
679 boolean featuresGen = false;
\r
680 if (visOnly && !nonpos && (visible == null || visible.size() < 1))
\r
682 // no point continuing.
\r
683 return "No Features Visible";
\r
686 if (visible != null && visOnly)
\r
688 // write feature colours only if we're given them and we are generating
\r
690 // TODO: decide if feature links should also be written here ?
\r
691 Enumeration en = visible.keys();
\r
692 String type, color;
\r
693 while (en.hasMoreElements())
\r
695 type = en.nextElement().toString();
\r
697 if (visible.get(type) instanceof GraduatedColor)
\r
699 GraduatedColor gc = (GraduatedColor) visible.get(type);
\r
700 color = (gc.isColourByLabel() ? "label|" : "")
\r
701 + Format.getHexString(gc.getMinColor()) + "|"
\r
702 + Format.getHexString(gc.getMaxColor())
\r
703 + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
\r
704 + gc.getMax() + "|";
\r
705 if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
\r
707 if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
\r
713 if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
\r
715 System.err.println("WARNING: Unsupported threshold type ("
\r
716 + gc.getThreshType() + ") : Assuming 'above'");
\r
721 color += "|" + gc.getThresh();
\r
728 else if (visible.get(type) instanceof java.awt.Color)
\r
730 color = Format.getHexString((java.awt.Color) visible.get(type));
\r
734 // legacy support for integer objects containing colour triplet values
\r
735 color = Format.getHexString(new java.awt.Color(Integer
\r
736 .parseInt(visible.get(type).toString())));
\r
741 out.append(newline);
\r
744 // Work out which groups are both present and visible
\r
745 Vector groups = new Vector();
\r
746 int groupIndex = 0;
\r
747 boolean isnonpos = false;
\r
749 for (int i = 0; i < seqs.length; i++)
\r
751 next = seqs[i].getSequenceFeatures();
\r
754 for (int j = 0; j < next.length; j++)
\r
756 isnonpos = next[j].begin == 0 && next[j].end == 0;
\r
757 if ((!nonpos && isnonpos)
\r
758 || (!isnonpos && visOnly && !visible
\r
759 .containsKey(next[j].type)))
\r
764 if (next[j].featureGroup != null
\r
765 && !groups.contains(next[j].featureGroup))
\r
767 groups.addElement(next[j].featureGroup);
\r
773 String group = null;
\r
777 if (groups.size() > 0 && groupIndex < groups.size())
\r
779 group = groups.elementAt(groupIndex).toString();
\r
780 out.append(newline);
\r
781 out.append("STARTGROUP\t");
\r
783 out.append(newline);
\r
790 for (int i = 0; i < seqs.length; i++)
\r
792 next = seqs[i].getSequenceFeatures();
\r
795 for (int j = 0; j < next.length; j++)
\r
797 isnonpos = next[j].begin == 0 && next[j].end == 0;
\r
798 if ((!nonpos && isnonpos)
\r
799 || (!isnonpos && visOnly && !visible
\r
800 .containsKey(next[j].type)))
\r
802 // skip if feature is nonpos and we ignore them or if we only
\r
803 // output visible and it isn't non-pos and it's not visible
\r
808 && (next[j].featureGroup == null || !next[j].featureGroup
\r
814 if (group == null && next[j].featureGroup != null)
\r
818 // we have features to output
\r
819 featuresGen = true;
\r
820 if (next[j].description == null
\r
821 || next[j].description.equals(""))
\r
823 out.append(next[j].type + "\t");
\r
827 if (next[j].links != null
\r
828 && next[j].getDescription().indexOf("<html>") == -1)
\r
830 out.append("<html>");
\r
833 out.append(next[j].description + " ");
\r
834 if (next[j].links != null)
\r
836 for (int l = 0; l < next[j].links.size(); l++)
\r
838 String label = next[j].links.elementAt(l).toString();
\r
839 String href = label.substring(label.indexOf("|") + 1);
\r
840 label = label.substring(0, label.indexOf("|"));
\r
842 if (next[j].description.indexOf(href) == -1)
\r
844 out.append("<a href=\"" + href + "\">" + label + "</a>");
\r
848 if (next[j].getDescription().indexOf("</html>") == -1)
\r
850 out.append("</html>");
\r
856 out.append(seqs[i].getName());
\r
857 out.append("\t-1\t");
\r
858 out.append(next[j].begin);
\r
860 out.append(next[j].end);
\r
862 out.append(next[j].type);
\r
863 if (next[j].score != Float.NaN)
\r
866 out.append(next[j].score);
\r
868 out.append(newline);
\r
875 out.append("ENDGROUP\t");
\r
877 out.append(newline);
\r
885 } while (groupIndex < groups.size() + 1);
\r
889 return "No Features Visible";
\r
892 return out.toString();
\r
896 * generate a gff file for sequence features includes non-pos features by
\r
903 public String printGFFFormat(SequenceI[] seqs, Hashtable visible)
\r
905 return printGFFFormat(seqs, visible, true, true);
\r
908 public String printGFFFormat(SequenceI[] seqs, Hashtable visible,
\r
909 boolean visOnly, boolean nonpos)
\r
911 StringBuffer out = new StringBuffer();
\r
912 SequenceFeature[] next;
\r
915 for (int i = 0; i < seqs.length; i++)
\r
917 if (seqs[i].getSequenceFeatures() != null)
\r
919 next = seqs[i].getSequenceFeatures();
\r
920 for (int j = 0; j < next.length; j++)
\r
922 isnonpos = next[j].begin == 0 && next[j].end == 0;
\r
923 if ((!nonpos && isnonpos)
\r
924 || (!isnonpos && visOnly && !visible
\r
925 .containsKey(next[j].type)))
\r
930 source = next[j].featureGroup;
\r
931 if (source == null)
\r
933 source = next[j].getDescription();
\r
936 out.append(seqs[i].getName());
\r
938 out.append(source);
\r
940 out.append(next[j].type);
\r
942 out.append(next[j].begin);
\r
944 out.append(next[j].end);
\r
946 out.append(next[j].score);
\r
949 if (next[j].getValue("STRAND") != null)
\r
951 out.append(next[j].getValue("STRAND"));
\r
959 if (next[j].getValue("FRAME") != null)
\r
961 out.append(next[j].getValue("FRAME"));
\r
967 // TODO: verify/check GFF - should there be a /t here before attribute
\r
970 if (next[j].getValue("ATTRIBUTES") != null)
\r
972 out.append(next[j].getValue("ATTRIBUTES"));
\r
975 out.append(newline);
\r
981 return out.toString();
\r
985 * this is only for the benefit of object polymorphism - method does nothing.
\r
987 public void parse()
\r
993 * this is only for the benefit of object polymorphism - method does nothing.
\r
995 * @return error message
\r
997 public String print()
\r
999 return "USE printGFFFormat() or printJalviewFormat()";
\r