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.
23 import jalview.analysis.AlignmentUtils;
24 import jalview.analysis.SequenceIdMatcher;
25 import jalview.api.AlignViewportI;
26 import jalview.api.FeatureColourI;
27 import jalview.api.FeatureRenderer;
28 import jalview.api.FeaturesSourceI;
29 import jalview.datamodel.AlignedCodonFrame;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.MappedFeatures;
33 import jalview.datamodel.SequenceDummy;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36 import jalview.datamodel.features.FeatureMatcherSet;
37 import jalview.datamodel.features.FeatureMatcherSetI;
38 import jalview.gui.Desktop;
39 import jalview.io.gff.GffHelperFactory;
40 import jalview.io.gff.GffHelperI;
41 import jalview.schemes.FeatureColour;
42 import jalview.util.ColorUtils;
43 import jalview.util.MapList;
44 import jalview.util.ParseHtmlBodyAndLinks;
45 import jalview.util.StringUtils;
47 import java.awt.Color;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collections;
52 import java.util.HashMap;
53 import java.util.LinkedHashMap;
54 import java.util.List;
56 import java.util.Map.Entry;
57 import java.util.TreeMap;
60 * Parses and writes features files, which may be in Jalview, GFF2 or GFF3
61 * format. These are tab-delimited formats but with differences in the use of
64 * A Jalview feature file may define feature colours and then declare that the
65 * remainder of the file is in GFF format with the line 'GFF'.
67 * GFF3 files may include alignment mappings for features, which Jalview will
68 * attempt to model, and may include sequence data following a ##FASTA line.
75 public class FeaturesFile extends AlignFile implements FeaturesSourceI
78 * map-valued attributes are prefixed with this for output to GFF3;
79 * the prefix is removed if found on reading
81 public static final String MAP_ATTRIBUTE_PREFIX = "jvmap_";
83 private static final String TAB_REGEX = "\\t";
85 private static final String STARTGROUP = "STARTGROUP";
87 private static final String ENDGROUP = "ENDGROUP";
89 private static final String STARTFILTERS = "STARTFILTERS";
91 private static final String ENDFILTERS = "ENDFILTERS";
93 private static final String ID_NOT_SPECIFIED = "ID_NOT_SPECIFIED";
95 private static final String NOTE = "Note";
97 protected static final String GFF_VERSION = "##gff-version";
99 private AlignmentI lastmatchedAl = null;
101 private SequenceIdMatcher matcher = null;
103 protected AlignmentI dataset;
105 protected int gffVersion;
108 * Creates a new FeaturesFile object.
110 public FeaturesFile()
115 * Constructor which does not parse the file immediately
119 * @throws IOException
121 public FeaturesFile(String file, DataSourceType paste)
124 super(false, file, paste);
129 * @throws IOException
131 public FeaturesFile(FileParse source) throws IOException
137 * Constructor that optionally parses the file immediately
139 * @param parseImmediately
142 * @throws IOException
144 public FeaturesFile(boolean parseImmediately, String file,
145 DataSourceType type) throws IOException
147 super(parseImmediately, file, type);
151 * Parse GFF or sequence features file using case-independent matching,
155 * - alignment/dataset containing sequences that are to be annotated
157 * - hashtable to store feature colour definitions
159 * - process html strings into plain text
160 * @return true if features were added
162 public boolean parse(AlignmentI align,
163 Map<String, FeatureColourI> colours, boolean removeHTML)
165 return parse(align, colours, removeHTML, false);
169 * Extends the default addProperties by also adding peptide-to-cDNA mappings
170 * (if any) derived while parsing a GFF file
173 public void addProperties(AlignmentI al)
175 super.addProperties(al);
176 if (dataset != null && dataset.getCodonFrames() != null)
178 AlignmentI ds = (al.getDataset() == null) ? al : al.getDataset();
179 for (AlignedCodonFrame codons : dataset.getCodonFrames())
181 ds.addCodonFrame(codons);
187 * Parse GFF or Jalview format sequence features file
190 * - alignment/dataset containing sequences that are to be annotated
192 * - map to store feature colour definitions
194 * - process html strings into plain text
195 * @param relaxedIdmatching
196 * - when true, ID matches to compound sequence IDs are allowed
197 * @return true if features were added
199 public boolean parse(AlignmentI align,
200 Map<String, FeatureColourI> colours, boolean removeHTML,
201 boolean relaxedIdmatching)
203 return parse(align, colours, null, removeHTML, relaxedIdmatching);
207 * Parse GFF or Jalview format sequence features file
210 * - alignment/dataset containing sequences that are to be annotated
212 * - map to store feature colour definitions
214 * - map to store feature filter definitions
216 * - process html strings into plain text
217 * @param relaxedIdmatching
218 * - when true, ID matches to compound sequence IDs are allowed
219 * @return true if features were added
221 public boolean parse(AlignmentI align,
222 Map<String, FeatureColourI> colours,
223 Map<String, FeatureMatcherSetI> filters, boolean removeHTML,
224 boolean relaxedIdmatching)
226 Map<String, String> gffProps = new HashMap<>();
228 * keep track of any sequences we try to create from the data
230 List<SequenceI> newseqs = new ArrayList<>();
236 String featureGroup = null;
238 while ((line = nextLine()) != null)
240 // skip comments/process pragmas
241 if (line.length() == 0 || line.startsWith("#"))
243 if (line.toLowerCase().startsWith("##"))
245 processGffPragma(line, gffProps, align, newseqs);
250 gffColumns = line.split(TAB_REGEX);
251 if (gffColumns.length == 1)
253 if (line.trim().equalsIgnoreCase("GFF"))
256 * Jalview features file with appended GFF
257 * assume GFF2 (though it may declare ##gff-version 3)
264 if (gffColumns.length > 0 && gffColumns.length < 4)
267 * if 2 or 3 tokens, we anticipate either 'startgroup', 'endgroup' or
268 * a feature type colour specification
270 String ft = gffColumns[0];
271 if (ft.equalsIgnoreCase(STARTFILTERS))
273 parseFilters(filters);
276 if (ft.equalsIgnoreCase(STARTGROUP))
278 featureGroup = gffColumns[1];
280 else if (ft.equalsIgnoreCase(ENDGROUP))
282 // We should check whether this is the current group,
283 // but at present there's no way of showing more than 1 group
288 String colscheme = gffColumns[1];
289 FeatureColourI colour = FeatureColour
290 .parseJalviewFeatureColour(colscheme);
293 colours.put(ft, colour);
300 * if not a comment, GFF pragma, startgroup, endgroup or feature
301 * colour specification, that just leaves a feature details line
302 * in either Jalview or GFF format
306 parseJalviewFeature(line, gffColumns, align, colours, removeHTML,
307 relaxedIdmatching, featureGroup);
311 parseGff(gffColumns, align, relaxedIdmatching, newseqs);
315 } catch (Exception ex)
317 // should report somewhere useful for UI if necessary
318 warningMessage = ((warningMessage == null) ? "" : warningMessage)
319 + "Parsing error at\n" + line;
320 System.out.println("Error parsing feature file: " + ex + "\n" + line);
321 ex.printStackTrace(System.err);
327 * experimental - add any dummy sequences with features to the alignment
328 * - we need them for Ensembl feature extraction - though maybe not otherwise
330 for (SequenceI newseq : newseqs)
332 if (newseq.getFeatures().hasFeatures())
334 align.addSequence(newseq);
341 * Reads input lines from STARTFILTERS to ENDFILTERS and adds a feature type
342 * filter to the map for each line parsed. After exit from this method,
343 * nextLine() should return the line after ENDFILTERS (or we are already at
344 * end of file if ENDFILTERS was missing).
347 * @throws IOException
349 protected void parseFilters(Map<String, FeatureMatcherSetI> filters)
353 while ((line = nextLine()) != null)
355 if (line.toUpperCase().startsWith(ENDFILTERS))
359 String[] tokens = line.split(TAB_REGEX);
360 if (tokens.length != 2)
362 System.err.println(String.format("Invalid token count %d for %d",
363 tokens.length, line));
367 String featureType = tokens[0];
368 FeatureMatcherSetI fm = FeatureMatcherSet.fromString(tokens[1]);
369 if (fm != null && filters != null)
371 filters.put(featureType, fm);
378 * Try to parse a Jalview format feature specification and add it as a
379 * sequence feature to any matching sequences in the alignment. Returns true
380 * if successful (a feature was added), or false if not.
385 * @param featureColours
387 * @param relaxedIdmatching
388 * @param featureGroup
390 protected boolean parseJalviewFeature(String line, String[] gffColumns,
391 AlignmentI alignment, Map<String, FeatureColourI> featureColours,
392 boolean removeHTML, boolean relaxedIdMatching,
396 * tokens: description seqid seqIndex start end type [score]
398 if (gffColumns.length < 6)
400 System.err.println("Ignoring feature line '" + line
401 + "' with too few columns (" + gffColumns.length + ")");
404 String desc = gffColumns[0];
405 String seqId = gffColumns[1];
406 SequenceI seq = findSequence(seqId, alignment, null, relaxedIdMatching);
408 if (!ID_NOT_SPECIFIED.equals(seqId))
410 seq = findSequence(seqId, alignment, null, relaxedIdMatching);
416 String seqIndex = gffColumns[2];
419 int idx = Integer.parseInt(seqIndex);
420 seq = alignment.getSequenceAt(idx);
421 } catch (NumberFormatException ex)
423 System.err.println("Invalid sequence index: " + seqIndex);
429 System.out.println("Sequence not found: " + line);
433 int startPos = Integer.parseInt(gffColumns[3]);
434 int endPos = Integer.parseInt(gffColumns[4]);
436 String ft = gffColumns[5];
438 if (!featureColours.containsKey(ft))
441 * Perhaps an old style groups file with no colours -
442 * synthesize a colour from the feature type
444 Color colour = ColorUtils.createColourFromName(ft);
445 featureColours.put(ft, new FeatureColour(colour));
447 SequenceFeature sf = null;
448 if (gffColumns.length > 6)
450 float score = Float.NaN;
453 score = Float.valueOf(gffColumns[6]).floatValue();
454 } catch (NumberFormatException ex)
456 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
458 sf = new SequenceFeature(ft, desc, startPos, endPos, score,
463 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
466 parseDescriptionHTML(sf, removeHTML);
468 seq.addSequenceFeature(sf);
471 && (seq = alignment.findName(seq, seqId, false)) != null)
473 seq.addSequenceFeature(new SequenceFeature(sf));
479 * clear any temporary handles used to speed up ID matching
481 protected void resetMatcher()
483 lastmatchedAl = null;
488 * Returns a sequence matching the given id, as follows
490 * <li>strict matching is on exact sequence name</li>
491 * <li>relaxed matching allows matching on a token within the sequence name,
493 * <li>first tries to find a match in the alignment sequences</li>
494 * <li>else tries to find a match in the new sequences already generated while
495 * parsing the features file</li>
496 * <li>else creates a new placeholder sequence, adds it to the new sequences
497 * list, and returns it</li>
503 * @param relaxedIdMatching
507 protected SequenceI findSequence(String seqId, AlignmentI align,
508 List<SequenceI> newseqs, boolean relaxedIdMatching)
510 // TODO encapsulate in SequenceIdMatcher, share the matcher
511 // with the GffHelper (removing code duplication)
512 SequenceI match = null;
513 if (relaxedIdMatching)
515 if (lastmatchedAl != align)
517 lastmatchedAl = align;
518 matcher = new SequenceIdMatcher(align.getSequencesArray());
521 matcher.addAll(newseqs);
524 match = matcher.findIdMatch(seqId);
528 match = align.findName(seqId, true);
529 if (match == null && newseqs != null)
531 for (SequenceI m : newseqs)
533 if (seqId.equals(m.getName()))
541 if (match == null && newseqs != null)
543 match = new SequenceDummy(seqId);
544 if (relaxedIdMatching)
546 matcher.addAll(Arrays.asList(new SequenceI[] { match }));
548 // add dummy sequence to the newseqs list
554 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
556 if (sf.getDescription() == null)
560 ParseHtmlBodyAndLinks parsed = new ParseHtmlBodyAndLinks(
561 sf.getDescription(), removeHTML, newline);
565 sf.setDescription(parsed.getNonHtmlContent());
568 for (String link : parsed.getLinks())
575 * Returns contents of a Jalview format features file, for visible features, as
576 * filtered by type and group. Features with a null group are displayed if their
577 * feature type is visible. Non-positional features may optionally be included
578 * (with no check on type or group).
582 * @param includeNonPositional
583 * if true, include non-positional features
584 * (regardless of group or type)
585 * @param includeComplement
586 * if true, include visible complementary
587 * (CDS/protein) positional features, with
588 * locations converted to local sequence
592 public String printJalviewFormat(SequenceI[] sequences,
593 FeatureRenderer fr, boolean includeNonPositional,
594 boolean includeComplement)
596 Map<String, FeatureColourI> visibleColours = fr
597 .getDisplayedFeatureCols();
598 Map<String, FeatureMatcherSetI> featureFilters = fr.getFeatureFilters();
601 * write out feature colours (if we know them)
603 // TODO: decide if feature links should also be written here ?
604 StringBuilder out = new StringBuilder(256);
605 if (visibleColours != null)
607 for (Entry<String, FeatureColourI> featureColour : visibleColours
610 FeatureColourI colour = featureColour.getValue();
611 out.append(colour.toJalviewFormat(featureColour.getKey())).append(
616 String[] types = visibleColours == null ? new String[0]
617 : visibleColours.keySet()
618 .toArray(new String[visibleColours.keySet().size()]);
621 * feature filters if any
623 outputFeatureFilters(out, visibleColours, featureFilters);
626 * output features within groups
628 int count = outputFeaturesByGroup(out, fr, types, sequences,
629 includeNonPositional);
631 if (includeComplement)
633 count += outputComplementFeatures(out, fr, sequences);
636 return count > 0 ? out.toString() : "No Features Visible";
640 * Outputs any visible complementary (CDS/peptide) positional features as
641 * Jalview format, within feature group. The coordinates of the linked features
642 * are converted to the corresponding positions of the local sequences.
649 private int outputComplementFeatures(StringBuilder out,
650 FeatureRenderer fr, SequenceI[] sequences)
652 AlignViewportI comp = fr.getViewport().getCodingComplement();
653 FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp)
654 .getFeatureRenderer();
657 * bin features by feature group and sequence
659 Map<String, Map<String, List<SequenceFeature>>> map = new TreeMap<>(
660 String.CASE_INSENSITIVE_ORDER);
663 for (SequenceI seq : sequences)
666 * find complementary features
668 List<SequenceFeature> complementary = findComplementaryFeatures(seq,
670 String seqName = seq.getName();
672 for (SequenceFeature sf : complementary)
674 String group = sf.getFeatureGroup();
675 if (!map.containsKey(group))
677 map.put(group, new LinkedHashMap<>()); // preserves sequence order
679 Map<String, List<SequenceFeature>> groupFeatures = map.get(group);
680 if (!groupFeatures.containsKey(seqName))
682 groupFeatures.put(seqName, new ArrayList<>());
684 List<SequenceFeature> foundFeatures = groupFeatures.get(seqName);
685 foundFeatures.add(sf);
691 * output features by group
693 for (Entry<String, Map<String, List<SequenceFeature>>> groupFeatures : map.entrySet())
696 String group = groupFeatures.getKey();
697 if (!"".equals(group))
699 out.append(STARTGROUP).append(TAB).append(group).append(newline);
701 Map<String, List<SequenceFeature>> seqFeaturesMap = groupFeatures
703 for (Entry<String, List<SequenceFeature>> seqFeatures : seqFeaturesMap
706 String sequenceName = seqFeatures.getKey();
707 for (SequenceFeature sf : seqFeatures.getValue())
709 formatJalviewFeature(out, sequenceName, sf);
712 if (!"".equals(group))
714 out.append(ENDGROUP).append(TAB).append(group).append(newline);
722 * Answers a list of mapped features visible in the (CDS/protein) complement,
723 * with feature positions translated to local sequence coordinates
729 protected List<SequenceFeature> findComplementaryFeatures(SequenceI seq,
733 * avoid duplication of features (e.g. peptide feature
734 * at all 3 mapped codon positions)
736 List<SequenceFeature> found = new ArrayList<>();
737 List<SequenceFeature> complementary = new ArrayList<>();
739 for (int pos = seq.getStart(); pos <= seq.getEnd(); pos++)
741 MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq, pos);
745 MapList mapping = mf.mapping.getMap();
746 for (SequenceFeature sf : mf.features)
749 * make a virtual feature with local coordinates
751 if (!found.contains(sf))
753 String group = sf.getFeatureGroup();
759 int begin = sf.getBegin();
760 int end = sf.getEnd();
761 int[] range = mf.mapping.getTo() == seq.getDatasetSequence()
762 ? mapping.locateInTo(begin, end)
763 : mapping.locateInFrom(begin, end);
764 SequenceFeature sf2 = new SequenceFeature(sf, range[0],
765 range[1], group, sf.getScore());
766 complementary.add(sf2);
772 return complementary;
776 * Outputs any feature filters defined for visible feature types, sandwiched by
777 * STARTFILTERS and ENDFILTERS lines
781 * @param featureFilters
783 void outputFeatureFilters(StringBuilder out,
784 Map<String, FeatureColourI> visible,
785 Map<String, FeatureMatcherSetI> featureFilters)
787 if (visible == null || featureFilters == null
788 || featureFilters.isEmpty())
793 boolean first = true;
794 for (String featureType : visible.keySet())
796 FeatureMatcherSetI filter = featureFilters.get(featureType);
802 out.append(newline).append(STARTFILTERS).append(newline);
804 out.append(featureType).append(TAB).append(filter.toStableString())
810 out.append(ENDFILTERS).append(newline);
816 * Appends output of visible sequence features within feature groups to the
817 * output buffer. Groups other than the null or empty group are sandwiched by
818 * STARTGROUP and ENDGROUP lines. Answers the number of features written.
822 * @param featureTypes
824 * @param includeNonPositional
827 private int outputFeaturesByGroup(StringBuilder out,
828 FeatureRenderer fr, String[] featureTypes,
829 SequenceI[] sequences, boolean includeNonPositional)
831 List<String> featureGroups = fr.getFeatureGroups();
834 * sort groups alphabetically, and ensure that features with a
835 * null or empty group are output after those in named groups
837 List<String> sortedGroups = new ArrayList<>(featureGroups);
838 sortedGroups.remove(null);
839 sortedGroups.remove("");
840 Collections.sort(sortedGroups);
841 sortedGroups.add(null);
842 sortedGroups.add("");
845 List<String> visibleGroups = fr.getDisplayedFeatureGroups();
848 * loop over all groups (may be visible or not);
849 * non-positional features are output even if group is not visible
851 for (String group : sortedGroups)
853 boolean firstInGroup = true;
854 boolean isNullGroup = group == null || "".equals(group);
856 for (int i = 0; i < sequences.length; i++)
858 String sequenceName = sequences[i].getName();
859 List<SequenceFeature> features = new ArrayList<>();
862 * get any non-positional features in this group, if wanted
863 * (for any feature type, whether visible or not)
865 if (includeNonPositional)
867 features.addAll(sequences[i].getFeatures()
868 .getFeaturesForGroup(false, group));
872 * add positional features for visible feature types, but
873 * (for named groups) only if feature group is visible
875 if (featureTypes.length > 0
876 && (isNullGroup || visibleGroups.contains(group)))
878 features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
879 true, group, featureTypes));
882 for (SequenceFeature sf : features)
884 if (sf.isNonPositional() || fr.isVisible(sf))
892 out.append(STARTGROUP).append(TAB).append(group)
896 firstInGroup = false;
897 formatJalviewFeature(out, sequenceName, sf);
902 if (!isNullGroup && !firstInGroup)
904 out.append(ENDGROUP).append(TAB).append(group).append(newline);
911 * Formats one feature in Jalview format and appends to the string buffer
914 * @param sequenceName
915 * @param sequenceFeature
917 protected void formatJalviewFeature(
918 StringBuilder out, String sequenceName,
919 SequenceFeature sequenceFeature)
921 if (sequenceFeature.description == null
922 || sequenceFeature.description.equals(""))
924 out.append(sequenceFeature.type).append(TAB);
928 if (sequenceFeature.links != null
929 && sequenceFeature.getDescription().indexOf("<html>") == -1)
931 out.append("<html>");
934 out.append(sequenceFeature.description);
935 if (sequenceFeature.links != null)
937 for (int l = 0; l < sequenceFeature.links.size(); l++)
939 String label = sequenceFeature.links.elementAt(l);
940 String href = label.substring(label.indexOf("|") + 1);
941 label = label.substring(0, label.indexOf("|"));
943 if (sequenceFeature.description.indexOf(href) == -1)
945 out.append(" <a href=\"").append(href).append("\">")
946 .append(label).append("</a>");
950 if (sequenceFeature.getDescription().indexOf("</html>") == -1)
952 out.append("</html>");
958 out.append(sequenceName);
959 out.append("\t-1\t");
960 out.append(sequenceFeature.begin);
962 out.append(sequenceFeature.end);
964 out.append(sequenceFeature.type);
965 if (!Float.isNaN(sequenceFeature.score))
968 out.append(sequenceFeature.score);
974 * Parse method that is called when a GFF file is dragged to the desktop
979 AlignViewportI av = getViewport();
982 if (av.getAlignment() != null)
984 dataset = av.getAlignment().getDataset();
988 // working in the applet context ?
989 dataset = av.getAlignment();
994 dataset = new Alignment(new SequenceI[] {});
997 Map<String, FeatureColourI> featureColours = new HashMap<>();
998 boolean parseResult = parse(dataset, featureColours, false, true);
1001 // pass error up somehow
1005 // update viewport with the dataset data ?
1009 setSeqs(dataset.getSequencesArray());
1014 * Implementation of unused abstract method
1016 * @return error message
1019 public String print(SequenceI[] sqs, boolean jvsuffix)
1021 System.out.println("Use printGffFormat() or printJalviewFormat()");
1026 * Returns features output in GFF2 format
1029 * the sequences whose features are to be
1032 * a map whose keys are the type names of
1034 * @param visibleFeatureGroups
1035 * @param includeNonPositionalFeatures
1036 * @param includeComplement
1039 public String printGffFormat(SequenceI[] sequences,
1040 FeatureRenderer fr, boolean includeNonPositionalFeatures,
1041 boolean includeComplement)
1043 FeatureRenderer fr2 = null;
1044 if (includeComplement)
1046 AlignViewportI comp = fr.getViewport().getCodingComplement();
1047 fr2 = Desktop.getAlignFrameFor(comp).getFeatureRenderer();
1050 Map<String, FeatureColourI> visibleColours = fr.getDisplayedFeatureCols();
1052 StringBuilder out = new StringBuilder(256);
1054 out.append(String.format("%s %d\n", GFF_VERSION, gffVersion == 0 ? 2 : gffVersion));
1056 String[] types = visibleColours == null ? new String[0]
1057 : visibleColours.keySet()
1058 .toArray(new String[visibleColours.keySet().size()]);
1060 for (SequenceI seq : sequences)
1062 List<SequenceFeature> seqFeatures = new ArrayList<>();
1063 List<SequenceFeature> features = new ArrayList<>();
1064 if (includeNonPositionalFeatures)
1066 features.addAll(seq.getFeatures().getNonPositionalFeatures());
1068 if (visibleColours != null && !visibleColours.isEmpty())
1070 features.addAll(seq.getFeatures().getPositionalFeatures(types));
1072 for (SequenceFeature sf : features)
1074 if (sf.isNonPositional() || fr.isVisible(sf))
1077 * drop features hidden by group visibility, colour threshold,
1078 * or feature filter condition
1080 seqFeatures.add(sf);
1084 if (includeComplement)
1086 seqFeatures.addAll(findComplementaryFeatures(seq, fr2));
1090 * sort features here if wanted
1092 for (SequenceFeature sf : seqFeatures)
1094 formatGffFeature(out, seq, sf);
1095 out.append(newline);
1099 return out.toString();
1103 * Formats one feature as GFF and appends to the string buffer
1105 private void formatGffFeature(StringBuilder out, SequenceI seq,
1108 String source = sf.featureGroup;
1111 source = sf.getDescription();
1114 out.append(seq.getName());
1118 out.append(sf.type);
1120 out.append(sf.begin);
1124 out.append(sf.score);
1127 int strand = sf.getStrand();
1128 out.append(strand == 1 ? "+" : (strand == -1 ? "-" : "."));
1131 String phase = sf.getPhase();
1132 out.append(phase == null ? "." : phase);
1134 if (sf.otherDetails != null && !sf.otherDetails.isEmpty())
1136 Map<String, Object> map = sf.otherDetails;
1137 formatAttributes(out, map);
1142 * A helper method that outputs attributes stored in the map as
1143 * semicolon-delimited values e.g.
1146 * AC_Male=0;AF_NFE=0.00000e 00;Hom_FIN=0;GQ_MEDIAN=9
1149 * A map-valued attribute is formatted as a comma-delimited list within braces,
1153 * jvmap_CSQ={ALLELE_NUM=1,UNIPARC=UPI0002841053,Feature=ENST00000585561}
1156 * The {@code jvmap_} prefix designates a values map and is removed if the value
1157 * is parsed when read in. (The GFF3 specification allows 'semi-structured data'
1158 * to be represented provided the attribute name begins with a lower case
1163 * @see http://gmod.org/wiki/GFF3#GFF3_Format
1165 void formatAttributes(StringBuilder sb, Map<String, Object> map)
1168 boolean first = true;
1169 for (String key : map.keySet())
1171 if (SequenceFeature.STRAND.equals(key)
1172 || SequenceFeature.PHASE.equals(key))
1175 * values stashed in map but output to their own columns
1186 Object value = map.get(key);
1187 if (value instanceof Map<?, ?>)
1189 formatMapAttribute(sb, key, (Map<?, ?>) value);
1193 String formatted = StringUtils.urlEncode(value.toString(),
1194 GffHelperI.GFF_ENCODABLE);
1195 sb.append(key).append("=").append(formatted);
1201 * Formats the map entries as
1204 * jvmap_key={key1=value1,key2=value2,...}
1207 * and appends this to the string buffer
1213 private void formatMapAttribute(StringBuilder sb, String key,
1216 if (map == null || map.isEmpty())
1222 * AbstractMap.toString would be a shortcut here, but more reliable
1223 * to code the required format in case toString changes in future
1225 sb.append(MAP_ATTRIBUTE_PREFIX).append(key).append("={");
1226 boolean first = true;
1227 for (Entry<?, ?> entry : map.entrySet())
1234 sb.append(entry.getKey().toString()).append("=");
1235 String formatted = StringUtils.urlEncode(entry.getValue().toString(),
1236 GffHelperI.GFF_ENCODABLE);
1237 sb.append(formatted);
1243 * Returns a mapping given list of one or more Align descriptors (exonerate
1246 * @param alignedRegions
1247 * a list of "Align fromStart toStart fromCount"
1248 * @param mapIsFromCdna
1249 * if true, 'from' is dna, else 'from' is protein
1251 * either 1 (forward) or -1 (reverse)
1253 * @throws IOException
1255 protected MapList constructCodonMappingFromAlign(
1256 List<String> alignedRegions, boolean mapIsFromCdna, int strand)
1261 throw new IOException(
1262 "Invalid strand for a codon mapping (cannot be 0)");
1264 int regions = alignedRegions.size();
1265 // arrays to hold [start, end] for each aligned region
1266 int[] fromRanges = new int[regions * 2]; // from dna
1267 int[] toRanges = new int[regions * 2]; // to protein
1268 int fromRangesIndex = 0;
1269 int toRangesIndex = 0;
1271 for (String range : alignedRegions)
1274 * Align mapFromStart mapToStart mapFromCount
1275 * e.g. if mapIsFromCdna
1276 * Align 11270 143 120
1278 * 120 bases from pos 11270 align to pos 143 in peptide
1279 * if !mapIsFromCdna this would instead be
1280 * Align 143 11270 40
1282 String[] tokens = range.split(" ");
1283 if (tokens.length != 3)
1285 throw new IOException("Wrong number of fields for Align");
1292 fromStart = Integer.parseInt(tokens[0]);
1293 toStart = Integer.parseInt(tokens[1]);
1294 fromCount = Integer.parseInt(tokens[2]);
1295 } catch (NumberFormatException nfe)
1297 throw new IOException(
1298 "Invalid number in Align field: " + nfe.getMessage());
1302 * Jalview always models from dna to protein, so adjust values if the
1303 * GFF mapping is from protein to dna
1308 int temp = fromStart;
1309 fromStart = toStart;
1312 fromRanges[fromRangesIndex++] = fromStart;
1313 fromRanges[fromRangesIndex++] = fromStart + strand * (fromCount - 1);
1316 * If a codon has an intron gap, there will be contiguous 'toRanges';
1317 * this is handled for us by the MapList constructor.
1318 * (It is not clear that exonerate ever generates this case)
1320 toRanges[toRangesIndex++] = toStart;
1321 toRanges[toRangesIndex++] = toStart + (fromCount - 1) / 3;
1324 return new MapList(fromRanges, toRanges, 3, 1);
1328 * Parse a GFF format feature. This may include creating a 'dummy' sequence to
1329 * hold the feature, or for its mapped sequence, or both, to be resolved
1330 * either later in the GFF file (##FASTA section), or when the user loads
1331 * additional sequences.
1335 * @param relaxedIdMatching
1339 protected SequenceI parseGff(String[] gffColumns, AlignmentI alignment,
1340 boolean relaxedIdMatching, List<SequenceI> newseqs)
1343 * GFF: seqid source type start end score strand phase [attributes]
1345 if (gffColumns.length < 5)
1347 System.err.println("Ignoring GFF feature line with too few columns ("
1348 + gffColumns.length + ")");
1353 * locate referenced sequence in alignment _or_
1354 * as a forward or external reference (SequenceDummy)
1356 String seqId = gffColumns[0];
1357 SequenceI seq = findSequence(seqId, alignment, newseqs,
1360 SequenceFeature sf = null;
1361 GffHelperI helper = GffHelperFactory.getHelper(gffColumns);
1366 sf = helper.processGff(seq, gffColumns, alignment, newseqs,
1370 seq.addSequenceFeature(sf);
1371 while ((seq = alignment.findName(seq, seqId, true)) != null)
1373 seq.addSequenceFeature(new SequenceFeature(sf));
1376 } catch (IOException e)
1378 System.err.println("GFF parsing failed with: " + e.getMessage());
1387 * After encountering ##fasta in a GFF3 file, process the remainder of the
1388 * file as FAST sequence data. Any placeholder sequences created during
1389 * feature parsing are updated with the actual sequences.
1393 * @throws IOException
1395 protected void processAsFasta(AlignmentI align, List<SequenceI> newseqs)
1401 } catch (IOException q)
1404 FastaFile parser = new FastaFile(this);
1405 List<SequenceI> includedseqs = parser.getSeqs();
1407 SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
1410 * iterate over includedseqs, and replacing matching ones with newseqs
1411 * sequences. Generic iterator not used here because we modify
1412 * includedseqs as we go
1414 for (int p = 0, pSize = includedseqs.size(); p < pSize; p++)
1416 // search for any dummy seqs that this sequence can be used to update
1417 SequenceI includedSeq = includedseqs.get(p);
1418 SequenceI dummyseq = smatcher.findIdMatch(includedSeq);
1419 if (dummyseq != null && dummyseq instanceof SequenceDummy)
1421 // probably have the pattern wrong
1422 // idea is that a flyweight proxy for a sequence ID can be created for
1423 // 1. stable reference creation
1424 // 2. addition of annotation
1425 // 3. future replacement by a real sequence
1426 // current pattern is to create SequenceDummy objects - a convenience
1427 // constructor for a Sequence.
1428 // problem is that when promoted to a real sequence, all references
1429 // need to be updated somehow. We avoid that by keeping the same object.
1430 ((SequenceDummy) dummyseq).become(includedSeq);
1431 dummyseq.createDatasetSequence();
1434 * Update mappings so they are now to the dataset sequence
1436 for (AlignedCodonFrame mapping : align.getCodonFrames())
1438 mapping.updateToDataset(dummyseq);
1442 * replace parsed sequence with the realised forward reference
1444 includedseqs.set(p, dummyseq);
1447 * and remove from the newseqs list
1449 newseqs.remove(dummyseq);
1454 * finally add sequences to the dataset
1456 for (SequenceI seq : includedseqs)
1458 // experimental: mapping-based 'alignment' to query sequence
1459 AlignmentUtils.alignSequenceAs(seq, align,
1460 String.valueOf(align.getGapCharacter()), false, true);
1462 // rename sequences if GFF handler requested this
1463 // TODO a more elegant way e.g. gffHelper.postProcess(newseqs) ?
1464 List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures();
1467 String newName = (String) sfs.get(0).getValue(
1468 GffHelperI.RENAME_TOKEN);
1469 if (newName != null)
1471 seq.setName(newName);
1474 align.addSequence(seq);
1479 * Process a ## directive
1485 * @throws IOException
1487 protected void processGffPragma(String line, Map<String, String> gffProps,
1488 AlignmentI align, List<SequenceI> newseqs) throws IOException
1491 if ("###".equals(line))
1493 // close off any open 'forward references'
1497 String[] tokens = line.substring(2).split(" ");
1498 String pragma = tokens[0];
1499 String value = tokens.length == 1 ? null : tokens[1];
1501 if ("gff-version".equalsIgnoreCase(pragma))
1507 // value may be e.g. "3.1.2"
1508 gffVersion = Integer.parseInt(value.split("\\.")[0]);
1509 } catch (NumberFormatException e)
1515 else if ("sequence-region".equalsIgnoreCase(pragma))
1517 // could capture <seqid start end> if wanted here
1519 else if ("feature-ontology".equalsIgnoreCase(pragma))
1521 // should resolve against the specified feature ontology URI
1523 else if ("attribute-ontology".equalsIgnoreCase(pragma))
1525 // URI of attribute ontology - not currently used in GFF3
1527 else if ("source-ontology".equalsIgnoreCase(pragma))
1529 // URI of source ontology - not currently used in GFF3
1531 else if ("species-build".equalsIgnoreCase(pragma))
1533 // save URI of specific NCBI taxon version of annotations
1534 gffProps.put("species-build", value);
1536 else if ("fasta".equalsIgnoreCase(pragma))
1538 // process the rest of the file as a fasta file and replace any dummy
1540 processAsFasta(align, newseqs);
1544 System.err.println("Ignoring unknown pragma: " + line);