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.datamodel;
23 import jalview.util.MapList;
24 import jalview.util.MappingUtils;
26 import java.util.ArrayList;
27 import java.util.List;
30 * Stores mapping between the columns of a protein alignment and a DNA alignment
31 * and a list of individual codon to amino acid mappings between sequences.
33 public class AlignedCodonFrame
37 * Data bean to hold mappings from one sequence to another
39 private class SequenceToSequenceMapping
41 private SequenceI fromSeq;
43 private Mapping mapping;
45 SequenceToSequenceMapping(SequenceI from, Mapping map)
52 private List<SequenceToSequenceMapping> mappings;
57 public AlignedCodonFrame()
59 mappings = new ArrayList<SequenceToSequenceMapping>();
63 * Adds a mapping between the dataset sequences for the associated dna and
64 * protein sequence objects
70 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
72 // JBPNote DEBUG! THIS !
73 // dnaseq.transferAnnotation(aaseq, mp);
74 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
76 SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
77 : dnaseq.getDatasetSequence();
78 SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
79 .getDatasetSequence();
82 * if we already hold a mapping between these sequences, just add to it
84 for (SequenceToSequenceMapping ssm : mappings)
86 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
88 ssm.mapping.map.addMapList(map);
94 * otherwise, add a new sequence mapping
96 Mapping mp = new Mapping(toSeq, map);
97 mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
100 public SequenceI[] getdnaSeqs()
102 // TODO return a list instead?
104 List<SequenceI> seqs = new ArrayList<SequenceI>();
105 for (SequenceToSequenceMapping ssm : mappings)
107 seqs.add(ssm.fromSeq);
109 return seqs.toArray(new SequenceI[seqs.size()]);
112 public SequenceI[] getAaSeqs()
114 // TODO not used - remove?
115 List<SequenceI> seqs = new ArrayList<SequenceI>();
116 for (SequenceToSequenceMapping ssm : mappings)
118 seqs.add(ssm.mapping.to);
120 return seqs.toArray(new SequenceI[seqs.size()]);
123 public MapList[] getdnaToProt()
125 List<MapList> maps = new ArrayList<MapList>();
126 for (SequenceToSequenceMapping ssm : mappings)
128 maps.add(ssm.mapping.map);
130 return maps.toArray(new MapList[maps.size()]);
133 public Mapping[] getProtMappings()
135 List<Mapping> maps = new ArrayList<Mapping>();
136 for (SequenceToSequenceMapping ssm : mappings)
138 maps.add(ssm.mapping);
140 return maps.toArray(new Mapping[maps.size()]);
144 * Returns the first mapping found which is to or from the given sequence, or
150 public Mapping getMappingForSequence(SequenceI seq)
152 SequenceI seqDs = seq.getDatasetSequence();
153 seqDs = seqDs != null ? seqDs : seq;
155 for (SequenceToSequenceMapping ssm : mappings)
157 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
166 * Return the corresponding aligned or dataset aa sequence for given dna
167 * sequence, null if not found.
172 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
174 SequenceI dnads = dnaSeqRef.getDatasetSequence();
175 for (SequenceToSequenceMapping ssm : mappings)
177 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
179 return ssm.mapping.to;
188 * @return null or corresponding aaSeq entry for dnaSeq entry
190 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
192 SequenceI aads = aaSeqRef.getDatasetSequence();
193 for (SequenceToSequenceMapping ssm : mappings)
195 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
204 * test to see if codon frame involves seq in any way
207 * a nucleotide or protein sequence
208 * @return true if a mapping exists to or from this sequence to any translated
211 public boolean involvesSequence(SequenceI seq)
213 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
217 * Add search results for regions in other sequences that translate or are
218 * translated from a particular position in seq
224 * where highlighted regions go
226 public void markMappedRegion(SequenceI seq, int index,
227 SearchResults results)
230 SequenceI ds = seq.getDatasetSequence();
231 for (SequenceToSequenceMapping ssm : mappings)
233 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
235 codon = ssm.mapping.map.locateInTo(index, index);
238 for (int i = 0; i < codon.length; i += 2)
240 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
244 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
247 codon = ssm.mapping.map.locateInFrom(index, index);
250 for (int i = 0; i < codon.length; i += 2)
252 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
261 * Returns the DNA codon positions (base 1) for the given position (base 1) in
262 * a mapped protein sequence, or null if no mapping is found.
264 * Intended for use in aligning cDNA to match aligned protein. Only the first
265 * mapping found is returned, so not suitable for use if multiple protein
266 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
267 * ill-defined for this case anyway).
270 * the DNA dataset sequence
272 * residue position (base 1) in a protein sequence
275 public int[] getDnaPosition(SequenceI seq, int aaPos)
278 * Adapted from markMappedRegion().
282 for (SequenceToSequenceMapping ssm : mappings)
284 if (ssm.fromSeq == seq)
286 ml = getdnaToProt()[i];
291 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
295 * Convenience method to return the first aligned sequence in the given
296 * alignment whose dataset has a mapping with the given dataset sequence.
303 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
306 * Search mapped protein ('to') sequences first.
308 for (SequenceToSequenceMapping ssm : mappings)
310 if (ssm.fromSeq == seq)
312 for (SequenceI sourceAligned : al.getSequences())
314 if (ssm.mapping.to == sourceAligned.getDatasetSequence())
316 return sourceAligned;
323 * Then try mapped dna sequences.
325 for (SequenceToSequenceMapping ssm : mappings)
327 if (ssm.mapping.to == seq)
329 for (SequenceI sourceAligned : al.getSequences())
331 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
333 return sourceAligned;
343 * Returns the region in the target sequence's dataset that is mapped to the
344 * given position (base 1) in the query sequence's dataset. The region is a
345 * set of start/end position pairs.
352 public int[] getMappedRegion(SequenceI target, SequenceI query,
355 SequenceI targetDs = target.getDatasetSequence() == null ? target
356 : target.getDatasetSequence();
357 SequenceI queryDs = query.getDatasetSequence() == null ? query : query
358 .getDatasetSequence();
359 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
363 for (SequenceToSequenceMapping ssm : mappings)
366 * try mapping from target to query
368 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
370 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
377 * else try mapping from query to target
379 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
381 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
392 * Returns the mapped DNA codons for the given position in a protein sequence,
393 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
394 * codons. There may be more than one codon mapped to the protein if (for
395 * example), there are mappings to cDNA variants.
398 * the peptide dataset sequence
400 * residue position (base 1) in the peptide sequence
403 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
406 SequenceI dnaSeq = null;
407 List<char[]> result = new ArrayList<char[]>();
409 for (SequenceToSequenceMapping ssm : mappings)
411 if (ssm.mapping.to == protein)
413 ml = ssm.mapping.map;
414 dnaSeq = ssm.fromSeq;
416 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
417 if (codonPos == null)
423 * Read off the mapped nucleotides (converting to position base 0)
425 codonPos = MappingUtils.flattenRanges(codonPos);
426 char[] dna = dnaSeq.getSequence();
427 int start = dnaSeq.getStart();
428 result.add(new char[] { dna[codonPos[0] - start],
429 dna[codonPos[1] - start], dna[codonPos[2] - start] });
432 return result.isEmpty() ? null : result;
436 * Returns any mappings found which are to (or from) the given sequence, and
437 * to distinct sequences.
442 public List<Mapping> getMappingsForSequence(SequenceI seq)
444 List<Mapping> result = new ArrayList<Mapping>();
445 List<SequenceI> related = new ArrayList<SequenceI>();
446 SequenceI seqDs = seq.getDatasetSequence();
447 seqDs = seqDs != null ? seqDs : seq;
449 for (SequenceToSequenceMapping ssm : mappings)
451 final Mapping mapping = ssm.mapping;
452 if (ssm.fromSeq == seqDs || mapping.to == seqDs)
454 if (!related.contains(mapping.to))
457 related.add(mapping.to);
465 * Test whether the given sequence is substitutable for one or more dummy
466 * sequences in this mapping
472 public boolean isRealisableWith(SequenceI seq)
474 return realiseWith(seq, false) > 0;
478 * Replace any matchable mapped dummy sequences with the given real one.
479 * Returns the count of sequence mappings instantiated.
484 public int realiseWith(SequenceI seq)
486 return realiseWith(seq, true);
490 * Returns the number of mapped dummy sequences that could be replaced with
491 * the given real sequence.
496 * if true, performs replacements, else only counts
499 protected int realiseWith(SequenceI seq, boolean doUpdate)
501 SequenceI ds = seq.getDatasetSequence() != null ? seq
502 .getDatasetSequence() : seq;
506 * check for replaceable DNA ('map from') sequences
508 for (SequenceToSequenceMapping ssm : mappings)
510 SequenceI dna = ssm.fromSeq;
511 if (dna instanceof SequenceDummy
512 && dna.getName().equals(ds.getName()))
514 Mapping mapping = ssm.mapping;
515 int mapStart = mapping.getMap().getFromLowest();
516 int mapEnd = mapping.getMap().getFromHighest();
517 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
523 // TODO: new method ? ds.realise(dna);
524 // might want to copy database refs as well
525 ds.setSequenceFeatures(dna.getSequenceFeatures());
528 System.out.println("Realised mapped sequence " + ds.getName());
534 * check for replaceable protein ('map to') sequences
536 Mapping mapping = ssm.mapping;
537 SequenceI prot = mapping.getTo();
538 int mapStart = mapping.getMap().getToLowest();
539 int mapEnd = mapping.getMap().getToHighest();
540 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
546 // TODO: new method ? ds.realise(dna);
547 // might want to copy database refs as well
548 ds.setSequenceFeatures(dna.getSequenceFeatures());
549 ssm.mapping.setTo(ds);
557 * Helper method to test whether a 'real' sequence could replace a 'dummy'
558 * sequence in the map. The criteria are that they have the same name, and
559 * that the mapped region overlaps the candidate sequence.
567 protected static boolean couldRealiseSequence(SequenceI existing,
568 SequenceI replacement, int mapStart, int mapEnd)
570 if (existing instanceof SequenceDummy
571 && !(replacement instanceof SequenceDummy)
572 && existing.getName().equals(replacement.getName()))
574 int start = replacement.getStart();
575 int end = replacement.getEnd();
576 boolean mappingOverlapsSequence = (mapStart >= start && mapStart <= end)
577 || (mapEnd >= start && mapEnd <= end);
578 if (mappingOverlapsSequence)
587 * Change any mapping to the given sequence to be to its dataset sequence
588 * instead. For use when mappings are created before their referenced
589 * sequences are instantiated, for example when parsing GFF data.
593 public void updateToDataset(SequenceI seq)
595 if (seq == null || seq.getDatasetSequence() == null)
599 SequenceI ds = seq.getDatasetSequence();
601 for (SequenceToSequenceMapping ssm : mappings)
606 if (ssm.fromSeq == seq)
614 if (ssm.mapping.to == seq)