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
65 * @see SequenceToSequenceMapping#hashCode()
70 return mappings.hashCode();
74 * Answers true if the objects hold the same mapping between the same two
80 public boolean equals(Object obj)
82 if (!(obj instanceof SequenceToSequenceMapping))
86 SequenceToSequenceMapping that = (SequenceToSequenceMapping) obj;
87 if (this.mapping == null)
89 return that.mapping == null;
91 return this.mapping.equals(that.mapping);
94 public SequenceI getFromSeq()
99 public Mapping getMapping()
105 private List<SequenceToSequenceMapping> mappings;
110 public AlignedCodonFrame()
112 mappings = new ArrayList<SequenceToSequenceMapping>();
116 * Adds a mapping between the dataset sequences for the associated dna and
117 * protein sequence objects
123 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
125 // JBPNote DEBUG! THIS !
126 // dnaseq.transferAnnotation(aaseq, mp);
127 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
129 SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
130 : dnaseq.getDatasetSequence();
131 SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
132 .getDatasetSequence();
135 * if we already hold a mapping between these sequences, just add to it
136 * note that 'adding' a duplicate map does nothing; this protects against
137 * creating duplicate mappings in AlignedCodonFrame
139 for (SequenceToSequenceMapping ssm : mappings)
141 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
143 ssm.mapping.map.addMapList(map);
149 * otherwise, add a new sequence mapping
151 Mapping mp = new Mapping(toSeq, map);
152 mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
155 public SequenceI[] getdnaSeqs()
157 // TODO return a list instead?
159 List<SequenceI> seqs = new ArrayList<SequenceI>();
160 for (SequenceToSequenceMapping ssm : mappings)
162 seqs.add(ssm.fromSeq);
164 return seqs.toArray(new SequenceI[seqs.size()]);
167 public SequenceI[] getAaSeqs()
169 // TODO not used - remove?
170 List<SequenceI> seqs = new ArrayList<SequenceI>();
171 for (SequenceToSequenceMapping ssm : mappings)
173 seqs.add(ssm.mapping.to);
175 return seqs.toArray(new SequenceI[seqs.size()]);
178 public MapList[] getdnaToProt()
180 List<MapList> maps = new ArrayList<MapList>();
181 for (SequenceToSequenceMapping ssm : mappings)
183 maps.add(ssm.mapping.map);
185 return maps.toArray(new MapList[maps.size()]);
188 public Mapping[] getProtMappings()
190 List<Mapping> maps = new ArrayList<Mapping>();
191 for (SequenceToSequenceMapping ssm : mappings)
193 maps.add(ssm.mapping);
195 return maps.toArray(new Mapping[maps.size()]);
199 * Returns the first mapping found which is to or from the given sequence, or
205 public Mapping getMappingForSequence(SequenceI seq)
207 SequenceI seqDs = seq.getDatasetSequence();
208 seqDs = seqDs != null ? seqDs : seq;
210 for (SequenceToSequenceMapping ssm : mappings)
212 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
221 * Return the corresponding aligned or dataset aa sequence for given dna
222 * sequence, null if not found.
227 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
229 SequenceI dnads = dnaSeqRef.getDatasetSequence();
230 for (SequenceToSequenceMapping ssm : mappings)
232 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
234 return ssm.mapping.to;
243 * @return null or corresponding aaSeq entry for dnaSeq entry
245 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
247 SequenceI aads = aaSeqRef.getDatasetSequence();
248 for (SequenceToSequenceMapping ssm : mappings)
250 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
259 * test to see if codon frame involves seq in any way
262 * a nucleotide or protein sequence
263 * @return true if a mapping exists to or from this sequence to any translated
266 public boolean involvesSequence(SequenceI seq)
268 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
272 * Add search results for regions in other sequences that translate or are
273 * translated from a particular position in seq
279 * where highlighted regions go
281 public void markMappedRegion(SequenceI seq, int index,
282 SearchResults results)
285 SequenceI ds = seq.getDatasetSequence();
286 for (SequenceToSequenceMapping ssm : mappings)
288 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
290 codon = ssm.mapping.map.locateInTo(index, index);
293 for (int i = 0; i < codon.length; i += 2)
295 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
299 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
302 codon = ssm.mapping.map.locateInFrom(index, index);
305 for (int i = 0; i < codon.length; i += 2)
307 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
316 * Returns the DNA codon positions (base 1) for the given position (base 1) in
317 * a mapped protein sequence, or null if no mapping is found.
319 * Intended for use in aligning cDNA to match aligned protein. Only the first
320 * mapping found is returned, so not suitable for use if multiple protein
321 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
322 * ill-defined for this case anyway).
325 * the DNA dataset sequence
327 * residue position (base 1) in a protein sequence
330 public int[] getDnaPosition(SequenceI seq, int aaPos)
333 * Adapted from markMappedRegion().
337 for (SequenceToSequenceMapping ssm : mappings)
339 if (ssm.fromSeq == seq)
341 ml = getdnaToProt()[i];
346 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
350 * Convenience method to return the first aligned sequence in the given
351 * alignment whose dataset has a mapping with the given (aligned or dataset)
359 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
362 * Search mapped protein ('to') sequences first.
364 for (SequenceToSequenceMapping ssm : mappings)
366 if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
368 for (SequenceI sourceAligned : al.getSequences())
370 if (ssm.mapping.to == sourceAligned.getDatasetSequence()
371 || ssm.mapping.to == sourceAligned)
373 return sourceAligned;
380 * Then try mapped dna sequences.
382 for (SequenceToSequenceMapping ssm : mappings)
384 if (ssm.mapping.to == seq
385 || ssm.mapping.to == seq.getDatasetSequence())
387 for (SequenceI sourceAligned : al.getSequences())
389 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
391 return sourceAligned;
401 * Returns the region in the target sequence's dataset that is mapped to the
402 * given position (base 1) in the query sequence's dataset. The region is a
403 * set of start/end position pairs.
410 public int[] getMappedRegion(SequenceI target, SequenceI query,
413 SequenceI targetDs = target.getDatasetSequence() == null ? target
414 : target.getDatasetSequence();
415 SequenceI queryDs = query.getDatasetSequence() == null ? query : query
416 .getDatasetSequence();
417 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
421 for (SequenceToSequenceMapping ssm : mappings)
424 * try mapping from target to query
426 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
428 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
435 * else try mapping from query to target
437 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
439 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
450 * Returns the mapped DNA codons for the given position in a protein sequence,
451 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
452 * codons. There may be more than one codon mapped to the protein if (for
453 * example), there are mappings to cDNA variants.
456 * the peptide dataset sequence
458 * residue position (base 1) in the peptide sequence
461 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
464 SequenceI dnaSeq = null;
465 List<char[]> result = new ArrayList<char[]>();
467 for (SequenceToSequenceMapping ssm : mappings)
469 if (ssm.mapping.to == protein)
471 ml = ssm.mapping.map;
472 dnaSeq = ssm.fromSeq;
474 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
475 if (codonPos == null)
481 * Read off the mapped nucleotides (converting to position base 0)
483 codonPos = MappingUtils.flattenRanges(codonPos);
484 char[] dna = dnaSeq.getSequence();
485 int start = dnaSeq.getStart();
486 result.add(new char[] { dna[codonPos[0] - start],
487 dna[codonPos[1] - start], dna[codonPos[2] - start] });
490 return result.isEmpty() ? null : result;
494 * Returns any mappings found which are from the given sequence, and to
495 * distinct sequences.
500 public List<Mapping> getMappingsFromSequence(SequenceI seq)
502 List<Mapping> result = new ArrayList<Mapping>();
503 List<SequenceI> related = new ArrayList<SequenceI>();
504 SequenceI seqDs = seq.getDatasetSequence();
505 seqDs = seqDs != null ? seqDs : seq;
507 for (SequenceToSequenceMapping ssm : mappings)
509 final Mapping mapping = ssm.mapping;
510 if (ssm.fromSeq == seqDs)
512 if (!related.contains(mapping.to))
515 related.add(mapping.to);
523 * Test whether the given sequence is substitutable for one or more dummy
524 * sequences in this mapping
530 public boolean isRealisableWith(SequenceI seq)
532 return realiseWith(seq, false) > 0;
536 * Replace any matchable mapped dummy sequences with the given real one.
537 * Returns the count of sequence mappings instantiated.
542 public int realiseWith(SequenceI seq)
544 return realiseWith(seq, true);
548 * Returns the number of mapped dummy sequences that could be replaced with
549 * the given real sequence.
554 * if true, performs replacements, else only counts
557 protected int realiseWith(SequenceI seq, boolean doUpdate)
559 SequenceI ds = seq.getDatasetSequence() != null ? seq
560 .getDatasetSequence() : seq;
564 * check for replaceable DNA ('map from') sequences
566 for (SequenceToSequenceMapping ssm : mappings)
568 SequenceI dna = ssm.fromSeq;
569 if (dna instanceof SequenceDummy
570 && dna.getName().equals(ds.getName()))
572 Mapping mapping = ssm.mapping;
573 int mapStart = mapping.getMap().getFromLowest();
574 int mapEnd = mapping.getMap().getFromHighest();
575 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
581 // TODO: new method ? ds.realise(dna);
582 // might want to copy database refs as well
583 ds.setSequenceFeatures(dna.getSequenceFeatures());
586 System.out.println("Realised mapped sequence " + ds.getName());
592 * check for replaceable protein ('map to') sequences
594 Mapping mapping = ssm.mapping;
595 SequenceI prot = mapping.getTo();
596 int mapStart = mapping.getMap().getToLowest();
597 int mapEnd = mapping.getMap().getToHighest();
598 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
604 // TODO: new method ? ds.realise(dna);
605 // might want to copy database refs as well
606 ds.setSequenceFeatures(dna.getSequenceFeatures());
607 ssm.mapping.setTo(ds);
615 * Helper method to test whether a 'real' sequence could replace a 'dummy'
616 * sequence in the map. The criteria are that they have the same name, and
617 * that the mapped region overlaps the candidate sequence.
625 protected static boolean couldRealiseSequence(SequenceI existing,
626 SequenceI replacement, int mapStart, int mapEnd)
628 if (existing instanceof SequenceDummy
629 && !(replacement instanceof SequenceDummy)
630 && existing.getName().equals(replacement.getName()))
632 int start = replacement.getStart();
633 int end = replacement.getEnd();
634 boolean mappingOverlapsSequence = (mapStart >= start && mapStart <= end)
635 || (mapEnd >= start && mapEnd <= end);
636 if (mappingOverlapsSequence)
645 * Change any mapping to the given sequence to be to its dataset sequence
646 * instead. For use when mappings are created before their referenced
647 * sequences are instantiated, for example when parsing GFF data.
651 public void updateToDataset(SequenceI seq)
653 if (seq == null || seq.getDatasetSequence() == null)
657 SequenceI ds = seq.getDatasetSequence();
659 for (SequenceToSequenceMapping ssm : mappings)
664 if (ssm.fromSeq == seq)
672 if (ssm.mapping.to == seq)
680 * Answers true if this object contains no mappings
684 public boolean isEmpty()
686 return mappings.isEmpty();
690 * Method for debug / inspection purposes only, may change in future
693 public String toString()
695 return mappings == null ? "null" : mappings.toString();
699 * Returns the first mapping found that is from 'fromSeq' to 'toSeq', or null
703 * aligned or dataset sequence
705 * aligned or dataset sequence
708 public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
710 for (SequenceToSequenceMapping mapping : mappings)
712 SequenceI from = mapping.fromSeq;
713 SequenceI to = mapping.mapping.to;
714 if ((from == fromSeq || from == fromSeq.getDatasetSequence())
715 && (to == toSeq || to == toSeq.getDatasetSequence()))
717 return mapping.mapping;
724 * Returns a hashcode derived from the list of sequence mappings
726 * @see SequenceToSequenceMapping#hashCode()
727 * @see AbstractList#hashCode()
730 public int hashCode()
732 return this.mappings.hashCode();
736 * Two AlignedCodonFrame objects are equal if they hold the same ordered list
739 * @see SequenceToSequenceMapping#
742 public boolean equals(Object obj)
744 if (!(obj instanceof AlignedCodonFrame))
748 return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
751 public List<SequenceToSequenceMapping> getMappings()