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
37 private List<SequenceMapping> mappings;
42 public AlignedCodonFrame()
44 mappings = new ArrayList<>();
48 * Adds a mapping between the dataset sequences for the associated dna and
49 * protein sequence objects
55 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
57 addMap(dnaseq, aaseq, map, null);
61 * Adds a mapping between the dataset sequences for the associated dna and
62 * protein sequence objects
69 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
79 : aaseq.getDatasetSequence();
82 * if we already hold a mapping between these sequences, just add to it
83 * note that 'adding' a duplicate map does nothing; this protects against
84 * creating duplicate mappings in AlignedCodonFrame
86 for (SequenceMapping ssm : mappings)
88 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
90 ssm.mapping.map.addMapList(map);
96 * otherwise, add a new sequence mapping
98 Mapping mp = new Mapping(toSeq, map);
99 mp.setMappedFromId(mapFromId);
100 mappings.add(new SequenceMapping(fromSeq, mp));
103 public SequenceI[] getdnaSeqs()
105 // TODO return a list instead?
107 List<SequenceI> seqs = new ArrayList<>();
108 for (SequenceMapping ssm : mappings)
110 seqs.add(ssm.fromSeq);
112 return seqs.toArray(new SequenceI[seqs.size()]);
115 public SequenceI[] getAaSeqs()
117 // TODO not used - remove?
118 List<SequenceI> seqs = new ArrayList<>();
119 for (SequenceMapping ssm : mappings)
121 seqs.add(ssm.mapping.to);
123 return seqs.toArray(new SequenceI[seqs.size()]);
126 public MapList[] getdnaToProt()
128 List<MapList> maps = new ArrayList<>();
129 for (SequenceMapping ssm : mappings)
131 maps.add(ssm.mapping.map);
133 return maps.toArray(new MapList[maps.size()]);
136 public Mapping[] getProtMappings()
138 List<Mapping> maps = new ArrayList<>();
139 for (SequenceMapping ssm : mappings)
141 maps.add(ssm.mapping);
143 return maps.toArray(new Mapping[maps.size()]);
147 * Returns the first mapping found which is to or from the given sequence, or
153 public Mapping getMappingForSequence(SequenceI seq, boolean cdsOnly)
155 SequenceI seqDs = seq.getDatasetSequence();
156 seqDs = seqDs != null ? seqDs : seq;
158 for (SequenceMapping ssm : mappings)
160 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
162 if (!cdsOnly || ssm.fromSeq.getName().startsWith("CDS")
163 || ssm.mapping.to.getName().startsWith("CDS"))
173 * Return the corresponding aligned or dataset aa sequence for given dna
174 * sequence, null if not found.
179 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
181 SequenceI dnads = dnaSeqRef.getDatasetSequence();
182 for (SequenceMapping ssm : mappings)
184 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
186 return ssm.mapping.to;
195 * @return null or corresponding aaSeq entry for dnaSeq entry
197 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
199 SequenceI aads = aaSeqRef.getDatasetSequence();
200 for (SequenceMapping ssm : mappings)
202 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
211 * test to see if codon frame involves seq in any way
214 * a nucleotide or protein sequence
215 * @return true if a mapping exists to or from this sequence to any translated
218 public boolean involvesSequence(SequenceI seq)
220 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
224 * Add search results for regions in other sequences that translate or are
225 * translated from a particular position in seq
231 * where highlighted regions go
233 public void markMappedRegion(SequenceI seq, int index,
234 SearchResultsI results)
237 SequenceI ds = seq.getDatasetSequence();
238 for (SequenceMapping ssm : mappings)
240 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
242 codon = ssm.mapping.map.locateInTo(index, index);
245 for (int i = 0; i < codon.length; i += 2)
247 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
251 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
254 codon = ssm.mapping.map.locateInFrom(index, index);
257 for (int i = 0; i < codon.length; i += 2)
259 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
268 * Returns the DNA codon positions (base 1) for the given position (base 1) in
269 * a mapped protein sequence, or null if no mapping is found.
271 * Intended for use in aligning cDNA to match aligned protein. Only the first
272 * mapping found is returned, so not suitable for use if multiple protein
273 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
274 * ill-defined for this case anyway).
277 * the DNA dataset sequence
279 * residue position (base 1) in a protein sequence
282 public int[] getDnaPosition(SequenceI seq, int aaPos)
285 * Adapted from markMappedRegion().
289 for (SequenceMapping ssm : mappings)
291 if (ssm.fromSeq == seq)
293 ml = getdnaToProt()[i];
298 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
302 * Convenience method to return the first aligned sequence in the given
303 * alignment whose dataset has a mapping with the given (aligned or dataset)
311 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
314 * Search mapped protein ('to') sequences first.
316 for (SequenceMapping ssm : mappings)
318 if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
320 for (SequenceI sourceAligned : al.getSequences())
322 if (ssm.mapping.to == sourceAligned.getDatasetSequence()
323 || ssm.mapping.to == sourceAligned)
325 return sourceAligned;
332 * Then try mapped dna sequences.
334 for (SequenceMapping ssm : mappings)
336 if (ssm.mapping.to == seq
337 || ssm.mapping.to == seq.getDatasetSequence())
339 for (SequenceI sourceAligned : al.getSequences())
341 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
343 return sourceAligned;
353 * Returns the region in the target sequence's dataset that is mapped to the
354 * given position (base 1) in the query sequence's dataset. The region is a
355 * set of start/end position pairs.
362 public int[] getMappedRegion(SequenceI target, SequenceI query,
365 SequenceI targetDs = target.getDatasetSequence() == null ? target
366 : target.getDatasetSequence();
367 SequenceI queryDs = query.getDatasetSequence() == null ? query
368 : query.getDatasetSequence();
369 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
373 for (SequenceMapping ssm : mappings)
376 * try mapping from target to query
378 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
380 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
387 * else try mapping from query to target
389 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
391 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
402 * Returns the mapped DNA codons for the given position in a protein sequence,
403 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
404 * codons. There may be more than one codon mapped to the protein if (for
405 * example), there are mappings to cDNA variants.
408 * the peptide dataset sequence
410 * residue position (base 1) in the peptide sequence
413 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
416 SequenceI dnaSeq = null;
417 List<char[]> result = new ArrayList<>();
419 for (SequenceMapping ssm : mappings)
421 if (ssm.mapping.to == protein
422 && ssm.mapping.getMap().getFromRatio() == 3)
424 ml = ssm.mapping.map;
425 dnaSeq = ssm.fromSeq;
427 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
428 if (codonPos == null)
434 * Read off the mapped nucleotides (converting to position base 0)
436 codonPos = MappingUtils.flattenRanges(codonPos);
437 int start = dnaSeq.getStart();
438 char c1 = dnaSeq.getCharAt(codonPos[0] - start);
439 char c2 = dnaSeq.getCharAt(codonPos[1] - start);
440 char c3 = dnaSeq.getCharAt(codonPos[2] - start);
441 result.add(new char[] { c1, c2, c3 });
444 return result.isEmpty() ? null : result;
448 * Returns any mappings found which are from the given sequence, and to
449 * distinct sequences.
454 public List<Mapping> getMappingsFromSequence(SequenceI seq)
456 List<Mapping> result = new ArrayList<>();
457 List<SequenceI> related = new ArrayList<>();
458 SequenceI seqDs = seq.getDatasetSequence();
459 seqDs = seqDs != null ? seqDs : seq;
461 for (SequenceMapping ssm : mappings)
463 final Mapping mapping = ssm.mapping;
464 if (ssm.fromSeq == seqDs)
466 if (!related.contains(mapping.to))
469 related.add(mapping.to);
477 * Test whether the given sequence is substitutable for one or more dummy
478 * sequences in this mapping
484 public boolean isRealisableWith(SequenceI seq)
486 return realiseWith(seq, false) > 0;
490 * Replace any matchable mapped dummy sequences with the given real one.
491 * Returns the count of sequence mappings instantiated.
496 public int realiseWith(SequenceI seq)
498 return realiseWith(seq, true);
502 * Returns the number of mapped dummy sequences that could be replaced with
503 * the given real sequence.
508 * if true, performs replacements, else only counts
511 protected int realiseWith(SequenceI seq, boolean doUpdate)
513 SequenceI ds = seq.getDatasetSequence() != null
514 ? seq.getDatasetSequence()
519 * check for replaceable DNA ('map from') sequences
521 for (SequenceMapping ssm : mappings)
523 SequenceI dna = ssm.fromSeq;
524 if (dna instanceof SequenceDummy
525 && dna.getName().equals(ds.getName()))
527 Mapping mapping = ssm.mapping;
528 int mapStart = mapping.getMap().getFromLowest();
529 int mapEnd = mapping.getMap().getFromHighest();
530 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
536 // TODO: new method ? ds.realise(dna);
537 // might want to copy database refs as well
538 ds.setSequenceFeatures(dna.getSequenceFeatures());
541 System.out.println("Realised mapped sequence " + ds.getName());
547 * check for replaceable protein ('map to') sequences
549 Mapping mapping = ssm.mapping;
550 SequenceI prot = mapping.getTo();
551 int mapStart = mapping.getMap().getToLowest();
552 int mapEnd = mapping.getMap().getToHighest();
553 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
559 // TODO: new method ? ds.realise(dna);
560 // might want to copy database refs as well
561 ds.setSequenceFeatures(dna.getSequenceFeatures());
562 ssm.mapping.setTo(ds);
570 * Helper method to test whether a 'real' sequence could replace a 'dummy'
571 * sequence in the map. The criteria are that they have the same name, and
572 * that the mapped region overlaps the candidate sequence.
580 protected static boolean couldRealiseSequence(SequenceI existing,
581 SequenceI replacement, int mapStart, int mapEnd)
583 if (existing instanceof SequenceDummy
584 && !(replacement instanceof SequenceDummy)
585 && existing.getName().equals(replacement.getName()))
587 int start = replacement.getStart();
588 int end = replacement.getEnd();
589 boolean mappingOverlapsSequence = (mapStart >= start
590 && mapStart <= end) || (mapEnd >= start && mapEnd <= end);
591 if (mappingOverlapsSequence)
600 * Change any mapping to the given sequence to be to its dataset sequence
601 * instead. For use when mappings are created before their referenced
602 * sequences are instantiated, for example when parsing GFF data.
606 public void updateToDataset(SequenceI seq)
608 if (seq == null || seq.getDatasetSequence() == null)
612 SequenceI ds = seq.getDatasetSequence();
614 for (SequenceMapping ssm : mappings)
619 if (ssm.fromSeq == seq)
627 if (ssm.mapping.to == seq)
635 * Answers true if this object contains no mappings
639 public boolean isEmpty()
641 return mappings.isEmpty();
645 * Method for debug / inspection purposes only, may change in future
648 public String toString()
650 return mappings == null ? "null" : mappings.toString();
654 * Returns the first mapping found that is between 'fromSeq' and 'toSeq', or
658 * aligned or dataset sequence
660 * aligned or dataset sequence
663 public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
665 SequenceI dssFrom = fromSeq.getDatasetSequence() == null ? fromSeq
666 : fromSeq.getDatasetSequence();
667 SequenceI dssTo = toSeq.getDatasetSequence() == null ? toSeq
668 : toSeq.getDatasetSequence();
670 for (SequenceMapping mapping : mappings)
672 SequenceI from = mapping.fromSeq;
673 SequenceI to = mapping.mapping.to;
674 if ((from == dssFrom && to == dssTo)
675 || (from == dssTo && to == dssFrom))
677 return mapping.mapping;
684 * Returns a hashcode derived from the list of sequence mappings
686 * @see SequenceMapping#hashCode()
687 * @see AbstractList#hashCode()
690 public int hashCode()
692 return this.mappings.hashCode();
696 * Two AlignedCodonFrame objects are equal if they hold the same ordered list
699 * @see SequenceToSequenceMapping#
702 public boolean equals(Object obj)
704 if (!(obj instanceof AlignedCodonFrame))
708 return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
711 public List<SequenceMapping> getMappings()