f610d0889eb5ecfba21593eea8a49ced920e69a7
[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, false); // 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    * @return sequence ready to be added to alignment.
424    * @deprecated Use {@link #translateCodingRegion(SequenceI,String,int[],AlignedCodonFrame,char,DBRefEntry,boolean)} instead
425    */
426   public static SequenceI translateCodingRegion(SequenceI selection,
427           String seqstring, int[] viscontigs, AlignedCodonFrame codons,
428           char gapCharacter, DBRefEntry product)
429   {
430     return translateCodingRegion(selection, seqstring, viscontigs, codons,
431             gapCharacter, product, false);
432   }
433
434   /**
435    * Translate a na sequence
436    * 
437    * @param selection
438    *          sequence displayed under viscontigs visible columns
439    * @param seqstring
440    *          ORF read in some global alignment reference frame
441    * @param viscontigs
442    *          mapping from global reference frame to visible seqstring ORF read
443    * @param codons
444    *          Definition of global ORF alignment reference frame
445    * @param gapCharacter
446    * @param starForStop when true stop codons will translate as '*', otherwise as 'X'  
447    * @return sequence ready to be added to alignment.
448    */
449   public static SequenceI translateCodingRegion(SequenceI selection,
450           String seqstring, int[] viscontigs, AlignedCodonFrame codons,
451           char gapCharacter, DBRefEntry product, final boolean starForStop)
452   {
453     java.util.List skip = new ArrayList();
454     int skipint[] = null;
455     ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring
456     // intervals
457     int vc, scontigs[] = new int[viscontigs.length];
458     int npos = 0;
459     for (vc = 0; vc < viscontigs.length; vc += 2)
460     {
461       if (vc == 0)
462       {
463         vismapping.addShift(npos, viscontigs[vc]);
464       }
465       else
466       {
467         // hidden region
468         vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1);
469       }
470       scontigs[vc] = viscontigs[vc];
471       scontigs[vc + 1] = viscontigs[vc + 1];
472     }
473
474     StringBuffer protein = new StringBuffer();
475     String seq = seqstring.replace('U', 'T');
476     char codon[] = new char[3];
477     int cdp[] = new int[3], rf = 0, lastnpos = 0, nend;
478     int aspos = 0;
479     int resSize = 0;
480     for (npos = 0, nend = seq.length(); npos < nend; npos++)
481     {
482       if (!jalview.util.Comparison.isGap(seq.charAt(npos)))
483       {
484         cdp[rf] = npos; // store position
485         codon[rf++] = seq.charAt(npos); // store base
486       }
487       // filled an RF yet ?
488       if (rf == 3)
489       {
490         String aa = ResidueProperties.codonTranslate(new String(codon));
491         rf = 0;
492         if (aa == null)
493         {
494           aa = String.valueOf(gapCharacter);
495           if (skipint == null)
496           {
497             skipint = new int[]
498             { cdp[0], cdp[2] };
499           }
500           skipint[1] = cdp[2];
501         }
502         else
503         {
504           if (skipint != null)
505           {
506             // edit scontigs
507             skipint[0] = vismapping.shift(skipint[0]);
508             skipint[1] = vismapping.shift(skipint[1]);
509             for (vc = 0; vc < scontigs.length; )
510             {
511               if (scontigs[vc + 1] < skipint[0])
512               {
513                 // before skipint starts
514                 vc += 2;
515                 continue;
516               }
517               if (scontigs[vc]>skipint[1])
518               {
519                 // finished editing so
520                 break;
521               }
522               // Edit the contig list to include the skipped region which did not translate
523               int[] t;
524               // from : s1 e1 s2 e2 s3 e3
525               // to s:  s1 e1 s2 k0 k1 e2 s3 e3
526               // list increases by one unless one boundary (s2==k0 or e2==k1) matches, and decreases by one if skipint intersects whole visible contig 
527               if (scontigs[vc] <= skipint[0])
528               {
529                 if (skipint[0] == scontigs[vc])
530                 {
531                   // skipint at start of contig
532                   // shift the start of this contig
533                   if (scontigs[vc + 1] > skipint[1])
534                   {
535                     scontigs[vc] = skipint[1];
536                     vc+=2;
537                   }
538                   else
539                   {
540                     if (scontigs[vc+1]==skipint[1])
541                     {
542                       // remove the contig
543                       t = new int[scontigs.length - 2];
544                       if (vc > 0)
545                       {
546                         System.arraycopy(scontigs, 0, t, 0, vc - 1);
547                       }
548                       if (vc + 2 < t.length)
549                       {
550                         System.arraycopy(scontigs, vc + 2, t, vc, t.length
551                               - vc + 2);
552                       }
553                       scontigs=t;
554                     } else {
555                       // truncate contig to before the skipint region
556                       scontigs[vc+1] = skipint[0]-1;
557                       vc+=2;
558                     }
559                   }
560                 }
561                 else
562                 {
563                   // scontig starts before start of skipint
564                   if (scontigs[vc+1]<skipint[1]) {
565                     // skipint truncates end of scontig
566                     scontigs[vc+1] = skipint[0]-1; 
567                     vc+=2;
568                   } else {
569                     // divide region to new contigs
570                     t = new int[scontigs.length + 2];
571                     System.arraycopy(scontigs, 0, t, 0, vc +1);
572                     t[vc+1] = skipint[0];
573                     t[vc+2] = skipint[1];
574                     System.arraycopy(scontigs, vc + 1, t, vc+3, scontigs.length-(vc+1));
575                     scontigs=t;
576                     vc+=4;
577                   }
578                 }
579               }
580             }
581             skip.add(skipint);
582             skipint = null;
583           }
584           if (aa.equals("STOP"))
585           {
586             aa = starForStop ? "*" : "X";
587           }
588           resSize++;
589         }
590         // insert/delete gaps prior to this codon - if necessary
591         boolean findpos = true;
592         while (findpos)
593         {
594           // first ensure that the codons array is long enough.
595           codons.checkCodonFrameWidth(aspos);
596           // now check to see if we place the aa at the current aspos in the
597           // protein alignment
598           switch (Dna.compare_codonpos(cdp, codons.codons[aspos]))
599           {
600           case -1:
601             codons.insertAAGap(aspos, gapCharacter);
602             findpos = false;
603             break;
604           case +1:
605             // this aa appears after the aligned codons at aspos, so prefix it
606             // with a gap
607             aa = "" + gapCharacter + aa;
608             aspos++;
609             // if (aspos >= codons.aaWidth)
610             // codons.aaWidth = aspos + 1;
611             break; // check the next position for alignment
612           case 0:
613             // codon aligns at aspos position.
614             findpos = false;
615           }
616         }
617         // codon aligns with all other sequence residues found at aspos
618         protein.append(aa);
619         lastnpos = npos;
620         if (codons.codons[aspos] == null)
621         {
622           // mark this column as aligning to this aligned reading frame
623           codons.codons[aspos] = new int[]
624           { cdp[0], cdp[1], cdp[2] };
625         }
626         if (aspos >= codons.aaWidth)
627         {
628           // update maximum alignment width
629           // (we can do this without calling checkCodonFrameWidth because it was
630           // already done above)
631           codons.setAaWidth(aspos);
632         }
633         // ready for next translated reading frame alignment position (if any)
634         aspos++;
635       }
636     }
637     if (resSize > 0)
638     {
639       SequenceI newseq = new Sequence(selection.getName(),
640               protein.toString());
641       if (rf != 0)
642       {
643         if (jalview.bin.Cache.log!=null) {
644           jalview.bin.Cache.log.debug("trimming contigs for incomplete terminal codon.");
645         } else {
646           System.err.println("trimming contigs for incomplete terminal codon.");
647         }
648         // map and trim contigs to ORF region
649         vc = scontigs.length - 1;
650         lastnpos = vismapping.shift(lastnpos); // place npos in context of
651         // whole dna alignment (rather
652         // than visible contigs)
653         // incomplete ORF could be broken over one or two visible contig
654         // intervals.
655         while (vc >= 0 && scontigs[vc] > lastnpos)
656         {
657           if (vc > 0 && scontigs[vc - 1] > lastnpos)
658           {
659             vc -= 2;
660           }
661           else
662           {
663             // correct last interval in list.
664             scontigs[vc] = lastnpos;
665           }
666         }
667
668         if (vc > 0 && (vc + 1) < scontigs.length)
669         {
670           // truncate map list to just vc elements
671           int t[] = new int[vc + 1];
672           System.arraycopy(scontigs, 0, t, 0, vc + 1);
673           scontigs = t;
674         }
675         if (vc <= 0)
676           scontigs = null;
677       }
678       if (scontigs != null)
679       {
680         npos = 0;
681         // map scontigs to actual sequence positions on selection
682         for (vc = 0; vc < scontigs.length; vc += 2)
683         {
684           scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
685           scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
686           if (scontigs[vc + 1] == selection.getEnd())
687             break;
688         }
689         // trim trailing empty intervals.
690         if ((vc + 2) < scontigs.length)
691         {
692           int t[] = new int[vc + 2];
693           System.arraycopy(scontigs, 0, t, 0, vc + 2);
694           scontigs = t;
695         }
696         /*
697          * delete intervals in scontigs which are not translated. 1. map skip
698          * into sequence position intervals 2. truncate existing ranges and add
699          * new ranges to exclude untranslated regions. if (skip.size()>0) {
700          * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
701          * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
702          * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
703          * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
704          * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
705          * range } else { // truncate range and create new one if necessary iv =
706          * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
707          * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
708          * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
709          */
710         MapList map = new MapList(scontigs, new int[]
711         { 1, resSize }, 3, 1);
712
713         // update newseq as if it was generated as mapping from product
714
715         if (product != null)
716         {
717           newseq.setName(product.getSource() + "|"
718                   + product.getAccessionId());
719           if (product.getMap() != null)
720           {
721             // Mapping mp = product.getMap();
722             // newseq.setStart(mp.getPosition(scontigs[0]));
723             // newseq.setEnd(mp
724             // .getPosition(scontigs[scontigs.length - 1]));
725           }
726         }
727         transferCodedFeatures(selection, newseq, map, null, null);
728         SequenceI rseq = newseq.deriveSequence(); // construct a dataset
729         // sequence for our new
730         // peptide, regardless.
731         // store a mapping (this actually stores a mapping between the dataset
732         // sequences for the two sequences
733         codons.addMap(selection, rseq, map);
734         return rseq;
735       }
736     }
737     // register the mapping somehow
738     //
739     return null;
740   }
741
742   /**
743    * Given a peptide newly translated from a dna sequence, copy over and set any
744    * features on the peptide from the DNA. If featureTypes is null, all features
745    * on the dna sequence are searched (rather than just the displayed ones), and
746    * similarly for featureGroups.
747    * 
748    * @param dna
749    * @param pep
750    * @param map
751    * @param featureTypes
752    *          hash who's keys are the displayed feature type strings
753    * @param featureGroups
754    *          hash where keys are feature groups and values are Boolean objects
755    *          indicating if they are displayed.
756    */
757   private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
758           MapList map, Hashtable featureTypes, Hashtable featureGroups)
759   {
760     SequenceFeature[] sf = (dna.getDatasetSequence()!=null ? dna.getDatasetSequence() : dna).getSequenceFeatures();
761     Boolean fgstate;
762     jalview.datamodel.DBRefEntry[] dnarefs = jalview.util.DBRefUtils
763             .selectRefs(dna.getDBRef(),
764                     jalview.datamodel.DBRefSource.DNACODINGDBS);
765     if (dnarefs != null)
766     {
767       // intersect with pep
768       for (int d = 0; d < dnarefs.length; d++)
769       {
770         Mapping mp = dnarefs[d].getMap();
771         if (mp != null)
772         {
773         }
774       }
775     }
776     if (sf != null)
777     {
778       for (int f = 0; f < sf.length; f++)
779       {
780         fgstate = (featureGroups == null) ? null : ((Boolean) featureGroups
781                 .get(sf[f].featureGroup));
782         if ((featureTypes == null || featureTypes.containsKey(sf[f]
783                 .getType())) && (fgstate == null || fgstate.booleanValue()))
784         {
785           if (FeatureProperties.isCodingFeature(null, sf[f].getType()))
786           {
787             // if (map.intersectsFrom(sf[f].begin, sf[f].end))
788             {
789
790             }
791           }
792         }
793       }
794     }
795   }
796 }