2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.bin.Cache;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.DBRefEntry;
30 import jalview.datamodel.DBRefSource;
31 import jalview.datamodel.FeatureProperties;
32 import jalview.datamodel.GraphLine;
33 import jalview.datamodel.Mapping;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceFeature;
36 import jalview.datamodel.SequenceI;
37 import jalview.schemes.ResidueProperties;
38 import jalview.util.Comparison;
39 import jalview.util.DBRefUtils;
40 import jalview.util.MapList;
41 import jalview.util.ShiftList;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Hashtable;
46 import java.util.List;
51 * Test whether codon positions cdp1 should align before, with, or after cdp2.
52 * Returns zero if all positions match (or either argument is null). Returns
53 * -1 if any position in the first codon precedes the corresponding position
54 * in the second codon. Else returns +1 (some position in the second codon
55 * precedes the corresponding position in the first).
57 * Note this is not necessarily symmetric, for example:
59 * <li>compareCodonPos([2,5,6], [3,4,5]) returns -1</li>
60 * <li>compareCodonPos([3,4,5], [2,5,6]) also returns -1</li>
67 public static int compareCodonPos(int[] cdp1, int[] cdp2)
71 || (cdp1[0] == cdp2[0] && cdp1[1] == cdp2[1] && cdp1[2] == cdp2[2]))
75 if (cdp1[0] < cdp2[0] || cdp1[1] < cdp2[1] || cdp1[2] < cdp2[2])
77 // one base in cdp1 precedes the corresponding base in the other codon
80 // one base in cdp1 appears after the corresponding base in the other codon.
85 * DNA->mapped protein sequence alignment translation given set of sequences
86 * 1. id distinct coding regions within selected region for each sequence 2.
87 * generate peptides based on inframe (or given) translation or (optionally
88 * and where specified) out of frame translations (annotated appropriately) 3.
89 * align peptides based on codon alignment
92 * id potential products from dna 1. search for distinct products within
93 * selected region for each selected sequence 2. group by associated DB type.
94 * 3. return as form for input into above function
100 * create a new alignment of protein sequences by an inframe translation of
101 * the provided NA sequences
106 * @param gapCharacter
110 * destination dataset for translated sequences and mappings
113 public static AlignmentI cdnaTranslate(SequenceI[] selection,
114 String[] seqstring, int viscontigs[], char gapCharacter,
115 AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)
117 return cdnaTranslate(Arrays.asList(selection), seqstring, null,
118 viscontigs, gapCharacter, annotations, aWidth, dataset);
126 * - array of DbRefEntry objects from which exon map in seqstring is
129 * @param gapCharacter
135 public static AlignmentI cdnaTranslate(List<SequenceI> cdnaseqs,
136 String[] seqstring, DBRefEntry[] product, int viscontigs[],
137 char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,
140 AlignedCodonFrame acf = new AlignedCodonFrame(aWidth);
143 * This array will be built up so that position i holds the codon positions
144 * e.g. [7, 9, 10] that match column i (base 0) in the aligned translation.
145 * Note this implies a contract that if two codons do not align exactly,
146 * their translated products must occupy different column positions.
148 int[][] alignedCodons = new int[aWidth][];
151 int sSize = cdnaseqs.size();
152 List<SequenceI> pepseqs = new ArrayList<SequenceI>();
153 for (s = 0; s < sSize; s++)
155 SequenceI newseq = translateCodingRegion(cdnaseqs.get(s),
156 seqstring[s], viscontigs, acf, alignedCodons, pepseqs,
163 SequenceI ds = newseq;
166 while (ds.getDatasetSequence() != null)
168 ds = ds.getDatasetSequence();
170 dataset.addSequence(ds);
175 SequenceI[] newseqs = pepseqs.toArray(new SequenceI[pepseqs.size()]);
176 AlignmentI al = new Alignment(newseqs);
177 // ensure we look aligned.
179 // link the protein translation to the DNA dataset
180 al.setDataset(dataset);
181 translateAlignedAnnotations(annotations, al, acf, alignedCodons);
182 al.addCodonFrame(acf);
187 * fake the collection of DbRefs with associated exon mappings to identify if
188 * a translation would generate distinct product in the currently selected
195 public static boolean canTranslate(SequenceI[] selection,
198 for (int gd = 0; gd < selection.length; gd++)
200 SequenceI dna = selection[gd];
201 DBRefEntry[] dnarefs = DBRefUtils
202 .selectRefs(dna.getDBRef(),
203 jalview.datamodel.DBRefSource.DNACODINGDBS);
206 // intersect with pep
207 List<DBRefEntry> mappedrefs = new ArrayList<DBRefEntry>();
208 DBRefEntry[] refs = dna.getDBRef();
209 for (int d = 0; d < refs.length; d++)
211 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
212 && refs[d].getMap().getMap().getFromRatio() == 3
213 && refs[d].getMap().getMap().getToRatio() == 1)
215 mappedrefs.add(refs[d]); // add translated protein maps
218 dnarefs = mappedrefs.toArray(new DBRefEntry[mappedrefs.size()]);
219 for (int d = 0; d < dnarefs.length; d++)
221 Mapping mp = dnarefs[d].getMap();
224 for (int vc = 0; vc < viscontigs.length; vc += 2)
226 int[] mpr = mp.locateMappedRange(viscontigs[vc],
241 * Translate na alignment annotations onto translated amino acid alignment al
242 * using codon mapping codons
248 protected static void translateAlignedAnnotations(
249 AlignmentAnnotation[] annotations, AlignmentI al,
250 AlignedCodonFrame acf, int[][] codons)
252 // Can only do this for columns with consecutive codons, or where
253 // annotation is sequence associated.
255 if (annotations != null)
257 for (AlignmentAnnotation annotation : annotations)
260 * Skip hidden or autogenerated annotation. Also (for now), RNA
261 * secondary structure annotation. If we want to show this against
262 * protein we need a smarter way to 'translate' without generating
263 * invalid (unbalanced) structure annotation.
265 if (annotation.autoCalculated || !annotation.visible
266 || annotation.isRNA())
271 int aSize = acf.getaaWidth(); // aa alignment width.
272 Annotation[] anots = (annotation.annotations == null) ? null
273 : new Annotation[aSize];
276 for (int a = 0; a < aSize; a++)
278 // process through codon map.
279 if (a < codons.length && codons[a] != null
280 && codons[a][0] == (codons[a][2] - 2))
282 anots[a] = getCodonAnnotation(codons[a],
283 annotation.annotations);
288 AlignmentAnnotation aa = new AlignmentAnnotation(annotation.label,
289 annotation.description, anots);
290 aa.graph = annotation.graph;
291 aa.graphGroup = annotation.graphGroup;
292 aa.graphHeight = annotation.graphHeight;
293 if (annotation.getThreshold() != null)
295 aa.setThreshold(new GraphLine(annotation
298 if (annotation.hasScore)
300 aa.setScore(annotation.getScore());
303 final SequenceI seqRef = annotation.sequenceRef;
306 SequenceI aaSeq = acf.getAaForDnaSeq(seqRef);
309 // aa.compactAnnotationArray(); // throw away alignment annotation
311 aa.setSequenceRef(aaSeq);
313 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true);
314 aa.adjustForAlignment();
315 aaSeq.addAlignmentAnnotation(aa);
318 al.addAnnotation(aa);
323 private static Annotation getCodonAnnotation(int[] is,
324 Annotation[] annotations)
326 // Have a look at all the codon positions for annotation and put the first
327 // one found into the translated annotation pos.
329 Annotation annot = null;
330 for (int p = 0; p < 3; p++)
332 if (annotations[is[p]] != null)
336 annot = new Annotation(annotations[is[p]]);
342 Annotation cpy = new Annotation(annotations[is[p]]);
343 if (annot.colour == null)
345 annot.colour = cpy.colour;
347 if (annot.description == null || annot.description.length() == 0)
349 annot.description = cpy.description;
351 if (annot.displayCharacter == null)
353 annot.displayCharacter = cpy.displayCharacter;
355 if (annot.secondaryStructure == 0)
357 annot.secondaryStructure = cpy.secondaryStructure;
359 annot.value += cpy.value;
366 annot.value /= contrib;
372 * Translate a na sequence
375 * sequence displayed under viscontigs visible columns
377 * ORF read in some global alignment reference frame
379 * mapping from global reference frame to visible seqstring ORF read
381 * Definition of global ORF alignment reference frame
382 * @param alignedCodons
384 * @param gapCharacter
386 * when true stop codons will translate as '*', otherwise as 'X'
387 * @return sequence ready to be added to alignment.
389 protected static SequenceI translateCodingRegion(SequenceI selection,
390 String seqstring, int[] viscontigs, AlignedCodonFrame acf,
391 int[][] alignedCodons, List<SequenceI> proteinSeqs,
393 final boolean starForStop)
395 List<int[]> skip = new ArrayList<int[]>();
396 int skipint[] = null;
397 ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
399 int vc, scontigs[] = new int[viscontigs.length];
401 for (vc = 0; vc < viscontigs.length; vc += 2)
405 vismapping.addShift(npos, viscontigs[vc]);
410 vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
412 scontigs[vc] = viscontigs[vc];
413 scontigs[vc + 1] = viscontigs[vc + 1];
416 // allocate a roughly sized buffer for the protein sequence
417 StringBuilder protein = new StringBuilder(seqstring.length() / 2);
418 String seq = seqstring.replace('U', 'T').replace('u', 'T');
419 char codon[] = new char[3];
420 int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
423 for (npos = 0, nend = seq.length(); npos < nend; npos++)
425 if (!Comparison.isGap(seq.charAt(npos)))
427 cdp[rf] = npos; // store position
428 codon[rf++] = seq.charAt(npos); // store base
433 * Filled up a reading frame...
435 String aa = ResidueProperties.codonTranslate(new String(codon));
437 final String gapString = String.valueOf(gapCharacter);
453 skipint[0] = vismapping.shift(skipint[0]);
454 skipint[1] = vismapping.shift(skipint[1]);
455 for (vc = 0; vc < scontigs.length;)
457 if (scontigs[vc + 1] < skipint[0])
459 // before skipint starts
463 if (scontigs[vc] > skipint[1])
465 // finished editing so
468 // Edit the contig list to include the skipped region which did
471 // from : s1 e1 s2 e2 s3 e3
472 // to s: s1 e1 s2 k0 k1 e2 s3 e3
473 // list increases by one unless one boundary (s2==k0 or e2==k1)
474 // matches, and decreases by one if skipint intersects whole
476 if (scontigs[vc] <= skipint[0])
478 if (skipint[0] == scontigs[vc])
480 // skipint at start of contig
481 // shift the start of this contig
482 if (scontigs[vc + 1] > skipint[1])
484 scontigs[vc] = skipint[1];
489 if (scontigs[vc + 1] == skipint[1])
492 t = new int[scontigs.length - 2];
495 System.arraycopy(scontigs, 0, t, 0, vc - 1);
497 if (vc + 2 < t.length)
499 System.arraycopy(scontigs, vc + 2, t, vc, t.length
506 // truncate contig to before the skipint region
507 scontigs[vc + 1] = skipint[0] - 1;
514 // scontig starts before start of skipint
515 if (scontigs[vc + 1] < skipint[1])
517 // skipint truncates end of scontig
518 scontigs[vc + 1] = skipint[0] - 1;
523 // divide region to new contigs
524 t = new int[scontigs.length + 2];
525 System.arraycopy(scontigs, 0, t, 0, vc + 1);
526 t[vc + 1] = skipint[0];
527 t[vc + 2] = skipint[1];
528 System.arraycopy(scontigs, vc + 1, t, vc + 3,
529 scontigs.length - (vc + 1));
539 if (aa.equals("STOP"))
541 aa = starForStop ? "*" : "X";
545 // insert gaps prior to this codon - if necessary
546 boolean findpos = true;
549 // expand the codons array if necessary
550 alignedCodons = checkCodonFrameWidth(alignedCodons, aspos);
553 * Compare this codon's base positions with those currently aligned to
554 * this column in the translation.
556 final int compareCodonPos = Dna.compareCodonPos(cdp,
557 alignedCodons[aspos]);
559 System.out.println(seq + "/" + aa + " codons: "
560 + Arrays.deepToString(alignedCodons));
562 .println(("Compare " + Arrays.toString(cdp) + " at pos "
564 + Arrays.toString(alignedCodons[aspos]) + " got " + compareCodonPos));
566 switch (compareCodonPos)
571 * This codon should precede the mapped positions - need to insert a
572 * gap in all prior sequences.
574 insertAAGap(aspos, gapCharacter, alignedCodons, proteinSeqs);
581 * This codon belongs after the aligned codons at aspos. Prefix it
582 * with a gap and try the next position.
591 * Exact match - codon 'belongs' at this translated position.
598 if (alignedCodons[aspos] == null)
600 // mark this column as aligning to this aligned reading frame
601 alignedCodons[aspos] = new int[]
602 { cdp[0], cdp[1], cdp[2] };
604 else if (!Arrays.equals(alignedCodons[aspos], cdp))
606 throw new IllegalStateException("Tried to coalign "
607 + Arrays.asList(alignedCodons[aspos], cdp));
609 if (aspos >= acf.aaWidth)
611 // update maximum alignment width
612 // (we can do this without calling checkCodonFrameWidth because it was
613 // already done above)
614 System.out.println("aspos " + aspos + " >= " + acf.aaWidth);
615 acf.setAaWidth(aspos);
617 // ready for next translated reading frame alignment position (if any)
623 SequenceI newseq = new Sequence(selection.getName(),
627 final String errMsg = "trimming contigs for incomplete terminal codon.";
628 if (Cache.log != null)
630 Cache.log.debug(errMsg);
634 System.err.println(errMsg);
636 // map and trim contigs to ORF region
637 vc = scontigs.length - 1;
638 lastnpos = vismapping.shift(lastnpos); // place npos in context of
639 // whole dna alignment (rather
640 // than visible contigs)
641 // incomplete ORF could be broken over one or two visible contig
643 while (vc >= 0 && scontigs[vc] > lastnpos)
645 if (vc > 0 && scontigs[vc - 1] > lastnpos)
651 // correct last interval in list.
652 scontigs[vc] = lastnpos;
656 if (vc > 0 && (vc + 1) < scontigs.length)
658 // truncate map list to just vc elements
659 int t[] = new int[vc + 1];
660 System.arraycopy(scontigs, 0, t, 0, vc + 1);
668 if (scontigs != null)
671 // map scontigs to actual sequence positions on selection
672 for (vc = 0; vc < scontigs.length; vc += 2)
674 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
675 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
676 if (scontigs[vc + 1] == selection.getEnd())
681 // trim trailing empty intervals.
682 if ((vc + 2) < scontigs.length)
684 int t[] = new int[vc + 2];
685 System.arraycopy(scontigs, 0, t, 0, vc + 2);
689 * delete intervals in scontigs which are not translated. 1. map skip
690 * into sequence position intervals 2. truncate existing ranges and add
691 * new ranges to exclude untranslated regions. if (skip.size()>0) {
692 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
693 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
694 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
695 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
696 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
697 * range } else { // truncate range and create new one if necessary iv =
698 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
699 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
700 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
702 MapList map = new MapList(scontigs, new int[]
703 { 1, resSize }, 3, 1);
705 transferCodedFeatures(selection, newseq, map, null, null);
708 * Construct a dataset sequence for our new peptide.
710 SequenceI rseq = newseq.deriveSequence();
713 * Store a mapping (between the dataset sequences for the two
716 // SIDE-EFFECT: acf stores the aligned sequence reseq; to remove!
717 acf.addMap(selection, rseq, map);
721 // register the mapping somehow
727 * Insert a gap into the aligned proteins and the codon mapping array.
730 * @param gapCharacter
731 * @param alignedCodons
734 protected static void insertAAGap(int pos, char gapCharacter,
735 int[][] alignedCodons, List<SequenceI> proteinSeqs)
737 System.out.println("insertAAGap " + pos + "/" + proteinSeqs.size());
739 for (SequenceI seq : proteinSeqs)
741 seq.insertCharAt(pos, gapCharacter);
744 // if (pos < aaWidth)
747 System.arraycopy(alignedCodons, pos, alignedCodons, pos + 1,
748 alignedCodons.length - pos - 1);
749 alignedCodons[pos] = null; // clear so new codon position can be marked.
754 * Check the codons array is big enough to accommodate the given position, if
757 * @param alignedCodons
759 * @return the resized array (or the original if no resize needed)
761 protected static int[][] checkCodonFrameWidth(int[][] alignedCodons,
764 // TODO why not codons.length < aspos ?
765 // should codons expand if length is 2 or 3 and aslen==2 ?
766 if (alignedCodons.length <= aspos + 1)
768 // probably never have to do this ?
769 int[][] c = new int[alignedCodons.length + 10][];
770 for (int i = 0; i < alignedCodons.length; i++)
772 c[i] = alignedCodons[i];
776 return alignedCodons;
780 * Given a peptide newly translated from a dna sequence, copy over and set any
781 * features on the peptide from the DNA. If featureTypes is null, all features
782 * on the dna sequence are searched (rather than just the displayed ones), and
783 * similarly for featureGroups.
788 * @param featureTypes
789 * hash who's keys are the displayed feature type strings
790 * @param featureGroups
791 * hash where keys are feature groups and values are Boolean objects
792 * indicating if they are displayed.
794 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
795 MapList map, Hashtable featureTypes, Hashtable featureGroups)
797 SequenceFeature[] sf = (dna.getDatasetSequence() != null ? dna
798 .getDatasetSequence() : dna).getSequenceFeatures();
800 DBRefEntry[] dnarefs = DBRefUtils.selectRefs(dna.getDBRef(),
801 DBRefSource.DNACODINGDBS);
804 // intersect with pep
805 for (int d = 0; d < dnarefs.length; d++)
807 Mapping mp = dnarefs[d].getMap();
815 for (int f = 0; f < sf.length; f++)
817 fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
818 .get(sf[f].featureGroup));
819 if ((featureTypes == null || featureTypes.containsKey(sf[f]
820 .getType())) && (fgstate == null || fgstate.booleanValue()))
822 if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
824 // if (map.intersectsFrom(sf[f].begin, sf[f].end))