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 static jalview.io.gff.GffConstants.CLINICAL_SIGNIFICANCE;
25 import jalview.api.DBRefEntryI;
26 import jalview.datamodel.AlignedCodon;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.DBRefEntry;
33 import jalview.datamodel.IncompleteCodonException;
34 import jalview.datamodel.Mapping;
35 import jalview.datamodel.Sequence;
36 import jalview.datamodel.SequenceFeature;
37 import jalview.datamodel.SequenceGroup;
38 import jalview.datamodel.SequenceI;
39 import jalview.io.gff.SequenceOntologyFactory;
40 import jalview.io.gff.SequenceOntologyI;
41 import jalview.schemes.ResidueProperties;
42 import jalview.util.Comparison;
43 import jalview.util.DBRefUtils;
44 import jalview.util.MapList;
45 import jalview.util.MappingUtils;
46 import jalview.util.StringUtils;
48 import java.io.UnsupportedEncodingException;
49 import java.net.URLEncoder;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.Collection;
53 import java.util.Collections;
54 import java.util.Comparator;
55 import java.util.HashMap;
56 import java.util.HashSet;
57 import java.util.Iterator;
58 import java.util.LinkedHashMap;
59 import java.util.List;
61 import java.util.Map.Entry;
62 import java.util.NoSuchElementException;
64 import java.util.TreeMap;
67 * grab bag of useful alignment manipulation operations Expect these to be
68 * refactored elsewhere at some point.
73 public class AlignmentUtils
76 private static final String SEQUENCE_VARIANT = "sequence_variant:";
77 private static final String ID = "ID";
80 * A data model to hold the 'normal' base value at a position, and an optional
81 * sequence variant feature
83 static class DnaVariant
87 SequenceFeature variant;
89 DnaVariant(String nuc)
94 DnaVariant(String nuc, SequenceFeature var)
102 * given an existing alignment, create a new alignment including all, or up to
103 * flankSize additional symbols from each sequence's dataset sequence
109 public static AlignmentI expandContext(AlignmentI core, int flankSize)
111 List<SequenceI> sq = new ArrayList<SequenceI>();
113 for (SequenceI s : core.getSequences())
115 SequenceI newSeq = s.deriveSequence();
116 final int newSeqStart = newSeq.getStart() - 1;
117 if (newSeqStart > maxoffset
118 && newSeq.getDatasetSequence().getStart() < s.getStart())
120 maxoffset = newSeqStart;
126 maxoffset = Math.min(maxoffset, flankSize);
130 * now add offset left and right to create an expanded alignment
132 for (SequenceI s : sq)
135 while (ds.getDatasetSequence() != null)
137 ds = ds.getDatasetSequence();
139 int s_end = s.findPosition(s.getStart() + s.getLength());
140 // find available flanking residues for sequence
141 int ustream_ds = s.getStart() - ds.getStart();
142 int dstream_ds = ds.getEnd() - s_end;
144 // build new flanked sequence
146 // compute gap padding to start of flanking sequence
147 int offset = maxoffset - ustream_ds;
149 // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
152 if (flankSize < ustream_ds)
154 // take up to flankSize residues
155 offset = maxoffset - flankSize;
156 ustream_ds = flankSize;
158 if (flankSize <= dstream_ds)
160 dstream_ds = flankSize - 1;
163 // TODO use Character.toLowerCase to avoid creating String objects?
164 char[] upstream = new String(ds.getSequence(s.getStart() - 1
165 - ustream_ds, s.getStart() - 1)).toLowerCase().toCharArray();
166 char[] downstream = new String(ds.getSequence(s_end - 1, s_end
167 + dstream_ds)).toLowerCase().toCharArray();
168 char[] coreseq = s.getSequence();
169 char[] nseq = new char[offset + upstream.length + downstream.length
171 char c = core.getGapCharacter();
174 for (; p < offset; p++)
179 System.arraycopy(upstream, 0, nseq, p, upstream.length);
180 System.arraycopy(coreseq, 0, nseq, p + upstream.length,
182 System.arraycopy(downstream, 0, nseq, p + coreseq.length
183 + upstream.length, downstream.length);
184 s.setSequence(new String(nseq));
185 s.setStart(s.getStart() - ustream_ds);
186 s.setEnd(s_end + downstream.length);
188 AlignmentI newAl = new jalview.datamodel.Alignment(
189 sq.toArray(new SequenceI[0]));
190 for (SequenceI s : sq)
192 if (s.getAnnotation() != null)
194 for (AlignmentAnnotation aa : s.getAnnotation())
196 aa.adjustForAlignment(); // JAL-1712 fix
197 newAl.addAnnotation(aa);
201 newAl.setDataset(core.getDataset());
206 * Returns the index (zero-based position) of a sequence in an alignment, or
213 public static int getSequenceIndex(AlignmentI al, SequenceI seq)
217 for (SequenceI alSeq : al.getSequences())
230 * Returns a map of lists of sequences in the alignment, keyed by sequence
231 * name. For use in mapping between different alignment views of the same
234 * @see jalview.datamodel.AlignmentI#getSequencesByName()
236 public static Map<String, List<SequenceI>> getSequencesByName(
239 Map<String, List<SequenceI>> theMap = new LinkedHashMap<String, List<SequenceI>>();
240 for (SequenceI seq : al.getSequences())
242 String name = seq.getName();
245 List<SequenceI> seqs = theMap.get(name);
248 seqs = new ArrayList<SequenceI>();
249 theMap.put(name, seqs);
258 * Build mapping of protein to cDNA alignment. Mappings are made between
259 * sequences where the cDNA translates to the protein sequence. Any new
260 * mappings are added to the protein alignment. Returns true if any mappings
261 * either already exist or were added, else false.
263 * @param proteinAlignment
264 * @param cdnaAlignment
267 public static boolean mapProteinAlignmentToCdna(
268 final AlignmentI proteinAlignment, final AlignmentI cdnaAlignment)
270 if (proteinAlignment == null || cdnaAlignment == null)
275 Set<SequenceI> mappedDna = new HashSet<SequenceI>();
276 Set<SequenceI> mappedProtein = new HashSet<SequenceI>();
279 * First pass - map sequences where cross-references exist. This include
280 * 1-to-many mappings to support, for example, variant cDNA.
282 boolean mappingPerformed = mapProteinToCdna(proteinAlignment,
283 cdnaAlignment, mappedDna, mappedProtein, true);
286 * Second pass - map sequences where no cross-references exist. This only
287 * does 1-to-1 mappings and assumes corresponding sequences are in the same
288 * order in the alignments.
290 mappingPerformed |= mapProteinToCdna(proteinAlignment, cdnaAlignment,
291 mappedDna, mappedProtein, false);
292 return mappingPerformed;
296 * Make mappings between compatible sequences (where the cDNA translation
297 * matches the protein).
299 * @param proteinAlignment
300 * @param cdnaAlignment
302 * a set of mapped DNA sequences (to add to)
303 * @param mappedProtein
304 * a set of mapped Protein sequences (to add to)
306 * if true, only map sequences where xrefs exist
309 protected static boolean mapProteinToCdna(
310 final AlignmentI proteinAlignment,
311 final AlignmentI cdnaAlignment, Set<SequenceI> mappedDna,
312 Set<SequenceI> mappedProtein, boolean xrefsOnly)
314 boolean mappingExistsOrAdded = false;
315 List<SequenceI> thisSeqs = proteinAlignment.getSequences();
316 for (SequenceI aaSeq : thisSeqs)
318 boolean proteinMapped = false;
319 AlignedCodonFrame acf = new AlignedCodonFrame();
321 for (SequenceI cdnaSeq : cdnaAlignment.getSequences())
324 * Always try to map if sequences have xref to each other; this supports
325 * variant cDNA or alternative splicing for a protein sequence.
327 * If no xrefs, try to map progressively, assuming that alignments have
328 * mappable sequences in corresponding order. These are not
329 * many-to-many, as that would risk mixing species with similar cDNA
332 if (xrefsOnly && !AlignmentUtils.haveCrossRef(aaSeq, cdnaSeq))
338 * Don't map non-xrefd sequences more than once each. This heuristic
339 * allows us to pair up similar sequences in ordered alignments.
342 && (mappedProtein.contains(aaSeq) || mappedDna
347 if (mappingExists(proteinAlignment.getCodonFrames(),
348 aaSeq.getDatasetSequence(), cdnaSeq.getDatasetSequence()))
350 mappingExistsOrAdded = true;
354 MapList map = mapCdnaToProtein(aaSeq, cdnaSeq);
357 acf.addMap(cdnaSeq, aaSeq, map);
358 mappingExistsOrAdded = true;
359 proteinMapped = true;
360 mappedDna.add(cdnaSeq);
361 mappedProtein.add(aaSeq);
367 proteinAlignment.addCodonFrame(acf);
370 return mappingExistsOrAdded;
374 * Answers true if the mappings include one between the given (dataset)
377 public static boolean mappingExists(List<AlignedCodonFrame> mappings,
378 SequenceI aaSeq, SequenceI cdnaSeq)
380 if (mappings != null)
382 for (AlignedCodonFrame acf : mappings)
384 if (cdnaSeq == acf.getDnaForAaSeq(aaSeq))
394 * Builds a mapping (if possible) of a cDNA to a protein sequence.
396 * <li>first checks if the cdna translates exactly to the protein sequence</li>
397 * <li>else checks for translation after removing a STOP codon</li>
398 * <li>else checks for translation after removing a START codon</li>
399 * <li>if that fails, inspect CDS features on the cDNA sequence</li>
401 * Returns null if no mapping is determined.
404 * the aligned protein sequence
406 * the aligned cdna sequence
409 public static MapList mapCdnaToProtein(SequenceI proteinSeq,
413 * Here we handle either dataset sequence set (desktop) or absent (applet).
414 * Use only the char[] form of the sequence to avoid creating possibly large
417 final SequenceI proteinDataset = proteinSeq.getDatasetSequence();
418 char[] aaSeqChars = proteinDataset != null ? proteinDataset
419 .getSequence() : proteinSeq.getSequence();
420 final SequenceI cdnaDataset = cdnaSeq.getDatasetSequence();
421 char[] cdnaSeqChars = cdnaDataset != null ? cdnaDataset.getSequence()
422 : cdnaSeq.getSequence();
423 if (aaSeqChars == null || cdnaSeqChars == null)
429 * cdnaStart/End, proteinStartEnd are base 1 (for dataset sequence mapping)
431 final int mappedLength = 3 * aaSeqChars.length;
432 int cdnaLength = cdnaSeqChars.length;
433 int cdnaStart = cdnaSeq.getStart();
434 int cdnaEnd = cdnaSeq.getEnd();
435 final int proteinStart = proteinSeq.getStart();
436 final int proteinEnd = proteinSeq.getEnd();
439 * If lengths don't match, try ignoring stop codon (if present)
441 if (cdnaLength != mappedLength && cdnaLength > 2)
443 String lastCodon = String.valueOf(cdnaSeqChars, cdnaLength - 3, 3)
445 for (String stop : ResidueProperties.STOP)
447 if (lastCodon.equals(stop))
457 * If lengths still don't match, try ignoring start codon.
460 if (cdnaLength != mappedLength
462 && String.valueOf(cdnaSeqChars, 0, 3).toUpperCase()
463 .equals(ResidueProperties.START))
470 if (translatesAs(cdnaSeqChars, startOffset, aaSeqChars))
473 * protein is translation of dna (+/- start/stop codons)
475 MapList map = new MapList(new int[] { cdnaStart, cdnaEnd }, new int[]
476 { proteinStart, proteinEnd }, 3, 1);
481 * translation failed - try mapping CDS annotated regions of dna
483 return mapCdsToProtein(cdnaSeq, proteinSeq);
487 * Test whether the given cdna sequence, starting at the given offset,
488 * translates to the given amino acid sequence, using the standard translation
489 * table. Designed to fail fast i.e. as soon as a mismatch position is found.
491 * @param cdnaSeqChars
496 protected static boolean translatesAs(char[] cdnaSeqChars, int cdnaStart,
499 if (cdnaSeqChars == null || aaSeqChars == null)
505 int dnaPos = cdnaStart;
506 for (; dnaPos < cdnaSeqChars.length - 2
507 && aaPos < aaSeqChars.length; dnaPos += 3, aaPos++)
509 String codon = String.valueOf(cdnaSeqChars, dnaPos, 3);
510 final String translated = ResidueProperties.codonTranslate(codon);
513 * allow * in protein to match untranslatable in dna
515 final char aaRes = aaSeqChars[aaPos];
516 if ((translated == null || "STOP".equals(translated)) && aaRes == '*')
520 if (translated == null || !(aaRes == translated.charAt(0)))
523 // System.out.println(("Mismatch at " + i + "/" + aaResidue + ": "
524 // + codon + "(" + translated + ") != " + aaRes));
530 * check we matched all of the protein sequence
532 if (aaPos != aaSeqChars.length)
538 * check we matched all of the dna except
539 * for optional trailing STOP codon
541 if (dnaPos == cdnaSeqChars.length)
545 if (dnaPos == cdnaSeqChars.length - 3)
547 String codon = String.valueOf(cdnaSeqChars, dnaPos, 3);
548 if ("STOP".equals(ResidueProperties.codonTranslate(codon)))
557 * Align sequence 'seq' to match the alignment of a mapped sequence. Note this
558 * currently assumes that we are aligning cDNA to match protein.
561 * the sequence to be realigned
563 * the alignment whose sequence alignment is to be 'copied'
565 * character string represent a gap in the realigned sequence
566 * @param preserveUnmappedGaps
567 * @param preserveMappedGaps
568 * @return true if the sequence was realigned, false if it could not be
570 public static boolean alignSequenceAs(SequenceI seq, AlignmentI al,
571 String gap, boolean preserveMappedGaps,
572 boolean preserveUnmappedGaps)
575 * Get any mappings from the source alignment to the target (dataset)
578 // TODO there may be one AlignedCodonFrame per dataset sequence, or one with
579 // all mappings. Would it help to constrain this?
580 List<AlignedCodonFrame> mappings = al.getCodonFrame(seq);
581 if (mappings == null || mappings.isEmpty())
587 * Locate the aligned source sequence whose dataset sequence is mapped. We
588 * just take the first match here (as we can't align like more than one
591 SequenceI alignFrom = null;
592 AlignedCodonFrame mapping = null;
593 for (AlignedCodonFrame mp : mappings)
595 alignFrom = mp.findAlignedSequence(seq, al);
596 if (alignFrom != null)
603 if (alignFrom == null)
607 alignSequenceAs(seq, alignFrom, mapping, gap, al.getGapCharacter(),
608 preserveMappedGaps, preserveUnmappedGaps);
613 * Align sequence 'alignTo' the same way as 'alignFrom', using the mapping to
614 * match residues and codons. Flags control whether existing gaps in unmapped
615 * (intron) and mapped (exon) regions are preserved or not. Gaps between
616 * intron and exon are only retained if both flags are set.
623 * @param preserveUnmappedGaps
624 * @param preserveMappedGaps
626 public static void alignSequenceAs(SequenceI alignTo,
627 SequenceI alignFrom, AlignedCodonFrame mapping, String myGap,
628 char sourceGap, boolean preserveMappedGaps,
629 boolean preserveUnmappedGaps)
631 // TODO generalise to work for Protein-Protein, dna-dna, dna-protein
633 // aligned and dataset sequence positions, all base zero
637 int basesWritten = 0;
638 char myGapChar = myGap.charAt(0);
639 int ratio = myGap.length();
641 int fromOffset = alignFrom.getStart() - 1;
642 int toOffset = alignTo.getStart() - 1;
643 int sourceGapMappedLength = 0;
644 boolean inExon = false;
645 final char[] thisSeq = alignTo.getSequence();
646 final char[] thatAligned = alignFrom.getSequence();
647 StringBuilder thisAligned = new StringBuilder(2 * thisSeq.length);
650 * Traverse the 'model' aligned sequence
652 for (char sourceChar : thatAligned)
654 if (sourceChar == sourceGap)
656 sourceGapMappedLength += ratio;
661 * Found a non-gap character. Locate its mapped region if any.
664 // Note mapping positions are base 1, our sequence positions base 0
665 int[] mappedPos = mapping.getMappedRegion(alignTo, alignFrom,
666 sourceDsPos + fromOffset);
667 if (mappedPos == null)
670 * unmapped position; treat like a gap
672 sourceGapMappedLength += ratio;
673 // System.err.println("Can't align: no codon mapping to residue "
674 // + sourceDsPos + "(" + sourceChar + ")");
679 int mappedCodonStart = mappedPos[0]; // position (1...) of codon start
680 int mappedCodonEnd = mappedPos[mappedPos.length - 1]; // codon end pos
681 StringBuilder trailingCopiedGap = new StringBuilder();
684 * Copy dna sequence up to and including this codon. Optionally, include
685 * gaps before the codon starts (in introns) and/or after the codon starts
688 * Note this only works for 'linear' splicing, not reverse or interleaved.
689 * But then 'align dna as protein' doesn't make much sense otherwise.
691 int intronLength = 0;
692 while (basesWritten + toOffset < mappedCodonEnd
693 && thisSeqPos < thisSeq.length)
695 final char c = thisSeq[thisSeqPos++];
699 int sourcePosition = basesWritten + toOffset;
700 if (sourcePosition < mappedCodonStart)
703 * Found an unmapped (intron) base. First add in any preceding gaps
706 if (preserveUnmappedGaps && trailingCopiedGap.length() > 0)
708 thisAligned.append(trailingCopiedGap.toString());
709 intronLength += trailingCopiedGap.length();
710 trailingCopiedGap = new StringBuilder();
717 final boolean startOfCodon = sourcePosition == mappedCodonStart;
718 int gapsToAdd = calculateGapsToInsert(preserveMappedGaps,
719 preserveUnmappedGaps, sourceGapMappedLength, inExon,
720 trailingCopiedGap.length(), intronLength, startOfCodon);
721 for (int i = 0; i < gapsToAdd; i++)
723 thisAligned.append(myGapChar);
725 sourceGapMappedLength = 0;
728 thisAligned.append(c);
729 trailingCopiedGap = new StringBuilder();
733 if (inExon && preserveMappedGaps)
735 trailingCopiedGap.append(myGapChar);
737 else if (!inExon && preserveUnmappedGaps)
739 trailingCopiedGap.append(myGapChar);
746 * At end of model aligned sequence. Copy any remaining target sequence, optionally
747 * including (intron) gaps.
749 while (thisSeqPos < thisSeq.length)
751 final char c = thisSeq[thisSeqPos++];
752 if (c != myGapChar || preserveUnmappedGaps)
754 thisAligned.append(c);
756 sourceGapMappedLength--;
760 * finally add gaps to pad for any trailing source gaps or
761 * unmapped characters
763 if (preserveUnmappedGaps)
765 while (sourceGapMappedLength > 0)
767 thisAligned.append(myGapChar);
768 sourceGapMappedLength--;
773 * All done aligning, set the aligned sequence.
775 alignTo.setSequence(new String(thisAligned));
779 * Helper method to work out how many gaps to insert when realigning.
781 * @param preserveMappedGaps
782 * @param preserveUnmappedGaps
783 * @param sourceGapMappedLength
785 * @param trailingCopiedGap
786 * @param intronLength
787 * @param startOfCodon
790 protected static int calculateGapsToInsert(boolean preserveMappedGaps,
791 boolean preserveUnmappedGaps, int sourceGapMappedLength,
792 boolean inExon, int trailingGapLength, int intronLength,
793 final boolean startOfCodon)
799 * Reached start of codon. Ignore trailing gaps in intron unless we are
800 * preserving gaps in both exon and intron. Ignore them anyway if the
801 * protein alignment introduces a gap at least as large as the intronic
804 if (inExon && !preserveMappedGaps)
806 trailingGapLength = 0;
808 if (!inExon && !(preserveMappedGaps && preserveUnmappedGaps))
810 trailingGapLength = 0;
814 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
818 if (intronLength + trailingGapLength <= sourceGapMappedLength)
820 gapsToAdd = sourceGapMappedLength - intronLength;
824 gapsToAdd = Math.min(intronLength + trailingGapLength
825 - sourceGapMappedLength, trailingGapLength);
832 * second or third base of codon; check for any gaps in dna
834 if (!preserveMappedGaps)
836 trailingGapLength = 0;
838 gapsToAdd = Math.max(sourceGapMappedLength, trailingGapLength);
844 * Realigns the given protein to match the alignment of the dna, using codon
845 * mappings to translate aligned codon positions to protein residues.
848 * the alignment whose sequences are realigned by this method
850 * the dna alignment whose alignment we are 'copying'
851 * @return the number of sequences that were realigned
853 public static int alignProteinAsDna(AlignmentI protein, AlignmentI dna)
855 if (protein.isNucleotide() || !dna.isNucleotide())
857 System.err.println("Wrong alignment type in alignProteinAsDna");
860 List<SequenceI> unmappedProtein = new ArrayList<SequenceI>();
861 Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons = buildCodonColumnsMap(
862 protein, dna, unmappedProtein);
863 return alignProteinAs(protein, alignedCodons, unmappedProtein);
867 * Realigns the given dna to match the alignment of the protein, using codon
868 * mappings to translate aligned peptide positions to codons.
871 * the alignment whose sequences are realigned by this method
873 * the protein alignment whose alignment we are 'copying'
874 * @return the number of sequences that were realigned
876 public static int alignCdsAsProtein(AlignmentI dna, AlignmentI protein)
878 if (protein.isNucleotide() || !dna.isNucleotide())
880 System.err.println("Wrong alignment type in alignProteinAsDna");
883 // todo: implement this
884 List<AlignedCodonFrame> mappings = protein.getCodonFrames();
885 int alignedCount = 0;
886 for (SequenceI dnaSeq : dna.getSequences())
888 if (alignCdsSequenceAsProtein(dnaSeq, protein, mappings,
889 dna.getGapCharacter()))
898 * Helper method to align (if possible) the dna sequence to match the
899 * alignment of a mapped protein sequence. This is currently limited to
900 * handling coding sequence only.
908 static boolean alignCdsSequenceAsProtein(SequenceI cdsSeq,
909 AlignmentI protein, List<AlignedCodonFrame> mappings, char gapChar)
911 SequenceI cdsDss = cdsSeq.getDatasetSequence();
915 .println("alignCdsSequenceAsProtein needs aligned sequence!");
919 List<AlignedCodonFrame> dnaMappings = MappingUtils
920 .findMappingsForSequence(cdsSeq, mappings);
921 for (AlignedCodonFrame mapping : dnaMappings)
923 SequenceI peptide = mapping.findAlignedSequence(cdsSeq, protein);
924 int peptideLength = peptide.getLength();
927 Mapping map = mapping.getMappingBetween(cdsSeq, peptide);
930 MapList mapList = map.getMap();
931 if (map.getTo() == peptide.getDatasetSequence())
933 mapList = mapList.getInverse();
935 int cdsLength = cdsDss.getLength();
936 int mappedFromLength = MappingUtils.getLength(mapList
938 int mappedToLength = MappingUtils
939 .getLength(mapList.getToRanges());
940 boolean addStopCodon = (cdsLength == mappedFromLength * 3 + 3)
941 || (peptide.getDatasetSequence().getLength() == mappedFromLength - 1);
942 if (cdsLength != mappedToLength && !addStopCodon)
946 .format("Can't align cds as protein (length mismatch %d/%d): %s",
947 cdsLength, mappedToLength,
952 * pre-fill the aligned cds sequence with gaps
954 char[] alignedCds = new char[peptideLength * 3
955 + (addStopCodon ? 3 : 0)];
956 Arrays.fill(alignedCds, gapChar);
959 * walk over the aligned peptide sequence and insert mapped
960 * codons for residues in the aligned cds sequence
962 char[] alignedPeptide = peptide.getSequence();
963 char[] nucleotides = cdsDss.getSequence();
965 int cdsStart = cdsDss.getStart();
966 int proteinPos = peptide.getStart() - 1;
968 for (char residue : alignedPeptide)
970 if (Comparison.isGap(residue))
977 int[] codon = mapList.locateInTo(proteinPos, proteinPos);
980 // e.g. incomplete start codon, X in peptide
985 for (int j = codon[0]; j <= codon[1]; j++)
987 char mappedBase = nucleotides[j - cdsStart];
988 alignedCds[cdsCol++] = mappedBase;
996 * append stop codon if not mapped from protein,
997 * closing it up to the end of the mapped sequence
999 if (copiedBases == nucleotides.length - 3)
1001 for (int i = alignedCds.length - 1; i >= 0; i--)
1003 if (!Comparison.isGap(alignedCds[i]))
1005 cdsCol = i + 1; // gap just after end of sequence
1009 for (int i = nucleotides.length - 3; i < nucleotides.length; i++)
1011 alignedCds[cdsCol++] = nucleotides[i];
1014 cdsSeq.setSequence(new String(alignedCds));
1023 * Builds a map whose key is an aligned codon position (3 alignment column
1024 * numbers base 0), and whose value is a map from protein sequence to each
1025 * protein's peptide residue for that codon. The map generates an ordering of
1026 * the codons, and allows us to read off the peptides at each position in
1027 * order to assemble 'aligned' protein sequences.
1030 * the protein alignment
1032 * the coding dna alignment
1033 * @param unmappedProtein
1034 * any unmapped proteins are added to this list
1037 protected static Map<AlignedCodon, Map<SequenceI, AlignedCodon>> buildCodonColumnsMap(
1038 AlignmentI protein, AlignmentI dna,
1039 List<SequenceI> unmappedProtein)
1042 * maintain a list of any proteins with no mappings - these will be
1043 * rendered 'as is' in the protein alignment as we can't align them
1045 unmappedProtein.addAll(protein.getSequences());
1047 List<AlignedCodonFrame> mappings = protein.getCodonFrames();
1050 * Map will hold, for each aligned codon position e.g. [3, 5, 6], a map of
1051 * {dnaSequence, {proteinSequence, codonProduct}} at that position. The
1052 * comparator keeps the codon positions ordered.
1054 Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons = new TreeMap<AlignedCodon, Map<SequenceI, AlignedCodon>>(
1055 new CodonComparator());
1057 for (SequenceI dnaSeq : dna.getSequences())
1059 for (AlignedCodonFrame mapping : mappings)
1061 SequenceI prot = mapping.findAlignedSequence(dnaSeq, protein);
1064 Mapping seqMap = mapping.getMappingForSequence(dnaSeq);
1065 addCodonPositions(dnaSeq, prot, protein.getGapCharacter(),
1066 seqMap, alignedCodons);
1067 unmappedProtein.remove(prot);
1073 * Finally add any unmapped peptide start residues (e.g. for incomplete
1074 * codons) as if at the codon position before the second residue
1076 // TODO resolve JAL-2022 so this fudge can be removed
1077 int mappedSequenceCount = protein.getHeight() - unmappedProtein.size();
1078 addUnmappedPeptideStarts(alignedCodons, mappedSequenceCount);
1080 return alignedCodons;
1084 * Scans for any protein mapped from position 2 (meaning unmapped start
1085 * position e.g. an incomplete codon), and synthesizes a 'codon' for it at the
1086 * preceding position in the alignment
1088 * @param alignedCodons
1089 * the codon-to-peptide map
1090 * @param mappedSequenceCount
1091 * the number of distinct sequences in the map
1093 protected static void addUnmappedPeptideStarts(
1094 Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons,
1095 int mappedSequenceCount)
1097 // TODO delete this ugly hack once JAL-2022 is resolved
1098 // i.e. we can model startPhase > 0 (incomplete start codon)
1100 List<SequenceI> sequencesChecked = new ArrayList<SequenceI>();
1101 AlignedCodon lastCodon = null;
1102 Map<SequenceI, AlignedCodon> toAdd = new HashMap<SequenceI, AlignedCodon>();
1104 for (Entry<AlignedCodon, Map<SequenceI, AlignedCodon>> entry : alignedCodons
1107 for (Entry<SequenceI, AlignedCodon> sequenceCodon : entry.getValue()
1110 SequenceI seq = sequenceCodon.getKey();
1111 if (sequencesChecked.contains(seq))
1115 sequencesChecked.add(seq);
1116 AlignedCodon codon = sequenceCodon.getValue();
1117 if (codon.peptideCol > 1)
1120 .println("Problem mapping protein with >1 unmapped start positions: "
1123 else if (codon.peptideCol == 1)
1126 * first position (peptideCol == 0) was unmapped - add it
1128 if (lastCodon != null)
1130 AlignedCodon firstPeptide = new AlignedCodon(lastCodon.pos1,
1131 lastCodon.pos2, lastCodon.pos3, String.valueOf(seq
1133 toAdd.put(seq, firstPeptide);
1138 * unmapped residue at start of alignment (no prior column) -
1139 * 'insert' at nominal codon [0, 0, 0]
1141 AlignedCodon firstPeptide = new AlignedCodon(0, 0, 0,
1142 String.valueOf(seq.getCharAt(0)), 0);
1143 toAdd.put(seq, firstPeptide);
1146 if (sequencesChecked.size() == mappedSequenceCount)
1148 // no need to check past first mapped position in all sequences
1152 lastCodon = entry.getKey();
1156 * add any new codons safely after iterating over the map
1158 for (Entry<SequenceI, AlignedCodon> startCodon : toAdd.entrySet())
1160 addCodonToMap(alignedCodons, startCodon.getValue(),
1161 startCodon.getKey());
1166 * Update the aligned protein sequences to match the codon alignments given in
1170 * @param alignedCodons
1171 * an ordered map of codon positions (columns), with sequence/peptide
1172 * values present in each column
1173 * @param unmappedProtein
1176 protected static int alignProteinAs(AlignmentI protein,
1177 Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons,
1178 List<SequenceI> unmappedProtein)
1181 * Prefill aligned sequences with gaps before inserting aligned protein
1184 int alignedWidth = alignedCodons.size();
1185 char[] gaps = new char[alignedWidth];
1186 Arrays.fill(gaps, protein.getGapCharacter());
1187 String allGaps = String.valueOf(gaps);
1188 for (SequenceI seq : protein.getSequences())
1190 if (!unmappedProtein.contains(seq))
1192 seq.setSequence(allGaps);
1197 for (AlignedCodon codon : alignedCodons.keySet())
1199 final Map<SequenceI, AlignedCodon> columnResidues = alignedCodons
1201 for (Entry<SequenceI, AlignedCodon> entry : columnResidues.entrySet())
1203 // place translated codon at its column position in sequence
1204 entry.getKey().getSequence()[column] = entry.getValue().product
1213 * Populate the map of aligned codons by traversing the given sequence
1214 * mapping, locating the aligned positions of mapped codons, and adding those
1215 * positions and their translation products to the map.
1218 * the aligned sequence we are mapping from
1220 * the sequence to be aligned to the codons
1222 * the gap character in the dna sequence
1224 * a mapping to a sequence translation
1225 * @param alignedCodons
1226 * the map we are building up
1228 static void addCodonPositions(SequenceI dna, SequenceI protein,
1229 char gapChar, Mapping seqMap,
1230 Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons)
1232 Iterator<AlignedCodon> codons = seqMap.getCodonIterator(dna, gapChar);
1235 * add codon positions, and their peptide translations, to the alignment
1236 * map, while remembering the first codon mapped
1238 while (codons.hasNext())
1242 AlignedCodon codon = codons.next();
1243 addCodonToMap(alignedCodons, codon, protein);
1244 } catch (IncompleteCodonException e)
1246 // possible incomplete trailing codon - ignore
1247 } catch (NoSuchElementException e)
1249 // possibly peptide lacking STOP
1255 * Helper method to add a codon-to-peptide entry to the aligned codons map
1257 * @param alignedCodons
1261 protected static void addCodonToMap(
1262 Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons,
1263 AlignedCodon codon, SequenceI protein)
1265 Map<SequenceI, AlignedCodon> seqProduct = alignedCodons.get(codon);
1266 if (seqProduct == null)
1268 seqProduct = new HashMap<SequenceI, AlignedCodon>();
1269 alignedCodons.put(codon, seqProduct);
1271 seqProduct.put(protein, codon);
1275 * Returns true if a cDNA/Protein mapping either exists, or could be made,
1276 * between at least one pair of sequences in the two alignments. Currently,
1279 * <li>One alignment must be nucleotide, and the other protein</li>
1280 * <li>At least one pair of sequences must be already mapped, or mappable</li>
1281 * <li>Mappable means the nucleotide translation matches the protein sequence</li>
1282 * <li>The translation may ignore start and stop codons if present in the
1290 public static boolean isMappable(AlignmentI al1, AlignmentI al2)
1292 if (al1 == null || al2 == null)
1298 * Require one nucleotide and one protein
1300 if (al1.isNucleotide() == al2.isNucleotide())
1304 AlignmentI dna = al1.isNucleotide() ? al1 : al2;
1305 AlignmentI protein = dna == al1 ? al2 : al1;
1306 List<AlignedCodonFrame> mappings = protein.getCodonFrames();
1307 for (SequenceI dnaSeq : dna.getSequences())
1309 for (SequenceI proteinSeq : protein.getSequences())
1311 if (isMappable(dnaSeq, proteinSeq, mappings))
1321 * Returns true if the dna sequence is mapped, or could be mapped, to the
1329 protected static boolean isMappable(SequenceI dnaSeq,
1330 SequenceI proteinSeq, List<AlignedCodonFrame> mappings)
1332 if (dnaSeq == null || proteinSeq == null)
1337 SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq
1338 .getDatasetSequence();
1339 SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq
1340 : proteinSeq.getDatasetSequence();
1342 for (AlignedCodonFrame mapping : mappings)
1344 if (proteinDs == mapping.getAaForDnaSeq(dnaDs))
1354 * Just try to make a mapping (it is not yet stored), test whether
1357 return mapCdnaToProtein(proteinDs, dnaDs) != null;
1361 * Finds any reference annotations associated with the sequences in
1362 * sequenceScope, that are not already added to the alignment, and adds them
1363 * to the 'candidates' map. Also populates a lookup table of annotation
1364 * labels, keyed by calcId, for use in constructing tooltips or the like.
1366 * @param sequenceScope
1367 * the sequences to scan for reference annotations
1368 * @param labelForCalcId
1369 * (optional) map to populate with label for calcId
1371 * map to populate with annotations for sequence
1373 * the alignment to check for presence of annotations
1375 public static void findAddableReferenceAnnotations(
1376 List<SequenceI> sequenceScope,
1377 Map<String, String> labelForCalcId,
1378 final Map<SequenceI, List<AlignmentAnnotation>> candidates,
1381 if (sequenceScope == null)
1387 * For each sequence in scope, make a list of any annotations on the
1388 * underlying dataset sequence which are not already on the alignment.
1390 * Add to a map of { alignmentSequence, <List of annotations to add> }
1392 for (SequenceI seq : sequenceScope)
1394 SequenceI dataset = seq.getDatasetSequence();
1395 if (dataset == null)
1399 AlignmentAnnotation[] datasetAnnotations = dataset.getAnnotation();
1400 if (datasetAnnotations == null)
1404 final List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
1405 for (AlignmentAnnotation dsann : datasetAnnotations)
1408 * Find matching annotations on the alignment. If none is found, then
1409 * add this annotation to the list of 'addable' annotations for this
1412 final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
1413 .findAnnotations(seq, dsann.getCalcId(), dsann.label);
1414 if (!matchedAlignmentAnnotations.iterator().hasNext())
1417 if (labelForCalcId != null)
1419 labelForCalcId.put(dsann.getCalcId(), dsann.label);
1424 * Save any addable annotations for this sequence
1426 if (!result.isEmpty())
1428 candidates.put(seq, result);
1434 * Adds annotations to the top of the alignment annotations, in the same order
1435 * as their related sequences.
1437 * @param annotations
1438 * the annotations to add
1440 * the alignment to add them to
1441 * @param selectionGroup
1442 * current selection group (or null if none)
1444 public static void addReferenceAnnotations(
1445 Map<SequenceI, List<AlignmentAnnotation>> annotations,
1446 final AlignmentI alignment, final SequenceGroup selectionGroup)
1448 for (SequenceI seq : annotations.keySet())
1450 for (AlignmentAnnotation ann : annotations.get(seq))
1452 AlignmentAnnotation copyAnn = new AlignmentAnnotation(ann);
1454 int endRes = ann.annotations.length;
1455 if (selectionGroup != null)
1457 startRes = selectionGroup.getStartRes();
1458 endRes = selectionGroup.getEndRes();
1460 copyAnn.restrict(startRes, endRes);
1463 * Add to the sequence (sets copyAnn.datasetSequence), unless the
1464 * original annotation is already on the sequence.
1466 if (!seq.hasAnnotation(ann))
1468 seq.addAlignmentAnnotation(copyAnn);
1471 copyAnn.adjustForAlignment();
1472 // add to the alignment and set visible
1473 alignment.addAnnotation(copyAnn);
1474 copyAnn.visible = true;
1480 * Set visibility of alignment annotations of specified types (labels), for
1481 * specified sequences. This supports controls like
1482 * "Show all secondary structure", "Hide all Temp factor", etc.
1484 * @al the alignment to scan for annotations
1486 * the types (labels) of annotations to be updated
1487 * @param forSequences
1488 * if not null, only annotations linked to one of these sequences are
1489 * in scope for update; if null, acts on all sequence annotations
1491 * if this flag is true, 'types' is ignored (label not checked)
1493 * if true, set visibility on, else set off
1495 public static void showOrHideSequenceAnnotations(AlignmentI al,
1496 Collection<String> types, List<SequenceI> forSequences,
1497 boolean anyType, boolean doShow)
1499 AlignmentAnnotation[] anns = al.getAlignmentAnnotation();
1502 for (AlignmentAnnotation aa : anns)
1504 if (anyType || types.contains(aa.label))
1506 if ((aa.sequenceRef != null)
1507 && (forSequences == null || forSequences
1508 .contains(aa.sequenceRef)))
1510 aa.visible = doShow;
1518 * Returns true if either sequence has a cross-reference to the other
1524 public static boolean haveCrossRef(SequenceI seq1, SequenceI seq2)
1526 // Note: moved here from class CrossRef as the latter class has dependencies
1527 // not availability to the applet's classpath
1528 return hasCrossRef(seq1, seq2) || hasCrossRef(seq2, seq1);
1532 * Returns true if seq1 has a cross-reference to seq2. Currently this assumes
1533 * that sequence name is structured as Source|AccessionId.
1539 public static boolean hasCrossRef(SequenceI seq1, SequenceI seq2)
1541 if (seq1 == null || seq2 == null)
1545 String name = seq2.getName();
1546 final DBRefEntry[] xrefs = seq1.getDBRefs();
1549 for (DBRefEntry xref : xrefs)
1551 String xrefName = xref.getSource() + "|" + xref.getAccessionId();
1552 // case-insensitive test, consistent with DBRefEntry.equalRef()
1553 if (xrefName.equalsIgnoreCase(name))
1563 * Constructs an alignment consisting of the mapped (CDS) regions in the given
1564 * nucleotide sequences, and updates mappings to match. The CDS sequences are
1565 * added to the original alignment's dataset, which is shared by the new
1566 * alignment. Mappings from nucleotide to CDS, and from CDS to protein, are
1567 * added to the alignment dataset.
1570 * aligned nucleotide (dna or cds) sequences
1572 * the alignment dataset the sequences belong to
1574 * (optional) to restrict results to CDS that map to specified
1576 * @return an alignment whose sequences are the cds-only parts of the dna
1577 * sequences (or null if no mappings are found)
1579 public static AlignmentI makeCdsAlignment(SequenceI[] dna,
1580 AlignmentI dataset, SequenceI[] products)
1582 if (dataset == null || dataset.getDataset() != null)
1584 throw new IllegalArgumentException(
1585 "IMPLEMENTATION ERROR: dataset.getDataset() must be null!");
1587 List<SequenceI> foundSeqs = new ArrayList<SequenceI>();
1588 List<SequenceI> cdsSeqs = new ArrayList<SequenceI>();
1589 List<AlignedCodonFrame> mappings = dataset.getCodonFrames();
1590 HashSet<SequenceI> productSeqs = null;
1591 if (products != null)
1593 productSeqs = new HashSet<SequenceI>();
1594 for (SequenceI seq : products)
1596 productSeqs.add(seq.getDatasetSequence() == null ? seq : seq
1597 .getDatasetSequence());
1602 * Construct CDS sequences from mappings on the alignment dataset.
1604 * - find the protein product(s) mapped to from each dna sequence
1605 * - if the mapping covers the whole dna sequence (give or take start/stop
1606 * codon), take the dna as the CDS sequence
1607 * - else search dataset mappings for a suitable dna sequence, i.e. one
1608 * whose whole sequence is mapped to the protein
1609 * - if no sequence found, construct one from the dna sequence and mapping
1610 * (and add it to dataset so it is found if this is repeated)
1612 for (SequenceI dnaSeq : dna)
1614 SequenceI dnaDss = dnaSeq.getDatasetSequence() == null ? dnaSeq
1615 : dnaSeq.getDatasetSequence();
1617 List<AlignedCodonFrame> seqMappings = MappingUtils
1618 .findMappingsForSequence(dnaSeq, mappings);
1619 for (AlignedCodonFrame mapping : seqMappings)
1621 List<Mapping> mappingsFromSequence = mapping
1622 .getMappingsFromSequence(dnaSeq);
1624 for (Mapping aMapping : mappingsFromSequence)
1626 MapList mapList = aMapping.getMap();
1627 if (mapList.getFromRatio() == 1)
1630 * not a dna-to-protein mapping (likely dna-to-cds)
1636 * skip if mapping is not to one of the target set of proteins
1638 SequenceI proteinProduct = aMapping.getTo();
1639 if (productSeqs != null && !productSeqs.contains(proteinProduct))
1645 * try to locate the CDS from the dataset mappings;
1646 * guard against duplicate results (for the case that protein has
1647 * dbrefs to both dna and cds sequences)
1649 SequenceI cdsSeq = findCdsForProtein(mappings, dnaSeq,
1650 seqMappings, aMapping);
1653 if (!foundSeqs.contains(cdsSeq))
1655 foundSeqs.add(cdsSeq);
1656 SequenceI derivedSequence = cdsSeq.deriveSequence();
1657 cdsSeqs.add(derivedSequence);
1658 if (!dataset.getSequences().contains(cdsSeq))
1660 dataset.addSequence(cdsSeq);
1667 * didn't find mapped CDS sequence - construct it and add
1668 * its dataset sequence to the dataset
1670 cdsSeq = makeCdsSequence(dnaSeq.getDatasetSequence(), aMapping);
1671 SequenceI cdsSeqDss = cdsSeq.createDatasetSequence();
1672 cdsSeqs.add(cdsSeq);
1673 if (!dataset.getSequences().contains(cdsSeqDss))
1675 dataset.addSequence(cdsSeqDss);
1679 * add a mapping from CDS to the (unchanged) mapped to range
1681 List<int[]> cdsRange = Collections.singletonList(new int[] { 1,
1682 cdsSeq.getLength() });
1683 MapList cdsToProteinMap = new MapList(cdsRange, mapList.getToRanges(),
1684 mapList.getFromRatio(), mapList.getToRatio());
1685 AlignedCodonFrame cdsToProteinMapping = new AlignedCodonFrame();
1686 cdsToProteinMapping.addMap(cdsSeq, proteinProduct, cdsToProteinMap);
1689 * guard against duplicating the mapping if repeating this action
1691 if (!mappings.contains(cdsToProteinMapping))
1693 mappings.add(cdsToProteinMapping);
1697 * copy protein's dbrefs to CDS sequence
1698 * this enables Get Cross-References from CDS alignment
1700 DBRefEntry[] proteinRefs = DBRefUtils.selectDbRefs(false,
1701 proteinProduct.getDBRefs());
1702 if (proteinRefs != null)
1704 for (DBRefEntry ref : proteinRefs)
1706 DBRefEntry cdsToProteinRef = new DBRefEntry(ref);
1707 cdsToProteinRef.setMap(new Mapping(proteinProduct,
1709 cdsSeqDss.addDBRef(cdsToProteinRef);
1714 * add another mapping from original 'from' range to CDS
1716 AlignedCodonFrame dnaToCdsMapping = new AlignedCodonFrame();
1717 MapList dnaToCdsMap = new MapList(mapList.getFromRanges(),
1720 dnaToCdsMapping.addMap(dnaSeq.getDatasetSequence(), cdsSeq,
1722 if (!mappings.contains(dnaToCdsMapping))
1724 mappings.add(dnaToCdsMapping);
1728 * add DBRef with mapping from protein to CDS
1729 * (this enables Get Cross-References from protein alignment)
1730 * This is tricky because we can't have two DBRefs with the
1731 * same source and accession, so need a different accession for
1732 * the CDS from the dna sequence
1734 DBRefEntryI dnaRef = dnaDss.getSourceDBRef();
1737 // assuming cds version same as dna ?!?
1738 DBRefEntry proteinToCdsRef = new DBRefEntry(dnaRef.getSource(),
1739 dnaRef.getVersion(), cdsSeq.getName());
1740 proteinToCdsRef.setMap(new Mapping(cdsSeqDss, cdsToProteinMap
1742 proteinProduct.addDBRef(proteinToCdsRef);
1746 * transfer any features on dna that overlap the CDS
1748 transferFeatures(dnaSeq, cdsSeq, cdsToProteinMap, null,
1749 SequenceOntologyI.CDS);
1754 AlignmentI cds = new Alignment(cdsSeqs.toArray(new SequenceI[cdsSeqs
1756 cds.setDataset(dataset);
1762 * A helper method that finds a CDS sequence in the alignment dataset that is
1763 * mapped to the given protein sequence, and either is, or has a mapping from,
1764 * the given dna sequence.
1767 * set of all mappings on the dataset
1769 * a dna (or cds) sequence we are searching from
1770 * @param seqMappings
1771 * the set of mappings involving dnaSeq
1773 * an initial candidate from seqMappings
1776 static SequenceI findCdsForProtein(List<AlignedCodonFrame> mappings,
1777 SequenceI dnaSeq, List<AlignedCodonFrame> seqMappings,
1781 * TODO a better dna-cds-protein mapping data representation to allow easy
1782 * navigation; until then this clunky looping around lists of mappings
1784 SequenceI seqDss = dnaSeq.getDatasetSequence() == null ? dnaSeq
1785 : dnaSeq.getDatasetSequence();
1786 SequenceI proteinProduct = aMapping.getTo();
1789 * is this mapping from the whole dna sequence (i.e. CDS)?
1790 * allowing for possible stop codon on dna but not peptide
1792 int mappedFromLength = MappingUtils.getLength(aMapping.getMap()
1794 int dnaLength = seqDss.getLength();
1795 if (mappedFromLength == dnaLength || mappedFromLength == dnaLength - 3)
1801 * looks like we found the dna-to-protein mapping; search for the
1802 * corresponding cds-to-protein mapping
1804 List<AlignedCodonFrame> mappingsToPeptide = MappingUtils
1805 .findMappingsForSequence(proteinProduct, mappings);
1806 for (AlignedCodonFrame acf : mappingsToPeptide)
1808 for (SequenceToSequenceMapping map : acf.getMappings())
1810 Mapping mapping = map.getMapping();
1811 if (mapping != aMapping && mapping.getMap().getFromRatio() == 3
1812 && proteinProduct == mapping.getTo()
1813 && seqDss != map.getFromSeq())
1815 mappedFromLength = MappingUtils.getLength(mapping.getMap()
1817 if (mappedFromLength == map.getFromSeq().getLength())
1820 * found a 3:1 mapping to the protein product which covers
1821 * the whole dna sequence i.e. is from CDS; finally check it
1822 * is from the dna start sequence
1824 SequenceI cdsSeq = map.getFromSeq();
1825 List<AlignedCodonFrame> dnaToCdsMaps = MappingUtils
1826 .findMappingsForSequence(cdsSeq, seqMappings);
1827 if (!dnaToCdsMaps.isEmpty())
1839 * Helper method that makes a CDS sequence as defined by the mappings from the
1840 * given sequence i.e. extracts the 'mapped from' ranges (which may be on
1841 * forward or reverse strand).
1845 * @return CDS sequence (as a dataset sequence)
1847 static SequenceI makeCdsSequence(SequenceI seq, Mapping mapping)
1849 char[] seqChars = seq.getSequence();
1850 List<int[]> fromRanges = mapping.getMap().getFromRanges();
1851 int cdsWidth = MappingUtils.getLength(fromRanges);
1852 char[] newSeqChars = new char[cdsWidth];
1855 for (int[] range : fromRanges)
1857 if (range[0] <= range[1])
1859 // forward strand mapping - just copy the range
1860 int length = range[1] - range[0] + 1;
1861 System.arraycopy(seqChars, range[0] - 1, newSeqChars, newPos,
1867 // reverse strand mapping - copy and complement one by one
1868 for (int i = range[0]; i >= range[1]; i--)
1870 newSeqChars[newPos++] = Dna.getComplement(seqChars[i - 1]);
1876 * assign 'from id' held in the mapping if set (e.g. EMBL protein_id),
1877 * else generate a sequence name
1879 String mapFromId = mapping.getMappedFromId();
1880 String seqId = "CDS|" + (mapFromId != null ? mapFromId : seq.getName());
1881 SequenceI newSeq = new Sequence(seqId, newSeqChars, 1, newPos);
1882 // newSeq.setDescription(mapFromId);
1888 * Transfers co-located features on 'fromSeq' to 'toSeq', adjusting the
1889 * feature start/end ranges, optionally omitting specified feature types.
1890 * Returns the number of features copied.
1895 * if not null, only features of this type are copied (including
1896 * subtypes in the Sequence Ontology)
1898 * the mapping from 'fromSeq' to 'toSeq'
1901 public static int transferFeatures(SequenceI fromSeq, SequenceI toSeq,
1902 MapList mapping, String select, String... omitting)
1904 SequenceI copyTo = toSeq;
1905 while (copyTo.getDatasetSequence() != null)
1907 copyTo = copyTo.getDatasetSequence();
1910 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
1912 SequenceFeature[] sfs = fromSeq.getSequenceFeatures();
1915 for (SequenceFeature sf : sfs)
1917 String type = sf.getType();
1918 if (select != null && !so.isA(type, select))
1922 boolean omit = false;
1923 for (String toOmit : omitting)
1925 if (type.equals(toOmit))
1936 * locate the mapped range - null if either start or end is
1937 * not mapped (no partial overlaps are calculated)
1939 int start = sf.getBegin();
1940 int end = sf.getEnd();
1941 int[] mappedTo = mapping.locateInTo(start, end);
1943 * if whole exon range doesn't map, try interpreting it
1944 * as 5' or 3' exon overlapping the CDS range
1946 if (mappedTo == null)
1948 mappedTo = mapping.locateInTo(end, end);
1949 if (mappedTo != null)
1952 * end of exon is in CDS range - 5' overlap
1953 * to a range from the start of the peptide
1958 if (mappedTo == null)
1960 mappedTo = mapping.locateInTo(start, start);
1961 if (mappedTo != null)
1964 * start of exon is in CDS range - 3' overlap
1965 * to a range up to the end of the peptide
1967 mappedTo[1] = toSeq.getLength();
1970 if (mappedTo != null)
1972 SequenceFeature copy = new SequenceFeature(sf);
1973 copy.setBegin(Math.min(mappedTo[0], mappedTo[1]));
1974 copy.setEnd(Math.max(mappedTo[0], mappedTo[1]));
1975 copyTo.addSequenceFeature(copy);
1984 * Returns a mapping from dna to protein by inspecting sequence features of
1985 * type "CDS" on the dna.
1991 public static MapList mapCdsToProtein(SequenceI dnaSeq,
1992 SequenceI proteinSeq)
1994 List<int[]> ranges = findCdsPositions(dnaSeq);
1995 int mappedDnaLength = MappingUtils.getLength(ranges);
1997 int proteinLength = proteinSeq.getLength();
1998 int proteinStart = proteinSeq.getStart();
1999 int proteinEnd = proteinSeq.getEnd();
2002 * incomplete start codon may mean X at start of peptide
2003 * we ignore both for mapping purposes
2005 if (proteinSeq.getCharAt(0) == 'X')
2007 // todo JAL-2022 support startPhase > 0
2011 List<int[]> proteinRange = new ArrayList<int[]>();
2014 * dna length should map to protein (or protein plus stop codon)
2016 int codesForResidues = mappedDnaLength / 3;
2017 if (codesForResidues == (proteinLength + 1))
2019 // assuming extra codon is for STOP and not in peptide
2022 if (codesForResidues == proteinLength)
2024 proteinRange.add(new int[] { proteinStart, proteinEnd });
2025 return new MapList(ranges, proteinRange, 3, 1);
2031 * Returns a list of CDS ranges found (as sequence positions base 1), i.e. of
2032 * start/end positions of sequence features of type "CDS" (or a sub-type of
2033 * CDS in the Sequence Ontology). The ranges are sorted into ascending start
2034 * position order, so this method is only valid for linear CDS in the same
2035 * sense as the protein product.
2040 public static List<int[]> findCdsPositions(SequenceI dnaSeq)
2042 List<int[]> result = new ArrayList<int[]>();
2043 SequenceFeature[] sfs = dnaSeq.getSequenceFeatures();
2049 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
2052 for (SequenceFeature sf : sfs)
2055 * process a CDS feature (or a sub-type of CDS)
2057 if (so.isA(sf.getType(), SequenceOntologyI.CDS))
2062 phase = Integer.parseInt(sf.getPhase());
2063 } catch (NumberFormatException e)
2068 * phase > 0 on first codon means 5' incomplete - skip to the start
2069 * of the next codon; example ENST00000496384
2071 int begin = sf.getBegin();
2072 int end = sf.getEnd();
2073 if (result.isEmpty())
2078 // shouldn't happen!
2080 .println("Error: start phase extends beyond start CDS in "
2081 + dnaSeq.getName());
2084 result.add(new int[] { begin, end });
2089 * remove 'startPhase' positions (usually 0) from the first range
2090 * so we begin at the start of a complete codon
2092 if (!result.isEmpty())
2094 // TODO JAL-2022 correctly model start phase > 0
2095 result.get(0)[0] += startPhase;
2099 * Finally sort ranges by start position. This avoids a dependency on
2100 * keeping features in order on the sequence (if they are in order anyway,
2101 * the sort will have almost no work to do). The implicit assumption is CDS
2102 * ranges are assembled in order. Other cases should not use this method,
2103 * but instead construct an explicit mapping for CDS (e.g. EMBL parsing).
2105 Collections.sort(result, new Comparator<int[]>()
2108 public int compare(int[] o1, int[] o2)
2110 return Integer.compare(o1[0], o2[0]);
2117 * Maps exon features from dna to protein, and computes variants in peptide
2118 * product generated by variants in dna, and adds them as sequence_variant
2119 * features on the protein sequence. Returns the number of variant features
2124 * @param dnaToProtein
2126 public static int computeProteinFeatures(SequenceI dnaSeq,
2127 SequenceI peptide, MapList dnaToProtein)
2129 while (dnaSeq.getDatasetSequence() != null)
2131 dnaSeq = dnaSeq.getDatasetSequence();
2133 while (peptide.getDatasetSequence() != null)
2135 peptide = peptide.getDatasetSequence();
2138 transferFeatures(dnaSeq, peptide, dnaToProtein, SequenceOntologyI.EXON);
2141 * compute protein variants from dna variants and codon mappings;
2142 * NB - alternatively we could retrieve this using the REST service e.g.
2143 * http://rest.ensembl.org/overlap/translation
2144 * /ENSP00000288602?feature=transcript_variation;content-type=text/xml
2145 * which would be a bit slower but possibly more reliable
2149 * build a map with codon variations for each potentially varying peptide
2151 LinkedHashMap<Integer, List<DnaVariant>[]> variants = buildDnaVariantsMap(
2152 dnaSeq, dnaToProtein);
2155 * scan codon variations, compute peptide variants and add to peptide sequence
2158 for (Entry<Integer, List<DnaVariant>[]> variant : variants.entrySet())
2160 int peptidePos = variant.getKey();
2161 List<DnaVariant>[] codonVariants = variant.getValue();
2162 count += computePeptideVariants(peptide, peptidePos, codonVariants);
2166 * sort to get sequence features in start position order
2167 * - would be better to store in Sequence as a TreeSet or NCList?
2169 if (peptide.getSequenceFeatures() != null)
2171 Arrays.sort(peptide.getSequenceFeatures(),
2172 new Comparator<SequenceFeature>()
2175 public int compare(SequenceFeature o1, SequenceFeature o2)
2177 int c = Integer.compare(o1.getBegin(), o2.getBegin());
2178 return c == 0 ? Integer.compare(o1.getEnd(), o2.getEnd())
2187 * Computes non-synonymous peptide variants from codon variants and adds them
2188 * as sequence_variant features on the protein sequence (one feature per
2189 * allele variant). Selected attributes (variant id, clinical significance)
2190 * are copied over to the new features.
2193 * the protein sequence
2195 * the position to compute peptide variants for
2196 * @param codonVariants
2197 * a list of dna variants per codon position
2198 * @return the number of features added
2200 static int computePeptideVariants(SequenceI peptide, int peptidePos,
2201 List<DnaVariant>[] codonVariants)
2203 String residue = String.valueOf(peptide.getCharAt(peptidePos - 1));
2205 String base1 = codonVariants[0].get(0).base;
2206 String base2 = codonVariants[1].get(0).base;
2207 String base3 = codonVariants[2].get(0).base;
2210 * variants in first codon base
2212 for (DnaVariant var : codonVariants[0])
2214 if (var.variant != null)
2216 String alleles = (String) var.variant.getValue("alleles");
2217 if (alleles != null)
2219 for (String base : alleles.split(","))
2221 String codon = base + base2 + base3;
2222 if (addPeptideVariant(peptide, peptidePos, residue, var, codon))
2232 * variants in second codon base
2234 for (DnaVariant var : codonVariants[1])
2236 if (var.variant != null)
2238 String alleles = (String) var.variant.getValue("alleles");
2239 if (alleles != null)
2241 for (String base : alleles.split(","))
2243 String codon = base1 + base + base3;
2244 if (addPeptideVariant(peptide, peptidePos, residue, var, codon))
2254 * variants in third codon base
2256 for (DnaVariant var : codonVariants[2])
2258 if (var.variant != null)
2260 String alleles = (String) var.variant.getValue("alleles");
2261 if (alleles != null)
2263 for (String base : alleles.split(","))
2265 String codon = base1 + base2 + base;
2266 if (addPeptideVariant(peptide, peptidePos, residue, var, codon))
2279 * Helper method that adds a peptide variant feature, provided the given codon
2280 * translates to a value different to the current residue (is a non-synonymous
2281 * variant). ID and clinical_significance attributes of the dna variant (if
2282 * present) are copied to the new feature.
2289 * @return true if a feature was added, else false
2291 static boolean addPeptideVariant(SequenceI peptide, int peptidePos,
2292 String residue, DnaVariant var, String codon)
2295 * get peptide translation of codon e.g. GAT -> D
2296 * note that variants which are not single alleles,
2297 * e.g. multibase variants or HGMD_MUTATION etc
2298 * are currently ignored here
2300 String trans = codon.contains("-") ? "-"
2301 : (codon.length() > 3 ? null : ResidueProperties
2302 .codonTranslate(codon));
2303 if (trans != null && !trans.equals(residue))
2305 String residue3Char = StringUtils
2306 .toSentenceCase(ResidueProperties.aa2Triplet.get(residue));
2307 String trans3Char = StringUtils
2308 .toSentenceCase(ResidueProperties.aa2Triplet.get(trans));
2309 String desc = "p." + residue3Char + peptidePos + trans3Char;
2310 // set score to 0f so 'graduated colour' option is offered! JAL-2060
2311 SequenceFeature sf = new SequenceFeature(
2312 SequenceOntologyI.SEQUENCE_VARIANT, desc, peptidePos,
2313 peptidePos, 0f, "Jalview");
2314 StringBuilder attributes = new StringBuilder(32);
2315 String id = (String) var.variant.getValue(ID);
2318 if (id.startsWith(SEQUENCE_VARIANT))
2320 id = id.substring(SEQUENCE_VARIANT.length());
2322 sf.setValue(ID, id);
2323 attributes.append(ID).append("=").append(id);
2324 // TODO handle other species variants
2325 StringBuilder link = new StringBuilder(32);
2328 link.append(desc).append(" ").append(id)
2329 .append("|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=")
2330 .append(URLEncoder.encode(id, "UTF-8"));
2331 sf.addLink(link.toString());
2332 } catch (UnsupportedEncodingException e)
2337 String clinSig = (String) var.variant
2338 .getValue(CLINICAL_SIGNIFICANCE);
2339 if (clinSig != null)
2341 sf.setValue(CLINICAL_SIGNIFICANCE, clinSig);
2342 attributes.append(";").append(CLINICAL_SIGNIFICANCE).append("=")
2345 peptide.addSequenceFeature(sf);
2346 if (attributes.length() > 0)
2348 sf.setAttributes(attributes.toString());
2356 * Builds a map whose key is position in the protein sequence, and value is a
2357 * list of the base and all variants for each corresponding codon position
2360 * @param dnaToProtein
2363 static LinkedHashMap<Integer, List<DnaVariant>[]> buildDnaVariantsMap(
2364 SequenceI dnaSeq, MapList dnaToProtein)
2367 * map from peptide position to all variants of the codon which codes for it
2368 * LinkedHashMap ensures we keep the peptide features in sequence order
2370 LinkedHashMap<Integer, List<DnaVariant>[]> variants = new LinkedHashMap<Integer, List<DnaVariant>[]>();
2371 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
2373 SequenceFeature[] dnaFeatures = dnaSeq.getSequenceFeatures();
2374 if (dnaFeatures == null)
2379 int dnaStart = dnaSeq.getStart();
2380 int[] lastCodon = null;
2381 int lastPeptidePostion = 0;
2384 * build a map of codon variations for peptides
2386 for (SequenceFeature sf : dnaFeatures)
2388 int dnaCol = sf.getBegin();
2389 if (dnaCol != sf.getEnd())
2391 // not handling multi-locus variant features
2394 if (so.isA(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT))
2396 int[] mapsTo = dnaToProtein.locateInTo(dnaCol, dnaCol);
2399 // feature doesn't lie within coding region
2402 int peptidePosition = mapsTo[0];
2403 List<DnaVariant>[] codonVariants = variants.get(peptidePosition);
2404 if (codonVariants == null)
2406 codonVariants = new ArrayList[3];
2407 codonVariants[0] = new ArrayList<DnaVariant>();
2408 codonVariants[1] = new ArrayList<DnaVariant>();
2409 codonVariants[2] = new ArrayList<DnaVariant>();
2410 variants.put(peptidePosition, codonVariants);
2414 * extract dna variants to a string array
2416 String alls = (String) sf.getValue("alleles");
2421 String[] alleles = alls.toUpperCase().split(",");
2423 for (String allele : alleles)
2425 alleles[i++] = allele.trim(); // lose any space characters "A, G"
2429 * get this peptide's codon positions e.g. [3, 4, 5] or [4, 7, 10]
2431 int[] codon = peptidePosition == lastPeptidePostion ? lastCodon
2432 : MappingUtils.flattenRanges(dnaToProtein.locateInFrom(
2433 peptidePosition, peptidePosition));
2434 lastPeptidePostion = peptidePosition;
2438 * save nucleotide (and any variant) for each codon position
2440 for (int codonPos = 0; codonPos < 3; codonPos++)
2442 String nucleotide = String.valueOf(
2443 dnaSeq.getCharAt(codon[codonPos] - dnaStart))
2445 List<DnaVariant> codonVariant = codonVariants[codonPos];
2446 if (codon[codonPos] == dnaCol)
2448 if (!codonVariant.isEmpty()
2449 && codonVariant.get(0).variant == null)
2452 * already recorded base value, add this variant
2454 codonVariant.get(0).variant = sf;
2459 * add variant with base value
2461 codonVariant.add(new DnaVariant(nucleotide, sf));
2464 else if (codonVariant.isEmpty())
2467 * record (possibly non-varying) base value
2469 codonVariant.add(new DnaVariant(nucleotide));
2478 * Makes an alignment with a copy of the given sequences, adding in any
2479 * non-redundant sequences which are mapped to by the cross-referenced
2485 * the alignment dataset shared by the new copy
2488 public static AlignmentI makeCopyAlignment(SequenceI[] seqs,
2489 SequenceI[] xrefs, AlignmentI dataset)
2491 AlignmentI copy = new Alignment(new Alignment(seqs));
2492 copy.setDataset(dataset);
2494 SequenceIdMatcher matcher = new SequenceIdMatcher(seqs);
2497 for (SequenceI xref : xrefs)
2499 DBRefEntry[] dbrefs = xref.getDBRefs();
2502 for (DBRefEntry dbref : dbrefs)
2504 if (dbref.getMap() == null || dbref.getMap().getTo() == null)
2508 SequenceI mappedTo = dbref.getMap().getTo();
2509 SequenceI match = matcher.findIdMatch(mappedTo);
2512 matcher.add(mappedTo);
2513 copy.addSequence(mappedTo);
2523 * Try to align sequences in 'unaligned' to match the alignment of their
2524 * mapped regions in 'aligned'. For example, could use this to align CDS
2525 * sequences which are mapped to their parent cDNA sequences.
2527 * This method handles 1:1 mappings (dna-to-dna or protein-to-protein). For
2528 * dna-to-protein or protein-to-dna use alternative methods.
2531 * sequences to be aligned
2533 * holds aligned sequences and their mappings
2536 public static int alignAs(AlignmentI unaligned, AlignmentI aligned)
2539 * easy case - aligning a copy of aligned sequences
2541 if (alignAsSameSequences(unaligned, aligned))
2543 return unaligned.getHeight();
2547 * fancy case - aligning via mappings between sequences
2549 List<SequenceI> unmapped = new ArrayList<SequenceI>();
2550 Map<Integer, Map<SequenceI, Character>> columnMap = buildMappedColumnsMap(
2551 unaligned, aligned, unmapped);
2552 int width = columnMap.size();
2553 char gap = unaligned.getGapCharacter();
2554 int realignedCount = 0;
2556 for (SequenceI seq : unaligned.getSequences())
2558 if (!unmapped.contains(seq))
2560 char[] newSeq = new char[width];
2561 Arrays.fill(newSeq, gap); // JBPComment - doubt this is faster than the
2562 // Integer iteration below
2567 * traverse the map to find columns populated
2570 for (Integer column : columnMap.keySet())
2572 Character c = columnMap.get(column).get(seq);
2576 * sequence has a character at this position
2586 * trim trailing gaps
2588 if (lastCol < width)
2590 char[] tmp = new char[lastCol + 1];
2591 System.arraycopy(newSeq, 0, tmp, 0, lastCol + 1);
2594 // TODO: optimise SequenceI to avoid char[]->String->char[]
2595 seq.setSequence(String.valueOf(newSeq));
2599 return realignedCount;
2603 * If unaligned and aligned sequences share the same dataset sequences, then
2604 * simply copies the aligned sequences to the unaligned sequences and returns
2605 * true; else returns false
2611 static boolean alignAsSameSequences(AlignmentI unaligned,
2614 if (aligned.getDataset() == null || unaligned.getDataset() == null)
2616 return false; // should only pass alignments with datasets here
2619 Map<SequenceI, SequenceI> alignedDatasets = new HashMap<SequenceI, SequenceI>();
2620 for (SequenceI seq : aligned.getSequences())
2622 alignedDatasets.put(seq.getDatasetSequence(), seq);
2626 * first pass - check whether all sequences to be aligned share a dataset
2627 * sequence with an aligned sequence
2629 for (SequenceI seq : unaligned.getSequences())
2631 if (!alignedDatasets.containsKey(seq.getDatasetSequence()))
2638 * second pass - copy aligned sequences
2640 for (SequenceI seq : unaligned.getSequences())
2642 SequenceI alignedSequence = alignedDatasets.get(seq
2643 .getDatasetSequence());
2644 // TODO: getSequenceAsString() will be deprecated in the future
2645 // TODO: need to leave to SequenceI implementor to update gaps
2646 seq.setSequence(alignedSequence.getSequenceAsString());
2653 * Returns a map whose key is alignment column number (base 1), and whose
2654 * values are a map of sequence characters in that column.
2661 static Map<Integer, Map<SequenceI, Character>> buildMappedColumnsMap(
2662 AlignmentI unaligned, AlignmentI aligned, List<SequenceI> unmapped)
2665 * Map will hold, for each aligned column position, a map of
2666 * {unalignedSequence, characterPerSequence} at that position.
2667 * TreeMap keeps the entries in ascending column order.
2669 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<Integer, Map<SequenceI, Character>>();
2672 * record any sequences that have no mapping so can't be realigned
2674 unmapped.addAll(unaligned.getSequences());
2676 List<AlignedCodonFrame> mappings = aligned.getCodonFrames();
2678 for (SequenceI seq : unaligned.getSequences())
2680 for (AlignedCodonFrame mapping : mappings)
2682 SequenceI fromSeq = mapping.findAlignedSequence(seq, aligned);
2683 if (fromSeq != null)
2685 Mapping seqMap = mapping.getMappingBetween(fromSeq, seq);
2686 if (addMappedPositions(seq, fromSeq, seqMap, map))
2688 unmapped.remove(seq);
2697 * Helper method that adds to a map the mapped column positions of a sequence. <br>
2698 * For example if aaTT-Tg-gAAA is mapped to TTTAAA then the map should record
2699 * that columns 3,4,6,10,11,12 map to characters T,T,T,A,A,A of the mapped to
2703 * the sequence whose column positions we are recording
2705 * a sequence that is mapped to the first sequence
2707 * the mapping from 'fromSeq' to 'seq'
2709 * a map to add the column positions (in fromSeq) of the mapped
2713 static boolean addMappedPositions(SequenceI seq, SequenceI fromSeq,
2714 Mapping seqMap, Map<Integer, Map<SequenceI, Character>> map)
2722 * invert mapping if it is from unaligned to aligned sequence
2724 if (seqMap.getTo() == fromSeq.getDatasetSequence())
2726 seqMap = new Mapping(seq.getDatasetSequence(), seqMap.getMap()
2730 char[] fromChars = fromSeq.getSequence();
2731 int toStart = seq.getStart();
2732 char[] toChars = seq.getSequence();
2735 * traverse [start, end, start, end...] ranges in fromSeq
2737 for (int[] fromRange : seqMap.getMap().getFromRanges())
2739 for (int i = 0; i < fromRange.length - 1; i += 2)
2741 boolean forward = fromRange[i + 1] >= fromRange[i];
2744 * find the range mapped to (sequence positions base 1)
2746 int[] range = seqMap.locateMappedRange(fromRange[i],
2750 System.err.println("Error in mapping " + seqMap + " from "
2751 + fromSeq.getName());
2754 int fromCol = fromSeq.findIndex(fromRange[i]);
2755 int mappedCharPos = range[0];
2758 * walk over the 'from' aligned sequence in forward or reverse
2759 * direction; when a non-gap is found, record the column position
2760 * of the next character of the mapped-to sequence; stop when all
2761 * the characters of the range have been counted
2763 while (mappedCharPos <= range[1] && fromCol <= fromChars.length
2766 if (!Comparison.isGap(fromChars[fromCol - 1]))
2769 * mapped from sequence has a character in this column
2770 * record the column position for the mapped to character
2772 Map<SequenceI, Character> seqsMap = map.get(fromCol);
2773 if (seqsMap == null)
2775 seqsMap = new HashMap<SequenceI, Character>();
2776 map.put(fromCol, seqsMap);
2778 seqsMap.put(seq, toChars[mappedCharPos - toStart]);
2781 fromCol += (forward ? 1 : -1);
2788 // strictly temporary hack until proper criteria for aligning protein to cds
2789 // are in place; this is so Ensembl -> fetch xrefs Uniprot aligns the Uniprot
2790 public static boolean looksLikeEnsembl(AlignmentI alignment)
2792 for (SequenceI seq : alignment.getSequences())
2794 String name = seq.getName();
2795 if (!name.startsWith("ENSG") && !name.startsWith("ENST"))