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
97 .getDatasetSequence())) && 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 // JBPNote DEBUG! THIS !
132 // dnaseq.transferAnnotation(aaseq, mp);
133 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
135 SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
136 : dnaseq.getDatasetSequence();
137 SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
138 .getDatasetSequence();
141 * if we already hold a mapping between these sequences, just add to it
142 * note that 'adding' a duplicate map does nothing; this protects against
143 * creating duplicate mappings in AlignedCodonFrame
145 for (SequenceToSequenceMapping ssm : mappings)
147 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
149 ssm.mapping.map.addMapList(map);
155 * otherwise, add a new sequence mapping
157 Mapping mp = new Mapping(toSeq, map);
158 mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
161 public SequenceI[] getdnaSeqs()
163 // TODO return a list instead?
165 List<SequenceI> seqs = new ArrayList<SequenceI>();
166 for (SequenceToSequenceMapping ssm : mappings)
168 seqs.add(ssm.fromSeq);
170 return seqs.toArray(new SequenceI[seqs.size()]);
173 public SequenceI[] getAaSeqs()
175 // TODO not used - remove?
176 List<SequenceI> seqs = new ArrayList<SequenceI>();
177 for (SequenceToSequenceMapping ssm : mappings)
179 seqs.add(ssm.mapping.to);
181 return seqs.toArray(new SequenceI[seqs.size()]);
184 public MapList[] getdnaToProt()
186 List<MapList> maps = new ArrayList<MapList>();
187 for (SequenceToSequenceMapping ssm : mappings)
189 maps.add(ssm.mapping.map);
191 return maps.toArray(new MapList[maps.size()]);
194 public Mapping[] getProtMappings()
196 List<Mapping> maps = new ArrayList<Mapping>();
197 for (SequenceToSequenceMapping ssm : mappings)
199 maps.add(ssm.mapping);
201 return maps.toArray(new Mapping[maps.size()]);
205 * Returns the first mapping found which is to or from the given sequence, or
211 public Mapping getMappingForSequence(SequenceI seq)
213 SequenceI seqDs = seq.getDatasetSequence();
214 seqDs = seqDs != null ? seqDs : seq;
216 for (SequenceToSequenceMapping ssm : mappings)
218 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
227 * Return the corresponding aligned or dataset aa sequence for given dna
228 * sequence, null if not found.
233 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
235 SequenceI dnads = dnaSeqRef.getDatasetSequence();
236 for (SequenceToSequenceMapping ssm : mappings)
238 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
240 return ssm.mapping.to;
249 * @return null or corresponding aaSeq entry for dnaSeq entry
251 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
253 SequenceI aads = aaSeqRef.getDatasetSequence();
254 for (SequenceToSequenceMapping ssm : mappings)
256 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
265 * test to see if codon frame involves seq in any way
268 * a nucleotide or protein sequence
269 * @return true if a mapping exists to or from this sequence to any translated
272 public boolean involvesSequence(SequenceI seq)
274 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
278 * Add search results for regions in other sequences that translate or are
279 * translated from a particular position in seq
285 * where highlighted regions go
287 public void markMappedRegion(SequenceI seq, int index,
288 SearchResults results)
291 SequenceI ds = seq.getDatasetSequence();
292 for (SequenceToSequenceMapping ssm : mappings)
294 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
296 codon = ssm.mapping.map.locateInTo(index, index);
299 for (int i = 0; i < codon.length; i += 2)
301 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
305 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
308 codon = ssm.mapping.map.locateInFrom(index, index);
311 for (int i = 0; i < codon.length; i += 2)
313 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
322 * Returns the DNA codon positions (base 1) for the given position (base 1) in
323 * a mapped protein sequence, or null if no mapping is found.
325 * Intended for use in aligning cDNA to match aligned protein. Only the first
326 * mapping found is returned, so not suitable for use if multiple protein
327 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
328 * ill-defined for this case anyway).
331 * the DNA dataset sequence
333 * residue position (base 1) in a protein sequence
336 public int[] getDnaPosition(SequenceI seq, int aaPos)
339 * Adapted from markMappedRegion().
343 for (SequenceToSequenceMapping ssm : mappings)
345 if (ssm.fromSeq == seq)
347 ml = getdnaToProt()[i];
352 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
356 * Convenience method to return the first aligned sequence in the given
357 * alignment whose dataset has a mapping with the given (aligned or dataset)
365 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
368 * Search mapped protein ('to') sequences first.
370 for (SequenceToSequenceMapping ssm : mappings)
372 if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
374 for (SequenceI sourceAligned : al.getSequences())
376 if (ssm.mapping.to == sourceAligned.getDatasetSequence()
377 || ssm.mapping.to == sourceAligned)
379 return sourceAligned;
386 * Then try mapped dna sequences.
388 for (SequenceToSequenceMapping ssm : mappings)
390 if (ssm.mapping.to == seq
391 || ssm.mapping.to == seq.getDatasetSequence())
393 for (SequenceI sourceAligned : al.getSequences())
395 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
397 return sourceAligned;
407 * Returns the region in the target sequence's dataset that is mapped to the
408 * given position (base 1) in the query sequence's dataset. The region is a
409 * set of start/end position pairs.
416 public int[] getMappedRegion(SequenceI target, SequenceI query,
419 SequenceI targetDs = target.getDatasetSequence() == null ? target
420 : target.getDatasetSequence();
421 SequenceI queryDs = query.getDatasetSequence() == null ? query : query
422 .getDatasetSequence();
423 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
427 for (SequenceToSequenceMapping ssm : mappings)
430 * try mapping from target to query
432 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
434 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
441 * else try mapping from query to target
443 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
445 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
456 * Returns the mapped DNA codons for the given position in a protein sequence,
457 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
458 * codons. There may be more than one codon mapped to the protein if (for
459 * example), there are mappings to cDNA variants.
462 * the peptide dataset sequence
464 * residue position (base 1) in the peptide sequence
467 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
470 SequenceI dnaSeq = null;
471 List<char[]> result = new ArrayList<char[]>();
473 for (SequenceToSequenceMapping ssm : mappings)
475 if (ssm.mapping.to == protein)
477 ml = ssm.mapping.map;
478 dnaSeq = ssm.fromSeq;
480 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
481 if (codonPos == null)
487 * Read off the mapped nucleotides (converting to position base 0)
489 codonPos = MappingUtils.flattenRanges(codonPos);
490 char[] dna = dnaSeq.getSequence();
491 int start = dnaSeq.getStart();
492 result.add(new char[] { dna[codonPos[0] - start],
493 dna[codonPos[1] - start], dna[codonPos[2] - start] });
496 return result.isEmpty() ? null : result;
500 * Returns any mappings found which are from the given sequence, and to
501 * distinct sequences.
506 public List<Mapping> getMappingsFromSequence(SequenceI seq)
508 List<Mapping> result = new ArrayList<Mapping>();
509 List<SequenceI> related = new ArrayList<SequenceI>();
510 SequenceI seqDs = seq.getDatasetSequence();
511 seqDs = seqDs != null ? seqDs : seq;
513 for (SequenceToSequenceMapping ssm : mappings)
515 final Mapping mapping = ssm.mapping;
516 if (ssm.fromSeq == seqDs)
518 if (!related.contains(mapping.to))
521 related.add(mapping.to);
529 * Test whether the given sequence is substitutable for one or more dummy
530 * sequences in this mapping
536 public boolean isRealisableWith(SequenceI seq)
538 return realiseWith(seq, false) > 0;
542 * Replace any matchable mapped dummy sequences with the given real one.
543 * Returns the count of sequence mappings instantiated.
548 public int realiseWith(SequenceI seq)
550 return realiseWith(seq, true);
554 * Returns the number of mapped dummy sequences that could be replaced with
555 * the given real sequence.
560 * if true, performs replacements, else only counts
563 protected int realiseWith(SequenceI seq, boolean doUpdate)
565 SequenceI ds = seq.getDatasetSequence() != null ? seq
566 .getDatasetSequence() : seq;
570 * check for replaceable DNA ('map from') sequences
572 for (SequenceToSequenceMapping ssm : mappings)
574 SequenceI dna = ssm.fromSeq;
575 if (dna instanceof SequenceDummy
576 && dna.getName().equals(ds.getName()))
578 Mapping mapping = ssm.mapping;
579 int mapStart = mapping.getMap().getFromLowest();
580 int mapEnd = mapping.getMap().getFromHighest();
581 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
587 // TODO: new method ? ds.realise(dna);
588 // might want to copy database refs as well
589 ds.setSequenceFeatures(dna.getSequenceFeatures());
592 System.out.println("Realised mapped sequence " + ds.getName());
598 * check for replaceable protein ('map to') sequences
600 Mapping mapping = ssm.mapping;
601 SequenceI prot = mapping.getTo();
602 int mapStart = mapping.getMap().getToLowest();
603 int mapEnd = mapping.getMap().getToHighest();
604 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
610 // TODO: new method ? ds.realise(dna);
611 // might want to copy database refs as well
612 ds.setSequenceFeatures(dna.getSequenceFeatures());
613 ssm.mapping.setTo(ds);
621 * Helper method to test whether a 'real' sequence could replace a 'dummy'
622 * sequence in the map. The criteria are that they have the same name, and
623 * that the mapped region overlaps the candidate sequence.
631 protected static boolean couldRealiseSequence(SequenceI existing,
632 SequenceI replacement, int mapStart, int mapEnd)
634 if (existing instanceof SequenceDummy
635 && !(replacement instanceof SequenceDummy)
636 && existing.getName().equals(replacement.getName()))
638 int start = replacement.getStart();
639 int end = replacement.getEnd();
640 boolean mappingOverlapsSequence = (mapStart >= start && mapStart <= end)
641 || (mapEnd >= start && mapEnd <= end);
642 if (mappingOverlapsSequence)
651 * Change any mapping to the given sequence to be to its dataset sequence
652 * instead. For use when mappings are created before their referenced
653 * sequences are instantiated, for example when parsing GFF data.
657 public void updateToDataset(SequenceI seq)
659 if (seq == null || seq.getDatasetSequence() == null)
663 SequenceI ds = seq.getDatasetSequence();
665 for (SequenceToSequenceMapping ssm : mappings)
670 if (ssm.fromSeq == seq)
678 if (ssm.mapping.to == seq)
686 * Answers true if this object contains no mappings
690 public boolean isEmpty()
692 return mappings.isEmpty();
696 * Method for debug / inspection purposes only, may change in future
699 public String toString()
701 return mappings == null ? "null" : mappings.toString();
705 * Returns the first mapping found that is from 'fromSeq' to 'toSeq', or null
709 * aligned or dataset sequence
711 * aligned or dataset sequence
714 public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
716 for (SequenceToSequenceMapping mapping : mappings)
718 SequenceI from = mapping.fromSeq;
719 SequenceI to = mapping.mapping.to;
720 if ((from == fromSeq || from == fromSeq.getDatasetSequence())
721 && (to == toSeq || to == toSeq.getDatasetSequence()))
723 return mapping.mapping;
730 * Returns a hashcode derived from the list of sequence mappings
732 * @see SequenceToSequenceMapping#hashCode()
733 * @see AbstractList#hashCode()
736 public int hashCode()
738 return this.mappings.hashCode();
742 * Two AlignedCodonFrame objects are equal if they hold the same ordered list
745 * @see SequenceToSequenceMapping#
748 public boolean equals(Object obj)
750 if (!(obj instanceof AlignedCodonFrame))
754 return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
757 public List<SequenceToSequenceMapping> getMappings()