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;
45 import java.util.ArrayList;
46 import java.util.Hashtable;
47 import java.util.Vector;
55 * @return -1 if cdp1 aligns before cdp2, 0 if in the same column or cdp2 is
56 * null, +1 if after cdp2
58 private static int compare_codonpos(int[] cdp1, int[] cdp2)
61 || (cdp1[0] == cdp2[0] && cdp1[1] == cdp2[1] && cdp1[2] == cdp2[2]))
65 if (cdp1[0] < cdp2[0] || cdp1[1] < cdp2[1] || cdp1[2] < cdp2[2])
67 return -1; // one base in cdp1 precedes the corresponding base in the
70 return 1; // one base in cdp1 appears after the corresponding base in the
75 * DNA->mapped protein sequence alignment translation given set of sequences
76 * 1. id distinct coding regions within selected region for each sequence 2.
77 * generate peptides based on inframe (or given) translation or (optionally
78 * and where specified) out of frame translations (annotated appropriately) 3.
79 * align peptides based on codon alignment
82 * id potential products from dna 1. search for distinct products within
83 * selected region for each selected sequence 2. group by associated DB type.
84 * 3. return as form for input into above function
90 * create a new alignment of protein sequences by an inframe translation of
91 * the provided NA sequences
100 * destination dataset for translated sequences and mappings
103 public static AlignmentI CdnaTranslate(SequenceI[] selection,
104 String[] seqstring, int viscontigs[], char gapCharacter,
105 AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)
107 return CdnaTranslate(selection, seqstring, null, viscontigs,
108 gapCharacter, annotations, aWidth, dataset);
116 * - array of DbRefEntry objects from which exon map in seqstring is
119 * @param gapCharacter
125 public static AlignmentI CdnaTranslate(SequenceI[] selection,
126 String[] seqstring, DBRefEntry[] product, int viscontigs[],
127 char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,
130 AlignedCodonFrame codons = new AlignedCodonFrame(aWidth); // stores hash of
136 int s, sSize = selection.length;
137 Vector pepseqs = new Vector();
138 for (s = 0; s < sSize; s++)
140 SequenceI newseq = translateCodingRegion(selection[s], seqstring[s],
141 viscontigs, codons, gapCharacter,
142 (product != null) ? product[s] : null, false); // possibly
147 pepseqs.addElement(newseq);
148 SequenceI ds = newseq;
151 while (ds.getDatasetSequence() != null)
153 ds = ds.getDatasetSequence();
155 dataset.addSequence(ds);
159 if (codons.aaWidth == 0)
163 SequenceI[] newseqs = new SequenceI[pepseqs.size()];
164 pepseqs.copyInto(newseqs);
165 AlignmentI al = new Alignment(newseqs);
166 al.padGaps(); // ensure we look aligned.
167 al.setDataset(dataset);
168 // translateAlignedAnnotations(annotations, al, codons);
169 al.addCodonFrame(codons);
174 * fake the collection of DbRefs with associated exon mappings to identify if
175 * a translation would generate distinct product in the currently selected
182 public static boolean canTranslate(SequenceI[] selection,
185 for (int gd = 0; gd < selection.length; gd++)
187 SequenceI dna = selection[gd];
188 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
189 .selectRefs(dna.getDBRef(),
190 jalview.datamodel.DBRefSource.DNACODINGDBS);
193 // intersect with pep
194 // intersect with pep
195 Vector mappedrefs = new Vector();
196 DBRefEntry[] refs = dna.getDBRef();
197 for (int d = 0; d < refs.length; d++)
199 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
200 && refs[d].getMap().getMap().getFromRatio() == 3
201 && refs[d].getMap().getMap().getToRatio() == 1)
203 mappedrefs.addElement(refs[d]); // add translated protein maps
206 dnarefs = new DBRefEntry[mappedrefs.size()];
207 mappedrefs.copyInto(dnarefs);
208 for (int d = 0; d < dnarefs.length; d++)
210 Mapping mp = dnarefs[d].getMap();
213 for (int vc = 0; vc < viscontigs.length; vc += 2)
215 int[] mpr = mp.locateMappedRange(viscontigs[vc],
230 * generate a set of translated protein products from annotated sequenceI
234 * @param gapCharacter
236 * destination dataset for translated sequences
241 public static AlignmentI CdnaTranslate(SequenceI[] selection,
242 int viscontigs[], char gapCharacter, Alignment dataset)
245 Vector cdnasqs = new Vector();
246 Vector cdnasqi = new Vector();
247 Vector cdnaprod = new Vector();
248 for (int gd = 0; gd < selection.length; gd++)
250 SequenceI dna = selection[gd];
251 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
252 .selectRefs(dna.getDBRef(),
253 jalview.datamodel.DBRefSource.DNACODINGDBS);
256 // intersect with pep
257 Vector mappedrefs = new Vector();
258 DBRefEntry[] refs = dna.getDBRef();
259 for (int d = 0; d < refs.length; d++)
261 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
262 && refs[d].getMap().getMap().getFromRatio() == 3
263 && refs[d].getMap().getMap().getToRatio() == 1)
265 mappedrefs.addElement(refs[d]); // add translated protein maps
268 dnarefs = new DBRefEntry[mappedrefs.size()];
269 mappedrefs.copyInto(dnarefs);
270 for (int d = 0; d < dnarefs.length; d++)
272 Mapping mp = dnarefs[d].getMap();
273 StringBuffer sqstr = new StringBuffer();
276 Mapping intersect = mp.intersectVisContigs(viscontigs);
277 // generate seqstring for this sequence based on mapping
279 if (sqstr.length() > alwidth)
281 alwidth = sqstr.length();
283 cdnasqs.addElement(sqstr.toString());
284 cdnasqi.addElement(dna);
285 cdnaprod.addElement(intersect);
289 SequenceI[] cdna = new SequenceI[cdnasqs.size()];
290 DBRefEntry[] prods = new DBRefEntry[cdnaprod.size()];
291 String[] xons = new String[cdnasqs.size()];
292 cdnasqs.copyInto(xons);
293 cdnaprod.copyInto(prods);
294 cdnasqi.copyInto(cdna);
295 return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,
296 null, alwidth, dataset);
302 * Translate na alignment annotations onto translated amino acid alignment al
303 * using codon mapping codons
309 public static void translateAlignedAnnotations(
310 AlignmentAnnotation[] annotations, AlignmentI al,
311 AlignedCodonFrame codons)
313 // Can only do this for columns with consecutive codons, or where
314 // annotation is sequence associated.
316 if (annotations != null)
318 for (AlignmentAnnotation annotation : annotations)
321 * Skip hidden or autogenerated annotation. Also (for now), RNA
322 * secondary structure annotation. If we want to show this against
323 * protein we need a smarter way to 'translate' without generating
324 * invalid (unbalanced) structure annotation.
326 if (annotation.autoCalculated || !annotation.visible
327 || annotation.isRNA())
332 int aSize = codons.getaaWidth(); // aa alignment width.
333 Annotation[] anots = (annotation.annotations == null) ? null
334 : new Annotation[aSize];
337 for (int a = 0; a < aSize; a++)
339 // process through codon map.
340 if (a < codons.codons.length && codons.codons[a] != null
341 && codons.codons[a][0] == (codons.codons[a][2] - 2))
343 anots[a] = getCodonAnnotation(codons.codons[a],
344 annotation.annotations);
349 AlignmentAnnotation aa = new AlignmentAnnotation(annotation.label,
350 annotation.description, anots);
351 aa.graph = annotation.graph;
352 aa.graphGroup = annotation.graphGroup;
353 aa.graphHeight = annotation.graphHeight;
354 if (annotation.getThreshold() != null)
356 aa.setThreshold(new GraphLine(annotation
359 if (annotation.hasScore)
361 aa.setScore(annotation.getScore());
364 final SequenceI seqRef = annotation.sequenceRef;
367 SequenceI aaSeq = codons.getAaForDnaSeq(seqRef);
370 // aa.compactAnnotationArray(); // throw away alignment annotation
372 aa.setSequenceRef(aaSeq);
374 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true);
375 aa.adjustForAlignment();
376 aaSeq.addAlignmentAnnotation(aa);
379 al.addAnnotation(aa);
384 private static Annotation getCodonAnnotation(int[] is,
385 Annotation[] annotations)
387 // Have a look at all the codon positions for annotation and put the first
388 // one found into the translated annotation pos.
390 Annotation annot = null;
391 for (int p = 0; p < 3; p++)
393 if (annotations[is[p]] != null)
397 annot = new Annotation(annotations[is[p]]);
403 Annotation cpy = new Annotation(annotations[is[p]]);
404 if (annot.colour == null)
406 annot.colour = cpy.colour;
408 if (annot.description == null || annot.description.length() == 0)
410 annot.description = cpy.description;
412 if (annot.displayCharacter == null)
414 annot.displayCharacter = cpy.displayCharacter;
416 if (annot.secondaryStructure == 0)
418 annot.secondaryStructure = cpy.secondaryStructure;
420 annot.value += cpy.value;
427 annot.value /= contrib;
433 * Translate a na sequence
436 * sequence displayed under viscontigs visible columns
438 * ORF read in some global alignment reference frame
440 * mapping from global reference frame to visible seqstring ORF read
442 * Definition of global ORF alignment reference frame
443 * @param gapCharacter
444 * @return sequence ready to be added to alignment.
446 * {@link #translateCodingRegion(SequenceI,String,int[],AlignedCodonFrame,char,DBRefEntry,boolean)}
450 public static SequenceI translateCodingRegion(SequenceI selection,
451 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
452 char gapCharacter, DBRefEntry product)
454 return translateCodingRegion(selection, seqstring, viscontigs, codons,
455 gapCharacter, product, false);
459 * Translate a na sequence
462 * sequence displayed under viscontigs visible columns
464 * ORF read in some global alignment reference frame
466 * mapping from global reference frame to visible seqstring ORF read
468 * Definition of global ORF alignment reference frame
469 * @param gapCharacter
471 * when true stop codons will translate as '*', otherwise as 'X'
472 * @return sequence ready to be added to alignment.
474 public static SequenceI translateCodingRegion(SequenceI selection,
475 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
476 char gapCharacter, DBRefEntry product, final boolean starForStop)
478 List<int[]> skip = new ArrayList<int[]>();
479 int skipint[] = null;
480 ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
482 int vc, scontigs[] = new int[viscontigs.length];
484 for (vc = 0; vc < viscontigs.length; vc += 2)
488 vismapping.addShift(npos, viscontigs[vc]);
493 vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
495 scontigs[vc] = viscontigs[vc];
496 scontigs[vc + 1] = viscontigs[vc + 1];
499 // allocate a roughly sized buffer for the protein sequence
500 StringBuilder protein = new StringBuilder(seqstring.length() / 2);
501 String seq = seqstring.replace('U', 'T');
502 char codon[] = new char[3];
503 int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
506 for (npos = 0, nend = seq.length(); npos < nend; npos++)
508 if (!Comparison.isGap(seq.charAt(npos)))
510 cdp[rf] = npos; // store position
511 codon[rf++] = seq.charAt(npos); // store base
516 * Filled up a reading frame...
518 String aa = ResidueProperties.codonTranslate(new String(codon));
522 aa = String.valueOf(gapCharacter);
535 skipint[0] = vismapping.shift(skipint[0]);
536 skipint[1] = vismapping.shift(skipint[1]);
537 for (vc = 0; vc < scontigs.length;)
539 if (scontigs[vc + 1] < skipint[0])
541 // before skipint starts
545 if (scontigs[vc] > skipint[1])
547 // finished editing so
550 // Edit the contig list to include the skipped region which did
553 // from : s1 e1 s2 e2 s3 e3
554 // to s: s1 e1 s2 k0 k1 e2 s3 e3
555 // list increases by one unless one boundary (s2==k0 or e2==k1)
556 // matches, and decreases by one if skipint intersects whole
558 if (scontigs[vc] <= skipint[0])
560 if (skipint[0] == scontigs[vc])
562 // skipint at start of contig
563 // shift the start of this contig
564 if (scontigs[vc + 1] > skipint[1])
566 scontigs[vc] = skipint[1];
571 if (scontigs[vc + 1] == skipint[1])
574 t = new int[scontigs.length - 2];
577 System.arraycopy(scontigs, 0, t, 0, vc - 1);
579 if (vc + 2 < t.length)
581 System.arraycopy(scontigs, vc + 2, t, vc, t.length
588 // truncate contig to before the skipint region
589 scontigs[vc + 1] = skipint[0] - 1;
596 // scontig starts before start of skipint
597 if (scontigs[vc + 1] < skipint[1])
599 // skipint truncates end of scontig
600 scontigs[vc + 1] = skipint[0] - 1;
605 // divide region to new contigs
606 t = new int[scontigs.length + 2];
607 System.arraycopy(scontigs, 0, t, 0, vc + 1);
608 t[vc + 1] = skipint[0];
609 t[vc + 2] = skipint[1];
610 System.arraycopy(scontigs, vc + 1, t, vc + 3,
611 scontigs.length - (vc + 1));
621 if (aa.equals("STOP"))
623 aa = starForStop ? "*" : "X";
627 // insert/delete gaps prior to this codon - if necessary
628 boolean findpos = true;
631 // first ensure that the codons array is long enough.
632 codons.checkCodonFrameWidth(aspos);
633 // now check to see if we place the aa at the current aspos in the
635 switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
638 codons.insertAAGap(aspos, gapCharacter);
642 // this aa appears after the aligned codons at aspos, so prefix it
644 aa = "" + gapCharacter + aa;
646 // if (aspos >= codons.aaWidth)
647 // codons.aaWidth = aspos + 1;
648 break; // check the next position for alignment
650 // codon aligns at aspos position.
654 // codon aligns with all other sequence residues found at aspos
657 if (codons.codons[aspos] == null)
659 // mark this column as aligning to this aligned reading frame
660 codons.codons[aspos] = new int[]
661 { cdp[0], cdp[1], cdp[2] };
663 if (aspos >= codons.aaWidth)
665 // update maximum alignment width
666 // (we can do this without calling checkCodonFrameWidth because it was
667 // already done above)
668 codons.setAaWidth(aspos);
670 // ready for next translated reading frame alignment position (if any)
676 SequenceI newseq = new Sequence(selection.getName(),
680 if (jalview.bin.Cache.log != null)
682 jalview.bin.Cache.log
683 .debug("trimming contigs for incomplete terminal codon.");
688 .println("trimming contigs for incomplete terminal codon.");
690 // map and trim contigs to ORF region
691 vc = scontigs.length - 1;
692 lastnpos = vismapping.shift(lastnpos); // place npos in context of
693 // whole dna alignment (rather
694 // than visible contigs)
695 // incomplete ORF could be broken over one or two visible contig
697 while (vc >= 0 && scontigs[vc] > lastnpos)
699 if (vc > 0 && scontigs[vc - 1] > lastnpos)
705 // correct last interval in list.
706 scontigs[vc] = lastnpos;
710 if (vc > 0 && (vc + 1) < scontigs.length)
712 // truncate map list to just vc elements
713 int t[] = new int[vc + 1];
714 System.arraycopy(scontigs, 0, t, 0, vc + 1);
722 if (scontigs != null)
725 // map scontigs to actual sequence positions on selection
726 for (vc = 0; vc < scontigs.length; vc += 2)
728 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
729 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
730 if (scontigs[vc + 1] == selection.getEnd())
735 // trim trailing empty intervals.
736 if ((vc + 2) < scontigs.length)
738 int t[] = new int[vc + 2];
739 System.arraycopy(scontigs, 0, t, 0, vc + 2);
743 * delete intervals in scontigs which are not translated. 1. map skip
744 * into sequence position intervals 2. truncate existing ranges and add
745 * new ranges to exclude untranslated regions. if (skip.size()>0) {
746 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
747 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
748 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
749 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
750 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
751 * range } else { // truncate range and create new one if necessary iv =
752 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
753 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
754 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
756 MapList map = new MapList(scontigs, new int[]
757 { 1, resSize }, 3, 1);
759 // update newseq as if it was generated as mapping from product
763 newseq.setName(product.getSource() + "|"
764 + product.getAccessionId());
765 if (product.getMap() != null)
767 // Mapping mp = product.getMap();
768 // newseq.setStart(mp.getPosition(scontigs[0]));
770 // .getPosition(scontigs[scontigs.length - 1]));
773 transferCodedFeatures(selection, newseq, map, null, null);
774 SequenceI rseq = newseq.deriveSequence(); // construct a dataset
775 // sequence for our new
776 // peptide, regardless.
777 // store a mapping (this actually stores a mapping between the dataset
778 // sequences for the two sequences
779 codons.addMap(selection, rseq, map);
783 // register the mapping somehow
789 * Given a peptide newly translated from a dna sequence, copy over and set any
790 * features on the peptide from the DNA. If featureTypes is null, all features
791 * on the dna sequence are searched (rather than just the displayed ones), and
792 * similarly for featureGroups.
797 * @param featureTypes
798 * hash who's keys are the displayed feature type strings
799 * @param featureGroups
800 * hash where keys are feature groups and values are Boolean objects
801 * indicating if they are displayed.
803 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
804 MapList map, Hashtable featureTypes, Hashtable featureGroups)
806 SequenceFeature[] sf = (dna.getDatasetSequence() != null ? dna
807 .getDatasetSequence() : dna).getSequenceFeatures();
809 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
810 .selectRefs(dna.getDBRef(),
811 jalview.datamodel.DBRefSource.DNACODINGDBS);
814 // intersect with pep
815 for (int d = 0; d < dnarefs.length; d++)
817 Mapping mp = dnarefs[d].getMap();
825 for (int f = 0; f < sf.length; f++)
827 fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
828 .get(sf[f].featureGroup));
829 if ((featureTypes == null || featureTypes.containsKey(sf[f]
830 .getType())) && (fgstate == null || fgstate.booleanValue()))
832 if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
834 // if (map.intersectsFrom(sf[f].begin, sf[f].end))