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.AbstractList;
27 import java.util.ArrayList;
28 import java.util.List;
31 * Stores mapping between the columns of a protein alignment and a DNA alignment
32 * and a list of individual codon to amino acid mappings between sequences.
34 public class AlignedCodonFrame
38 * Data bean to hold mappings from one sequence to another
40 public class SequenceToSequenceMapping
42 private SequenceI fromSeq;
44 private Mapping mapping;
46 SequenceToSequenceMapping(SequenceI from, Mapping map)
53 * Readable representation for debugging only, not guaranteed not to change
56 public String toString()
58 return String.format("From %s %s", fromSeq.getName(),
63 * Returns a hashCode derived from the hashcodes of the mappings and fromSeq
65 * @see SequenceToSequenceMapping#hashCode()
70 return (fromSeq == null ? 0 : fromSeq.hashCode() * 31)
75 * Answers true if the objects hold the same mapping between the same two
81 public boolean equals(Object obj)
83 if (!(obj instanceof SequenceToSequenceMapping))
87 SequenceToSequenceMapping that = (SequenceToSequenceMapping) obj;
88 if (this.mapping == null)
90 return that.mapping == null;
92 // TODO: can simplify by asserting fromSeq is a dataset sequence
93 return (this.fromSeq == that.fromSeq
94 || (this.fromSeq != null && that.fromSeq != null
95 && this.fromSeq.getDatasetSequence() != null
96 && this.fromSeq.getDatasetSequence() == that.fromSeq
97 .getDatasetSequence()))
98 && this.mapping.equals(that.mapping);
101 public SequenceI getFromSeq()
106 public Mapping getMapping()
112 private List<SequenceToSequenceMapping> mappings;
117 public AlignedCodonFrame()
119 mappings = new ArrayList<>();
123 * Adds a mapping between the dataset sequences for the associated dna and
124 * protein sequence objects
130 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
132 addMap(dnaseq, aaseq, map, null);
136 * Adds a mapping between the dataset sequences for the associated dna and
137 * protein sequence objects
144 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map,
147 // JBPNote DEBUG! THIS !
148 // dnaseq.transferAnnotation(aaseq, mp);
149 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
151 SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
152 : dnaseq.getDatasetSequence();
153 SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq
154 : aaseq.getDatasetSequence();
157 * if we already hold a mapping between these sequences, just add to it
158 * note that 'adding' a duplicate map does nothing; this protects against
159 * creating duplicate mappings in AlignedCodonFrame
161 for (SequenceToSequenceMapping ssm : mappings)
163 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
165 ssm.mapping.map.addMapList(map);
171 * otherwise, add a new sequence mapping
173 Mapping mp = new Mapping(toSeq, map);
174 mp.setMappedFromId(mapFromId);
175 mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
178 public SequenceI[] getdnaSeqs()
180 // TODO return a list instead?
182 List<SequenceI> seqs = new ArrayList<>();
183 for (SequenceToSequenceMapping ssm : mappings)
185 seqs.add(ssm.fromSeq);
187 return seqs.toArray(new SequenceI[seqs.size()]);
190 public SequenceI[] getAaSeqs()
192 // TODO not used - remove?
193 List<SequenceI> seqs = new ArrayList<>();
194 for (SequenceToSequenceMapping ssm : mappings)
196 seqs.add(ssm.mapping.to);
198 return seqs.toArray(new SequenceI[seqs.size()]);
201 public MapList[] getdnaToProt()
203 List<MapList> maps = new ArrayList<>();
204 for (SequenceToSequenceMapping ssm : mappings)
206 maps.add(ssm.mapping.map);
208 return maps.toArray(new MapList[maps.size()]);
211 public Mapping[] getProtMappings()
213 List<Mapping> maps = new ArrayList<>();
214 for (SequenceToSequenceMapping ssm : mappings)
216 maps.add(ssm.mapping);
218 return maps.toArray(new Mapping[maps.size()]);
222 * Returns the first mapping found which is to or from the given sequence, or
223 * null if none is found
228 public Mapping getMappingForSequence(SequenceI seq)
230 SequenceI seqDs = seq.getDatasetSequence();
231 seqDs = seqDs != null ? seqDs : seq;
233 for (SequenceToSequenceMapping ssm : mappings)
235 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
244 * Return the corresponding aligned or dataset aa sequence for given dna
245 * sequence, null if not found.
250 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
252 SequenceI dnads = dnaSeqRef.getDatasetSequence();
253 for (SequenceToSequenceMapping ssm : mappings)
255 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
257 return ssm.mapping.to;
266 * @return null or corresponding aaSeq entry for dnaSeq entry
268 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
270 SequenceI aads = aaSeqRef.getDatasetSequence();
271 for (SequenceToSequenceMapping ssm : mappings)
273 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
282 * test to see if codon frame involves seq in any way
285 * a nucleotide or protein sequence
286 * @return true if a mapping exists to or from this sequence to any translated
289 public boolean involvesSequence(SequenceI seq)
291 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
295 * Add search results for regions in other sequences that translate or are
296 * translated from a particular position in seq
302 * where highlighted regions go
304 public void markMappedRegion(SequenceI seq, int index,
305 SearchResultsI results)
308 SequenceI ds = seq.getDatasetSequence();
309 for (SequenceToSequenceMapping ssm : mappings)
311 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
313 codon = ssm.mapping.map.locateInTo(index, index);
316 for (int i = 0; i < codon.length; i += 2)
318 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
322 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
325 codon = ssm.mapping.map.locateInFrom(index, index);
328 for (int i = 0; i < codon.length; i += 2)
330 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
339 * Returns the DNA codon positions (base 1) for the given position (base 1) in
340 * a mapped protein sequence, or null if no mapping is found.
342 * Intended for use in aligning cDNA to match aligned protein. Only the first
343 * mapping found is returned, so not suitable for use if multiple protein
344 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
345 * ill-defined for this case anyway).
348 * the DNA dataset sequence
350 * residue position (base 1) in a protein sequence
353 public int[] getDnaPosition(SequenceI seq, int aaPos)
356 * Adapted from markMappedRegion().
360 for (SequenceToSequenceMapping ssm : mappings)
362 if (ssm.fromSeq == seq)
364 ml = getdnaToProt()[i];
369 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
373 * Convenience method to return the first aligned sequence in the given
374 * alignment whose dataset has a mapping with the given (aligned or dataset)
382 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
385 * Search mapped protein ('to') sequences first.
387 for (SequenceToSequenceMapping ssm : mappings)
389 if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
391 for (SequenceI sourceAligned : al.getSequences())
393 if (ssm.mapping.to == sourceAligned.getDatasetSequence()
394 || ssm.mapping.to == sourceAligned)
396 return sourceAligned;
403 * Then try mapped dna sequences.
405 for (SequenceToSequenceMapping ssm : mappings)
407 if (ssm.mapping.to == seq
408 || ssm.mapping.to == seq.getDatasetSequence())
410 for (SequenceI sourceAligned : al.getSequences())
412 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
414 return sourceAligned;
424 * Returns the region in the target sequence's dataset that is mapped to the
425 * given position (base 1) in the query sequence's dataset. The region is a
426 * set of start/end position pairs.
433 public int[] getMappedRegion(SequenceI target, SequenceI query,
436 SequenceI targetDs = target.getDatasetSequence() == null ? target
437 : target.getDatasetSequence();
438 SequenceI queryDs = query.getDatasetSequence() == null ? query
439 : query.getDatasetSequence();
440 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
444 for (SequenceToSequenceMapping ssm : mappings)
447 * try mapping from target to query
449 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
451 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
458 * else try mapping from query to target
460 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
462 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
473 * Returns the mapped DNA codons for the given position in a protein sequence,
474 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
475 * codons. There may be more than one codon mapped to the protein if (for
476 * example), there are mappings to cDNA variants.
479 * the peptide dataset sequence
481 * residue position (base 1) in the peptide sequence
484 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
487 SequenceI dnaSeq = null;
488 List<char[]> result = new ArrayList<>();
490 for (SequenceToSequenceMapping ssm : mappings)
492 if (ssm.mapping.to == protein
493 && ssm.mapping.getMap().getFromRatio() == 3)
495 ml = ssm.mapping.map;
496 dnaSeq = ssm.fromSeq;
498 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
499 if (codonPos == null)
505 * Read off the mapped nucleotides (converting to position base 0)
507 codonPos = MappingUtils.flattenRanges(codonPos);
508 int start = dnaSeq.getStart();
509 char c1 = dnaSeq.getCharAt(codonPos[0] - start);
510 char c2 = dnaSeq.getCharAt(codonPos[1] - start);
511 char c3 = dnaSeq.getCharAt(codonPos[2] - start);
512 result.add(new char[] { c1, c2, c3 });
515 return result.isEmpty() ? null : result;
519 * Returns any mappings found which are from the given sequence, and to
520 * distinct sequences.
525 public List<Mapping> getMappingsFromSequence(SequenceI seq)
527 List<Mapping> result = new ArrayList<>();
528 List<SequenceI> related = new ArrayList<>();
529 SequenceI seqDs = seq.getDatasetSequence();
530 seqDs = seqDs != null ? seqDs : seq;
532 for (SequenceToSequenceMapping ssm : mappings)
534 final Mapping mapping = ssm.mapping;
535 if (ssm.fromSeq == seqDs)
537 if (!related.contains(mapping.to))
540 related.add(mapping.to);
548 * Test whether the given sequence is substitutable for one or more dummy
549 * sequences in this mapping
555 public boolean isRealisableWith(SequenceI seq)
557 return realiseWith(seq, false) > 0;
561 * Replace any matchable mapped dummy sequences with the given real one.
562 * Returns the count of sequence mappings instantiated.
567 public int realiseWith(SequenceI seq)
569 return realiseWith(seq, true);
573 * Returns the number of mapped dummy sequences that could be replaced with
574 * the given real sequence.
579 * if true, performs replacements, else only counts
582 protected int realiseWith(SequenceI seq, boolean doUpdate)
584 SequenceI ds = seq.getDatasetSequence() != null
585 ? seq.getDatasetSequence()
590 * check for replaceable DNA ('map from') sequences
592 for (SequenceToSequenceMapping ssm : mappings)
594 SequenceI dna = ssm.fromSeq;
595 if (dna instanceof SequenceDummy
596 && dna.getName().equals(ds.getName()))
598 Mapping mapping = ssm.mapping;
599 int mapStart = mapping.getMap().getFromLowest();
600 int mapEnd = mapping.getMap().getFromHighest();
601 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
607 // TODO: new method ? ds.realise(dna);
608 // might want to copy database refs as well
609 ds.setSequenceFeatures(dna.getSequenceFeatures());
612 System.out.println("Realised mapped sequence " + ds.getName());
618 * check for replaceable protein ('map to') sequences
620 Mapping mapping = ssm.mapping;
621 SequenceI prot = mapping.getTo();
622 int mapStart = mapping.getMap().getToLowest();
623 int mapEnd = mapping.getMap().getToHighest();
624 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
630 // TODO: new method ? ds.realise(dna);
631 // might want to copy database refs as well
632 ds.setSequenceFeatures(dna.getSequenceFeatures());
633 ssm.mapping.setTo(ds);
641 * Helper method to test whether a 'real' sequence could replace a 'dummy'
642 * sequence in the map. The criteria are that they have the same name, and
643 * that the mapped region overlaps the candidate sequence.
651 protected static boolean couldRealiseSequence(SequenceI existing,
652 SequenceI replacement, int mapStart, int mapEnd)
654 if (existing instanceof SequenceDummy
655 && !(replacement instanceof SequenceDummy)
656 && existing.getName().equals(replacement.getName()))
658 int start = replacement.getStart();
659 int end = replacement.getEnd();
660 boolean mappingOverlapsSequence = (mapStart >= start
661 && mapStart <= end) || (mapEnd >= start && mapEnd <= end);
662 if (mappingOverlapsSequence)
671 * Change any mapping to the given sequence to be to its dataset sequence
672 * instead. For use when mappings are created before their referenced
673 * sequences are instantiated, for example when parsing GFF data.
677 public void updateToDataset(SequenceI seq)
679 if (seq == null || seq.getDatasetSequence() == null)
683 SequenceI ds = seq.getDatasetSequence();
685 for (SequenceToSequenceMapping ssm : mappings)
690 if (ssm.fromSeq == seq)
698 if (ssm.mapping.to == seq)
706 * Answers true if this object contains no mappings
710 public boolean isEmpty()
712 return mappings.isEmpty();
716 * Method for debug / inspection purposes only, may change in future
719 public String toString()
721 return mappings == null ? "null" : mappings.toString();
725 * Returns the first mapping found that is between 'fromSeq' and 'toSeq', or
729 * aligned or dataset sequence
731 * aligned or dataset sequence
734 public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
736 SequenceI dssFrom = fromSeq.getDatasetSequence() == null ? fromSeq
737 : fromSeq.getDatasetSequence();
738 SequenceI dssTo = toSeq.getDatasetSequence() == null ? toSeq
739 : toSeq.getDatasetSequence();
741 for (SequenceToSequenceMapping mapping : mappings)
743 SequenceI from = mapping.fromSeq;
744 SequenceI to = mapping.mapping.to;
745 if ((from == dssFrom && to == dssTo)
746 || (from == dssTo && to == dssFrom))
748 return mapping.mapping;
755 * Returns a hashcode derived from the list of sequence mappings
757 * @see SequenceToSequenceMapping#hashCode()
758 * @see AbstractList#hashCode()
761 public int hashCode()
763 return this.mappings.hashCode();
767 * Two AlignedCodonFrame objects are equal if they hold the same ordered list
770 * @see SequenceToSequenceMapping#
773 public boolean equals(Object obj)
775 if (!(obj instanceof AlignedCodonFrame))
779 return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
782 public List<SequenceToSequenceMapping> getMappings()