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 (aligned or dataset)
314 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
317 * Search mapped protein ('to') sequences first.
319 for (SequenceToSequenceMapping ssm : mappings)
321 if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
323 for (SequenceI sourceAligned : al.getSequences())
325 if (ssm.mapping.to == sourceAligned.getDatasetSequence()
326 || ssm.mapping.to == sourceAligned)
328 return sourceAligned;
335 * Then try mapped dna sequences.
337 for (SequenceToSequenceMapping ssm : mappings)
339 if (ssm.mapping.to == seq
340 || ssm.mapping.to == seq.getDatasetSequence())
342 for (SequenceI sourceAligned : al.getSequences())
344 if (ssm.fromSeq == sourceAligned.getDatasetSequence())
346 return sourceAligned;
356 * Returns the region in the target sequence's dataset that is mapped to the
357 * given position (base 1) in the query sequence's dataset. The region is a
358 * set of start/end position pairs.
365 public int[] getMappedRegion(SequenceI target, SequenceI query,
368 SequenceI targetDs = target.getDatasetSequence() == null ? target
369 : target.getDatasetSequence();
370 SequenceI queryDs = query.getDatasetSequence() == null ? query : query
371 .getDatasetSequence();
372 if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
376 for (SequenceToSequenceMapping ssm : mappings)
379 * try mapping from target to query
381 if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
383 int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
390 * else try mapping from query to target
392 else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
394 int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
405 * Returns the mapped DNA codons for the given position in a protein sequence,
406 * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
407 * codons. There may be more than one codon mapped to the protein if (for
408 * example), there are mappings to cDNA variants.
411 * the peptide dataset sequence
413 * residue position (base 1) in the peptide sequence
416 public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
419 SequenceI dnaSeq = null;
420 List<char[]> result = new ArrayList<char[]>();
422 for (SequenceToSequenceMapping ssm : mappings)
424 if (ssm.mapping.to == protein)
426 ml = ssm.mapping.map;
427 dnaSeq = ssm.fromSeq;
429 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
430 if (codonPos == null)
436 * Read off the mapped nucleotides (converting to position base 0)
438 codonPos = MappingUtils.flattenRanges(codonPos);
439 char[] dna = dnaSeq.getSequence();
440 int start = dnaSeq.getStart();
441 result.add(new char[] { dna[codonPos[0] - start],
442 dna[codonPos[1] - start], dna[codonPos[2] - start] });
445 return result.isEmpty() ? null : result;
449 * Returns any mappings found which are from the given sequence, and to
450 * distinct sequences.
455 public List<Mapping> getMappingsFromSequence(SequenceI seq)
457 List<Mapping> result = new ArrayList<Mapping>();
458 List<SequenceI> related = new ArrayList<SequenceI>();
459 SequenceI seqDs = seq.getDatasetSequence();
460 seqDs = seqDs != null ? seqDs : seq;
462 for (SequenceToSequenceMapping ssm : mappings)
464 final Mapping mapping = ssm.mapping;
465 if (ssm.fromSeq == seqDs)
467 if (!related.contains(mapping.to))
470 related.add(mapping.to);
478 * Test whether the given sequence is substitutable for one or more dummy
479 * sequences in this mapping
485 public boolean isRealisableWith(SequenceI seq)
487 return realiseWith(seq, false) > 0;
491 * Replace any matchable mapped dummy sequences with the given real one.
492 * Returns the count of sequence mappings instantiated.
497 public int realiseWith(SequenceI seq)
499 return realiseWith(seq, true);
503 * Returns the number of mapped dummy sequences that could be replaced with
504 * the given real sequence.
509 * if true, performs replacements, else only counts
512 protected int realiseWith(SequenceI seq, boolean doUpdate)
514 SequenceI ds = seq.getDatasetSequence() != null ? seq
515 .getDatasetSequence() : seq;
519 * check for replaceable DNA ('map from') sequences
521 for (SequenceToSequenceMapping 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 && mapStart <= end)
590 || (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 (SequenceToSequenceMapping 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 from 'fromSeq' to 'toSeq', or null
658 * aligned or dataset sequence
660 * aligned or dataset sequence
663 public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
665 for (SequenceToSequenceMapping mapping : mappings)
667 SequenceI from = mapping.fromSeq;
668 SequenceI to = mapping.mapping.to;
669 if ((from == fromSeq || from == fromSeq.getDatasetSequence())
670 && (to == toSeq || to == toSeq.getDatasetSequence()))
672 return mapping.mapping;