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 java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Comparator;
26 import java.util.Iterator;
27 import java.util.List;
29 import jalview.api.AlignViewportI;
30 import jalview.datamodel.AlignedCodon;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentAnnotation;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.Annotation;
36 import jalview.datamodel.DBRefEntry;
37 import jalview.datamodel.FeatureProperties;
38 import jalview.datamodel.GraphLine;
39 import jalview.datamodel.Mapping;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceFeature;
42 import jalview.datamodel.SequenceI;
43 import jalview.schemes.ResidueProperties;
44 import jalview.util.Comparison;
45 import jalview.util.DBRefUtils;
46 import jalview.util.MapList;
47 import jalview.util.ShiftList;
51 private static final String STOP_ASTERIX = "*";
53 private static final Comparator<AlignedCodon> comparator = new CodonComparator();
56 * 'final' variables describe the inputs to the translation, which should not
59 private final List<SequenceI> selection;
61 private final String[] seqstring;
63 private final Iterator<int[]> contigs;
65 private final char gapChar;
67 private final AlignmentAnnotation[] annotations;
69 private final int dnaWidth;
71 private final AlignmentI dataset;
73 private ShiftList vismapping;
75 private int[] startcontigs;
78 * Working variables for the translation.
80 * The width of the translation-in-progress protein alignment.
82 private int aaWidth = 0;
85 * This array will be built up so that position i holds the codon positions
86 * e.g. [7, 9, 10] that match column i (base 0) in the aligned translation.
87 * Note this implies a contract that if two codons do not align exactly, their
88 * translated products must occupy different column positions.
90 private AlignedCodon[] alignedCodons;
93 * Constructor given a viewport and the visible contigs.
96 * @param visibleContigs
98 public Dna(AlignViewportI viewport, Iterator<int[]> visibleContigs)
100 this.selection = Arrays.asList(viewport.getSequenceSelection());
101 this.seqstring = viewport.getViewAsString(true);
102 this.contigs = visibleContigs;
103 this.gapChar = viewport.getGapCharacter();
104 this.annotations = viewport.getAlignment().getAlignmentAnnotation();
105 this.dnaWidth = viewport.getAlignment().getWidth();
106 this.dataset = viewport.getAlignment().getDataset();
111 * Initialise contigs used as starting point for translateCodingRegion
113 private void initContigs()
115 vismapping = new ShiftList(); // map from viscontigs to seqstring
119 int[] lastregion = null;
120 ArrayList<Integer> tempcontigs = new ArrayList<>();
121 while (contigs.hasNext())
123 int[] region = contigs.next();
124 if (lastregion == null)
126 vismapping.addShift(npos, region[0]);
131 vismapping.addShift(npos, region[0] - lastregion[1] + 1);
134 tempcontigs.add(region[0]);
135 tempcontigs.add(region[1]);
138 startcontigs = new int[tempcontigs.size()];
140 for (Integer val : tempcontigs)
142 startcontigs[i] = val;
149 * Test whether codon positions cdp1 should align before, with, or after cdp2.
150 * Returns zero if all positions match (or either argument is null). Returns
151 * -1 if any position in the first codon precedes the corresponding position
152 * in the second codon. Else returns +1 (some position in the second codon
153 * precedes the corresponding position in the first).
155 * Note this is not necessarily symmetric, for example:
157 * <li>compareCodonPos([2,5,6], [3,4,5]) returns -1</li>
158 * <li>compareCodonPos([3,4,5], [2,5,6]) also returns -1</li>
165 public static final int compareCodonPos(AlignedCodon ac1, AlignedCodon ac2)
167 return comparator.compare(ac1, ac2);
168 // return jalview_2_8_2compare(ac1, ac2);
172 * Codon comparison up to Jalview 2.8.2. This rule is sequence order dependent
173 * - see http://issues.jalview.org/browse/JAL-1635
179 private static int jalview_2_8_2compare(AlignedCodon ac1,
182 if (ac1 == null || ac2 == null || (ac1.equals(ac2)))
186 if (ac1.pos1 < ac2.pos1 || ac1.pos2 < ac2.pos2 || ac1.pos3 < ac2.pos3)
188 // one base in cdp1 precedes the corresponding base in the other codon
191 // one base in cdp1 appears after the corresponding base in the other codon.
196 * Translates cDNA using the specified code table
200 public AlignmentI translateCdna(GeneticCodeI codeTable)
202 AlignedCodonFrame acf = new AlignedCodonFrame();
204 alignedCodons = new AlignedCodon[dnaWidth];
207 int sSize = selection.size();
208 List<SequenceI> pepseqs = new ArrayList<>();
209 for (s = 0; s < sSize; s++)
211 SequenceI newseq = translateCodingRegion(selection.get(s),
212 seqstring[s], acf, pepseqs, codeTable);
217 SequenceI ds = newseq;
220 while (ds.getDatasetSequence() != null)
222 ds = ds.getDatasetSequence();
224 dataset.addSequence(ds);
229 SequenceI[] newseqs = pepseqs.toArray(new SequenceI[pepseqs.size()]);
230 AlignmentI al = new Alignment(newseqs);
231 // ensure we look aligned.
233 // link the protein translation to the DNA dataset
234 al.setDataset(dataset);
235 translateAlignedAnnotations(al, acf);
236 al.addCodonFrame(acf);
241 * fake the collection of DbRefs with associated exon mappings to identify if
242 * a translation would generate distinct product in the currently selected
249 public static boolean canTranslate(SequenceI[] selection,
252 for (int gd = 0; gd < selection.length; gd++)
254 SequenceI dna = selection[gd];
255 List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
256 jalview.datamodel.DBRefSource.DNACODINGDBS);
259 // intersect with pep
260 List<DBRefEntry> mappedrefs = new ArrayList<>();
261 List<DBRefEntry> refs = dna.getDBRefs();
262 for (int d = 0, nd = refs.size(); d < nd; d++)
264 DBRefEntry ref = refs.get(d);
265 if (ref.getMap() != null && ref.getMap().getMap() != null
266 && ref.getMap().getMap().getFromRatio() == 3
267 && ref.getMap().getMap().getToRatio() == 1)
269 mappedrefs.add(ref); // add translated protein maps
272 dnarefs = mappedrefs;//.toArray(new DBRefEntry[mappedrefs.size()]);
273 for (int d = 0, nd = dnarefs.size(); d < nd; d++)
275 Mapping mp = dnarefs.get(d).getMap();
278 for (int vc = 0, nv = viscontigs.length; vc < nv; vc += 2)
280 int[] mpr = mp.locateMappedRange(viscontigs[vc],
295 * Translate nucleotide alignment annotations onto translated amino acid
296 * alignment using codon mapping codons
299 * the translated protein alignment
301 protected void translateAlignedAnnotations(AlignmentI al,
302 AlignedCodonFrame acf)
304 // Can only do this for columns with consecutive codons, or where
305 // annotation is sequence associated.
307 if (annotations != null)
309 for (AlignmentAnnotation annotation : annotations)
312 * Skip hidden or autogenerated annotation. Also (for now), RNA
313 * secondary structure annotation. If we want to show this against
314 * protein we need a smarter way to 'translate' without generating
315 * invalid (unbalanced) structure annotation.
317 if (annotation.autoCalculated || !annotation.visible
318 || annotation.isRNA())
324 Annotation[] anots = (annotation.annotations == null) ? null
325 : new Annotation[aSize];
328 for (int a = 0; a < aSize; a++)
330 // process through codon map.
331 if (a < alignedCodons.length && alignedCodons[a] != null
332 && alignedCodons[a].pos1 == (alignedCodons[a].pos3 - 2))
334 anots[a] = getCodonAnnotation(alignedCodons[a],
335 annotation.annotations);
340 AlignmentAnnotation aa = new AlignmentAnnotation(annotation.label,
341 annotation.description, anots);
342 aa.graph = annotation.graph;
343 aa.graphGroup = annotation.graphGroup;
344 aa.graphHeight = annotation.graphHeight;
345 if (annotation.getThreshold() != null)
347 aa.setThreshold(new GraphLine(annotation.getThreshold()));
349 if (annotation.hasScore)
351 aa.setScore(annotation.getScore());
354 final SequenceI seqRef = annotation.sequenceRef;
357 SequenceI aaSeq = acf.getAaForDnaSeq(seqRef);
360 // aa.compactAnnotationArray(); // throw away alignment annotation
362 aa.setSequenceRef(aaSeq);
364 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true);
365 aa.adjustForAlignment();
366 aaSeq.addAlignmentAnnotation(aa);
369 al.addAnnotation(aa);
374 private static Annotation getCodonAnnotation(AlignedCodon is,
375 Annotation[] annotations)
377 // Have a look at all the codon positions for annotation and put the first
378 // one found into the translated annotation pos.
380 Annotation annot = null;
381 for (int p = 1; p <= 3; p++)
383 int dnaCol = is.getBaseColumn(p);
384 if (annotations[dnaCol] != null)
388 annot = new Annotation(annotations[dnaCol]);
394 Annotation cpy = new Annotation(annotations[dnaCol]);
395 if (annot.colour == null)
397 annot.colour = cpy.colour;
399 if (annot.description == null || annot.description.length() == 0)
401 annot.description = cpy.description;
403 if (annot.displayCharacter == null)
405 annot.displayCharacter = cpy.displayCharacter;
407 if (annot.secondaryStructure == 0)
409 annot.secondaryStructure = cpy.secondaryStructure;
411 annot.value += cpy.value;
418 annot.value /= contrib;
424 * Translate a na sequence
427 * sequence displayed under viscontigs visible columns
429 * ORF read in some global alignment reference frame
431 * Definition of global ORF alignment reference frame
434 * @return sequence ready to be added to alignment.
436 protected SequenceI translateCodingRegion(SequenceI selection,
437 String seqstring, AlignedCodonFrame acf,
438 List<SequenceI> proteinSeqs, GeneticCodeI codeTable)
440 List<int[]> skip = new ArrayList<>();
441 int[] skipint = null;
446 int[] scontigs = new int[startcontigs.length];
447 System.arraycopy(startcontigs, 0, scontigs, 0, startcontigs.length);
449 // allocate a roughly sized buffer for the protein sequence
450 StringBuilder protein = new StringBuilder(seqstring.length() / 2);
451 String seq = seqstring.replace('U', 'T').replace('u', 'T');
452 char codon[] = new char[3];
453 int cdp[] = new int[3];
459 for (npos = 0, nend = seq.length(); npos < nend; npos++)
461 if (!Comparison.isGap(seq.charAt(npos)))
463 cdp[rf] = npos; // store position
464 codon[rf++] = seq.charAt(npos); // store base
469 * Filled up a reading frame...
471 AlignedCodon alignedCodon = new AlignedCodon(cdp[0], cdp[1], cdp[2]);
472 String aa = codeTable.translate(new String(codon));
474 final String gapString = String.valueOf(gapChar);
480 skipint = new int[] { alignedCodon.pos1,
486 skipint[1] = alignedCodon.pos3; // cdp[2];
493 skipint[0] = vismapping.shift(skipint[0]);
494 skipint[1] = vismapping.shift(skipint[1]);
495 for (vc = 0; vc < scontigs.length;)
497 if (scontigs[vc + 1] < skipint[0])
499 // before skipint starts
503 if (scontigs[vc] > skipint[1])
505 // finished editing so
508 // Edit the contig list to include the skipped region which did
511 // from : s1 e1 s2 e2 s3 e3
512 // to s: s1 e1 s2 k0 k1 e2 s3 e3
513 // list increases by one unless one boundary (s2==k0 or e2==k1)
514 // matches, and decreases by one if skipint intersects whole
516 if (scontigs[vc] <= skipint[0])
518 if (skipint[0] == scontigs[vc])
520 // skipint at start of contig
521 // shift the start of this contig
522 if (scontigs[vc + 1] > skipint[1])
524 scontigs[vc] = skipint[1];
529 if (scontigs[vc + 1] == skipint[1])
532 t = new int[scontigs.length - 2];
535 System.arraycopy(scontigs, 0, t, 0, vc - 1);
537 if (vc + 2 < t.length)
539 System.arraycopy(scontigs, vc + 2, t, vc,
546 // truncate contig to before the skipint region
547 scontigs[vc + 1] = skipint[0] - 1;
554 // scontig starts before start of skipint
555 if (scontigs[vc + 1] < skipint[1])
557 // skipint truncates end of scontig
558 scontigs[vc + 1] = skipint[0] - 1;
563 // divide region to new contigs
564 t = new int[scontigs.length + 2];
565 System.arraycopy(scontigs, 0, t, 0, vc + 1);
566 t[vc + 1] = skipint[0];
567 t[vc + 2] = skipint[1];
568 System.arraycopy(scontigs, vc + 1, t, vc + 3,
569 scontigs.length - (vc + 1));
579 if (aa.equals(ResidueProperties.STOP))
585 boolean findpos = true;
589 * Compare this codon's base positions with those currently aligned to
590 * this column in the translation.
592 final int compareCodonPos = compareCodonPos(alignedCodon,
593 alignedCodons[aspos]);
594 switch (compareCodonPos)
599 * This codon should precede the mapped positions - need to insert a
600 * gap in all prior sequences.
602 insertAAGap(aspos, proteinSeqs);
609 * This codon belongs after the aligned codons at aspos. Prefix it
610 * with a gap and try the next position.
619 * Exact match - codon 'belongs' at this translated position.
626 if (alignedCodons[aspos] == null)
628 // mark this column as aligning to this aligned reading frame
629 alignedCodons[aspos] = alignedCodon;
631 else if (!alignedCodons[aspos].equals(alignedCodon))
633 throw new IllegalStateException(
634 "Tried to coalign " + alignedCodons[aspos].toString()
635 + " with " + alignedCodon.toString());
637 if (aspos >= aaWidth)
639 // update maximum alignment width
642 // ready for next translated reading frame alignment position (if any)
648 SequenceI newseq = new Sequence(selection.getName(),
652 final String errMsg = "trimming contigs for incomplete terminal codon.";
653 System.err.println(errMsg);
654 // map and trim contigs to ORF region
655 vc = scontigs.length - 1;
656 lastnpos = vismapping.shift(lastnpos); // place npos in context of
657 // whole dna alignment (rather
658 // than visible contigs)
659 // incomplete ORF could be broken over one or two visible contig
661 while (vc >= 0 && scontigs[vc] > lastnpos)
663 if (vc > 0 && scontigs[vc - 1] > lastnpos)
669 // correct last interval in list.
670 scontigs[vc] = lastnpos;
674 if (vc > 0 && (vc + 1) < scontigs.length)
676 // truncate map list to just vc elements
677 int t[] = new int[vc + 1];
678 System.arraycopy(scontigs, 0, t, 0, vc + 1);
686 if (scontigs != null)
689 // map scontigs to actual sequence positions on selection
690 for (vc = 0; vc < scontigs.length; vc += 2)
692 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
693 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
694 if (scontigs[vc + 1] == selection.getEnd())
699 // trim trailing empty intervals.
700 if ((vc + 2) < scontigs.length)
702 int t[] = new int[vc + 2];
703 System.arraycopy(scontigs, 0, t, 0, vc + 2);
707 * delete intervals in scontigs which are not translated. 1. map skip
708 * into sequence position intervals 2. truncate existing ranges and add
709 * new ranges to exclude untranslated regions. if (skip.size()>0) {
710 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
711 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
712 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
713 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
714 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
715 * range } else { // truncate range and create new one if necessary iv =
716 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
717 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
718 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
720 MapList map = new MapList(scontigs, new int[] { 1, resSize }, 3, 1);
722 transferCodedFeatures(selection, newseq, map);
725 * Construct a dataset sequence for our new peptide.
727 SequenceI rseq = newseq.deriveSequence();
730 * Store a mapping (between the dataset sequences for the two
733 // SIDE-EFFECT: acf stores the aligned sequence reseq; to remove!
734 acf.addMap(selection, rseq, map);
738 // register the mapping somehow
744 * Insert a gap into the aligned proteins and the codon mapping array.
750 protected void insertAAGap(int pos, List<SequenceI> proteinSeqs)
753 for (SequenceI seq : proteinSeqs)
755 seq.insertCharAt(pos, gapChar);
758 checkCodonFrameWidth();
764 * Shift from [pos] to the end one to the right, and null out [pos]
766 System.arraycopy(alignedCodons, pos, alignedCodons, pos + 1,
767 alignedCodons.length - pos - 1);
768 alignedCodons[pos] = null;
773 * Check the codons array can accommodate a single insertion, if not resize
776 protected void checkCodonFrameWidth()
778 if (alignedCodons[alignedCodons.length - 1] != null)
781 * arraycopy insertion would bump a filled slot off the end, so expand.
783 AlignedCodon[] c = new AlignedCodon[alignedCodons.length + 10];
784 System.arraycopy(alignedCodons, 0, c, 0, alignedCodons.length);
790 * Given a peptide newly translated from a dna sequence, copy over and set any
791 * features on the peptide from the DNA.
797 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
800 // BH 2019.01.25 nop?
801 // List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
802 // DBRefSource.DNACODINGDBS);
803 // if (dnarefs != null)
805 // // intersect with pep
806 // for (int d = 0, nd = dnarefs.size(); d < nd; d++)
808 // Mapping mp = dnarefs.get(d).getMap();
814 for (SequenceFeature sf : dna.getFeatures().getAllFeatures())
816 if (FeatureProperties.isCodingFeature(null, sf.getType()))
818 // if (map.intersectsFrom(sf[f].begin, sf[f].end))
827 * Returns an alignment consisting of the reversed (and optionally
828 * complemented) sequences set in this object's constructor
833 public AlignmentI reverseCdna(boolean complement)
835 int sSize = selection.size();
836 List<SequenceI> reversed = new ArrayList<>();
837 for (int s = 0; s < sSize; s++)
839 SequenceI newseq = reverseSequence(selection.get(s).getName(),
840 seqstring[s], complement);
844 reversed.add(newseq);
848 SequenceI[] newseqs = reversed.toArray(new SequenceI[reversed.size()]);
849 AlignmentI al = new Alignment(newseqs);
850 ((Alignment) al).createDatasetAlignment();
855 * Returns a reversed, and optionally complemented, sequence. The new
856 * sequence's name is the original name with "|rev" or "|revcomp" appended.
857 * aAcCgGtT and DNA ambiguity codes are complemented, any other characters are
864 public static SequenceI reverseSequence(String seqName, String sequence,
867 String newName = seqName + "|rev" + (complement ? "comp" : "");
868 char[] originalSequence = sequence.toCharArray();
869 int length = originalSequence.length;
870 char[] reversedSequence = new char[length];
872 for (int i = 0; i < length; i++)
874 char c = complement ? getComplement(originalSequence[i])
875 : originalSequence[i];
876 reversedSequence[length - i - 1] = c;
877 if (!Comparison.isGap(c))
882 SequenceI reversed = new Sequence(newName, reversedSequence, 1, bases);
887 * Answers the reverse complement of the input string
889 * @see #getComplement(char)
893 public static String reverseComplement(String s)
895 StringBuilder sb = new StringBuilder(s.length());
896 for (int i = s.length() - 1; i >= 0; i--)
898 sb.append(Dna.getComplement(s.charAt(i)));
900 return sb.toString();
904 * Returns dna complement (preserving case) for aAcCgGtTuU. Ambiguity codes
905 * are treated as on http://reverse-complement.com/. Anything else is left
911 public static char getComplement(char c)