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 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.
29 public class AlignedCodonFrame
33 * tied array of na Sequence objects.
35 private SequenceI[] dnaSeqs = null;
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
42 private Mapping[] dnaToProt = null;
45 * initialise codon frame with a nominal alignment width
49 public AlignedCodonFrame()
54 * add a mapping between the dataset sequences for the associated dna and
55 * protein sequence objects
61 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
66 nlen = dnaSeqs.length + 1;
68 SequenceI[] ndna = new SequenceI[nlen];
69 Mapping[] ndtp = new Mapping[nlen];
72 System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
73 System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
78 dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
79 .getDatasetSequence();
80 Mapping mp = new Mapping(map);
81 // JBPNote DEBUG! THIS !
82 // dnaseq.transferAnnotation(aaseq, mp);
83 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
84 mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
85 .getDatasetSequence();
89 public SequenceI[] getdnaSeqs()
94 public SequenceI[] getAaSeqs()
96 if (dnaToProt == null)
100 SequenceI[] sqs = new SequenceI[dnaToProt.length];
101 for (int sz = 0; sz < dnaToProt.length; sz++)
103 sqs[sz] = dnaToProt[sz].to;
108 public MapList[] getdnaToProt()
110 if (dnaToProt == null)
114 MapList[] sqs = new MapList[dnaToProt.length];
115 for (int sz = 0; sz < dnaToProt.length; sz++)
117 sqs[sz] = dnaToProt[sz].map;
122 public Mapping[] getProtMappings()
128 * Return the corresponding aligned or dataset aa sequence for given dna
129 * sequence, null if not found.
134 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
140 SequenceI dnads = dnaSeqRef.getDatasetSequence();
141 for (int ds = 0; ds < dnaSeqs.length; ds++)
143 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
145 return dnaToProt[ds].to;
154 * @return null or corresponding aaSeq entry for dnaSeq entry
156 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
158 if (dnaToProt == null)
162 SequenceI aads = aaSeqRef.getDatasetSequence();
163 for (int as = 0; as < dnaToProt.length; as++)
165 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
174 * test to see if codon frame involves seq in any way
177 * a nucleotide or protein sequence
178 * @return true if a mapping exists to or from this sequence to any translated
181 public boolean involvesSequence(SequenceI seq)
183 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
187 * Add search results for regions in other sequences that translate or are
188 * translated from a particular position in seq
194 * where highlighted regions go
196 public void markMappedRegion(SequenceI seq, int index,
197 SearchResults results)
199 if (dnaToProt == null)
204 SequenceI ds = seq.getDatasetSequence();
205 for (int mi = 0; mi < dnaToProt.length; mi++)
207 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
209 // DEBUG System.err.println("dna pos "+index);
210 codon = dnaToProt[mi].map.locateInTo(index, index);
213 for (int i = 0; i < codon.length; i += 2)
215 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
219 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
221 // DEBUG System.err.println("aa pos "+index);
223 codon = dnaToProt[mi].map.locateInFrom(index, index);
226 for (int i = 0; i < codon.length; i += 2)
228 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);
237 * Returns the DNA codon positions (base 1) for the given position (base 1) in
238 * a mapped protein sequence, or null if no mapping is found.
240 * Intended for use in aligning cDNA to match aligned protein. Only the first
241 * mapping found is returned, so not suitable for use if multiple protein
242 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
243 * ill-defined for this case anyway).
246 * the DNA dataset sequence
248 * residue position (base 1) in a protein sequence
251 public int[] getDnaPosition(SequenceI seq, int aaPos)
254 * Adapted from markMappedRegion().
257 for (int i = 0; i < dnaToProt.length; i++)
259 if (dnaSeqs[i] == seq)
261 ml = getdnaToProt()[i];
265 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
269 * Convenience method to return the first aligned sequence in the given
270 * alignment whose dataset has a mapping with the given dataset sequence.
277 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
280 * Search mapped protein ('to') sequences first.
282 if (this.dnaToProt != null)
284 for (int i = 0; i < dnaToProt.length; i++)
286 if (this.dnaSeqs[i] == seq)
288 for (SequenceI sourceAligned : al.getSequences())
290 if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
292 return sourceAligned;
300 * Then try mapped dna sequences.
302 if (this.dnaToProt != null)
304 for (int i = 0; i < dnaToProt.length; i++)
306 if (this.dnaToProt[i].to == seq)
308 for (SequenceI sourceAligned : al.getSequences())
310 if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
312 return sourceAligned;
323 * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
324 * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
325 * a set of start/end position pairs.
332 public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
335 SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
336 : mappedFrom.getDatasetSequence();
337 SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
338 : mappedTo.getDatasetSequence();
339 if (targetDs == null || sourceDs == null || dnaToProt == null)
343 for (int mi = 0; mi < dnaToProt.length; mi++)
345 if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
347 int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);