Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.analysis

File Dna.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
27% of files have more coverage

Code metrics

150
371
16
1
1,003
672
138
0.37
23.19
16
8.62

Classes

Class Line # Actions
Dna 50 371 138
0.649906965%
 

Contributing tests

This file is covered by 16 tests. .

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 jalview.api.AlignViewportI;
24    import jalview.datamodel.AlignedCodon;
25    import jalview.datamodel.AlignedCodonFrame;
26    import jalview.datamodel.Alignment;
27    import jalview.datamodel.AlignmentAnnotation;
28    import jalview.datamodel.AlignmentI;
29    import jalview.datamodel.Annotation;
30    import jalview.datamodel.DBRefEntry;
31    import jalview.datamodel.DBRefSource;
32    import jalview.datamodel.FeatureProperties;
33    import jalview.datamodel.GraphLine;
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.Comparison;
40    import jalview.util.DBRefUtils;
41    import jalview.util.MapList;
42    import jalview.util.ShiftList;
43   
44    import java.util.ArrayList;
45    import java.util.Arrays;
46    import java.util.Comparator;
47    import java.util.Iterator;
48    import java.util.List;
49   
 
50    public class Dna
51    {
52    private static final String STOP_ASTERIX = "*";
53   
54    private static final Comparator<AlignedCodon> comparator = new CodonComparator();
55   
56    /*
57    * 'final' variables describe the inputs to the translation, which should not
58    * be modified.
59    */
60    private final List<SequenceI> selection;
61   
62    private final String[] seqstring;
63   
64    private final Iterator<int[]> contigs;
65   
66    private final char gapChar;
67   
68    private final AlignmentAnnotation[] annotations;
69   
70    private final int dnaWidth;
71   
72    private final AlignmentI dataset;
73   
74    private ShiftList vismapping;
75   
76    private int[] startcontigs;
77   
78    /*
79    * Working variables for the translation.
80    *
81    * The width of the translation-in-progress protein alignment.
82    */
83    private int aaWidth = 0;
84   
85    /*
86    * This array will be built up so that position i holds the codon positions
87    * e.g. [7, 9, 10] that match column i (base 0) in the aligned translation.
88    * Note this implies a contract that if two codons do not align exactly, their
89    * translated products must occupy different column positions.
90    */
91    private AlignedCodon[] alignedCodons;
92   
93    /**
94    * Constructor given a viewport and the visible contigs.
95    *
96    * @param viewport
97    * @param visibleContigs
98    */
 
99  24 toggle public Dna(AlignViewportI viewport, Iterator<int[]> visibleContigs)
100    {
101  24 this.selection = Arrays.asList(viewport.getSequenceSelection());
102  24 this.seqstring = viewport.getViewAsString(true);
103  24 this.contigs = visibleContigs;
104  24 this.gapChar = viewport.getGapCharacter();
105  24 this.annotations = viewport.getAlignment().getAlignmentAnnotation();
106  24 this.dnaWidth = viewport.getAlignment().getWidth();
107  24 this.dataset = viewport.getAlignment().getDataset();
108  24 initContigs();
109    }
110   
111    /**
112    * Initialise contigs used as starting point for translateCodingRegion
113    */
 
114  24 toggle private void initContigs()
115    {
116  24 vismapping = new ShiftList(); // map from viscontigs to seqstring
117    // intervals
118   
119  24 int npos = 0;
120  24 int[] lastregion = null;
121  24 ArrayList<Integer> tempcontigs = new ArrayList<>();
122  50 while (contigs.hasNext())
123    {
124  26 int[] region = contigs.next();
125  26 if (lastregion == null)
126    {
127  24 vismapping.addShift(npos, region[0]);
128    }
129    else
130    {
131    // hidden region
132  2 vismapping.addShift(npos, region[0] - lastregion[1] + 1);
133    }
134  26 lastregion = region;
135  26 tempcontigs.add(region[0]);
136  26 tempcontigs.add(region[1]);
137    }
138   
139  24 startcontigs = new int[tempcontigs.size()];
140  24 int i = 0;
141  24 for (Integer val : tempcontigs)
142    {
143  52 startcontigs[i] = val;
144  52 i++;
145    }
146  24 tempcontigs = null;
147    }
148   
149    /**
150    * Test whether codon positions cdp1 should align before, with, or after cdp2.
151    * Returns zero if all positions match (or either argument is null). Returns
152    * -1 if any position in the first codon precedes the corresponding position
153    * in the second codon. Else returns +1 (some position in the second codon
154    * precedes the corresponding position in the first).
155    *
156    * Note this is not necessarily symmetric, for example:
157    * <ul>
158    * <li>compareCodonPos([2,5,6], [3,4,5]) returns -1</li>
159    * <li>compareCodonPos([3,4,5], [2,5,6]) also returns -1</li>
160    * </ul>
161    *
162    * @param ac1
163    * @param ac2
164    * @return
165    */
 
166  3347 toggle public static final int compareCodonPos(AlignedCodon ac1, AlignedCodon ac2)
167    {
168  3347 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  23 toggle public AlignmentI translateCdna(GeneticCodeI codeTable)
202    {
203  23 AlignedCodonFrame acf = new AlignedCodonFrame();
204   
205  23 alignedCodons = new AlignedCodon[dnaWidth];
206   
207  23 int s;
208  23 int sSize = selection.size();
209  23 List<SequenceI> pepseqs = new ArrayList<>();
210  240 for (s = 0; s < sSize; s++)
211    {
212  217 SequenceI newseq = translateCodingRegion(selection.get(s),
213    seqstring[s], acf, pepseqs, codeTable);
214   
215  217 if (newseq != null)
216    {
217  217 pepseqs.add(newseq);
218  217 SequenceI ds = newseq;
219  217 if (dataset != null)
220    {
221  2 while (ds.getDatasetSequence() != null)
222    {
223  1 ds = ds.getDatasetSequence();
224    }
225  1 dataset.addSequence(ds);
226    }
227    }
228    }
229   
230  23 SequenceI[] newseqs = pepseqs.toArray(new SequenceI[pepseqs.size()]);
231  23 AlignmentI al = new Alignment(newseqs);
232    // ensure we look aligned.
233  23 al.padGaps();
234    // link the protein translation to the DNA dataset
235  23 al.setDataset(dataset);
236  23 translateAlignedAnnotations(al, acf);
237  23 al.addCodonFrame(acf);
238  23 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  23 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  23 if (annotations != null)
309    {
310  23 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  318 if (annotation.autoCalculated || !annotation.visible
319    || annotation.isRNA())
320    {
321  318 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  217 toggle protected SequenceI translateCodingRegion(SequenceI selection,
438    String seqstring, AlignedCodonFrame acf,
439    List<SequenceI> proteinSeqs, GeneticCodeI codeTable)
440    {
441  217 List<int[]> skip = new ArrayList<>();
442  217 int[] skipint = null;
443   
444  217 int npos = 0;
445  217 int vc = 0;
446   
447  217 int[] scontigs = new int[startcontigs.length];
448  217 System.arraycopy(startcontigs, 0, scontigs, 0, startcontigs.length);
449   
450    // allocate a roughly sized buffer for the protein sequence
451  217 StringBuilder protein = new StringBuilder(seqstring.length() / 2);
452  217 String seq = seqstring.replace('U', 'T').replace('u', 'T');
453  217 char codon[] = new char[3];
454  217 int cdp[] = new int[3];
455  217 int rf = 0;
456  217 int lastnpos = 0;
457  217 int nend;
458  217 int aspos = 0;
459  217 int resSize = 0;
460  6275 for (npos = 0, nend = seq.length(); npos < nend; npos++)
461    {
462  6058 if (!Comparison.isGap(seq.charAt(npos)))
463    {
464  6028 cdp[rf] = npos; // store position
465  6028 codon[rf++] = seq.charAt(npos); // store base
466    }
467  6058 if (rf == 3)
468    {
469    /*
470    * Filled up a reading frame...
471    */
472  1888 AlignedCodon alignedCodon = new AlignedCodon(cdp[0], cdp[1], cdp[2]);
473  1888 String aa = codeTable.translate(new String(codon));
474  1888 rf = 0;
475  1888 final String gapString = String.valueOf(gapChar);
476  1888 if (aa == null)
477    {
478  1 aa = gapString;
479  1 if (skipint == null)
480    {
481  1 skipint = new int[] { alignedCodon.pos1,
482    alignedCodon.pos3 /*
483    * cdp[0],
484    * cdp[2]
485    */ };
486    }
487  1 skipint[1] = alignedCodon.pos3; // cdp[2];
488    }
489    else
490    {
491  1887 if (skipint != null)
492    {
493    // edit scontigs
494  1 skipint[0] = vismapping.shift(skipint[0]);
495  1 skipint[1] = vismapping.shift(skipint[1]);
496  2 for (vc = 0; vc < scontigs.length;)
497    {
498  1 if (scontigs[vc + 1] < skipint[0])
499    {
500    // before skipint starts
501  0 vc += 2;
502  0 continue;
503    }
504  1 if (scontigs[vc] > skipint[1])
505    {
506    // finished editing so
507  0 break;
508    }
509    // Edit the contig list to include the skipped region which did
510    // not translate
511  1 int[] t;
512    // from : s1 e1 s2 e2 s3 e3
513    // to s: s1 e1 s2 k0 k1 e2 s3 e3
514    // list increases by one unless one boundary (s2==k0 or e2==k1)
515    // matches, and decreases by one if skipint intersects whole
516    // visible contig
517  1 if (scontigs[vc] <= skipint[0])
518    {
519  1 if (skipint[0] == scontigs[vc])
520    {
521    // skipint at start of contig
522    // shift the start of this contig
523  0 if (scontigs[vc + 1] > skipint[1])
524    {
525  0 scontigs[vc] = skipint[1];
526  0 vc += 2;
527    }
528    else
529    {
530  0 if (scontigs[vc + 1] == skipint[1])
531    {
532    // remove the contig
533  0 t = new int[scontigs.length - 2];
534  0 if (vc > 0)
535    {
536  0 System.arraycopy(scontigs, 0, t, 0, vc - 1);
537    }
538  0 if (vc + 2 < t.length)
539    {
540  0 System.arraycopy(scontigs, vc + 2, t, vc,
541    t.length - vc + 2);
542    }
543  0 scontigs = t;
544    }
545    else
546    {
547    // truncate contig to before the skipint region
548  0 scontigs[vc + 1] = skipint[0] - 1;
549  0 vc += 2;
550    }
551    }
552    }
553    else
554    {
555    // scontig starts before start of skipint
556  1 if (scontigs[vc + 1] < skipint[1])
557    {
558    // skipint truncates end of scontig
559  0 scontigs[vc + 1] = skipint[0] - 1;
560  0 vc += 2;
561    }
562    else
563    {
564    // divide region to new contigs
565  1 t = new int[scontigs.length + 2];
566  1 System.arraycopy(scontigs, 0, t, 0, vc + 1);
567  1 t[vc + 1] = skipint[0];
568  1 t[vc + 2] = skipint[1];
569  1 System.arraycopy(scontigs, vc + 1, t, vc + 3,
570    scontigs.length - (vc + 1));
571  1 scontigs = t;
572  1 vc += 4;
573    }
574    }
575    }
576    }
577  1 skip.add(skipint);
578  1 skipint = null;
579    }
580  1887 if (aa.equals(ResidueProperties.STOP))
581    {
582  0 aa = STOP_ASTERIX;
583    }
584  1887 resSize++;
585    }
586  1888 boolean findpos = true;
587  5150 while (findpos)
588    {
589    /*
590    * Compare this codon's base positions with those currently aligned to
591    * this column in the translation.
592    */
593  3262 final int compareCodonPos = compareCodonPos(alignedCodon,
594    alignedCodons[aspos]);
595  3262 switch (compareCodonPos)
596    {
597  178 case -1:
598   
599    /*
600    * This codon should precede the mapped positions - need to insert a
601    * gap in all prior sequences.
602    */
603  178 insertAAGap(aspos, proteinSeqs);
604  178 findpos = false;
605  178 break;
606   
607  1374 case +1:
608   
609    /*
610    * This codon belongs after the aligned codons at aspos. Prefix it
611    * with a gap and try the next position.
612    */
613  1374 aa = gapString + aa;
614  1374 aspos++;
615  1374 break;
616   
617  1710 case 0:
618   
619    /*
620    * Exact match - codon 'belongs' at this translated position.
621    */
622  1710 findpos = false;
623    }
624    }
625  1888 protein.append(aa);
626  1888 lastnpos = npos;
627  1888 if (alignedCodons[aspos] == null)
628    {
629    // mark this column as aligning to this aligned reading frame
630  466 alignedCodons[aspos] = alignedCodon;
631    }
632  1422 else if (!alignedCodons[aspos].equals(alignedCodon))
633    {
634  0 throw new IllegalStateException(
635    "Tried to coalign " + alignedCodons[aspos].toString()
636    + " with " + alignedCodon.toString());
637    }
638  1888 if (aspos >= aaWidth)
639    {
640    // update maximum alignment width
641  449 aaWidth = aspos;
642    }
643    // ready for next translated reading frame alignment position (if any)
644  1888 aspos++;
645    }
646    }
647  217 if (resSize > 0)
648    {
649  217 SequenceI newseq = new Sequence(selection.getName(),
650    protein.toString());
651  217 if (rf != 0)
652    {
653  188 final String errMsg = "trimming contigs for incomplete terminal codon.";
654  188 System.err.println(errMsg);
655    // map and trim contigs to ORF region
656  188 vc = scontigs.length - 1;
657  188 lastnpos = vismapping.shift(lastnpos); // place npos in context of
658    // whole dna alignment (rather
659    // than visible contigs)
660    // incomplete ORF could be broken over one or two visible contig
661    // intervals.
662  376 while (vc >= 0 && scontigs[vc] > lastnpos)
663    {
664  188 if (vc > 0 && scontigs[vc - 1] > lastnpos)
665    {
666  0 vc -= 2;
667    }
668    else
669    {
670    // correct last interval in list.
671  188 scontigs[vc] = lastnpos;
672    }
673    }
674   
675  188 if (vc > 0 && (vc + 1) < scontigs.length)
676    {
677    // truncate map list to just vc elements
678  0 int t[] = new int[vc + 1];
679  0 System.arraycopy(scontigs, 0, t, 0, vc + 1);
680  0 scontigs = t;
681    }
682  188 if (vc <= 0)
683    {
684  0 scontigs = null;
685    }
686    }
687  217 if (scontigs != null)
688    {
689  217 npos = 0;
690    // map scontigs to actual sequence positions on selection
691  412 for (vc = 0; vc < scontigs.length; vc += 2)
692    {
693  220 scontigs[vc] = selection.findPosition(scontigs[vc]); // not from 1!
694  220 scontigs[vc + 1] = selection.findPosition(scontigs[vc + 1]); // exclusive
695  220 if (scontigs[vc + 1] == selection.getEnd())
696    {
697  25 break;
698    }
699    }
700    // trim trailing empty intervals.
701  217 if ((vc + 2) < scontigs.length)
702    {
703  0 int t[] = new int[vc + 2];
704  0 System.arraycopy(scontigs, 0, t, 0, vc + 2);
705  0 scontigs = t;
706    }
707    /*
708    * delete intervals in scontigs which are not translated. 1. map skip
709    * into sequence position intervals 2. truncate existing ranges and add
710    * new ranges to exclude untranslated regions. if (skip.size()>0) {
711    * Vector narange = new Vector(); for (vc=0; vc<scontigs.length; vc++) {
712    * narange.addElement(new int[] {scontigs[vc]}); } int sint=0,iv[]; vc =
713    * 0; while (sint<skip.size()) { skipint = (int[]) skip.elementAt(sint);
714    * do { iv = (int[]) narange.elementAt(vc); if (iv[0]>=skipint[0] &&
715    * iv[0]<=skipint[1]) { if (iv[0]==skipint[0]) { // delete beginning of
716    * range } else { // truncate range and create new one if necessary iv =
717    * (int[]) narange.elementAt(vc+1); if (iv[0]<=skipint[1]) { // truncate
718    * range iv[0] = skipint[1]; } else { } } } else if (iv[0]<skipint[0]) {
719    * iv = (int[]) narange.elementAt(vc+1); } } while (iv[0]) } }
720    */
721  217 MapList map = new MapList(scontigs, new int[] { 1, resSize }, 3, 1);
722   
723  217 transferCodedFeatures(selection, newseq, map);
724   
725    /*
726    * Construct a dataset sequence for our new peptide.
727    */
728  217 SequenceI rseq = newseq.deriveSequence();
729   
730    /*
731    * Store a mapping (between the dataset sequences for the two
732    * sequences).
733    */
734    // SIDE-EFFECT: acf stores the aligned sequence reseq; to remove!
735  217 acf.addMap(selection, rseq, map);
736  217 return rseq;
737    }
738    }
739    // register the mapping somehow
740    //
741  0 return null;
742    }
743   
744    /**
745    * Insert a gap into the aligned proteins and the codon mapping array.
746    *
747    * @param pos
748    * @param proteinSeqs
749    * @return
750    */
 
751  178 toggle protected void insertAAGap(int pos, List<SequenceI> proteinSeqs)
752    {
753  178 aaWidth++;
754  178 for (SequenceI seq : proteinSeqs)
755    {
756  598 seq.insertCharAt(pos, gapChar);
757    }
758   
759  178 checkCodonFrameWidth();
760  178 if (pos < aaWidth)
761    {
762  178 aaWidth++;
763   
764    /*
765    * Shift from [pos] to the end one to the right, and null out [pos]
766    */
767  178 System.arraycopy(alignedCodons, pos, alignedCodons, pos + 1,
768    alignedCodons.length - pos - 1);
769  178 alignedCodons[pos] = null;
770    }
771    }
772   
773    /**
774    * Check the codons array can accommodate a single insertion, if not resize
775    * it.
776    */
 
777  178 toggle protected void checkCodonFrameWidth()
778    {
779  178 if (alignedCodons[alignedCodons.length - 1] != null)
780    {
781    /*
782    * arraycopy insertion would bump a filled slot off the end, so expand.
783    */
784  0 AlignedCodon[] c = new AlignedCodon[alignedCodons.length + 10];
785  0 System.arraycopy(alignedCodons, 0, c, 0, alignedCodons.length);
786  0 alignedCodons = c;
787    }
788    }
789   
790    /**
791    * Given a peptide newly translated from a dna sequence, copy over and set any
792    * features on the peptide from the DNA.
793    *
794    * @param dna
795    * @param pep
796    * @param map
797    */
 
798  217 toggle private static void transferCodedFeatures(SequenceI dna, SequenceI pep,
799    MapList map)
800    {
801    // BH 2019.01.25 nop?
802    // List<DBRefEntry> dnarefs = DBRefUtils.selectRefs(dna.getDBRefs(),
803    // DBRefSource.DNACODINGDBS);
804    // if (dnarefs != null)
805    // {
806    // // intersect with pep
807    // for (int d = 0, nd = dnarefs.size(); d < nd; d++)
808    // {
809    // Mapping mp = dnarefs.get(d).getMap();
810    // if (mp != null)
811    // {
812    // }
813    // }
814    // }
815  217 for (SequenceFeature sf : dna.getFeatures().getAllFeatures())
816    {
817  0 if (FeatureProperties.isCodingFeature(null, sf.getType()))
818    {
819    // if (map.intersectsFrom(sf[f].begin, sf[f].end))
820    {
821   
822    }
823    }
824    }
825    }
826   
827    /**
828    * Returns an alignment consisting of the reversed (and optionally
829    * complemented) sequences set in this object's constructor
830    *
831    * @param complement
832    * @return
833    */
 
834  1 toggle public AlignmentI reverseCdna(boolean complement)
835    {
836  1 int sSize = selection.size();
837  1 List<SequenceI> reversed = new ArrayList<>();
838  2 for (int s = 0; s < sSize; s++)
839    {
840  1 SequenceI newseq = reverseSequence(selection.get(s).getName(),
841    seqstring[s], complement);
842   
843  1 if (newseq != null)
844    {
845  1 reversed.add(newseq);
846    }
847    }
848   
849  1 SequenceI[] newseqs = reversed.toArray(new SequenceI[reversed.size()]);
850  1 AlignmentI al = new Alignment(newseqs);
851  1 ((Alignment) al).createDatasetAlignment();
852  1 return al;
853    }
854   
855    /**
856    * Returns a reversed, and optionally complemented, sequence. The new
857    * sequence's name is the original name with "|rev" or "|revcomp" appended.
858    * aAcCgGtT and DNA ambiguity codes are complemented, any other characters are
859    * left unchanged.
860    *
861    * @param seq
862    * @param complement
863    * @return
864    */
 
865  3 toggle public static SequenceI reverseSequence(String seqName, String sequence,
866    boolean complement)
867    {
868  3 String newName = seqName + "|rev" + (complement ? "comp" : "");
869  3 char[] originalSequence = sequence.toCharArray();
870  3 int length = originalSequence.length;
871  3 char[] reversedSequence = new char[length];
872  3 int bases = 0;
873  63 for (int i = 0; i < length; i++)
874    {
875  60 char c = complement ? getComplement(originalSequence[i])
876    : originalSequence[i];
877  60 reversedSequence[length - i - 1] = c;
878  60 if (!Comparison.isGap(c))
879    {
880  45 bases++;
881    }
882    }
883  3 SequenceI reversed = new Sequence(newName, reversedSequence, 1, bases);
884  3 return reversed;
885    }
886   
887    /**
888    * Answers the reverse complement of the input string
889    *
890    * @see #getComplement(char)
891    * @param s
892    * @return
893    */
 
894  36 toggle public static String reverseComplement(String s)
895    {
896  36 StringBuilder sb = new StringBuilder(s.length());
897  76 for (int i = s.length() - 1; i >= 0; i--)
898    {
899  40 sb.append(Dna.getComplement(s.charAt(i)));
900    }
901  36 return sb.toString();
902    }
903   
904    /**
905    * Returns dna complement (preserving case) for aAcCgGtTuU. Ambiguity codes
906    * are treated as on http://reverse-complement.com/. Anything else is left
907    * unchanged.
908    *
909    * @param c
910    * @return
911    */
 
912  106 toggle public static char getComplement(char c)
913    {
914  106 char result = c;
915  106 switch (c)
916    {
917  7 case '-':
918  0 case '.':
919  0 case ' ':
920  7 break;
921  2 case 'a':
922  2 result = 't';
923  2 break;
924  13 case 'A':
925  13 result = 'T';
926  13 break;
927  3 case 'c':
928  3 result = 'g';
929  3 break;
930  22 case 'C':
931  22 result = 'G';
932  22 break;
933  3 case 'g':
934  3 result = 'c';
935  3 break;
936  17 case 'G':
937  17 result = 'C';
938  17 break;
939  3 case 't':
940  3 result = 'a';
941  3 break;
942  6 case 'T':
943  6 result = 'A';
944  6 break;
945  1 case 'u':
946  1 result = 'a';
947  1 break;
948  2 case 'U':
949  2 result = 'A';
950  2 break;
951  2 case 'r':
952  2 result = 'y';
953  2 break;
954  1 case 'R':
955  1 result = 'Y';
956  1 break;
957  1 case 'y':
958  1 result = 'r';
959  1 break;
960  2 case 'Y':
961  2 result = 'R';
962  2 break;
963  2 case 'k':
964  2 result = 'm';
965  2 break;
966  1 case 'K':
967  1 result = 'M';
968  1 break;
969  1 case 'm':
970  1 result = 'k';
971  1 break;
972  2 case 'M':
973  2 result = 'K';
974  2 break;
975  2 case 'b':
976  2 result = 'v';
977  2 break;
978  1 case 'B':
979  1 result = 'V';
980  1 break;
981  1 case 'v':
982  1 result = 'b';
983  1 break;
984  2 case 'V':
985  2 result = 'B';
986  2 break;
987  2 case 'd':
988  2 result = 'h';
989  2 break;
990  1 case 'D':
991  1 result = 'H';
992  1 break;
993  1 case 'h':
994  1 result = 'd';
995  1 break;
996  2 case 'H':
997  2 result = 'D';
998  2 break;
999    }
1000   
1001  106 return result;
1002    }
1003    }