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 || (this.fromSeq != null
94 && that.fromSeq != null
95 && this.fromSeq.getDatasetSequence() != null && this.fromSeq
96 .getDatasetSequence() == that.fromSeq.getDatasetSequence()))
97 && this.mapping.equals(that.mapping);
100 public SequenceI getFromSeq()
105 public Mapping getMapping()
111 private List<SequenceToSequenceMapping> mappings;
116 public AlignedCodonFrame()
118 mappings = new ArrayList<SequenceToSequenceMapping>();
122 * Adds a mapping between the dataset sequences for the associated dna and
123 * protein sequence objects
129 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
131 addMap(dnaseq, aaseq, map, null);
135 * Adds a mapping between the dataset sequences for the associated dna and
136 * protein sequence objects
143 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map,
146 // JBPNote DEBUG! THIS !
147 // dnaseq.transferAnnotation(aaseq, mp);
148 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
150 SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
151 : dnaseq.getDatasetSequence();
152 SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
153 .getDatasetSequence();
156 * if we already hold a mapping between these sequences, just add to it
157 * note that 'adding' a duplicate map does nothing; this protects against
158 * creating duplicate mappings in AlignedCodonFrame
160 for (SequenceToSequenceMapping ssm : mappings)
162 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
164 ssm.mapping.map.addMapList(map);
170 * otherwise, add a new sequence mapping
172 Mapping mp = new Mapping(toSeq, map);
173 mp.setMappedFromId(mapFromId);
174 mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
177 public SequenceI[] getdnaSeqs()
179 // TODO return a list instead?
181 List<SequenceI> seqs = new ArrayList<SequenceI>();
182 for (SequenceToSequenceMapping ssm : mappings)
184 seqs.add(ssm.fromSeq);
186 return seqs.toArray(new SequenceI[seqs.size()]);
189 public SequenceI[] getAaSeqs()
191 // TODO not used - remove?
192 List<SequenceI> seqs = new ArrayList<SequenceI>();
193 for (SequenceToSequenceMapping ssm : mappings)
195 seqs.add(ssm.mapping.to);
197 return seqs.toArray(new SequenceI[seqs.size()]);
200 public MapList[] getdnaToProt()
202 List<MapList> maps = new ArrayList<MapList>();
203 for (SequenceToSequenceMapping ssm : mappings)
205 maps.add(ssm.mapping.map);
207 return maps.toArray(new MapList[maps.size()]);
210 public Mapping[] getProtMappings()
212 List<Mapping> maps = new ArrayList<Mapping>();
213 for (SequenceToSequenceMapping ssm : mappings)
215 maps.add(ssm.mapping);
217 return maps.toArray(new Mapping[maps.size()]);
221 * Returns the first mapping found which is to or from the given sequence, or
227 public Mapping getMappingForSequence(SequenceI seq)
229 SequenceI seqDs = seq.getDatasetSequence();
230 seqDs = seqDs != null ? seqDs : seq;
232 for (SequenceToSequenceMapping ssm : mappings)
234 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
243 * Return the corresponding aligned or dataset aa sequence for given dna
244 * sequence, null if not found.
249 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
251 SequenceI dnads = dnaSeqRef.getDatasetSequence();
252 for (SequenceToSequenceMapping ssm : mappings)
254 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
256 return ssm.mapping.to;
265 * @return null or corresponding aaSeq entry for dnaSeq entry
267 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
269 SequenceI aads = aaSeqRef.getDatasetSequence();
270 for (SequenceToSequenceMapping ssm : mappings)
272 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
281 * test to see if codon frame involves seq in any way
284 * a nucleotide or protein sequence
285 * @return true if a mapping exists to or from this sequence to any translated
288 public boolean involvesSequence(SequenceI seq)
290 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
294 * Add search results for regions in other sequences that translate or are
295 * translated from a particular position in seq
301 * where highlighted regions go
303 public void markMappedRegion(SequenceI seq, int index,
304 SearchResultsI results)
307 SequenceI ds = seq.getDatasetSequence();
308 for (SequenceToSequenceMapping ssm : mappings)
310 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
312 codon = ssm.mapping.map.locateInTo(index, index);
315 for (int i = 0; i < codon.length; i += 2)
317 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
321 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
324 codon = ssm.mapping.map.locateInFrom(index, index);
327 for (int i = 0; i < codon.length; i += 2)
329 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
338 * Returns the DNA codon positions (base 1) for the given position (base 1) in
339 * a mapped protein sequence, or null if no mapping is found.
341 * Intended for use in aligning cDNA to match aligned protein. Only the first
342 * mapping found is returned, so not suitable for use if multiple protein
343 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
344 * ill-defined for this case anyway).
347 * the DNA dataset sequence
349 * residue position (base 1) in a protein sequence
352 public int[] getDnaPosition(SequenceI seq, int aaPos)
355 * Adapted from markMappedRegion().
359 for (SequenceToSequenceMapping ssm : mappings)
361 if (ssm.fromSeq == seq)
363 ml = getdnaToProt()[i];
368 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
372 * Convenience method to return the first aligned sequence in the given
373 * alignment whose dataset has a mapping with the given (aligned or dataset)
381 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
384 * Search mapped protein ('to') sequences first.
386 for (SequenceToSequenceMapping ssm : mappings)
388 if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
390 for (SequenceI sourceAligned : al.getSequences())
392 if (ssm.mapping.to == sourceAligned.getDatasetSequence()
393 || ssm.mapping.to == sourceAligned)
395 return sourceAligned;
402 * Then try mapped dna sequences.
404 for (SequenceToSequenceMapping ssm : mappings)
406 if (ssm.mapping.to == seq
407 || ssm.mapping.to == seq.getDatasetSequence())
409 for (SequenceI sourceAligned : al.getSequences())
411 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
413 return sourceAligned;
423 * Returns the region in the target sequence's dataset that is mapped to the
424 * given position (base 1) in the query sequence's dataset. The region is a
425 * set of start/end position pairs.
432 public int[] getMappedRegion(SequenceI target, SequenceI query,
435 SequenceI targetDs = target.getDatasetSequence() == null ? target
436 : target.getDatasetSequence();
437 SequenceI queryDs = query.getDatasetSequence() == null ? query : query
438 .getDatasetSequence();
439 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
443 for (SequenceToSequenceMapping ssm : mappings)
446 * try mapping from target to query
448 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
450 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
457 * else try mapping from query to target
459 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
461 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
472 * Returns the mapped DNA codons for the given position in a protein sequence,
473 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
474 * codons. There may be more than one codon mapped to the protein if (for
475 * example), there are mappings to cDNA variants.
478 * the peptide dataset sequence
480 * residue position (base 1) in the peptide sequence
483 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
486 SequenceI dnaSeq = null;
487 List<char[]> result = new ArrayList<char[]>();
489 for (SequenceToSequenceMapping ssm : mappings)
491 if (ssm.mapping.to == protein
492 && ssm.mapping.getMap().getFromRatio() == 3)
494 ml = ssm.mapping.map;
495 dnaSeq = ssm.fromSeq;
497 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
498 if (codonPos == null)
504 * Read off the mapped nucleotides (converting to position base 0)
506 codonPos = MappingUtils.flattenRanges(codonPos);
507 char[] dna = dnaSeq.getSequence();
508 int start = dnaSeq.getStart();
509 result.add(new char[] { dna[codonPos[0] - start],
510 dna[codonPos[1] - start], dna[codonPos[2] - start] });
513 return result.isEmpty() ? null : result;
517 * Returns any mappings found which are from the given sequence, and to
518 * distinct sequences.
523 public List<Mapping> getMappingsFromSequence(SequenceI seq)
525 List<Mapping> result = new ArrayList<Mapping>();
526 List<SequenceI> related = new ArrayList<SequenceI>();
527 SequenceI seqDs = seq.getDatasetSequence();
528 seqDs = seqDs != null ? seqDs : seq;
530 for (SequenceToSequenceMapping ssm : mappings)
532 final Mapping mapping = ssm.mapping;
533 if (ssm.fromSeq == seqDs)
535 if (!related.contains(mapping.to))
538 related.add(mapping.to);
546 * Test whether the given sequence is substitutable for one or more dummy
547 * sequences in this mapping
553 public boolean isRealisableWith(SequenceI seq)
555 return realiseWith(seq, false) > 0;
559 * Replace any matchable mapped dummy sequences with the given real one.
560 * Returns the count of sequence mappings instantiated.
565 public int realiseWith(SequenceI seq)
567 return realiseWith(seq, true);
571 * Returns the number of mapped dummy sequences that could be replaced with
572 * the given real sequence.
577 * if true, performs replacements, else only counts
580 protected int realiseWith(SequenceI seq, boolean doUpdate)
582 SequenceI ds = seq.getDatasetSequence() != null ? seq
583 .getDatasetSequence() : seq;
587 * check for replaceable DNA ('map from') sequences
589 for (SequenceToSequenceMapping ssm : mappings)
591 SequenceI dna = ssm.fromSeq;
592 if (dna instanceof SequenceDummy
593 && dna.getName().equals(ds.getName()))
595 Mapping mapping = ssm.mapping;
596 int mapStart = mapping.getMap().getFromLowest();
597 int mapEnd = mapping.getMap().getFromHighest();
598 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
604 // TODO: new method ? ds.realise(dna);
605 // might want to copy database refs as well
606 ds.setSequenceFeatures(dna.getSequenceFeatures());
609 System.out.println("Realised mapped sequence " + ds.getName());
615 * check for replaceable protein ('map to') sequences
617 Mapping mapping = ssm.mapping;
618 SequenceI prot = mapping.getTo();
619 int mapStart = mapping.getMap().getToLowest();
620 int mapEnd = mapping.getMap().getToHighest();
621 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
627 // TODO: new method ? ds.realise(dna);
628 // might want to copy database refs as well
629 ds.setSequenceFeatures(dna.getSequenceFeatures());
630 ssm.mapping.setTo(ds);
638 * Helper method to test whether a 'real' sequence could replace a 'dummy'
639 * sequence in the map. The criteria are that they have the same name, and
640 * that the mapped region overlaps the candidate sequence.
648 protected static boolean couldRealiseSequence(SequenceI existing,
649 SequenceI replacement, int mapStart, int mapEnd)
651 if (existing instanceof SequenceDummy
652 && !(replacement instanceof SequenceDummy)
653 && existing.getName().equals(replacement.getName()))
655 int start = replacement.getStart();
656 int end = replacement.getEnd();
657 boolean mappingOverlapsSequence = (mapStart >= start && mapStart <= end)
658 || (mapEnd >= start && mapEnd <= end);
659 if (mappingOverlapsSequence)
668 * Change any mapping to the given sequence to be to its dataset sequence
669 * instead. For use when mappings are created before their referenced
670 * sequences are instantiated, for example when parsing GFF data.
674 public void updateToDataset(SequenceI seq)
676 if (seq == null || seq.getDatasetSequence() == null)
680 SequenceI ds = seq.getDatasetSequence();
682 for (SequenceToSequenceMapping ssm : mappings)
687 if (ssm.fromSeq == seq)
695 if (ssm.mapping.to == seq)
703 * Answers true if this object contains no mappings
707 public boolean isEmpty()
709 return mappings.isEmpty();
713 * Method for debug / inspection purposes only, may change in future
716 public String toString()
718 return mappings == null ? "null" : mappings.toString();
722 * Returns the first mapping found that is between 'fromSeq' and 'toSeq', or
726 * aligned or dataset sequence
728 * aligned or dataset sequence
731 public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
733 SequenceI dssFrom = fromSeq.getDatasetSequence() == null ? fromSeq
734 : fromSeq.getDatasetSequence();
735 SequenceI dssTo = toSeq.getDatasetSequence() == null ? toSeq : toSeq
736 .getDatasetSequence();
738 for (SequenceToSequenceMapping mapping : mappings)
740 SequenceI from = mapping.fromSeq;
741 SequenceI to = mapping.mapping.to;
742 if ((from == dssFrom && to == dssTo)
743 || (from == dssTo && to == dssFrom))
745 return mapping.mapping;
752 * Returns a hashcode derived from the list of sequence mappings
754 * @see SequenceToSequenceMapping#hashCode()
755 * @see AbstractList#hashCode()
758 public int hashCode()
760 return this.mappings.hashCode();
764 * Two AlignedCodonFrame objects are equal if they hold the same ordered list
767 * @see SequenceToSequenceMapping#
770 public boolean equals(Object obj)
772 if (!(obj instanceof AlignedCodonFrame))
776 return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
779 public List<SequenceToSequenceMapping> getMappings()