From: gmungoc Date: Sun, 18 Jan 2015 11:58:13 +0000 (+0000) Subject: JAL-1619 refactoring wip X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=c676a0add80bc023d07f18901fbca3b48682a3d0;p=jalview.git JAL-1619 refactoring wip --- diff --git a/src/jalview/analysis/AlignmentUtils.java b/src/jalview/analysis/AlignmentUtils.java index 681589c..8591d6a 100644 --- a/src/jalview/analysis/AlignmentUtils.java +++ b/src/jalview/analysis/AlignmentUtils.java @@ -216,8 +216,7 @@ public class AlignmentUtils for (SequenceI aaSeq : thisSeqs) { - AlignedCodonFrame acf = new AlignedCodonFrame( - proteinAlignment.getWidth()); + AlignedCodonFrame acf = new AlignedCodonFrame(); List candidates = cdnaSeqs.get(aaSeq.getName()); if (candidates == null) { diff --git a/src/jalview/analysis/CrossRef.java b/src/jalview/analysis/CrossRef.java index fa0fe2f..435a477 100644 --- a/src/jalview/analysis/CrossRef.java +++ b/src/jalview/analysis/CrossRef.java @@ -20,21 +20,21 @@ */ package jalview.analysis; -import java.util.Enumeration; -import java.util.List; -import java.util.Vector; -import java.util.Hashtable; - import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; -import jalview.datamodel.DBRefSource; import jalview.datamodel.DBRefEntry; +import jalview.datamodel.DBRefSource; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceI; import jalview.ws.SequenceFetcher; import jalview.ws.seqfetcher.ASequenceFetcher; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.List; +import java.util.Vector; + /** * Functions for cross-referencing sequence databases. user must first specify * if cross-referencing from protein or dna (set dna==true) @@ -230,7 +230,7 @@ public class CrossRef { Vector rseqs = new Vector(); Alignment ral = null; - AlignedCodonFrame cf = new AlignedCodonFrame(0); // nominal width + AlignedCodonFrame cf = new AlignedCodonFrame(); // nominal width for (int s = 0; s < seqs.length; s++) { SequenceI dss = seqs[s]; @@ -258,7 +258,9 @@ public class CrossRef for (int r = 0; xrfs != null && r < xrfs.length; r++) { if (source != null && !source.equals(xrfs[r].getSource())) + { continue; + } if (xrfs[r].hasMap()) { if (xrfs[r].getMap().getTo() != null) @@ -291,7 +293,9 @@ public class CrossRef { found |= searchDataset(dss, xrfs[r], dataset, rseqs, cf); // ,false,!dna); if (found) + { xrfs[r] = null; // we've recovered seqs for this one. + } } } } @@ -328,7 +332,9 @@ public class CrossRef for (int r = 0; r < xrfs.length; r++) { if (xrfs[r] != null) + { t[l++] = xrfs[r]; + } } xrfs = t; try @@ -432,7 +438,9 @@ public class CrossRef { boolean found = false; if (lrfs == null) + { return false; + } for (int i = 0; i < lrfs.length; i++) { DBRefEntry xref = new DBRefEntry(lrfs[i]); @@ -484,7 +492,9 @@ public class CrossRef boolean found = false; SequenceI[] typer = new SequenceI[1]; if (dataset == null) + { return false; + } if (dataset.getSequences() == null) { System.err.println("Empty dataset sequence set - NO VECTOR"); @@ -494,6 +504,7 @@ public class CrossRef synchronized (ds = dataset.getSequences()) { for (SequenceI nxt : ds) + { if (nxt != null) { if (nxt.getDatasetSequence() != null) @@ -566,6 +577,7 @@ public class CrossRef } } + } } return found; } diff --git a/src/jalview/analysis/Dna.java b/src/jalview/analysis/Dna.java index 0e7d736..ebaee8b 100644 --- a/src/jalview/analysis/Dna.java +++ b/src/jalview/analysis/Dna.java @@ -20,6 +20,7 @@ */ package jalview.analysis; +import jalview.api.AlignViewportI; import jalview.bin.Cache; import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Alignment; @@ -47,6 +48,41 @@ import java.util.List; public class Dna { + private static final String STOP_X = "X"; + + final private List selection; + + final private String[] seqstring; + + final private int[] contigs; + + final private char gapChar; + + final private AlignmentAnnotation[] annotations; + + final private int dnaWidth; + + final private Alignment dataset; + + private int aaWidth = 0; + + /** + * Constructor given a viewport and the visible contigs. + * + * @param viewport + * @param visibleContigs + */ + public Dna(AlignViewportI viewport, int[] visibleContigs) + { + this.selection = Arrays.asList(viewport.getSequenceSelection()); + this.seqstring = viewport.getViewAsString(true); + this.contigs = visibleContigs; + this.gapChar = viewport.getGapCharacter(); + this.annotations = viewport.getAlignment().getAlignmentAnnotation(); + this.dnaWidth = viewport.getAlignment().getWidth(); + this.dataset = viewport.getAlignment().getDataset(); + } + /** * Test whether codon positions cdp1 should align before, with, or after cdp2. * Returns zero if all positions match (or either argument is null). Returns @@ -82,62 +118,12 @@ public class Dna } /** - * DNA->mapped protein sequence alignment translation given set of sequences - * 1. id distinct coding regions within selected region for each sequence 2. - * generate peptides based on inframe (or given) translation or (optionally - * and where specified) out of frame translations (annotated appropriately) 3. - * align peptides based on codon alignment - */ - /** - * id potential products from dna 1. search for distinct products within - * selected region for each selected sequence 2. group by associated DB type. - * 3. return as form for input into above function - */ - /** - * - */ - /** - * create a new alignment of protein sequences by an inframe translation of - * the provided NA sequences - * - * @param selection - * @param seqstring - * @param viscontigs - * @param gapCharacter - * @param annotations - * @param aWidth - * @param dataset - * destination dataset for translated sequences and mappings - * @return - */ - public static AlignmentI cdnaTranslate(SequenceI[] selection, - String[] seqstring, int viscontigs[], char gapCharacter, - AlignmentAnnotation[] annotations, int aWidth, Alignment dataset) - { - return cdnaTranslate(Arrays.asList(selection), seqstring, null, - viscontigs, gapCharacter, annotations, aWidth, dataset); - } - - /** * - * @param cdnaseqs - * @param seqstring - * @param product - * - array of DbRefEntry objects from which exon map in seqstring is - * derived - * @param viscontigs - * @param gapCharacter - * @param annotations - * @param aWidth - * @param dataset * @return */ - public static AlignmentI cdnaTranslate(List cdnaseqs, - String[] seqstring, DBRefEntry[] product, int viscontigs[], - char gapCharacter, AlignmentAnnotation[] annotations, int aWidth, - Alignment dataset) + public AlignmentI translateCdna() { - AlignedCodonFrame acf = new AlignedCodonFrame(aWidth); + AlignedCodonFrame acf = new AlignedCodonFrame(); /* * This array will be built up so that position i holds the codon positions @@ -145,17 +131,15 @@ public class Dna * Note this implies a contract that if two codons do not align exactly, * their translated products must occupy different column positions. */ - int[][] alignedCodons = new int[aWidth][]; + int[][] alignedCodons = new int[dnaWidth][]; int s; - int sSize = cdnaseqs.size(); + int sSize = selection.size(); List pepseqs = new ArrayList(); for (s = 0; s < sSize; s++) { - SequenceI newseq = translateCodingRegion(cdnaseqs.get(s), - seqstring[s], viscontigs, acf, alignedCodons, pepseqs, - gapCharacter, - false); + SequenceI newseq = translateCodingRegion(selection.get(s), + seqstring[s], acf, alignedCodons, pepseqs); if (newseq != null) { @@ -386,31 +370,30 @@ public class Dna * when true stop codons will translate as '*', otherwise as 'X' * @return sequence ready to be added to alignment. */ - protected static SequenceI translateCodingRegion(SequenceI selection, - String seqstring, int[] viscontigs, AlignedCodonFrame acf, - int[][] alignedCodons, List proteinSeqs, - char gapCharacter, - final boolean starForStop) + protected SequenceI translateCodingRegion(SequenceI selection, + String seqstring, AlignedCodonFrame acf, + int[][] alignedCodons, List proteinSeqs) { List skip = new ArrayList(); int skipint[] = null; ShiftList vismapping = new ShiftList(); // map from viscontigs to seqstring // intervals - int vc, scontigs[] = new int[viscontigs.length]; + int vc; + int[] scontigs = new int[contigs.length]; int npos = 0; - for (vc = 0; vc < viscontigs.length; vc += 2) + for (vc = 0; vc < contigs.length; vc += 2) { if (vc == 0) { - vismapping.addShift(npos, viscontigs[vc]); + vismapping.addShift(npos, contigs[vc]); } else { // hidden region - vismapping.addShift(npos, viscontigs[vc] - viscontigs[vc - 1] + 1); + vismapping.addShift(npos, contigs[vc] - contigs[vc - 1] + 1); } - scontigs[vc] = viscontigs[vc]; - scontigs[vc + 1] = viscontigs[vc + 1]; + scontigs[vc] = contigs[vc]; + scontigs[vc + 1] = contigs[vc + 1]; } // allocate a roughly sized buffer for the protein sequence @@ -434,7 +417,7 @@ public class Dna */ String aa = ResidueProperties.codonTranslate(new String(codon)); rf = 0; - final String gapString = String.valueOf(gapCharacter); + final String gapString = String.valueOf(gapChar); if (aa == null) { aa = gapString; @@ -538,7 +521,7 @@ public class Dna } if (aa.equals("STOP")) { - aa = starForStop ? "*" : "X"; + aa = STOP_X; } resSize++; } @@ -558,6 +541,10 @@ public class Dna // debug System.out.println(seq + "/" + aa + " codons: " + Arrays.deepToString(alignedCodons)); + for (SequenceI s : proteinSeqs) + { + System.out.println(s.getSequenceAsString()); + } System.out .println(("Compare " + Arrays.toString(cdp) + " at pos " + aspos + " with " @@ -571,7 +558,7 @@ public class Dna * This codon should precede the mapped positions - need to insert a * gap in all prior sequences. */ - insertAAGap(aspos, gapCharacter, alignedCodons, proteinSeqs); + alignedCodons = insertAAGap(aspos, alignedCodons, proteinSeqs); findpos = false; break; @@ -606,13 +593,13 @@ public class Dna throw new IllegalStateException("Tried to coalign " + Arrays.asList(alignedCodons[aspos], cdp)); } - if (aspos >= acf.aaWidth) + System.out.println(aspos + "/" + aaWidth); + if (aspos >= aaWidth) { // update maximum alignment width // (we can do this without calling checkCodonFrameWidth because it was // already done above) - System.out.println("aspos " + aspos + " >= " + acf.aaWidth); - acf.setAaWidth(aspos); + aaWidth = aspos; } // ready for next translated reading frame alignment position (if any) aspos++; @@ -727,27 +714,29 @@ public class Dna * Insert a gap into the aligned proteins and the codon mapping array. * * @param pos - * @param gapCharacter * @param alignedCodons * @param proteinSeqs + * @return */ - protected static void insertAAGap(int pos, char gapCharacter, - int[][] alignedCodons, List proteinSeqs) + protected int[][] insertAAGap(int pos, int[][] alignedCodons, + List proteinSeqs) { System.out.println("insertAAGap " + pos + "/" + proteinSeqs.size()); - // aaWidth++; + aaWidth++; for (SequenceI seq : proteinSeqs) { - seq.insertCharAt(pos, gapCharacter); + seq.insertCharAt(pos, gapChar); } - // if (pos < aaWidth) - // { - // aaWidth++; - System.arraycopy(alignedCodons, pos, alignedCodons, pos + 1, - alignedCodons.length - pos - 1); - alignedCodons[pos] = null; // clear so new codon position can be marked. - // } + int[][] resized = checkCodonFrameWidth(alignedCodons, pos); + if (pos < aaWidth) + { + aaWidth++; + System.arraycopy(resized, pos, resized, pos + 1, resized.length - pos + - 1); + resized[pos] = null; // clear so new codon position can be marked. + } + return resized; } /** @@ -763,6 +752,7 @@ public class Dna { // TODO why not codons.length < aspos ? // should codons expand if length is 2 or 3 and aslen==2 ? + System.out.println("Checking " + alignedCodons.length + "/" + aspos); if (alignedCodons.length <= aspos + 1) { // probably never have to do this ? diff --git a/src/jalview/datamodel/AlignedCodonFrame.java b/src/jalview/datamodel/AlignedCodonFrame.java index 6048808..bfac717 100644 --- a/src/jalview/datamodel/AlignedCodonFrame.java +++ b/src/jalview/datamodel/AlignedCodonFrame.java @@ -73,18 +73,8 @@ public class AlignedCodonFrame * * @param aWidth */ - public AlignedCodonFrame(int aWidth) + public AlignedCodonFrame() { - if (aWidth <= 0) - { - codons = null; - return; - } - codons = new int[aWidth][]; - for (int res = 0; res < aWidth; res++) - { - codons[res] = null; - } } /** diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index cc7839e..5fe97a4 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -25,6 +25,7 @@ import jalview.analysis.AlignmentSorter; import jalview.analysis.AlignmentUtils; import jalview.analysis.Conservation; import jalview.analysis.CrossRef; +import jalview.analysis.Dna; import jalview.analysis.NJTree; import jalview.analysis.ParseProperties; import jalview.analysis.SequenceIdMatcher; @@ -4871,50 +4872,38 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, /** * Construct and display a new frame containing the translation of this - * frame's cDNA sequences to their protein (amino acid) equivalents. + * frame's cDNA sequences to their aligned protein (amino acid) equivalents. */ @Override public void showTranslation_actionPerformed(ActionEvent e) { - // ///////////////////////////// - // Collect Data to be translated/transferred - - SequenceI[] selection = viewport.getSequenceSelection(); - String[] seqstring = viewport.getViewAsString(true); AlignmentI al = null; try { - al = jalview.analysis.Dna.cdnaTranslate(selection, seqstring, - viewport.getViewAsVisibleContigs(true), viewport - .getGapCharacter(), viewport.getAlignment() - .getAlignmentAnnotation(), viewport.getAlignment() - .getWidth(), viewport.getAlignment().getDataset()); + Dna dna = new Dna(viewport, viewport.getViewAsVisibleContigs(true)); + + al = dna.translateCdna(); } catch (Exception ex) { - al = null; jalview.bin.Cache.log.error( "Exception during translation. Please report this !", ex); - JOptionPane - .showMessageDialog( - Desktop.desktop, - MessageManager - .getString("label.error_when_translating_sequences_submit_bug_report"), - MessageManager - .getString("label.implementation_error") - + MessageManager - .getString("translation_failed"), - JOptionPane.ERROR_MESSAGE); + final String msg = MessageManager + .getString("label.error_when_translating_sequences_submit_bug_report"); + final String title = MessageManager + .getString("label.implementation_error") + + MessageManager.getString("translation_failed"); + JOptionPane.showMessageDialog(Desktop.desktop, msg, title, + JOptionPane.ERROR_MESSAGE); return; } if (al == null) { - JOptionPane - .showMessageDialog( - Desktop.desktop, - MessageManager - .getString("label.select_at_least_three_bases_in_at_least_one_sequence_to_cDNA_translation"), - MessageManager.getString("label.translation_failed"), - JOptionPane.WARNING_MESSAGE); + final String msg = MessageManager + .getString("label.select_at_least_three_bases_in_at_least_one_sequence_to_cDNA_translation"); + final String title = MessageManager + .getString("label.translation_failed"); + JOptionPane.showMessageDialog(Desktop.desktop, msg, title, + JOptionPane.WARNING_MESSAGE); } else { diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 452d4f9..be89829 100644 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -2384,7 +2384,7 @@ public class Jalview2XML AlcodonFrame[] alc = vamsasSet.getAlcodonFrame(); for (int i = 0; i < alc.length; i++) { - AlignedCodonFrame cf = new AlignedCodonFrame(0); + AlignedCodonFrame cf = new AlignedCodonFrame(); if (alc[i].getAlcodMapCount() > 0) { AlcodMap[] maps = alc[i].getAlcodMap(); diff --git a/src/jalview/io/vamsas/Sequencemapping.java b/src/jalview/io/vamsas/Sequencemapping.java index 3878a0a..6fa49a0 100644 --- a/src/jalview/io/vamsas/Sequencemapping.java +++ b/src/jalview/io/vamsas/Sequencemapping.java @@ -20,14 +20,14 @@ */ package jalview.io.vamsas; -import java.util.Vector; - import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Mapping; import jalview.datamodel.SequenceI; import jalview.gui.Desktop; import jalview.io.VamsasAppDatastore; -import uk.ac.vamsas.client.Vobject; + +import java.util.Vector; + import uk.ac.vamsas.objects.core.AlignmentSequence; import uk.ac.vamsas.objects.core.DataSet; import uk.ac.vamsas.objects.core.Sequence; @@ -283,12 +283,12 @@ public class Sequencemapping extends Rangetype jalview.bin.Cache.log.info("Ignoring non sequence-sequence mapping"); return; } - mobj = this.getvObj2jv((Vobject) sdloc); + mobj = this.getvObj2jv(sdloc); if (mobj instanceof SequenceI) { from = (SequenceI) mobj; } - mobj = this.getvObj2jv((Vobject) sdmap); + mobj = this.getvObj2jv(sdmap); if (mobj instanceof SequenceI) { to = (SequenceI) mobj; @@ -329,7 +329,7 @@ public class Sequencemapping extends Rangetype .getV_parent()); jalview.datamodel.AlignmentI dsMap = (jalview.datamodel.AlignmentI) getvObj2jv(sdmap .getV_parent()); - AlignedCodonFrame afc = new AlignedCodonFrame(0); + AlignedCodonFrame afc = new AlignedCodonFrame(); if (dsLoc != null && dsLoc != dsMap) { diff --git a/test/jalview/analysis/DnaTest.java b/test/jalview/analysis/DnaTest.java index 4a8d4d0..49a544d 100644 --- a/test/jalview/analysis/DnaTest.java +++ b/test/jalview/analysis/DnaTest.java @@ -1,8 +1,12 @@ package jalview.analysis; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import jalview.api.AlignViewportI; import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; +import jalview.gui.AlignViewport; import jalview.io.FormatAdapter; import java.io.IOException; @@ -12,6 +16,7 @@ import org.junit.Test; public class DnaTest { + // @formatter:off // AA encoding codons as ordered on the Jalview help page Amino Acid Table private static String fasta = ">B\n" + "GCT" + "GCC" + "GCA" + "GCG" + "TGT" + "TGC" + "GAT" + "GAC" + "GAA" + "GAG" + "TTT" + "TTC" @@ -23,23 +28,137 @@ public class DnaTest + "ACA" + "ACG" + "GTT" + "GTC" + "GTA" + "GTG" + "TGG" + "TAT" + "TAC" + "TAA" + "TAG" + "TGA"; + private static String JAL_1312_example_align_fasta = ">B.FR.83.HXB2_LAI_IIIB_BRU_K03455/45-306\n" + + "ATGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAATTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATACTGGGACA\n" + + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAGATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAC\n" + + ">gi|27804621|gb|AY178912.1|/1-259\n" + + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n" + + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGGACA\n" + + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTTCATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAT\n" + + ">gi|27804623|gb|AY178913.1|/1-259\n" + + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n" + + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGAACA\n" + + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTTCATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n" + + ">gi|27804627|gb|AY178915.1|/1-260\n" + + "-TGGGAAAA-ATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGAACGATTCGCAGTTAACCCTGGCCTGTTAGAAACATCAGAAGGTTGTAGACAAATATTGGGACA\n" + + "GCTACAACCATCCCTTGAGACAGGATCAGAAGAACTTAAATCATTATWTAATACCATAGCAGTCCTCTATTG\n" + + "TGTACATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAG\n" + + ">gi|27804631|gb|AY178917.1|/1-261\n" + + "-TGGGAAAAAATTCGGTTGAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACACCAGAAGGCTGTAGACAAATACTGGGACA\n" + + "GCTACAACCGTCCCTTCAGACAGGATCGGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTGCATCAAAGGATAGATGTAAAAGACACCAAGGAGGCTTTAGAC\n" + + ">gi|27804635|gb|AY178919.1|/1-261\n" + + "-TGGGAGAGAATTCGGTTACGGCCAGGAGGAAAGAAAAAATATAAATTGAAACATATAGTATGGGCAGGCAG\n" + + "AGAGCTAGATCGATTCGCAGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAGATATTGGGACA\n" + + "GCTACAACCGTCCCTTAAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTACATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n" + + ">gi|27804641|gb|AY178922.1|/1-261\n" + + "-TGGGAGAAAATTCGGTTACGGCCAGGGGGAAAGAAAAGATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAAATACTGGGACA\n" + + "GTTACACCCATCCCTTCATACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTGCATCAAAGGATAGAAGTAAAAGACACCAAGGAAGCTTTAGAC\n" + + ">gi|27804647|gb|AY178925.1|/1-261\n" + + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATCAATTAAAACATGTAGTATGGGCAAGCAG\n" + + "GGAACTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n" + + "GCTACAACCATCCCTTCAGACAGGATCAGAGGAACTTAAATCATTATTTAATACAGTAGCAGTCCTCTATTG\n" + + "TGTACATCAAAGAATAGATGTAAAAGACACCAAGGAAGCTCTAGAA\n" + + ">gi|27804649|gb|AY178926.1|/1-261\n" + + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGAACGATTCGCGGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAACTACTGGGACA\n" + + "GTTACAACCATCCCTTCAGACAGGATCAGAAGAACTCAAATCATTATATAATACAATAGCAACCCTCTATTG\n" + + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCCTTAGAT\n" + + ">gi|27804653|gb|AY178928.1|/1-261\n" + + "-TGGGAAAGAATTCGGTTAAGGCCAGGGGGAAAGAAACAATATAAATTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGACCGATTCGCACTTAACCCCGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n" + + "GCTACAATCGTCCCTTCAGACAGGATCAGAAGAACTTAGATCACTATATAATACAGTAGCAGTCCTCTATTG\n" + + "TGTGCATCAAAAGATAGATGTAAAAGACACCAAGGAAGCCTTAGAC\n" + + ">gi|27804659|gb|AY178931.1|/1-261\n" + + "-TGGGAAAAAATTCGGTTACGGCCAGGAGGAAAGAAAAGATATAAATTAAAACATATAGTATGGGCAAGCAG\n" + + "GGAGCTAGAACGATTYGCAGTTAATCCTGGCCTTTTAGAAACAGCAGAAGGCTGTAGACAAATACTGGGACA\n" + + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" + + "TGTACATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAA\n"; + // @formatter:on + + /** + * Corner case for this test is the presence of codons after codons that were + * not translated. + * + * @throws IOException + */ + @Test + public void testTranslateCdna_withUntranslatableCodons() + throws IOException + { + AlignmentI alf = new FormatAdapter().readFile( + JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE, + "FASTA"); + ColumnSelection cs = new ColumnSelection(); + AlignViewportI av = new AlignViewport(alf, cs); + Dna dna = new Dna(av, new int[] + { 0, alf.getWidth() - 1 }); + AlignmentI translated = dna.translateCdna(); + assertNotNull("Couldn't do a full width translation of test data.", + translated); + } + + /** + * Test variant in which 15 column blocks at a time are translated (the rest + * hidden). + * + * @throws IOException + */ + @Test + public void testTranslateCdna_withUntranslatableCodonsAndHiddenColumns() + throws IOException + { + AlignmentI alf = new FormatAdapter().readFile( + JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE, + "FASTA"); + int vwidth = 15; + for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth) + { + ColumnSelection cs = new ColumnSelection(); + if (ipos > 0) + { + cs.hideColumns(0, ipos - 1); + } + cs.hideColumns(ipos + vwidth, alf.getWidth()); + int[] vcontigs = cs.getVisibleContigs(0, alf.getWidth()); + AlignViewportI av = new AlignViewport(alf, cs); + Dna dna = new Dna(av, vcontigs); + AlignmentI transAlf = dna.translateCdna(); + + assertTrue("Translation failed (ipos=" + ipos + + ") No alignment data.", transAlf != null); + assertTrue("Translation failed (ipos=" + ipos + ") Empty alignment.", + transAlf.getHeight() > 0); + assertTrue("Translation failed (ipos=" + ipos + ") Translated " + + transAlf.getHeight() + " sequences from " + alf.getHeight() + + " sequences", alf.getHeight() == transAlf.getHeight()); + } + } + /** * Test simple translation to Amino Acids (with STOP codons translated to X). * * @throws IOException */ @Test - public void testCdnaTranslate_simple() throws IOException + public void testTranslateCdna_simple() throws IOException { AlignmentI alf = new FormatAdapter().readFile(fasta, FormatAdapter.PASTE, "FASTA"); - final String sequenceAsString = alf - .getSequenceAt(0).getSequenceAsString(); - AlignmentI translated = Dna.cdnaTranslate(alf.getSequencesArray(), - new String[] - { sequenceAsString }, new int[] - { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), null, - alf.getWidth(), null); + ColumnSelection cs = new ColumnSelection(); + AlignViewportI av = new AlignViewport(alf, cs); + Dna dna = new Dna(av, new int[] + { 0, alf.getWidth() - 1 }); + AlignmentI translated = dna.translateCdna(); String aa = translated.getSequenceAt(0).getSequenceAsString(); assertEquals( "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYYXXX", @@ -52,7 +171,7 @@ public class DnaTest * @throws IOException */ @Test - public void testCdnaTranslate_hiddenColumns() throws IOException + public void testTranslatCdna_hiddenColumns() throws IOException { AlignmentI alf = new FormatAdapter().readFile(fasta, FormatAdapter.PASTE, "FASTA"); @@ -60,11 +179,10 @@ public class DnaTest cs.hideColumns(6, 14); // hide codons 3/4/5 cs.hideColumns(24, 35); // hide codons 9-12 cs.hideColumns(177, 191); // hide codons 60-64 - AlignmentI translated = Dna.cdnaTranslate(alf.getSequencesArray(), - cs.getVisibleSequenceStrings(0, alf.getWidth(), - alf.getSequencesArray()), new int[] - { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), null, - alf.getWidth(), null); + AlignViewportI av = new AlignViewport(alf, cs); + Dna dna = new Dna(av, new int[] + { 0, alf.getWidth() - 1 }); + AlignmentI translated = dna.translateCdna(); String aa = translated.getSequenceAt(0).getSequenceAsString(); assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa); } diff --git a/test/jalview/analysis/DnaTranslation.java b/test/jalview/analysis/DnaTranslation.java deleted file mode 100644 index 8e6e5ae..0000000 --- a/test/jalview/analysis/DnaTranslation.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2) - * Copyright (C) 2014 The Jalview Authors - * - * This file is part of Jalview. - * - * Jalview is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. - * - * Jalview is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty - * of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Jalview. If not, see . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ -package jalview.analysis; - -import static org.junit.Assert.*; -import jalview.datamodel.ColumnSelection; - -import java.io.IOException; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; - -public class DnaTranslation -{ - - private static String JAL_1312_example_align_fasta = ">B.FR.83.HXB2_LAI_IIIB_BRU_K03455/45-306\n" - + "ATGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAATTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATACTGGGACA\n" - + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAGATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAC\n" - + ">gi|27804621|gb|AY178912.1|/1-259\n" - + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n" - + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGGACA\n" - + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTTCATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAT\n" - + ">gi|27804623|gb|AY178913.1|/1-259\n" - + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n" - + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGAACA\n" - + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTTCATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n" - + ">gi|27804627|gb|AY178915.1|/1-260\n" - + "-TGGGAAAA-ATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGAACGATTCGCAGTTAACCCTGGCCTGTTAGAAACATCAGAAGGTTGTAGACAAATATTGGGACA\n" - + "GCTACAACCATCCCTTGAGACAGGATCAGAAGAACTTAAATCATTATWTAATACCATAGCAGTCCTCTATTG\n" - + "TGTACATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAG\n" - + ">gi|27804631|gb|AY178917.1|/1-261\n" - + "-TGGGAAAAAATTCGGTTGAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACACCAGAAGGCTGTAGACAAATACTGGGACA\n" - + "GCTACAACCGTCCCTTCAGACAGGATCGGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTGCATCAAAGGATAGATGTAAAAGACACCAAGGAGGCTTTAGAC\n" - + ">gi|27804635|gb|AY178919.1|/1-261\n" - + "-TGGGAGAGAATTCGGTTACGGCCAGGAGGAAAGAAAAAATATAAATTGAAACATATAGTATGGGCAGGCAG\n" - + "AGAGCTAGATCGATTCGCAGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAGATATTGGGACA\n" - + "GCTACAACCGTCCCTTAAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTACATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n" - + ">gi|27804641|gb|AY178922.1|/1-261\n" - + "-TGGGAGAAAATTCGGTTACGGCCAGGGGGAAAGAAAAGATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAAATACTGGGACA\n" - + "GTTACACCCATCCCTTCATACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTGCATCAAAGGATAGAAGTAAAAGACACCAAGGAAGCTTTAGAC\n" - + ">gi|27804647|gb|AY178925.1|/1-261\n" - + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATCAATTAAAACATGTAGTATGGGCAAGCAG\n" - + "GGAACTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n" - + "GCTACAACCATCCCTTCAGACAGGATCAGAGGAACTTAAATCATTATTTAATACAGTAGCAGTCCTCTATTG\n" - + "TGTACATCAAAGAATAGATGTAAAAGACACCAAGGAAGCTCTAGAA\n" - + ">gi|27804649|gb|AY178926.1|/1-261\n" - + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGAACGATTCGCGGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAACTACTGGGACA\n" - + "GTTACAACCATCCCTTCAGACAGGATCAGAAGAACTCAAATCATTATATAATACAATAGCAACCCTCTATTG\n" - + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCCTTAGAT\n" - + ">gi|27804653|gb|AY178928.1|/1-261\n" - + "-TGGGAAAGAATTCGGTTAAGGCCAGGGGGAAAGAAACAATATAAATTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGACCGATTCGCACTTAACCCCGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n" - + "GCTACAATCGTCCCTTCAGACAGGATCAGAAGAACTTAGATCACTATATAATACAGTAGCAGTCCTCTATTG\n" - + "TGTGCATCAAAAGATAGATGTAAAAGACACCAAGGAAGCCTTAGAC\n" - + ">gi|27804659|gb|AY178931.1|/1-261\n" - + "-TGGGAAAAAATTCGGTTACGGCCAGGAGGAAAGAAAAGATATAAATTAAAACATATAGTATGGGCAAGCAG\n" - + "GGAGCTAGAACGATTYGCAGTTAATCCTGGCCTTTTAGAAACAGCAGAAGGCTGTAGACAAATACTGGGACA\n" - + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n" - + "TGTACATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAA\n"; - - @Test - public void translationWithUntranslatableCodonsTest() - { - // Corner case for this test is the presence of codons after codons that - // were not translated. - jalview.datamodel.AlignmentI alf = null; - try - { - alf = new jalview.io.FormatAdapter().readFile( - JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE, - "FASTA"); - } catch (IOException x) - { - x.printStackTrace(); - fail("Unexpected IOException (" + x.getMessage() - + ") - check test environment"); - } - { - // full translation - ColumnSelection cs = new jalview.datamodel.ColumnSelection(); - assertNotNull("Couldn't do a full width translation of test data.", - jalview.analysis.Dna.cdnaTranslate( - alf.getSequencesArray(), - cs.getVisibleSequenceStrings(0, alf.getWidth(), - alf.getSequencesArray()), new int[] - { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), - null, alf.getWidth(), null)); - } - int vwidth = 15; // translate in 15 base stretches - for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth) - { - ColumnSelection cs = new jalview.datamodel.ColumnSelection(); - if (ipos > 0) - { - cs.hideColumns(0, ipos - 1); - } - cs.hideColumns(ipos + vwidth, alf.getWidth()); - int[] vcontigs = cs.getVisibleContigs(0, alf.getWidth()); - String[] sel = cs.getVisibleSequenceStrings(0, alf.getWidth(), - alf.getSequencesArray()); - jalview.datamodel.AlignmentI transAlf = jalview.analysis.Dna - .cdnaTranslate(alf.getSequencesArray(), sel, vcontigs, - alf.getGapCharacter(), null, alf.getWidth(), null); - - assertTrue("Translation failed (ipos=" + ipos - + ") No alignment data.", transAlf != null); - assertTrue("Translation failed (ipos=" + ipos + ") Empty algnment.", - transAlf.getHeight() > 0); - assertTrue("Translation failed (ipos=" + ipos + ") Translated " - + transAlf.getHeight() + " sequences from " + alf.getHeight() - + " sequences", alf.getHeight() == transAlf.getHeight()); - } - - } -}