2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
21 package jalview.datamodel;
23 import java.util.Enumeration;
24 import java.util.Vector;
26 import jalview.util.MapList;
29 * Stores mapping between the columns of a protein alignment and a DNA alignment
30 * and a list of individual codon to amino acid mappings between sequences.
33 public class AlignedCodonFrame
36 * array of nucleotide positions for aligned codons at column of aligned
39 public int[][] codons = null;
42 * width of protein sequence alignement implicit assertion that codons.length
45 public int aaWidth = 0;
48 * initialise codon frame with a nominal alignment width
52 public AlignedCodonFrame(int aWidth)
59 codons = new int[aWidth][];
60 for (int res = 0; res < aWidth; res++)
65 * ensure that codons array is at least as wide as aslen residues
68 * @return (possibly newly expanded) codon array
70 public int[][] checkCodonFrameWidth(int aslen)
72 if (codons.length <= aslen + 1)
74 // probably never have to do this ?
75 int[][] c = new int[codons.length + 10][];
76 for (int i = 0; i < codons.length; i++)
87 * @return width of aligned translated amino acid residues
89 public int getaaWidth()
95 * TODO: not an ideal solution - we reference the aligned amino acid sequences
96 * in order to make insertions on them Better would be dnaAlignment and
97 * aaAlignment reference....
99 Vector a_aaSeqs = new Vector();
102 * increase aaWidth by one and insert a new aligned codon position space at
107 public void insertAAGap(int aspos, char gapCharacter)
109 // this aa appears before the aligned codons at aspos - so shift them in
110 // each pair of mapped sequences
112 if (a_aaSeqs != null)
114 // we actually have to modify the aligned sequences here, so use the
116 Enumeration sq = a_aaSeqs.elements();
117 while (sq.hasMoreElements())
119 ((SequenceI) sq.nextElement()).insertCharAt(aspos, gapCharacter);
122 checkCodonFrameWidth(aspos);
126 System.arraycopy(codons, aspos, codons, aspos + 1, codons.length - aspos - 1);
127 codons[aspos] = null; // clear so new codon position can be marked.
131 public void setAaWidth(int aapos)
137 * tied array of na Sequence objects.
139 SequenceI[] dnaSeqs = null;
142 * tied array of Mappings to protein sequence Objects and SequenceI[]
143 * aaSeqs=null; MapLists where eac maps from the corresponding dnaSeqs element
144 * to corresponding aaSeqs element
146 Mapping[] dnaToProt = null;
149 * add a mapping between the dataset sequences for the associated dna and
150 * protein sequence objects
156 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
161 nlen = dnaSeqs.length + 1;
163 SequenceI[] ndna = new SequenceI[nlen];
164 Mapping[] ndtp = new Mapping[nlen];
167 System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
168 System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
173 dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
174 .getDatasetSequence();
175 Mapping mp = new Mapping(map);
176 // JBPNote DEBUG! THIS !
177 // dnaseq.transferAnnotation(aaseq, mp);
178 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
179 mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
180 .getDatasetSequence();
181 a_aaSeqs.addElement(aaseq);
182 dnaToProt[nlen] = mp;
185 public SequenceI[] getdnaSeqs()
190 public SequenceI[] getAaSeqs()
192 if (dnaToProt == null)
194 SequenceI[] sqs = new SequenceI[dnaToProt.length];
195 for (int sz = 0; sz < dnaToProt.length; sz++)
197 sqs[sz] = dnaToProt[sz].to;
202 public MapList[] getdnaToProt()
204 if (dnaToProt == null)
206 MapList[] sqs = new MapList[dnaToProt.length];
207 for (int sz = 0; sz < dnaToProt.length; sz++)
209 sqs[sz] = dnaToProt[sz].map;
214 public Mapping[] getProtMappings()
222 * @return null or corresponding aaSeq entry for dnaSeq entry
224 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
230 SequenceI dnads = dnaSeqRef.getDatasetSequence();
231 for (int ds = 0; ds < dnaSeqs.length; ds++)
233 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
234 return dnaToProt[ds].to;
242 * @return null or corresponding aaSeq entry for dnaSeq entry
244 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
246 if (dnaToProt == null)
250 SequenceI aads = aaSeqRef.getDatasetSequence();
251 for (int as = 0; as < dnaToProt.length; as++)
253 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
260 * test to see if codon frame involves seq in any way
263 * a nucleotide or protein sequence
264 * @return true if a mapping exists to or from this sequence to any translated
267 public boolean involvesSequence(SequenceI seq)
269 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
273 * Add search results for regions in other sequences that translate or are
274 * translated from a particular position in seq
280 * where highlighted regions go
282 public void markMappedRegion(SequenceI seq, int index,
283 SearchResults results)
285 if (dnaToProt == null)
290 SequenceI ds = seq.getDatasetSequence();
291 for (int mi = 0; mi < dnaToProt.length; mi++)
293 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
295 // DEBUG System.err.println("dna pos "+index);
296 codon = dnaToProt[mi].map.locateInTo(index, index);
299 for (int i = 0; i < codon.length; i += 2)
301 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
305 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
307 // DEBUG System.err.println("aa pos "+index);
309 codon = dnaToProt[mi].map.locateInFrom(index, index);
312 for (int i = 0; i < codon.length; i += 2)
314 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);