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