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