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.
21 package jalview.io.gff;
23 import jalview.analysis.SequenceIdMatcher;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.MappingType;
27 import jalview.datamodel.SequenceDummy;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.util.MapList;
31 import jalview.util.StringUtils;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Map.Entry;
41 * Base class with common functionality for flavours of GFF handler (GFF2 or
44 public abstract class GffHelperBase implements GffHelperI
46 protected static final String COMMA = ",";
48 protected static final String EQUALS = "=";
50 protected static final String NOTE = "Note";
53 * GFF columns 1-9 (zero-indexed):
55 protected static final int SEQID_COL = 0;
57 protected static final int SOURCE_COL = 1;
59 protected static final int TYPE_COL = 2;
61 protected static final int START_COL = 3;
63 protected static final int END_COL = 4;
65 protected static final int SCORE_COL = 5;
67 protected static final int STRAND_COL = 6;
69 protected static final int PHASE_COL = 7;
71 protected static final int ATTRIBUTES_COL = 8;
73 private AlignmentI lastmatchedAl = null;
75 private SequenceIdMatcher matcher = null;
78 * Constructs and returns a mapping, or null if data appear invalid
85 * type of mapping (e.g. protein to nucleotide)
88 protected MapList constructMappingFromAlign(int fromStart, int fromEnd,
89 int toStart, int toEnd, MappingType mappingType)
91 int[] from = new int[] { fromStart, fromEnd };
92 int[] to = new int[] { toStart, toEnd };
95 * Jalview always models from dna to protein, so switch values if the
96 * GFF mapping is from protein to dna
98 if (mappingType == MappingType.PeptideToNucleotide)
103 mappingType = mappingType.getInverse();
106 int fromRatio = mappingType.getFromRatio();
107 int toRatio = mappingType.getToRatio();
110 * sanity check that mapped residue counts match
111 * TODO understand why PASA generates such cases...
113 if (!trimMapping(from, to, fromRatio, toRatio))
115 System.err.println("Ignoring mapping from " + Arrays.toString(from)
116 + " to " + Arrays.toString(to) + " as counts don't match!");
121 * If a codon has an intron gap, there will be contiguous 'toRanges';
122 * this is handled for us by the MapList constructor.
123 * (It is not clear that exonerate ever generates this case)
126 return new MapList(from, to, fromRatio, toRatio);
130 * Checks that the 'from' and 'to' ranges have equivalent lengths. If not,
131 * tries to trim the end of the longer so they do. Returns true if the
132 * mappings could be made equivalent, else false. Note the range array values
133 * may be modified by this method.
141 protected static boolean trimMapping(int[] from, int[] to, int fromRatio,
144 int fromLength = Math.abs(from[1] - from[0]) + 1;
145 int toLength = Math.abs(to[1] - to[0]) + 1;
146 int fromOverlap = fromLength * toRatio - toLength * fromRatio;
147 if (fromOverlap == 0)
151 if (fromOverlap > 0 && fromOverlap % toRatio == 0)
154 * restrict from range to make them match up
155 * it's kind of arbitrary which end we truncate - here it is the end
158 "Truncating mapping from " + Arrays.toString(from) + " to ");
159 if (from[1] > from[0])
161 from[1] -= fromOverlap / toRatio;
165 from[1] += fromOverlap / toRatio;
167 System.err.println(Arrays.toString(from));
170 else if (fromOverlap < 0 && fromOverlap % fromRatio == 0)
172 fromOverlap = -fromOverlap; // > 0
174 * restrict to range to make them match up
177 "Truncating mapping to " + Arrays.toString(to) + " to ");
180 to[1] -= fromOverlap / fromRatio;
184 to[1] += fromOverlap / fromRatio;
186 System.err.println(Arrays.toString(to));
191 * Couldn't truncate to an exact match..
197 * Returns a sequence matching the given id, as follows
199 * <li>strict matching is on exact sequence name</li>
200 * <li>relaxed matching allows matching on a token within the sequence name,
202 * <li>first tries to find a match in the alignment sequences</li>
203 * <li>else tries to find a match in the new sequences already generated while
204 * parsing the features file</li>
205 * <li>else creates a new placeholder sequence, adds it to the new sequences
206 * list, and returns it</li>
212 * @param relaxedIdMatching
216 protected SequenceI findSequence(String seqId, AlignmentI align,
217 List<SequenceI> newseqs, boolean relaxedIdMatching)
223 SequenceI match = null;
224 if (relaxedIdMatching)
226 if (lastmatchedAl != align)
228 lastmatchedAl = align;
229 matcher = new SequenceIdMatcher(align.getSequencesArray());
232 matcher.addAll(newseqs);
235 match = matcher.findIdMatch(seqId);
239 match = align.findName(seqId, true);
240 if (match == null && newseqs != null)
242 for (SequenceI m : newseqs)
244 if (seqId.equals(m.getName()))
252 if (match == null && newseqs != null)
254 match = new SequenceDummy(seqId);
255 if (relaxedIdMatching)
257 matcher.addAll(Arrays.asList(new SequenceI[] { match }));
259 // add dummy sequence to the newseqs list
266 * Parses the input line to a map of name / value(s) pairs. For example the
270 * Notes=Fe-S;Method=manual curation, prediction; source = Pfam; Notes = Metal
273 * if parsed with delimiter=";" and separators {' ', '='} <br>
274 * would return a map with { Notes={Fe=S, Metal}, Method={manual curation,
275 * prediction}, source={Pfam}} <br>
277 * This method supports parsing of either GFF2 format (which uses space ' ' as
278 * the name/value delimiter, and allows multiple occurrences of the same
279 * name), or GFF3 format (which uses '=' as the name/value delimiter, and
280 * strictly does not allow repeat occurrences of the same name - but does
281 * allow a comma-separated list of values).
283 * Returns a (possibly empty) map of lists of values by attribute name.
286 * @param namesDelimiter
287 * the major delimiter between name-value pairs
288 * @param nameValueSeparator
289 * separator used between name and value
290 * @param valuesDelimiter
291 * delimits a list of more than one value
294 public static Map<String, List<String>> parseNameValuePairs(String text,
295 String namesDelimiter, char nameValueSeparator,
296 String valuesDelimiter)
298 Map<String, List<String>> map = new HashMap<>();
299 if (text == null || text.trim().length() == 0)
305 * split by major delimiter (; for GFF3)
307 for (String nameValuePair : text.trim().split(namesDelimiter))
309 nameValuePair = nameValuePair.trim();
310 if (nameValuePair.length() == 0)
316 * find name/value separator (= for GFF3)
318 int sepPos = nameValuePair.indexOf(nameValueSeparator);
321 // no name=value found
325 String name = nameValuePair.substring(0, sepPos).trim();
326 String values = nameValuePair.substring(sepPos + 1).trim();
327 if (values.isEmpty())
332 List<String> vals = map.get(name);
335 vals = new ArrayList<>();
340 * if 'values' contains more name/value separators, parse as a map
341 * (nested sub-attribute values)
343 if (values.indexOf(nameValueSeparator) != -1)
349 for (String val : values.split(valuesDelimiter))
360 * Constructs a SequenceFeature from the GFF column data. Subclasses may wish
361 * to call this method then adjust the SequenceFeature depending on the
362 * particular usage of different tools that generate GFF.
368 protected SequenceFeature buildSequenceFeature(String[] gff,
369 Map<String, List<String>> attributes)
371 return buildSequenceFeature(gff, TYPE_COL, gff[SOURCE_COL], attributes);
381 protected SequenceFeature buildSequenceFeature(String[] gff,
382 int typeColumn, String group, Map<String, List<String>> attributes)
386 int start = Integer.parseInt(gff[START_COL]);
387 int end = Integer.parseInt(gff[END_COL]);
390 * default 'score' is 0 rather than Float.NaN - see JAL-2554
395 score = Float.parseFloat(gff[SCORE_COL]);
396 } catch (NumberFormatException nfe)
398 // e.g. '.' - leave as zero
401 SequenceFeature sf = new SequenceFeature(gff[typeColumn],
402 gff[SOURCE_COL], start, end, score, group);
404 sf.setStrand(gff[STRAND_COL]);
406 sf.setPhase(gff[PHASE_COL]);
408 if (attributes != null)
411 * Add attributes in column 9 to the sequence feature's
412 * 'otherData' table; use Note as a best proxy for description;
413 * decode any encoded comma, equals, semi-colon as per GFF3 spec
415 for (Entry<String, List<String>> attr : attributes.entrySet())
417 String key = attr.getKey();
418 List<String> values = attr.getValue();
419 if (values.size() == 1 && values.get(0).contains(EQUALS))
422 * 'value' is actually nested subattributes as x=a,y=b,z=c
424 Map<String, String> valueMap = parseAttributeMap(values.get(0));
425 sf.setValue(key, valueMap);
429 String csvValues = StringUtils.listToDelimitedString(values,
431 csvValues = StringUtils.urlDecode(csvValues, GFF_ENCODABLE);
432 sf.setValue(key, csvValues);
433 if (NOTE.equals(key))
435 sf.setDescription(csvValues);
442 } catch (NumberFormatException nfe)
444 System.err.println("Invalid number in gff: " + nfe.getMessage());
450 * Parses a (GFF3 format) list of comma-separated key=value pairs into a Map
453 * An input string like {@code a=b,c,d=e,f=g,h} is parsed to
465 protected static Map<String, String> parseAttributeMap(String s)
467 Map<String, String> map = new HashMap<>();
468 String[] fields = s.split(EQUALS);
470 while (i < fields.length - 1)
472 boolean lastPair = i == fields.length - 2;
473 String before = fields[i];
474 String after = fields[i + 1];
477 * if 'key' looks like a,b,c then the last token is the
480 String theKey = before.contains(COMMA)
481 ? before.substring(before.lastIndexOf(COMMA) + 1)
485 * if 'value' looks like a,b,c then all but the last token is the value,
486 * unless this is the last field (no more = to follow), in which case
487 * all of it makes up the value
489 String theValue = after.contains(COMMA) && !lastPair
490 ? after.substring(0, after.lastIndexOf(COMMA))
492 map.put(StringUtils.urlDecode(theKey, GFF_ENCODABLE),
493 StringUtils.urlDecode(theValue, GFF_ENCODABLE));
501 * Returns any existing mapping held on the alignment between the given
502 * dataset sequences, or a new one if none found. This is a convenience method
503 * to facilitate processing multiple GFF lines that make up a single 'spliced'
504 * mapping, by extending the first mapping as the others are read.
511 protected AlignedCodonFrame getMapping(AlignmentI align,
512 SequenceI fromSeq, SequenceI toSeq)
514 AlignedCodonFrame acf = align.getMapping(fromSeq, toSeq);
517 acf = new AlignedCodonFrame();