Clover icon

Coverage Report

  1. Project Clover database Tue Mar 10 2026 14:58:44 GMT
  2. Package jalview.analysis

File Dna.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

152
371
16
1
1,005
674
139
0.37
23.19
16
8.69

Classes

Class Line # Actions
Dna 49 371 139
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ 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.Arrays;
25    import java.util.Comparator;
26    import java.util.Iterator;
27    import java.util.List;
28   
29    import jalview.api.AlignViewportI;
30    import jalview.datamodel.AlignedCodon;
31    import jalview.datamodel.AlignedCodonFrame;
32    import jalview.datamodel.Alignment;
33    import jalview.datamodel.AlignmentAnnotation;
34    import jalview.datamodel.AlignmentI;
35    import jalview.datamodel.Annotation;
36    import jalview.datamodel.DBRefEntry;
37    import jalview.datamodel.FeatureProperties;
38    import jalview.datamodel.GraphLine;
39    import jalview.datamodel.Mapping;
40    import jalview.datamodel.Sequence;
41    import jalview.datamodel.SequenceFeature;
42    import jalview.datamodel.SequenceI;
43    import jalview.schemes.ResidueProperties;
44    import jalview.util.Comparison;
45    import jalview.util.DBRefUtils;
46    import jalview.util.MapList;
47    import jalview.util.ShiftList;
48   
 
49    public class Dna
50    {
51    private static final String STOP_ASTERIX = "*";
52   
53    private static final Comparator<AlignedCodon> comparator = new CodonComparator();
54   
55    /*
56    * 'final' variables describe the inputs to the translation, which should not
57    * be modified.
58    */
59    private final List<SequenceI> selection;
60   
61    private final String[] seqstring;
62   
63    private final Iterator<int[]> contigs;
64   
65    private final char gapChar;
66   
67    private final AlignmentAnnotation[] annotations;
68   
69    private final int dnaWidth;
70   
71    private final AlignmentI dataset;
72   
73    private ShiftList vismapping;
74   
75    private int[] startcontigs;
76   
77    /*
78    * Working variables for the translation.
79    *
80    * The width of the translation-in-progress protein alignment.
81    */
82    private int aaWidth = 0;
83   
84    /*
85    * This array will be built up so that position i holds the codon positions
86    * e.g. [7, 9, 10] that match column i (base 0) in the aligned translation.
87    * Note this implies a contract that if two codons do not align exactly, their
88    * translated products must occupy different column positions.
89    */
90    private AlignedCodon[] alignedCodons;
91   
92    /**
93    * Constructor given a viewport and the visible contigs.
94    *
95    * @param viewport
96    * @param visibleContigs
97    */
 
98  0 toggle public Dna(AlignViewportI viewport, Iterator<int[]> visibleContigs)
99    {
100  0 this.selection = Arrays.asList(viewport.getSequenceSelection());
101  0 this.seqstring = viewport.getViewAsString(true);
102  0 this.contigs = visibleContigs;
103  0 this.gapChar = viewport.getGapCharacter();
104  0 this.annotations = viewport.getAlignment().getAlignmentAnnotation();
105  0 this.dnaWidth = viewport.getAlignment().getWidth();
106  0 this.dataset = viewport.getAlignment().getDataset();
107  0 initContigs();
108    }
109   
110    /**
111    * Initialise contigs used as starting point for translateCodingRegion
112    */
 
113  0 toggle private void initContigs()
114    {
115  0 vismapping = new ShiftList(); // map from viscontigs to seqstring
116    // intervals
117   
118  0 int npos = 0;
119  0 int[] lastregion = null;
120  0 ArrayList<Integer> tempcontigs = new ArrayList<>();
121  0 while (contigs.hasNext())
122    {
123  0 int[] region = contigs.next();
124  0 if (lastregion == null)
125    {
126  0 vismapping.addShift(npos, region[0]);
127    }
128    else
129    {
130    // hidden region
131  0 vismapping.addShift(npos, region[0] - lastregion[1] + 1);
132    }
133  0 lastregion = region;
134  0 tempcontigs.add(region[0]);
135  0 tempcontigs.add(region[1]);
136    }
137   
138  0 startcontigs = new int[tempcontigs.size()];
139  0 int i = 0;
140  0 for (Integer val : tempcontigs)
141    {
142  0 startcontigs[i] = val;
143  0 i++;
144    }
145  0 tempcontigs = null;
146    }
147   
148    /**
149    * Test whether codon positions cdp1 should align before, with, or after cdp2.
150    * Returns zero if all positions match (or either argument is null). Returns
151    * -1 if any position in the first codon precedes the corresponding position
152    * in the second codon. Else returns +1 (some position in the second codon
153    * precedes the corresponding position in the first).
154    *
155    * Note this is not necessarily symmetric, for example:
156    * <ul>
157    * <li>compareCodonPos([2,5,6], [3,4,5]) returns -1</li>
158    * <li>compareCodonPos([3,4,5], [2,5,6]) also returns -1</li>
159    * </ul>
160    *
161    * @param ac1
162    * @param ac2
163    * @return
164    */
 
165  0 toggle public static final int compareCodonPos(AlignedCodon ac1,
166    AlignedCodon ac2)
167    {
168  0 return comparator.compare(ac1, ac2);
169    // return jalview_2_8_2compare(ac1, ac2);
170    }
171   
172    /**
173    * Codon comparison up to Jalview 2.8.2. This rule is sequence order dependent
174    * - see http://issues.jalview.org/browse/JAL-1635
175    *
176    * @param ac1
177    * @param ac2
178    * @return
179    */
 
180  0 toggle private static int jalview_2_8_2compare(AlignedCodon ac1,
181    AlignedCodon ac2)
182    {
183  0 if (ac1 == null || ac2 == null || (ac1.equals(ac2)))
184    {
185  0 return 0;
186    }
187  0 if (ac1.pos1 < ac2.pos1 || ac1.pos2 < ac2.pos2 || ac1.pos3 < ac2.pos3)
188    {
189    // one base in cdp1 precedes the corresponding base in the other codon
190  0 return -1;
191    }
192    // one base in cdp1 appears after the corresponding base in the other codon.
193  0 return 1;
194    }
195   
196    /**
197    * Translates cDNA using the specified code table
198    *
199    * @return
200    */
 
201  0 toggle public AlignmentI translateCdna(GeneticCodeI codeTable)
202    {
203  0 AlignedCodonFrame acf = new AlignedCodonFrame();
204   
205  0 alignedCodons = new AlignedCodon[dnaWidth];
206   
207  0 int s;
208  0 int sSize = selection.size();
209  0 List<SequenceI> pepseqs = new ArrayList<>();
210  0 for (s = 0; s < sSize; s++)
211    {
212  0 SequenceI newseq = translateCodingRegion(selection.get(s),
213    seqstring[s], acf, pepseqs, codeTable);
214   
215  0 if (newseq != null)
216    {
217  0 pepseqs.add(newseq);
218  0 SequenceI ds = newseq;
219  0 if (dataset != null)
220    {
221  0 while (ds.getDatasetSequence() != null)
222    {
223  0 ds = ds.getDatasetSequence();
224    }
225  0 dataset.addSequence(ds);
226    }
227    }
228    }
229   
230  0 SequenceI[] newseqs = pepseqs.toArray(new SequenceI[pepseqs.size()]);
231  0 AlignmentI al = new Alignment(newseqs);
232    // ensure we look aligned.
233  0 al.padGaps();
234    // link the protein translation to the DNA dataset
235  0 al.setDataset(dataset);
236  0 translateAlignedAnnotations(al, acf);
237  0 al.addCodonFrame(acf);
238  0 return al;
239    }
240   
241    /**
242    * fake the collection of DbRefs with associated exon mappings to identify if
243    * a translation would generate distinct product in the currently selected
244    * region.
245    *
246    * @param selection
247    * @param viscontigs
248    * @return
249    */
 
250  0 toggle public static boolean canTranslate(SequenceI[] selection,
251    int viscontigs[])
252    {
253  0 for (int gd = 0; gd < selection.length; gd++)
254    {
255  0 SequenceI dna = selection[gd];
256  0 List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
257    jalview.datamodel.DBRefSource.DNACODINGDBS);
258  0 if (dnarefs != null)
259    {
260    // intersect with pep
261  0 List<DBRefEntry> mappedrefs = new ArrayList<>();
262  0 List<DBRefEntry> refs = dna.getDBRefs();
263  0 for (int d = 0, nd = refs.size(); d < nd; d++)
264    {
265  0 DBRefEntry ref = refs.get(d);
266  0 if (ref.getMap() != null && ref.getMap().getMap() != null
267    && ref.getMap().getMap().getFromRatio() == 3
268    && ref.getMap().getMap().getToRatio() == 1)
269    {
270  0 mappedrefs.add(ref); // add translated protein maps
271    }
272    }
273  0 dnarefs = mappedrefs;// .toArray(new DBRefEntry[mappedrefs.size()]);
274  0 for (int d = 0, nd = dnarefs.size(); d < nd; d++)
275    {
276  0 Mapping mp = dnarefs.get(d).getMap();
277  0 if (mp != null)
278    {
279  0 for (int vc = 0, nv = viscontigs.length; vc < nv; vc += 2)
280    {
281  0 int[] mpr = mp.locateMappedRange(viscontigs[vc],
282    viscontigs[vc + 1]);
283  0 if (mpr != null)
284    {
285  0 return true;
286    }
287    }
288    }
289    }
290    }
291    }
292  0 return false;
293    }
294   
295    /**
296    * Translate nucleotide alignment annotations onto translated amino acid
297    * alignment using codon mapping codons
298    *
299    * @param al
300    * the translated protein alignment
301    */
 
302  0 toggle protected void translateAlignedAnnotations(AlignmentI al,
303    AlignedCodonFrame acf)
304    {
305    // Can only do this for columns with consecutive codons, or where
306    // annotation is sequence associated.
307   
308  0 if (annotations != null)
309    {
310  0 for (AlignmentAnnotation annotation : annotations)
311    {
312    /*
313    * Skip hidden or autogenerated annotation. Also (for now), RNA
314    * secondary structure annotation. If we want to show this against
315    * protein we need a smarter way to 'translate' without generating
316    * invalid (unbalanced) structure annotation.
317    */
318  0 if (annotation.autoCalculated || !annotation.isForDisplay()
319    || annotation.isRNA())
320    {
321  0 continue;
322    }
323   
324  0 int aSize = aaWidth;
325  0 Annotation[] anots = (annotation.annotations == null) ? null
326    : new Annotation[aSize];
327  0 if (anots != null)
328    {
329  0 for (int a = 0; a < aSize; a++)
330    {
331    // process through codon map.
332  0 if (a < alignedCodons.length && alignedCodons[a] != null
333    && alignedCodons[a].pos1 == (alignedCodons[a].pos3 - 2))
334    {
335  0 anots[a] = getCodonAnnotation(alignedCodons[a],
336    annotation.annotations);
337    }
338    }
339    }
340   
341  0 AlignmentAnnotation aa = new AlignmentAnnotation(annotation.label,
342    annotation.description, anots);
343  0 aa.graph = annotation.graph;
344  0 aa.graphGroup = annotation.graphGroup;
345  0 aa.graphHeight = annotation.graphHeight;
346  0 if (annotation.getThreshold() != null)
347    {
348  0 aa.setThreshold(new GraphLine(annotation.getThreshold()));
349    }
350  0 if (annotation.hasScore)
351    {
352  0 aa.setScore(annotation.getScore());
353    }
354   
355  0 final SequenceI seqRef = annotation.sequenceRef;
356  0 if (seqRef != null)
357    {
358  0 SequenceI aaSeq = acf.getAaForDnaSeq(seqRef);
359  0 if (aaSeq != null)
360    {
361    // aa.compactAnnotationArray(); // throw away alignment annotation
362    // positioning
363  0 aa.setSequenceRef(aaSeq);
364    // rebuild mapping
365  0 aa.createSequenceMapping(aaSeq, aaSeq.getStart(), true);
366  0 aa.adjustForAlignment();
367  0 aaSeq.addAlignmentAnnotation(aa);
368    }
369    }
370  0 al.addAnnotation(aa);
371    }
372    }
373    }
374   
 
375  0 toggle private static Annotation getCodonAnnotation(AlignedCodon is,
376    Annotation[] annotations)
377    {
378    // Have a look at all the codon positions for annotation and put the first
379    // one found into the translated annotation pos.
380  0 int contrib = 0;
381  0 Annotation annot = null;
382  0 for (int p = 1; p <= 3; p++)
383    {
384  0 int dnaCol = is.getBaseColumn(p);
385  0 if (annotations[dnaCol] != null)
386    {
387  0 if (annot == null)
388    {
389  0 annot = new Annotation(annotations[dnaCol]);
390  0 contrib = 1;
391    }
392    else
393    {
394    // merge with last
395  0 Annotation cpy = new Annotation(annotations[dnaCol]);
396  0 if (annot.colour == null)
397    {
398  0 annot.colour = cpy.colour;
399    }
400  0 if (annot.description == null || annot.description.length() == 0)
401    {
402  0 annot.description = cpy.description;
403    }
404  0 if (annot.displayCharacter == null)
405    {
406  0 annot.displayCharacter = cpy.displayCharacter;
407    }
408  0 if (annot.secondaryStructure == 0)
409    {
410  0 annot.secondaryStructure = cpy.secondaryStructure;
411    }
412  0 annot.value += cpy.value;
413  0 contrib++;
414    }
415    }
416    }
417  0 if (contrib > 1)
418    {
419  0 annot.value /= contrib;
420    }
421  0 return annot;
422    }
423   
424    /**
425    * Translate a na sequence
426    *
427    * @param selection
428    * sequence displayed under viscontigs visible columns
429    * @param seqstring
430    * ORF read in some global alignment reference frame
431    * @param acf
432    * Definition of global ORF alignment reference frame
433    * @param proteinSeqs
434    * @param codeTable
435    * @return sequence ready to be added to alignment.
436    */
 
437  0 toggle protected SequenceI translateCodingRegion(SequenceI selection,
438    String seqstring, AlignedCodonFrame acf,
439    List<SequenceI> proteinSeqs, GeneticCodeI codeTable)
440    {
441  0 List<int[]> skip = new ArrayList<>();
442  0 int[] skipint = null;
443   
444  0 int npos = 0;
445  0 int vc = 0;
446   
447  0 int[] scontigs = new int[startcontigs.length];
448  0 System.arraycopy(startcontigs, 0, scontigs, 0, startcontigs.length);
449   
450    // allocate a roughly sized buffer for the protein sequence
451  0 StringBuilder protein = new StringBuilder(seqstring.length() / 2);
452  0 String seq = seqstring.replace('U', 'T').replace('u', 'T');
453  0 char codon[] = new char[3];
454  0 int cdp[] = new int[3];
455  0 int rf = 0;
456  0 int lastnpos = 0;
457  0 int nend;
458  0 int aspos = 0;
459  0 int resSize = 0;
460  0 for (npos = 0, nend = seq.length(); npos < nend; npos++)
461    {
462  0 if (!Comparison.isGap(seq.charAt(npos)))
463    {
464  0 cdp[rf] = npos; // store position
465  0 codon[rf++] = seq.charAt(npos); // store base
466    }
467  0 if (rf == 3)
468    {
469    /*
470    * Filled up a reading frame...
471    */
472  0 AlignedCodon alignedCodon = new AlignedCodon(cdp[0], cdp[1],
473    cdp[2]);
474  0 String aa = codeTable.translate(new String(codon));
475  0 rf = 0;
476  0 final String gapString = String.valueOf(gapChar);
477  0 if (aa == null)
478    {
479  0 aa = gapString;
480  0 if (skipint == null)
481    {
482  0 skipint = new int[] { alignedCodon.pos1,
483    alignedCodon.pos3 /*
484    * cdp[0],
485    * cdp[2]
486    */ };
487    }
488  0 skipint[1] = alignedCodon.pos3; // cdp[2];
489    }
490    else
491    {
492  0 if (skipint != null)
493    {
494    // edit scontigs
495  0 skipint[0] = vismapping.shift(skipint[0]);
496  0 skipint[1] = vismapping.shift(skipint[1]);
497  0 for (vc = 0; vc < scontigs.length;)
498    {
499  0 if (scontigs[vc + 1] < skipint[0])
500    {
501    // before skipint starts
502  0 vc += 2;
503  0 continue;
504    }
505  0 if (scontigs[vc] > skipint[1])
506    {
507    // finished editing so
508  0 break;
509    }
510    // Edit the contig list to include the skipped region which did
511    // not translate
512  0 int[] t;
513    // from : s1 e1 s2 e2 s3 e3
514    // to s: s1 e1 s2 k0 k1 e2 s3 e3
515    // list increases by one unless one boundary (s2==k0 or e2==k1)
516    // matches, and decreases by one if skipint intersects whole
517    // visible contig
518  0 if (scontigs[vc] <= skipint[0])
519    {
520  0 if (skipint[0] == scontigs[vc])
521    {
522    // skipint at start of contig
523    // shift the start of this contig
524  0 if (scontigs[vc + 1] > skipint[1])
525    {
526  0 scontigs[vc] = skipint[1];
527  0 vc += 2;
528    }
529    else
530    {
531  0 if (scontigs[vc + 1] == skipint[1])
532    {
533    // remove the contig
534  0 t = new int[scontigs.length - 2];
535  0 if (vc > 0)
536    {
537  0 System.arraycopy(scontigs, 0, t, 0, vc - 1);
538    }
539  0 if (vc + 2 < t.length)
540    {
541  0 System.arraycopy(scontigs, vc + 2, t, vc,
542    t.length - vc + 2);
543    }
544  0 scontigs = t;
545    }
546    else
547    {
548    // truncate contig to before the skipint region
549  0 scontigs[vc + 1] = skipint[0] - 1;
550  0 vc += 2;
551    }
552    }
553    }
554    else
555    {
556    // scontig starts before start of skipint
557  0 if (scontigs[vc + 1] < skipint[1])
558    {
559    // skipint truncates end of scontig
560  0 scontigs[vc + 1] = skipint[0] - 1;
561  0 vc += 2;
562    }
563    else
564    {
565    // divide region to new contigs
566  0 t = new int[scontigs.length + 2];
567  0 System.arraycopy(scontigs, 0, t, 0, vc + 1);
568  0 t[vc + 1] = skipint[0];
569  0 t[vc + 2] = skipint[1];
570  0 System.arraycopy(scontigs, vc + 1, t, vc + 3,
571    scontigs.length - (vc + 1));
572  0 scontigs = t;
573  0 vc += 4;
574    }
575    }
576    }
577    }
578  0 skip.add(skipint);
579  0 skipint = null;
580    }
581  0 if (aa.equals(ResidueProperties.STOP))
582    {
583  0 aa = STOP_ASTERIX;
584    }
585  0 resSize++;
586    }
587  0 boolean findpos = true;
588  0 while (findpos)
589    {
590    /*
591    * Compare this codon's base positions with those currently aligned to
592    * this column in the translation. Always check we've not run out of existing aligned codons !
593    */
594  0 final int compareCodonPos = (aspos < alignedCodons.length)
595    ? compareCodonPos(alignedCodon, alignedCodons[aspos])
596    : -1;
597  0 switch (compareCodonPos)
598    {
599  0 case -1:
600   
601    /*
602    * This codon should precede the mapped positions - need to insert a
603    * gap in all prior sequences.
604    */
605  0 insertAAGap(aspos, proteinSeqs);
606  0 findpos = false;
607  0 break;
608   
609  0 case +1:
610   
611    /*
612    * This codon belongs after the aligned codons at aspos. Prefix it
613    * with a gap and try the next position.
614    */
615  0 aa = gapString + aa;
616  0 aspos++;
617  0 break;
618   
619  0 case 0:
620   
621    /*
622    * Exact match - codon 'belongs' at this translated position.
623    */
624  0 findpos = false;
625    }
626    }
627  0 protein.append(aa);
628  0 lastnpos = npos;
629  0 if (alignedCodons[aspos] == null)
630    {
631    // mark this column as aligning to this aligned reading frame
632  0 alignedCodons[aspos] = alignedCodon;
633    }
634  0 else if (!alignedCodons[aspos].equals(alignedCodon))
635    {
636  0 throw new IllegalStateException(
637    "Tried to coalign " + alignedCodons[aspos].toString()
638    + " with " + alignedCodon.toString());
639    }
640  0 if (aspos >= aaWidth)
641    {
642    // update maximum alignment width
643  0 aaWidth = aspos;
644    }
645    // ready for next translated reading frame alignment position (if any)
646  0 aspos++;
647    }
648    }
649  0 if (resSize > 0)
650    {
651  0 SequenceI newseq = new Sequence(selection.getName(),
652    protein.toString());
653  0 if (rf != 0)
654    {
655  0 final String errMsg = "trimming contigs for incomplete terminal codon.";
656  0 jalview.bin.Console.errPrintln(errMsg);
657    // map and trim contigs to ORF region
658  0 vc = scontigs.length - 1;
659  0 lastnpos = vismapping.shift(lastnpos); // place npos in context of
660    // whole dna alignment (rather
661    // than visible contigs)
662    // incomplete ORF could be broken over one or two visible contig
663    // intervals.
664  0 while (vc >= 0 && scontigs[vc] > lastnpos)
665    {
666  0 if (vc > 0 && scontigs[vc - 1] > lastnpos)
667    {
668  0 vc -= 2;
669    }
670    else
671    {
672    // correct last interval in list.
673  0 scontigs[vc] = lastnpos;
674    }
675    }
676   
677  0 if (vc > 0 && (vc + 1) < scontigs.length)
678    {
679    // truncate map list to just vc elements
680  0 int t[] = new int[vc + 1];
681  0 System.arraycopy(scontigs, 0, t, 0, vc + 1);
682  0 scontigs = t;
683    }
684  0 if (vc <= 0)
685    {
686  0 scontigs = null;
687    }
688    }
689  0 if (scontigs != null)
690    {
691  0 npos = 0;
692    // map scontigs to actual sequence positions on selection
693  0 for (vc = 0; vc < scontigs.length; vc += 2)
694    {
695  0 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
696  0 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
697  0 if (scontigs[vc + 1] == selection.getEnd())
698    {
699  0 break;
700    }
701    }
702    // trim trailing empty intervals.
703  0 if ((vc + 2) < scontigs.length)
704    {
705  0 int t[] = new int[vc + 2];
706  0 System.arraycopy(scontigs, 0, t, 0, vc + 2);
707  0 scontigs = t;
708    }
709    /*
710    * delete intervals in scontigs which are not translated. 1. map skip
711    * into sequence position intervals 2. truncate existing ranges and add
712    * new ranges to exclude untranslated regions. if (skip.size()>0) {
713    * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
714    * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
715    * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
716    * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
717    * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
718    * range } else { // truncate range and create new one if necessary iv =
719    * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
720    * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
721    * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
722    */
723  0 MapList map = new MapList(scontigs, new int[] { 1, resSize }, 3, 1);
724   
725  0 transferCodedFeatures(selection, newseq, map);
726   
727    /*
728    * Construct a dataset sequence for our new peptide.
729    */
730  0 SequenceI rseq = newseq.deriveSequence();
731   
732    /*
733    * Store a mapping (between the dataset sequences for the two
734    * sequences).
735    */
736    // SIDE-EFFECT: acf stores the aligned sequence reseq; to remove!
737  0 acf.addMap(selection, rseq, map);
738  0 return rseq;
739    }
740    }
741    // register the mapping somehow
742    //
743  0 return null;
744    }
745   
746    /**
747    * Insert a gap into the aligned proteins and the codon mapping array.
748    *
749    * @param pos
750    * @param proteinSeqs
751    * @return
752    */
 
753  0 toggle protected void insertAAGap(int pos, List<SequenceI> proteinSeqs)
754    {
755  0 aaWidth++;
756  0 for (SequenceI seq : proteinSeqs)
757    {
758  0 seq.insertCharAt(pos, gapChar);
759    }
760   
761  0 checkCodonFrameWidth();
762  0 if (pos < aaWidth)
763    {
764  0 aaWidth++;
765   
766    /*
767    * Shift from [pos] to the end one to the right, and null out [pos]
768    */
769  0 System.arraycopy(alignedCodons, pos, alignedCodons, pos + 1,
770    alignedCodons.length - pos - 1);
771  0 alignedCodons[pos] = null;
772    }
773    }
774   
775    /**
776    * Check the codons array can accommodate a single insertion, if not resize
777    * it.
778    */
 
779  0 toggle protected void checkCodonFrameWidth()
780    {
781  0 if (alignedCodons[alignedCodons.length - 1] != null)
782    {
783    /*
784    * arraycopy insertion would bump a filled slot off the end, so expand.
785    */
786  0 AlignedCodon[] c = new AlignedCodon[alignedCodons.length + 10];
787  0 System.arraycopy(alignedCodons, 0, c, 0, alignedCodons.length);
788  0 alignedCodons = c;
789    }
790    }
791   
792    /**
793    * Given a peptide newly translated from a dna sequence, copy over and set any
794    * features on the peptide from the DNA.
795    *
796    * @param dna
797    * @param pep
798    * @param map
799    */
 
800  0 toggle private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
801    MapList map)
802    {
803    // BH 2019.01.25 nop?
804    // List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
805    // DBRefSource.DNACODINGDBS);
806    // if (dnarefs != null)
807    // {
808    // // intersect with pep
809    // for (int d = 0, nd = dnarefs.size(); d < nd; d++)
810    // {
811    // Mapping mp = dnarefs.get(d).getMap();
812    // if (mp != null)
813    // {
814    // }
815    // }
816    // }
817  0 for (SequenceFeature sf : dna.getFeatures().getAllFeatures())
818    {
819  0 if (FeatureProperties.isCodingFeature(null, sf.getType()))
820    {
821    // if (map.intersectsFrom(sf[f].begin, sf[f].end))
822    {
823   
824    }
825    }
826    }
827    }
828   
829    /**
830    * Returns an alignment consisting of the reversed (and optionally
831    * complemented) sequences set in this object's constructor
832    *
833    * @param complement
834    * @return
835    */
 
836  0 toggle public AlignmentI reverseCdna(boolean complement)
837    {
838  0 int sSize = selection.size();
839  0 List<SequenceI> reversed = new ArrayList<>();
840  0 for (int s = 0; s < sSize; s++)
841    {
842  0 SequenceI newseq = reverseSequence(selection.get(s).getName(),
843    seqstring[s], complement);
844   
845  0 if (newseq != null)
846    {
847  0 reversed.add(newseq);
848    }
849    }
850   
851  0 SequenceI[] newseqs = reversed.toArray(new SequenceI[reversed.size()]);
852  0 AlignmentI al = new Alignment(newseqs);
853  0 ((Alignment) al).createDatasetAlignment();
854  0 return al;
855    }
856   
857    /**
858    * Returns a reversed, and optionally complemented, sequence. The new
859    * sequence's name is the original name with "|rev" or "|revcomp" appended.
860    * aAcCgGtT and DNA ambiguity codes are complemented, any other characters are
861    * left unchanged.
862    *
863    * @param seq
864    * @param complement
865    * @return
866    */
 
867  0 toggle public static SequenceI reverseSequence(String seqName, String sequence,
868    boolean complement)
869    {
870  0 String newName = seqName + "|rev" + (complement ? "comp" : "");
871  0 char[] originalSequence = sequence.toCharArray();
872  0 int length = originalSequence.length;
873  0 char[] reversedSequence = new char[length];
874  0 int bases = 0;
875  0 for (int i = 0; i < length; i++)
876    {
877  0 char c = complement ? getComplement(originalSequence[i])
878    : originalSequence[i];
879  0 reversedSequence[length - i - 1] = c;
880  0 if (!Comparison.isGap(c))
881    {
882  0 bases++;
883    }
884    }
885  0 SequenceI reversed = new Sequence(newName, reversedSequence, 1, bases);
886  0 return reversed;
887    }
888   
889    /**
890    * Answers the reverse complement of the input string
891    *
892    * @see #getComplement(char)
893    * @param s
894    * @return
895    */
 
896  0 toggle public static String reverseComplement(String s)
897    {
898  0 StringBuilder sb = new StringBuilder(s.length());
899  0 for (int i = s.length() - 1; i >= 0; i--)
900    {
901  0 sb.append(Dna.getComplement(s.charAt(i)));
902    }
903  0 return sb.toString();
904    }
905   
906    /**
907    * Returns dna complement (preserving case) for aAcCgGtTuU. Ambiguity codes
908    * are treated as on http://reverse-complement.com/. Anything else is left
909    * unchanged.
910    *
911    * @param c
912    * @return
913    */
 
914  0 toggle public static char getComplement(char c)
915    {
916  0 char result = c;
917  0 switch (c)
918    {
919  0 case '-':
920  0 case '.':
921  0 case ' ':
922  0 break;
923  0 case 'a':
924  0 result = 't';
925  0 break;
926  0 case 'A':
927  0 result = 'T';
928  0 break;
929  0 case 'c':
930  0 result = 'g';
931  0 break;
932  0 case 'C':
933  0 result = 'G';
934  0 break;
935  0 case 'g':
936  0 result = 'c';
937  0 break;
938  0 case 'G':
939  0 result = 'C';
940  0 break;
941  0 case 't':
942  0 result = 'a';
943  0 break;
944  0 case 'T':
945  0 result = 'A';
946  0 break;
947  0 case 'u':
948  0 result = 'a';
949  0 break;
950  0 case 'U':
951  0 result = 'A';
952  0 break;
953  0 case 'r':
954  0 result = 'y';
955  0 break;
956  0 case 'R':
957  0 result = 'Y';
958  0 break;
959  0 case 'y':
960  0 result = 'r';
961  0 break;
962  0 case 'Y':
963  0 result = 'R';
964  0 break;
965  0 case 'k':
966  0 result = 'm';
967  0 break;
968  0 case 'K':
969  0 result = 'M';
970  0 break;
971  0 case 'm':
972  0 result = 'k';
973  0 break;
974  0 case 'M':
975  0 result = 'K';
976  0 break;
977  0 case 'b':
978  0 result = 'v';
979  0 break;
980  0 case 'B':
981  0 result = 'V';
982  0 break;
983  0 case 'v':
984  0 result = 'b';
985  0 break;
986  0 case 'V':
987  0 result = 'B';
988  0 break;
989  0 case 'd':
990  0 result = 'h';
991  0 break;
992  0 case 'D':
993  0 result = 'H';
994  0 break;
995  0 case 'h':
996  0 result = 'd';
997  0 break;
998  0 case 'H':
999  0 result = 'D';
1000  0 break;
1001    }
1002   
1003  0 return result;
1004    }
1005    }