5240f0cf7bb1bbdc035c3e8e46bfe3622ac7b75c
[jalview.git] / src / jalview / datamodel / AlignedCodonFrame.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel;
22
23 import java.util.AbstractList;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import jalview.util.MapList;
28 import jalview.util.MappingUtils;
29
30 /**
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.
33  */
34 public class AlignedCodonFrame
35 {
36
37   /*
38    * Data bean to hold mappings from one sequence to another
39    */
40   public class SequenceToSequenceMapping
41   {
42     private SequenceI fromSeq;
43
44     private Mapping mapping;
45
46     SequenceToSequenceMapping(SequenceI from, Mapping map)
47     {
48       this.fromSeq = from;
49       this.mapping = map;
50     }
51
52     /**
53      * Readable representation for debugging only, not guaranteed not to change
54      */
55     @Override
56     public String toString()
57     {
58       return String.format("From %s %s", fromSeq.getName(),
59               mapping.toString());
60     }
61
62     /**
63      * Returns a hashCode derived from the hashcodes of the mappings and fromSeq
64      * 
65      * @see SequenceToSequenceMapping#hashCode()
66      */
67     @Override
68     public int hashCode()
69     {
70       return (fromSeq == null ? 0 : fromSeq.hashCode() * 31)
71               + mapping.hashCode();
72     }
73
74     /**
75      * Answers true if the objects hold the same mapping between the same two
76      * sequences
77      * 
78      * @see Mapping#equals
79      */
80     @Override
81     public boolean equals(Object obj)
82     {
83       if (!(obj instanceof SequenceToSequenceMapping))
84       {
85         return false;
86       }
87       SequenceToSequenceMapping that = (SequenceToSequenceMapping) obj;
88       if (this.mapping == null)
89       {
90         return that.mapping == null;
91       }
92       // TODO: can simplify by asserting fromSeq is a dataset sequence
93       return (this.fromSeq == that.fromSeq
94               || (this.fromSeq != null && that.fromSeq != null
95                       && this.fromSeq.getDatasetSequence() != null
96                       && this.fromSeq.getDatasetSequence() == that.fromSeq
97                               .getDatasetSequence()))
98               && this.mapping.equals(that.mapping);
99     }
100
101     public SequenceI getFromSeq()
102     {
103       return fromSeq;
104     }
105
106     public Mapping getMapping()
107     {
108       return mapping;
109     }
110
111     /**
112      * Returns true if the mapping covers the full length of the given sequence.
113      * This allows us to distinguish the CDS that codes for a protein from
114      * another overlapping CDS in the parent dna sequence.
115      * 
116      * @param seq
117      * @return
118      */
119     public boolean covers(SequenceI seq)
120     {
121       List<int[]> mappedRanges = null;
122       MapList mapList = mapping.getMap();
123       if (fromSeq == seq || fromSeq == seq.getDatasetSequence())
124       {
125         mappedRanges = mapList.getFromRanges();
126       }
127       else if (mapping.to == seq || mapping.to == seq.getDatasetSequence())
128       {
129         mappedRanges = mapList.getToRanges();
130       }
131       else
132       {
133         return false;
134       }
135
136       /*
137        * check that each mapped range lies within the sequence range
138        * (necessary for circular CDS - example EMBL:J03321:AAA91567)
139        * and mapped length covers (at least) sequence length
140        */
141       int length = 0;
142       for (int[] range : mappedRanges)
143       {
144         int from = Math.min(range[0], range[1]);
145         int to = Math.max(range[0], range[1]);
146         if (from < seq.getStart() || to > seq.getEnd())
147         {
148           return false;
149         }
150         length += (to - from + 1);
151       }
152       // add 1 to mapped length to allow for a mapped stop codon
153       if (length + 1 < (seq.getEnd() - seq.getStart() + 1))
154       {
155         return false;
156       }
157       return true;
158     }
159
160     /**
161      * Adds any regions mapped to or from position {@code pos} in sequence
162      * {@code seq} to the given search results
163      * 
164      * @param seq
165      * @param pos
166      * @param sr
167      */
168     public void markMappedRegion(SequenceI seq, int pos, SearchResultsI sr)
169     {
170       int[] codon = null;
171       SequenceI mappedSeq = null;
172       SequenceI ds = seq.getDatasetSequence();
173       if (ds == null)
174       {
175         ds = seq;
176       }
177       
178       if (this.fromSeq == seq || this.fromSeq == ds)
179       {
180         codon = this.mapping.map.locateInTo(pos, pos);
181         mappedSeq = this.mapping.to;
182       }
183       else if (this.mapping.to == seq || this.mapping.to == ds)
184       {
185         codon = this.mapping.map.locateInFrom(pos, pos);
186         mappedSeq = this.fromSeq;
187       }
188
189       if (codon != null)
190       {
191         for (int i = 0; i < codon.length; i += 2)
192         {
193           sr.addResult(mappedSeq, codon[i], codon[i + 1]);
194         }
195       }
196     }
197   }
198
199   private List<SequenceToSequenceMapping> mappings;
200
201   /**
202    * Constructor
203    */
204   public AlignedCodonFrame()
205   {
206     mappings = new ArrayList<>();
207   }
208
209   /**
210    * Adds a mapping between the dataset sequences for the associated dna and
211    * protein sequence objects
212    * 
213    * @param dnaseq
214    * @param aaseq
215    * @param map
216    */
217   public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
218   {
219     addMap(dnaseq, aaseq, map, null);
220   }
221
222   /**
223    * Adds a mapping between the dataset sequences for the associated dna and
224    * protein sequence objects
225    * 
226    * @param dnaseq
227    * @param aaseq
228    * @param map
229    * @param mapFromId
230    */
231   public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map,
232           String mapFromId)
233   {
234     // JBPNote DEBUG! THIS !
235     // dnaseq.transferAnnotation(aaseq, mp);
236     // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
237
238     SequenceI fromSeq = (dnaseq.getDatasetSequence() == null) ? dnaseq
239             : dnaseq.getDatasetSequence();
240     SequenceI toSeq = (aaseq.getDatasetSequence() == null) ? aaseq
241             : aaseq.getDatasetSequence();
242
243     /*
244      * if we already hold a mapping between these sequences, just add to it 
245      * note that 'adding' a duplicate map does nothing; this protects against
246      * creating duplicate mappings in AlignedCodonFrame
247      */
248     for (SequenceToSequenceMapping ssm : mappings)
249     {
250       if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
251       {
252         ssm.mapping.map.addMapList(map);
253         return;
254       }
255     }
256
257     /*
258      * otherwise, add a new sequence mapping
259      */
260     Mapping mp = new Mapping(toSeq, map);
261     mp.setMappedFromId(mapFromId);
262     mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
263   }
264
265   public SequenceI[] getdnaSeqs()
266   {
267     // TODO return a list instead?
268     // return dnaSeqs;
269     List<SequenceI> seqs = new ArrayList<>();
270     for (SequenceToSequenceMapping ssm : mappings)
271     {
272       seqs.add(ssm.fromSeq);
273     }
274     return seqs.toArray(new SequenceI[seqs.size()]);
275   }
276
277   public SequenceI[] getAaSeqs()
278   {
279     // TODO not used - remove?
280     List<SequenceI> seqs = new ArrayList<>();
281     for (SequenceToSequenceMapping ssm : mappings)
282     {
283       seqs.add(ssm.mapping.to);
284     }
285     return seqs.toArray(new SequenceI[seqs.size()]);
286   }
287
288   public MapList[] getdnaToProt()
289   {
290     List<MapList> maps = new ArrayList<>();
291     for (SequenceToSequenceMapping ssm : mappings)
292     {
293       maps.add(ssm.mapping.map);
294     }
295     return maps.toArray(new MapList[maps.size()]);
296   }
297
298   public Mapping[] getProtMappings()
299   {
300     List<Mapping> maps = new ArrayList<>();
301     for (SequenceToSequenceMapping ssm : mappings)
302     {
303       maps.add(ssm.mapping);
304     }
305     return maps.toArray(new Mapping[maps.size()]);
306   }
307
308   /**
309    * Returns the first mapping found which is to or from the given sequence, or
310    * null if none is found
311    * 
312    * @param seq
313    * @return
314    */
315   public Mapping getMappingForSequence(SequenceI seq)
316   {
317     SequenceI seqDs = seq.getDatasetSequence();
318     seqDs = seqDs != null ? seqDs : seq;
319
320     for (SequenceToSequenceMapping ssm : mappings)
321     {
322       if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
323       {
324         return ssm.mapping;
325       }
326     }
327     return null;
328   }
329
330   /**
331    * Return the corresponding aligned or dataset aa sequence for given dna
332    * sequence, null if not found.
333    * 
334    * @param sequenceRef
335    * @return
336    */
337   public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
338   {
339     SequenceI dnads = dnaSeqRef.getDatasetSequence();
340     for (SequenceToSequenceMapping ssm : mappings)
341     {
342       if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
343       {
344         return ssm.mapping.to;
345       }
346     }
347     return null;
348   }
349
350   /**
351    * Return the corresponding aligned or dataset dna sequence for given amino
352    * acid sequence, or null if not found. returns the sequence from the first
353    * mapping found that involves the protein sequence.
354    * 
355    * @param aaSeqRef
356    * @return
357    */
358   public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
359   {
360     SequenceI aads = aaSeqRef.getDatasetSequence();
361     for (SequenceToSequenceMapping ssm : mappings)
362     {
363       if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
364       {
365         return ssm.fromSeq;
366       }
367     }
368     return null;
369   }
370
371   /**
372    * test to see if codon frame involves seq in any way
373    * 
374    * @param seq
375    *          a nucleotide or protein sequence
376    * @return true if a mapping exists to or from this sequence to any translated
377    *         sequence
378    */
379   public boolean involvesSequence(SequenceI seq)
380   {
381     return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
382   }
383
384   /**
385    * Add search results for regions in other sequences that translate or are
386    * translated from a particular position in seq
387    * 
388    * @param seq
389    * @param index
390    *          position in seq
391    * @param results
392    *          where highlighted regions go
393    */
394   public void markMappedRegion(SequenceI seq, int index,
395           SearchResultsI results)
396   {
397     SequenceI ds = seq.getDatasetSequence();
398     for (SequenceToSequenceMapping ssm : mappings)
399     {
400       ssm.markMappedRegion(ds, index, results);
401     }
402   }
403
404   /**
405    * Returns the DNA codon positions (base 1) for the given position (base 1) in
406    * a mapped protein sequence, or null if no mapping is found.
407    * 
408    * Intended for use in aligning cDNA to match aligned protein. Only the first
409    * mapping found is returned, so not suitable for use if multiple protein
410    * sequences are mapped to the same cDNA (but aligning cDNA as protein is
411    * ill-defined for this case anyway).
412    * 
413    * @param seq
414    *          the DNA dataset sequence
415    * @param aaPos
416    *          residue position (base 1) in a protein sequence
417    * @return
418    */
419   public int[] getDnaPosition(SequenceI seq, int aaPos)
420   {
421     /*
422      * Adapted from markMappedRegion().
423      */
424     MapList ml = null;
425     int i = 0;
426     for (SequenceToSequenceMapping ssm : mappings)
427     {
428       if (ssm.fromSeq == seq)
429       {
430         ml = getdnaToProt()[i];
431         break;
432       }
433       i++;
434     }
435     return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
436   }
437
438   /**
439    * Convenience method to return the first aligned sequence in the given
440    * alignment whose dataset has a mapping with the given (aligned or dataset)
441    * sequence.
442    * 
443    * @param seq
444    * 
445    * @param al
446    * @return
447    */
448   public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
449   {
450     /*
451      * Search mapped protein ('to') sequences first.
452      */
453     for (SequenceToSequenceMapping ssm : mappings)
454     {
455       if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
456       {
457         for (SequenceI sourceAligned : al.getSequences())
458         {
459           if (ssm.mapping.to == sourceAligned.getDatasetSequence()
460                   || ssm.mapping.to == sourceAligned)
461           {
462             return sourceAligned;
463           }
464         }
465       }
466     }
467
468     /*
469      * Then try mapped dna sequences.
470      */
471     for (SequenceToSequenceMapping ssm : mappings)
472     {
473       if (ssm.mapping.to == seq
474               || ssm.mapping.to == seq.getDatasetSequence())
475       {
476         for (SequenceI sourceAligned : al.getSequences())
477         {
478           if (ssm.fromSeq == sourceAligned.getDatasetSequence())
479           {
480             return sourceAligned;
481           }
482         }
483       }
484     }
485
486     return null;
487   }
488
489   /**
490    * Returns the region in the target sequence's dataset that is mapped to the
491    * given position (base 1) in the query sequence's dataset. The region is a
492    * set of start/end position pairs.
493    * 
494    * @param target
495    * @param query
496    * @param queryPos
497    * @return
498    */
499   public int[] getMappedRegion(SequenceI target, SequenceI query,
500           int queryPos)
501   {
502     SequenceI targetDs = target.getDatasetSequence() == null ? target
503             : target.getDatasetSequence();
504     SequenceI queryDs = query.getDatasetSequence() == null ? query
505             : query.getDatasetSequence();
506     if (targetDs == null || queryDs == null /*|| dnaToProt == null*/)
507     {
508       return null;
509     }
510     for (SequenceToSequenceMapping ssm : mappings)
511     {
512       /*
513        * try mapping from target to query
514        */
515       if (ssm.fromSeq == targetDs && ssm.mapping.to == queryDs)
516       {
517         int[] codon = ssm.mapping.map.locateInFrom(queryPos, queryPos);
518         if (codon != null)
519         {
520           return codon;
521         }
522       }
523       /*
524        * else try mapping from query to target
525        */
526       else if (ssm.fromSeq == queryDs && ssm.mapping.to == targetDs)
527       {
528         int[] codon = ssm.mapping.map.locateInTo(queryPos, queryPos);
529         if (codon != null)
530         {
531           return codon;
532         }
533       }
534     }
535     return null;
536   }
537
538   /**
539    * Returns the mapped DNA codons for the given position in a protein sequence,
540    * or null if no mapping is found. Returns a list of (e.g.) ['g', 'c', 't']
541    * codons. There may be more than one codon mapped to the protein if (for
542    * example), there are mappings to cDNA variants.
543    * 
544    * @param protein
545    *          the peptide dataset sequence
546    * @param aaPos
547    *          residue position (base 1) in the peptide sequence
548    * @return
549    */
550   public List<char[]> getMappedCodons(SequenceI protein, int aaPos)
551   {
552     MapList ml = null;
553     SequenceI dnaSeq = null;
554     List<char[]> result = new ArrayList<>();
555
556     for (SequenceToSequenceMapping ssm : mappings)
557     {
558       if (ssm.mapping.to == protein
559               && ssm.mapping.getMap().getFromRatio() == 3)
560       {
561         ml = ssm.mapping.map;
562         dnaSeq = ssm.fromSeq;
563
564         int[] codonPos = ml.locateInFrom(aaPos, aaPos);
565         if (codonPos == null)
566         {
567           return null;
568         }
569
570         /*
571          * Read off the mapped nucleotides (converting to position base 0)
572          */
573         codonPos = MappingUtils.flattenRanges(codonPos);
574         int start = dnaSeq.getStart();
575         char c1 = dnaSeq.getCharAt(codonPos[0] - start);
576         char c2 = dnaSeq.getCharAt(codonPos[1] - start);
577         char c3 = dnaSeq.getCharAt(codonPos[2] - start);
578         result.add(new char[] { c1, c2, c3 });
579       }
580     }
581     return result.isEmpty() ? null : result;
582   }
583
584   /**
585    * Returns any mappings found which are from the given sequence, and to
586    * distinct sequences.
587    * 
588    * @param seq
589    * @return
590    */
591   public List<Mapping> getMappingsFromSequence(SequenceI seq)
592   {
593     List<Mapping> result = new ArrayList<>();
594     List<SequenceI> related = new ArrayList<>();
595     SequenceI seqDs = seq.getDatasetSequence();
596     seqDs = seqDs != null ? seqDs : seq;
597
598     for (SequenceToSequenceMapping ssm : mappings)
599     {
600       final Mapping mapping = ssm.mapping;
601       if (ssm.fromSeq == seqDs)
602       {
603         if (!related.contains(mapping.to))
604         {
605           result.add(mapping);
606           related.add(mapping.to);
607         }
608       }
609     }
610     return result;
611   }
612
613   /**
614    * Test whether the given sequence is substitutable for one or more dummy
615    * sequences in this mapping
616    * 
617    * @param map
618    * @param seq
619    * @return
620    */
621   public boolean isRealisableWith(SequenceI seq)
622   {
623     return realiseWith(seq, false) > 0;
624   }
625
626   /**
627    * Replace any matchable mapped dummy sequences with the given real one.
628    * Returns the count of sequence mappings instantiated.
629    * 
630    * @param seq
631    * @return
632    */
633   public int realiseWith(SequenceI seq)
634   {
635     return realiseWith(seq, true);
636   }
637
638   /**
639    * Returns the number of mapped dummy sequences that could be replaced with
640    * the given real sequence.
641    * 
642    * @param seq
643    *          a dataset sequence
644    * @param doUpdate
645    *          if true, performs replacements, else only counts
646    * @return
647    */
648   protected int realiseWith(SequenceI seq, boolean doUpdate)
649   {
650     SequenceI ds = seq.getDatasetSequence() != null
651             ? seq.getDatasetSequence()
652             : seq;
653     int count = 0;
654
655     /*
656      * check for replaceable DNA ('map from') sequences
657      */
658     for (SequenceToSequenceMapping ssm : mappings)
659     {
660       SequenceI dna = ssm.fromSeq;
661       if (dna instanceof SequenceDummy
662               && dna.getName().equals(ds.getName()))
663       {
664         Mapping mapping = ssm.mapping;
665         int mapStart = mapping.getMap().getFromLowest();
666         int mapEnd = mapping.getMap().getFromHighest();
667         boolean mappable = couldRealiseSequence(dna, ds, mapStart, mapEnd);
668         if (mappable)
669         {
670           count++;
671           if (doUpdate)
672           {
673             // TODO: new method ? ds.realise(dna);
674             // might want to copy database refs as well
675             ds.setSequenceFeatures(dna.getSequenceFeatures());
676             // dnaSeqs[i] = ds;
677             ssm.fromSeq = ds;
678             System.out.println("Realised mapped sequence " + ds.getName());
679           }
680         }
681       }
682
683       /*
684        * check for replaceable protein ('map to') sequences
685        */
686       Mapping mapping = ssm.mapping;
687       SequenceI prot = mapping.getTo();
688       int mapStart = mapping.getMap().getToLowest();
689       int mapEnd = mapping.getMap().getToHighest();
690       boolean mappable = couldRealiseSequence(prot, ds, mapStart, mapEnd);
691       if (mappable)
692       {
693         count++;
694         if (doUpdate)
695         {
696           // TODO: new method ? ds.realise(dna);
697           // might want to copy database refs as well
698           ds.setSequenceFeatures(dna.getSequenceFeatures());
699           ssm.mapping.setTo(ds);
700         }
701       }
702     }
703     return count;
704   }
705
706   /**
707    * Helper method to test whether a 'real' sequence could replace a 'dummy'
708    * sequence in the map. The criteria are that they have the same name, and
709    * that the mapped region overlaps the candidate sequence.
710    * 
711    * @param existing
712    * @param replacement
713    * @param mapStart
714    * @param mapEnd
715    * @return
716    */
717   protected static boolean couldRealiseSequence(SequenceI existing,
718           SequenceI replacement, int mapStart, int mapEnd)
719   {
720     if (existing instanceof SequenceDummy
721             && !(replacement instanceof SequenceDummy)
722             && existing.getName().equals(replacement.getName()))
723     {
724       int start = replacement.getStart();
725       int end = replacement.getEnd();
726       boolean mappingOverlapsSequence = (mapStart >= start
727               && mapStart <= end) || (mapEnd >= start && mapEnd <= end);
728       if (mappingOverlapsSequence)
729       {
730         return true;
731       }
732     }
733     return false;
734   }
735
736   /**
737    * Change any mapping to the given sequence to be to its dataset sequence
738    * instead. For use when mappings are created before their referenced
739    * sequences are instantiated, for example when parsing GFF data.
740    * 
741    * @param seq
742    */
743   public void updateToDataset(SequenceI seq)
744   {
745     if (seq == null || seq.getDatasetSequence() == null)
746     {
747       return;
748     }
749     SequenceI ds = seq.getDatasetSequence();
750
751     for (SequenceToSequenceMapping ssm : mappings)
752     /*
753      * 'from' sequences
754      */
755     {
756       if (ssm.fromSeq == seq)
757       {
758         ssm.fromSeq = ds;
759       }
760
761       /*
762        * 'to' sequences
763        */
764       if (ssm.mapping.to == seq)
765       {
766         ssm.mapping.to = ds;
767       }
768     }
769   }
770
771   /**
772    * Answers true if this object contains no mappings
773    * 
774    * @return
775    */
776   public boolean isEmpty()
777   {
778     return mappings.isEmpty();
779   }
780
781   /**
782    * Method for debug / inspection purposes only, may change in future
783    */
784   @Override
785   public String toString()
786   {
787     return mappings == null ? "null" : mappings.toString();
788   }
789
790   /**
791    * Returns the first mapping found that is between 'fromSeq' and 'toSeq', or
792    * null if none found
793    * 
794    * @param fromSeq
795    *          aligned or dataset sequence
796    * @param toSeq
797    *          aligned or dataset sequence
798    * @return
799    */
800   public Mapping getMappingBetween(SequenceI fromSeq, SequenceI toSeq)
801   {
802     SequenceI dssFrom = fromSeq.getDatasetSequence() == null ? fromSeq
803             : fromSeq.getDatasetSequence();
804     SequenceI dssTo = toSeq.getDatasetSequence() == null ? toSeq
805             : toSeq.getDatasetSequence();
806
807     for (SequenceToSequenceMapping mapping : mappings)
808     {
809       SequenceI from = mapping.fromSeq;
810       SequenceI to = mapping.mapping.to;
811       if ((from == dssFrom && to == dssTo)
812               || (from == dssTo && to == dssFrom))
813       {
814         return mapping.mapping;
815       }
816     }
817     return null;
818   }
819
820   /**
821    * Returns a hashcode derived from the list of sequence mappings
822    * 
823    * @see SequenceToSequenceMapping#hashCode()
824    * @see AbstractList#hashCode()
825    */
826   @Override
827   public int hashCode()
828   {
829     return this.mappings.hashCode();
830   }
831
832   /**
833    * Two AlignedCodonFrame objects are equal if they hold the same ordered list
834    * of mappings
835    * 
836    * @see SequenceToSequenceMapping#equals
837    */
838   @Override
839   public boolean equals(Object obj)
840   {
841     if (!(obj instanceof AlignedCodonFrame))
842     {
843       return false;
844     }
845     return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
846   }
847
848   public List<SequenceToSequenceMapping> getMappings()
849   {
850     return mappings;
851   }
852
853   /**
854    * Returns the first mapping found which is between the two given sequences,
855    * and covers the full extent of both.
856    * 
857    * @param seq1
858    * @param seq2
859    * @return
860    */
861   public SequenceToSequenceMapping getCoveringMapping(SequenceI seq1,
862           SequenceI seq2)
863   {
864     for (SequenceToSequenceMapping mapping : mappings)
865     {
866       if (mapping.covers(seq2) && mapping.covers(seq1))
867       {
868         return mapping;
869       }
870     }
871     return null;
872   }
873
874   /**
875    * Returns the first mapping found which is between the given sequence and
876    * another, is a triplet mapping (3:1 or 1:3), and covers the full extent of
877    * both sequences involved.
878    * 
879    * @param seq
880    * @return
881    */
882   public SequenceToSequenceMapping getCoveringCodonMapping(SequenceI seq)
883   {
884     for (SequenceToSequenceMapping mapping : mappings)
885     {
886       if (mapping.getMapping().getMap().isTripletMap()
887               && mapping.covers(seq))
888       {
889         if (mapping.fromSeq == seq
890                 && mapping.covers(mapping.getMapping().getTo()))
891         {
892           return mapping;
893         }
894         else if (mapping.getMapping().getTo() == seq
895                 && mapping.covers(mapping.fromSeq))
896         {
897           return mapping;
898         }
899       }
900     }
901     return null;
902   }
903 }