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.analysis;
23 import jalview.datamodel.AlignedCodon;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Mapping;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ResidueProperties;
32 import jalview.util.MapList;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Iterator;
39 import java.util.LinkedHashMap;
40 import java.util.List;
42 import java.util.Map.Entry;
44 import java.util.TreeMap;
47 * grab bag of useful alignment manipulation operations Expect these to be
48 * refactored elsewhere at some point.
53 public class AlignmentUtils
57 * given an existing alignment, create a new alignment including all, or up to
58 * flankSize additional symbols from each sequence's dataset sequence
64 public static AlignmentI expandContext(AlignmentI core, int flankSize)
66 List<SequenceI> sq = new ArrayList<SequenceI>();
68 for (SequenceI s : core.getSequences())
70 SequenceI newSeq = s.deriveSequence();
71 if (newSeq.getStart() > maxoffset
72 && newSeq.getDatasetSequence().getStart() < s.getStart())
74 maxoffset = newSeq.getStart();
80 maxoffset = flankSize;
82 // now add offset to create a new expanded alignment
83 for (SequenceI s : sq)
86 while (ds.getDatasetSequence() != null)
88 ds = ds.getDatasetSequence();
90 int s_end = s.findPosition(s.getStart() + s.getLength());
91 // find available flanking residues for sequence
92 int ustream_ds = s.getStart() - ds.getStart(), dstream_ds = ds
95 // build new flanked sequence
97 // compute gap padding to start of flanking sequence
98 int offset = maxoffset - ustream_ds;
100 // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
103 if (flankSize < ustream_ds)
105 // take up to flankSize residues
106 offset = maxoffset - flankSize;
107 ustream_ds = flankSize;
109 if (flankSize < dstream_ds)
111 dstream_ds = flankSize;
114 char[] upstream = new String(ds.getSequence(s.getStart() - 1
115 - ustream_ds, s.getStart() - 1)).toLowerCase().toCharArray();
116 char[] downstream = new String(ds.getSequence(s_end - 1, s_end + 1
117 + dstream_ds)).toLowerCase().toCharArray();
118 char[] coreseq = s.getSequence();
119 char[] nseq = new char[offset + upstream.length + downstream.length
121 char c = core.getGapCharacter();
122 // TODO could lowercase the flanking regions
124 for (; p < offset; p++)
128 // s.setSequence(new String(upstream).toLowerCase()+new String(coreseq) +
129 // new String(downstream).toLowerCase());
130 System.arraycopy(upstream, 0, nseq, p, upstream.length);
131 System.arraycopy(coreseq, 0, nseq, p + upstream.length,
133 System.arraycopy(downstream, 0, nseq, p + coreseq.length
134 + upstream.length, downstream.length);
135 s.setSequence(new String(nseq));
136 s.setStart(s.getStart() - ustream_ds);
137 s.setEnd(s_end + downstream.length);
139 AlignmentI newAl = new jalview.datamodel.Alignment(
140 sq.toArray(new SequenceI[0]));
141 for (SequenceI s : sq)
143 if (s.getAnnotation() != null)
145 for (AlignmentAnnotation aa : s.getAnnotation())
147 newAl.addAnnotation(aa);
151 newAl.setDataset(core.getDataset());
156 * Returns the index (zero-based position) of a sequence in an alignment, or
163 public static int getSequenceIndex(AlignmentI al, SequenceI seq)
167 for (SequenceI alSeq : al.getSequences())
180 * Returns a map of lists of sequences in the alignment, keyed by sequence
181 * name. For use in mapping between different alignment views of the same
184 * @see jalview.datamodel.AlignmentI#getSequencesByName()
186 public static Map<String, List<SequenceI>> getSequencesByName(
189 Map<String, List<SequenceI>> theMap = new LinkedHashMap<String, List<SequenceI>>();
190 for (SequenceI seq : al.getSequences())
192 String name = seq.getName();
195 List<SequenceI> seqs = theMap.get(name);
198 seqs = new ArrayList<SequenceI>();
199 theMap.put(name, seqs);
208 * Build mapping of protein to cDNA alignment. Mappings are made between
209 * sequences where the cDNA translates to the protein sequence. Any new
210 * mappings are added to the protein alignment. Returns true if any mappings
211 * either already exist or were added, else false.
213 * @param proteinAlignment
214 * @param cdnaAlignment
217 public static boolean mapProteinToCdna(
218 final AlignmentI proteinAlignment,
219 final AlignmentI cdnaAlignment)
221 if (proteinAlignment == null || cdnaAlignment == null)
226 Set<SequenceI> mappedDna = new HashSet<SequenceI>();
227 Set<SequenceI> mappedProtein = new HashSet<SequenceI>();
230 * First pass - map sequences where cross-references exist. This include
231 * 1-to-many mappings to support, for example, variant cDNA.
233 boolean mappingPerformed = mapProteinToCdna(proteinAlignment,
234 cdnaAlignment, mappedDna, mappedProtein, true);
237 * Second pass - map sequences where no cross-references exist. This only
238 * does 1-to-1 mappings and assumes corresponding sequences are in the same
239 * order in the alignments.
241 mappingPerformed |= mapProteinToCdna(proteinAlignment, cdnaAlignment,
242 mappedDna, mappedProtein, false);
243 return mappingPerformed;
247 * Make mappings between compatible sequences (where the cDNA translation
248 * matches the protein).
250 * @param proteinAlignment
251 * @param cdnaAlignment
253 * a set of mapped DNA sequences (to add to)
254 * @param mappedProtein
255 * a set of mapped Protein sequences (to add to)
257 * if true, only map sequences where xrefs exist
260 protected static boolean mapProteinToCdna(
261 final AlignmentI proteinAlignment,
262 final AlignmentI cdnaAlignment, Set<SequenceI> mappedDna,
263 Set<SequenceI> mappedProtein, boolean xrefsOnly)
265 boolean mappingPerformed = false;
266 List<SequenceI> thisSeqs = proteinAlignment.getSequences();
267 for (SequenceI aaSeq : thisSeqs)
269 boolean proteinMapped = false;
270 AlignedCodonFrame acf = new AlignedCodonFrame();
272 for (SequenceI cdnaSeq : cdnaAlignment.getSequences())
275 * Always try to map if sequences have xref to each other; this supports
276 * variant cDNA or alternative splicing for a protein sequence.
278 * If no xrefs, try to map progressively, assuming that alignments have
279 * mappable sequences in corresponding order. These are not
280 * many-to-many, as that would risk mixing species with similar cDNA
283 if (xrefsOnly && !CrossRef.haveCrossRef(aaSeq, cdnaSeq))
289 * Don't map non-xrefd sequences more than once each. This heuristic
290 * allows us to pair up similar sequences in ordered alignments.
293 && (mappedProtein.contains(aaSeq) || mappedDna
298 if (!mappingExists(proteinAlignment.getCodonFrames(),
299 aaSeq.getDatasetSequence(), cdnaSeq.getDatasetSequence()))
301 MapList map = mapProteinToCdna(aaSeq, cdnaSeq);
304 acf.addMap(cdnaSeq, aaSeq, map);
305 mappingPerformed = true;
306 proteinMapped = true;
307 mappedDna.add(cdnaSeq);
308 mappedProtein.add(aaSeq);
314 proteinAlignment.addCodonFrame(acf);
317 return mappingPerformed;
321 * Answers true if the mappings include one between the given (dataset)
324 public static boolean mappingExists(Set<AlignedCodonFrame> set,
325 SequenceI aaSeq, SequenceI cdnaSeq)
329 for (AlignedCodonFrame acf : set)
331 if (cdnaSeq == acf.getDnaForAaSeq(aaSeq))
341 * Build a mapping (if possible) of a protein to a cDNA sequence. The cDNA
342 * must be three times the length of the protein, possibly after ignoring
343 * start and/or stop codons, and must translate to the protein. Returns null
344 * if no mapping is determined.
350 public static MapList mapProteinToCdna(SequenceI proteinSeq,
354 * Here we handle either dataset sequence set (desktop) or absent (applet).
355 * Use only the char[] form of the sequence to avoid creating possibly large
358 final SequenceI proteinDataset = proteinSeq.getDatasetSequence();
359 char[] aaSeqChars = proteinDataset != null ? proteinDataset
360 .getSequence() : proteinSeq.getSequence();
361 final SequenceI cdnaDataset = cdnaSeq.getDatasetSequence();
362 char[] cdnaSeqChars = cdnaDataset != null ? cdnaDataset.getSequence()
363 : cdnaSeq.getSequence();
364 if (aaSeqChars == null || cdnaSeqChars == null)
370 * cdnaStart/End, proteinStartEnd are base 1 (for dataset sequence mapping)
372 final int mappedLength = 3 * aaSeqChars.length;
373 int cdnaLength = cdnaSeqChars.length;
375 int cdnaEnd = cdnaLength;
376 final int proteinStart = 1;
377 final int proteinEnd = aaSeqChars.length;
380 * If lengths don't match, try ignoring stop codon.
382 if (cdnaLength != mappedLength && cdnaLength > 2)
384 String lastCodon = String.valueOf(cdnaSeqChars, cdnaLength - 3, 3)
386 for (String stop : ResidueProperties.STOP)
388 if (lastCodon.equals(stop))
398 * If lengths still don't match, try ignoring start codon.
400 if (cdnaLength != mappedLength
402 && String.valueOf(cdnaSeqChars, 0, 3).toUpperCase()
404 ResidueProperties.START))
410 if (cdnaLength != mappedLength)
414 if (!translatesAs(cdnaSeqChars, cdnaStart - 1, aaSeqChars))
418 MapList map = new MapList(new int[]
419 { cdnaStart, cdnaEnd }, new int[]
420 { proteinStart, proteinEnd }, 3, 1);
425 * Test whether the given cdna sequence, starting at the given offset,
426 * translates to the given amino acid sequence, using the standard translation
427 * table. Designed to fail fast i.e. as soon as a mismatch position is found.
429 * @param cdnaSeqChars
434 protected static boolean translatesAs(char[] cdnaSeqChars, int cdnaStart,
438 for (int i = cdnaStart; i < cdnaSeqChars.length - 2
439 && aaResidue < aaSeqChars.length; i += 3, aaResidue++)
441 String codon = String.valueOf(cdnaSeqChars, i, 3);
442 final String translated = ResidueProperties.codonTranslate(
445 * ? allow X in protein to match untranslatable in dna ?
447 final char aaRes = aaSeqChars[aaResidue];
448 if (translated == null && aaRes == 'X')
452 if (translated == null
453 || !(aaRes == translated.charAt(0)))
456 System.out.println(("Mismatch at " + i + "/" + aaResidue + ": "
457 + codon + "(" + translated + ") != " + aaRes));
461 // fail if we didn't match all of the aa sequence
462 return (aaResidue == aaSeqChars.length);
466 * Align sequence 'seq' to match the alignment of a mapped sequence. Note this
467 * currently assumes that we are aligning cDNA to match protein.
470 * the sequence to be realigned
472 * the alignment whose sequence alignment is to be 'copied'
474 * character string represent a gap in the realigned sequence
475 * @param preserveUnmappedGaps
476 * @param preserveMappedGaps
477 * @return true if the sequence was realigned, false if it could not be
479 public static boolean alignSequenceAs(SequenceI seq, AlignmentI al,
480 String gap, boolean preserveMappedGaps,
481 boolean preserveUnmappedGaps)
484 * Get any mappings from the source alignment to the target (dataset) sequence.
486 // TODO there may be one AlignedCodonFrame per dataset sequence, or one with
487 // all mappings. Would it help to constrain this?
488 List<AlignedCodonFrame> mappings = al.getCodonFrame(seq);
489 if (mappings == null || mappings.isEmpty())
495 * Locate the aligned source sequence whose dataset sequence is mapped. We
496 * just take the first match here (as we can't align cDNA like more than one
499 SequenceI alignFrom = null;
500 AlignedCodonFrame mapping = null;
501 for (AlignedCodonFrame mp : mappings)
503 alignFrom = mp.findAlignedSequence(seq.getDatasetSequence(), al);
504 if (alignFrom != null)
511 if (alignFrom == null)
515 alignSequenceAs(seq, alignFrom, mapping, gap, al.getGapCharacter(),
516 preserveMappedGaps, preserveUnmappedGaps);
521 * Align sequence 'alignTo' the same way as 'alignFrom', using the mapping to
522 * match residues and codons. Flags control whether existing gaps in unmapped
523 * (intron) and mapped (exon) regions are preserved or not. Gaps linking intro
524 * and exon are only retained if both flags are set.
531 * @param preserveUnmappedGaps
532 * @param preserveMappedGaps
534 public static void alignSequenceAs(SequenceI alignTo,
536 AlignedCodonFrame mapping, String myGap, char sourceGap,
537 boolean preserveMappedGaps, boolean preserveUnmappedGaps)
539 // TODO generalise to work for Protein-Protein, dna-dna, dna-protein
540 final char[] thisSeq = alignTo.getSequence();
541 final char[] thatAligned = alignFrom.getSequence();
542 StringBuilder thisAligned = new StringBuilder(2 * thisSeq.length);
544 // aligned and dataset sequence positions, all base zero
548 int basesWritten = 0;
549 char myGapChar = myGap.charAt(0);
550 int ratio = myGap.length();
553 * Traverse the aligned protein sequence.
555 int sourceGapMappedLength = 0;
556 boolean inExon = false;
557 for (char sourceChar : thatAligned)
559 if (sourceChar == sourceGap)
561 sourceGapMappedLength += ratio;
566 * Found a residue. Locate its mapped codon (start) position.
569 // Note mapping positions are base 1, our sequence positions base 0
570 int[] mappedPos = mapping.getMappedRegion(alignTo, alignFrom,
572 if (mappedPos == null)
575 * Abort realignment if unmapped protein. Or could ignore it??
577 System.err.println("Can't align: no codon mapping to residue "
578 + sourceDsPos + "(" + sourceChar + ")");
582 int mappedCodonStart = mappedPos[0]; // position (1...) of codon start
583 int mappedCodonEnd = mappedPos[mappedPos.length - 1]; // codon end pos
584 StringBuilder trailingCopiedGap = new StringBuilder();
587 * Copy dna sequence up to and including this codon. Optionally, include
588 * gaps before the codon starts (in introns) and/or after the codon starts
591 * Note this only works for 'linear' splicing, not reverse or interleaved.
592 * But then 'align dna as protein' doesn't make much sense otherwise.
594 int intronLength = 0;
595 while (basesWritten < mappedCodonEnd && thisSeqPos < thisSeq.length)
597 final char c = thisSeq[thisSeqPos++];
602 if (basesWritten < mappedCodonStart)
605 * Found an unmapped (intron) base. First add in any preceding gaps
608 if (preserveUnmappedGaps && trailingCopiedGap.length() > 0)
610 thisAligned.append(trailingCopiedGap.toString());
611 intronLength += trailingCopiedGap.length();
612 trailingCopiedGap = new StringBuilder();
619 final boolean startOfCodon = basesWritten == mappedCodonStart;
620 int gapsToAdd = calculateGapsToInsert(preserveMappedGaps,
621 preserveUnmappedGaps, sourceGapMappedLength, inExon,
622 trailingCopiedGap.length(), intronLength, startOfCodon);
623 for (int i = 0; i < gapsToAdd; i++)
625 thisAligned.append(myGapChar);
627 sourceGapMappedLength = 0;
630 thisAligned.append(c);
631 trailingCopiedGap = new StringBuilder();
635 if (inExon && preserveMappedGaps)
637 trailingCopiedGap.append(myGapChar);
639 else if (!inExon && preserveUnmappedGaps)
641 trailingCopiedGap.append(myGapChar);
648 * At end of protein sequence. Copy any remaining dna sequence, optionally
649 * including (intron) gaps. We do not copy trailing gaps in protein.
651 while (thisSeqPos < thisSeq.length)
653 final char c = thisSeq[thisSeqPos++];
654 if (c != myGapChar || preserveUnmappedGaps)
656 thisAligned.append(c);
661 * All done aligning, set the aligned sequence.
663 alignTo.setSequence(new String(thisAligned));
667 * Helper method to work out how many gaps to insert when realigning.
669 * @param preserveMappedGaps
670 * @param preserveUnmappedGaps
671 * @param sourceGapMappedLength
673 * @param trailingCopiedGap
674 * @param intronLength
675 * @param startOfCodon
678 protected static int calculateGapsToInsert(boolean preserveMappedGaps,
679 boolean preserveUnmappedGaps, int sourceGapMappedLength,
680 boolean inExon, int trailingGapLength,
681 int intronLength, final boolean startOfCodon)
687 * Reached start of codon. Ignore trailing gaps in intron unless we are
688 * preserving gaps in both exon and intron. Ignore them anyway if the
689 * protein alignment introduces a gap at least as large as the intronic
692 if (inExon && !preserveMappedGaps)
694 trailingGapLength = 0;
696 if (!inExon && !(preserveMappedGaps && preserveUnmappedGaps))
698 trailingGapLength = 0;
702 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
706 if (intronLength + trailingGapLength <= sourceGapMappedLength)
708 gapsToAdd = sourceGapMappedLength - intronLength;
712 gapsToAdd = Math.min(intronLength + trailingGapLength
713 - sourceGapMappedLength, trailingGapLength);
720 * second or third base of codon; check for any gaps in dna
722 if (!preserveMappedGaps)
724 trailingGapLength = 0;
726 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
732 * Returns a list of sequences mapped from the given sequences and aligned
733 * (gapped) in the same way. For example, the cDNA for aligned protein, where
734 * a single gap in protein generates three gaps in cDNA.
737 * @param gapCharacter
741 public static List<SequenceI> getAlignedTranslation(
742 List<SequenceI> sequences, char gapCharacter,
743 Set<AlignedCodonFrame> mappings)
745 List<SequenceI> alignedSeqs = new ArrayList<SequenceI>();
747 for (SequenceI seq : sequences)
749 List<SequenceI> mapped = getAlignedTranslation(seq, gapCharacter,
751 alignedSeqs.addAll(mapped);
757 * Returns sequences aligned 'like' the source sequence, as mapped by the
758 * given mappings. Normally we expect zero or one 'mapped' sequences, but this
759 * will support 1-to-many as well.
762 * @param gapCharacter
766 protected static List<SequenceI> getAlignedTranslation(SequenceI seq,
767 char gapCharacter, Set<AlignedCodonFrame> mappings)
769 List<SequenceI> result = new ArrayList<SequenceI>();
770 for (AlignedCodonFrame mapping : mappings)
772 if (mapping.involvesSequence(seq))
774 SequenceI mapped = getAlignedTranslation(seq, gapCharacter, mapping);
785 * Returns the translation of 'seq' (as held in the mapping) with
786 * corresponding alignment (gaps).
789 * @param gapCharacter
793 protected static SequenceI getAlignedTranslation(SequenceI seq,
794 char gapCharacter, AlignedCodonFrame mapping)
796 String gap = String.valueOf(gapCharacter);
797 boolean toDna = false;
799 SequenceI mapTo = mapping.getDnaForAaSeq(seq);
802 // mapping is from protein to nucleotide
804 // should ideally get gap count ratio from mapping
805 gap = String.valueOf(new char[]
806 { gapCharacter, gapCharacter, gapCharacter });
810 // mapping is from nucleotide to protein
811 mapTo = mapping.getAaForDnaSeq(seq);
814 StringBuilder newseq = new StringBuilder(seq.getLength()
817 int residueNo = 0; // in seq, base 1
818 int[] phrase = new int[fromRatio];
819 int phraseOffset = 0;
821 boolean first = true;
822 final Sequence alignedSeq = new Sequence("", "");
824 for (char c : seq.getSequence())
826 if (c == gapCharacter)
829 if (gapWidth >= fromRatio)
837 phrase[phraseOffset++] = residueNo + 1;
838 if (phraseOffset == fromRatio)
841 * Have read a whole codon (or protein residue), now translate: map
842 * source phrase to positions in target sequence add characters at
843 * these positions to newseq Note mapping positions are base 1, our
844 * sequence positions base 0.
846 SearchResults sr = new SearchResults();
847 for (int pos : phrase)
849 mapping.markMappedRegion(seq, pos, sr);
851 newseq.append(sr.toString());
855 // Hack: Copy sequence dataset, name and description from
856 // SearchResults.match[0].sequence
857 // TODO? carry over sequence names from original 'complement'
859 SequenceI mappedTo = sr.getResultSequence(0);
860 alignedSeq.setName(mappedTo.getName());
861 alignedSeq.setDescription(mappedTo.getDescription());
862 alignedSeq.setDatasetSequence(mappedTo);
869 alignedSeq.setSequence(newseq.toString());
874 * Realigns the given protein to match the alignment of the dna, using codon
875 * mappings to translate aligned codon positions to protein residues.
878 * the alignment whose sequences are realigned by this method
880 * the dna alignment whose alignment we are 'copying'
881 * @return the number of sequences that were realigned
883 public static int alignProteinAsDna(AlignmentI protein, AlignmentI dna)
885 Set<AlignedCodonFrame> mappings = protein.getCodonFrames();
888 * Map will hold, for each aligned codon position e.g. [3, 5, 6], a map of
889 * {dnaSequence, {proteinSequence, codonProduct}} at that position. The
890 * comparator keeps the codon positions ordered.
892 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons = new TreeMap<AlignedCodon, Map<SequenceI, String>>(
893 new CodonComparator());
894 for (SequenceI dnaSeq : dna.getSequences())
896 for (AlignedCodonFrame mapping : mappings)
898 Mapping seqMap = mapping.getMappingForSequence(dnaSeq);
899 SequenceI prot = mapping.findAlignedSequence(
900 dnaSeq.getDatasetSequence(), protein);
903 addCodonPositions(dnaSeq, prot, protein.getGapCharacter(),
904 seqMap, alignedCodons);
908 return alignProteinAs(protein, alignedCodons);
912 * Update the aligned protein sequences to match the codon alignments given in
916 * @param alignedCodons
917 * an ordered map of codon positions (columns), with sequence/peptide
918 * values present in each column
921 protected static int alignProteinAs(AlignmentI protein,
922 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons)
925 * Prefill aligned sequences with gaps before inserting aligned protein
928 int alignedWidth = alignedCodons.size();
929 char[] gaps = new char[alignedWidth];
930 Arrays.fill(gaps, protein.getGapCharacter());
931 String allGaps = String.valueOf(gaps);
932 for (SequenceI seq : protein.getSequences())
934 seq.setSequence(allGaps);
938 for (AlignedCodon codon : alignedCodons.keySet())
940 final Map<SequenceI, String> columnResidues = alignedCodons.get(codon);
941 for (Entry<SequenceI, String> entry : columnResidues
944 // place translated codon at its column position in sequence
945 entry.getKey().getSequence()[column] = entry.getValue().charAt(0);
953 * Populate the map of aligned codons by traversing the given sequence
954 * mapping, locating the aligned positions of mapped codons, and adding those
955 * positions and their translation products to the map.
958 * the aligned sequence we are mapping from
960 * the sequence to be aligned to the codons
962 * the gap character in the dna sequence
964 * a mapping to a sequence translation
965 * @param alignedCodons
966 * the map we are building up
968 static void addCodonPositions(SequenceI dna, SequenceI protein,
971 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons)
973 Iterator<AlignedCodon> codons = seqMap.getCodonIterator(dna, gapChar);
974 while (codons.hasNext())
976 AlignedCodon codon = codons.next();
977 Map<SequenceI, String> seqProduct = alignedCodons.get(codon);
978 if (seqProduct == null)
980 seqProduct = new HashMap<SequenceI, String>();
981 alignedCodons.put(codon, seqProduct);
983 seqProduct.put(protein, codon.product);
988 * Returns true if a cDNA/Protein mapping either exists, or could be made,
989 * between at least one pair of sequences in the two alignments. Currently,
992 * <li>One alignment must be nucleotide, and the other protein</li>
993 * <li>At least one pair of sequences must be already mapped, or mappable</li>
994 * <li>Mappable means the nucleotide translation matches the protein sequence</li>
995 * <li>The translation may ignore start and stop codons if present in the
1003 public static boolean isMappable(AlignmentI al1, AlignmentI al2)
1006 * Require one nucleotide and one protein
1008 if (al1.isNucleotide() == al2.isNucleotide())
1012 AlignmentI dna = al1.isNucleotide() ? al1 : al2;
1013 AlignmentI protein = dna == al1 ? al2 : al1;
1014 Set<AlignedCodonFrame> mappings = protein.getCodonFrames();
1015 for (SequenceI dnaSeq : dna.getSequences())
1017 for (SequenceI proteinSeq : protein.getSequences())
1019 if (isMappable(dnaSeq, proteinSeq, mappings))
1029 * Returns true if the dna sequence is mapped, or could be mapped, to the
1037 public static boolean isMappable(SequenceI dnaSeq, SequenceI proteinSeq,
1038 Set<AlignedCodonFrame> mappings)
1040 SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq.getDatasetSequence();
1041 SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq
1042 : proteinSeq.getDatasetSequence();
1047 for (AlignedCodonFrame mapping : mappings) {
1048 if ( proteinDs == mapping.getAaForDnaSeq(dnaDs)) {
1054 * Just try to make a mapping (it is not yet stored), test whether
1057 return mapProteinToCdna(proteinDs, dnaDs) != null;