9c173063c16fcc75e66da881a1a07a588dd5d7e2
[jalview.git] / src / jalview / datamodel / AlignedCodonFrame.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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
25 /**
26  * Stores mapping between the columns of a protein alignment and a DNA alignment
27  * and a list of individual codon to amino acid mappings between sequences.
28  */
29 public class AlignedCodonFrame
30 {
31
32   /*
33    * tied array of na Sequence objects.
34    */
35   private SequenceI[] dnaSeqs = null;
36
37   /*
38    * tied array of Mappings to protein sequence Objects and SequenceI[]
39    * aaSeqs=null; MapLists where each maps from the corresponding dnaSeqs
40    * element to corresponding aaSeqs element
41    */
42   private Mapping[] dnaToProt = null;
43
44   /**
45    * Constructor
46    */
47   public AlignedCodonFrame()
48   {
49   }
50
51   /**
52    * Adds a mapping between the dataset sequences for the associated dna and
53    * protein sequence objects
54    * 
55    * @param dnaseq
56    * @param aaseq
57    * @param map
58    */
59   public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
60   {
61     int nlen = 1;
62     if (dnaSeqs != null)
63     {
64       nlen = dnaSeqs.length + 1;
65     }
66     SequenceI[] ndna = new SequenceI[nlen];
67     Mapping[] ndtp = new Mapping[nlen];
68     if (dnaSeqs != null)
69     {
70       System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
71       System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
72     }
73     dnaSeqs = ndna;
74     dnaToProt = ndtp;
75     nlen--;
76     dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
77             .getDatasetSequence();
78     Mapping mp = new Mapping(map);
79     // JBPNote DEBUG! THIS !
80     // dnaseq.transferAnnotation(aaseq, mp);
81     // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
82     mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
83             .getDatasetSequence();
84     dnaToProt[nlen] = mp;
85   }
86
87   public SequenceI[] getdnaSeqs()
88   {
89     return dnaSeqs;
90   }
91
92   public SequenceI[] getAaSeqs()
93   {
94     if (dnaToProt == null)
95     {
96       return null;
97     }
98     SequenceI[] sqs = new SequenceI[dnaToProt.length];
99     for (int sz = 0; sz < dnaToProt.length; sz++)
100     {
101       sqs[sz] = dnaToProt[sz].to;
102     }
103     return sqs;
104   }
105
106   public MapList[] getdnaToProt()
107   {
108     if (dnaToProt == null)
109     {
110       return null;
111     }
112     MapList[] sqs = new MapList[dnaToProt.length];
113     for (int sz = 0; sz < dnaToProt.length; sz++)
114     {
115       sqs[sz] = dnaToProt[sz].map;
116     }
117     return sqs;
118   }
119
120   public Mapping[] getProtMappings()
121   {
122     return dnaToProt;
123   }
124
125   /**
126    * Return the corresponding aligned or dataset aa sequence for given dna
127    * sequence, null if not found.
128    * 
129    * @param sequenceRef
130    * @return
131    */
132   public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
133   {
134     if (dnaSeqs == null)
135     {
136       return null;
137     }
138     SequenceI dnads = dnaSeqRef.getDatasetSequence();
139     for (int ds = 0; ds < dnaSeqs.length; ds++)
140     {
141       if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
142       {
143         return dnaToProt[ds].to;
144       }
145     }
146     return null;
147   }
148
149   /**
150    * 
151    * @param sequenceRef
152    * @return null or corresponding aaSeq entry for dnaSeq entry
153    */
154   public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
155   {
156     if (dnaToProt == null)
157     {
158       return null;
159     }
160     SequenceI aads = aaSeqRef.getDatasetSequence();
161     for (int as = 0; as < dnaToProt.length; as++)
162     {
163       if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
164       {
165         return dnaSeqs[as];
166       }
167     }
168     return null;
169   }
170
171   /**
172    * test to see if codon frame involves seq in any way
173    * 
174    * @param seq
175    *          a nucleotide or protein sequence
176    * @return true if a mapping exists to or from this sequence to any translated
177    *         sequence
178    */
179   public boolean involvesSequence(SequenceI seq)
180   {
181     return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
182   }
183
184   /**
185    * Add search results for regions in other sequences that translate or are
186    * translated from a particular position in seq
187    * 
188    * @param seq
189    * @param index
190    *          position in seq
191    * @param results
192    *          where highlighted regions go
193    */
194   public void markMappedRegion(SequenceI seq, int index,
195           SearchResults results)
196   {
197     if (dnaToProt == null)
198     {
199       return;
200     }
201     int[] codon;
202     SequenceI ds = seq.getDatasetSequence();
203     for (int mi = 0; mi < dnaToProt.length; mi++)
204     {
205       if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
206       {
207         // DEBUG System.err.println("dna pos "+index);
208         codon = dnaToProt[mi].map.locateInTo(index, index);
209         if (codon != null)
210         {
211           for (int i = 0; i < codon.length; i += 2)
212           {
213             results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
214           }
215         }
216       }
217       else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
218       {
219         // DEBUG System.err.println("aa pos "+index);
220         {
221           codon = dnaToProt[mi].map.locateInFrom(index, index);
222           if (codon != null)
223           {
224             for (int i = 0; i < codon.length; i += 2)
225             {
226               results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);
227             }
228           }
229         }
230       }
231     }
232   }
233
234   /**
235    * Returns the DNA codon positions (base 1) for the given position (base 1) in
236    * a mapped protein sequence, or null if no mapping is found.
237    * 
238    * Intended for use in aligning cDNA to match aligned protein. Only the first
239    * mapping found is returned, so not suitable for use if multiple protein
240    * sequences are mapped to the same cDNA (but aligning cDNA as protein is
241    * ill-defined for this case anyway).
242    * 
243    * @param seq
244    *          the DNA dataset sequence
245    * @param aaPos
246    *          residue position (base 1) in a protein sequence
247    * @return
248    */
249   public int[] getDnaPosition(SequenceI seq, int aaPos)
250   {
251     /*
252      * Adapted from markMappedRegion().
253      */
254     MapList ml = null;
255     for (int i = 0; i < dnaToProt.length; i++)
256     {
257       if (dnaSeqs[i] == seq)
258       {
259         ml = getdnaToProt()[i];
260         break;
261       }
262     }
263     return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
264   }
265
266   /**
267    * Convenience method to return the first aligned sequence in the given
268    * alignment whose dataset has a mapping with the given dataset sequence.
269    * 
270    * @param seq
271    * 
272    * @param al
273    * @return
274    */
275   public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
276   {
277     /*
278      * Search mapped protein ('to') sequences first.
279      */
280     if (this.dnaToProt != null)
281     {
282       for (int i = 0; i < dnaToProt.length; i++)
283       {
284         if (this.dnaSeqs[i] == seq)
285         {
286           for (SequenceI sourceAligned : al.getSequences())
287           {
288             if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
289             {
290               return sourceAligned;
291             }
292           }
293         }
294       }
295     }
296
297     /*
298      * Then try mapped dna sequences.
299      */
300     if (this.dnaToProt != null)
301     {
302       for (int i = 0; i < dnaToProt.length; i++)
303       {
304         if (this.dnaToProt[i].to == seq)
305         {
306           for (SequenceI sourceAligned : al.getSequences())
307           {
308             if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
309             {
310               return sourceAligned;
311             }
312           }
313         }
314       }
315     }
316
317     return null;
318   }
319
320   /**
321    * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
322    * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
323    * a set of start/end position pairs.
324    * 
325    * @param mappedFrom
326    * @param mappedTo
327    * @param pos
328    * @return
329    */
330   public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
331           int pos)
332   {
333     SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
334             : mappedFrom.getDatasetSequence();
335     SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
336             : mappedTo.getDatasetSequence();
337     if (targetDs == null || sourceDs == null || dnaToProt == null)
338     {
339       return null;
340     }
341     for (int mi = 0; mi < dnaToProt.length; mi++)
342     {
343       if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
344       {
345         int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);
346         if (codon != null) {
347           return codon;
348         }
349       }
350     }
351     return null;
352   }
353 }