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.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.FeatureProperties;
31 import jalview.datamodel.Mapping;
32 import jalview.datamodel.SearchResults;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
36 import jalview.schemes.ResidueProperties;
37 import jalview.util.DBRefUtils;
38 import jalview.util.MapList;
39 import jalview.util.MappingUtils;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.LinkedHashMap;
48 import java.util.List;
50 import java.util.Map.Entry;
52 import java.util.TreeMap;
55 * grab bag of useful alignment manipulation operations Expect these to be
56 * refactored elsewhere at some point.
61 public class AlignmentUtils
65 * given an existing alignment, create a new alignment including all, or up to
66 * flankSize additional symbols from each sequence's dataset sequence
72 public static AlignmentI expandContext(AlignmentI core, int flankSize)
74 List<SequenceI> sq = new ArrayList<SequenceI>();
76 for (SequenceI s : core.getSequences())
78 SequenceI newSeq = s.deriveSequence();
79 final int newSeqStart = newSeq.getStart() - 1;
80 if (newSeqStart > maxoffset
81 && newSeq.getDatasetSequence().getStart() < s.getStart())
83 maxoffset = newSeqStart;
89 maxoffset = Math.min(maxoffset, flankSize);
93 * now add offset left and right to create an expanded alignment
95 for (SequenceI s : sq)
98 while (ds.getDatasetSequence() != null)
100 ds = ds.getDatasetSequence();
102 int s_end = s.findPosition(s.getStart() + s.getLength());
103 // find available flanking residues for sequence
104 int ustream_ds = s.getStart() - ds.getStart();
105 int dstream_ds = ds.getEnd() - s_end;
107 // build new flanked sequence
109 // compute gap padding to start of flanking sequence
110 int offset = maxoffset - ustream_ds;
112 // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
115 if (flankSize < ustream_ds)
117 // take up to flankSize residues
118 offset = maxoffset - flankSize;
119 ustream_ds = flankSize;
121 if (flankSize <= dstream_ds)
123 dstream_ds = flankSize - 1;
126 // TODO use Character.toLowerCase to avoid creating String objects?
127 char[] upstream = new String(ds.getSequence(s.getStart() - 1
128 - ustream_ds, s.getStart() - 1)).toLowerCase().toCharArray();
129 char[] downstream = new String(ds.getSequence(s_end - 1, s_end
130 + dstream_ds)).toLowerCase().toCharArray();
131 char[] coreseq = s.getSequence();
132 char[] nseq = new char[offset + upstream.length + downstream.length
134 char c = core.getGapCharacter();
137 for (; p < offset; p++)
142 System.arraycopy(upstream, 0, nseq, p, upstream.length);
143 System.arraycopy(coreseq, 0, nseq, p + upstream.length,
145 System.arraycopy(downstream, 0, nseq, p + coreseq.length
146 + upstream.length, downstream.length);
147 s.setSequence(new String(nseq));
148 s.setStart(s.getStart() - ustream_ds);
149 s.setEnd(s_end + downstream.length);
151 AlignmentI newAl = new jalview.datamodel.Alignment(
152 sq.toArray(new SequenceI[0]));
153 for (SequenceI s : sq)
155 if (s.getAnnotation() != null)
157 for (AlignmentAnnotation aa : s.getAnnotation())
159 aa.adjustForAlignment(); // JAL-1712 fix
160 newAl.addAnnotation(aa);
164 newAl.setDataset(core.getDataset());
169 * Returns the index (zero-based position) of a sequence in an alignment, or
176 public static int getSequenceIndex(AlignmentI al, SequenceI seq)
180 for (SequenceI alSeq : al.getSequences())
193 * Returns a map of lists of sequences in the alignment, keyed by sequence
194 * name. For use in mapping between different alignment views of the same
197 * @see jalview.datamodel.AlignmentI#getSequencesByName()
199 public static Map<String, List<SequenceI>> getSequencesByName(
202 Map<String, List<SequenceI>> theMap = new LinkedHashMap<String, List<SequenceI>>();
203 for (SequenceI seq : al.getSequences())
205 String name = seq.getName();
208 List<SequenceI> seqs = theMap.get(name);
211 seqs = new ArrayList<SequenceI>();
212 theMap.put(name, seqs);
221 * Build mapping of protein to cDNA alignment. Mappings are made between
222 * sequences where the cDNA translates to the protein sequence. Any new
223 * mappings are added to the protein alignment. Returns true if any mappings
224 * either already exist or were added, else false.
226 * @param proteinAlignment
227 * @param cdnaAlignment
230 public static boolean mapProteinAlignmentToCdna(
231 final AlignmentI proteinAlignment, final AlignmentI cdnaAlignment)
233 if (proteinAlignment == null || cdnaAlignment == null)
238 Set<SequenceI> mappedDna = new HashSet<SequenceI>();
239 Set<SequenceI> mappedProtein = new HashSet<SequenceI>();
242 * First pass - map sequences where cross-references exist. This include
243 * 1-to-many mappings to support, for example, variant cDNA.
245 boolean mappingPerformed = mapProteinToCdna(proteinAlignment,
246 cdnaAlignment, mappedDna, mappedProtein, true);
249 * Second pass - map sequences where no cross-references exist. This only
250 * does 1-to-1 mappings and assumes corresponding sequences are in the same
251 * order in the alignments.
253 mappingPerformed |= mapProteinToCdna(proteinAlignment, cdnaAlignment,
254 mappedDna, mappedProtein, false);
255 return mappingPerformed;
259 * Make mappings between compatible sequences (where the cDNA translation
260 * matches the protein).
262 * @param proteinAlignment
263 * @param cdnaAlignment
265 * a set of mapped DNA sequences (to add to)
266 * @param mappedProtein
267 * a set of mapped Protein sequences (to add to)
269 * if true, only map sequences where xrefs exist
272 protected static boolean mapProteinToCdna(
273 final AlignmentI proteinAlignment,
274 final AlignmentI cdnaAlignment, Set<SequenceI> mappedDna,
275 Set<SequenceI> mappedProtein, boolean xrefsOnly)
277 boolean mappingExistsOrAdded = false;
278 List<SequenceI> thisSeqs = proteinAlignment.getSequences();
279 for (SequenceI aaSeq : thisSeqs)
281 boolean proteinMapped = false;
282 AlignedCodonFrame acf = new AlignedCodonFrame();
284 for (SequenceI cdnaSeq : cdnaAlignment.getSequences())
287 * Always try to map if sequences have xref to each other; this supports
288 * variant cDNA or alternative splicing for a protein sequence.
290 * If no xrefs, try to map progressively, assuming that alignments have
291 * mappable sequences in corresponding order. These are not
292 * many-to-many, as that would risk mixing species with similar cDNA
295 if (xrefsOnly && !AlignmentUtils.haveCrossRef(aaSeq, cdnaSeq))
301 * Don't map non-xrefd sequences more than once each. This heuristic
302 * allows us to pair up similar sequences in ordered alignments.
305 && (mappedProtein.contains(aaSeq) || mappedDna
310 if (mappingExists(proteinAlignment.getCodonFrames(),
311 aaSeq.getDatasetSequence(), cdnaSeq.getDatasetSequence()))
313 mappingExistsOrAdded = true;
317 MapList map = mapProteinSequenceToCdna(aaSeq, cdnaSeq);
320 acf.addMap(cdnaSeq, aaSeq, map);
321 mappingExistsOrAdded = true;
322 proteinMapped = true;
323 mappedDna.add(cdnaSeq);
324 mappedProtein.add(aaSeq);
330 proteinAlignment.addCodonFrame(acf);
333 return mappingExistsOrAdded;
337 * Answers true if the mappings include one between the given (dataset)
340 public static boolean mappingExists(List<AlignedCodonFrame> mappings,
341 SequenceI aaSeq, SequenceI cdnaSeq)
343 if (mappings != null)
345 for (AlignedCodonFrame acf : mappings)
347 if (cdnaSeq == acf.getDnaForAaSeq(aaSeq))
357 * Build a mapping (if possible) of a protein to a cDNA sequence. The cDNA
358 * must be three times the length of the protein, possibly after ignoring
359 * start and/or stop codons, and must translate to the protein. Returns null
360 * if no mapping is determined.
366 public static MapList mapProteinSequenceToCdna(SequenceI proteinSeq,
370 * Here we handle either dataset sequence set (desktop) or absent (applet).
371 * Use only the char[] form of the sequence to avoid creating possibly large
374 final SequenceI proteinDataset = proteinSeq.getDatasetSequence();
375 char[] aaSeqChars = proteinDataset != null ? proteinDataset
376 .getSequence() : proteinSeq.getSequence();
377 final SequenceI cdnaDataset = cdnaSeq.getDatasetSequence();
378 char[] cdnaSeqChars = cdnaDataset != null ? cdnaDataset.getSequence()
379 : cdnaSeq.getSequence();
380 if (aaSeqChars == null || cdnaSeqChars == null)
386 * cdnaStart/End, proteinStartEnd are base 1 (for dataset sequence mapping)
388 final int mappedLength = 3 * aaSeqChars.length;
389 int cdnaLength = cdnaSeqChars.length;
390 int cdnaStart = cdnaSeq.getStart();
391 int cdnaEnd = cdnaSeq.getEnd();
392 final int proteinStart = proteinSeq.getStart();
393 final int proteinEnd = proteinSeq.getEnd();
396 * If lengths don't match, try ignoring stop codon.
398 if (cdnaLength != mappedLength && cdnaLength > 2)
400 String lastCodon = String.valueOf(cdnaSeqChars, cdnaLength - 3, 3)
402 for (String stop : ResidueProperties.STOP)
404 if (lastCodon.equals(stop))
414 * If lengths still don't match, try ignoring start codon.
417 if (cdnaLength != mappedLength
419 && String.valueOf(cdnaSeqChars, 0, 3).toUpperCase()
420 .equals(ResidueProperties.START))
427 if (cdnaLength != mappedLength)
431 if (!translatesAs(cdnaSeqChars, startOffset, aaSeqChars))
435 MapList map = new MapList(new int[] { cdnaStart, cdnaEnd }, new int[] {
436 proteinStart, proteinEnd }, 3, 1);
441 * Test whether the given cdna sequence, starting at the given offset,
442 * translates to the given amino acid sequence, using the standard translation
443 * table. Designed to fail fast i.e. as soon as a mismatch position is found.
445 * @param cdnaSeqChars
450 protected static boolean translatesAs(char[] cdnaSeqChars, int cdnaStart,
453 if (cdnaSeqChars == null || aaSeqChars == null)
459 for (int i = cdnaStart; i < cdnaSeqChars.length - 2
460 && aaResidue < aaSeqChars.length; i += 3, aaResidue++)
462 String codon = String.valueOf(cdnaSeqChars, i, 3);
463 final String translated = ResidueProperties.codonTranslate(codon);
465 * allow * in protein to match untranslatable in dna
467 final char aaRes = aaSeqChars[aaResidue];
468 if ((translated == null || "STOP".equals(translated)) && aaRes == '*')
472 if (translated == null || !(aaRes == translated.charAt(0)))
475 // System.out.println(("Mismatch at " + i + "/" + aaResidue + ": "
476 // + codon + "(" + translated + ") != " + aaRes));
480 // fail if we didn't match all of the aa sequence
481 return (aaResidue == aaSeqChars.length);
485 * Align sequence 'seq' to match the alignment of a mapped sequence. Note this
486 * currently assumes that we are aligning cDNA to match protein.
489 * the sequence to be realigned
491 * the alignment whose sequence alignment is to be 'copied'
493 * character string represent a gap in the realigned sequence
494 * @param preserveUnmappedGaps
495 * @param preserveMappedGaps
496 * @return true if the sequence was realigned, false if it could not be
498 public static boolean alignSequenceAs(SequenceI seq, AlignmentI al,
499 String gap, boolean preserveMappedGaps,
500 boolean preserveUnmappedGaps)
503 * Get any mappings from the source alignment to the target (dataset)
506 // TODO there may be one AlignedCodonFrame per dataset sequence, or one with
507 // all mappings. Would it help to constrain this?
508 List<AlignedCodonFrame> mappings = al.getCodonFrame(seq);
509 if (mappings == null || mappings.isEmpty())
515 * Locate the aligned source sequence whose dataset sequence is mapped. We
516 * just take the first match here (as we can't align like more than one
519 SequenceI alignFrom = null;
520 AlignedCodonFrame mapping = null;
521 for (AlignedCodonFrame mp : mappings)
523 alignFrom = mp.findAlignedSequence(seq.getDatasetSequence(), al);
524 if (alignFrom != null)
531 if (alignFrom == null)
535 alignSequenceAs(seq, alignFrom, mapping, gap, al.getGapCharacter(),
536 preserveMappedGaps, preserveUnmappedGaps);
541 * Align sequence 'alignTo' the same way as 'alignFrom', using the mapping to
542 * match residues and codons. Flags control whether existing gaps in unmapped
543 * (intron) and mapped (exon) regions are preserved or not. Gaps between
544 * intron and exon are only retained if both flags are set.
551 * @param preserveUnmappedGaps
552 * @param preserveMappedGaps
554 public static void alignSequenceAs(SequenceI alignTo,
555 SequenceI alignFrom, AlignedCodonFrame mapping, String myGap,
556 char sourceGap, boolean preserveMappedGaps,
557 boolean preserveUnmappedGaps)
559 // TODO generalise to work for Protein-Protein, dna-dna, dna-protein
561 // aligned and dataset sequence positions, all base zero
565 int basesWritten = 0;
566 char myGapChar = myGap.charAt(0);
567 int ratio = myGap.length();
569 int fromOffset = alignFrom.getStart() - 1;
570 int toOffset = alignTo.getStart() - 1;
571 int sourceGapMappedLength = 0;
572 boolean inExon = false;
573 final char[] thisSeq = alignTo.getSequence();
574 final char[] thatAligned = alignFrom.getSequence();
575 StringBuilder thisAligned = new StringBuilder(2 * thisSeq.length);
578 * Traverse the 'model' aligned sequence
580 for (char sourceChar : thatAligned)
582 if (sourceChar == sourceGap)
584 sourceGapMappedLength += ratio;
589 * Found a non-gap character. Locate its mapped region if any.
592 // Note mapping positions are base 1, our sequence positions base 0
593 int[] mappedPos = mapping.getMappedRegion(alignTo, alignFrom,
594 sourceDsPos + fromOffset);
595 if (mappedPos == null)
598 * unmapped position; treat like a gap
600 sourceGapMappedLength += ratio;
601 // System.err.println("Can't align: no codon mapping to residue "
602 // + sourceDsPos + "(" + sourceChar + ")");
607 int mappedCodonStart = mappedPos[0]; // position (1...) of codon start
608 int mappedCodonEnd = mappedPos[mappedPos.length - 1]; // codon end pos
609 StringBuilder trailingCopiedGap = new StringBuilder();
612 * Copy dna sequence up to and including this codon. Optionally, include
613 * gaps before the codon starts (in introns) and/or after the codon starts
616 * Note this only works for 'linear' splicing, not reverse or interleaved.
617 * But then 'align dna as protein' doesn't make much sense otherwise.
619 int intronLength = 0;
620 while (basesWritten + toOffset < mappedCodonEnd
621 && thisSeqPos < thisSeq.length)
623 final char c = thisSeq[thisSeqPos++];
627 int sourcePosition = basesWritten + toOffset;
628 if (sourcePosition < mappedCodonStart)
631 * Found an unmapped (intron) base. First add in any preceding gaps
634 if (preserveUnmappedGaps && trailingCopiedGap.length() > 0)
636 thisAligned.append(trailingCopiedGap.toString());
637 intronLength += trailingCopiedGap.length();
638 trailingCopiedGap = new StringBuilder();
645 final boolean startOfCodon = sourcePosition == mappedCodonStart;
646 int gapsToAdd = calculateGapsToInsert(preserveMappedGaps,
647 preserveUnmappedGaps, sourceGapMappedLength, inExon,
648 trailingCopiedGap.length(), intronLength, startOfCodon);
649 for (int i = 0; i < gapsToAdd; i++)
651 thisAligned.append(myGapChar);
653 sourceGapMappedLength = 0;
656 thisAligned.append(c);
657 trailingCopiedGap = new StringBuilder();
661 if (inExon && preserveMappedGaps)
663 trailingCopiedGap.append(myGapChar);
665 else if (!inExon && preserveUnmappedGaps)
667 trailingCopiedGap.append(myGapChar);
674 * At end of model aligned sequence. Copy any remaining target sequence, optionally
675 * including (intron) gaps.
677 while (thisSeqPos < thisSeq.length)
679 final char c = thisSeq[thisSeqPos++];
680 if (c != myGapChar || preserveUnmappedGaps)
682 thisAligned.append(c);
684 sourceGapMappedLength--;
688 * finally add gaps to pad for any trailing source gaps or
689 * unmapped characters
691 if (preserveUnmappedGaps)
693 while (sourceGapMappedLength > 0)
695 thisAligned.append(myGapChar);
696 sourceGapMappedLength--;
701 * All done aligning, set the aligned sequence.
703 alignTo.setSequence(new String(thisAligned));
707 * Helper method to work out how many gaps to insert when realigning.
709 * @param preserveMappedGaps
710 * @param preserveUnmappedGaps
711 * @param sourceGapMappedLength
713 * @param trailingCopiedGap
714 * @param intronLength
715 * @param startOfCodon
718 protected static int calculateGapsToInsert(boolean preserveMappedGaps,
719 boolean preserveUnmappedGaps, int sourceGapMappedLength,
720 boolean inExon, int trailingGapLength, int intronLength,
721 final boolean startOfCodon)
727 * Reached start of codon. Ignore trailing gaps in intron unless we are
728 * preserving gaps in both exon and intron. Ignore them anyway if the
729 * protein alignment introduces a gap at least as large as the intronic
732 if (inExon && !preserveMappedGaps)
734 trailingGapLength = 0;
736 if (!inExon && !(preserveMappedGaps && preserveUnmappedGaps))
738 trailingGapLength = 0;
742 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
746 if (intronLength + trailingGapLength <= sourceGapMappedLength)
748 gapsToAdd = sourceGapMappedLength - intronLength;
752 gapsToAdd = Math.min(intronLength + trailingGapLength
753 - sourceGapMappedLength, trailingGapLength);
760 * second or third base of codon; check for any gaps in dna
762 if (!preserveMappedGaps)
764 trailingGapLength = 0;
766 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
772 * Returns a list of sequences mapped from the given sequences and aligned
773 * (gapped) in the same way. For example, the cDNA for aligned protein, where
774 * a single gap in protein generates three gaps in cDNA.
777 * @param gapCharacter
781 public static List<SequenceI> getAlignedTranslation(
782 List<SequenceI> sequences, char gapCharacter,
783 Set<AlignedCodonFrame> mappings)
785 List<SequenceI> alignedSeqs = new ArrayList<SequenceI>();
787 for (SequenceI seq : sequences)
789 List<SequenceI> mapped = getAlignedTranslation(seq, gapCharacter,
791 alignedSeqs.addAll(mapped);
797 * Returns sequences aligned 'like' the source sequence, as mapped by the
798 * given mappings. Normally we expect zero or one 'mapped' sequences, but this
799 * will support 1-to-many as well.
802 * @param gapCharacter
806 protected static List<SequenceI> getAlignedTranslation(SequenceI seq,
807 char gapCharacter, Set<AlignedCodonFrame> mappings)
809 List<SequenceI> result = new ArrayList<SequenceI>();
810 for (AlignedCodonFrame mapping : mappings)
812 if (mapping.involvesSequence(seq))
814 SequenceI mapped = getAlignedTranslation(seq, gapCharacter, mapping);
825 * Returns the translation of 'seq' (as held in the mapping) with
826 * corresponding alignment (gaps).
829 * @param gapCharacter
833 protected static SequenceI getAlignedTranslation(SequenceI seq,
834 char gapCharacter, AlignedCodonFrame mapping)
836 String gap = String.valueOf(gapCharacter);
837 boolean toDna = false;
839 SequenceI mapTo = mapping.getDnaForAaSeq(seq);
842 // mapping is from protein to nucleotide
844 // should ideally get gap count ratio from mapping
845 gap = String.valueOf(new char[] { gapCharacter, gapCharacter,
850 // mapping is from nucleotide to protein
851 mapTo = mapping.getAaForDnaSeq(seq);
854 StringBuilder newseq = new StringBuilder(seq.getLength()
857 int residueNo = 0; // in seq, base 1
858 int[] phrase = new int[fromRatio];
859 int phraseOffset = 0;
861 boolean first = true;
862 final Sequence alignedSeq = new Sequence("", "");
864 for (char c : seq.getSequence())
866 if (c == gapCharacter)
869 if (gapWidth >= fromRatio)
877 phrase[phraseOffset++] = residueNo + 1;
878 if (phraseOffset == fromRatio)
881 * Have read a whole codon (or protein residue), now translate: map
882 * source phrase to positions in target sequence add characters at
883 * these positions to newseq Note mapping positions are base 1, our
884 * sequence positions base 0.
886 SearchResults sr = new SearchResults();
887 for (int pos : phrase)
889 mapping.markMappedRegion(seq, pos, sr);
891 newseq.append(sr.getCharacters());
895 // Hack: Copy sequence dataset, name and description from
896 // SearchResults.match[0].sequence
897 // TODO? carry over sequence names from original 'complement'
899 SequenceI mappedTo = sr.getResultSequence(0);
900 alignedSeq.setName(mappedTo.getName());
901 alignedSeq.setDescription(mappedTo.getDescription());
902 alignedSeq.setDatasetSequence(mappedTo);
909 alignedSeq.setSequence(newseq.toString());
914 * Realigns the given protein to match the alignment of the dna, using codon
915 * mappings to translate aligned codon positions to protein residues.
918 * the alignment whose sequences are realigned by this method
920 * the dna alignment whose alignment we are 'copying'
921 * @return the number of sequences that were realigned
923 public static int alignProteinAsDna(AlignmentI protein, AlignmentI dna)
925 List<SequenceI> unmappedProtein = new ArrayList<SequenceI>();
926 unmappedProtein.addAll(protein.getSequences());
928 List<AlignedCodonFrame> mappings = protein.getCodonFrames();
931 * Map will hold, for each aligned codon position e.g. [3, 5, 6], a map of
932 * {dnaSequence, {proteinSequence, codonProduct}} at that position. The
933 * comparator keeps the codon positions ordered.
935 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons = new TreeMap<AlignedCodon, Map<SequenceI, String>>(
936 new CodonComparator());
937 for (SequenceI dnaSeq : dna.getSequences())
939 for (AlignedCodonFrame mapping : mappings)
941 Mapping seqMap = mapping.getMappingForSequence(dnaSeq);
942 SequenceI prot = mapping.findAlignedSequence(
943 dnaSeq.getDatasetSequence(), protein);
946 addCodonPositions(dnaSeq, prot, protein.getGapCharacter(),
947 seqMap, alignedCodons);
948 unmappedProtein.remove(prot);
952 return alignProteinAs(protein, alignedCodons, unmappedProtein);
956 * Update the aligned protein sequences to match the codon alignments given in
960 * @param alignedCodons
961 * an ordered map of codon positions (columns), with sequence/peptide
962 * values present in each column
963 * @param unmappedProtein
966 protected static int alignProteinAs(AlignmentI protein,
967 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons,
968 List<SequenceI> unmappedProtein)
971 * Prefill aligned sequences with gaps before inserting aligned protein
974 int alignedWidth = alignedCodons.size();
975 char[] gaps = new char[alignedWidth];
976 Arrays.fill(gaps, protein.getGapCharacter());
977 String allGaps = String.valueOf(gaps);
978 for (SequenceI seq : protein.getSequences())
980 if (!unmappedProtein.contains(seq))
982 seq.setSequence(allGaps);
987 for (AlignedCodon codon : alignedCodons.keySet())
989 final Map<SequenceI, String> columnResidues = alignedCodons
991 for (Entry<SequenceI, String> entry : columnResidues.entrySet())
993 // place translated codon at its column position in sequence
994 entry.getKey().getSequence()[column] = entry.getValue().charAt(0);
1002 * Populate the map of aligned codons by traversing the given sequence
1003 * mapping, locating the aligned positions of mapped codons, and adding those
1004 * positions and their translation products to the map.
1007 * the aligned sequence we are mapping from
1009 * the sequence to be aligned to the codons
1011 * the gap character in the dna sequence
1013 * a mapping to a sequence translation
1014 * @param alignedCodons
1015 * the map we are building up
1017 static void addCodonPositions(SequenceI dna, SequenceI protein,
1018 char gapChar, Mapping seqMap,
1019 Map<AlignedCodon, Map<SequenceI, String>> alignedCodons)
1021 Iterator<AlignedCodon> codons = seqMap.getCodonIterator(dna, gapChar);
1022 while (codons.hasNext())
1024 AlignedCodon codon = codons.next();
1025 Map<SequenceI, String> seqProduct = alignedCodons.get(codon);
1026 if (seqProduct == null)
1028 seqProduct = new HashMap<SequenceI, String>();
1029 alignedCodons.put(codon, seqProduct);
1031 seqProduct.put(protein, codon.product);
1036 * Returns true if a cDNA/Protein mapping either exists, or could be made,
1037 * between at least one pair of sequences in the two alignments. Currently,
1040 * <li>One alignment must be nucleotide, and the other protein</li>
1041 * <li>At least one pair of sequences must be already mapped, or mappable</li>
1042 * <li>Mappable means the nucleotide translation matches the protein sequence</li>
1043 * <li>The translation may ignore start and stop codons if present in the
1051 public static boolean isMappable(AlignmentI al1, AlignmentI al2)
1053 if (al1 == null || al2 == null)
1059 * Require one nucleotide and one protein
1061 if (al1.isNucleotide() == al2.isNucleotide())
1065 AlignmentI dna = al1.isNucleotide() ? al1 : al2;
1066 AlignmentI protein = dna == al1 ? al2 : al1;
1067 List<AlignedCodonFrame> mappings = protein.getCodonFrames();
1068 for (SequenceI dnaSeq : dna.getSequences())
1070 for (SequenceI proteinSeq : protein.getSequences())
1072 if (isMappable(dnaSeq, proteinSeq, mappings))
1082 * Returns true if the dna sequence is mapped, or could be mapped, to the
1090 protected static boolean isMappable(SequenceI dnaSeq,
1091 SequenceI proteinSeq, List<AlignedCodonFrame> mappings)
1093 if (dnaSeq == null || proteinSeq == null)
1098 SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq
1099 .getDatasetSequence();
1100 SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq
1101 : proteinSeq.getDatasetSequence();
1103 for (AlignedCodonFrame mapping : mappings)
1105 if (proteinDs == mapping.getAaForDnaSeq(dnaDs))
1115 * Just try to make a mapping (it is not yet stored), test whether
1118 return mapProteinSequenceToCdna(proteinDs, dnaDs) != null;
1122 * Finds any reference annotations associated with the sequences in
1123 * sequenceScope, that are not already added to the alignment, and adds them
1124 * to the 'candidates' map. Also populates a lookup table of annotation
1125 * labels, keyed by calcId, for use in constructing tooltips or the like.
1127 * @param sequenceScope
1128 * the sequences to scan for reference annotations
1129 * @param labelForCalcId
1130 * (optional) map to populate with label for calcId
1132 * map to populate with annotations for sequence
1134 * the alignment to check for presence of annotations
1136 public static void findAddableReferenceAnnotations(
1137 List<SequenceI> sequenceScope,
1138 Map<String, String> labelForCalcId,
1139 final Map<SequenceI, List<AlignmentAnnotation>> candidates,
1142 if (sequenceScope == null)
1148 * For each sequence in scope, make a list of any annotations on the
1149 * underlying dataset sequence which are not already on the alignment.
1151 * Add to a map of { alignmentSequence, <List of annotations to add> }
1153 for (SequenceI seq : sequenceScope)
1155 SequenceI dataset = seq.getDatasetSequence();
1156 if (dataset == null)
1160 AlignmentAnnotation[] datasetAnnotations = dataset.getAnnotation();
1161 if (datasetAnnotations == null)
1165 final List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1166 for (AlignmentAnnotation dsann : datasetAnnotations)
1169 * Find matching annotations on the alignment. If none is found, then
1170 * add this annotation to the list of 'addable' annotations for this
1173 final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
1174 .findAnnotations(seq, dsann.getCalcId(), dsann.label);
1175 if (!matchedAlignmentAnnotations.iterator().hasNext())
1178 if (labelForCalcId != null)
1180 labelForCalcId.put(dsann.getCalcId(), dsann.label);
1185 * Save any addable annotations for this sequence
1187 if (!result.isEmpty())
1189 candidates.put(seq, result);
1195 * Adds annotations to the top of the alignment annotations, in the same order
1196 * as their related sequences.
1198 * @param annotations
1199 * the annotations to add
1201 * the alignment to add them to
1202 * @param selectionGroup
1203 * current selection group (or null if none)
1205 public static void addReferenceAnnotations(
1206 Map<SequenceI, List<AlignmentAnnotation>> annotations,
1207 final AlignmentI alignment, final SequenceGroup selectionGroup)
1209 for (SequenceI seq : annotations.keySet())
1211 for (AlignmentAnnotation ann : annotations.get(seq))
1213 AlignmentAnnotation copyAnn = new AlignmentAnnotation(ann);
1215 int endRes = ann.annotations.length;
1216 if (selectionGroup != null)
1218 startRes = selectionGroup.getStartRes();
1219 endRes = selectionGroup.getEndRes();
1221 copyAnn.restrict(startRes, endRes);
1224 * Add to the sequence (sets copyAnn.datasetSequence), unless the
1225 * original annotation is already on the sequence.
1227 if (!seq.hasAnnotation(ann))
1229 seq.addAlignmentAnnotation(copyAnn);
1232 copyAnn.adjustForAlignment();
1233 // add to the alignment and set visible
1234 alignment.addAnnotation(copyAnn);
1235 copyAnn.visible = true;
1241 * Set visibility of alignment annotations of specified types (labels), for
1242 * specified sequences. This supports controls like
1243 * "Show all secondary structure", "Hide all Temp factor", etc.
1245 * @al the alignment to scan for annotations
1247 * the types (labels) of annotations to be updated
1248 * @param forSequences
1249 * if not null, only annotations linked to one of these sequences are
1250 * in scope for update; if null, acts on all sequence annotations
1252 * if this flag is true, 'types' is ignored (label not checked)
1254 * if true, set visibility on, else set off
1256 public static void showOrHideSequenceAnnotations(AlignmentI al,
1257 Collection<String> types, List<SequenceI> forSequences,
1258 boolean anyType, boolean doShow)
1260 for (AlignmentAnnotation aa : al.getAlignmentAnnotation())
1262 if (anyType || types.contains(aa.label))
1264 if ((aa.sequenceRef != null)
1265 && (forSequences == null || forSequences
1266 .contains(aa.sequenceRef)))
1268 aa.visible = doShow;
1275 * Returns true if either sequence has a cross-reference to the other
1281 public static boolean haveCrossRef(SequenceI seq1, SequenceI seq2)
1283 // Note: moved here from class CrossRef as the latter class has dependencies
1284 // not availability to the applet's classpath
1285 return hasCrossRef(seq1, seq2) || hasCrossRef(seq2, seq1);
1289 * Returns true if seq1 has a cross-reference to seq2. Currently this assumes
1290 * that sequence name is structured as Source|AccessionId.
1296 public static boolean hasCrossRef(SequenceI seq1, SequenceI seq2)
1298 if (seq1 == null || seq2 == null)
1302 String name = seq2.getName();
1303 final DBRefEntry[] xrefs = seq1.getDBRef();
1306 for (DBRefEntry xref : xrefs)
1308 String xrefName = xref.getSource() + "|" + xref.getAccessionId();
1309 // case-insensitive test, consistent with DBRefEntry.equalRef()
1310 if (xrefName.equalsIgnoreCase(name))
1320 * Constructs an alignment consisting of the mapped exon regions in the given
1321 * nucleotide sequences, and updates mappings to match.
1324 * aligned dna sequences
1326 * from dna to protein; these are replaced with new mappings
1327 * @return an alignment whose sequences are the exon-only parts of the dna
1328 * sequences (or null if no exons are found)
1330 public static AlignmentI makeExonAlignment(SequenceI[] dna,
1331 List<AlignedCodonFrame> mappings)
1333 List<AlignedCodonFrame> newMappings = new ArrayList<AlignedCodonFrame>();
1334 List<SequenceI> exonSequences = new ArrayList<SequenceI>();
1336 for (SequenceI dnaSeq : dna)
1338 final SequenceI ds = dnaSeq.getDatasetSequence();
1339 List<AlignedCodonFrame> seqMappings = MappingUtils
1340 .findMappingsForSequence(ds, mappings);
1341 for (AlignedCodonFrame acf : seqMappings)
1343 AlignedCodonFrame newMapping = new AlignedCodonFrame();
1344 final List<SequenceI> mappedExons = makeExonSequences(ds, acf,
1346 if (!mappedExons.isEmpty())
1348 exonSequences.addAll(mappedExons);
1349 newMappings.add(newMapping);
1353 AlignmentI al = new Alignment(
1354 exonSequences.toArray(new SequenceI[exonSequences.size()]));
1355 al.setDataset(null);
1358 * Replace the old mappings with the new ones
1361 mappings.addAll(newMappings);
1367 * Helper method to make exon-only sequences and populate their mappings to
1370 * For example, if ggCCaTTcGAg has mappings [3, 4, 6, 7, 9, 10] to protein
1371 * then generate a sequence CCTTGA with mapping [1, 6] to the same protein
1374 * Typically eukaryotic dna will include exons encoding for a single peptide
1375 * sequence i.e. return a single result. Bacterial dna may have overlapping
1376 * exon mappings coding for multiple peptides so return multiple results
1377 * (example EMBL KF591215).
1380 * a dna dataset sequence
1382 * containing one or more mappings of the sequence to protein
1384 * the new mapping to populate, from the exon-only sequences to their
1385 * mapped protein sequences
1388 protected static List<SequenceI> makeExonSequences(SequenceI dnaSeq,
1389 AlignedCodonFrame mapping, AlignedCodonFrame newMapping)
1391 List<SequenceI> exonSequences = new ArrayList<SequenceI>();
1392 List<Mapping> seqMappings = mapping.getMappingsForSequence(dnaSeq);
1393 final char[] dna = dnaSeq.getSequence();
1394 for (Mapping seqMapping : seqMappings)
1396 StringBuilder newSequence = new StringBuilder(dnaSeq.getLength());
1399 * Get the codon regions as { [2, 5], [7, 12], [14, 14] etc }
1401 final List<int[]> dnaExonRanges = seqMapping.getMap().getFromRanges();
1402 for (int[] range : dnaExonRanges)
1404 for (int pos = range[0]; pos <= range[1]; pos++)
1406 newSequence.append(dna[pos - 1]);
1410 SequenceI exon = new Sequence(dnaSeq.getName(),
1411 newSequence.toString());
1414 * Locate any xrefs to CDS database on the protein product and attach to
1415 * the CDS sequence. Also add as a sub-token of the sequence name.
1417 // default to "CDS" if we can't locate an actual gene id
1418 String cdsAccId = FeatureProperties
1419 .getCodingFeature(DBRefSource.EMBL);
1420 DBRefEntry[] cdsRefs = DBRefUtils.selectRefs(seqMapping.getTo()
1421 .getDBRef(), DBRefSource.CODINGDBS);
1422 if (cdsRefs != null)
1424 for (DBRefEntry cdsRef : cdsRefs)
1426 exon.addDBRef(new DBRefEntry(cdsRef));
1427 cdsAccId = cdsRef.getAccessionId();
1430 exon.setName(exon.getName() + "|" + cdsAccId);
1431 exon.createDatasetSequence();
1434 * Build new mappings - from the same protein regions, but now to
1437 List<int[]> exonRange = new ArrayList<int[]>();
1438 exonRange.add(new int[] { 1, newSequence.length() });
1439 MapList map = new MapList(exonRange, seqMapping.getMap()
1440 .getToRanges(), 3, 1);
1441 newMapping.addMap(exon.getDatasetSequence(), seqMapping.getTo(), map);
1442 MapList cdsToDnaMap = new MapList(dnaExonRanges, exonRange, 1, 1);
1443 newMapping.addMap(dnaSeq, exon.getDatasetSequence(), cdsToDnaMap);
1445 exonSequences.add(exon);
1447 return exonSequences;