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)))
458 // fail if we didn't match all of the aa sequence
459 return (aaResidue == aaSeqChars.length);
463 * Align sequence 'seq' to match the alignment of a mapped sequence. Note this
464 * currently assumes that we are aligning cDNA to match protein.
467 * the sequence to be realigned
469 * the alignment whose sequence alignment is to be 'copied'
471 * character string represent a gap in the realigned sequence
472 * @param preserveUnmappedGaps
473 * @param preserveMappedGaps
474 * @return true if the sequence was realigned, false if it could not be
476 public static boolean alignSequenceAs(SequenceI seq, AlignmentI al,
477 String gap, boolean preserveMappedGaps,
478 boolean preserveUnmappedGaps)
481 * Get any mappings from the source alignment to the target (dataset) sequence.
483 // TODO there may be one AlignedCodonFrame per dataset sequence, or one with
484 // all mappings. Would it help to constrain this?
485 List<AlignedCodonFrame> mappings = al.getCodonFrame(seq);
486 if (mappings == null || mappings.isEmpty())
492 * Locate the aligned source sequence whose dataset sequence is mapped. We
493 * just take the first match here (as we can't align cDNA like more than one
496 SequenceI alignFrom = null;
497 AlignedCodonFrame mapping = null;
498 for (AlignedCodonFrame mp : mappings)
500 alignFrom = mp.findAlignedSequence(seq.getDatasetSequence(), al);
501 if (alignFrom != null)
508 if (alignFrom == null)
512 alignSequenceAs(seq, alignFrom, mapping, gap, al.getGapCharacter(),
513 preserveMappedGaps, preserveUnmappedGaps);
518 * Align sequence 'alignTo' the same way as 'alignFrom', using the mapping to
519 * match residues and codons. Flags control whether existing gaps in unmapped
520 * (intron) and mapped (exon) regions are preserved or not. Gaps linking intro
521 * and exon are only retained if both flags are set.
528 * @param preserveUnmappedGaps
529 * @param preserveMappedGaps
531 public static void alignSequenceAs(SequenceI alignTo,
533 AlignedCodonFrame mapping, String myGap, char sourceGap,
534 boolean preserveMappedGaps, boolean preserveUnmappedGaps)
536 // TODO generalise to work for Protein-Protein, dna-dna, dna-protein
537 final char[] thisSeq = alignTo.getSequence();
538 final char[] thatAligned = alignFrom.getSequence();
539 StringBuilder thisAligned = new StringBuilder(2 * thisSeq.length);
541 // aligned and dataset sequence positions, all base zero
545 int basesWritten = 0;
546 char myGapChar = myGap.charAt(0);
547 int ratio = myGap.length();
550 * Traverse the aligned protein sequence.
552 int sourceGapMappedLength = 0;
553 boolean inExon = false;
554 for (char sourceChar : thatAligned)
556 if (sourceChar == sourceGap)
558 sourceGapMappedLength += ratio;
563 * Found a residue. Locate its mapped codon (start) position.
566 // Note mapping positions are base 1, our sequence positions base 0
567 int[] mappedPos = mapping.getMappedRegion(alignTo, alignFrom,
569 if (mappedPos == null)
572 * Abort realignment if unmapped protein. Or could ignore it??
574 System.err.println("Can't align: no codon mapping to residue "
575 + sourceDsPos + "(" + sourceChar + ")");
579 int mappedCodonStart = mappedPos[0]; // position (1...) of codon start
580 int mappedCodonEnd = mappedPos[mappedPos.length - 1]; // codon end pos
581 StringBuilder trailingCopiedGap = new StringBuilder();
584 * Copy dna sequence up to and including this codon. Optionally, include
585 * gaps before the codon starts (in introns) and/or after the codon starts
588 * Note this only works for 'linear' splicing, not reverse or interleaved.
589 * But then 'align dna as protein' doesn't make much sense otherwise.
591 int intronLength = 0;
592 while (basesWritten < mappedCodonEnd && thisSeqPos < thisSeq.length)
594 final char c = thisSeq[thisSeqPos++];
599 if (basesWritten < mappedCodonStart)
602 * Found an unmapped (intron) base. First add in any preceding gaps
605 if (preserveUnmappedGaps && trailingCopiedGap.length() > 0)
607 thisAligned.append(trailingCopiedGap.toString());
608 intronLength += trailingCopiedGap.length();
609 trailingCopiedGap = new StringBuilder();
616 final boolean startOfCodon = basesWritten == mappedCodonStart;
617 int gapsToAdd = calculateGapsToInsert(preserveMappedGaps,
618 preserveUnmappedGaps, sourceGapMappedLength, inExon,
619 trailingCopiedGap.length(), intronLength, startOfCodon);
620 for (int i = 0; i < gapsToAdd; i++)
622 thisAligned.append(myGapChar);
624 sourceGapMappedLength = 0;
627 thisAligned.append(c);
628 trailingCopiedGap = new StringBuilder();
632 if (inExon && preserveMappedGaps)
634 trailingCopiedGap.append(myGapChar);
636 else if (!inExon && preserveUnmappedGaps)
638 trailingCopiedGap.append(myGapChar);
645 * At end of protein sequence. Copy any remaining dna sequence, optionally
646 * including (intron) gaps. We do not copy trailing gaps in protein.
648 while (thisSeqPos < thisSeq.length)
650 final char c = thisSeq[thisSeqPos++];
651 if (c != myGapChar || preserveUnmappedGaps)
653 thisAligned.append(c);
658 * All done aligning, set the aligned sequence.
660 alignTo.setSequence(new String(thisAligned));
664 * Helper method to work out how many gaps to insert when realigning.
666 * @param preserveMappedGaps
667 * @param preserveUnmappedGaps
668 * @param sourceGapMappedLength
670 * @param trailingCopiedGap
671 * @param intronLength
672 * @param startOfCodon
675 protected static int calculateGapsToInsert(boolean preserveMappedGaps,
676 boolean preserveUnmappedGaps, int sourceGapMappedLength,
677 boolean inExon, int trailingGapLength,
678 int intronLength, final boolean startOfCodon)
684 * Reached start of codon. Ignore trailing gaps in intron unless we are
685 * preserving gaps in both exon and intron. Ignore them anyway if the
686 * protein alignment introduces a gap at least as large as the intronic
689 if (inExon && !preserveMappedGaps)
691 trailingGapLength = 0;
693 if (!inExon && !(preserveMappedGaps && preserveUnmappedGaps))
695 trailingGapLength = 0;
699 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
703 if (intronLength + trailingGapLength <= sourceGapMappedLength)
705 gapsToAdd = sourceGapMappedLength - intronLength;
709 gapsToAdd = Math.min(intronLength + trailingGapLength
710 - sourceGapMappedLength, trailingGapLength);
717 * second or third base of codon; check for any gaps in dna
719 if (!preserveMappedGaps)
721 trailingGapLength = 0;
723 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
729 * Returns a list of sequences mapped from the given sequences and aligned
730 * (gapped) in the same way. For example, the cDNA for aligned protein, where
731 * a single gap in protein generates three gaps in cDNA.
734 * @param gapCharacter
738 public static List<SequenceI> getAlignedTranslation(
739 List<SequenceI> sequences, char gapCharacter,
740 Set<AlignedCodonFrame> mappings)
742 List<SequenceI> alignedSeqs = new ArrayList<SequenceI>();
744 for (SequenceI seq : sequences)
746 List<SequenceI> mapped = getAlignedTranslation(seq, gapCharacter,
748 alignedSeqs.addAll(mapped);
754 * Returns sequences aligned 'like' the source sequence, as mapped by the
755 * given mappings. Normally we expect zero or one 'mapped' sequences, but this
756 * will support 1-to-many as well.
759 * @param gapCharacter
763 protected static List<SequenceI> getAlignedTranslation(SequenceI seq,
764 char gapCharacter, Set<AlignedCodonFrame> mappings)
766 List<SequenceI> result = new ArrayList<SequenceI>();
767 for (AlignedCodonFrame mapping : mappings)
769 if (mapping.involvesSequence(seq))
771 SequenceI mapped = getAlignedTranslation(seq, gapCharacter, mapping);
782 * Returns the translation of 'seq' (as held in the mapping) with
783 * corresponding alignment (gaps).
786 * @param gapCharacter
790 protected static SequenceI getAlignedTranslation(SequenceI seq,
791 char gapCharacter, AlignedCodonFrame mapping)
793 String gap = String.valueOf(gapCharacter);
794 boolean toDna = false;
796 SequenceI mapTo = mapping.getDnaForAaSeq(seq);
799 // mapping is from protein to nucleotide
801 // should ideally get gap count ratio from mapping
802 gap = String.valueOf(new char[]
803 { gapCharacter, gapCharacter, gapCharacter });
807 // mapping is from nucleotide to protein
808 mapTo = mapping.getAaForDnaSeq(seq);
811 StringBuilder newseq = new StringBuilder(seq.getLength()
814 int residueNo = 0; // in seq, base 1
815 int[] phrase = new int[fromRatio];
816 int phraseOffset = 0;
818 boolean first = true;
819 final Sequence alignedSeq = new Sequence("", "");
821 for (char c : seq.getSequence())
823 if (c == gapCharacter)
826 if (gapWidth >= fromRatio)
834 phrase[phraseOffset++] = residueNo + 1;
835 if (phraseOffset == fromRatio)
838 * Have read a whole codon (or protein residue), now translate: map
839 * source phrase to positions in target sequence add characters at
840 * these positions to newseq Note mapping positions are base 1, our
841 * sequence positions base 0.
843 SearchResults sr = new SearchResults();
844 for (int pos : phrase)
846 mapping.markMappedRegion(seq, pos, sr);
848 newseq.append(sr.toString());
852 // Hack: Copy sequence dataset, name and description from
853 // SearchResults.match[0].sequence
854 // TODO? carry over sequence names from original 'complement'
856 SequenceI mappedTo = sr.getResultSequence(0);
857 alignedSeq.setName(mappedTo.getName());
858 alignedSeq.setDescription(mappedTo.getDescription());
859 alignedSeq.setDatasetSequence(mappedTo);
866 alignedSeq.setSequence(newseq.toString());
871 * Realigns the given protein to match the alignment of the dna, using codon
872 * mappings to translate aligned codon positions to protein residues.
875 * the alignment whose sequences are realigned by this method
877 * the dna alignment whose alignment we are 'copying'
878 * @return the number of sequences that were realigned
880 public static int alignProteinAsDna(AlignmentI protein, AlignmentI dna)
882 Set<AlignedCodonFrame> mappings = protein.getCodonFrames();
885 * Map will hold, for each aligned codon position e.g. [3, 5, 6], a map of
886 * {dnaSequence, {proteinSequence, codonProduct}} at that position. The
887 * comparator keeps the codon positions ordered.
889 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons = new TreeMap<AlignedCodon, Map<SequenceI, String>>(
890 new CodonComparator());
891 for (SequenceI dnaSeq : dna.getSequences())
893 for (AlignedCodonFrame mapping : mappings)
895 Mapping seqMap = mapping.getMappingForSequence(dnaSeq);
896 SequenceI prot = mapping.findAlignedSequence(
897 dnaSeq.getDatasetSequence(), protein);
900 addCodonPositions(dnaSeq, prot, protein.getGapCharacter(),
901 seqMap, alignedCodons);
905 return alignProteinAs(protein, alignedCodons);
909 * Update the aligned protein sequences to match the codon alignments given in
913 * @param alignedCodons
914 * an ordered map of codon positions (columns), with sequence/peptide
915 * values present in each column
918 protected static int alignProteinAs(AlignmentI protein,
919 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons)
922 * Prefill aligned sequences with gaps before inserting aligned protein
925 int alignedWidth = alignedCodons.size();
926 char[] gaps = new char[alignedWidth];
927 Arrays.fill(gaps, protein.getGapCharacter());
928 String allGaps = String.valueOf(gaps);
929 for (SequenceI seq : protein.getSequences())
931 seq.setSequence(allGaps);
935 for (AlignedCodon codon : alignedCodons.keySet())
937 final Map<SequenceI, String> columnResidues = alignedCodons.get(codon);
938 for (Entry<SequenceI, String> entry : columnResidues
941 // place translated codon at its column position in sequence
942 entry.getKey().getSequence()[column] = entry.getValue().charAt(0);
950 * Populate the map of aligned codons by traversing the given sequence
951 * mapping, locating the aligned positions of mapped codons, and adding those
952 * positions and their translation products to the map.
955 * the aligned sequence we are mapping from
957 * the sequence to be aligned to the codons
959 * the gap character in the dna sequence
961 * a mapping to a sequence translation
962 * @param alignedCodons
963 * the map we are building up
965 static void addCodonPositions(SequenceI dna, SequenceI protein,
968 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons)
970 Iterator<AlignedCodon> codons = seqMap.getCodonIterator(dna, gapChar);
971 while (codons.hasNext())
973 AlignedCodon codon = codons.next();
974 Map<SequenceI, String> seqProduct = alignedCodons.get(codon);
975 if (seqProduct == null)
977 seqProduct = new HashMap<SequenceI, String>();
978 alignedCodons.put(codon, seqProduct);
980 seqProduct.put(protein, codon.product);
985 * Returns true if a cDNA/Protein mapping either exists, or could be made,
986 * between at least one pair of sequences in the two alignments. Currently,
989 * <li>One alignment must be nucleotide, and the other protein</li>
990 * <li>At least one pair of sequences must be already mapped, or mappable</li>
991 * <li>Mappable means the nucleotide translation matches the protein sequence</li>
992 * <li>The translation may ignore start and stop codons if present in the
1000 public static boolean isMappable(AlignmentI al1, AlignmentI al2)
1003 * Require one nucleotide and one protein
1005 if (al1.isNucleotide() == al2.isNucleotide())
1009 AlignmentI dna = al1.isNucleotide() ? al1 : al2;
1010 AlignmentI protein = dna == al1 ? al2 : al1;
1011 Set<AlignedCodonFrame> mappings = protein.getCodonFrames();
1012 for (SequenceI dnaSeq : dna.getSequences())
1014 for (SequenceI proteinSeq : protein.getSequences())
1016 if (isMappable(dnaSeq, proteinSeq, mappings))
1026 * Returns true if the dna sequence is mapped, or could be mapped, to the
1034 public static boolean isMappable(SequenceI dnaSeq, SequenceI proteinSeq,
1035 Set<AlignedCodonFrame> mappings)
1037 SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq.getDatasetSequence();
1038 SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq
1039 : proteinSeq.getDatasetSequence();
1044 for (AlignedCodonFrame mapping : mappings) {
1045 if ( proteinDs == mapping.getAaForDnaSeq(dnaDs)) {
1051 * Just try to make a mapping (it is not yet stored), test whether
1054 return mapProteinToCdna(proteinDs, dnaDs) != null;