+
+ /**
+ * Returns the DNA codon positions (base 1) for the given position (base 1) in
+ * a mapped protein sequence, or null if no mapping is found.
+ *
+ * Intended for use in aligning cDNA to match aligned protein. Only the first
+ * mapping found is returned, so not suitable for use if multiple protein
+ * sequences are mapped to the same cDNA (but aligning cDNA as protein is
+ * ill-defined for this case anyway).
+ *
+ * @param seq
+ * the DNA dataset sequence
+ * @param aaPos
+ * residue position (base 1) in a protein sequence
+ * @return
+ */
+ public int[] getDnaPosition(SequenceI seq, int aaPos)
+ {
+ /*
+ * Adapted from markMappedRegion().
+ */
+ MapList ml = null;
+ for (int i = 0; i < dnaToProt.length; i++)
+ {
+ if (dnaSeqs[i] == seq)
+ {
+ ml = getdnaToProt()[i];
+ break;
+ }
+ }
+ return ml == null ? null : ml.locateInFrom(aaPos, aaPos);
+ }
+
+ /**
+ * Convenience method to return the first aligned sequence in the given
+ * alignment whose dataset has a mapping with the given dataset sequence.
+ *
+ * @param seq
+ *
+ * @param al
+ * @return
+ */
+ public SequenceI findAlignedSequence(SequenceI seq, AlignmentI al)
+ {
+ /*
+ * Search mapped protein ('to') sequences first.
+ */
+ if (this.dnaToProt != null)
+ {
+ for (int i = 0; i < dnaToProt.length; i++)
+ {
+ if (this.dnaSeqs[i] == seq)
+ {
+ for (SequenceI sourceAligned : al.getSequences())
+ {
+ if (this.dnaToProt[i].to == sourceAligned.getDatasetSequence())
+ {
+ return sourceAligned;
+ }
+ }
+ }
+ }
+ }
+
+ /*
+ * Then try mapped dna sequences.
+ */
+ if (this.dnaToProt != null)
+ {
+ for (int i = 0; i < dnaToProt.length; i++)
+ {
+ if (this.dnaToProt[i].to == seq)
+ {
+ for (SequenceI sourceAligned : al.getSequences())
+ {
+ if (this.dnaSeqs[i] == sourceAligned.getDatasetSequence())
+ {
+ return sourceAligned;
+ }
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Returns the region in the 'mappedFrom' sequence's dataset that is mapped to
+ * position 'pos' (base 1) in the 'mappedTo' sequence's dataset. The region is
+ * a set of start/end position pairs.
+ *
+ * @param mappedFrom
+ * @param mappedTo
+ * @param pos
+ * @return
+ */
+ public int[] getMappedRegion(SequenceI mappedFrom, SequenceI mappedTo,
+ int pos)
+ {
+ SequenceI targetDs = mappedFrom.getDatasetSequence() == null ? mappedFrom
+ : mappedFrom.getDatasetSequence();
+ SequenceI sourceDs = mappedTo.getDatasetSequence() == null ? mappedTo
+ : mappedTo.getDatasetSequence();
+ if (targetDs == null || sourceDs == null || dnaToProt == null)
+ {
+ return null;
+ }
+ for (int mi = 0; mi < dnaToProt.length; mi++)
+ {
+ if (dnaSeqs[mi] == targetDs && dnaToProt[mi].to == sourceDs)
+ {
+ int[] codon = dnaToProt[mi].map.locateInFrom(pos, pos);
+ if (codon != null)
+ {
+ return codon;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the DNA codon for the given position (base 1) in a mapped protein
+ * sequence, or null if no mapping is found.
+ *
+ * @param protein
+ * the peptide dataset sequence
+ * @param aaPos
+ * residue position (base 1) in the peptide sequence
+ * @return
+ */
+ public char[] getMappedCodon(SequenceI protein, int aaPos)
+ {
+ if (dnaToProt == null)
+ {
+ return null;
+ }
+ MapList ml = null;
+ SequenceI dnaSeq = null;
+ for (int i = 0; i < dnaToProt.length; i++)
+ {
+ if (dnaToProt[i].to == protein)
+ {
+ ml = getdnaToProt()[i];
+ dnaSeq = dnaSeqs[i];
+ break;
+ }
+ }
+ if (ml == null)
+ {
+ return null;
+ }
+ int[] codonPos = ml.locateInFrom(aaPos, aaPos);
+ if (codonPos == null)
+ {
+ return null;
+ }
+
+ /*
+ * Read off the mapped nucleotides (converting to position base 0)
+ */
+ codonPos = MappingUtils.flattenRanges(codonPos);
+ char[] dna = dnaSeq.getSequence();
+ int start = dnaSeq.getStart();
+ return new char[] { dna[codonPos[0] - start], dna[codonPos[1] - start],
+ dna[codonPos[2] - start] };
+ }
+
+ /**
+ * Returns any mappings found which are to (or from) the given sequence, and
+ * to distinct sequences.
+ *
+ * @param seq
+ * @return
+ */
+ public List<Mapping> getMappingsForSequence(SequenceI seq)
+ {
+ List<Mapping> result = new ArrayList<Mapping>();
+ if (dnaSeqs == null)
+ {
+ return result;
+ }
+ List<SequenceI> related = new ArrayList<SequenceI>();
+ SequenceI seqDs = seq.getDatasetSequence();
+ seqDs = seqDs != null ? seqDs : seq;
+
+ for (int ds = 0; ds < dnaSeqs.length; ds++)
+ {
+ final Mapping mapping = dnaToProt[ds];
+ if (dnaSeqs[ds] == seqDs || mapping.to == seqDs)
+ {
+ if (!related.contains(mapping.to))
+ {
+ result.add(mapping);
+ related.add(mapping.to);
+ }
+ }
+ }
+ return result;
+ }