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;
47 public AlignedCodonFrame()
52 * Adds a mapping between the dataset sequences for the associated dna and
53 * protein sequence objects
59 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
64 nlen = dnaSeqs.length + 1;
66 SequenceI[] ndna = new SequenceI[nlen];
67 Mapping[] ndtp = new Mapping[nlen];
70 System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
71 System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
76 dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
77 .getDatasetSequence();
78 Mapping mp = new Mapping(map);
79 // JBPNote DEBUG! THIS !
80 // dnaseq.transferAnnotation(aaseq, mp);
81 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
82 mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
83 .getDatasetSequence();
87 public SequenceI[] getdnaSeqs()
92 public SequenceI[] getAaSeqs()
94 if (dnaToProt == null)
98 SequenceI[] sqs = new SequenceI[dnaToProt.length];
99 for (int sz = 0; sz < dnaToProt.length; sz++)
101 sqs[sz] = dnaToProt[sz].to;
106 public MapList[] getdnaToProt()
108 if (dnaToProt == null)
112 MapList[] sqs = new MapList[dnaToProt.length];
113 for (int sz = 0; sz < dnaToProt.length; sz++)
115 sqs[sz] = dnaToProt[sz].map;
120 public Mapping[] getProtMappings()
126 * Return the corresponding aligned or dataset aa sequence for given dna
127 * sequence, null if not found.
132 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
138 SequenceI dnads = dnaSeqRef.getDatasetSequence();
139 for (int ds = 0; ds < dnaSeqs.length; ds++)
141 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
143 return dnaToProt[ds].to;
152 * @return null or corresponding aaSeq entry for dnaSeq entry
154 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
156 if (dnaToProt == null)
160 SequenceI aads = aaSeqRef.getDatasetSequence();
161 for (int as = 0; as < dnaToProt.length; as++)
163 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
172 * test to see if codon frame involves seq in any way
175 * a nucleotide or protein sequence
176 * @return true if a mapping exists to or from this sequence to any translated
179 public boolean involvesSequence(SequenceI seq)
181 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
185 * Add search results for regions in other sequences that translate or are
186 * translated from a particular position in seq
192 * where highlighted regions go
194 public void markMappedRegion(SequenceI seq, int index,
195 SearchResults results)
197 if (dnaToProt == null)
202 SequenceI ds = seq.getDatasetSequence();
203 for (int mi = 0; mi < dnaToProt.length; mi++)
205 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
207 // DEBUG System.err.println("dna pos "+index);
208 codon = dnaToProt[mi].map.locateInTo(index, index);
211 for (int i = 0; i < codon.length; i += 2)
213 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
217 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
219 // DEBUG System.err.println("aa pos "+index);
221 codon = dnaToProt[mi].map.locateInFrom(index, index);
224 for (int i = 0; i < codon.length; i += 2)
226 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);
235 * Returns the DNA codon positions (base 1) for the given position (base 1) in
236 * a mapped protein sequence, or null if no mapping is found.
238 * Intended for use in aligning cDNA to match aligned protein. Only the first
239 * mapping found is returned, so not suitable for use if multiple protein
240 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
241 * ill-defined for this case anyway).
244 * the DNA dataset sequence
246 * residue position (base 1) in a protein sequence
249 public int[] getDnaPosition(SequenceI seq, int aaPos)
252 * Adapted from markMappedRegion().
255 for (int i = 0; i < dnaToProt.length; i++)
257 if (dnaSeqs[i] == seq)
259 ml = getdnaToProt()[i];
263 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
267 * Convenience method to return the first aligned sequence in the given
268 * alignment whose dataset has a mapping with the given dataset sequence.
275 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
278 * Search mapped protein ('to') sequences first.
280 if (this.dnaToProt != null)
282 for (int i = 0; i < dnaToProt.length; i++)
284 if (this.dnaSeqs[i] == seq)
286 for (SequenceI sourceAligned : al.getSequences())
288 if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
290 return sourceAligned;
298 * Then try mapped dna sequences.
300 if (this.dnaToProt != null)
302 for (int i = 0; i < dnaToProt.length; i++)
304 if (this.dnaToProt[i].to == seq)
306 for (SequenceI sourceAligned : al.getSequences())
308 if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
310 return sourceAligned;
321 * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
322 * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
323 * a set of start/end position pairs.
330 public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
333 SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
334 : mappedFrom.getDatasetSequence();
335 SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
336 : mappedTo.getDatasetSequence();
337 if (targetDs == null || sourceDs == null || dnaToProt == null)
341 for (int mi = 0; mi < dnaToProt.length; mi++)
343 if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
345 int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);