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 java.util.ArrayList;
24 import java.util.Hashtable;
25 import java.util.Vector;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.DBRefEntry;
33 import jalview.datamodel.FeatureProperties;
34 import jalview.datamodel.Mapping;
35 import jalview.datamodel.Sequence;
36 import jalview.datamodel.SequenceFeature;
37 import jalview.datamodel.SequenceI;
38 import jalview.schemes.ResidueProperties;
39 import jalview.util.MapList;
40 import jalview.util.ShiftList;
48 * @return -1 if cdp1 aligns before cdp2, 0 if in the same column or cdp2 is
49 * null, +1 if after cdp2
51 private static int compare_codonpos(int[] cdp1, int[] cdp2)
54 || (cdp1[0] == cdp2[0] && cdp1[1] == cdp2[1] && cdp1[2] == cdp2[2]))
56 if (cdp1[0] < cdp2[0] || cdp1[1] < cdp2[1] || cdp1[2] < cdp2[2])
57 return -1; // one base in cdp1 precedes the corresponding base in the
59 return 1; // one base in cdp1 appears after the corresponding base in the
64 * DNA->mapped protein sequence alignment translation given set of sequences
65 * 1. id distinct coding regions within selected region for each sequence 2.
66 * generate peptides based on inframe (or given) translation or (optionally
67 * and where specified) out of frame translations (annotated appropriately) 3.
68 * align peptides based on codon alignment
71 * id potential products from dna 1. search for distinct products within
72 * selected region for each selected sequence 2. group by associated DB type.
73 * 3. return as form for input into above function
79 * create a new alignment of protein sequences by an inframe translation of
80 * the provided NA sequences
89 * destination dataset for translated sequences and mappings
92 public static AlignmentI CdnaTranslate(SequenceI[] selection,
93 String[] seqstring, int viscontigs[], char gapCharacter,
94 AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)
96 return CdnaTranslate(selection, seqstring, null, viscontigs,
97 gapCharacter, annotations, aWidth, dataset);
105 * - array of DbRefEntry objects from which exon map in seqstring is
108 * @param gapCharacter
114 public static AlignmentI CdnaTranslate(SequenceI[] selection,
115 String[] seqstring, DBRefEntry[] product, int viscontigs[],
116 char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,
119 AlignedCodonFrame codons = new AlignedCodonFrame(aWidth); // stores hash of
125 int s, sSize = selection.length;
126 Vector pepseqs = new Vector();
127 for (s = 0; s < sSize; s++)
129 SequenceI newseq = translateCodingRegion(selection[s], seqstring[s],
130 viscontigs, codons, gapCharacter,
131 (product != null) ? product[s] : null, false); // possibly anonymous
135 pepseqs.addElement(newseq);
136 SequenceI ds = newseq;
139 while (ds.getDatasetSequence() != null)
141 ds = ds.getDatasetSequence();
143 dataset.addSequence(ds);
147 if (codons.aaWidth == 0)
149 SequenceI[] newseqs = new SequenceI[pepseqs.size()];
150 pepseqs.copyInto(newseqs);
151 AlignmentI al = new Alignment(newseqs);
152 al.padGaps(); // ensure we look aligned.
153 al.setDataset(dataset);
154 translateAlignedAnnotations(annotations, al, codons);
155 al.addCodonFrame(codons);
160 * fake the collection of DbRefs with associated exon mappings to identify if
161 * a translation would generate distinct product in the currently selected
168 public static boolean canTranslate(SequenceI[] selection,
171 for (int gd = 0; gd < selection.length; gd++)
173 SequenceI dna = selection[gd];
174 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
175 .selectRefs(dna.getDBRef(),
176 jalview.datamodel.DBRefSource.DNACODINGDBS);
179 // intersect with pep
180 // intersect with pep
181 Vector mappedrefs = new Vector();
182 DBRefEntry[] refs = dna.getDBRef();
183 for (int d = 0; d < refs.length; d++)
185 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
186 && refs[d].getMap().getMap().getFromRatio() == 3
187 && refs[d].getMap().getMap().getToRatio() == 1)
189 mappedrefs.addElement(refs[d]); // add translated protein maps
192 dnarefs = new DBRefEntry[mappedrefs.size()];
193 mappedrefs.copyInto(dnarefs);
194 for (int d = 0; d < dnarefs.length; d++)
196 Mapping mp = dnarefs[d].getMap();
199 for (int vc = 0; vc < viscontigs.length; vc += 2)
201 int[] mpr = mp.locateMappedRange(viscontigs[vc],
216 * generate a set of translated protein products from annotated sequenceI
220 * @param gapCharacter
222 * destination dataset for translated sequences
227 public static AlignmentI CdnaTranslate(SequenceI[] selection,
228 int viscontigs[], char gapCharacter, Alignment dataset)
231 Vector cdnasqs = new Vector();
232 Vector cdnasqi = new Vector();
233 Vector cdnaprod = new Vector();
234 for (int gd = 0; gd < selection.length; gd++)
236 SequenceI dna = selection[gd];
237 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
238 .selectRefs(dna.getDBRef(),
239 jalview.datamodel.DBRefSource.DNACODINGDBS);
242 // intersect with pep
243 Vector mappedrefs = new Vector();
244 DBRefEntry[] refs = dna.getDBRef();
245 for (int d = 0; d < refs.length; d++)
247 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
248 && refs[d].getMap().getMap().getFromRatio() == 3
249 && refs[d].getMap().getMap().getToRatio() == 1)
251 mappedrefs.addElement(refs[d]); // add translated protein maps
254 dnarefs = new DBRefEntry[mappedrefs.size()];
255 mappedrefs.copyInto(dnarefs);
256 for (int d = 0; d < dnarefs.length; d++)
258 Mapping mp = dnarefs[d].getMap();
259 StringBuffer sqstr = new StringBuffer();
262 Mapping intersect = mp.intersectVisContigs(viscontigs);
263 // generate seqstring for this sequence based on mapping
265 if (sqstr.length() > alwidth)
266 alwidth = sqstr.length();
267 cdnasqs.addElement(sqstr.toString());
268 cdnasqi.addElement(dna);
269 cdnaprod.addElement(intersect);
273 SequenceI[] cdna = new SequenceI[cdnasqs.size()];
274 DBRefEntry[] prods = new DBRefEntry[cdnaprod.size()];
275 String[] xons = new String[cdnasqs.size()];
276 cdnasqs.copyInto(xons);
277 cdnaprod.copyInto(prods);
278 cdnasqi.copyInto(cdna);
279 return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,
280 null, alwidth, dataset);
286 * translate na alignment annotations onto translated amino acid alignment al
287 * using codon mapping codons
293 public static void translateAlignedAnnotations(
294 AlignmentAnnotation[] annotations, AlignmentI al,
295 AlignedCodonFrame codons)
297 // //////////////////////////////
298 // Copy annotations across
300 // Can only do this for columns with consecutive codons, or where
301 // annotation is sequence associated.
304 if (annotations != null)
306 for (int i = 0; i < annotations.length; i++)
308 // Skip any autogenerated annotation
309 if (annotations[i].autoCalculated)
314 aSize = codons.getaaWidth(); // aa alignment width.
315 jalview.datamodel.Annotation[] anots = (annotations[i].annotations == null) ? null
316 : new jalview.datamodel.Annotation[aSize];
319 for (a = 0; a < aSize; a++)
321 // process through codon map.
322 if (codons.codons[a] != null
323 && codons.codons[a][0] == (codons.codons[a][2] - 2))
325 anots[a] = getCodonAnnotation(codons.codons[a],
326 annotations[i].annotations);
331 jalview.datamodel.AlignmentAnnotation aa = new jalview.datamodel.AlignmentAnnotation(
332 annotations[i].label, annotations[i].description, anots);
333 aa.graph = annotations[i].graph;
334 aa.graphGroup = annotations[i].graphGroup;
335 aa.graphHeight = annotations[i].graphHeight;
336 if (annotations[i].getThreshold() != null)
338 aa.setThreshold(new jalview.datamodel.GraphLine(annotations[i]
341 if (annotations[i].hasScore)
343 aa.setScore(annotations[i].getScore());
345 if (annotations[i].sequenceRef != null)
347 SequenceI aaSeq = codons
348 .getAaForDnaSeq(annotations[i].sequenceRef);
351 // aa.compactAnnotationArray(); // throw away alignment annotation
353 aa.setSequenceRef(aaSeq);
354 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true); // rebuild
356 aa.adjustForAlignment();
357 aaSeq.addAlignmentAnnotation(aa);
361 al.addAnnotation(aa);
366 private static Annotation getCodonAnnotation(int[] is,
367 Annotation[] annotations)
369 // Have a look at all the codon positions for annotation and put the first
370 // one found into the translated annotation pos.
372 Annotation annot = null;
373 for (int p = 0; p < 3; p++)
375 if (annotations[is[p]] != null)
379 annot = new Annotation(annotations[is[p]]);
385 Annotation cpy = new Annotation(annotations[is[p]]);
386 if (annot.colour == null)
388 annot.colour = cpy.colour;
390 if (annot.description == null || annot.description.length() == 0)
392 annot.description = cpy.description;
394 if (annot.displayCharacter == null)
396 annot.displayCharacter = cpy.displayCharacter;
398 if (annot.secondaryStructure == 0)
400 annot.secondaryStructure = cpy.secondaryStructure;
402 annot.value += cpy.value;
409 annot.value /= (float) contrib;
415 * Translate a na sequence
418 * sequence displayed under viscontigs visible columns
420 * ORF read in some global alignment reference frame
422 * mapping from global reference frame to visible seqstring ORF read
424 * Definition of global ORF alignment reference frame
425 * @param gapCharacter
426 * @return sequence ready to be added to alignment.
427 * @deprecated Use {@link #translateCodingRegion(SequenceI,String,int[],AlignedCodonFrame,char,DBRefEntry,boolean)} instead
429 public static SequenceI translateCodingRegion(SequenceI selection,
430 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
431 char gapCharacter, DBRefEntry product)
433 return translateCodingRegion(selection, seqstring, viscontigs, codons,
434 gapCharacter, product, false);
438 * Translate a na sequence
441 * sequence displayed under viscontigs visible columns
443 * ORF read in some global alignment reference frame
445 * mapping from global reference frame to visible seqstring ORF read
447 * Definition of global ORF alignment reference frame
448 * @param gapCharacter
449 * @param starForStop when true stop codons will translate as '*', otherwise as 'X'
450 * @return sequence ready to be added to alignment.
452 public static SequenceI translateCodingRegion(SequenceI selection,
453 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
454 char gapCharacter, DBRefEntry product, final boolean starForStop)
456 java.util.List skip = new ArrayList();
457 int skipint[] = null;
458 ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
460 int vc, scontigs[] = new int[viscontigs.length];
462 for (vc = 0; vc < viscontigs.length; vc += 2)
466 vismapping.addShift(npos, viscontigs[vc]);
471 vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
473 scontigs[vc] = viscontigs[vc];
474 scontigs[vc + 1] = viscontigs[vc + 1];
477 StringBuffer protein = new StringBuffer();
478 String seq = seqstring.replace('U', 'T');
479 char codon[] = new char[3];
480 int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
483 for (npos = 0, nend = seq.length(); npos < nend; npos++)
485 if (!jalview.util.Comparison.isGap(seq.charAt(npos)))
487 cdp[rf] = npos; // store position
488 codon[rf++] = seq.charAt(npos); // store base
490 // filled an RF yet ?
493 String aa = ResidueProperties.codonTranslate(new String(codon));
497 aa = String.valueOf(gapCharacter);
510 skipint[0] = vismapping.shift(skipint[0]);
511 skipint[1] = vismapping.shift(skipint[1]);
512 for (vc = 0; vc < scontigs.length; )
514 if (scontigs[vc + 1] < skipint[0])
516 // before skipint starts
520 if (scontigs[vc]>skipint[1])
522 // finished editing so
525 // Edit the contig list to include the skipped region which did not translate
527 // from : s1 e1 s2 e2 s3 e3
528 // to s: s1 e1 s2 k0 k1 e2 s3 e3
529 // list increases by one unless one boundary (s2==k0 or e2==k1) matches, and decreases by one if skipint intersects whole visible contig
530 if (scontigs[vc] <= skipint[0])
532 if (skipint[0] == scontigs[vc])
534 // skipint at start of contig
535 // shift the start of this contig
536 if (scontigs[vc + 1] > skipint[1])
538 scontigs[vc] = skipint[1];
543 if (scontigs[vc+1]==skipint[1])
546 t = new int[scontigs.length - 2];
549 System.arraycopy(scontigs, 0, t, 0, vc - 1);
551 if (vc + 2 < t.length)
553 System.arraycopy(scontigs, vc + 2, t, vc, t.length
558 // truncate contig to before the skipint region
559 scontigs[vc+1] = skipint[0]-1;
566 // scontig starts before start of skipint
567 if (scontigs[vc+1]<skipint[1]) {
568 // skipint truncates end of scontig
569 scontigs[vc+1] = skipint[0]-1;
572 // divide region to new contigs
573 t = new int[scontigs.length + 2];
574 System.arraycopy(scontigs, 0, t, 0, vc +1);
575 t[vc+1] = skipint[0];
576 t[vc+2] = skipint[1];
577 System.arraycopy(scontigs, vc + 1, t, vc+3, scontigs.length-(vc+1));
587 if (aa.equals("STOP"))
589 aa = starForStop ? "*" : "X";
593 // insert/delete gaps prior to this codon - if necessary
594 boolean findpos = true;
597 // first ensure that the codons array is long enough.
598 codons.checkCodonFrameWidth(aspos);
599 // now check to see if we place the aa at the current aspos in the
601 switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
604 codons.insertAAGap(aspos, gapCharacter);
608 // this aa appears after the aligned codons at aspos, so prefix it
610 aa = "" + gapCharacter + aa;
612 // if (aspos >= codons.aaWidth)
613 // codons.aaWidth = aspos + 1;
614 break; // check the next position for alignment
616 // codon aligns at aspos position.
620 // codon aligns with all other sequence residues found at aspos
623 if (codons.codons[aspos] == null)
625 // mark this column as aligning to this aligned reading frame
626 codons.codons[aspos] = new int[]
627 { cdp[0], cdp[1], cdp[2] };
629 if (aspos >= codons.aaWidth)
631 // update maximum alignment width
632 // (we can do this without calling checkCodonFrameWidth because it was
633 // already done above)
634 codons.setAaWidth(aspos);
636 // ready for next translated reading frame alignment position (if any)
642 SequenceI newseq = new Sequence(selection.getName(),
646 if (jalview.bin.Cache.log!=null) {
647 jalview.bin.Cache.log.debug("trimming contigs for incomplete terminal codon.");
649 System.err.println("trimming contigs for incomplete terminal codon.");
651 // map and trim contigs to ORF region
652 vc = scontigs.length - 1;
653 lastnpos = vismapping.shift(lastnpos); // place npos in context of
654 // whole dna alignment (rather
655 // than visible contigs)
656 // incomplete ORF could be broken over one or two visible contig
658 while (vc >= 0 && scontigs[vc] > lastnpos)
660 if (vc > 0 && scontigs[vc - 1] > lastnpos)
666 // correct last interval in list.
667 scontigs[vc] = lastnpos;
671 if (vc > 0 && (vc + 1) < scontigs.length)
673 // truncate map list to just vc elements
674 int t[] = new int[vc + 1];
675 System.arraycopy(scontigs, 0, t, 0, vc + 1);
681 if (scontigs != null)
684 // map scontigs to actual sequence positions on selection
685 for (vc = 0; vc < scontigs.length; vc += 2)
687 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
688 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
689 if (scontigs[vc + 1] == selection.getEnd())
692 // trim trailing empty intervals.
693 if ((vc + 2) < scontigs.length)
695 int t[] = new int[vc + 2];
696 System.arraycopy(scontigs, 0, t, 0, vc + 2);
700 * delete intervals in scontigs which are not translated. 1. map skip
701 * into sequence position intervals 2. truncate existing ranges and add
702 * new ranges to exclude untranslated regions. if (skip.size()>0) {
703 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
704 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
705 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
706 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
707 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
708 * range } else { // truncate range and create new one if necessary iv =
709 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
710 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
711 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
713 MapList map = new MapList(scontigs, new int[]
714 { 1, resSize }, 3, 1);
716 // update newseq as if it was generated as mapping from product
720 newseq.setName(product.getSource() + "|"
721 + product.getAccessionId());
722 if (product.getMap() != null)
724 // Mapping mp = product.getMap();
725 // newseq.setStart(mp.getPosition(scontigs[0]));
727 // .getPosition(scontigs[scontigs.length - 1]));
730 transferCodedFeatures(selection, newseq, map, null, null);
731 SequenceI rseq = newseq.deriveSequence(); // construct a dataset
732 // sequence for our new
733 // peptide, regardless.
734 // store a mapping (this actually stores a mapping between the dataset
735 // sequences for the two sequences
736 codons.addMap(selection, rseq, map);
740 // register the mapping somehow
746 * Given a peptide newly translated from a dna sequence, copy over and set any
747 * features on the peptide from the DNA. If featureTypes is null, all features
748 * on the dna sequence are searched (rather than just the displayed ones), and
749 * similarly for featureGroups.
754 * @param featureTypes
755 * hash who's keys are the displayed feature type strings
756 * @param featureGroups
757 * hash where keys are feature groups and values are Boolean objects
758 * indicating if they are displayed.
760 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
761 MapList map, Hashtable featureTypes, Hashtable featureGroups)
763 SequenceFeature[] sf = (dna.getDatasetSequence()!=null ? dna.getDatasetSequence() : dna).getSequenceFeatures();
765 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
766 .selectRefs(dna.getDBRef(),
767 jalview.datamodel.DBRefSource.DNACODINGDBS);
770 // intersect with pep
771 for (int d = 0; d < dnarefs.length; d++)
773 Mapping mp = dnarefs[d].getMap();
781 for (int f = 0; f < sf.length; f++)
783 fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
784 .get(sf[f].featureGroup));
785 if ((featureTypes == null || featureTypes.containsKey(sf[f]
786 .getType())) && (fgstate == null || fgstate.booleanValue()))
788 if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
790 // if (map.intersectsFrom(sf[f].begin, sf[f].end))