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