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 java.util.ArrayList;
24 import java.util.List;
26 import jalview.util.MapList;
27 import jalview.util.MappingUtils;
30 * Stores mapping between the columns of a protein alignment and a DNA alignment
31 * and a list of individual codon to amino acid mappings between sequences.
33 public class AlignedCodonFrame
37 * tied array of na Sequence objects.
39 private SequenceI[] dnaSeqs = null;
42 * tied array of Mappings to protein sequence Objects and SequenceI[]
43 * aaSeqs=null; MapLists where each maps from the corresponding dnaSeqs
44 * element to corresponding aaSeqs element
46 private Mapping[] dnaToProt = null;
51 public AlignedCodonFrame()
56 * Adds a mapping between the dataset sequences for the associated dna and
57 * protein sequence objects
63 public void addMap(SequenceI dnaseq, SequenceI aaseq, MapList map)
68 nlen = dnaSeqs.length + 1;
70 SequenceI[] ndna = new SequenceI[nlen];
71 Mapping[] ndtp = new Mapping[nlen];
74 System.arraycopy(dnaSeqs, 0, ndna, 0, dnaSeqs.length);
75 System.arraycopy(dnaToProt, 0, ndtp, 0, dnaSeqs.length);
80 dnaSeqs[nlen] = (dnaseq.getDatasetSequence() == null) ? dnaseq : dnaseq
81 .getDatasetSequence();
82 Mapping mp = new Mapping(map);
83 // JBPNote DEBUG! THIS !
84 // dnaseq.transferAnnotation(aaseq, mp);
85 // aaseq.transferAnnotation(dnaseq, new Mapping(map.getInverse()));
86 mp.to = (aaseq.getDatasetSequence() == null) ? aaseq : aaseq
87 .getDatasetSequence();
91 public SequenceI[] getdnaSeqs()
96 public SequenceI[] getAaSeqs()
98 if (dnaToProt == null)
102 SequenceI[] sqs = new SequenceI[dnaToProt.length];
103 for (int sz = 0; sz < dnaToProt.length; sz++)
105 sqs[sz] = dnaToProt[sz].to;
110 public MapList[] getdnaToProt()
112 if (dnaToProt == null)
116 MapList[] sqs = new MapList[dnaToProt.length];
117 for (int sz = 0; sz < dnaToProt.length; sz++)
119 sqs[sz] = dnaToProt[sz].map;
124 public Mapping[] getProtMappings()
130 * Returns the first mapping found which is to or from the given sequence, or
136 public Mapping getMappingForSequence(SequenceI seq)
142 SequenceI seqDs = seq.getDatasetSequence();
143 seqDs = seqDs != null ? seqDs : seq;
145 for (int ds = 0; ds < dnaSeqs.length; ds++)
147 if (dnaSeqs[ds] == seqDs || dnaToProt[ds].to == seqDs)
149 return dnaToProt[ds];
156 * Return the corresponding aligned or dataset aa sequence for given dna
157 * sequence, null if not found.
162 public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
168 SequenceI dnads = dnaSeqRef.getDatasetSequence();
169 for (int ds = 0; ds < dnaSeqs.length; ds++)
171 if (dnaSeqs[ds] == dnaSeqRef || dnaSeqs[ds] == dnads)
173 return dnaToProt[ds].to;
182 * @return null or corresponding aaSeq entry for dnaSeq entry
184 public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
186 if (dnaToProt == null)
190 SequenceI aads = aaSeqRef.getDatasetSequence();
191 for (int as = 0; as < dnaToProt.length; as++)
193 if (dnaToProt[as].to == aaSeqRef || dnaToProt[as].to == aads)
202 * test to see if codon frame involves seq in any way
205 * a nucleotide or protein sequence
206 * @return true if a mapping exists to or from this sequence to any translated
209 public boolean involvesSequence(SequenceI seq)
211 return getAaForDnaSeq(seq) != null || getDnaForAaSeq(seq) != null;
215 * Add search results for regions in other sequences that translate or are
216 * translated from a particular position in seq
222 * where highlighted regions go
224 public void markMappedRegion(SequenceI seq, int index,
225 SearchResults results)
227 if (dnaToProt == null)
232 SequenceI ds = seq.getDatasetSequence();
233 for (int mi = 0; mi < dnaToProt.length; mi++)
235 if (dnaSeqs[mi] == seq || dnaSeqs[mi] == ds)
237 // DEBUG System.err.println("dna pos "+index);
238 codon = dnaToProt[mi].map.locateInTo(index, index);
241 for (int i = 0; i < codon.length; i += 2)
243 results.addResult(dnaToProt[mi].to, codon[i], codon[i + 1]);
247 else if (dnaToProt[mi].to == seq || dnaToProt[mi].to == ds)
249 // DEBUG System.err.println("aa pos "+index);
251 codon = dnaToProt[mi].map.locateInFrom(index, index);
254 for (int i = 0; i < codon.length; i += 2)
256 results.addResult(dnaSeqs[mi], codon[i], codon[i + 1]);
265 * Returns the DNA codon positions (base 1) for the given position (base 1) in
266 * a mapped protein sequence, or null if no mapping is found.
268 * Intended for use in aligning cDNA to match aligned protein. Only the first
269 * mapping found is returned, so not suitable for use if multiple protein
270 * sequences are mapped to the same cDNA (but aligning cDNA as protein is
271 * ill-defined for this case anyway).
274 * the DNA dataset sequence
276 * residue position (base 1) in a protein sequence
279 public int[] getDnaPosition(SequenceI seq, int aaPos)
282 * Adapted from markMappedRegion().
285 for (int i = 0; i < dnaToProt.length; i++)
287 if (dnaSeqs[i] == seq)
289 ml = getdnaToProt()[i];
293 return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
297 * Convenience method to return the first aligned sequence in the given
298 * alignment whose dataset has a mapping with the given dataset sequence.
305 public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
308 * Search mapped protein ('to') sequences first.
310 if (this.dnaToProt != null)
312 for (int i = 0; i < dnaToProt.length; i++)
314 if (this.dnaSeqs[i] == seq)
316 for (SequenceI sourceAligned : al.getSequences())
318 if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
320 return sourceAligned;
328 * Then try mapped dna sequences.
330 if (this.dnaToProt != null)
332 for (int i = 0; i < dnaToProt.length; i++)
334 if (this.dnaToProt[i].to == seq)
336 for (SequenceI sourceAligned : al.getSequences())
338 if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
340 return sourceAligned;
351 * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
352 * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
353 * a set of start/end position pairs.
360 public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
363 SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
364 : mappedFrom.getDatasetSequence();
365 SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
366 : mappedTo.getDatasetSequence();
367 if (targetDs == null || sourceDs == null || dnaToProt == null)
371 for (int mi = 0; mi < dnaToProt.length; mi++)
373 if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
375 int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);
385 * Returns the DNA codon for the given position (base 1) in a mapped protein
386 * sequence, or null if no mapping is found.
389 * the peptide dataset sequence
391 * residue position (base 1) in the peptide sequence
394 public char[] getMappedCodon(SequenceI protein, int aaPos)
396 if (dnaToProt == null)
401 char[] dnaSeq = null;
402 for (int i = 0; i < dnaToProt.length; i++)
404 if (dnaToProt[i].to == protein)
406 ml = getdnaToProt()[i];
407 dnaSeq = dnaSeqs[i].getSequence();
415 int[] codonPos = ml.locateInFrom(aaPos, aaPos);
416 if (codonPos == null)
422 * Read off the mapped nucleotides (converting to position base 0)
424 codonPos = MappingUtils.flattenRanges(codonPos);
426 { dnaSeq[codonPos[0] - 1], dnaSeq[codonPos[1] - 1],
427 dnaSeq[codonPos[2] - 1] };
431 * Returns any mappings found which are to (or from) the given sequence, and
432 * to distinct sequences.
437 public List<Mapping> getMappingsForSequence(SequenceI seq)
439 List<Mapping> result = new ArrayList<Mapping>();
444 List<SequenceI> related = new ArrayList<SequenceI>();
445 SequenceI seqDs = seq.getDatasetSequence();
446 seqDs = seqDs != null ? seqDs : seq;
448 for (int ds = 0; ds < dnaSeqs.length; ds++)
450 final Mapping mapping = dnaToProt[ds];
451 if (dnaSeqs[ds] == seqDs || mapping.to == seqDs)
453 if (!related.contains(mapping.to))
456 related.add(mapping.to);