Jalview 2.8 Source Header
[jalview.git] / src / jalview / analysis / Dna.java
1 /*
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
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.analysis;
19
20 import java.util.Enumeration;
21 import java.util.Hashtable;
22 import java.util.Vector;
23
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.ColumnSelection;
30 import jalview.datamodel.DBRefEntry;
31 import jalview.datamodel.FeatureProperties;
32 import jalview.datamodel.Mapping;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36 import jalview.schemes.ResidueProperties;
37 import jalview.util.MapList;
38 import jalview.util.ShiftList;
39
40 public class Dna
41 {
42   /**
43    * 
44    * @param cdp1
45    * @param cdp2
46    * @return -1 if cdp1 aligns before cdp2, 0 if in the same column or cdp2 is
47    *         null, +1 if after cdp2
48    */
49   private static int compare_codonpos(int[] cdp1, int[] cdp2)
50   {
51     if (cdp2 == null
52             || (cdp1[0] == cdp2[0] && cdp1[1] == cdp2[1] && cdp1[2] == cdp2[2]))
53       return 0;
54     if (cdp1[0] < cdp2[0] || cdp1[1] < cdp2[1] || cdp1[2] < cdp2[2])
55       return -1; // one base in cdp1 precedes the corresponding base in the
56     // other codon
57     return 1; // one base in cdp1 appears after the corresponding base in the
58     // other codon.
59   }
60
61   /**
62    * DNA->mapped protein sequence alignment translation given set of sequences
63    * 1. id distinct coding regions within selected region for each sequence 2.
64    * generate peptides based on inframe (or given) translation or (optionally
65    * and where specified) out of frame translations (annotated appropriately) 3.
66    * align peptides based on codon alignment
67    */
68   /**
69    * id potential products from dna 1. search for distinct products within
70    * selected region for each selected sequence 2. group by associated DB type.
71    * 3. return as form for input into above function
72    */
73   /**
74    * 
75    */
76   /**
77    * create a new alignment of protein sequences by an inframe translation of
78    * the provided NA sequences
79    * 
80    * @param selection
81    * @param seqstring
82    * @param viscontigs
83    * @param gapCharacter
84    * @param annotations
85    * @param aWidth
86    * @param dataset
87    *          destination dataset for translated sequences and mappings
88    * @return
89    */
90   public static AlignmentI CdnaTranslate(SequenceI[] selection,
91           String[] seqstring, int viscontigs[], char gapCharacter,
92           AlignmentAnnotation[] annotations, int aWidth, Alignment dataset)
93   {
94     return CdnaTranslate(selection, seqstring, null, viscontigs,
95             gapCharacter, annotations, aWidth, dataset);
96   }
97
98   /**
99    * 
100    * @param selection
101    * @param seqstring
102    * @param product
103    *          - array of DbRefEntry objects from which exon map in seqstring is
104    *          derived
105    * @param viscontigs
106    * @param gapCharacter
107    * @param annotations
108    * @param aWidth
109    * @param dataset
110    * @return
111    */
112   public static AlignmentI CdnaTranslate(SequenceI[] selection,
113           String[] seqstring, DBRefEntry[] product, int viscontigs[],
114           char gapCharacter, AlignmentAnnotation[] annotations, int aWidth,
115           Alignment dataset)
116   {
117     AlignedCodonFrame codons = new AlignedCodonFrame(aWidth); // stores hash of
118     // subsequent
119     // positions for
120     // each codon
121     // start position
122     // in alignment
123     int s, sSize = selection.length;
124     Vector pepseqs = new Vector();
125     for (s = 0; s < sSize; s++)
126     {
127       SequenceI newseq = translateCodingRegion(selection[s], seqstring[s],
128               viscontigs, codons, gapCharacter,
129               (product != null) ? product[s] : null); // possibly anonymous
130       // product
131       if (newseq != null)
132       {
133         pepseqs.addElement(newseq);
134         SequenceI ds = newseq;
135         while (ds.getDatasetSequence() != null)
136         {
137           ds = ds.getDatasetSequence();
138         }
139         dataset.addSequence(ds);
140       }
141     }
142     if (codons.aaWidth == 0)
143       return null;
144     SequenceI[] newseqs = new SequenceI[pepseqs.size()];
145     pepseqs.copyInto(newseqs);
146     AlignmentI al = new Alignment(newseqs);
147     al.padGaps(); // ensure we look aligned.
148     al.setDataset(dataset);
149     translateAlignedAnnotations(annotations, al, codons);
150     al.addCodonFrame(codons);
151     return al;
152   }
153
154   /**
155    * fake the collection of DbRefs with associated exon mappings to identify if
156    * a translation would generate distinct product in the currently selected
157    * region.
158    * 
159    * @param selection
160    * @param viscontigs
161    * @return
162    */
163   public static boolean canTranslate(SequenceI[] selection,
164           int viscontigs[])
165   {
166     for (int gd = 0; gd < selection.length; gd++)
167     {
168       SequenceI dna = selection[gd];
169       jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
170               .selectRefs(dna.getDBRef(),
171                       jalview.datamodel.DBRefSource.DNACODINGDBS);
172       if (dnarefs != null)
173       {
174         // intersect with pep
175         // intersect with pep
176         Vector mappedrefs = new Vector();
177         DBRefEntry[] refs = dna.getDBRef();
178         for (int d = 0; d < refs.length; d++)
179         {
180           if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
181                   && refs[d].getMap().getMap().getFromRatio() == 3
182                   && refs[d].getMap().getMap().getToRatio() == 1)
183           {
184             mappedrefs.addElement(refs[d]); // add translated protein maps
185           }
186         }
187         dnarefs = new DBRefEntry[mappedrefs.size()];
188         mappedrefs.copyInto(dnarefs);
189         for (int d = 0; d < dnarefs.length; d++)
190         {
191           Mapping mp = dnarefs[d].getMap();
192           if (mp != null)
193           {
194             for (int vc = 0; vc < viscontigs.length; vc += 2)
195             {
196               int[] mpr = mp.locateMappedRange(viscontigs[vc],
197                       viscontigs[vc + 1]);
198               if (mpr != null)
199               {
200                 return true;
201               }
202             }
203           }
204         }
205       }
206     }
207     return false;
208   }
209
210   /**
211    * generate a set of translated protein products from annotated sequenceI
212    * 
213    * @param selection
214    * @param viscontigs
215    * @param gapCharacter
216    * @param dataset
217    *          destination dataset for translated sequences
218    * @param annotations
219    * @param aWidth
220    * @return
221    */
222   public static AlignmentI CdnaTranslate(SequenceI[] selection,
223           int viscontigs[], char gapCharacter, Alignment dataset)
224   {
225     int alwidth = 0;
226     Vector cdnasqs = new Vector();
227     Vector cdnasqi = new Vector();
228     Vector cdnaprod = new Vector();
229     for (int gd = 0; gd < selection.length; gd++)
230     {
231       SequenceI dna = selection[gd];
232       jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
233               .selectRefs(dna.getDBRef(),
234                       jalview.datamodel.DBRefSource.DNACODINGDBS);
235       if (dnarefs != null)
236       {
237         // intersect with pep
238         Vector mappedrefs = new Vector();
239         DBRefEntry[] refs = dna.getDBRef();
240         for (int d = 0; d < refs.length; d++)
241         {
242           if (refs[d].getMap() != null && refs[d].getMap().getMap() != null
243                   && refs[d].getMap().getMap().getFromRatio() == 3
244                   && refs[d].getMap().getMap().getToRatio() == 1)
245           {
246             mappedrefs.addElement(refs[d]); // add translated protein maps
247           }
248         }
249         dnarefs = new DBRefEntry[mappedrefs.size()];
250         mappedrefs.copyInto(dnarefs);
251         for (int d = 0; d < dnarefs.length; d++)
252         {
253           Mapping mp = dnarefs[d].getMap();
254           StringBuffer sqstr = new StringBuffer();
255           if (mp != null)
256           {
257             Mapping intersect = mp.intersectVisContigs(viscontigs);
258             // generate seqstring for this sequence based on mapping
259
260             if (sqstr.length() > alwidth)
261               alwidth = sqstr.length();
262             cdnasqs.addElement(sqstr.toString());
263             cdnasqi.addElement(dna);
264             cdnaprod.addElement(intersect);
265           }
266         }
267       }
268       SequenceI[] cdna = new SequenceI[cdnasqs.size()];
269       DBRefEntry[] prods = new DBRefEntry[cdnaprod.size()];
270       String[] xons = new String[cdnasqs.size()];
271       cdnasqs.copyInto(xons);
272       cdnaprod.copyInto(prods);
273       cdnasqi.copyInto(cdna);
274       return CdnaTranslate(cdna, xons, prods, viscontigs, gapCharacter,
275               null, alwidth, dataset);
276     }
277     return null;
278   }
279
280   /**
281    * translate na alignment annotations onto translated amino acid alignment al
282    * using codon mapping codons
283    * 
284    * @param annotations
285    * @param al
286    * @param codons
287    */
288   public static void translateAlignedAnnotations(
289           AlignmentAnnotation[] annotations, AlignmentI al,
290           AlignedCodonFrame codons)
291   {
292     // //////////////////////////////
293     // Copy annotations across
294     //
295     // Can only do this for columns with consecutive codons, or where
296     // annotation is sequence associated.
297
298     int pos, a, aSize;
299     if (annotations != null)
300     {
301       for (int i = 0; i < annotations.length; i++)
302       {
303         // Skip any autogenerated annotation
304         if (annotations[i].autoCalculated)
305         {
306           continue;
307         }
308
309         aSize = codons.getaaWidth(); // aa alignment width.
310         jalview.datamodel.Annotation[] anots = (annotations[i].annotations == null) ? null
311                 : new jalview.datamodel.Annotation[aSize];
312         if (anots != null)
313         {
314           for (a = 0; a < aSize; a++)
315           {
316             // process through codon map.
317             if (codons.codons[a] != null
318                     && codons.codons[a][0] == (codons.codons[a][2] - 2))
319             {
320               anots[a] = getCodonAnnotation(codons.codons[a],
321                       annotations[i].annotations);
322             }
323           }
324         }
325
326         jalview.datamodel.AlignmentAnnotation aa = new jalview.datamodel.AlignmentAnnotation(
327                 annotations[i].label, annotations[i].description, anots);
328         aa.graph = annotations[i].graph;
329         aa.graphGroup = annotations[i].graphGroup;
330         aa.graphHeight = annotations[i].graphHeight;
331         if (annotations[i].getThreshold() != null)
332         {
333           aa.setThreshold(new jalview.datamodel.GraphLine(annotations[i]
334                   .getThreshold()));
335         }
336         if (annotations[i].hasScore)
337         {
338           aa.setScore(annotations[i].getScore());
339         }
340         if (annotations[i].sequenceRef != null)
341         {
342           SequenceI aaSeq = codons
343                   .getAaForDnaSeq(annotations[i].sequenceRef);
344           if (aaSeq != null)
345           {
346             // aa.compactAnnotationArray(); // throw away alignment annotation
347             // positioning
348             aa.setSequenceRef(aaSeq);
349             aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true); // rebuild
350             // mapping
351             aa.adjustForAlignment();
352             aaSeq.addAlignmentAnnotation(aa);
353           }
354
355         }
356         al.addAnnotation(aa);
357       }
358     }
359   }
360
361   private static Annotation getCodonAnnotation(int[] is,
362           Annotation[] annotations)
363   {
364     // Have a look at all the codon positions for annotation and put the first
365     // one found into the translated annotation pos.
366     int contrib = 0;
367     Annotation annot = null;
368     for (int p = 0; p < 3; p++)
369     {
370       if (annotations[is[p]] != null)
371       {
372         if (annot == null)
373         {
374           annot = new Annotation(annotations[is[p]]);
375           contrib = 1;
376         }
377         else
378         {
379           // merge with last
380           Annotation cpy = new Annotation(annotations[is[p]]);
381           if (annot.colour == null)
382           {
383             annot.colour = cpy.colour;
384           }
385           if (annot.description == null || annot.description.length() == 0)
386           {
387             annot.description = cpy.description;
388           }
389           if (annot.displayCharacter == null)
390           {
391             annot.displayCharacter = cpy.displayCharacter;
392           }
393           if (annot.secondaryStructure == 0)
394           {
395             annot.secondaryStructure = cpy.secondaryStructure;
396           }
397           annot.value += cpy.value;
398           contrib++;
399         }
400       }
401     }
402     if (contrib > 1)
403     {
404       annot.value /= (float) contrib;
405     }
406     return annot;
407   }
408
409   /**
410    * Translate a na sequence
411    * 
412    * @param selection
413    *          sequence displayed under viscontigs visible columns
414    * @param seqstring
415    *          ORF read in some global alignment reference frame
416    * @param viscontigs
417    *          mapping from global reference frame to visible seqstring ORF read
418    * @param codons
419    *          Definition of global ORF alignment reference frame
420    * @param gapCharacter
421    * @param newSeq
422    * @return sequence ready to be added to alignment.
423    */
424   public static SequenceI translateCodingRegion(SequenceI selection,
425           String seqstring, int[] viscontigs, AlignedCodonFrame codons,
426           char gapCharacter, DBRefEntry product)
427   {
428     Vector skip = new Vector();
429     int skipint[] = null;
430     ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
431     // intervals
432     int vc, scontigs[] = new int[viscontigs.length];
433     int npos = 0;
434     for (vc = 0; vc < viscontigs.length; vc += 2)
435     {
436       if (vc == 0)
437       {
438         vismapping.addShift(npos, viscontigs[vc]);
439       }
440       else
441       {
442         // hidden region
443         vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
444       }
445       scontigs[vc] = viscontigs[vc];
446       scontigs[vc + 1] = viscontigs[vc + 1];
447     }
448
449     StringBuffer protein = new StringBuffer();
450     String seq = seqstring.replace('U', 'T');
451     char codon[] = new char[3];
452     int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
453     int aspos = 0;
454     int resSize = 0;
455     for (npos = 0, nend = seq.length(); npos < nend; npos++)
456     {
457       if (!jalview.util.Comparison.isGap(seq.charAt(npos)))
458       {
459         cdp[rf] = npos; // store position
460         codon[rf++] = seq.charAt(npos); // store base
461       }
462       // filled an RF yet ?
463       if (rf == 3)
464       {
465         String aa = ResidueProperties.codonTranslate(new String(codon));
466         rf = 0;
467         if (aa == null)
468         {
469           aa = String.valueOf(gapCharacter);
470           if (skipint == null)
471           {
472             skipint = new int[]
473             { cdp[0], cdp[2] };
474           }
475           skipint[1] = cdp[2];
476         }
477         else
478         {
479           if (skipint != null)
480           {
481             // edit scontigs
482             skipint[0] = vismapping.shift(skipint[0]);
483             skipint[1] = vismapping.shift(skipint[1]);
484             for (vc = 0; vc < scontigs.length; vc += 2)
485             {
486               if (scontigs[vc + 1] < skipint[0])
487               {
488                 continue;
489               }
490               if (scontigs[vc] <= skipint[0])
491               {
492                 if (skipint[0] == scontigs[vc])
493                 {
494
495                 }
496                 else
497                 {
498                   int[] t = new int[scontigs.length + 2];
499                   System.arraycopy(scontigs, 0, t, 0, vc - 1);
500                   // scontigs[vc]; //
501                 }
502               }
503             }
504             skip.addElement(skipint);
505             skipint = null;
506           }
507           if (aa.equals("STOP"))
508           {
509             aa = "X";
510           }
511           resSize++;
512         }
513         // insert/delete gaps prior to this codon - if necessary
514         boolean findpos = true;
515         while (findpos)
516         {
517           // first ensure that the codons array is long enough.
518           codons.checkCodonFrameWidth(aspos);
519           // now check to see if we place the aa at the current aspos in the
520           // protein alignment
521           switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
522           {
523           case -1:
524             codons.insertAAGap(aspos, gapCharacter);
525             findpos = false;
526             break;
527           case +1:
528             // this aa appears after the aligned codons at aspos, so prefix it
529             // with a gap
530             aa = "" + gapCharacter + aa;
531             aspos++;
532             // if (aspos >= codons.aaWidth)
533             // codons.aaWidth = aspos + 1;
534             break; // check the next position for alignment
535           case 0:
536             // codon aligns at aspos position.
537             findpos = false;
538           }
539         }
540         // codon aligns with all other sequence residues found at aspos
541         protein.append(aa);
542         lastnpos = npos;
543         if (codons.codons[aspos] == null)
544         {
545           // mark this column as aligning to this aligned reading frame
546           codons.codons[aspos] = new int[]
547           { cdp[0], cdp[1], cdp[2] };
548         }
549         if (aspos >= codons.aaWidth)
550         {
551           // update maximum alignment width
552           // (we can do this without calling checkCodonFrameWidth because it was
553           // already done above)
554           codons.setAaWidth(aspos);
555         }
556         // ready for next translated reading frame alignment position (if any)
557         aspos++;
558       }
559     }
560     if (resSize > 0)
561     {
562       SequenceI newseq = new Sequence(selection.getName(),
563               protein.toString());
564       if (rf != 0)
565       {
566         jalview.bin.Cache.log
567                 .debug("trimming contigs for incomplete terminal codon.");
568         // map and trim contigs to ORF region
569         vc = scontigs.length - 1;
570         lastnpos = vismapping.shift(lastnpos); // place npos in context of
571         // whole dna alignment (rather
572         // than visible contigs)
573         // incomplete ORF could be broken over one or two visible contig
574         // intervals.
575         while (vc >= 0 && scontigs[vc] > lastnpos)
576         {
577           if (vc > 0 && scontigs[vc - 1] > lastnpos)
578           {
579             vc -= 2;
580           }
581           else
582           {
583             // correct last interval in list.
584             scontigs[vc] = lastnpos;
585           }
586         }
587
588         if (vc > 0 && (vc + 1) < scontigs.length)
589         {
590           // truncate map list to just vc elements
591           int t[] = new int[vc + 1];
592           System.arraycopy(scontigs, 0, t, 0, vc + 1);
593           scontigs = t;
594         }
595         if (vc <= 0)
596           scontigs = null;
597       }
598       if (scontigs != null)
599       {
600         npos = 0;
601         // map scontigs to actual sequence positions on selection
602         for (vc = 0; vc < scontigs.length; vc += 2)
603         {
604           scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
605           scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
606           if (scontigs[vc + 1] == selection.getEnd())
607             break;
608         }
609         // trim trailing empty intervals.
610         if ((vc + 2) < scontigs.length)
611         {
612           int t[] = new int[vc + 2];
613           System.arraycopy(scontigs, 0, t, 0, vc + 2);
614           scontigs = t;
615         }
616         /*
617          * delete intervals in scontigs which are not translated. 1. map skip
618          * into sequence position intervals 2. truncate existing ranges and add
619          * new ranges to exclude untranslated regions. if (skip.size()>0) {
620          * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
621          * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
622          * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
623          * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
624          * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
625          * range } else { // truncate range and create new one if necessary iv =
626          * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
627          * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
628          * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
629          */
630         MapList map = new MapList(scontigs, new int[]
631         { 1, resSize }, 3, 1);
632
633         // update newseq as if it was generated as mapping from product
634
635         if (product != null)
636         {
637           newseq.setName(product.getSource() + "|"
638                   + product.getAccessionId());
639           if (product.getMap() != null)
640           {
641             // Mapping mp = product.getMap();
642             // newseq.setStart(mp.getPosition(scontigs[0]));
643             // newseq.setEnd(mp
644             // .getPosition(scontigs[scontigs.length - 1]));
645           }
646         }
647         transferCodedFeatures(selection, newseq, map, null, null);
648         SequenceI rseq = newseq.deriveSequence(); // construct a dataset
649         // sequence for our new
650         // peptide, regardless.
651         // store a mapping (this actually stores a mapping between the dataset
652         // sequences for the two sequences
653         codons.addMap(selection, rseq, map);
654         return rseq;
655       }
656     }
657     // register the mapping somehow
658     //
659     return null;
660   }
661
662   /**
663    * Given a peptide newly translated from a dna sequence, copy over and set any
664    * features on the peptide from the DNA. If featureTypes is null, all features
665    * on the dna sequence are searched (rather than just the displayed ones), and
666    * similarly for featureGroups.
667    * 
668    * @param dna
669    * @param pep
670    * @param map
671    * @param featureTypes
672    *          hash who's keys are the displayed feature type strings
673    * @param featureGroups
674    *          hash where keys are feature groups and values are Boolean objects
675    *          indicating if they are displayed.
676    */
677   private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
678           MapList map, Hashtable featureTypes, Hashtable featureGroups)
679   {
680     SequenceFeature[] sf = dna.getDatasetSequence().getSequenceFeatures();
681     Boolean fgstate;
682     jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
683             .selectRefs(dna.getDBRef(),
684                     jalview.datamodel.DBRefSource.DNACODINGDBS);
685     if (dnarefs != null)
686     {
687       // intersect with pep
688       for (int d = 0; d < dnarefs.length; d++)
689       {
690         Mapping mp = dnarefs[d].getMap();
691         if (mp != null)
692         {
693         }
694       }
695     }
696     if (sf != null)
697     {
698       for (int f = 0; f < sf.length; f++)
699       {
700         fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
701                 .get(sf[f].featureGroup));
702         if ((featureTypes == null || featureTypes.containsKey(sf[f]
703                 .getType())) && (fgstate == null || fgstate.booleanValue()))
704         {
705           if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
706           {
707             // if (map.intersectsFrom(sf[f].begin, sf[f].end))
708             {
709
710             }
711           }
712         }
713       }
714     }
715   }
716 }