2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 * Returns the first mapping found which is to or from the given sequence, or
132 public Mapping getMappingForSequence(SequenceI seq)
138 SequenceI seqDs = seq.getDatasetSequence();
139 seqDs = seqDs != null ? seqDs : seq;
141 for (int ds = 0; ds < dnaSeqs.length; ds++)
143 if (dnaSeqs[ds] == seqDs || dnaToProt[ds].to == seqDs)
145 return dnaToProt[ds];
152 * Return the corresponding aligned or dataset aa sequence for given dna
153 * sequence, null if not found.
158 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
164 SequenceI dnads = dnaSeqRef.getDatasetSequence();
165 for (int ds = 0; ds < dnaSeqs.length; ds++)
167 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
169 return dnaToProt[ds].to;
178 * @return null or corresponding aaSeq entry for dnaSeq entry
180 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
182 if (dnaToProt == null)
186 SequenceI aads = aaSeqRef.getDatasetSequence();
187 for (int as = 0; as < dnaToProt.length; as++)
189 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
198 * test to see if codon frame involves seq in any way
201 * a nucleotide or protein sequence
202 * @return true if a mapping exists to or from this sequence to any translated
205 public boolean involvesSequence(SequenceI seq)
207 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
211 * Add search results for regions in other sequences that translate or are
212 * translated from a particular position in seq
218 * where highlighted regions go
220 public void markMappedRegion(SequenceI seq, int index,
221 SearchResults results)
223 if (dnaToProt == null)
228 SequenceI ds = seq.getDatasetSequence();
229 for (int mi = 0; mi < dnaToProt.length; mi++)
231 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
233 // DEBUG System.err.println("dna pos "+index);
234 codon = dnaToProt[mi].map.locateInTo(index, index);
237 for (int i = 0; i < codon.length; i += 2)
239 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
243 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
245 // DEBUG System.err.println("aa pos "+index);
247 codon = dnaToProt[mi].map.locateInFrom(index, index);
250 for (int i = 0; i < codon.length; i += 2)
252 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);
261 * Returns the DNA codon positions (base 1) for the given position (base 1) in
262 * a mapped protein sequence, or null if no mapping is found.
264 * Intended for use in aligning cDNA to match aligned protein. Only the first
265 * mapping found is returned, so not suitable for use if multiple protein
266 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
267 * ill-defined for this case anyway).
270 * the DNA dataset sequence
272 * residue position (base 1) in a protein sequence
275 public int[] getDnaPosition(SequenceI seq, int aaPos)
278 * Adapted from markMappedRegion().
281 for (int i = 0; i < dnaToProt.length; i++)
283 if (dnaSeqs[i] == seq)
285 ml = getdnaToProt()[i];
289 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
293 * Convenience method to return the first aligned sequence in the given
294 * alignment whose dataset has a mapping with the given dataset sequence.
301 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
304 * Search mapped protein ('to') sequences first.
306 if (this.dnaToProt != null)
308 for (int i = 0; i < dnaToProt.length; i++)
310 if (this.dnaSeqs[i] == seq)
312 for (SequenceI sourceAligned : al.getSequences())
314 if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
316 return sourceAligned;
324 * Then try mapped dna sequences.
326 if (this.dnaToProt != null)
328 for (int i = 0; i < dnaToProt.length; i++)
330 if (this.dnaToProt[i].to == seq)
332 for (SequenceI sourceAligned : al.getSequences())
334 if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
336 return sourceAligned;
347 * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
348 * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
349 * a set of start/end position pairs.
356 public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
359 SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
360 : mappedFrom.getDatasetSequence();
361 SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
362 : mappedTo.getDatasetSequence();
363 if (targetDs == null || sourceDs == null || dnaToProt == null)
367 for (int mi = 0; mi < dnaToProt.length; mi++)
369 if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
371 int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);