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.FeaturesSourceI;
28 import jalview.datamodel.AlignedCodonFrame;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.SequenceDummy;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceI;
34 import jalview.datamodel.features.FeatureMatcherSet;
35 import jalview.datamodel.features.FeatureMatcherSetI;
36 import jalview.io.gff.GffHelperBase;
37 import jalview.io.gff.GffHelperFactory;
38 import jalview.io.gff.GffHelperI;
39 import jalview.schemes.FeatureColour;
40 import jalview.util.ColorUtils;
41 import jalview.util.MapList;
42 import jalview.util.ParseHtmlBodyAndLinks;
43 import jalview.util.StringUtils;
45 import java.awt.Color;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.List;
53 import java.util.Map.Entry;
56 * Parses and writes features files, which may be in Jalview, GFF2 or GFF3
57 * format. These are tab-delimited formats but with differences in the use of
60 * A Jalview feature file may define feature colours and then declare that the
61 * remainder of the file is in GFF format with the line 'GFF'.
63 * GFF3 files may include alignment mappings for features, which Jalview will
64 * attempt to model, and may include sequence data following a ##FASTA line.
71 public class FeaturesFile extends AlignFile implements FeaturesSourceI
73 private static final String TAB_REGEX = "\\t";
75 private static final String STARTGROUP = "STARTGROUP";
77 private static final String ENDGROUP = "ENDGROUP";
79 private static final String STARTFILTERS = "STARTFILTERS";
81 private static final String ENDFILTERS = "ENDFILTERS";
83 private static final String ID_NOT_SPECIFIED = "ID_NOT_SPECIFIED";
85 private static final String NOTE = "Note";
87 protected static final String TAB = "\t";
89 protected static final String GFF_VERSION = "##gff-version";
91 private AlignmentI lastmatchedAl = null;
93 private SequenceIdMatcher matcher = null;
95 protected AlignmentI dataset;
97 protected int gffVersion;
100 * Creates a new FeaturesFile object.
102 public FeaturesFile()
107 * Constructor which does not parse the file immediately
111 * @throws IOException
113 public FeaturesFile(String file, DataSourceType paste)
116 super(false, file, paste);
121 * @throws IOException
123 public FeaturesFile(FileParse source) throws IOException
129 * Constructor that optionally parses the file immediately
131 * @param parseImmediately
134 * @throws IOException
136 public FeaturesFile(boolean parseImmediately, String file,
137 DataSourceType type) throws IOException
139 super(parseImmediately, file, type);
143 * Parse GFF or sequence features file using case-independent matching,
147 * - alignment/dataset containing sequences that are to be annotated
149 * - hashtable to store feature colour definitions
151 * - process html strings into plain text
152 * @return true if features were added
154 public boolean parse(AlignmentI align,
155 Map<String, FeatureColourI> colours, boolean removeHTML)
157 return parse(align, colours, removeHTML, false);
161 * Extends the default addProperties by also adding peptide-to-cDNA mappings
162 * (if any) derived while parsing a GFF file
165 public void addProperties(AlignmentI al)
167 super.addProperties(al);
168 if (dataset != null && dataset.getCodonFrames() != null)
170 AlignmentI ds = (al.getDataset() == null) ? al : al.getDataset();
171 for (AlignedCodonFrame codons : dataset.getCodonFrames())
173 ds.addCodonFrame(codons);
179 * Parse GFF or Jalview format sequence features file
182 * - alignment/dataset containing sequences that are to be annotated
184 * - map to store feature colour definitions
186 * - process html strings into plain text
187 * @param relaxedIdmatching
188 * - when true, ID matches to compound sequence IDs are allowed
189 * @return true if features were added
191 public boolean parse(AlignmentI align,
192 Map<String, FeatureColourI> colours, boolean removeHTML,
193 boolean relaxedIdmatching)
195 return parse(align, colours, null, removeHTML, relaxedIdmatching);
199 * Parse GFF or Jalview format sequence features file
202 * - alignment/dataset containing sequences that are to be annotated
204 * - map to store feature colour definitions
206 * - map to store feature filter definitions
208 * - process html strings into plain text
209 * @param relaxedIdmatching
210 * - when true, ID matches to compound sequence IDs are allowed
211 * @return true if features were added
213 public boolean parse(AlignmentI align,
214 Map<String, FeatureColourI> colours,
215 Map<String, FeatureMatcherSetI> filters, boolean removeHTML,
216 boolean relaxedIdmatching)
218 Map<String, String> gffProps = new HashMap<>();
220 * keep track of any sequences we try to create from the data
222 List<SequenceI> newseqs = new ArrayList<>();
228 String featureGroup = null;
230 while ((line = nextLine()) != null)
232 // skip comments/process pragmas
233 if (line.length() == 0 || line.startsWith("#"))
235 if (line.toLowerCase().startsWith("##"))
237 processGffPragma(line, gffProps, align, newseqs);
242 gffColumns = line.split(TAB_REGEX);
243 if (gffColumns.length == 1)
245 if (line.trim().equalsIgnoreCase("GFF"))
248 * Jalview features file with appended GFF
249 * assume GFF2 (though it may declare ##gff-version 3)
256 if (gffColumns.length > 0 && gffColumns.length < 4)
259 * if 2 or 3 tokens, we anticipate either 'startgroup', 'endgroup' or
260 * a feature type colour specification
262 String ft = gffColumns[0];
263 if (ft.equalsIgnoreCase(STARTFILTERS))
265 parseFilters(filters);
268 if (ft.equalsIgnoreCase(STARTGROUP))
270 featureGroup = gffColumns[1];
272 else if (ft.equalsIgnoreCase(ENDGROUP))
274 // We should check whether this is the current group,
275 // but at present there's no way of showing more than 1 group
280 String colscheme = gffColumns[1];
281 FeatureColourI colour = FeatureColour
282 .parseJalviewFeatureColour(colscheme);
285 colours.put(ft, colour);
292 * if not a comment, GFF pragma, startgroup, endgroup or feature
293 * colour specification, that just leaves a feature details line
294 * in either Jalview or GFF format
298 parseJalviewFeature(line, gffColumns, align, colours, removeHTML,
299 relaxedIdmatching, featureGroup);
303 parseGff(gffColumns, align, relaxedIdmatching, newseqs);
307 } catch (Exception ex)
309 // should report somewhere useful for UI if necessary
310 warningMessage = ((warningMessage == null) ? "" : warningMessage)
311 + "Parsing error at\n" + line;
312 System.out.println("Error parsing feature file: " + ex + "\n" + line);
313 ex.printStackTrace(System.err);
319 * experimental - add any dummy sequences with features to the alignment
320 * - we need them for Ensembl feature extraction - though maybe not otherwise
322 for (SequenceI newseq : newseqs)
324 if (newseq.getFeatures().hasFeatures())
326 align.addSequence(newseq);
333 * Reads input lines from STARTFILTERS to ENDFILTERS and adds a feature type
334 * filter to the map for each line parsed. After exit from this method,
335 * nextLine() should return the line after ENDFILTERS (or we are already at
336 * end of file if ENDFILTERS was missing).
339 * @throws IOException
341 protected void parseFilters(Map<String, FeatureMatcherSetI> filters)
345 while ((line = nextLine()) != null)
347 if (line.toUpperCase().startsWith(ENDFILTERS))
351 String[] tokens = line.split(TAB_REGEX);
352 if (tokens.length != 2)
354 System.err.println(String.format("Invalid token count %d for %d",
355 tokens.length, line));
359 String featureType = tokens[0];
360 FeatureMatcherSetI fm = FeatureMatcherSet.fromString(tokens[1]);
361 if (fm != null && filters != null)
363 filters.put(featureType, fm);
370 * Try to parse a Jalview format feature specification and add it as a
371 * sequence feature to any matching sequences in the alignment. Returns true
372 * if successful (a feature was added), or false if not.
377 * @param featureColours
379 * @param relaxedIdmatching
380 * @param featureGroup
382 protected boolean parseJalviewFeature(String line, String[] gffColumns,
383 AlignmentI alignment, Map<String, FeatureColourI> featureColours,
384 boolean removeHTML, boolean relaxedIdMatching,
388 * tokens: description seqid seqIndex start end type [score]
390 if (gffColumns.length < 6)
392 System.err.println("Ignoring feature line '" + line
393 + "' with too few columns (" + gffColumns.length + ")");
396 String desc = gffColumns[0];
397 String seqId = gffColumns[1];
398 SequenceI seq = findSequence(seqId, alignment, null, relaxedIdMatching);
400 if (!ID_NOT_SPECIFIED.equals(seqId))
402 seq = findSequence(seqId, alignment, null, relaxedIdMatching);
408 String seqIndex = gffColumns[2];
411 int idx = Integer.parseInt(seqIndex);
412 seq = alignment.getSequenceAt(idx);
413 } catch (NumberFormatException ex)
415 System.err.println("Invalid sequence index: " + seqIndex);
421 System.out.println("Sequence not found: " + line);
425 int startPos = Integer.parseInt(gffColumns[3]);
426 int endPos = Integer.parseInt(gffColumns[4]);
428 String ft = gffColumns[5];
430 if (!featureColours.containsKey(ft))
433 * Perhaps an old style groups file with no colours -
434 * synthesize a colour from the feature type
436 Color colour = ColorUtils.createColourFromName(ft);
437 featureColours.put(ft, new FeatureColour(colour));
439 SequenceFeature sf = null;
440 if (gffColumns.length > 6)
442 float score = Float.NaN;
445 score = new Float(gffColumns[6]).floatValue();
446 } catch (NumberFormatException ex)
448 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
450 sf = new SequenceFeature(ft, desc, startPos, endPos, score,
455 sf = new SequenceFeature(ft, desc, startPos, endPos, featureGroup);
458 parseDescriptionHTML(sf, removeHTML);
460 seq.addSequenceFeature(sf);
463 && (seq = alignment.findName(seq, seqId, false)) != null)
465 seq.addSequenceFeature(new SequenceFeature(sf));
471 * clear any temporary handles used to speed up ID matching
473 protected void resetMatcher()
475 lastmatchedAl = null;
480 * Returns a sequence matching the given id, as follows
482 * <li>strict matching is on exact sequence name</li>
483 * <li>relaxed matching allows matching on a token within the sequence name,
485 * <li>first tries to find a match in the alignment sequences</li>
486 * <li>else tries to find a match in the new sequences already generated while
487 * parsing the features file</li>
488 * <li>else creates a new placeholder sequence, adds it to the new sequences
489 * list, and returns it</li>
495 * @param relaxedIdMatching
499 protected SequenceI findSequence(String seqId, AlignmentI align,
500 List<SequenceI> newseqs, boolean relaxedIdMatching)
502 // TODO encapsulate in SequenceIdMatcher, share the matcher
503 // with the GffHelper (removing code duplication)
504 SequenceI match = null;
505 if (relaxedIdMatching)
507 if (lastmatchedAl != align)
509 lastmatchedAl = align;
510 matcher = new SequenceIdMatcher(align.getSequencesArray());
513 matcher.addAll(newseqs);
516 match = matcher.findIdMatch(seqId);
520 match = align.findName(seqId, true);
521 if (match == null && newseqs != null)
523 for (SequenceI m : newseqs)
525 if (seqId.equals(m.getName()))
533 if (match == null && newseqs != null)
535 match = new SequenceDummy(seqId);
536 if (relaxedIdMatching)
538 matcher.addAll(Arrays.asList(new SequenceI[] { match }));
540 // add dummy sequence to the newseqs list
546 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
548 if (sf.getDescription() == null)
552 ParseHtmlBodyAndLinks parsed = new ParseHtmlBodyAndLinks(
553 sf.getDescription(), removeHTML, newline);
557 sf.setDescription(parsed.getNonHtmlContent());
560 for (String link : parsed.getLinks())
567 * Returns contents of a Jalview format features file, for visible features, as
568 * filtered by type and group. Features with a null group are displayed if their
569 * feature type is visible. Non-positional features may optionally be included
570 * (with no check on type or group).
575 * map of colour for each visible feature type
576 * @param featureFilters
577 * @param visibleFeatureGroups
578 * @param includeNonPositional
579 * if true, include non-positional features (regardless of group or
583 public String printJalviewFormat(SequenceI[] sequences,
584 Map<String, FeatureColourI> visible,
585 Map<String, FeatureMatcherSetI> featureFilters,
586 List<String> visibleFeatureGroups, boolean includeNonPositional)
588 if (!includeNonPositional && (visible == null || visible.isEmpty()))
590 // no point continuing.
591 return "No Features Visible";
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);
601 for (Entry<String, FeatureColourI> featureColour : visible.entrySet())
603 FeatureColourI colour = featureColour.getValue();
604 out.append(colour.toJalviewFormat(featureColour.getKey())).append(
609 String[] types = visible == null ? new String[0] : visible.keySet()
610 .toArray(new String[visible.keySet().size()]);
613 * feature filters if any
615 outputFeatureFilters(out, visible, featureFilters);
618 * sort groups alphabetically, and ensure that features with a
619 * null or empty group are output after those in named groups
621 List<String> sortedGroups = new ArrayList<>(visibleFeatureGroups);
622 sortedGroups.remove(null);
623 sortedGroups.remove("");
624 Collections.sort(sortedGroups);
625 sortedGroups.add(null);
626 sortedGroups.add("");
628 boolean foundSome = false;
631 * first output any non-positional features
633 if (includeNonPositional)
635 for (int i = 0; i < sequences.length; i++)
637 String sequenceName = sequences[i].getName();
638 for (SequenceFeature feature : sequences[i].getFeatures()
639 .getNonPositionalFeatures())
642 out.append(formatJalviewFeature(sequenceName, feature));
648 * positional features within groups
650 foundSome |= outputFeaturesByGroup(out, sortedGroups, types, sequences);
652 return foundSome ? out.toString() : "No Features Visible";
656 * Outputs any feature filters defined for visible feature types, sandwiched by
657 * STARTFILTERS and ENDFILTERS lines
661 * @param featureFilters
663 void outputFeatureFilters(StringBuilder out,
664 Map<String, FeatureColourI> visible,
665 Map<String, FeatureMatcherSetI> featureFilters)
667 if (visible == null || featureFilters == null
668 || featureFilters.isEmpty())
673 boolean first = true;
674 for (String featureType : visible.keySet())
676 FeatureMatcherSetI filter = featureFilters.get(featureType);
682 out.append(newline).append(STARTFILTERS).append(newline);
684 out.append(featureType).append(TAB).append(filter.toStableString())
690 out.append(ENDFILTERS).append(newline).append(newline);
696 * Appends output of sequence features within feature groups to the output
697 * buffer. Groups other than the null or empty group are sandwiched by
698 * STARTGROUP and ENDGROUP lines.
702 * @param featureTypes
706 private boolean outputFeaturesByGroup(StringBuilder out,
707 List<String> groups, String[] featureTypes, SequenceI[] sequences)
709 boolean foundSome = false;
710 for (String group : groups)
712 boolean isNamedGroup = (group != null && !"".equals(group));
716 out.append(STARTGROUP).append(TAB);
722 * output positional features within groups
724 for (int i = 0; i < sequences.length; i++)
726 String sequenceName = sequences[i].getName();
727 List<SequenceFeature> features = new ArrayList<>();
728 if (featureTypes.length > 0)
730 features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
731 true, group, featureTypes));
734 for (SequenceFeature sequenceFeature : features)
737 out.append(formatJalviewFeature(sequenceName, sequenceFeature));
743 out.append(ENDGROUP).append(TAB);
753 * @param sequenceName
754 * @param sequenceFeature
756 protected String formatJalviewFeature(
757 String sequenceName, SequenceFeature sequenceFeature)
759 StringBuilder out = new StringBuilder(64);
760 if (sequenceFeature.description == null
761 || sequenceFeature.description.equals(""))
763 out.append(sequenceFeature.type).append(TAB);
767 if (sequenceFeature.links != null
768 && sequenceFeature.getDescription().indexOf("<html>") == -1)
770 out.append("<html>");
773 out.append(sequenceFeature.description);
774 if (sequenceFeature.links != null)
776 for (int l = 0; l < sequenceFeature.links.size(); l++)
778 String label = sequenceFeature.links.elementAt(l);
779 String href = label.substring(label.indexOf("|") + 1);
780 label = label.substring(0, label.indexOf("|"));
782 if (sequenceFeature.description.indexOf(href) == -1)
784 out.append(" <a href=\"" + href + "\">" + label + "</a>");
788 if (sequenceFeature.getDescription().indexOf("</html>") == -1)
790 out.append("</html>");
796 out.append(sequenceName);
797 out.append("\t-1\t");
798 out.append(sequenceFeature.begin);
800 out.append(sequenceFeature.end);
802 out.append(sequenceFeature.type);
803 if (!Float.isNaN(sequenceFeature.score))
806 out.append(sequenceFeature.score);
810 return out.toString();
814 * Parse method that is called when a GFF file is dragged to the desktop
819 AlignViewportI av = getViewport();
822 if (av.getAlignment() != null)
824 dataset = av.getAlignment().getDataset();
828 // working in the applet context ?
829 dataset = av.getAlignment();
834 dataset = new Alignment(new SequenceI[] {});
837 Map<String, FeatureColourI> featureColours = new HashMap<>();
838 boolean parseResult = parse(dataset, featureColours, false, true);
841 // pass error up somehow
845 // update viewport with the dataset data ?
849 setSeqs(dataset.getSequencesArray());
854 * Implementation of unused abstract method
856 * @return error message
859 public String print(SequenceI[] sqs, boolean jvsuffix)
861 System.out.println("Use printGffFormat() or printJalviewFormat()");
866 * Returns features output in GFF2 format
869 * the sequences whose features are to be output
871 * a map whose keys are the type names of visible features
872 * @param visibleFeatureGroups
873 * @param includeNonPositionalFeatures
876 public String printGffFormat(SequenceI[] sequences,
877 Map<String, FeatureColourI> visible,
878 List<String> visibleFeatureGroups,
879 boolean includeNonPositionalFeatures)
881 StringBuilder out = new StringBuilder(256);
883 out.append(String.format("%s %d\n", GFF_VERSION, gffVersion == 0 ? 2 : gffVersion));
885 if (!includeNonPositionalFeatures
886 && (visible == null || visible.isEmpty()))
888 return out.toString();
891 String[] types = visible == null ? new String[0] : visible.keySet()
893 new String[visible.keySet().size()]);
895 for (SequenceI seq : sequences)
897 List<SequenceFeature> features = new ArrayList<>();
898 if (includeNonPositionalFeatures)
900 features.addAll(seq.getFeatures().getNonPositionalFeatures());
902 if (visible != null && !visible.isEmpty())
904 features.addAll(seq.getFeatures().getPositionalFeatures(types));
907 for (SequenceFeature sf : features)
909 String source = sf.featureGroup;
910 if (!sf.isNonPositional() && source != null
911 && !visibleFeatureGroups.contains(source))
913 // group is not visible
919 source = sf.getDescription();
922 out.append(seq.getName());
928 out.append(sf.begin);
932 out.append(sf.score);
935 int strand = sf.getStrand();
936 out.append(strand == 1 ? "+" : (strand == -1 ? "-" : "."));
939 String phase = sf.getPhase();
940 out.append(phase == null ? "." : phase);
942 // miscellaneous key-values (GFF column 9)
943 String attributes = sf.getAttributes();
944 if (attributes != null)
946 out.append(TAB).append(attributes);
953 return out.toString();
957 * Returns a mapping given list of one or more Align descriptors (exonerate
960 * @param alignedRegions
961 * a list of "Align fromStart toStart fromCount"
962 * @param mapIsFromCdna
963 * if true, 'from' is dna, else 'from' is protein
965 * either 1 (forward) or -1 (reverse)
967 * @throws IOException
969 protected MapList constructCodonMappingFromAlign(
970 List<String> alignedRegions, boolean mapIsFromCdna, int strand)
975 throw new IOException(
976 "Invalid strand for a codon mapping (cannot be 0)");
978 int regions = alignedRegions.size();
979 // arrays to hold [start, end] for each aligned region
980 int[] fromRanges = new int[regions * 2]; // from dna
981 int[] toRanges = new int[regions * 2]; // to protein
982 int fromRangesIndex = 0;
983 int toRangesIndex = 0;
985 for (String range : alignedRegions)
988 * Align mapFromStart mapToStart mapFromCount
989 * e.g. if mapIsFromCdna
990 * Align 11270 143 120
992 * 120 bases from pos 11270 align to pos 143 in peptide
993 * if !mapIsFromCdna this would instead be
996 String[] tokens = range.split(" ");
997 if (tokens.length != 3)
999 throw new IOException("Wrong number of fields for Align");
1006 fromStart = Integer.parseInt(tokens[0]);
1007 toStart = Integer.parseInt(tokens[1]);
1008 fromCount = Integer.parseInt(tokens[2]);
1009 } catch (NumberFormatException nfe)
1011 throw new IOException(
1012 "Invalid number in Align field: " + nfe.getMessage());
1016 * Jalview always models from dna to protein, so adjust values if the
1017 * GFF mapping is from protein to dna
1022 int temp = fromStart;
1023 fromStart = toStart;
1026 fromRanges[fromRangesIndex++] = fromStart;
1027 fromRanges[fromRangesIndex++] = fromStart + strand * (fromCount - 1);
1030 * If a codon has an intron gap, there will be contiguous 'toRanges';
1031 * this is handled for us by the MapList constructor.
1032 * (It is not clear that exonerate ever generates this case)
1034 toRanges[toRangesIndex++] = toStart;
1035 toRanges[toRangesIndex++] = toStart + (fromCount - 1) / 3;
1038 return new MapList(fromRanges, toRanges, 3, 1);
1042 * Parse a GFF format feature. This may include creating a 'dummy' sequence to
1043 * hold the feature, or for its mapped sequence, or both, to be resolved
1044 * either later in the GFF file (##FASTA section), or when the user loads
1045 * additional sequences.
1049 * @param relaxedIdMatching
1053 protected SequenceI parseGff(String[] gffColumns, AlignmentI alignment,
1054 boolean relaxedIdMatching, List<SequenceI> newseqs)
1057 * GFF: seqid source type start end score strand phase [attributes]
1059 if (gffColumns.length < 5)
1061 System.err.println("Ignoring GFF feature line with too few columns ("
1062 + gffColumns.length + ")");
1067 * locate referenced sequence in alignment _or_
1068 * as a forward or external reference (SequenceDummy)
1070 String seqId = gffColumns[0];
1071 SequenceI seq = findSequence(seqId, alignment, newseqs,
1074 SequenceFeature sf = null;
1075 GffHelperI helper = GffHelperFactory.getHelper(gffColumns);
1080 sf = helper.processGff(seq, gffColumns, alignment, newseqs,
1084 seq.addSequenceFeature(sf);
1085 while ((seq = alignment.findName(seq, seqId, true)) != null)
1087 seq.addSequenceFeature(new SequenceFeature(sf));
1090 } catch (IOException e)
1092 System.err.println("GFF parsing failed with: " + e.getMessage());
1101 * Process the 'column 9' data of the GFF file. This is less formally defined,
1102 * and its interpretation will vary depending on the tool that has generated
1108 protected void processGffColumnNine(String attributes, SequenceFeature sf)
1110 sf.setAttributes(attributes);
1113 * Parse attributes in column 9 and add them to the sequence feature's
1114 * 'otherData' table; use Note as a best proxy for description
1116 char nameValueSeparator = gffVersion == 3 ? '=' : ' ';
1117 // TODO check we don't break GFF2 values which include commas here
1118 Map<String, List<String>> nameValues = GffHelperBase
1119 .parseNameValuePairs(attributes, ";", nameValueSeparator, ",");
1120 for (Entry<String, List<String>> attr : nameValues.entrySet())
1122 String values = StringUtils.listToDelimitedString(attr.getValue(),
1124 sf.setValue(attr.getKey(), values);
1125 if (NOTE.equals(attr.getKey()))
1127 sf.setDescription(values);
1133 * After encountering ##fasta in a GFF3 file, process the remainder of the
1134 * file as FAST sequence data. Any placeholder sequences created during
1135 * feature parsing are updated with the actual sequences.
1139 * @throws IOException
1141 protected void processAsFasta(AlignmentI align, List<SequenceI> newseqs)
1147 } catch (IOException q)
1150 FastaFile parser = new FastaFile(this);
1151 List<SequenceI> includedseqs = parser.getSeqs();
1153 SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
1156 * iterate over includedseqs, and replacing matching ones with newseqs
1157 * sequences. Generic iterator not used here because we modify
1158 * includedseqs as we go
1160 for (int p = 0, pSize = includedseqs.size(); p < pSize; p++)
1162 // search for any dummy seqs that this sequence can be used to update
1163 SequenceI includedSeq = includedseqs.get(p);
1164 SequenceI dummyseq = smatcher.findIdMatch(includedSeq);
1165 if (dummyseq != null && dummyseq instanceof SequenceDummy)
1167 // probably have the pattern wrong
1168 // idea is that a flyweight proxy for a sequence ID can be created for
1169 // 1. stable reference creation
1170 // 2. addition of annotation
1171 // 3. future replacement by a real sequence
1172 // current pattern is to create SequenceDummy objects - a convenience
1173 // constructor for a Sequence.
1174 // problem is that when promoted to a real sequence, all references
1175 // need to be updated somehow. We avoid that by keeping the same object.
1176 ((SequenceDummy) dummyseq).become(includedSeq);
1177 dummyseq.createDatasetSequence();
1180 * Update mappings so they are now to the dataset sequence
1182 for (AlignedCodonFrame mapping : align.getCodonFrames())
1184 mapping.updateToDataset(dummyseq);
1188 * replace parsed sequence with the realised forward reference
1190 includedseqs.set(p, dummyseq);
1193 * and remove from the newseqs list
1195 newseqs.remove(dummyseq);
1200 * finally add sequences to the dataset
1202 for (SequenceI seq : includedseqs)
1204 // experimental: mapping-based 'alignment' to query sequence
1205 AlignmentUtils.alignSequenceAs(seq, align,
1206 String.valueOf(align.getGapCharacter()), false, true);
1208 // rename sequences if GFF handler requested this
1209 // TODO a more elegant way e.g. gffHelper.postProcess(newseqs) ?
1210 List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures();
1213 String newName = (String) sfs.get(0).getValue(
1214 GffHelperI.RENAME_TOKEN);
1215 if (newName != null)
1217 seq.setName(newName);
1220 align.addSequence(seq);
1225 * Process a ## directive
1231 * @throws IOException
1233 protected void processGffPragma(String line, Map<String, String> gffProps,
1234 AlignmentI align, List<SequenceI> newseqs) throws IOException
1237 if ("###".equals(line))
1239 // close off any open 'forward references'
1243 String[] tokens = line.substring(2).split(" ");
1244 String pragma = tokens[0];
1245 String value = tokens.length == 1 ? null : tokens[1];
1247 if ("gff-version".equalsIgnoreCase(pragma))
1253 // value may be e.g. "3.1.2"
1254 gffVersion = Integer.parseInt(value.split("\\.")[0]);
1255 } catch (NumberFormatException e)
1261 else if ("sequence-region".equalsIgnoreCase(pragma))
1263 // could capture <seqid start end> if wanted here
1265 else if ("feature-ontology".equalsIgnoreCase(pragma))
1267 // should resolve against the specified feature ontology URI
1269 else if ("attribute-ontology".equalsIgnoreCase(pragma))
1271 // URI of attribute ontology - not currently used in GFF3
1273 else if ("source-ontology".equalsIgnoreCase(pragma))
1275 // URI of source ontology - not currently used in GFF3
1277 else if ("species-build".equalsIgnoreCase(pragma))
1279 // save URI of specific NCBI taxon version of annotations
1280 gffProps.put("species-build", value);
1282 else if ("fasta".equalsIgnoreCase(pragma))
1284 // process the rest of the file as a fasta file and replace any dummy
1286 processAsFasta(align, newseqs);
1290 System.err.println("Ignoring unknown pragma: " + line);