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