X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fanalysis%2FDnaTest.java;h=262de12cdf8401eb55c94cad1ea7af51e2573449;hb=279c6495df1a0de231600a2d283098c7411c4730;hp=aeff465c9671ca0e293d46fd58c2df640db9aa7c;hpb=52288466dd1e71946a06fd1e6ea15fa8e652c693;p=jalview.git
diff --git a/test/jalview/analysis/DnaTest.java b/test/jalview/analysis/DnaTest.java
index aeff465..262de12 100644
--- a/test/jalview/analysis/DnaTest.java
+++ b/test/jalview/analysis/DnaTest.java
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ 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.testng.AssertJUnit.assertEquals;
@@ -8,17 +28,30 @@ import jalview.api.AlignViewportI;
import jalview.datamodel.AlignedCodon;
import jalview.datamodel.Alignment;
import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
+import jalview.datamodel.Sequence;
import jalview.datamodel.SequenceI;
import jalview.gui.AlignViewport;
+import jalview.gui.JvOptionPane;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
import jalview.io.FormatAdapter;
import java.io.IOException;
+import java.util.Iterator;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class DnaTest
{
+ @BeforeClass(alwaysRun = true)
+ public void setUpJvOptionPane()
+ {
+ JvOptionPane.setInteractiveMode(false);
+ JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+ }
+
// @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"
@@ -99,12 +132,15 @@ public class DnaTest
throws IOException
{
AlignmentI alf = new FormatAdapter().readFile(
- JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
- "FASTA");
- ColumnSelection cs = new ColumnSelection();
+ JAL_1312_example_align_fasta, DataSourceType.PASTE,
+ FileFormat.Fasta);
+ HiddenColumns cs = new HiddenColumns();
AlignViewportI av = new AlignViewport(alf, cs);
- Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
- AlignmentI translated = dna.translateCdna();
+ Iterator contigs = cs.getVisContigsIterator(0, alf.getWidth(),
+ false);
+ Dna dna = new Dna(av, contigs);
+ AlignmentI translated = dna.translateCdna(
+ GeneticCodes.getInstance().getStandardCodeTable());
assertNotNull("Couldn't do a full width translation of test data.",
translated);
}
@@ -120,29 +156,34 @@ public class DnaTest
throws IOException
{
AlignmentI alf = new FormatAdapter().readFile(
- JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
- "FASTA");
+ JAL_1312_example_align_fasta, DataSourceType.PASTE,
+ FileFormat.Fasta);
int vwidth = 15;
for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth)
{
- ColumnSelection cs = new ColumnSelection();
+ HiddenColumns cs = new HiddenColumns();
if (ipos > 0)
{
cs.hideColumns(0, ipos - 1);
}
cs.hideColumns(ipos + vwidth, alf.getWidth());
- int[] vcontigs = cs.getVisibleContigs(0, alf.getWidth());
+ Iterator vcontigs = cs.getVisContigsIterator(0, alf.getWidth(),
+ false);
AlignViewportI av = new AlignViewport(alf, cs);
Dna dna = new Dna(av, vcontigs);
- AlignmentI transAlf = dna.translateCdna();
+ AlignmentI transAlf = dna.translateCdna(
+ GeneticCodes.getInstance().getStandardCodeTable());
- assertTrue("Translation failed (ipos=" + ipos
- + ") No alignment data.", transAlf != null);
+ 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());
+ assertTrue(
+ "Translation failed (ipos=" + ipos + ") Translated "
+ + transAlf.getHeight() + " sequences from "
+ + alf.getHeight() + " sequences",
+ alf.getHeight() == transAlf.getHeight());
}
}
@@ -155,11 +196,14 @@ public class DnaTest
public void testTranslateCdna_simple() throws IOException
{
AlignmentI alf = new FormatAdapter().readFile(fasta,
- FormatAdapter.PASTE, "FASTA");
- ColumnSelection cs = new ColumnSelection();
+ DataSourceType.PASTE, FileFormat.Fasta);
+ HiddenColumns cs = new HiddenColumns();
AlignViewportI av = new AlignViewport(alf, cs);
- Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
- AlignmentI translated = dna.translateCdna();
+ Iterator contigs = cs.getVisContigsIterator(0, alf.getWidth(),
+ false);
+ Dna dna = new Dna(av, contigs);
+ AlignmentI translated = dna.translateCdna(
+ GeneticCodes.getInstance().getStandardCodeTable());
String aa = translated.getSequenceAt(0).getSequenceAsString();
assertEquals(
"AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYY***",
@@ -175,16 +219,20 @@ public class DnaTest
public void testTranslateCdna_hiddenColumns() throws IOException
{
AlignmentI alf = new FormatAdapter().readFile(fasta,
- FormatAdapter.PASTE, "FASTA");
- ColumnSelection cs = new jalview.datamodel.ColumnSelection();
+ DataSourceType.PASTE, FileFormat.Fasta);
+ HiddenColumns cs = new HiddenColumns();
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
AlignViewportI av = new AlignViewport(alf, cs);
- Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
- AlignmentI translated = dna.translateCdna();
+ Iterator contigs = cs.getVisContigsIterator(0, alf.getWidth(),
+ false);
+ Dna dna = new Dna(av, contigs);
+ AlignmentI translated = dna.translateCdna(
+ GeneticCodes.getInstance().getStandardCodeTable());
String aa = translated.getSequenceAt(0).getSequenceAsString();
- assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
+ assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW",
+ aa);
}
/**
@@ -262,11 +310,15 @@ public class DnaTest
/*
* Generate cDNA - 8 sequences of 12 bases each.
*/
- AlignmentI cdna = new DnaAlignmentGenerator().generate(12, 8, 97, 5, 5);
- ColumnSelection cs = new ColumnSelection();
+ AlignmentI cdna = new AlignmentGenerator(true).generate(12, 8, 97, 5,
+ 5);
+ HiddenColumns cs = new HiddenColumns();
AlignViewportI av = new AlignViewport(cdna, cs);
- Dna dna = new Dna(av, new int[] { 0, cdna.getWidth() - 1 });
- AlignmentI translated = dna.translateCdna();
+ Iterator contigs = cs.getVisContigsIterator(0, cdna.getWidth(),
+ false);
+ Dna dna = new Dna(av, contigs);
+ AlignmentI translated = dna.translateCdna(
+ GeneticCodes.getInstance().getStandardCodeTable());
/*
* Jumble the cDNA sequences and translate.
@@ -280,8 +332,10 @@ public class DnaTest
}
AlignmentI cdnaReordered = new Alignment(sorted);
av = new AlignViewport(cdnaReordered, cs);
- dna = new Dna(av, new int[] { 0, cdna.getWidth() - 1 });
- AlignmentI translated2 = dna.translateCdna();
+ contigs = cs.getVisContigsIterator(0, cdna.getWidth(), false);
+ dna = new Dna(av, contigs);
+ AlignmentI translated2 = dna.translateCdna(
+ GeneticCodes.getInstance().getStandardCodeTable());
/*
* Check translated sequences are the same in both alignments.
@@ -294,10 +348,10 @@ public class DnaTest
int sortedSequenceIndex = 0;
for (int originalSequenceIndex : jumbler)
{
- final String translation1 = translated.getSequenceAt(
- originalSequenceIndex).getSequenceAsString();
- final String translation2 = translated2.getSequenceAt(
- sortedSequenceIndex).getSequenceAsString();
+ final String translation1 = translated
+ .getSequenceAt(originalSequenceIndex).getSequenceAsString();
+ final String translation2 = translated2
+ .getSequenceAt(sortedSequenceIndex).getSequenceAsString();
assertEquals(translation2, translation1);
sortedSequenceIndex++;
}
@@ -341,8 +395,10 @@ public class DnaTest
private void assertSymmetric(String codon1, String codon2)
{
- assertEquals("Comparison of '" + codon1 + "' and '" + codon2
- + " not symmetric", Integer.signum(compare(codon1, codon2)),
+ assertEquals(
+ "Comparison of '" + codon1 + "' and '" + codon2
+ + " not symmetric",
+ Integer.signum(compare(codon1, codon2)),
-Integer.signum(compare(codon2, codon1)));
}
@@ -371,8 +427,8 @@ public class DnaTest
*/
private void assertPrecedes(String codon1, String codon2)
{
- assertEquals("Expected '" + codon1 + "' precedes '" + codon2 + "'",
- -1, compare(codon1, codon2));
+ assertEquals("Expected '" + codon1 + "' precedes '" + codon2 + "'", -1,
+ compare(codon1, codon2));
}
/**
@@ -437,4 +493,87 @@ public class DnaTest
assertEquals("[0, 2, 5]", convertCodon("A-A--A").toString());
assertEquals("[1, 3, 4]", convertCodon("-A-AA-").toString());
}
+
+ /**
+ * Test dna complementing
+ */
+ @Test(groups = "Functional")
+ public void testGetComplement()
+ {
+ assertEquals('t', Dna.getComplement('a'));
+ assertEquals('T', Dna.getComplement('A'));
+ assertEquals('a', Dna.getComplement('t'));
+ assertEquals('A', Dna.getComplement('T'));
+ assertEquals('c', Dna.getComplement('g'));
+ assertEquals('C', Dna.getComplement('G'));
+ assertEquals('g', Dna.getComplement('c'));
+ assertEquals('G', Dna.getComplement('C'));
+ // note uU --> aA but not vice versa
+ assertEquals('a', Dna.getComplement('u'));
+ assertEquals('A', Dna.getComplement('U'));
+ // ambiguity codes, see http://www.bioinformatics.org/sms/iupac.html
+ assertEquals('r', Dna.getComplement('y'));
+ assertEquals('R', Dna.getComplement('Y'));
+ assertEquals('y', Dna.getComplement('r'));
+ assertEquals('Y', Dna.getComplement('R'));
+ assertEquals('k', Dna.getComplement('m'));
+ assertEquals('K', Dna.getComplement('M'));
+ assertEquals('m', Dna.getComplement('k'));
+ assertEquals('M', Dna.getComplement('K'));
+ assertEquals('b', Dna.getComplement('v'));
+ assertEquals('B', Dna.getComplement('V'));
+ assertEquals('v', Dna.getComplement('b'));
+ assertEquals('V', Dna.getComplement('B'));
+ assertEquals('d', Dna.getComplement('h'));
+ assertEquals('D', Dna.getComplement('H'));
+ assertEquals('h', Dna.getComplement('d'));
+ assertEquals('H', Dna.getComplement('D'));
+ assertEquals('Q', Dna.getComplement('Q'));
+ }
+
+ @Test(groups = "Functional")
+ public void testReverseSequence()
+ {
+ String seq = "-Ac-GtU--rYkMbVdHNX-";
+ String seqRev = new StringBuilder(seq).reverse().toString();
+
+ // reverse:
+ SequenceI reversed = Dna.reverseSequence("Seq1", seq, false);
+ assertEquals(1, reversed.getStart());
+ assertEquals(15, reversed.getEnd());
+ assertEquals(20, reversed.getLength());
+ assertEquals(seqRev, reversed.getSequenceAsString());
+ assertEquals("Seq1|rev", reversed.getName());
+
+ // reverse complement:
+ SequenceI revcomp = Dna.reverseSequence("Seq1", seq, true);
+ assertEquals("-XNDhBvKmRy--AaC-gT-", revcomp.getSequenceAsString());
+ assertEquals("Seq1|revcomp", revcomp.getName());
+ }
+
+ @Test(groups = "Functional")
+ public void testReverseCdna()
+ {
+ String seq = "-Ac-GtU--rYkMbVdHNX-";
+ String seqRev = new StringBuilder(seq).reverse().toString();
+ String seqDs = seq.replaceAll("-", "");
+ String seqDsRev = new StringBuilder(seqDs).reverse().toString();
+
+ SequenceI dna = new Sequence("Seq1", seq);
+ Alignment al = new Alignment(new SequenceI[] { dna });
+ al.createDatasetAlignment();
+ assertEquals(seqDs,
+ al.getSequenceAt(0).getDatasetSequence().getSequenceAsString());
+
+ HiddenColumns cs = new HiddenColumns();
+ AlignViewportI av = new AlignViewport(al, cs);
+ Iterator contigs = cs.getVisContigsIterator(0, al.getWidth(),
+ false);
+ Dna testee = new Dna(av, contigs);
+ AlignmentI reversed = testee.reverseCdna(false);
+ assertEquals(1, reversed.getHeight());
+ assertEquals(seqRev, reversed.getSequenceAt(0).getSequenceAsString());
+ assertEquals(seqDsRev, reversed.getSequenceAt(0).getDatasetSequence()
+ .getSequenceAsString());
+ }
}