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 java.util.Locale;
25 import java.awt.Color;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.TreeMap;
37 import jalview.analysis.AlignmentUtils;
38 import jalview.analysis.SequenceIdMatcher;
39 import jalview.api.AlignViewportI;
40 import jalview.api.FeatureColourI;
41 import jalview.api.FeatureRenderer;
42 import jalview.api.FeaturesSourceI;
43 import jalview.datamodel.AlignedCodonFrame;
44 import jalview.datamodel.Alignment;
45 import jalview.datamodel.AlignmentI;
46 import jalview.datamodel.MappedFeatures;
47 import jalview.datamodel.SequenceDummy;
48 import jalview.datamodel.SequenceFeature;
49 import jalview.datamodel.SequenceI;
50 import jalview.datamodel.features.FeatureMatcherSet;
51 import jalview.datamodel.features.FeatureMatcherSetI;
52 import jalview.gui.Desktop;
53 import jalview.io.gff.GffHelperFactory;
54 import jalview.io.gff.GffHelperI;
55 import jalview.schemes.FeatureColour;
56 import jalview.util.ColorUtils;
57 import jalview.util.MapList;
58 import jalview.util.ParseHtmlBodyAndLinks;
59 import jalview.util.StringUtils;
62 * Parses and writes features files, which may be in Jalview, GFF2 or GFF3
63 * format. These are tab-delimited formats but with differences in the use of
66 * A Jalview feature file may define feature colours and then declare that the
67 * remainder of the file is in GFF format with the line 'GFF'.
69 * GFF3 files may include alignment mappings for features, which Jalview will
70 * attempt to model, and may include sequence data following a ##FASTA line.
77 public class FeaturesFile extends AlignFile implements FeaturesSourceI
79 private static final String EQUALS = "=";
81 private static final String TAB_REGEX = "\\t";
83 private static final String STARTGROUP = "STARTGROUP";
85 private static final String ENDGROUP = "ENDGROUP";
87 private static final String STARTFILTERS = "STARTFILTERS";
89 private static final String ENDFILTERS = "ENDFILTERS";
91 private static final String ID_NOT_SPECIFIED = "ID_NOT_SPECIFIED";
93 protected static final String GFF_VERSION = "##gff-version";
95 private AlignmentI lastmatchedAl = null;
97 private SequenceIdMatcher matcher = null;
99 protected AlignmentI dataset;
101 protected int gffVersion;
104 * Creates a new FeaturesFile object.
106 public FeaturesFile()
111 * Constructor which does not parse the file immediately
114 * File or String filename
116 * @throws IOException
118 public FeaturesFile(Object file, DataSourceType paste) throws IOException
120 super(false, file, paste);
125 * @throws IOException
127 public FeaturesFile(FileParse source) throws IOException
133 * Constructor that optionally parses the file immediately
135 * @param parseImmediately
138 * @throws IOException
140 public FeaturesFile(boolean parseImmediately, Object file,
141 DataSourceType type) throws IOException
143 super(parseImmediately, file, type);
147 * Parse GFF or sequence features file using case-independent matching,
151 * - alignment/dataset containing sequences that are to be annotated
153 * - hashtable to store feature colour definitions
155 * - process html strings into plain text
156 * @return true if features were added
158 public boolean parse(AlignmentI align,
159 Map<String, FeatureColourI> colours, boolean removeHTML)
161 return parse(align, colours, removeHTML, false);
165 * Extends the default addProperties by also adding peptide-to-cDNA mappings
166 * (if any) derived while parsing a GFF file
169 public void addProperties(AlignmentI al)
171 super.addProperties(al);
172 if (dataset != null && dataset.getCodonFrames() != null)
174 AlignmentI ds = (al.getDataset() == null) ? al : al.getDataset();
175 for (AlignedCodonFrame codons : dataset.getCodonFrames())
177 ds.addCodonFrame(codons);
183 * Parse GFF or Jalview format sequence features file
186 * - alignment/dataset containing sequences that are to be annotated
188 * - map to store feature colour definitions
190 * - process html strings into plain text
191 * @param relaxedIdmatching
192 * - when true, ID matches to compound sequence IDs are allowed
193 * @return true if features were added
195 public boolean parse(AlignmentI align,
196 Map<String, FeatureColourI> colours, boolean removeHTML,
197 boolean relaxedIdmatching)
199 return parse(align, colours, null, removeHTML, relaxedIdmatching);
203 * Parse GFF or Jalview format sequence features file
206 * - alignment/dataset containing sequences that are to be annotated
208 * - map to store feature colour definitions
210 * - map to store feature filter definitions
212 * - process html strings into plain text
213 * @param relaxedIdmatching
214 * - when true, ID matches to compound sequence IDs are allowed
215 * @return true if features were added
217 public boolean parse(AlignmentI align,
218 Map<String, FeatureColourI> colours,
219 Map<String, FeatureMatcherSetI> filters, boolean removeHTML,
220 boolean relaxedIdmatching)
222 Map<String, String> gffProps = new HashMap<>();
224 * keep track of any sequences we try to create from the data
226 List<SequenceI> newseqs = new ArrayList<>();
232 String featureGroup = null;
234 while ((line = nextLine()) != null)
236 // skip comments/process pragmas
237 if (line.length() == 0 || line.startsWith("#"))
239 if (line.toLowerCase(Locale.ROOT).startsWith("##"))
241 processGffPragma(line, gffProps, align, newseqs);
246 gffColumns = line.split(TAB_REGEX);
247 if (gffColumns.length == 1)
249 if (line.trim().equalsIgnoreCase("GFF"))
252 * Jalview features file with appended GFF
253 * assume GFF2 (though it may declare ##gff-version 3)
260 if (gffColumns.length > 0 && gffColumns.length < 4)
263 * if 2 or 3 tokens, we anticipate either 'startgroup', 'endgroup' or
264 * a feature type colour specification
266 String ft = gffColumns[0];
267 if (ft.equalsIgnoreCase(STARTFILTERS))
269 parseFilters(filters);
272 if (ft.equalsIgnoreCase(STARTGROUP))
274 featureGroup = gffColumns[1];
276 else if (ft.equalsIgnoreCase(ENDGROUP))
278 // We should check whether this is the current group,
279 // but at present there's no way of showing more than 1 group
284 String colscheme = gffColumns[1];
285 FeatureColourI colour = FeatureColour
286 .parseJalviewFeatureColour(colscheme);
289 colours.put(ft, colour);
296 * if not a comment, GFF pragma, startgroup, endgroup or feature
297 * colour specification, that just leaves a feature details line
298 * in either Jalview or GFF format
302 parseJalviewFeature(line, gffColumns, align, colours, removeHTML,
303 relaxedIdmatching, featureGroup);
307 parseGff(gffColumns, align, relaxedIdmatching, newseqs);
311 } catch (Exception ex)
313 // should report somewhere useful for UI if necessary
314 warningMessage = ((warningMessage == null) ? "" : warningMessage)
315 + "Parsing error at\n" + line;
316 System.out.println("Error parsing feature file: " + ex + "\n" + line);
317 ex.printStackTrace(System.err);
323 * experimental - add any dummy sequences with features to the alignment
324 * - we need them for Ensembl feature extraction - though maybe not otherwise
326 for (SequenceI newseq : newseqs)
328 if (newseq.getFeatures().hasFeatures())
330 align.addSequence(newseq);
337 * Reads input lines from STARTFILTERS to ENDFILTERS and adds a feature type
338 * filter to the map for each line parsed. After exit from this method,
339 * nextLine() should return the line after ENDFILTERS (or we are already at
340 * end of file if ENDFILTERS was missing).
343 * @throws IOException
345 protected void parseFilters(Map<String, FeatureMatcherSetI> filters)
349 while ((line = nextLine()) != null)
351 if (line.toUpperCase(Locale.ROOT).startsWith(ENDFILTERS))
355 String[] tokens = line.split(TAB_REGEX);
356 if (tokens.length != 2)
358 System.err.println(String.format("Invalid token count %d for %d",
359 tokens.length, line));
363 String featureType = tokens[0];
364 FeatureMatcherSetI fm = FeatureMatcherSet.fromString(tokens[1]);
365 if (fm != null && filters != null)
367 filters.put(featureType, fm);
374 * Try to parse a Jalview format feature specification and add it as a
375 * sequence feature to any matching sequences in the alignment. Returns true
376 * if successful (a feature was added), or false if not.
381 * @param featureColours
383 * @param relaxedIdmatching
384 * @param featureGroup
386 protected boolean parseJalviewFeature(String line, String[] gffColumns,
387 AlignmentI alignment, Map<String, FeatureColourI> featureColours,
388 boolean removeHTML, boolean relaxedIdMatching,
392 * tokens: description seqid seqIndex start end type [score]
394 if (gffColumns.length < 6)
396 System.err.println("Ignoring feature line '" + line
397 + "' with too few columns (" + gffColumns.length + ")");
400 String desc = gffColumns[0];
401 String seqId = gffColumns[1];
402 SequenceI seq = findSequence(seqId, alignment, null, relaxedIdMatching);
404 if (!ID_NOT_SPECIFIED.equals(seqId))
406 seq = findSequence(seqId, alignment, null, relaxedIdMatching);
412 String seqIndex = gffColumns[2];
415 int idx = Integer.parseInt(seqIndex);
416 seq = alignment.getSequenceAt(idx);
417 } catch (NumberFormatException ex)
419 System.err.println("Invalid sequence index: " + seqIndex);
425 System.out.println("Sequence not found: " + line);
429 int startPos = Integer.parseInt(gffColumns[3]);
430 int endPos = Integer.parseInt(gffColumns[4]);
432 String ft = gffColumns[5];
434 if (!featureColours.containsKey(ft))
437 * Perhaps an old style groups file with no colours -
438 * synthesize a colour from the feature type
440 Color colour = ColorUtils.createColourFromName(ft);
441 featureColours.put(ft, new FeatureColour(colour));
443 SequenceFeature sf = null;
444 if (gffColumns.length > 6)
446 float score = Float.NaN;
449 score = Float.valueOf(gffColumns[6]).floatValue();
450 } catch (NumberFormatException ex)
452 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
454 sf = new SequenceFeature(ft, desc, startPos, endPos, score,
459 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
462 parseDescriptionHTML(sf, removeHTML);
464 seq.addSequenceFeature(sf);
467 && (seq = alignment.findName(seq, seqId, false)) != null)
469 seq.addSequenceFeature(new SequenceFeature(sf));
475 * clear any temporary handles used to speed up ID matching
477 protected void resetMatcher()
479 lastmatchedAl = null;
484 * Returns a sequence matching the given id, as follows
486 * <li>strict matching is on exact sequence name</li>
487 * <li>relaxed matching allows matching on a token within the sequence name,
489 * <li>first tries to find a match in the alignment sequences</li>
490 * <li>else tries to find a match in the new sequences already generated while
491 * parsing the features file</li>
492 * <li>else creates a new placeholder sequence, adds it to the new sequences
493 * list, and returns it</li>
499 * @param relaxedIdMatching
503 protected SequenceI findSequence(String seqId, AlignmentI align,
504 List<SequenceI> newseqs, boolean relaxedIdMatching)
506 // TODO encapsulate in SequenceIdMatcher, share the matcher
507 // with the GffHelper (removing code duplication)
508 SequenceI match = null;
509 if (relaxedIdMatching)
511 if (lastmatchedAl != align)
513 lastmatchedAl = align;
514 matcher = new SequenceIdMatcher(align.getSequencesArray());
517 matcher.addAll(newseqs);
520 match = matcher.findIdMatch(seqId);
524 match = align.findName(seqId, true);
525 if (match == null && newseqs != null)
527 for (SequenceI m : newseqs)
529 if (seqId.equals(m.getName()))
537 if (match == null && newseqs != null)
539 match = new SequenceDummy(seqId);
540 if (relaxedIdMatching)
542 matcher.addAll(Arrays.asList(new SequenceI[] { match }));
544 // add dummy sequence to the newseqs list
550 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
552 if (sf.getDescription() == null)
556 ParseHtmlBodyAndLinks parsed = new ParseHtmlBodyAndLinks(
557 sf.getDescription(), removeHTML, newline);
561 sf.setDescription(parsed.getNonHtmlContent());
564 for (String link : parsed.getLinks())
571 * Returns contents of a Jalview format features file, for visible features,
572 * as filtered by type and group. Features with a null group are displayed if
573 * their feature type is visible. Non-positional features may optionally be
574 * included (with no check on type or group).
578 * @param includeNonPositional
579 * if true, include non-positional features (regardless of group or
581 * @param includeComplement
582 * if true, include visible complementary (CDS/protein) positional
583 * features, with locations converted to local sequence coordinates
586 public String printJalviewFormat(SequenceI[] sequences,
587 FeatureRenderer fr, boolean includeNonPositional,
588 boolean includeComplement)
590 Map<String, FeatureColourI> visibleColours = fr
591 .getDisplayedFeatureCols();
592 Map<String, FeatureMatcherSetI> featureFilters = fr.getFeatureFilters();
595 * write out feature colours (if we know them)
597 // TODO: decide if feature links should also be written here ?
598 StringBuilder out = new StringBuilder(256);
599 if (visibleColours != null)
601 for (Entry<String, FeatureColourI> featureColour : visibleColours
604 FeatureColourI colour = featureColour.getValue();
605 out.append(colour.toJalviewFormat(featureColour.getKey()))
610 String[] types = visibleColours == null ? new String[0]
611 : visibleColours.keySet()
612 .toArray(new String[visibleColours.keySet().size()]);
615 * feature filters if any
617 outputFeatureFilters(out, visibleColours, featureFilters);
620 * output features within groups
622 int count = outputFeaturesByGroup(out, fr, types, sequences,
623 includeNonPositional);
625 if (includeComplement)
627 count += outputComplementFeatures(out, fr, sequences);
630 return count > 0 ? out.toString() : "No Features Visible";
634 * Outputs any visible complementary (CDS/peptide) positional features as
635 * Jalview format, within feature group. The coordinates of the linked
636 * features are converted to the corresponding positions of the local
644 private int outputComplementFeatures(StringBuilder out,
645 FeatureRenderer fr, SequenceI[] sequences)
647 AlignViewportI comp = fr.getViewport().getCodingComplement();
648 FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp)
649 .getFeatureRenderer();
652 * bin features by feature group and sequence
654 Map<String, Map<String, List<SequenceFeature>>> map = new TreeMap<>(
655 String.CASE_INSENSITIVE_ORDER);
658 for (SequenceI seq : sequences)
661 * find complementary features
663 List<SequenceFeature> complementary = findComplementaryFeatures(seq,
665 String seqName = seq.getName();
667 for (SequenceFeature sf : complementary)
669 String group = sf.getFeatureGroup();
670 if (!map.containsKey(group))
672 map.put(group, new LinkedHashMap<>()); // preserves sequence order
674 Map<String, List<SequenceFeature>> groupFeatures = map.get(group);
675 if (!groupFeatures.containsKey(seqName))
677 groupFeatures.put(seqName, new ArrayList<>());
679 List<SequenceFeature> foundFeatures = groupFeatures.get(seqName);
680 foundFeatures.add(sf);
686 * output features by group
688 for (Entry<String, Map<String, List<SequenceFeature>>> groupFeatures : map
692 String group = groupFeatures.getKey();
693 if (!"".equals(group))
695 out.append(STARTGROUP).append(TAB).append(group).append(newline);
697 Map<String, List<SequenceFeature>> seqFeaturesMap = groupFeatures
699 for (Entry<String, List<SequenceFeature>> seqFeatures : seqFeaturesMap
702 String sequenceName = seqFeatures.getKey();
703 for (SequenceFeature sf : seqFeatures.getValue())
705 formatJalviewFeature(out, sequenceName, sf);
708 if (!"".equals(group))
710 out.append(ENDGROUP).append(TAB).append(group).append(newline);
718 * Answers a list of mapped features visible in the (CDS/protein) complement,
719 * with feature positions translated to local sequence coordinates
725 protected List<SequenceFeature> findComplementaryFeatures(SequenceI seq,
729 * avoid duplication of features (e.g. peptide feature
730 * at all 3 mapped codon positions)
732 List<SequenceFeature> found = new ArrayList<>();
733 List<SequenceFeature> complementary = new ArrayList<>();
735 for (int pos = seq.getStart(); pos <= seq.getEnd(); pos++)
737 MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq, pos);
741 for (SequenceFeature sf : mf.features)
744 * make a virtual feature with local coordinates
746 if (!found.contains(sf))
748 String group = sf.getFeatureGroup();
754 int begin = sf.getBegin();
755 int end = sf.getEnd();
756 int[] range = mf.getMappedPositions(begin, end);
757 SequenceFeature sf2 = new SequenceFeature(sf, range[0],
758 range[1], group, sf.getScore());
759 complementary.add(sf2);
765 return complementary;
769 * Outputs any feature filters defined for visible feature types, sandwiched
770 * by STARTFILTERS and ENDFILTERS lines
774 * @param featureFilters
776 void outputFeatureFilters(StringBuilder out,
777 Map<String, FeatureColourI> visible,
778 Map<String, FeatureMatcherSetI> featureFilters)
780 if (visible == null || featureFilters == null
781 || featureFilters.isEmpty())
786 boolean first = true;
787 for (String featureType : visible.keySet())
789 FeatureMatcherSetI filter = featureFilters.get(featureType);
795 out.append(newline).append(STARTFILTERS).append(newline);
797 out.append(featureType).append(TAB).append(filter.toStableString())
803 out.append(ENDFILTERS).append(newline);
809 * Appends output of visible sequence features within feature groups to the
810 * output buffer. Groups other than the null or empty group are sandwiched by
811 * STARTGROUP and ENDGROUP lines. Answers the number of features written.
815 * @param featureTypes
817 * @param includeNonPositional
820 private int outputFeaturesByGroup(StringBuilder out, FeatureRenderer fr,
821 String[] featureTypes, SequenceI[] sequences,
822 boolean includeNonPositional)
824 List<String> featureGroups = fr.getFeatureGroups();
827 * sort groups alphabetically, and ensure that features with a
828 * null or empty group are output after those in named groups
830 List<String> sortedGroups = new ArrayList<>(featureGroups);
831 sortedGroups.remove(null);
832 sortedGroups.remove("");
833 Collections.sort(sortedGroups);
834 sortedGroups.add(null);
835 sortedGroups.add("");
838 List<String> visibleGroups = fr.getDisplayedFeatureGroups();
841 * loop over all groups (may be visible or not);
842 * non-positional features are output even if group is not visible
844 for (String group : sortedGroups)
846 boolean firstInGroup = true;
847 boolean isNullGroup = group == null || "".equals(group);
849 for (int i = 0; i < sequences.length; i++)
851 String sequenceName = sequences[i].getName();
852 List<SequenceFeature> features = new ArrayList<>();
855 * get any non-positional features in this group, if wanted
856 * (for any feature type, whether visible or not)
858 if (includeNonPositional)
860 features.addAll(sequences[i].getFeatures()
861 .getFeaturesForGroup(false, group));
865 * add positional features for visible feature types, but
866 * (for named groups) only if feature group is visible
868 if (featureTypes.length > 0
869 && (isNullGroup || visibleGroups.contains(group)))
871 features.addAll(sequences[i].getFeatures()
872 .getFeaturesForGroup(true, group, featureTypes));
875 for (SequenceFeature sf : features)
877 if (sf.isNonPositional() || fr.isVisible(sf))
885 out.append(STARTGROUP).append(TAB).append(group)
889 firstInGroup = false;
890 formatJalviewFeature(out, sequenceName, sf);
895 if (!isNullGroup && !firstInGroup)
897 out.append(ENDGROUP).append(TAB).append(group).append(newline);
904 * Formats one feature in Jalview format and appends to the string buffer
907 * @param sequenceName
908 * @param sequenceFeature
910 protected void formatJalviewFeature(StringBuilder out,
911 String sequenceName, SequenceFeature sequenceFeature)
913 if (sequenceFeature.description == null
914 || sequenceFeature.description.equals(""))
916 out.append(sequenceFeature.type).append(TAB);
920 if (sequenceFeature.links != null
921 && sequenceFeature.getDescription().indexOf("<html>") == -1)
923 out.append("<html>");
926 out.append(sequenceFeature.description);
927 if (sequenceFeature.links != null)
929 for (int l = 0; l < sequenceFeature.links.size(); l++)
931 String label = sequenceFeature.links.elementAt(l);
932 String href = label.substring(label.indexOf("|") + 1);
933 label = label.substring(0, label.indexOf("|"));
935 if (sequenceFeature.description.indexOf(href) == -1)
937 out.append(" <a href=\"").append(href).append("\">")
938 .append(label).append("</a>");
942 if (sequenceFeature.getDescription().indexOf("</html>") == -1)
944 out.append("</html>");
950 out.append(sequenceName);
951 out.append("\t-1\t");
952 out.append(sequenceFeature.begin);
954 out.append(sequenceFeature.end);
956 out.append(sequenceFeature.type);
957 if (!Float.isNaN(sequenceFeature.score))
960 out.append(sequenceFeature.score);
966 * Parse method that is called when a GFF file is dragged to the desktop
971 AlignViewportI av = getViewport();
974 if (av.getAlignment() != null)
976 dataset = av.getAlignment().getDataset();
980 // working in the applet context ?
981 dataset = av.getAlignment();
986 dataset = new Alignment(new SequenceI[] {});
989 Map<String, FeatureColourI> featureColours = new HashMap<>();
990 boolean parseResult = parse(dataset, featureColours, false, true);
993 // pass error up somehow
997 // update viewport with the dataset data ?
1001 setSeqs(dataset.getSequencesArray());
1006 * Implementation of unused abstract method
1008 * @return error message
1011 public String print(SequenceI[] sqs, boolean jvsuffix)
1013 System.out.println("Use printGffFormat() or printJalviewFormat()");
1018 * Returns features output in GFF2 format
1021 * the sequences whose features are to be output
1023 * a map whose keys are the type names of visible features
1024 * @param visibleFeatureGroups
1025 * @param includeNonPositionalFeatures
1026 * @param includeComplement
1029 public String printGffFormat(SequenceI[] sequences, FeatureRenderer fr,
1030 boolean includeNonPositionalFeatures, boolean includeComplement)
1032 FeatureRenderer fr2 = null;
1033 if (includeComplement)
1035 AlignViewportI comp = fr.getViewport().getCodingComplement();
1036 fr2 = Desktop.getAlignFrameFor(comp).getFeatureRenderer();
1039 Map<String, FeatureColourI> visibleColours = fr
1040 .getDisplayedFeatureCols();
1042 StringBuilder out = new StringBuilder(256);
1044 out.append(String.format("%s %d\n", GFF_VERSION,
1045 gffVersion == 0 ? 2 : gffVersion));
1047 String[] types = visibleColours == null ? new String[0]
1048 : visibleColours.keySet()
1049 .toArray(new String[visibleColours.keySet().size()]);
1051 for (SequenceI seq : sequences)
1053 List<SequenceFeature> seqFeatures = new ArrayList<>();
1054 List<SequenceFeature> features = new ArrayList<>();
1055 if (includeNonPositionalFeatures)
1057 features.addAll(seq.getFeatures().getNonPositionalFeatures());
1059 if (visibleColours != null && !visibleColours.isEmpty())
1061 features.addAll(seq.getFeatures().getPositionalFeatures(types));
1063 for (SequenceFeature sf : features)
1065 if (sf.isNonPositional() || fr.isVisible(sf))
1068 * drop features hidden by group visibility, colour threshold,
1069 * or feature filter condition
1071 seqFeatures.add(sf);
1075 if (includeComplement)
1077 seqFeatures.addAll(findComplementaryFeatures(seq, fr2));
1081 * sort features here if wanted
1083 for (SequenceFeature sf : seqFeatures)
1085 formatGffFeature(out, seq, sf);
1086 out.append(newline);
1090 return out.toString();
1094 * Formats one feature as GFF and appends to the string buffer
1096 private void formatGffFeature(StringBuilder out, SequenceI seq,
1099 String source = sf.featureGroup;
1102 source = sf.getDescription();
1105 out.append(seq.getName());
1109 out.append(sf.type);
1111 out.append(sf.begin);
1115 out.append(sf.score);
1118 int strand = sf.getStrand();
1119 out.append(strand == 1 ? "+" : (strand == -1 ? "-" : "."));
1122 String phase = sf.getPhase();
1123 out.append(phase == null ? "." : phase);
1125 if (sf.otherDetails != null && !sf.otherDetails.isEmpty())
1127 Map<String, Object> map = sf.otherDetails;
1128 formatAttributes(out, map);
1133 * A helper method that outputs attributes stored in the map as
1134 * semicolon-delimited values e.g.
1137 * AC_Male=0;AF_NFE=0.00000e 00;Hom_FIN=0;GQ_MEDIAN=9
1140 * A map-valued attribute is formatted as a comma-delimited list within
1141 * braces, for example
1144 * jvmap_CSQ={ALLELE_NUM=1,UNIPARC=UPI0002841053,Feature=ENST00000585561}
1147 * The {@code jvmap_} prefix designates a values map and is removed if the
1148 * value is parsed when read in. (The GFF3 specification allows
1149 * 'semi-structured data' to be represented provided the attribute name begins
1150 * with a lower case letter.)
1154 * @see http://gmod.org/wiki/GFF3#GFF3_Format
1156 void formatAttributes(StringBuilder sb, Map<String, Object> map)
1159 boolean first = true;
1160 for (String key : map.keySet())
1162 if (SequenceFeature.STRAND.equals(key)
1163 || SequenceFeature.PHASE.equals(key))
1166 * values stashed in map but output to their own columns
1177 Object value = map.get(key);
1178 if (value instanceof Map<?, ?>)
1180 formatMapAttribute(sb, key, (Map<?, ?>) value);
1184 String formatted = StringUtils.urlEncode(value.toString(),
1185 GffHelperI.GFF_ENCODABLE);
1186 sb.append(key).append(EQUALS).append(formatted);
1192 * Formats the map entries as
1195 * key=key1=value1,key2=value2,...
1198 * and appends this to the string buffer
1204 private void formatMapAttribute(StringBuilder sb, String key,
1207 if (map == null || map.isEmpty())
1213 * AbstractMap.toString would be a shortcut here, but more reliable
1214 * to code the required format in case toString changes in future
1216 sb.append(key).append(EQUALS);
1217 boolean first = true;
1218 for (Entry<?, ?> entry : map.entrySet())
1225 sb.append(entry.getKey().toString()).append(EQUALS);
1226 String formatted = StringUtils.urlEncode(entry.getValue().toString(),
1227 GffHelperI.GFF_ENCODABLE);
1228 sb.append(formatted);
1233 * Returns a mapping given list of one or more Align descriptors (exonerate
1236 * @param alignedRegions
1237 * a list of "Align fromStart toStart fromCount"
1238 * @param mapIsFromCdna
1239 * if true, 'from' is dna, else 'from' is protein
1241 * either 1 (forward) or -1 (reverse)
1243 * @throws IOException
1245 protected MapList constructCodonMappingFromAlign(
1246 List<String> alignedRegions, boolean mapIsFromCdna, int strand)
1251 throw new IOException(
1252 "Invalid strand for a codon mapping (cannot be 0)");
1254 int regions = alignedRegions.size();
1255 // arrays to hold [start, end] for each aligned region
1256 int[] fromRanges = new int[regions * 2]; // from dna
1257 int[] toRanges = new int[regions * 2]; // to protein
1258 int fromRangesIndex = 0;
1259 int toRangesIndex = 0;
1261 for (String range : alignedRegions)
1264 * Align mapFromStart mapToStart mapFromCount
1265 * e.g. if mapIsFromCdna
1266 * Align 11270 143 120
1268 * 120 bases from pos 11270 align to pos 143 in peptide
1269 * if !mapIsFromCdna this would instead be
1270 * Align 143 11270 40
1272 String[] tokens = range.split(" ");
1273 if (tokens.length != 3)
1275 throw new IOException("Wrong number of fields for Align");
1282 fromStart = Integer.parseInt(tokens[0]);
1283 toStart = Integer.parseInt(tokens[1]);
1284 fromCount = Integer.parseInt(tokens[2]);
1285 } catch (NumberFormatException nfe)
1287 throw new IOException(
1288 "Invalid number in Align field: " + nfe.getMessage());
1292 * Jalview always models from dna to protein, so adjust values if the
1293 * GFF mapping is from protein to dna
1298 int temp = fromStart;
1299 fromStart = toStart;
1302 fromRanges[fromRangesIndex++] = fromStart;
1303 fromRanges[fromRangesIndex++] = fromStart + strand * (fromCount - 1);
1306 * If a codon has an intron gap, there will be contiguous 'toRanges';
1307 * this is handled for us by the MapList constructor.
1308 * (It is not clear that exonerate ever generates this case)
1310 toRanges[toRangesIndex++] = toStart;
1311 toRanges[toRangesIndex++] = toStart + (fromCount - 1) / 3;
1314 return new MapList(fromRanges, toRanges, 3, 1);
1318 * Parse a GFF format feature. This may include creating a 'dummy' sequence to
1319 * hold the feature, or for its mapped sequence, or both, to be resolved
1320 * either later in the GFF file (##FASTA section), or when the user loads
1321 * additional sequences.
1325 * @param relaxedIdMatching
1329 protected SequenceI parseGff(String[] gffColumns, AlignmentI alignment,
1330 boolean relaxedIdMatching, List<SequenceI> newseqs)
1333 * GFF: seqid source type start end score strand phase [attributes]
1335 if (gffColumns.length < 5)
1337 System.err.println("Ignoring GFF feature line with too few columns ("
1338 + gffColumns.length + ")");
1343 * locate referenced sequence in alignment _or_
1344 * as a forward or external reference (SequenceDummy)
1346 String seqId = gffColumns[0];
1347 SequenceI seq = findSequence(seqId, alignment, newseqs,
1350 SequenceFeature sf = null;
1351 GffHelperI helper = GffHelperFactory.getHelper(gffColumns);
1356 sf = helper.processGff(seq, gffColumns, alignment, newseqs,
1360 seq.addSequenceFeature(sf);
1361 while ((seq = alignment.findName(seq, seqId, true)) != null)
1363 seq.addSequenceFeature(new SequenceFeature(sf));
1366 } catch (IOException e)
1368 System.err.println("GFF parsing failed with: " + e.getMessage());
1377 * After encountering ##fasta in a GFF3 file, process the remainder of the
1378 * file as FAST sequence data. Any placeholder sequences created during
1379 * feature parsing are updated with the actual sequences.
1383 * @throws IOException
1385 protected void processAsFasta(AlignmentI align, List<SequenceI> newseqs)
1391 } catch (IOException q)
1394 // Opening a FastaFile object with the remainder of this object's dataIn.
1395 // Tell the constructor to NOT close the dataIn when finished.
1396 FastaFile parser = new FastaFile(this, false);
1397 List<SequenceI> includedseqs = parser.getSeqs();
1399 SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
1402 * iterate over includedseqs, and replacing matching ones with newseqs
1403 * sequences. Generic iterator not used here because we modify
1404 * includedseqs as we go
1406 for (int p = 0, pSize = includedseqs.size(); p < pSize; p++)
1408 // search for any dummy seqs that this sequence can be used to update
1409 SequenceI includedSeq = includedseqs.get(p);
1410 SequenceI dummyseq = smatcher.findIdMatch(includedSeq);
1411 if (dummyseq != null && dummyseq instanceof SequenceDummy)
1413 // probably have the pattern wrong
1414 // idea is that a flyweight proxy for a sequence ID can be created for
1415 // 1. stable reference creation
1416 // 2. addition of annotation
1417 // 3. future replacement by a real sequence
1418 // current pattern is to create SequenceDummy objects - a convenience
1419 // constructor for a Sequence.
1420 // problem is that when promoted to a real sequence, all references
1421 // need to be updated somehow. We avoid that by keeping the same object.
1422 ((SequenceDummy) dummyseq).become(includedSeq);
1423 dummyseq.createDatasetSequence();
1426 * Update mappings so they are now to the dataset sequence
1428 for (AlignedCodonFrame mapping : align.getCodonFrames())
1430 mapping.updateToDataset(dummyseq);
1434 * replace parsed sequence with the realised forward reference
1436 includedseqs.set(p, dummyseq);
1439 * and remove from the newseqs list
1441 newseqs.remove(dummyseq);
1446 * finally add sequences to the dataset
1448 for (SequenceI seq : includedseqs)
1450 // experimental: mapping-based 'alignment' to query sequence
1451 AlignmentUtils.alignSequenceAs(seq, align,
1452 String.valueOf(align.getGapCharacter()), false, true);
1454 // rename sequences if GFF handler requested this
1455 // TODO a more elegant way e.g. gffHelper.postProcess(newseqs) ?
1456 List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures();
1459 String newName = (String) sfs.get(0)
1460 .getValue(GffHelperI.RENAME_TOKEN);
1461 if (newName != null)
1463 seq.setName(newName);
1466 align.addSequence(seq);
1471 * Process a ## directive
1477 * @throws IOException
1479 protected void processGffPragma(String line, Map<String, String> gffProps,
1480 AlignmentI align, List<SequenceI> newseqs) throws IOException
1483 if ("###".equals(line))
1485 // close off any open 'forward references'
1489 String[] tokens = line.substring(2).split(" ");
1490 String pragma = tokens[0];
1491 String value = tokens.length == 1 ? null : tokens[1];
1493 if ("gff-version".equalsIgnoreCase(pragma))
1499 // value may be e.g. "3.1.2"
1500 gffVersion = Integer.parseInt(value.split("\\.")[0]);
1501 } catch (NumberFormatException e)
1507 else if ("sequence-region".equalsIgnoreCase(pragma))
1509 // could capture <seqid start end> if wanted here
1511 else if ("feature-ontology".equalsIgnoreCase(pragma))
1513 // should resolve against the specified feature ontology URI
1515 else if ("attribute-ontology".equalsIgnoreCase(pragma))
1517 // URI of attribute ontology - not currently used in GFF3
1519 else if ("source-ontology".equalsIgnoreCase(pragma))
1521 // URI of source ontology - not currently used in GFF3
1523 else if ("species-build".equalsIgnoreCase(pragma))
1525 // save URI of specific NCBI taxon version of annotations
1526 gffProps.put("species-build", value);
1528 else if ("fasta".equalsIgnoreCase(pragma))
1530 // process the rest of the file as a fasta file and replace any dummy
1532 processAsFasta(align, newseqs);
1536 System.err.println("Ignoring unknown pragma: " + line);