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;
24 import jalview.util.MappingUtils;
27 * Stores mapping between the columns of a protein alignment and a DNA alignment
28 * and a list of individual codon to amino acid mappings between sequences.
30 public class AlignedCodonFrame
34 * tied array of na Sequence objects.
36 private SequenceI[] dnaSeqs = null;
39 * tied array of Mappings to protein sequence Objects and SequenceI[]
40 * aaSeqs=null; MapLists where each maps from the corresponding dnaSeqs
41 * element to corresponding aaSeqs element
43 private Mapping[] dnaToProt = null;
48 public AlignedCodonFrame()
53 * Adds a mapping between the dataset sequences for the associated dna and
54 * protein sequence objects
60 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
65 nlen = dnaSeqs.length + 1;
67 SequenceI[] ndna = new SequenceI[nlen];
68 Mapping[] ndtp = new Mapping[nlen];
71 System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
72 System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
77 dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
78 .getDatasetSequence();
79 Mapping mp = new Mapping(map);
80 // JBPNote DEBUG! THIS !
81 // dnaseq.transferAnnotation(aaseq, mp);
82 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
83 mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
84 .getDatasetSequence();
88 public SequenceI[] getdnaSeqs()
93 public SequenceI[] getAaSeqs()
95 if (dnaToProt == null)
99 SequenceI[] sqs = new SequenceI[dnaToProt.length];
100 for (int sz = 0; sz < dnaToProt.length; sz++)
102 sqs[sz] = dnaToProt[sz].to;
107 public MapList[] getdnaToProt()
109 if (dnaToProt == null)
113 MapList[] sqs = new MapList[dnaToProt.length];
114 for (int sz = 0; sz < dnaToProt.length; sz++)
116 sqs[sz] = dnaToProt[sz].map;
121 public Mapping[] getProtMappings()
127 * Returns the first mapping found which is to or from the given sequence, or
133 public Mapping getMappingForSequence(SequenceI seq)
139 SequenceI seqDs = seq.getDatasetSequence();
140 seqDs = seqDs != null ? seqDs : seq;
142 for (int ds = 0; ds < dnaSeqs.length; ds++)
144 if (dnaSeqs[ds] == seqDs || dnaToProt[ds].to == seqDs)
146 return dnaToProt[ds];
153 * Return the corresponding aligned or dataset aa sequence for given dna
154 * sequence, null if not found.
159 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
165 SequenceI dnads = dnaSeqRef.getDatasetSequence();
166 for (int ds = 0; ds < dnaSeqs.length; ds++)
168 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
170 return dnaToProt[ds].to;
179 * @return null or corresponding aaSeq entry for dnaSeq entry
181 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
183 if (dnaToProt == null)
187 SequenceI aads = aaSeqRef.getDatasetSequence();
188 for (int as = 0; as < dnaToProt.length; as++)
190 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
199 * test to see if codon frame involves seq in any way
202 * a nucleotide or protein sequence
203 * @return true if a mapping exists to or from this sequence to any translated
206 public boolean involvesSequence(SequenceI seq)
208 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
212 * Add search results for regions in other sequences that translate or are
213 * translated from a particular position in seq
219 * where highlighted regions go
221 public void markMappedRegion(SequenceI seq, int index,
222 SearchResults results)
224 if (dnaToProt == null)
229 SequenceI ds = seq.getDatasetSequence();
230 for (int mi = 0; mi < dnaToProt.length; mi++)
232 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
234 // DEBUG System.err.println("dna pos "+index);
235 codon = dnaToProt[mi].map.locateInTo(index, index);
238 for (int i = 0; i < codon.length; i += 2)
240 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
244 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
246 // DEBUG System.err.println("aa pos "+index);
248 codon = dnaToProt[mi].map.locateInFrom(index, index);
251 for (int i = 0; i < codon.length; i += 2)
253 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);
262 * Returns the DNA codon positions (base 1) for the given position (base 1) in
263 * a mapped protein sequence, or null if no mapping is found.
265 * Intended for use in aligning cDNA to match aligned protein. Only the first
266 * mapping found is returned, so not suitable for use if multiple protein
267 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
268 * ill-defined for this case anyway).
271 * the DNA dataset sequence
273 * residue position (base 1) in a protein sequence
276 public int[] getDnaPosition(SequenceI seq, int aaPos)
279 * Adapted from markMappedRegion().
282 for (int i = 0; i < dnaToProt.length; i++)
284 if (dnaSeqs[i] == seq)
286 ml = getdnaToProt()[i];
290 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
294 * Convenience method to return the first aligned sequence in the given
295 * alignment whose dataset has a mapping with the given dataset sequence.
302 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
305 * Search mapped protein ('to') sequences first.
307 if (this.dnaToProt != null)
309 for (int i = 0; i < dnaToProt.length; i++)
311 if (this.dnaSeqs[i] == seq)
313 for (SequenceI sourceAligned : al.getSequences())
315 if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
317 return sourceAligned;
325 * Then try mapped dna sequences.
327 if (this.dnaToProt != null)
329 for (int i = 0; i < dnaToProt.length; i++)
331 if (this.dnaToProt[i].to == seq)
333 for (SequenceI sourceAligned : al.getSequences())
335 if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
337 return sourceAligned;
348 * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
349 * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
350 * a set of start/end position pairs.
357 public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
360 SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
361 : mappedFrom.getDatasetSequence();
362 SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
363 : mappedTo.getDatasetSequence();
364 if (targetDs == null || sourceDs == null || dnaToProt == null)
368 for (int mi = 0; mi < dnaToProt.length; mi++)
370 if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
372 int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);
382 * Returns the DNA codon for the given position (base 1) in a mapped protein
383 * sequence, or null if no mapping is found.
386 * the peptide dataset sequence
388 * residue position (base 1) in the peptide sequence
391 public char[] getMappedCodon(SequenceI protein, int aaPos)
393 if (dnaToProt == null)
398 char[] dnaSeq = null;
399 for (int i = 0; i < dnaToProt.length; i++)
401 if (dnaToProt[i].to == protein)
403 ml = getdnaToProt()[i];
404 dnaSeq = dnaSeqs[i].getSequence();
412 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
413 if (codonPos == null)
419 * Read off the mapped nucleotides (converting to position base 0)
421 codonPos = MappingUtils.flattenRanges(codonPos);
423 { dnaSeq[codonPos[0] - 1], dnaSeq[codonPos[1] - 1],
424 dnaSeq[codonPos[2] - 1] };