2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
20 import java.util.ArrayList;
21 import java.util.Hashtable;
22 import java.util.Vector;
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.FeatureProperties;
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.MapList;
37 import jalview.util.ShiftList;
45 * @return -1 if cdp1 aligns before cdp2, 0 if in the same column or cdp2 is
46 * null, +1 if after cdp2
48 private static int compare_codonpos(int[] cdp1, int[] cdp2)
51 || (cdp1[0] == cdp2[0] && cdp1[1] == cdp2[1] && cdp1[2] == cdp2[2]))
53 if (cdp1[0] < cdp2[0] || cdp1[1] < cdp2[1] || cdp1[2] < cdp2[2])
54 return -1; // one base in cdp1 precedes the corresponding base in the
56 return 1; // one base in cdp1 appears after the corresponding base in the
61 * DNA->mapped protein sequence alignment translation given set of sequences
62 * 1. id distinct coding regions within selected region for each sequence 2.
63 * generate peptides based on inframe (or given) translation or (optionally
64 * and where specified) out of frame translations (annotated appropriately) 3.
65 * align peptides based on codon alignment
68 * id potential products from dna 1. search for distinct products within
69 * selected region for each selected sequence 2. group by associated DB type.
70 * 3. return as form for input into above function
76 * create a new alignment of protein sequences by an inframe translation of
77 * the provided NA sequences
86 * destination dataset for translated sequences and mappings
89 public static AlignmentI CdnaTranslate(SequenceI[] selection,
90 String[] seqstring, int viscontigs[], char gapCharacter,
91 AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)
93 return CdnaTranslate(selection, seqstring, null, viscontigs,
94 gapCharacter, annotations, aWidth, dataset);
102 * - array of DbRefEntry objects from which exon map in seqstring is
105 * @param gapCharacter
111 public static AlignmentI CdnaTranslate(SequenceI[] selection,
112 String[] seqstring, DBRefEntry[] product, int viscontigs[],
113 char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,
116 AlignedCodonFrame codons = new AlignedCodonFrame(aWidth); // stores hash of
122 int s, sSize = selection.length;
123 Vector pepseqs = new Vector();
124 for (s = 0; s < sSize; s++)
126 SequenceI newseq = translateCodingRegion(selection[s], seqstring[s],
127 viscontigs, codons, gapCharacter,
128 (product != null) ? product[s] : null, false); // possibly anonymous
132 pepseqs.addElement(newseq);
133 SequenceI ds = newseq;
136 while (ds.getDatasetSequence() != null)
138 ds = ds.getDatasetSequence();
140 dataset.addSequence(ds);
144 if (codons.aaWidth == 0)
146 SequenceI[] newseqs = new SequenceI[pepseqs.size()];
147 pepseqs.copyInto(newseqs);
148 AlignmentI al = new Alignment(newseqs);
149 al.padGaps(); // ensure we look aligned.
150 al.setDataset(dataset);
151 translateAlignedAnnotations(annotations, al, codons);
152 al.addCodonFrame(codons);
157 * fake the collection of DbRefs with associated exon mappings to identify if
158 * a translation would generate distinct product in the currently selected
165 public static boolean canTranslate(SequenceI[] selection,
168 for (int gd = 0; gd < selection.length; gd++)
170 SequenceI dna = selection[gd];
171 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
172 .selectRefs(dna.getDBRef(),
173 jalview.datamodel.DBRefSource.DNACODINGDBS);
176 // intersect with pep
177 // intersect with pep
178 Vector mappedrefs = new Vector();
179 DBRefEntry[] refs = dna.getDBRef();
180 for (int d = 0; d < refs.length; d++)
182 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
183 && refs[d].getMap().getMap().getFromRatio() == 3
184 && refs[d].getMap().getMap().getToRatio() == 1)
186 mappedrefs.addElement(refs[d]); // add translated protein maps
189 dnarefs = new DBRefEntry[mappedrefs.size()];
190 mappedrefs.copyInto(dnarefs);
191 for (int d = 0; d < dnarefs.length; d++)
193 Mapping mp = dnarefs[d].getMap();
196 for (int vc = 0; vc < viscontigs.length; vc += 2)
198 int[] mpr = mp.locateMappedRange(viscontigs[vc],
213 * generate a set of translated protein products from annotated sequenceI
217 * @param gapCharacter
219 * destination dataset for translated sequences
224 public static AlignmentI CdnaTranslate(SequenceI[] selection,
225 int viscontigs[], char gapCharacter, Alignment dataset)
228 Vector cdnasqs = new Vector();
229 Vector cdnasqi = new Vector();
230 Vector cdnaprod = new Vector();
231 for (int gd = 0; gd < selection.length; gd++)
233 SequenceI dna = selection[gd];
234 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
235 .selectRefs(dna.getDBRef(),
236 jalview.datamodel.DBRefSource.DNACODINGDBS);
239 // intersect with pep
240 Vector mappedrefs = new Vector();
241 DBRefEntry[] refs = dna.getDBRef();
242 for (int d = 0; d < refs.length; d++)
244 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
245 && refs[d].getMap().getMap().getFromRatio() == 3
246 && refs[d].getMap().getMap().getToRatio() == 1)
248 mappedrefs.addElement(refs[d]); // add translated protein maps
251 dnarefs = new DBRefEntry[mappedrefs.size()];
252 mappedrefs.copyInto(dnarefs);
253 for (int d = 0; d < dnarefs.length; d++)
255 Mapping mp = dnarefs[d].getMap();
256 StringBuffer sqstr = new StringBuffer();
259 Mapping intersect = mp.intersectVisContigs(viscontigs);
260 // generate seqstring for this sequence based on mapping
262 if (sqstr.length() > alwidth)
263 alwidth = sqstr.length();
264 cdnasqs.addElement(sqstr.toString());
265 cdnasqi.addElement(dna);
266 cdnaprod.addElement(intersect);
270 SequenceI[] cdna = new SequenceI[cdnasqs.size()];
271 DBRefEntry[] prods = new DBRefEntry[cdnaprod.size()];
272 String[] xons = new String[cdnasqs.size()];
273 cdnasqs.copyInto(xons);
274 cdnaprod.copyInto(prods);
275 cdnasqi.copyInto(cdna);
276 return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,
277 null, alwidth, dataset);
283 * translate na alignment annotations onto translated amino acid alignment al
284 * using codon mapping codons
290 public static void translateAlignedAnnotations(
291 AlignmentAnnotation[] annotations, AlignmentI al,
292 AlignedCodonFrame codons)
294 // //////////////////////////////
295 // Copy annotations across
297 // Can only do this for columns with consecutive codons, or where
298 // annotation is sequence associated.
301 if (annotations != null)
303 for (int i = 0; i < annotations.length; i++)
305 // Skip any autogenerated annotation
306 if (annotations[i].autoCalculated)
311 aSize = codons.getaaWidth(); // aa alignment width.
312 jalview.datamodel.Annotation[] anots = (annotations[i].annotations == null) ? null
313 : new jalview.datamodel.Annotation[aSize];
316 for (a = 0; a < aSize; a++)
318 // process through codon map.
319 if (codons.codons[a] != null
320 && codons.codons[a][0] == (codons.codons[a][2] - 2))
322 anots[a] = getCodonAnnotation(codons.codons[a],
323 annotations[i].annotations);
328 jalview.datamodel.AlignmentAnnotation aa = new jalview.datamodel.AlignmentAnnotation(
329 annotations[i].label, annotations[i].description, anots);
330 aa.graph = annotations[i].graph;
331 aa.graphGroup = annotations[i].graphGroup;
332 aa.graphHeight = annotations[i].graphHeight;
333 if (annotations[i].getThreshold() != null)
335 aa.setThreshold(new jalview.datamodel.GraphLine(annotations[i]
338 if (annotations[i].hasScore)
340 aa.setScore(annotations[i].getScore());
342 if (annotations[i].sequenceRef != null)
344 SequenceI aaSeq = codons
345 .getAaForDnaSeq(annotations[i].sequenceRef);
348 // aa.compactAnnotationArray(); // throw away alignment annotation
350 aa.setSequenceRef(aaSeq);
351 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true); // rebuild
353 aa.adjustForAlignment();
354 aaSeq.addAlignmentAnnotation(aa);
358 al.addAnnotation(aa);
363 private static Annotation getCodonAnnotation(int[] is,
364 Annotation[] annotations)
366 // Have a look at all the codon positions for annotation and put the first
367 // one found into the translated annotation pos.
369 Annotation annot = null;
370 for (int p = 0; p < 3; p++)
372 if (annotations[is[p]] != null)
376 annot = new Annotation(annotations[is[p]]);
382 Annotation cpy = new Annotation(annotations[is[p]]);
383 if (annot.colour == null)
385 annot.colour = cpy.colour;
387 if (annot.description == null || annot.description.length() == 0)
389 annot.description = cpy.description;
391 if (annot.displayCharacter == null)
393 annot.displayCharacter = cpy.displayCharacter;
395 if (annot.secondaryStructure == 0)
397 annot.secondaryStructure = cpy.secondaryStructure;
399 annot.value += cpy.value;
406 annot.value /= (float) contrib;
412 * Translate a na sequence
415 * sequence displayed under viscontigs visible columns
417 * ORF read in some global alignment reference frame
419 * mapping from global reference frame to visible seqstring ORF read
421 * Definition of global ORF alignment reference frame
422 * @param gapCharacter
423 * @return sequence ready to be added to alignment.
424 * @deprecated Use {@link #translateCodingRegion(SequenceI,String,int[],AlignedCodonFrame,char,DBRefEntry,boolean)} instead
426 public static SequenceI translateCodingRegion(SequenceI selection,
427 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
428 char gapCharacter, DBRefEntry product)
430 return translateCodingRegion(selection, seqstring, viscontigs, codons,
431 gapCharacter, product, false);
435 * Translate a na sequence
438 * sequence displayed under viscontigs visible columns
440 * ORF read in some global alignment reference frame
442 * mapping from global reference frame to visible seqstring ORF read
444 * Definition of global ORF alignment reference frame
445 * @param gapCharacter
446 * @param starForStop when true stop codons will translate as '*', otherwise as 'X'
447 * @return sequence ready to be added to alignment.
449 public static SequenceI translateCodingRegion(SequenceI selection,
450 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
451 char gapCharacter, DBRefEntry product, final boolean starForStop)
453 java.util.List skip = new ArrayList();
454 int skipint[] = null;
455 ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
457 int vc, scontigs[] = new int[viscontigs.length];
459 for (vc = 0; vc < viscontigs.length; vc += 2)
463 vismapping.addShift(npos, viscontigs[vc]);
468 vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
470 scontigs[vc] = viscontigs[vc];
471 scontigs[vc + 1] = viscontigs[vc + 1];
474 StringBuffer protein = new StringBuffer();
475 String seq = seqstring.replace('U', 'T');
476 char codon[] = new char[3];
477 int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
480 for (npos = 0, nend = seq.length(); npos < nend; npos++)
482 if (!jalview.util.Comparison.isGap(seq.charAt(npos)))
484 cdp[rf] = npos; // store position
485 codon[rf++] = seq.charAt(npos); // store base
487 // filled an RF yet ?
490 String aa = ResidueProperties.codonTranslate(new String(codon));
494 aa = String.valueOf(gapCharacter);
507 skipint[0] = vismapping.shift(skipint[0]);
508 skipint[1] = vismapping.shift(skipint[1]);
509 for (vc = 0; vc < scontigs.length; )
511 if (scontigs[vc + 1] < skipint[0])
513 // before skipint starts
517 if (scontigs[vc]>skipint[1])
519 // finished editing so
522 // Edit the contig list to include the skipped region which did not translate
524 // from : s1 e1 s2 e2 s3 e3
525 // to s: s1 e1 s2 k0 k1 e2 s3 e3
526 // list increases by one unless one boundary (s2==k0 or e2==k1) matches, and decreases by one if skipint intersects whole visible contig
527 if (scontigs[vc] <= skipint[0])
529 if (skipint[0] == scontigs[vc])
531 // skipint at start of contig
532 // shift the start of this contig
533 if (scontigs[vc + 1] > skipint[1])
535 scontigs[vc] = skipint[1];
540 if (scontigs[vc+1]==skipint[1])
543 t = new int[scontigs.length - 2];
546 System.arraycopy(scontigs, 0, t, 0, vc - 1);
548 if (vc + 2 < t.length)
550 System.arraycopy(scontigs, vc + 2, t, vc, t.length
555 // truncate contig to before the skipint region
556 scontigs[vc+1] = skipint[0]-1;
563 // scontig starts before start of skipint
564 if (scontigs[vc+1]<skipint[1]) {
565 // skipint truncates end of scontig
566 scontigs[vc+1] = skipint[0]-1;
569 // divide region to new contigs
570 t = new int[scontigs.length + 2];
571 System.arraycopy(scontigs, 0, t, 0, vc +1);
572 t[vc+1] = skipint[0];
573 t[vc+2] = skipint[1];
574 System.arraycopy(scontigs, vc + 1, t, vc+3, scontigs.length-(vc+1));
584 if (aa.equals("STOP"))
586 aa = starForStop ? "*" : "X";
590 // insert/delete gaps prior to this codon - if necessary
591 boolean findpos = true;
594 // first ensure that the codons array is long enough.
595 codons.checkCodonFrameWidth(aspos);
596 // now check to see if we place the aa at the current aspos in the
598 switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
601 codons.insertAAGap(aspos, gapCharacter);
605 // this aa appears after the aligned codons at aspos, so prefix it
607 aa = "" + gapCharacter + aa;
609 // if (aspos >= codons.aaWidth)
610 // codons.aaWidth = aspos + 1;
611 break; // check the next position for alignment
613 // codon aligns at aspos position.
617 // codon aligns with all other sequence residues found at aspos
620 if (codons.codons[aspos] == null)
622 // mark this column as aligning to this aligned reading frame
623 codons.codons[aspos] = new int[]
624 { cdp[0], cdp[1], cdp[2] };
626 if (aspos >= codons.aaWidth)
628 // update maximum alignment width
629 // (we can do this without calling checkCodonFrameWidth because it was
630 // already done above)
631 codons.setAaWidth(aspos);
633 // ready for next translated reading frame alignment position (if any)
639 SequenceI newseq = new Sequence(selection.getName(),
643 if (jalview.bin.Cache.log!=null) {
644 jalview.bin.Cache.log.debug("trimming contigs for incomplete terminal codon.");
646 System.err.println("trimming contigs for incomplete terminal codon.");
648 // map and trim contigs to ORF region
649 vc = scontigs.length - 1;
650 lastnpos = vismapping.shift(lastnpos); // place npos in context of
651 // whole dna alignment (rather
652 // than visible contigs)
653 // incomplete ORF could be broken over one or two visible contig
655 while (vc >= 0 && scontigs[vc] > lastnpos)
657 if (vc > 0 && scontigs[vc - 1] > lastnpos)
663 // correct last interval in list.
664 scontigs[vc] = lastnpos;
668 if (vc > 0 && (vc + 1) < scontigs.length)
670 // truncate map list to just vc elements
671 int t[] = new int[vc + 1];
672 System.arraycopy(scontigs, 0, t, 0, vc + 1);
678 if (scontigs != null)
681 // map scontigs to actual sequence positions on selection
682 for (vc = 0; vc < scontigs.length; vc += 2)
684 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
685 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
686 if (scontigs[vc + 1] == selection.getEnd())
689 // trim trailing empty intervals.
690 if ((vc + 2) < scontigs.length)
692 int t[] = new int[vc + 2];
693 System.arraycopy(scontigs, 0, t, 0, vc + 2);
697 * delete intervals in scontigs which are not translated. 1. map skip
698 * into sequence position intervals 2. truncate existing ranges and add
699 * new ranges to exclude untranslated regions. if (skip.size()>0) {
700 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
701 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
702 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
703 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
704 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
705 * range } else { // truncate range and create new one if necessary iv =
706 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
707 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
708 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
710 MapList map = new MapList(scontigs, new int[]
711 { 1, resSize }, 3, 1);
713 // update newseq as if it was generated as mapping from product
717 newseq.setName(product.getSource() + "|"
718 + product.getAccessionId());
719 if (product.getMap() != null)
721 // Mapping mp = product.getMap();
722 // newseq.setStart(mp.getPosition(scontigs[0]));
724 // .getPosition(scontigs[scontigs.length - 1]));
727 transferCodedFeatures(selection, newseq, map, null, null);
728 SequenceI rseq = newseq.deriveSequence(); // construct a dataset
729 // sequence for our new
730 // peptide, regardless.
731 // store a mapping (this actually stores a mapping between the dataset
732 // sequences for the two sequences
733 codons.addMap(selection, rseq, map);
737 // register the mapping somehow
743 * Given a peptide newly translated from a dna sequence, copy over and set any
744 * features on the peptide from the DNA. If featureTypes is null, all features
745 * on the dna sequence are searched (rather than just the displayed ones), and
746 * similarly for featureGroups.
751 * @param featureTypes
752 * hash who's keys are the displayed feature type strings
753 * @param featureGroups
754 * hash where keys are feature groups and values are Boolean objects
755 * indicating if they are displayed.
757 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
758 MapList map, Hashtable featureTypes, Hashtable featureGroups)
760 SequenceFeature[] sf = (dna.getDatasetSequence()!=null ? dna.getDatasetSequence() : dna).getSequenceFeatures();
762 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
763 .selectRefs(dna.getDBRef(),
764 jalview.datamodel.DBRefSource.DNACODINGDBS);
767 // intersect with pep
768 for (int d = 0; d < dnarefs.length; d++)
770 Mapping mp = dnarefs[d].getMap();
778 for (int f = 0; f < sf.length; f++)
780 fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
781 .get(sf[f].featureGroup));
782 if ((featureTypes == null || featureTypes.containsKey(sf[f]
783 .getType())) && (fgstate == null || fgstate.booleanValue()))
785 if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
787 // if (map.intersectsFrom(sf[f].begin, sf[f].end))