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;
10 import java.io.IOException;
11 import java.util.List;
15 * A handler to parse GFF in the format generated by the exonerate tool
17 public class ExonerateHelper extends Gff2Helper
19 private static final String SIMILARITY = "similarity";
21 private static final String GENOME2GENOME = "genome2genome";
23 private static final String CDNA2GENOME = "cdna2genome";
25 private static final String CODING2GENOME = "coding2genome";
27 private static final String CODING2CODING = "coding2coding";
29 private static final String PROTEIN2GENOME = "protein2genome";
31 private static final String PROTEIN2DNA = "protein2dna";
33 private static final String ALIGN = "Align";
35 private static final String QUERY = "Query";
37 private static final String TARGET = "Target";
40 * Process one GFF feature line (as modelled by SequenceFeature)
43 * the sequence with which this feature is associated
45 * the sequence feature with ATTRIBUTES property containing any
46 * additional attributes
48 * the alignment we are adding GFF to
50 * any new sequences referenced by the GFF
51 * @param relaxedIdMatching
52 * if true, match word tokens in sequence names
53 * @return true if the sequence feature should be added to the sequence, else
54 * false (i.e. it has been processed in another way e.g. to generate a
58 public SequenceFeature processGff(SequenceI seq, String[] gffColumns,
59 AlignmentI align, List<SequenceI> newseqs,
60 boolean relaxedIdMatching)
62 String attr = gffColumns[ATTRIBUTES_COL];
63 Map<String, List<String>> set = parseNameValuePairs(attr);
67 processGffSimilarity(set, seq, gffColumns, align, newseqs,
69 } catch (IOException ivfe)
71 System.err.println(ivfe);
75 * return null to indicate we don't want to add a sequence feature for
76 * similarity (only process it to create mappings)
82 * Processes the 'Query' (or 'Target') and 'Align' properties associated with
83 * an exonerate GFF similarity feature; these properties define the mapping of
84 * the annotated range to a related sequence.
87 * parsed GFF column 9 key/value(s)
89 * the sequence the GFF feature is on
93 * the alignment the sequence belongs to, where any new mappings
96 * a list of new 'virtual sequences' generated while parsing GFF
97 * @param relaxedIdMatching
98 * if true allow fuzzy search for a matching target sequence
101 protected void processGffSimilarity(Map<String, List<String>> set,
102 SequenceI seq, String[] gff, AlignmentI align,
103 List<SequenceI> newseqs, boolean relaxedIdMatching)
107 * exonerate may be run with
108 * --showquerygff - outputs 'features on the query' e.g. (protein2genome)
109 * Target <dnaseqid> ; Align proteinStartPos dnaStartPos proteinCount
110 * --showtargetgff - outputs 'features on the target' e.g. (protein2genome)
111 * Query <proteinseqid> ; Align dnaStartPos proteinStartPos nucleotideCount
112 * where the Align spec may repeat
114 // TODO handle coding2coding and similar as well
115 boolean featureIsOnTarget = true;
116 List<String> mapTo = set.get(QUERY);
119 mapTo = set.get(TARGET);
120 featureIsOnTarget = false;
122 MappingType type = getMappingType(gff[SOURCE_COL]);
126 throw new IOException("Sorry, I don't handle " + gff[SOURCE_COL]);
129 if (mapTo == null || mapTo.size() != 1)
131 throw new IOException(
132 "Expecting exactly one sequence in Query or Target field (got "
137 * locate the mapped sequence in the alignment or 'new' (GFF file) sequences;
139 SequenceI mappedSequence = findSequence(mapTo.get(0), align, newseqs,
143 * If mapping is from protein to dna, we store it as dna to protein instead
145 SequenceI mapFromSequence = seq;
146 SequenceI mapToSequence = mappedSequence;
147 if ((type == MappingType.NucleotideToPeptide && featureIsOnTarget)
148 || (type == MappingType.PeptideToNucleotide && !featureIsOnTarget))
150 mapFromSequence = mappedSequence;
155 * Process the Align maps and create mappings.
156 * These may be cdna-genome, cdna-protein, genome-protein.
157 * The mapped sequences may or may not be in the alignment
158 * (they may be included later in the GFF file).
162 * get any existing mapping for these sequences (or start one),
163 * and add this mapped range
165 AlignedCodonFrame acf = getMapping(align, mapFromSequence,
169 * exonerate GFF has the strand of the target in column 7
170 * (differs from GFF3 which has it in the Target descriptor)
172 String strand = gff[STRAND_COL];
173 boolean forwardStrand = true;
174 if ("-".equals(strand))
176 forwardStrand = false;
178 else if (!"+".equals(strand))
180 System.err.println("Strand must be specified for alignment");
184 List<String> alignedRegions = set.get(ALIGN);
185 for (String region : alignedRegions)
187 MapList mapping = buildMapping(region, type, forwardStrand,
188 featureIsOnTarget, gff);
195 acf.addMap(mapFromSequence, mapToSequence, mapping);
197 align.addCodonFrame(acf);
201 * Construct the mapping
205 * @param forwardStrand
206 * @param featureIsOnTarget
210 protected MapList buildMapping(String region, MappingType type,
211 boolean forwardStrand, boolean featureIsOnTarget, String[] gff)
214 * process one "fromStart toStart fromCount" descriptor
216 String[] tokens = region.split(" ");
217 if (tokens.length != 3)
219 System.err.println("Malformed Align descriptor: " + region);
224 * get start/end of from/to mappings
225 * if feature is on the target sequence we have to invert the sense
232 alignFromStart = Integer.parseInt(tokens[0]);
233 alignToStart = Integer.parseInt(tokens[1]);
234 alignCount = Integer.parseInt(tokens[2]);
235 } catch (NumberFormatException nfe)
237 System.err.println(nfe.toString());
246 if (featureIsOnTarget)
248 fromStart = alignToStart;
249 toStart = alignFromStart;
250 toEnd = forwardStrand ? toStart + alignCount - 1 : toStart
252 int toLength = Math.abs(toEnd - toStart) + 1;
253 int fromLength = toLength * type.getFromRatio() / type.getToRatio();
254 fromEnd = fromStart + fromLength - 1;
258 // we use the 'Align' values here not the feature start/end
259 // not clear why they may differ but it seems they can
260 fromStart = alignFromStart;
261 fromEnd = alignFromStart + alignCount - 1;
262 int fromLength = fromEnd - fromStart + 1;
263 int toLength = fromLength * type.getToRatio() / type.getFromRatio();
264 toStart = alignToStart;
267 toEnd = toStart + toLength - 1;
271 toEnd = toStart - (toLength - 1);
275 MapList codonmapping = constructMappingFromAlign(fromStart, fromEnd,
276 toStart, toEnd, type);
281 * Returns a MappingType depending on the exonerate 'model' value.
286 protected static MappingType getMappingType(String model)
288 MappingType result = null;
290 if (model.contains(PROTEIN2DNA) || model.contains(PROTEIN2GENOME))
292 result = MappingType.PeptideToNucleotide;
294 else if (model.contains(CODING2CODING) || model.contains(CODING2GENOME)
295 || model.contains(CDNA2GENOME) || model.contains(GENOME2GENOME))
297 result = MappingType.NucleotideToNucleotide;
303 * Tests whether the GFF data looks like it was generated by exonerate, and is
304 * a format we are willing to handle
309 public static boolean recognises(String[] columns)
311 if (!SIMILARITY.equalsIgnoreCase(columns[TYPE_COL]))
317 * inspect alignment model
319 String model = columns[SOURCE_COL];
320 // e.g. exonerate:protein2genome:local
323 String mdl = model.toLowerCase();
324 if (mdl.contains(PROTEIN2DNA) || mdl.contains(PROTEIN2GENOME)
325 || mdl.contains(CODING2CODING) || mdl.contains(CODING2GENOME)
326 || mdl.contains(CDNA2GENOME) || mdl.contains(GENOME2GENOME))
331 System.err.println("Sorry, I don't handle exonerate model " + model);
336 protected SequenceFeature buildSequenceFeature(String[] gff,
337 Map<String, List<String>> set)
339 SequenceFeature sf = super.buildSequenceFeature(gff, set);
340 sf.setFeatureGroup("exonerate");