2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
20 import java.util.Enumeration;
21 import java.util.Vector;
23 import jalview.util.MapList;
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.
30 public class AlignedCodonFrame
33 * array of nucleotide positions for aligned codons at column of aligned
36 public int[][] codons = null;
39 * width of protein sequence alignement implicit assertion that codons.length
42 public int aaWidth = 0;
45 * initialise codon frame with a nominal alignment width
49 public AlignedCodonFrame(int aWidth)
56 codons = new int[aWidth][];
57 for (int res = 0; res < aWidth; res++)
62 * ensure that codons array is at least as wide as aslen residues
65 * @return (possibly newly expanded) codon array
67 public int[][] checkCodonFrameWidth(int aslen)
69 if (codons.length <= aslen + 1)
71 // probably never have to do this ?
72 int[][] c = new int[codons.length + 10][];
73 for (int i = 0; i < codons.length; i++)
84 * @return width of aligned translated amino acid residues
86 public int getaaWidth()
92 * TODO: not an ideal solution - we reference the aligned amino acid sequences
93 * in order to make insertions on them Better would be dnaAlignment and
94 * aaAlignment reference....
96 Vector a_aaSeqs = new Vector();
99 * increase aaWidth by one and insert a new aligned codon position space at
104 public void insertAAGap(int aspos, char gapCharacter)
106 // this aa appears before the aligned codons at aspos - so shift them in
107 // each pair of mapped sequences
109 if (a_aaSeqs != null)
111 // we actually have to modify the aligned sequences here, so use the
113 Enumeration sq = a_aaSeqs.elements();
114 while (sq.hasMoreElements())
116 ((SequenceI) sq.nextElement()).insertCharAt(aspos, gapCharacter);
119 checkCodonFrameWidth(aspos);
123 System.arraycopy(codons, aspos, codons, aspos + 1, codons.length - aspos - 1);
124 codons[aspos] = null; // clear so new codon position can be marked.
128 public void setAaWidth(int aapos)
134 * tied array of na Sequence objects.
136 SequenceI[] dnaSeqs = null;
139 * tied array of Mappings to protein sequence Objects and SequenceI[]
140 * aaSeqs=null; MapLists where eac maps from the corresponding dnaSeqs element
141 * to corresponding aaSeqs element
143 Mapping[] dnaToProt = null;
146 * add a mapping between the dataset sequences for the associated dna and
147 * protein sequence objects
153 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
158 nlen = dnaSeqs.length + 1;
160 SequenceI[] ndna = new SequenceI[nlen];
161 Mapping[] ndtp = new Mapping[nlen];
164 System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
165 System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
170 dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
171 .getDatasetSequence();
172 Mapping mp = new Mapping(map);
173 // JBPNote DEBUG! THIS !
174 // dnaseq.transferAnnotation(aaseq, mp);
175 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
176 mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
177 .getDatasetSequence();
178 a_aaSeqs.addElement(aaseq);
179 dnaToProt[nlen] = mp;
182 public SequenceI[] getdnaSeqs()
187 public SequenceI[] getAaSeqs()
189 if (dnaToProt == null)
191 SequenceI[] sqs = new SequenceI[dnaToProt.length];
192 for (int sz = 0; sz < dnaToProt.length; sz++)
194 sqs[sz] = dnaToProt[sz].to;
199 public MapList[] getdnaToProt()
201 if (dnaToProt == null)
203 MapList[] sqs = new MapList[dnaToProt.length];
204 for (int sz = 0; sz < dnaToProt.length; sz++)
206 sqs[sz] = dnaToProt[sz].map;
211 public Mapping[] getProtMappings()
219 * @return null or corresponding aaSeq entry for dnaSeq entry
221 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
227 SequenceI dnads = dnaSeqRef.getDatasetSequence();
228 for (int ds = 0; ds < dnaSeqs.length; ds++)
230 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
231 return dnaToProt[ds].to;
239 * @return null or corresponding aaSeq entry for dnaSeq entry
241 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
243 if (dnaToProt == null)
247 SequenceI aads = aaSeqRef.getDatasetSequence();
248 for (int as = 0; as < dnaToProt.length; as++)
250 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
257 * test to see if codon frame involves seq in any way
260 * a nucleotide or protein sequence
261 * @return true if a mapping exists to or from this sequence to any translated
264 public boolean involvesSequence(SequenceI seq)
266 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
270 * Add search results for regions in other sequences that translate or are
271 * translated from a particular position in seq
277 * where highlighted regions go
279 public void markMappedRegion(SequenceI seq, int index,
280 SearchResults results)
282 if (dnaToProt == null)
287 SequenceI ds = seq.getDatasetSequence();
288 for (int mi = 0; mi < dnaToProt.length; mi++)
290 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
292 // DEBUG System.err.println("dna pos "+index);
293 codon = dnaToProt[mi].map.locateInTo(index, index);
296 for (int i = 0; i < codon.length; i += 2)
298 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
302 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
304 // DEBUG System.err.println("aa pos "+index);
306 codon = dnaToProt[mi].map.locateInFrom(index, index);
309 for (int i = 0; i < codon.length; i += 2)
311 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);