1 package jalview.io.gff;
3 import jalview.datamodel.AlignedCodonFrame;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.MappingType;
6 import jalview.datamodel.SequenceFeature;
7 import jalview.datamodel.SequenceI;
8 import jalview.util.MapList;
9 import jalview.util.StringUtils;
11 import java.io.IOException;
12 import java.util.List;
16 * Base class with generic / common functionality for processing GFF3 data.
17 * Override this as required for any specialisations resulting from
18 * peculiarities of GFF3 generated by particular tools.
20 public class Gff3Helper extends GffHelperBase
22 protected static final String TARGET = "Target";
24 protected static final String ID = "ID";
26 private static final String NAME = "Name";
29 * GFF3 uses '=' to delimit name/value pairs in column 9, and comma to
30 * separate multiple values for a name
35 public static Map<String, List<String>> parseNameValuePairs(String text)
37 return parseNameValuePairs(text, ";", '=', ",");
41 * Process one GFF feature line (as modelled by SequenceFeature)
44 * the sequence with which this feature is associated
46 * the sequence feature with ATTRIBUTES property containing any
47 * additional attributes
49 * the alignment we are adding GFF to
51 * any new sequences referenced by the GFF
52 * @param relaxedIdMatching
53 * if true, match word tokens in sequence names
54 * @return true if the sequence feature should be added to the sequence, else
55 * false (i.e. it has been processed in another way e.g. to generate a
60 public SequenceFeature processGff(SequenceI seq, String[] gff,
61 AlignmentI align, List<SequenceI> newseqs,
62 boolean relaxedIdMatching) throws IOException
64 SequenceFeature sf = null;
68 String soTerm = gff[TYPE_COL];
69 String atts = gff[ATTRIBUTES_COL];
70 Map<String, List<String>> attributes = parseNameValuePairs(atts);
72 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
73 if (so.isA(soTerm, SequenceOntologyI.PROTEIN_MATCH))
75 sf = processProteinMatch(attributes, seq, gff, align, newseqs,
78 else if (so.isA(soTerm, SequenceOntologyI.NUCLEOTIDE_MATCH))
80 sf = processNucleotideMatch(attributes, seq, gff, align, newseqs,
85 sf = buildSequenceFeature(gff, attributes);
91 * fall back on generating a sequence feature with no special processing
93 sf = buildSequenceFeature(gff, null);
100 * Processes one GFF3 nucleotide (e.g. cDNA to genome) match.
103 * parsed GFF column 9 key/value(s)
105 * the sequence the GFF feature is on
107 * the GFF column data
109 * the alignment the sequence belongs to, where any new mappings
112 * a list of new 'virtual sequences' generated while parsing GFF
113 * @param relaxedIdMatching
114 * if true allow fuzzy search for a matching target sequence
115 * @return a sequence feature, if one should be added to the sequence, else
117 * @throws IOException
119 protected SequenceFeature processNucleotideMatch(
120 Map<String, List<String>> attributes, SequenceI seq,
121 String[] gffColumns, AlignmentI align, List<SequenceI> newseqs,
122 boolean relaxedIdMatching) throws IOException
124 String strand = gffColumns[STRAND_COL];
127 * (For now) we don't process mappings from reverse complement ; to do
128 * this would require (a) creating a virtual sequence placeholder for
129 * the reverse complement (b) resolving the sequence by its id from some
130 * source (GFF ##FASTA or other) (c) creating the reverse complement
131 * sequence (d) updating the mapping to be to the reverse complement
133 if ("-".equals(strand))
136 .println("Skipping mapping from reverse complement as not yet supported");
140 List<String> targets = attributes.get(TARGET);
143 System.err.println("'Target' missing in GFF");
148 * Typically we only expect one Target per GFF line, but this can handle
149 * multiple matches, to the same or different sequences (e.g. dna variants)
151 for (String target : targets)
154 * Process "seqid start end [strand]"
156 String[] tokens = target.split(" ");
157 if (tokens.length < 3)
159 System.err.println("Incomplete Target: " + target);
164 * Locate the mapped sequence in the alignment, or as a
165 * (new or existing) virtual sequence in the newseqs list
167 String targetId = findTargetId(tokens[0], attributes);
168 SequenceI mappedSequence1 = findSequence(targetId, align, newseqs,
170 SequenceI mappedSequence = mappedSequence1;
171 if (mappedSequence == null)
177 * get any existing mapping for these sequences (or start one),
178 * and add this mapped range
180 AlignedCodonFrame acf = getMapping(align, seq, mappedSequence);
184 int toStart = Integer.parseInt(tokens[1]);
185 int toEnd = Integer.parseInt(tokens[2]);
186 if (tokens.length > 3 && "-".equals(tokens[3]))
188 // mapping to reverse strand - swap start/end
194 int fromStart = Integer.parseInt(gffColumns[START_COL]);
195 int fromEnd = Integer.parseInt(gffColumns[END_COL]);
196 MapList mapping = constructMappingFromAlign(fromStart, fromEnd,
197 toStart, toEnd, MappingType.NucleotideToNucleotide);
201 acf.addMap(seq, mappedSequence, mapping);
202 align.addCodonFrame(acf);
204 } catch (NumberFormatException nfe)
206 System.err.println("Invalid start or end in Target " + target);
210 SequenceFeature sf = buildSequenceFeature(gffColumns, attributes);
215 * Returns the target sequence id extracted from the GFF name/value pairs.
216 * Default (standard behaviour) is the first token for "Target". This may be
217 * overridden where tools report this in a non-standard way.
220 * first token of a "Target" value from GFF column 9, typically
223 * a map with all parsed column 9 attributes
226 @SuppressWarnings("unused")
227 protected String findTargetId(String target, Map<String, List<String>> set)
233 * Processes one GFF 'protein_match'; fields of interest are
235 * <li>feature group - the database reporting a match e.g. Pfam</li>
236 * <li>Name - the matched entry's accession id in the database</li>
237 * <li>ID - a sequence identifier for the matched region (which may be
238 * appended as FASTA in the GFF file)</li>
242 * parsed GFF column 9 key/value(s)
244 * the sequence the GFF feature is on
246 * the sequence feature holding GFF data
248 * the alignment the sequence belongs to, where any new mappings
251 * a list of new 'virtual sequences' generated while parsing GFF
252 * @param relaxedIdMatching
253 * if true allow fuzzy search for a matching target sequence
254 * @return the (real or virtual) sequence(s) mapped to by this match
255 * @throws IOException
257 protected SequenceFeature processProteinMatch(
258 Map<String, List<String>> set, SequenceI seq,
259 String[] gffColumns, AlignmentI align, List<SequenceI> newseqs,
260 boolean relaxedIdMatching)
262 // This is currently tailored to InterProScan GFF output:
263 // ID holds the ID of the matched sequence, Target references the
264 // query sequence; this looks wrong, as ID should just be the GFF internal
265 // ID of the GFF feature, while Target would normally reference the matched
267 // TODO refactor as needed if other protein-protein GFF varies
269 SequenceFeature sf = buildSequenceFeature(gffColumns, set);
272 * locate the mapped sequence in the alignment, or as a
273 * (new or existing) virtual sequence in the newseqs list
275 List<String> targets = set.get(TARGET);
278 for (String target : targets)
281 SequenceI mappedSequence1 = findSequence(findTargetId(target, set),
282 align, newseqs, relaxedIdMatching);
283 SequenceI mappedSequence = mappedSequence1;
284 if (mappedSequence == null)
290 * give the mapped sequence a copy of the sequence feature, with
291 * start/end range adjusted
293 SequenceFeature sf2 = new SequenceFeature(sf);
295 int sequenceFeatureLength = 1 + sf.getEnd() - sf.getBegin();
296 sf2.setEnd(sequenceFeatureLength);
297 mappedSequence.addSequenceFeature(sf2);
300 * add a property to the mapped sequence so that it can eventually be
301 * renamed with its qualified accession id; renaming has to wait until
302 * all sequence reference resolution is complete
304 String accessionId = StringUtils.listToDelimitedString(
306 if (accessionId.length() > 0)
308 String database = sf.getType(); // TODO InterProScan only??
309 String qualifiedAccId = database + "|" + accessionId;
310 sf2.setValue(RENAME_TOKEN, qualifiedAccId);
314 * get any existing mapping for these sequences (or start one),
315 * and add this mapped range
317 AlignedCodonFrame alco = getMapping(align, seq, mappedSequence);
318 int[] from = new int[] { sf.getBegin(), sf.getEnd() };
319 int[] to = new int[] { 1, sequenceFeatureLength };
320 MapList mapping = new MapList(from, to, 1, 1);
322 alco.addMap(seq, mappedSequence, mapping);
323 align.addCodonFrame(alco);
331 * Return '=' as the name-value separator used in column 9 attributes.
334 protected char getNameValueSeparator()
340 * Modifies the default SequenceFeature in order to set the Target sequence id
344 protected SequenceFeature buildSequenceFeature(String[] gff,
345 Map<String, List<String>> attributes)
347 SequenceFeature sf = super.buildSequenceFeature(gff, attributes);
348 String desc = getDescription(sf, attributes);
351 sf.setDescription(desc);
357 * Apply heuristic rules to try to get the most useful feature description
363 protected String getDescription(SequenceFeature sf,
364 Map<String, List<String>> attributes)
367 String target = (String) sf.getValue(TARGET);
370 desc = target.split(" ")[0];
373 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
374 String type = sf.getType();
375 if (so.isA(type, SequenceOntologyI.SEQUENCE_VARIANT))
378 * Ensembl returns dna variants as 'alleles'
380 desc = StringUtils.listToDelimitedString(attributes.get("alleles"),
385 * extract 'Name' for a transcript (to show gene name)
386 * or an exon (so 'colour by label' shows exon boundaries)
388 if (SequenceOntologyI.NMD_TRANSCRIPT_VARIANT.equals(type)
389 || so.isA(type, SequenceOntologyI.TRANSCRIPT)
390 || so.isA(type, SequenceOntologyI.EXON))
392 desc = StringUtils.listToDelimitedString(attributes.get("Name"), ",");
396 * if the above fails, try ID
400 desc = (String) sf.getValue(ID);