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