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.datamodel.AlignedCodonFrame;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.FeatureProperties;
30 import jalview.datamodel.GraphLine;
31 import jalview.datamodel.Mapping;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceI;
35 import jalview.schemes.ResidueProperties;
36 import jalview.util.Comparison;
37 import jalview.util.MapList;
38 import jalview.util.ShiftList;
40 import java.util.ArrayList;
41 import java.util.Hashtable;
42 import java.util.List;
43 import java.util.Vector;
51 * @return -1 if cdp1 aligns before cdp2, 0 if in the same column or cdp2 is
52 * null, +1 if after cdp2
54 private static int compare_codonpos(int[] cdp1, int[] cdp2)
57 || (cdp1[0] == cdp2[0] && cdp1[1] == cdp2[1] && cdp1[2] == cdp2[2]))
61 if (cdp1[0] < cdp2[0] || cdp1[1] < cdp2[1] || cdp1[2] < cdp2[2])
63 return -1; // one base in cdp1 precedes the corresponding base in the
66 return 1; // one base in cdp1 appears after the corresponding base in the
71 * DNA->mapped protein sequence alignment translation given set of sequences
72 * 1. id distinct coding regions within selected region for each sequence 2.
73 * generate peptides based on inframe (or given) translation or (optionally
74 * and where specified) out of frame translations (annotated appropriately) 3.
75 * align peptides based on codon alignment
78 * id potential products from dna 1. search for distinct products within
79 * selected region for each selected sequence 2. group by associated DB type.
80 * 3. return as form for input into above function
86 * create a new alignment of protein sequences by an inframe translation of
87 * the provided NA sequences
96 * destination dataset for translated sequences and mappings
99 public static AlignmentI CdnaTranslate(SequenceI[] selection,
100 String[] seqstring, int viscontigs[], char gapCharacter,
101 AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)
103 return CdnaTranslate(selection, seqstring, null, viscontigs,
104 gapCharacter, annotations, aWidth, dataset);
112 * - array of DbRefEntry objects from which exon map in seqstring is
115 * @param gapCharacter
121 public static AlignmentI CdnaTranslate(SequenceI[] selection,
122 String[] seqstring, DBRefEntry[] product, int viscontigs[],
123 char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,
126 AlignedCodonFrame codons = new AlignedCodonFrame(aWidth); // stores hash of
132 int s, sSize = selection.length;
133 Vector pepseqs = new Vector();
134 for (s = 0; s < sSize; s++)
136 SequenceI newseq = translateCodingRegion(selection[s], seqstring[s],
137 viscontigs, codons, gapCharacter,
138 (product != null) ? product[s] : null, false); // possibly
143 pepseqs.addElement(newseq);
144 SequenceI ds = newseq;
147 while (ds.getDatasetSequence() != null)
149 ds = ds.getDatasetSequence();
151 dataset.addSequence(ds);
155 if (codons.aaWidth == 0)
159 SequenceI[] newseqs = new SequenceI[pepseqs.size()];
160 pepseqs.copyInto(newseqs);
161 AlignmentI al = new Alignment(newseqs);
162 al.padGaps(); // ensure we look aligned.
163 al.setDataset(dataset);
164 translateAlignedAnnotations(annotations, al, codons);
165 al.addCodonFrame(codons);
170 * fake the collection of DbRefs with associated exon mappings to identify if
171 * a translation would generate distinct product in the currently selected
178 public static boolean canTranslate(SequenceI[] selection,
181 for (int gd = 0; gd < selection.length; gd++)
183 SequenceI dna = selection[gd];
184 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
185 .selectRefs(dna.getDBRef(),
186 jalview.datamodel.DBRefSource.DNACODINGDBS);
189 // intersect with pep
190 // intersect with pep
191 Vector mappedrefs = new Vector();
192 DBRefEntry[] refs = dna.getDBRef();
193 for (int d = 0; d < refs.length; d++)
195 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
196 && refs[d].getMap().getMap().getFromRatio() == 3
197 && refs[d].getMap().getMap().getToRatio() == 1)
199 mappedrefs.addElement(refs[d]); // add translated protein maps
202 dnarefs = new DBRefEntry[mappedrefs.size()];
203 mappedrefs.copyInto(dnarefs);
204 for (int d = 0; d < dnarefs.length; d++)
206 Mapping mp = dnarefs[d].getMap();
209 for (int vc = 0; vc < viscontigs.length; vc += 2)
211 int[] mpr = mp.locateMappedRange(viscontigs[vc],
226 * generate a set of translated protein products from annotated sequenceI
230 * @param gapCharacter
232 * destination dataset for translated sequences
237 public static AlignmentI CdnaTranslate(SequenceI[] selection,
238 int viscontigs[], char gapCharacter, Alignment dataset)
241 Vector cdnasqs = new Vector();
242 Vector cdnasqi = new Vector();
243 Vector cdnaprod = new Vector();
244 for (int gd = 0; gd < selection.length; gd++)
246 SequenceI dna = selection[gd];
247 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
248 .selectRefs(dna.getDBRef(),
249 jalview.datamodel.DBRefSource.DNACODINGDBS);
252 // intersect with pep
253 Vector mappedrefs = new Vector();
254 DBRefEntry[] refs = dna.getDBRef();
255 for (int d = 0; d < refs.length; d++)
257 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
258 && refs[d].getMap().getMap().getFromRatio() == 3
259 && refs[d].getMap().getMap().getToRatio() == 1)
261 mappedrefs.addElement(refs[d]); // add translated protein maps
264 dnarefs = new DBRefEntry[mappedrefs.size()];
265 mappedrefs.copyInto(dnarefs);
266 for (int d = 0; d < dnarefs.length; d++)
268 Mapping mp = dnarefs[d].getMap();
269 StringBuffer sqstr = new StringBuffer();
272 Mapping intersect = mp.intersectVisContigs(viscontigs);
273 // generate seqstring for this sequence based on mapping
275 if (sqstr.length() > alwidth)
277 alwidth = sqstr.length();
279 cdnasqs.addElement(sqstr.toString());
280 cdnasqi.addElement(dna);
281 cdnaprod.addElement(intersect);
285 SequenceI[] cdna = new SequenceI[cdnasqs.size()];
286 DBRefEntry[] prods = new DBRefEntry[cdnaprod.size()];
287 String[] xons = new String[cdnasqs.size()];
288 cdnasqs.copyInto(xons);
289 cdnaprod.copyInto(prods);
290 cdnasqi.copyInto(cdna);
291 return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,
292 null, alwidth, dataset);
298 * Translate na alignment annotations onto translated amino acid alignment al
299 * using codon mapping codons
305 public static void translateAlignedAnnotations(
306 AlignmentAnnotation[] annotations, AlignmentI al,
307 AlignedCodonFrame codons)
309 // Can only do this for columns with consecutive codons, or where
310 // annotation is sequence associated.
312 if (annotations != null)
314 for (AlignmentAnnotation annotation : annotations)
317 * Skip hidden or autogenerated annotation. Also (for now), RNA
318 * secondary structure annotation. If we want to show this against
319 * protein we need a smarter way to 'translate' without generating
320 * invalid (unbalanced) structure annotation.
322 if (annotation.autoCalculated || !annotation.visible
323 || annotation.isRNA())
328 int aSize = codons.getaaWidth(); // aa alignment width.
329 Annotation[] anots = (annotation.annotations == null) ? null
330 : new Annotation[aSize];
333 for (int a = 0; a < aSize; a++)
335 // process through codon map.
336 if (a < codons.codons.length && codons.codons[a] != null
337 && codons.codons[a][0] == (codons.codons[a][2] - 2))
339 anots[a] = getCodonAnnotation(codons.codons[a],
340 annotation.annotations);
345 AlignmentAnnotation aa = new AlignmentAnnotation(annotation.label,
346 annotation.description, anots);
347 aa.graph = annotation.graph;
348 aa.graphGroup = annotation.graphGroup;
349 aa.graphHeight = annotation.graphHeight;
350 if (annotation.getThreshold() != null)
352 aa.setThreshold(new GraphLine(annotation
355 if (annotation.hasScore)
357 aa.setScore(annotation.getScore());
360 final SequenceI seqRef = annotation.sequenceRef;
363 SequenceI aaSeq = codons.getAaForDnaSeq(seqRef);
366 // aa.compactAnnotationArray(); // throw away alignment annotation
368 aa.setSequenceRef(aaSeq);
370 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true);
371 aa.adjustForAlignment();
372 aaSeq.addAlignmentAnnotation(aa);
375 al.addAnnotation(aa);
380 private static Annotation getCodonAnnotation(int[] is,
381 Annotation[] annotations)
383 // Have a look at all the codon positions for annotation and put the first
384 // one found into the translated annotation pos.
386 Annotation annot = null;
387 for (int p = 0; p < 3; p++)
389 if (annotations[is[p]] != null)
393 annot = new Annotation(annotations[is[p]]);
399 Annotation cpy = new Annotation(annotations[is[p]]);
400 if (annot.colour == null)
402 annot.colour = cpy.colour;
404 if (annot.description == null || annot.description.length() == 0)
406 annot.description = cpy.description;
408 if (annot.displayCharacter == null)
410 annot.displayCharacter = cpy.displayCharacter;
412 if (annot.secondaryStructure == 0)
414 annot.secondaryStructure = cpy.secondaryStructure;
416 annot.value += cpy.value;
423 annot.value /= contrib;
429 * Translate a na sequence
432 * sequence displayed under viscontigs visible columns
434 * ORF read in some global alignment reference frame
436 * mapping from global reference frame to visible seqstring ORF read
438 * Definition of global ORF alignment reference frame
439 * @param gapCharacter
440 * @return sequence ready to be added to alignment.
442 * {@link #translateCodingRegion(SequenceI,String,int[],AlignedCodonFrame,char,DBRefEntry,boolean)}
446 public static SequenceI translateCodingRegion(SequenceI selection,
447 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
448 char gapCharacter, DBRefEntry product)
450 return translateCodingRegion(selection, seqstring, viscontigs, codons,
451 gapCharacter, product, false);
455 * Translate a na sequence
458 * sequence displayed under viscontigs visible columns
460 * ORF read in some global alignment reference frame
462 * mapping from global reference frame to visible seqstring ORF read
464 * Definition of global ORF alignment reference frame
465 * @param gapCharacter
467 * when true stop codons will translate as '*', otherwise as 'X'
468 * @return sequence ready to be added to alignment.
470 public static SequenceI translateCodingRegion(SequenceI selection,
471 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
472 char gapCharacter, DBRefEntry product, final boolean starForStop)
474 List<int[]> skip = new ArrayList<int[]>();
475 int skipint[] = null;
476 ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
478 int vc, scontigs[] = new int[viscontigs.length];
480 for (vc = 0; vc < viscontigs.length; vc += 2)
484 vismapping.addShift(npos, viscontigs[vc]);
489 vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
491 scontigs[vc] = viscontigs[vc];
492 scontigs[vc + 1] = viscontigs[vc + 1];
495 // allocate a roughly sized buffer for the protein sequence
496 StringBuilder protein = new StringBuilder(seqstring.length() / 2);
497 String seq = seqstring.replace('U', 'T');
498 char codon[] = new char[3];
499 int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
502 for (npos = 0, nend = seq.length(); npos < nend; npos++)
504 if (!Comparison.isGap(seq.charAt(npos)))
506 cdp[rf] = npos; // store position
507 codon[rf++] = seq.charAt(npos); // store base
512 * Filled up a reading frame...
514 String aa = ResidueProperties.codonTranslate(new String(codon));
518 aa = String.valueOf(gapCharacter);
531 skipint[0] = vismapping.shift(skipint[0]);
532 skipint[1] = vismapping.shift(skipint[1]);
533 for (vc = 0; vc < scontigs.length;)
535 if (scontigs[vc + 1] < skipint[0])
537 // before skipint starts
541 if (scontigs[vc] > skipint[1])
543 // finished editing so
546 // Edit the contig list to include the skipped region which did
549 // from : s1 e1 s2 e2 s3 e3
550 // to s: s1 e1 s2 k0 k1 e2 s3 e3
551 // list increases by one unless one boundary (s2==k0 or e2==k1)
552 // matches, and decreases by one if skipint intersects whole
554 if (scontigs[vc] <= skipint[0])
556 if (skipint[0] == scontigs[vc])
558 // skipint at start of contig
559 // shift the start of this contig
560 if (scontigs[vc + 1] > skipint[1])
562 scontigs[vc] = skipint[1];
567 if (scontigs[vc + 1] == skipint[1])
570 t = new int[scontigs.length - 2];
573 System.arraycopy(scontigs, 0, t, 0, vc - 1);
575 if (vc + 2 < t.length)
577 System.arraycopy(scontigs, vc + 2, t, vc, t.length
584 // truncate contig to before the skipint region
585 scontigs[vc + 1] = skipint[0] - 1;
592 // scontig starts before start of skipint
593 if (scontigs[vc + 1] < skipint[1])
595 // skipint truncates end of scontig
596 scontigs[vc + 1] = skipint[0] - 1;
601 // divide region to new contigs
602 t = new int[scontigs.length + 2];
603 System.arraycopy(scontigs, 0, t, 0, vc + 1);
604 t[vc + 1] = skipint[0];
605 t[vc + 2] = skipint[1];
606 System.arraycopy(scontigs, vc + 1, t, vc + 3,
607 scontigs.length - (vc + 1));
617 if (aa.equals("STOP"))
619 aa = starForStop ? "*" : "X";
623 // insert/delete gaps prior to this codon - if necessary
624 boolean findpos = true;
627 // first ensure that the codons array is long enough.
628 codons.checkCodonFrameWidth(aspos);
629 // now check to see if we place the aa at the current aspos in the
631 switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
634 codons.insertAAGap(aspos, gapCharacter);
638 // this aa appears after the aligned codons at aspos, so prefix it
640 aa = "" + gapCharacter + aa;
642 // if (aspos >= codons.aaWidth)
643 // codons.aaWidth = aspos + 1;
644 break; // check the next position for alignment
646 // codon aligns at aspos position.
650 // codon aligns with all other sequence residues found at aspos
653 if (codons.codons[aspos] == null)
655 // mark this column as aligning to this aligned reading frame
656 codons.codons[aspos] = new int[]
657 { cdp[0], cdp[1], cdp[2] };
659 if (aspos >= codons.aaWidth)
661 // update maximum alignment width
662 // (we can do this without calling checkCodonFrameWidth because it was
663 // already done above)
664 codons.setAaWidth(aspos);
666 // ready for next translated reading frame alignment position (if any)
672 SequenceI newseq = new Sequence(selection.getName(),
676 if (jalview.bin.Cache.log != null)
678 jalview.bin.Cache.log
679 .debug("trimming contigs for incomplete terminal codon.");
684 .println("trimming contigs for incomplete terminal codon.");
686 // map and trim contigs to ORF region
687 vc = scontigs.length - 1;
688 lastnpos = vismapping.shift(lastnpos); // place npos in context of
689 // whole dna alignment (rather
690 // than visible contigs)
691 // incomplete ORF could be broken over one or two visible contig
693 while (vc >= 0 && scontigs[vc] > lastnpos)
695 if (vc > 0 && scontigs[vc - 1] > lastnpos)
701 // correct last interval in list.
702 scontigs[vc] = lastnpos;
706 if (vc > 0 && (vc + 1) < scontigs.length)
708 // truncate map list to just vc elements
709 int t[] = new int[vc + 1];
710 System.arraycopy(scontigs, 0, t, 0, vc + 1);
718 if (scontigs != null)
721 // map scontigs to actual sequence positions on selection
722 for (vc = 0; vc < scontigs.length; vc += 2)
724 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
725 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
726 if (scontigs[vc + 1] == selection.getEnd())
731 // trim trailing empty intervals.
732 if ((vc + 2) < scontigs.length)
734 int t[] = new int[vc + 2];
735 System.arraycopy(scontigs, 0, t, 0, vc + 2);
739 * delete intervals in scontigs which are not translated. 1. map skip
740 * into sequence position intervals 2. truncate existing ranges and add
741 * new ranges to exclude untranslated regions. if (skip.size()>0) {
742 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
743 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
744 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
745 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
746 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
747 * range } else { // truncate range and create new one if necessary iv =
748 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
749 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
750 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
752 MapList map = new MapList(scontigs, new int[]
753 { 1, resSize }, 3, 1);
755 // update newseq as if it was generated as mapping from product
759 newseq.setName(product.getSource() + "|"
760 + product.getAccessionId());
761 if (product.getMap() != null)
763 // Mapping mp = product.getMap();
764 // newseq.setStart(mp.getPosition(scontigs[0]));
766 // .getPosition(scontigs[scontigs.length - 1]));
769 transferCodedFeatures(selection, newseq, map, null, null);
770 SequenceI rseq = newseq.deriveSequence(); // construct a dataset
771 // sequence for our new
772 // peptide, regardless.
773 // store a mapping (this actually stores a mapping between the dataset
774 // sequences for the two sequences
775 codons.addMap(selection, rseq, map);
779 // register the mapping somehow
785 * Given a peptide newly translated from a dna sequence, copy over and set any
786 * features on the peptide from the DNA. If featureTypes is null, all features
787 * on the dna sequence are searched (rather than just the displayed ones), and
788 * similarly for featureGroups.
793 * @param featureTypes
794 * hash who's keys are the displayed feature type strings
795 * @param featureGroups
796 * hash where keys are feature groups and values are Boolean objects
797 * indicating if they are displayed.
799 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
800 MapList map, Hashtable featureTypes, Hashtable featureGroups)
802 SequenceFeature[] sf = (dna.getDatasetSequence() != null ? dna
803 .getDatasetSequence() : dna).getSequenceFeatures();
805 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
806 .selectRefs(dna.getDBRef(),
807 jalview.datamodel.DBRefSource.DNACODINGDBS);
810 // intersect with pep
811 for (int d = 0; d < dnarefs.length; d++)
813 Mapping mp = dnarefs[d].getMap();
821 for (int f = 0; f < sf.length; f++)
823 fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
824 .get(sf[f].featureGroup));
825 if ((featureTypes == null || featureTypes.containsKey(sf[f]
826 .getType())) && (fgstate == null || fgstate.booleanValue()))
828 if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
830 // if (map.intersectsFrom(sf[f].begin, sf[f].end))