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.GffHelperBase;
40 import jalview.io.gff.GffHelperFactory;
41 import jalview.io.gff.GffHelperI;
42 import jalview.schemes.FeatureColour;
43 import jalview.util.ColorUtils;
44 import jalview.util.MapList;
45 import jalview.util.ParseHtmlBodyAndLinks;
46 import jalview.util.StringUtils;
48 import java.awt.Color;
49 import java.io.IOException;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.HashMap;
54 import java.util.LinkedHashMap;
55 import java.util.List;
57 import java.util.Map.Entry;
58 import java.util.TreeMap;
61 * Parses and writes features files, which may be in Jalview, GFF2 or GFF3
62 * format. These are tab-delimited formats but with differences in the use of
65 * A Jalview feature file may define feature colours and then declare that the
66 * remainder of the file is in GFF format with the line 'GFF'.
68 * GFF3 files may include alignment mappings for features, which Jalview will
69 * attempt to model, and may include sequence data following a ##FASTA line.
76 public class FeaturesFile extends AlignFile implements FeaturesSourceI
78 private static final String TAB_REGEX = "\\t";
80 private static final String STARTGROUP = "STARTGROUP";
82 private static final String ENDGROUP = "ENDGROUP";
84 private static final String STARTFILTERS = "STARTFILTERS";
86 private static final String ENDFILTERS = "ENDFILTERS";
88 private static final String ID_NOT_SPECIFIED = "ID_NOT_SPECIFIED";
90 private static final String NOTE = "Note";
92 protected static final String GFF_VERSION = "##gff-version";
94 private AlignmentI lastmatchedAl = null;
96 private SequenceIdMatcher matcher = null;
98 protected AlignmentI dataset;
100 protected int gffVersion;
103 * Creates a new FeaturesFile object.
105 public FeaturesFile()
110 * Constructor which does not parse the file immediately
114 * @throws IOException
116 public FeaturesFile(String file, DataSourceType paste)
119 super(false, file, paste);
124 * @throws IOException
126 public FeaturesFile(FileParse source) throws IOException
132 * Constructor that optionally parses the file immediately
134 * @param parseImmediately
137 * @throws IOException
139 public FeaturesFile(boolean parseImmediately, String file,
140 DataSourceType type) throws IOException
142 super(parseImmediately, file, type);
146 * Parse GFF or sequence features file using case-independent matching,
150 * - alignment/dataset containing sequences that are to be annotated
152 * - hashtable to store feature colour definitions
154 * - process html strings into plain text
155 * @return true if features were added
157 public boolean parse(AlignmentI align,
158 Map<String, FeatureColourI> colours, boolean removeHTML)
160 return parse(align, colours, removeHTML, false);
164 * Extends the default addProperties by also adding peptide-to-cDNA mappings
165 * (if any) derived while parsing a GFF file
168 public void addProperties(AlignmentI al)
170 super.addProperties(al);
171 if (dataset != null && dataset.getCodonFrames() != null)
173 AlignmentI ds = (al.getDataset() == null) ? al : al.getDataset();
174 for (AlignedCodonFrame codons : dataset.getCodonFrames())
176 ds.addCodonFrame(codons);
182 * Parse GFF or Jalview format sequence features file
185 * - alignment/dataset containing sequences that are to be annotated
187 * - map to store feature colour definitions
189 * - process html strings into plain text
190 * @param relaxedIdmatching
191 * - when true, ID matches to compound sequence IDs are allowed
192 * @return true if features were added
194 public boolean parse(AlignmentI align,
195 Map<String, FeatureColourI> colours, boolean removeHTML,
196 boolean relaxedIdmatching)
198 return parse(align, colours, null, removeHTML, relaxedIdmatching);
202 * Parse GFF or Jalview format sequence features file
205 * - alignment/dataset containing sequences that are to be annotated
207 * - map to store feature colour definitions
209 * - map to store feature filter definitions
211 * - process html strings into plain text
212 * @param relaxedIdmatching
213 * - when true, ID matches to compound sequence IDs are allowed
214 * @return true if features were added
216 public boolean parse(AlignmentI align,
217 Map<String, FeatureColourI> colours,
218 Map<String, FeatureMatcherSetI> filters, boolean removeHTML,
219 boolean relaxedIdmatching)
221 Map<String, String> gffProps = new HashMap<>();
223 * keep track of any sequences we try to create from the data
225 List<SequenceI> newseqs = new ArrayList<>();
231 String featureGroup = null;
233 while ((line = nextLine()) != null)
235 // skip comments/process pragmas
236 if (line.length() == 0 || line.startsWith("#"))
238 if (line.toLowerCase().startsWith("##"))
240 processGffPragma(line, gffProps, align, newseqs);
245 gffColumns = line.split(TAB_REGEX);
246 if (gffColumns.length == 1)
248 if (line.trim().equalsIgnoreCase("GFF"))
251 * Jalview features file with appended GFF
252 * assume GFF2 (though it may declare ##gff-version 3)
259 if (gffColumns.length > 0 && gffColumns.length < 4)
262 * if 2 or 3 tokens, we anticipate either 'startgroup', 'endgroup' or
263 * a feature type colour specification
265 String ft = gffColumns[0];
266 if (ft.equalsIgnoreCase(STARTFILTERS))
268 parseFilters(filters);
271 if (ft.equalsIgnoreCase(STARTGROUP))
273 featureGroup = gffColumns[1];
275 else if (ft.equalsIgnoreCase(ENDGROUP))
277 // We should check whether this is the current group,
278 // but at present there's no way of showing more than 1 group
283 String colscheme = gffColumns[1];
284 FeatureColourI colour = FeatureColour
285 .parseJalviewFeatureColour(colscheme);
288 colours.put(ft, colour);
295 * if not a comment, GFF pragma, startgroup, endgroup or feature
296 * colour specification, that just leaves a feature details line
297 * in either Jalview or GFF format
301 parseJalviewFeature(line, gffColumns, align, colours, removeHTML,
302 relaxedIdmatching, featureGroup);
306 parseGff(gffColumns, align, relaxedIdmatching, newseqs);
310 } catch (Exception ex)
312 // should report somewhere useful for UI if necessary
313 warningMessage = ((warningMessage == null) ? "" : warningMessage)
314 + "Parsing error at\n" + line;
315 System.out.println("Error parsing feature file: " + ex + "\n" + line);
316 ex.printStackTrace(System.err);
322 * experimental - add any dummy sequences with features to the alignment
323 * - we need them for Ensembl feature extraction - though maybe not otherwise
325 for (SequenceI newseq : newseqs)
327 if (newseq.getFeatures().hasFeatures())
329 align.addSequence(newseq);
336 * Reads input lines from STARTFILTERS to ENDFILTERS and adds a feature type
337 * filter to the map for each line parsed. After exit from this method,
338 * nextLine() should return the line after ENDFILTERS (or we are already at
339 * end of file if ENDFILTERS was missing).
342 * @throws IOException
344 protected void parseFilters(Map<String, FeatureMatcherSetI> filters)
348 while ((line = nextLine()) != null)
350 if (line.toUpperCase().startsWith(ENDFILTERS))
354 String[] tokens = line.split(TAB_REGEX);
355 if (tokens.length != 2)
357 System.err.println(String.format("Invalid token count %d for %d",
358 tokens.length, line));
362 String featureType = tokens[0];
363 FeatureMatcherSetI fm = FeatureMatcherSet.fromString(tokens[1]);
364 if (fm != null && filters != null)
366 filters.put(featureType, fm);
373 * Try to parse a Jalview format feature specification and add it as a
374 * sequence feature to any matching sequences in the alignment. Returns true
375 * if successful (a feature was added), or false if not.
380 * @param featureColours
382 * @param relaxedIdmatching
383 * @param featureGroup
385 protected boolean parseJalviewFeature(String line, String[] gffColumns,
386 AlignmentI alignment, Map<String, FeatureColourI> featureColours,
387 boolean removeHTML, boolean relaxedIdMatching,
391 * tokens: description seqid seqIndex start end type [score]
393 if (gffColumns.length < 6)
395 System.err.println("Ignoring feature line '" + line
396 + "' with too few columns (" + gffColumns.length + ")");
399 String desc = gffColumns[0];
400 String seqId = gffColumns[1];
401 SequenceI seq = findSequence(seqId, alignment, null, relaxedIdMatching);
403 if (!ID_NOT_SPECIFIED.equals(seqId))
405 seq = findSequence(seqId, alignment, null, relaxedIdMatching);
411 String seqIndex = gffColumns[2];
414 int idx = Integer.parseInt(seqIndex);
415 seq = alignment.getSequenceAt(idx);
416 } catch (NumberFormatException ex)
418 System.err.println("Invalid sequence index: " + seqIndex);
424 System.out.println("Sequence not found: " + line);
428 int startPos = Integer.parseInt(gffColumns[3]);
429 int endPos = Integer.parseInt(gffColumns[4]);
431 String ft = gffColumns[5];
433 if (!featureColours.containsKey(ft))
436 * Perhaps an old style groups file with no colours -
437 * synthesize a colour from the feature type
439 Color colour = ColorUtils.createColourFromName(ft);
440 featureColours.put(ft, new FeatureColour(colour));
442 SequenceFeature sf = null;
443 if (gffColumns.length > 6)
445 float score = Float.NaN;
448 score = Float.valueOf(gffColumns[6]).floatValue();
449 } catch (NumberFormatException ex)
451 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
453 sf = new SequenceFeature(ft, desc, startPos, endPos, score,
458 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
461 parseDescriptionHTML(sf, removeHTML);
463 seq.addSequenceFeature(sf);
466 && (seq = alignment.findName(seq, seqId, false)) != null)
468 seq.addSequenceFeature(new SequenceFeature(sf));
474 * clear any temporary handles used to speed up ID matching
476 protected void resetMatcher()
478 lastmatchedAl = null;
483 * Returns a sequence matching the given id, as follows
485 * <li>strict matching is on exact sequence name</li>
486 * <li>relaxed matching allows matching on a token within the sequence name,
488 * <li>first tries to find a match in the alignment sequences</li>
489 * <li>else tries to find a match in the new sequences already generated while
490 * parsing the features file</li>
491 * <li>else creates a new placeholder sequence, adds it to the new sequences
492 * list, and returns it</li>
498 * @param relaxedIdMatching
502 protected SequenceI findSequence(String seqId, AlignmentI align,
503 List<SequenceI> newseqs, boolean relaxedIdMatching)
505 // TODO encapsulate in SequenceIdMatcher, share the matcher
506 // with the GffHelper (removing code duplication)
507 SequenceI match = null;
508 if (relaxedIdMatching)
510 if (lastmatchedAl != align)
512 lastmatchedAl = align;
513 matcher = new SequenceIdMatcher(align.getSequencesArray());
516 matcher.addAll(newseqs);
519 match = matcher.findIdMatch(seqId);
523 match = align.findName(seqId, true);
524 if (match == null && newseqs != null)
526 for (SequenceI m : newseqs)
528 if (seqId.equals(m.getName()))
536 if (match == null && newseqs != null)
538 match = new SequenceDummy(seqId);
539 if (relaxedIdMatching)
541 matcher.addAll(Arrays.asList(new SequenceI[] { match }));
543 // add dummy sequence to the newseqs list
549 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
551 if (sf.getDescription() == null)
555 ParseHtmlBodyAndLinks parsed = new ParseHtmlBodyAndLinks(
556 sf.getDescription(), removeHTML, newline);
560 sf.setDescription(parsed.getNonHtmlContent());
563 for (String link : parsed.getLinks())
570 * Returns contents of a Jalview format features file, for visible features, as
571 * filtered by type and group. Features with a null group are displayed if their
572 * feature type is visible. Non-positional features may optionally be included
573 * (with no check on type or group).
577 * @param includeNonPositional
578 * if true, include non-positional features
579 * (regardless of group or type)
580 * @param includeComplement
581 * if true, include visible complementary
582 * (CDS/protein) positional features, with
583 * locations converted to local sequence
587 public String printJalviewFormat(SequenceI[] sequences,
588 FeatureRenderer fr, boolean includeNonPositional,
589 boolean includeComplement)
591 Map<String, FeatureColourI> visibleColours = fr
592 .getDisplayedFeatureCols();
593 Map<String, FeatureMatcherSetI> featureFilters = fr.getFeatureFilters();
596 * write out feature colours (if we know them)
598 // TODO: decide if feature links should also be written here ?
599 StringBuilder out = new StringBuilder(256);
600 if (visibleColours != null)
602 for (Entry<String, FeatureColourI> featureColour : visibleColours
605 FeatureColourI colour = featureColour.getValue();
606 out.append(colour.toJalviewFormat(featureColour.getKey())).append(
611 String[] types = visibleColours == null ? new String[0]
612 : visibleColours.keySet()
613 .toArray(new String[visibleColours.keySet().size()]);
616 * feature filters if any
618 outputFeatureFilters(out, visibleColours, featureFilters);
621 * output features within groups
623 int count = outputFeaturesByGroup(out, fr, types, sequences,
624 includeNonPositional);
626 if (includeComplement)
628 count += outputComplementFeatures(out, fr, sequences);
631 return count > 0 ? out.toString() : "No Features Visible";
635 * Outputs any visible complementary (CDS/peptide) positional features as
636 * Jalview format, within feature group. The coordinates of the linked features
637 * are converted to the corresponding positions of the local sequences.
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.entrySet())
691 String group = groupFeatures.getKey();
692 if (!"".equals(group))
694 out.append(STARTGROUP).append(TAB).append(group).append(newline);
696 Map<String, List<SequenceFeature>> seqFeaturesMap = groupFeatures
698 for (Entry<String, List<SequenceFeature>> seqFeatures : seqFeaturesMap
701 String sequenceName = seqFeatures.getKey();
702 for (SequenceFeature sf : seqFeatures.getValue())
704 formatJalviewFeature(out, sequenceName, sf);
707 if (!"".equals(group))
709 out.append(ENDGROUP).append(TAB).append(group).append(newline);
716 protected List<SequenceFeature> findComplementaryFeatures(SequenceI seq,
720 * avoid duplication of features (e.g. peptide feature
721 * at all 3 mapped codon positions)
723 List<SequenceFeature> found = new ArrayList<>();
724 List<SequenceFeature> complementary = new ArrayList<>();
726 for (int pos = seq.getStart(); pos <= seq.getEnd(); pos++)
728 MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq, pos);
732 MapList mapping = mf.mapping.getMap();
733 for (SequenceFeature sf : mf.features)
736 * make a virtual feature with local coordinates
738 if (!found.contains(sf))
740 String group = sf.getFeatureGroup();
746 int begin = sf.getBegin();
747 int end = sf.getEnd();
748 int[] range = mf.mapping.getTo() == seq.getDatasetSequence()
749 ? mapping.locateInTo(begin, end)
750 : mapping.locateInFrom(begin, end);
751 SequenceFeature sf2 = new SequenceFeature(sf, range[0],
752 range[1], group, sf.getScore());
753 complementary.add(sf2);
759 return complementary;
763 * Outputs any feature filters defined for visible feature types, sandwiched by
764 * STARTFILTERS and ENDFILTERS lines
768 * @param featureFilters
770 void outputFeatureFilters(StringBuilder out,
771 Map<String, FeatureColourI> visible,
772 Map<String, FeatureMatcherSetI> featureFilters)
774 if (visible == null || featureFilters == null
775 || featureFilters.isEmpty())
780 boolean first = true;
781 for (String featureType : visible.keySet())
783 FeatureMatcherSetI filter = featureFilters.get(featureType);
789 out.append(newline).append(STARTFILTERS).append(newline);
791 out.append(featureType).append(TAB).append(filter.toStableString())
797 out.append(ENDFILTERS).append(newline);
803 * Appends output of visible sequence features within feature groups to the
804 * output buffer. Groups other than the null or empty group are sandwiched by
805 * STARTGROUP and ENDGROUP lines. Answers the number of features written.
809 * @param featureTypes
811 * @param includeNonPositional
814 private int outputFeaturesByGroup(StringBuilder out,
815 FeatureRenderer fr, String[] featureTypes,
816 SequenceI[] sequences, boolean includeNonPositional)
818 List<String> featureGroups = fr.getFeatureGroups();
821 * sort groups alphabetically, and ensure that features with a
822 * null or empty group are output after those in named groups
824 List<String> sortedGroups = new ArrayList<>(featureGroups);
825 sortedGroups.remove(null);
826 sortedGroups.remove("");
827 Collections.sort(sortedGroups);
828 sortedGroups.add(null);
829 sortedGroups.add("");
832 List<String> visibleGroups = fr.getDisplayedFeatureGroups();
835 * loop over all groups (may be visible or not);
836 * non-positional features are output even if group is not visible
838 for (String group : sortedGroups)
840 boolean firstInGroup = true;
841 boolean isNullGroup = group == null || "".equals(group);
843 for (int i = 0; i < sequences.length; i++)
845 String sequenceName = sequences[i].getName();
846 List<SequenceFeature> features = new ArrayList<>();
849 * get any non-positional features in this group, if wanted
850 * (for any feature type, whether visible or not)
852 if (includeNonPositional)
854 features.addAll(sequences[i].getFeatures()
855 .getFeaturesForGroup(false, group));
859 * add positional features for visible feature types, but
860 * (for named groups) only if feature group is visible
862 if (featureTypes.length > 0
863 && (isNullGroup || visibleGroups.contains(group)))
865 features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
866 true, group, featureTypes));
869 for (SequenceFeature sf : features)
871 if (sf.isNonPositional() || fr.isVisible(sf))
879 out.append(STARTGROUP).append(TAB).append(group)
883 firstInGroup = false;
884 formatJalviewFeature(out, sequenceName, sf);
889 if (!isNullGroup && !firstInGroup)
891 out.append(ENDGROUP).append(TAB).append(group).append(newline);
898 * Formats one feature in Jalview format and appends to the string buffer
901 * @param sequenceName
902 * @param sequenceFeature
904 protected void formatJalviewFeature(
905 StringBuilder out, String sequenceName,
906 SequenceFeature sequenceFeature)
908 if (sequenceFeature.description == null
909 || sequenceFeature.description.equals(""))
911 out.append(sequenceFeature.type).append(TAB);
915 if (sequenceFeature.links != null
916 && sequenceFeature.getDescription().indexOf("<html>") == -1)
918 out.append("<html>");
921 out.append(sequenceFeature.description);
922 if (sequenceFeature.links != null)
924 for (int l = 0; l < sequenceFeature.links.size(); l++)
926 String label = sequenceFeature.links.elementAt(l);
927 String href = label.substring(label.indexOf("|") + 1);
928 label = label.substring(0, label.indexOf("|"));
930 if (sequenceFeature.description.indexOf(href) == -1)
932 out.append(" <a href=\"").append(href).append("\">")
933 .append(label).append("</a>");
937 if (sequenceFeature.getDescription().indexOf("</html>") == -1)
939 out.append("</html>");
945 out.append(sequenceName);
946 out.append("\t-1\t");
947 out.append(sequenceFeature.begin);
949 out.append(sequenceFeature.end);
951 out.append(sequenceFeature.type);
952 if (!Float.isNaN(sequenceFeature.score))
955 out.append(sequenceFeature.score);
961 * Parse method that is called when a GFF file is dragged to the desktop
966 AlignViewportI av = getViewport();
969 if (av.getAlignment() != null)
971 dataset = av.getAlignment().getDataset();
975 // working in the applet context ?
976 dataset = av.getAlignment();
981 dataset = new Alignment(new SequenceI[] {});
984 Map<String, FeatureColourI> featureColours = new HashMap<>();
985 boolean parseResult = parse(dataset, featureColours, false, true);
988 // pass error up somehow
992 // update viewport with the dataset data ?
996 setSeqs(dataset.getSequencesArray());
1001 * Implementation of unused abstract method
1003 * @return error message
1006 public String print(SequenceI[] sqs, boolean jvsuffix)
1008 System.out.println("Use printGffFormat() or printJalviewFormat()");
1013 * Returns features output in GFF2 format
1016 * the sequences whose features are to be
1019 * a map whose keys are the type names of
1021 * @param visibleFeatureGroups
1022 * @param includeNonPositionalFeatures
1023 * @param includeComplement
1026 public String printGffFormat(SequenceI[] sequences,
1027 FeatureRenderer fr, boolean includeNonPositionalFeatures,
1028 boolean includeComplement)
1030 FeatureRenderer fr2 = null;
1031 if (includeComplement)
1033 AlignViewportI comp = fr.getViewport().getCodingComplement();
1034 fr2 = Desktop.getAlignFrameFor(comp).getFeatureRenderer();
1037 Map<String, FeatureColourI> visibleColours = fr.getDisplayedFeatureCols();
1039 StringBuilder out = new StringBuilder(256);
1041 out.append(String.format("%s %d\n", GFF_VERSION, gffVersion == 0 ? 2 : gffVersion));
1043 String[] types = visibleColours == null ? new String[0]
1044 : visibleColours.keySet()
1045 .toArray(new String[visibleColours.keySet().size()]);
1047 for (SequenceI seq : sequences)
1049 List<SequenceFeature> seqFeatures = new ArrayList<>();
1050 List<SequenceFeature> features = new ArrayList<>();
1051 if (includeNonPositionalFeatures)
1053 features.addAll(seq.getFeatures().getNonPositionalFeatures());
1055 if (visibleColours != null && !visibleColours.isEmpty())
1057 features.addAll(seq.getFeatures().getPositionalFeatures(types));
1059 for (SequenceFeature sf : features)
1061 if (sf.isNonPositional() || fr.isVisible(sf))
1064 * drop features hidden by group visibility, colour threshold,
1065 * or feature filter condition
1067 seqFeatures.add(sf);
1071 if (includeComplement)
1073 seqFeatures.addAll(findComplementaryFeatures(seq, fr2));
1077 * sort features here if wanted
1079 for (SequenceFeature sf : seqFeatures)
1081 formatGffFeature(out, seq, sf);
1082 out.append(newline);
1086 return out.toString();
1090 * Formats one feature as GFF and appends to the string buffer
1092 private void formatGffFeature(StringBuilder out, SequenceI seq,
1095 String source = sf.featureGroup;
1098 source = sf.getDescription();
1101 out.append(seq.getName());
1105 out.append(sf.type);
1107 out.append(sf.begin);
1111 out.append(sf.score);
1114 int strand = sf.getStrand();
1115 out.append(strand == 1 ? "+" : (strand == -1 ? "-" : "."));
1118 String phase = sf.getPhase();
1119 out.append(phase == null ? "." : phase);
1121 // miscellaneous key-values (GFF column 9)
1122 String attributes = sf.getAttributes();
1123 if (attributes != null)
1125 out.append(TAB).append(attributes);
1130 * Returns a mapping given list of one or more Align descriptors (exonerate
1133 * @param alignedRegions
1134 * a list of "Align fromStart toStart fromCount"
1135 * @param mapIsFromCdna
1136 * if true, 'from' is dna, else 'from' is protein
1138 * either 1 (forward) or -1 (reverse)
1140 * @throws IOException
1142 protected MapList constructCodonMappingFromAlign(
1143 List<String> alignedRegions, boolean mapIsFromCdna, int strand)
1148 throw new IOException(
1149 "Invalid strand for a codon mapping (cannot be 0)");
1151 int regions = alignedRegions.size();
1152 // arrays to hold [start, end] for each aligned region
1153 int[] fromRanges = new int[regions * 2]; // from dna
1154 int[] toRanges = new int[regions * 2]; // to protein
1155 int fromRangesIndex = 0;
1156 int toRangesIndex = 0;
1158 for (String range : alignedRegions)
1161 * Align mapFromStart mapToStart mapFromCount
1162 * e.g. if mapIsFromCdna
1163 * Align 11270 143 120
1165 * 120 bases from pos 11270 align to pos 143 in peptide
1166 * if !mapIsFromCdna this would instead be
1167 * Align 143 11270 40
1169 String[] tokens = range.split(" ");
1170 if (tokens.length != 3)
1172 throw new IOException("Wrong number of fields for Align");
1179 fromStart = Integer.parseInt(tokens[0]);
1180 toStart = Integer.parseInt(tokens[1]);
1181 fromCount = Integer.parseInt(tokens[2]);
1182 } catch (NumberFormatException nfe)
1184 throw new IOException(
1185 "Invalid number in Align field: " + nfe.getMessage());
1189 * Jalview always models from dna to protein, so adjust values if the
1190 * GFF mapping is from protein to dna
1195 int temp = fromStart;
1196 fromStart = toStart;
1199 fromRanges[fromRangesIndex++] = fromStart;
1200 fromRanges[fromRangesIndex++] = fromStart + strand * (fromCount - 1);
1203 * If a codon has an intron gap, there will be contiguous 'toRanges';
1204 * this is handled for us by the MapList constructor.
1205 * (It is not clear that exonerate ever generates this case)
1207 toRanges[toRangesIndex++] = toStart;
1208 toRanges[toRangesIndex++] = toStart + (fromCount - 1) / 3;
1211 return new MapList(fromRanges, toRanges, 3, 1);
1215 * Parse a GFF format feature. This may include creating a 'dummy' sequence to
1216 * hold the feature, or for its mapped sequence, or both, to be resolved
1217 * either later in the GFF file (##FASTA section), or when the user loads
1218 * additional sequences.
1222 * @param relaxedIdMatching
1226 protected SequenceI parseGff(String[] gffColumns, AlignmentI alignment,
1227 boolean relaxedIdMatching, List<SequenceI> newseqs)
1230 * GFF: seqid source type start end score strand phase [attributes]
1232 if (gffColumns.length < 5)
1234 System.err.println("Ignoring GFF feature line with too few columns ("
1235 + gffColumns.length + ")");
1240 * locate referenced sequence in alignment _or_
1241 * as a forward or external reference (SequenceDummy)
1243 String seqId = gffColumns[0];
1244 SequenceI seq = findSequence(seqId, alignment, newseqs,
1247 SequenceFeature sf = null;
1248 GffHelperI helper = GffHelperFactory.getHelper(gffColumns);
1253 sf = helper.processGff(seq, gffColumns, alignment, newseqs,
1257 seq.addSequenceFeature(sf);
1258 while ((seq = alignment.findName(seq, seqId, true)) != null)
1260 seq.addSequenceFeature(new SequenceFeature(sf));
1263 } catch (IOException e)
1265 System.err.println("GFF parsing failed with: " + e.getMessage());
1274 * Process the 'column 9' data of the GFF file. This is less formally defined,
1275 * and its interpretation will vary depending on the tool that has generated
1281 protected void processGffColumnNine(String attributes, SequenceFeature sf)
1283 sf.setAttributes(attributes);
1286 * Parse attributes in column 9 and add them to the sequence feature's
1287 * 'otherData' table; use Note as a best proxy for description
1289 char nameValueSeparator = gffVersion == 3 ? '=' : ' ';
1290 // TODO check we don't break GFF2 values which include commas here
1291 Map<String, List<String>> nameValues = GffHelperBase
1292 .parseNameValuePairs(attributes, ";", nameValueSeparator, ",");
1293 for (Entry<String, List<String>> attr : nameValues.entrySet())
1295 String values = StringUtils.listToDelimitedString(attr.getValue(),
1297 sf.setValue(attr.getKey(), values);
1298 if (NOTE.equals(attr.getKey()))
1300 sf.setDescription(values);
1306 * After encountering ##fasta in a GFF3 file, process the remainder of the
1307 * file as FAST sequence data. Any placeholder sequences created during
1308 * feature parsing are updated with the actual sequences.
1312 * @throws IOException
1314 protected void processAsFasta(AlignmentI align, List<SequenceI> newseqs)
1320 } catch (IOException q)
1323 FastaFile parser = new FastaFile(this);
1324 List<SequenceI> includedseqs = parser.getSeqs();
1326 SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
1329 * iterate over includedseqs, and replacing matching ones with newseqs
1330 * sequences. Generic iterator not used here because we modify
1331 * includedseqs as we go
1333 for (int p = 0, pSize = includedseqs.size(); p < pSize; p++)
1335 // search for any dummy seqs that this sequence can be used to update
1336 SequenceI includedSeq = includedseqs.get(p);
1337 SequenceI dummyseq = smatcher.findIdMatch(includedSeq);
1338 if (dummyseq != null && dummyseq instanceof SequenceDummy)
1340 // probably have the pattern wrong
1341 // idea is that a flyweight proxy for a sequence ID can be created for
1342 // 1. stable reference creation
1343 // 2. addition of annotation
1344 // 3. future replacement by a real sequence
1345 // current pattern is to create SequenceDummy objects - a convenience
1346 // constructor for a Sequence.
1347 // problem is that when promoted to a real sequence, all references
1348 // need to be updated somehow. We avoid that by keeping the same object.
1349 ((SequenceDummy) dummyseq).become(includedSeq);
1350 dummyseq.createDatasetSequence();
1353 * Update mappings so they are now to the dataset sequence
1355 for (AlignedCodonFrame mapping : align.getCodonFrames())
1357 mapping.updateToDataset(dummyseq);
1361 * replace parsed sequence with the realised forward reference
1363 includedseqs.set(p, dummyseq);
1366 * and remove from the newseqs list
1368 newseqs.remove(dummyseq);
1373 * finally add sequences to the dataset
1375 for (SequenceI seq : includedseqs)
1377 // experimental: mapping-based 'alignment' to query sequence
1378 AlignmentUtils.alignSequenceAs(seq, align,
1379 String.valueOf(align.getGapCharacter()), false, true);
1381 // rename sequences if GFF handler requested this
1382 // TODO a more elegant way e.g. gffHelper.postProcess(newseqs) ?
1383 List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures();
1386 String newName = (String) sfs.get(0).getValue(
1387 GffHelperI.RENAME_TOKEN);
1388 if (newName != null)
1390 seq.setName(newName);
1393 align.addSequence(seq);
1398 * Process a ## directive
1404 * @throws IOException
1406 protected void processGffPragma(String line, Map<String, String> gffProps,
1407 AlignmentI align, List<SequenceI> newseqs) throws IOException
1410 if ("###".equals(line))
1412 // close off any open 'forward references'
1416 String[] tokens = line.substring(2).split(" ");
1417 String pragma = tokens[0];
1418 String value = tokens.length == 1 ? null : tokens[1];
1420 if ("gff-version".equalsIgnoreCase(pragma))
1426 // value may be e.g. "3.1.2"
1427 gffVersion = Integer.parseInt(value.split("\\.")[0]);
1428 } catch (NumberFormatException e)
1434 else if ("sequence-region".equalsIgnoreCase(pragma))
1436 // could capture <seqid start end> if wanted here
1438 else if ("feature-ontology".equalsIgnoreCase(pragma))
1440 // should resolve against the specified feature ontology URI
1442 else if ("attribute-ontology".equalsIgnoreCase(pragma))
1444 // URI of attribute ontology - not currently used in GFF3
1446 else if ("source-ontology".equalsIgnoreCase(pragma))
1448 // URI of source ontology - not currently used in GFF3
1450 else if ("species-build".equalsIgnoreCase(pragma))
1452 // save URI of specific NCBI taxon version of annotations
1453 gffProps.put("species-build", value);
1455 else if ("fasta".equalsIgnoreCase(pragma))
1457 // process the rest of the file as a fasta file and replace any dummy
1459 processAsFasta(align, newseqs);
1463 System.err.println("Ignoring unknown pragma: " + line);