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); // possibly anonymous
132 pepseqs.addElement(newseq);
133 SequenceI ds = newseq;
134 while (ds.getDatasetSequence() != null)
136 ds = ds.getDatasetSequence();
138 dataset.addSequence(ds);
141 if (codons.aaWidth == 0)
143 SequenceI[] newseqs = new SequenceI[pepseqs.size()];
144 pepseqs.copyInto(newseqs);
145 AlignmentI al = new Alignment(newseqs);
146 al.padGaps(); // ensure we look aligned.
147 al.setDataset(dataset);
148 translateAlignedAnnotations(annotations, al, codons);
149 al.addCodonFrame(codons);
154 * fake the collection of DbRefs with associated exon mappings to identify if
155 * a translation would generate distinct product in the currently selected
162 public static boolean canTranslate(SequenceI[] selection,
165 for (int gd = 0; gd < selection.length; gd++)
167 SequenceI dna = selection[gd];
168 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
169 .selectRefs(dna.getDBRef(),
170 jalview.datamodel.DBRefSource.DNACODINGDBS);
173 // intersect with pep
174 // intersect with pep
175 Vector mappedrefs = new Vector();
176 DBRefEntry[] refs = dna.getDBRef();
177 for (int d = 0; d < refs.length; d++)
179 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
180 && refs[d].getMap().getMap().getFromRatio() == 3
181 && refs[d].getMap().getMap().getToRatio() == 1)
183 mappedrefs.addElement(refs[d]); // add translated protein maps
186 dnarefs = new DBRefEntry[mappedrefs.size()];
187 mappedrefs.copyInto(dnarefs);
188 for (int d = 0; d < dnarefs.length; d++)
190 Mapping mp = dnarefs[d].getMap();
193 for (int vc = 0; vc < viscontigs.length; vc += 2)
195 int[] mpr = mp.locateMappedRange(viscontigs[vc],
210 * generate a set of translated protein products from annotated sequenceI
214 * @param gapCharacter
216 * destination dataset for translated sequences
221 public static AlignmentI CdnaTranslate(SequenceI[] selection,
222 int viscontigs[], char gapCharacter, Alignment dataset)
225 Vector cdnasqs = new Vector();
226 Vector cdnasqi = new Vector();
227 Vector cdnaprod = new Vector();
228 for (int gd = 0; gd < selection.length; gd++)
230 SequenceI dna = selection[gd];
231 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
232 .selectRefs(dna.getDBRef(),
233 jalview.datamodel.DBRefSource.DNACODINGDBS);
236 // intersect with pep
237 Vector mappedrefs = new Vector();
238 DBRefEntry[] refs = dna.getDBRef();
239 for (int d = 0; d < refs.length; d++)
241 if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
242 && refs[d].getMap().getMap().getFromRatio() == 3
243 && refs[d].getMap().getMap().getToRatio() == 1)
245 mappedrefs.addElement(refs[d]); // add translated protein maps
248 dnarefs = new DBRefEntry[mappedrefs.size()];
249 mappedrefs.copyInto(dnarefs);
250 for (int d = 0; d < dnarefs.length; d++)
252 Mapping mp = dnarefs[d].getMap();
253 StringBuffer sqstr = new StringBuffer();
256 Mapping intersect = mp.intersectVisContigs(viscontigs);
257 // generate seqstring for this sequence based on mapping
259 if (sqstr.length() > alwidth)
260 alwidth = sqstr.length();
261 cdnasqs.addElement(sqstr.toString());
262 cdnasqi.addElement(dna);
263 cdnaprod.addElement(intersect);
267 SequenceI[] cdna = new SequenceI[cdnasqs.size()];
268 DBRefEntry[] prods = new DBRefEntry[cdnaprod.size()];
269 String[] xons = new String[cdnasqs.size()];
270 cdnasqs.copyInto(xons);
271 cdnaprod.copyInto(prods);
272 cdnasqi.copyInto(cdna);
273 return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,
274 null, alwidth, dataset);
280 * translate na alignment annotations onto translated amino acid alignment al
281 * using codon mapping codons
287 public static void translateAlignedAnnotations(
288 AlignmentAnnotation[] annotations, AlignmentI al,
289 AlignedCodonFrame codons)
291 // //////////////////////////////
292 // Copy annotations across
294 // Can only do this for columns with consecutive codons, or where
295 // annotation is sequence associated.
298 if (annotations != null)
300 for (int i = 0; i < annotations.length; i++)
302 // Skip any autogenerated annotation
303 if (annotations[i].autoCalculated)
308 aSize = codons.getaaWidth(); // aa alignment width.
309 jalview.datamodel.Annotation[] anots = (annotations[i].annotations == null) ? null
310 : new jalview.datamodel.Annotation[aSize];
313 for (a = 0; a < aSize; a++)
315 // process through codon map.
316 if (codons.codons[a] != null
317 && codons.codons[a][0] == (codons.codons[a][2] - 2))
319 anots[a] = getCodonAnnotation(codons.codons[a],
320 annotations[i].annotations);
325 jalview.datamodel.AlignmentAnnotation aa = new jalview.datamodel.AlignmentAnnotation(
326 annotations[i].label, annotations[i].description, anots);
327 aa.graph = annotations[i].graph;
328 aa.graphGroup = annotations[i].graphGroup;
329 aa.graphHeight = annotations[i].graphHeight;
330 if (annotations[i].getThreshold() != null)
332 aa.setThreshold(new jalview.datamodel.GraphLine(annotations[i]
335 if (annotations[i].hasScore)
337 aa.setScore(annotations[i].getScore());
339 if (annotations[i].sequenceRef != null)
341 SequenceI aaSeq = codons
342 .getAaForDnaSeq(annotations[i].sequenceRef);
345 // aa.compactAnnotationArray(); // throw away alignment annotation
347 aa.setSequenceRef(aaSeq);
348 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true); // rebuild
350 aa.adjustForAlignment();
351 aaSeq.addAlignmentAnnotation(aa);
355 al.addAnnotation(aa);
360 private static Annotation getCodonAnnotation(int[] is,
361 Annotation[] annotations)
363 // Have a look at all the codon positions for annotation and put the first
364 // one found into the translated annotation pos.
366 Annotation annot = null;
367 for (int p = 0; p < 3; p++)
369 if (annotations[is[p]] != null)
373 annot = new Annotation(annotations[is[p]]);
379 Annotation cpy = new Annotation(annotations[is[p]]);
380 if (annot.colour == null)
382 annot.colour = cpy.colour;
384 if (annot.description == null || annot.description.length() == 0)
386 annot.description = cpy.description;
388 if (annot.displayCharacter == null)
390 annot.displayCharacter = cpy.displayCharacter;
392 if (annot.secondaryStructure == 0)
394 annot.secondaryStructure = cpy.secondaryStructure;
396 annot.value += cpy.value;
403 annot.value /= (float) contrib;
409 * Translate a na sequence
412 * sequence displayed under viscontigs visible columns
414 * ORF read in some global alignment reference frame
416 * mapping from global reference frame to visible seqstring ORF read
418 * Definition of global ORF alignment reference frame
419 * @param gapCharacter
421 * @return sequence ready to be added to alignment.
423 public static SequenceI translateCodingRegion(SequenceI selection,
424 String seqstring, int[] viscontigs, AlignedCodonFrame codons,
425 char gapCharacter, DBRefEntry product)
427 java.util.List skip = new ArrayList();
428 int skipint[] = null;
429 ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
431 int vc, scontigs[] = new int[viscontigs.length];
433 for (vc = 0; vc < viscontigs.length; vc += 2)
437 vismapping.addShift(npos, viscontigs[vc]);
442 vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
444 scontigs[vc] = viscontigs[vc];
445 scontigs[vc + 1] = viscontigs[vc + 1];
448 StringBuffer protein = new StringBuffer();
449 String seq = seqstring.replace('U', 'T');
450 char codon[] = new char[3];
451 int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
454 for (npos = 0, nend = seq.length(); npos < nend; npos++)
456 if (!jalview.util.Comparison.isGap(seq.charAt(npos)))
458 cdp[rf] = npos; // store position
459 codon[rf++] = seq.charAt(npos); // store base
461 // filled an RF yet ?
464 String aa = ResidueProperties.codonTranslate(new String(codon));
468 aa = String.valueOf(gapCharacter);
481 skipint[0] = vismapping.shift(skipint[0]);
482 skipint[1] = vismapping.shift(skipint[1]);
483 for (vc = 0; vc < scontigs.length; vc += 2)
485 if (scontigs[vc + 1] < skipint[0])
489 if (scontigs[vc] <= skipint[0])
491 if (skipint[0] == scontigs[vc])
497 int[] t = new int[scontigs.length + 2];
498 System.arraycopy(scontigs, 0, t, 0, vc - 1);
506 if (aa.equals("STOP"))
512 // insert/delete gaps prior to this codon - if necessary
513 boolean findpos = true;
516 // first ensure that the codons array is long enough.
517 codons.checkCodonFrameWidth(aspos);
518 // now check to see if we place the aa at the current aspos in the
520 switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
523 codons.insertAAGap(aspos, gapCharacter);
527 // this aa appears after the aligned codons at aspos, so prefix it
529 aa = "" + gapCharacter + aa;
531 // if (aspos >= codons.aaWidth)
532 // codons.aaWidth = aspos + 1;
533 break; // check the next position for alignment
535 // codon aligns at aspos position.
539 // codon aligns with all other sequence residues found at aspos
542 if (codons.codons[aspos] == null)
544 // mark this column as aligning to this aligned reading frame
545 codons.codons[aspos] = new int[]
546 { cdp[0], cdp[1], cdp[2] };
548 if (aspos >= codons.aaWidth)
550 // update maximum alignment width
551 // (we can do this without calling checkCodonFrameWidth because it was
552 // already done above)
553 codons.setAaWidth(aspos);
555 // ready for next translated reading frame alignment position (if any)
561 SequenceI newseq = new Sequence(selection.getName(),
565 jalview.bin.Cache.log
566 .debug("trimming contigs for incomplete terminal codon.");
567 // map and trim contigs to ORF region
568 vc = scontigs.length - 1;
569 lastnpos = vismapping.shift(lastnpos); // place npos in context of
570 // whole dna alignment (rather
571 // than visible contigs)
572 // incomplete ORF could be broken over one or two visible contig
574 while (vc >= 0 && scontigs[vc] > lastnpos)
576 if (vc > 0 && scontigs[vc - 1] > lastnpos)
582 // correct last interval in list.
583 scontigs[vc] = lastnpos;
587 if (vc > 0 && (vc + 1) < scontigs.length)
589 // truncate map list to just vc elements
590 int t[] = new int[vc + 1];
591 System.arraycopy(scontigs, 0, t, 0, vc + 1);
597 if (scontigs != null)
600 // map scontigs to actual sequence positions on selection
601 for (vc = 0; vc < scontigs.length; vc += 2)
603 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
604 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
605 if (scontigs[vc + 1] == selection.getEnd())
608 // trim trailing empty intervals.
609 if ((vc + 2) < scontigs.length)
611 int t[] = new int[vc + 2];
612 System.arraycopy(scontigs, 0, t, 0, vc + 2);
616 * delete intervals in scontigs which are not translated. 1. map skip
617 * into sequence position intervals 2. truncate existing ranges and add
618 * new ranges to exclude untranslated regions. if (skip.size()>0) {
619 * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
620 * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
621 * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
622 * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
623 * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
624 * range } else { // truncate range and create new one if necessary iv =
625 * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
626 * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
627 * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
629 MapList map = new MapList(scontigs, new int[]
630 { 1, resSize }, 3, 1);
632 // update newseq as if it was generated as mapping from product
636 newseq.setName(product.getSource() + "|"
637 + product.getAccessionId());
638 if (product.getMap() != null)
640 // Mapping mp = product.getMap();
641 // newseq.setStart(mp.getPosition(scontigs[0]));
643 // .getPosition(scontigs[scontigs.length - 1]));
646 transferCodedFeatures(selection, newseq, map, null, null);
647 SequenceI rseq = newseq.deriveSequence(); // construct a dataset
648 // sequence for our new
649 // peptide, regardless.
650 // store a mapping (this actually stores a mapping between the dataset
651 // sequences for the two sequences
652 codons.addMap(selection, rseq, map);
656 // register the mapping somehow
662 * Given a peptide newly translated from a dna sequence, copy over and set any
663 * features on the peptide from the DNA. If featureTypes is null, all features
664 * on the dna sequence are searched (rather than just the displayed ones), and
665 * similarly for featureGroups.
670 * @param featureTypes
671 * hash who's keys are the displayed feature type strings
672 * @param featureGroups
673 * hash where keys are feature groups and values are Boolean objects
674 * indicating if they are displayed.
676 private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
677 MapList map, Hashtable featureTypes, Hashtable featureGroups)
679 SequenceFeature[] sf = dna.getDatasetSequence().getSequenceFeatures();
681 jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
682 .selectRefs(dna.getDBRef(),
683 jalview.datamodel.DBRefSource.DNACODINGDBS);
686 // intersect with pep
687 for (int d = 0; d < dnarefs.length; d++)
689 Mapping mp = dnarefs[d].getMap();
697 for (int f = 0; f < sf.length; f++)
699 fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
700 .get(sf[f].featureGroup));
701 if ((featureTypes == null || featureTypes.containsKey(sf[f]
702 .getType())) && (fgstate == null || fgstate.booleanValue()))
704 if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
706 // if (map.intersectsFrom(sf[f].begin, sf[f].end))