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.ArrayList;
27 import java.util.List;
30 * Stores mapping between the columns of a protein alignment and a DNA alignment
31 * and a list of individual codon to amino acid mappings between sequences.
33 public class AlignedCodonFrame
37 * Data bean to hold mappings from one sequence to another
39 private class SequenceToSequenceMapping
41 private SequenceI fromSeq;
43 private Mapping mapping;
45 SequenceToSequenceMapping(SequenceI from, Mapping map)
52 * Readable representation for debugging only, not guaranteed not to change
55 public String toString()
57 return String.format("From %s %s", fromSeq.getName(),
62 private List<SequenceToSequenceMapping> mappings;
67 public AlignedCodonFrame()
69 mappings = new ArrayList<SequenceToSequenceMapping>();
73 * Adds a mapping between the dataset sequences for the associated dna and
74 * protein sequence objects
80 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
82 // JBPNote DEBUG! THIS !
83 // dnaseq.transferAnnotation(aaseq, mp);
84 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
86 SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
87 : dnaseq.getDatasetSequence();
88 SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
89 .getDatasetSequence();
92 * if we already hold a mapping between these sequences, just add to it
94 for (SequenceToSequenceMapping ssm : mappings)
96 if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
98 ssm.mapping.map.addMapList(map);
104 * otherwise, add a new sequence mapping
106 Mapping mp = new Mapping(toSeq, map);
107 mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
110 public SequenceI[] getdnaSeqs()
112 // TODO return a list instead?
114 List<SequenceI> seqs = new ArrayList<SequenceI>();
115 for (SequenceToSequenceMapping ssm : mappings)
117 seqs.add(ssm.fromSeq);
119 return seqs.toArray(new SequenceI[seqs.size()]);
122 public SequenceI[] getAaSeqs()
124 // TODO not used - remove?
125 List<SequenceI> seqs = new ArrayList<SequenceI>();
126 for (SequenceToSequenceMapping ssm : mappings)
128 seqs.add(ssm.mapping.to);
130 return seqs.toArray(new SequenceI[seqs.size()]);
133 public MapList[] getdnaToProt()
135 List<MapList> maps = new ArrayList<MapList>();
136 for (SequenceToSequenceMapping ssm : mappings)
138 maps.add(ssm.mapping.map);
140 return maps.toArray(new MapList[maps.size()]);
143 public Mapping[] getProtMappings()
145 List<Mapping> maps = new ArrayList<Mapping>();
146 for (SequenceToSequenceMapping ssm : mappings)
148 maps.add(ssm.mapping);
150 return maps.toArray(new Mapping[maps.size()]);
154 * Returns the first mapping found which is to or from the given sequence, or
160 public Mapping getMappingForSequence(SequenceI seq)
162 SequenceI seqDs = seq.getDatasetSequence();
163 seqDs = seqDs != null ? seqDs : seq;
165 for (SequenceToSequenceMapping ssm : mappings)
167 if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
176 * Return the corresponding aligned or dataset aa sequence for given dna
177 * sequence, null if not found.
182 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
184 SequenceI dnads = dnaSeqRef.getDatasetSequence();
185 for (SequenceToSequenceMapping ssm : mappings)
187 if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
189 return ssm.mapping.to;
198 * @return null or corresponding aaSeq entry for dnaSeq entry
200 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
202 SequenceI aads = aaSeqRef.getDatasetSequence();
203 for (SequenceToSequenceMapping ssm : mappings)
205 if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
214 * test to see if codon frame involves seq in any way
217 * a nucleotide or protein sequence
218 * @return true if a mapping exists to or from this sequence to any translated
221 public boolean involvesSequence(SequenceI seq)
223 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
227 * Add search results for regions in other sequences that translate or are
228 * translated from a particular position in seq
234 * where highlighted regions go
236 public void markMappedRegion(SequenceI seq, int index,
237 SearchResults results)
240 SequenceI ds = seq.getDatasetSequence();
241 for (SequenceToSequenceMapping ssm : mappings)
243 if (ssm.fromSeq == seq || ssm.fromSeq == ds)
245 codon = ssm.mapping.map.locateInTo(index, index);
248 for (int i = 0; i < codon.length; i += 2)
250 results.addResult(ssm.mapping.to, codon[i], codon[i + 1]);
254 else if (ssm.mapping.to == seq || ssm.mapping.to == ds)
257 codon = ssm.mapping.map.locateInFrom(index, index);
260 for (int i = 0; i < codon.length; i += 2)
262 results.addResult(ssm.fromSeq, codon[i], codon[i + 1]);
271 * Returns the DNA codon positions (base 1) for the given position (base 1) in
272 * a mapped protein sequence, or null if no mapping is found.
274 * Intended for use in aligning cDNA to match aligned protein. Only the first
275 * mapping found is returned, so not suitable for use if multiple protein
276 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
277 * ill-defined for this case anyway).
280 * the DNA dataset sequence
282 * residue position (base 1) in a protein sequence
285 public int[] getDnaPosition(SequenceI seq, int aaPos)
288 * Adapted from markMappedRegion().
292 for (SequenceToSequenceMapping ssm : mappings)
294 if (ssm.fromSeq == seq)
296 ml = getdnaToProt()[i];
301 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
305 * Convenience method to return the first aligned sequence in the given
306 * alignment whose dataset has a mapping with the given dataset sequence.
313 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
316 * Search mapped protein ('to') sequences first.
318 for (SequenceToSequenceMapping ssm : mappings)
320 if (ssm.fromSeq == seq)
322 for (SequenceI sourceAligned : al.getSequences())
324 if (ssm.mapping.to == sourceAligned.getDatasetSequence())
326 return sourceAligned;
333 * Then try mapped dna sequences.
335 for (SequenceToSequenceMapping ssm : mappings)
337 if (ssm.mapping.to == seq)
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 : query
368 .getDatasetSequence();
369 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
373 for (SequenceToSequenceMapping 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<char[]>();
419 for (SequenceToSequenceMapping ssm : mappings)
421 if (ssm.mapping.to == protein)
423 ml = ssm.mapping.map;
424 dnaSeq = ssm.fromSeq;
426 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
427 if (codonPos == null)
433 * Read off the mapped nucleotides (converting to position base 0)
435 codonPos = MappingUtils.flattenRanges(codonPos);
436 char[] dna = dnaSeq.getSequence();
437 int start = dnaSeq.getStart();
438 result.add(new char[] { dna[codonPos[0] - start],
439 dna[codonPos[1] - start], dna[codonPos[2] - start] });
442 return result.isEmpty() ? null : result;
446 * Returns any mappings found which are to (or from) the given sequence, and
447 * to distinct sequences.
452 public List<Mapping> getMappingsForSequence(SequenceI seq)
454 List<Mapping> result = new ArrayList<Mapping>();
455 List<SequenceI> related = new ArrayList<SequenceI>();
456 SequenceI seqDs = seq.getDatasetSequence();
457 seqDs = seqDs != null ? seqDs : seq;
459 for (SequenceToSequenceMapping ssm : mappings)
461 final Mapping mapping = ssm.mapping;
462 if (ssm.fromSeq == seqDs || mapping.to == seqDs)
464 if (!related.contains(mapping.to))
467 related.add(mapping.to);
475 * Test whether the given sequence is substitutable for one or more dummy
476 * sequences in this mapping
482 public boolean isRealisableWith(SequenceI seq)
484 return realiseWith(seq, false) > 0;
488 * Replace any matchable mapped dummy sequences with the given real one.
489 * Returns the count of sequence mappings instantiated.
494 public int realiseWith(SequenceI seq)
496 return realiseWith(seq, true);
500 * Returns the number of mapped dummy sequences that could be replaced with
501 * the given real sequence.
506 * if true, performs replacements, else only counts
509 protected int realiseWith(SequenceI seq, boolean doUpdate)
511 SequenceI ds = seq.getDatasetSequence() != null ? seq
512 .getDatasetSequence() : seq;
516 * check for replaceable DNA ('map from') sequences
518 for (SequenceToSequenceMapping ssm : mappings)
520 SequenceI dna = ssm.fromSeq;
521 if (dna instanceof SequenceDummy
522 && dna.getName().equals(ds.getName()))
524 Mapping mapping = ssm.mapping;
525 int mapStart = mapping.getMap().getFromLowest();
526 int mapEnd = mapping.getMap().getFromHighest();
527 boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
533 // TODO: new method ? ds.realise(dna);
534 // might want to copy database refs as well
535 ds.setSequenceFeatures(dna.getSequenceFeatures());
538 System.out.println("Realised mapped sequence " + ds.getName());
544 * check for replaceable protein ('map to') sequences
546 Mapping mapping = ssm.mapping;
547 SequenceI prot = mapping.getTo();
548 int mapStart = mapping.getMap().getToLowest();
549 int mapEnd = mapping.getMap().getToHighest();
550 boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
556 // TODO: new method ? ds.realise(dna);
557 // might want to copy database refs as well
558 ds.setSequenceFeatures(dna.getSequenceFeatures());
559 ssm.mapping.setTo(ds);
567 * Helper method to test whether a 'real' sequence could replace a 'dummy'
568 * sequence in the map. The criteria are that they have the same name, and
569 * that the mapped region overlaps the candidate sequence.
577 protected static boolean couldRealiseSequence(SequenceI existing,
578 SequenceI replacement, int mapStart, int mapEnd)
580 if (existing instanceof SequenceDummy
581 && !(replacement instanceof SequenceDummy)
582 && existing.getName().equals(replacement.getName()))
584 int start = replacement.getStart();
585 int end = replacement.getEnd();
586 boolean mappingOverlapsSequence = (mapStart >= start && mapStart <= end)
587 || (mapEnd >= start && mapEnd <= end);
588 if (mappingOverlapsSequence)
597 * Change any mapping to the given sequence to be to its dataset sequence
598 * instead. For use when mappings are created before their referenced
599 * sequences are instantiated, for example when parsing GFF data.
603 public void updateToDataset(SequenceI seq)
605 if (seq == null || seq.getDatasetSequence() == null)
609 SequenceI ds = seq.getDatasetSequence();
611 for (SequenceToSequenceMapping ssm : mappings)
616 if (ssm.fromSeq == seq)
624 if (ssm.mapping.to == seq)
632 * Answers true if this object contains no mappings
636 public boolean isEmpty()
638 return mappings.isEmpty();