import static org.testng.AssertJUnit.assertSame;
import static org.testng.AssertJUnit.assertTrue;
+import jalview.analysis.AlignmentGenerator;
import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
-import jalview.io.AppletFormatAdapter;
+import jalview.gui.JvOptionPane;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
+import jalview.io.FileFormatI;
import jalview.io.FormatAdapter;
import jalview.util.MapList;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
*/
public class AlignmentTest
{
+
+ @BeforeClass(alwaysRun = true)
+ public void setUpJvOptionPane()
+ {
+ JvOptionPane.setInteractiveMode(false);
+ JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+ }
+
// @formatter:off
private static final String TEST_DATA =
"# STOCKHOLM 1.0\n" +
* @return
* @throws IOException
*/
- protected AlignmentI loadAlignment(final String data, String format)
+ protected AlignmentI loadAlignment(final String data, FileFormatI format)
throws IOException
{
- AlignmentI a = new FormatAdapter().readFile(data,
- AppletFormatAdapter.PASTE, format);
+ AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
+ format);
a.setDataset(null);
return a;
}
@BeforeMethod(alwaysRun = true)
public void setUp() throws IOException
{
- al = loadAlignment(TEST_DATA, "STH");
+ al = loadAlignment(TEST_DATA, FileFormat.Stockholm);
int i = 0;
for (AlignmentAnnotation ann : al.getAlignmentAnnotation())
{
AlignmentAnnotation ann = iter.next();
assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
assertFalse(iter.hasNext());
+
+ // invalid id
+ anns = al.findAnnotation("CalcIdForD.melanogaster.?");
+ assertFalse(iter.hasNext());
+ anns = al.findAnnotation(null);
+ assertFalse(iter.hasNext());
+ }
+
+ /**
+ * Test method that returns annotations that match on reference sequence,
+ * label, or calcId.
+ */
+ @Test(groups = { "Functional" })
+ public void testFindAnnotations_bySeqLabelandorCalcId()
+ {
+ // TODO: finish testFindAnnotations_bySeqLabelandorCalcId test
+ /* Note - this is an incomplete test - need to check null or
+ * non-null [ matches, not matches ] behaviour for each of the three
+ * parameters..*/
+
+ // search for a single, unique calcId with wildcards on other params
+ Iterable<AlignmentAnnotation> anns = al.findAnnotations(null,
+ "CalcIdForD.melanogaster.2", null);
+ Iterator<AlignmentAnnotation> iter = anns.iterator();
+ assertTrue(iter.hasNext());
+ AlignmentAnnotation ann = iter.next();
+ assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
+ assertFalse(iter.hasNext());
+
+ // save reference to test sequence reference parameter
+ SequenceI rseq = ann.sequenceRef;
+
+ // search for annotation associated with a single sequence
+ anns = al.findAnnotations(rseq, null, null);
+ iter = anns.iterator();
+ assertTrue(iter.hasNext());
+ ann = iter.next();
+ assertEquals("D.melanogaster.2", ann.sequenceRef.getName());
+ assertFalse(iter.hasNext());
+
+ // search for annotation with a non-existant calcId
+ anns = al.findAnnotations(null, "CalcIdForD.melanogaster.?", null);
+ iter = anns.iterator();
+ assertFalse(iter.hasNext());
+
+ // search for annotation with a particular label - expect three
+ anns = al.findAnnotations(null, null, "Secondary Structure");
+ iter = anns.iterator();
+ assertTrue(iter.hasNext());
+ iter.next();
+ assertTrue(iter.hasNext());
+ iter.next();
+ assertTrue(iter.hasNext());
+ iter.next();
+ // third found.. so
+ assertFalse(iter.hasNext());
+
+ // null on all parameters == find all annotations
+ anns = al.findAnnotations(null, null, null);
+ iter = anns.iterator();
+ int n = al.getAlignmentAnnotation().length;
+ while (iter.hasNext())
+ {
+ n--;
+ iter.next();
+ }
+ assertTrue("Found " + n + " fewer annotations from search.", n == 0);
}
@Test(groups = { "Functional" })
public void testAlignAs_dnaAsDna() throws IOException
{
// aligned cDNA:
- AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
+ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
// unaligned cDNA:
- AlignmentI al2 = loadAlignment(CDNA_SEQS_2, "FASTA");
+ AlignmentI al2 = loadAlignment(CDNA_SEQS_2, FileFormat.Fasta);
/*
* Make mappings between sequences. The 'aligned cDNA' is playing the role
public void testAlignAs_proteinAsCdna() throws IOException
{
// see also AlignmentUtilsTests
- AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
- AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
+ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
+ AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
makeMappings(al1, al2);
// Fudge - alignProteinAsCdna expects mappings to be on protein
/*
* Load alignments and add mappings for cDNA to protein
*/
- AlignmentI al1 = loadAlignment(CDNA_SEQS_1, "FASTA");
- AlignmentI al2 = loadAlignment(AA_SEQS_1, "FASTA");
+ AlignmentI al1 = loadAlignment(CDNA_SEQS_1, FileFormat.Fasta);
+ AlignmentI al2 = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
makeMappings(al1, al2);
/*
* Load alignments and add mappings from nucleotide to protein (or from
* first to second if both the same type)
*/
- AlignmentI al1 = loadAlignment(fromSeqs, "FASTA");
- AlignmentI al2 = loadAlignment(toSeqs, "FASTA");
+ AlignmentI al1 = loadAlignment(fromSeqs, FileFormat.Fasta);
+ AlignmentI al2 = loadAlignment(toSeqs, FileFormat.Fasta);
makeMappings(al1, al2);
/*
String dna1 = "A-Aa-gG-GCC-cT-TT";
String dna2 = "c--CCGgg-TT--T-AA-A";
AlignmentI al1 = loadAlignment(">Dna1/6-17\n" + dna1
- + "\n>Dna2/20-31\n" + dna2 + "\n", "FASTA");
+ + "\n>Dna2/20-31\n" + dna2 + "\n", FileFormat.Fasta);
AlignmentI al2 = loadAlignment(
- ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", "FASTA");
+ ">Pep1/7-9\n-P--YK\n>Pep2/11-13\nG-T--F\n", FileFormat.Fasta);
AlignedCodonFrame acf = new AlignedCodonFrame();
// Seq1 has intron at dna positions 3,4,9 so splice is AAG GCC TTT
// Seq2 has intron at dna positions 1,5,6 so splice is CCG TTT AAA
@Test(groups = "Functional")
public void testCopyConstructor() throws IOException
{
- AlignmentI protein = loadAlignment(AA_SEQS_1, FormatAdapter.PASTE);
+ AlignmentI protein = loadAlignment(AA_SEQS_1, FileFormat.Fasta);
// create sequence and alignment datasets
protein.setDataset(null);
AlignedCodonFrame acf = new AlignedCodonFrame();
public void testCreateDatasetAlignment() throws IOException
{
AlignmentI protein = new FormatAdapter().readFile(AA_SEQS_1,
- AppletFormatAdapter.PASTE, "FASTA");
+ DataSourceType.PASTE, FileFormat.Fasta);
/*
* create a dataset sequence on first sequence
* leave the second without one
"addSequence broke dataset reference integrity");
}
- @Test(groups = "Functional")
- public void getVisibleStartAndEndIndexTest()
- {
- Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- AlignmentI align = new Alignment(new SequenceI[] { seq });
- ArrayList<int[]> hiddenCols = new ArrayList<int[]>();
-
- int[] startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
- assertEquals(0, startEnd[0]);
- assertEquals(25, startEnd[1]);
-
- hiddenCols.add(new int[] { 0, 0 });
- startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
- assertEquals(1, startEnd[0]);
- assertEquals(25, startEnd[1]);
-
- hiddenCols.add(new int[] { 6, 9 });
- hiddenCols.add(new int[] { 11, 12 });
- startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
- assertEquals(1, startEnd[0]);
- assertEquals(25, startEnd[1]);
-
- hiddenCols.add(new int[] { 24, 25 });
- startEnd = align.getVisibleStartAndEndIndex(hiddenCols);
- System.out.println(startEnd[0] + " : " + startEnd[1]);
- assertEquals(1, startEnd[0]);
- assertEquals(23, startEnd[1]);
- }
-
/**
* Tests that dbrefs with mappings to sequence get updated if the sequence
* acquires a dataset sequence
assertSame(pep.getDatasetSequence(), cds.getDBRefs()[0].map.to);
}
+ @Test(groups = { "Functional" })
+ public void testFindGroup()
+ {
+ SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
+ SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
+ AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
+
+ assertNull(a.findGroup(null, 0));
+ assertNull(a.findGroup(seq1, 1));
+ assertNull(a.findGroup(seq1, -1));
+
+ /*
+ * add a group consisting of just "DEF"
+ */
+ SequenceGroup sg1 = new SequenceGroup();
+ sg1.addSequence(seq1, false);
+ sg1.setStartRes(3);
+ sg1.setEndRes(5);
+ a.addGroup(sg1);
+
+ assertNull(a.findGroup(seq1, 2)); // position not in group
+ assertNull(a.findGroup(seq1, 6)); // position not in group
+ assertNull(a.findGroup(seq2, 5)); // sequence not in group
+ assertSame(a.findGroup(seq1, 3), sg1); // yes
+ assertSame(a.findGroup(seq1, 4), sg1);
+ assertSame(a.findGroup(seq1, 5), sg1);
+
+ /*
+ * add a group consisting of
+ * EF--
+ * KLMN
+ */
+ SequenceGroup sg2 = new SequenceGroup();
+ sg2.addSequence(seq1, false);
+ sg2.addSequence(seq2, false);
+ sg2.setStartRes(4);
+ sg2.setEndRes(7);
+ a.addGroup(sg2);
+
+ assertNull(a.findGroup(seq1, 2)); // unchanged
+ assertSame(a.findGroup(seq1, 3), sg1); // unchanged
+ /*
+ * if a residue is in more than one group, method returns
+ * the first found (in order groups were added)
+ */
+ assertSame(a.findGroup(seq1, 4), sg1);
+ assertSame(a.findGroup(seq1, 5), sg1);
+
+ /*
+ * seq2 only belongs to the second group
+ */
+ assertSame(a.findGroup(seq2, 4), sg2);
+ assertSame(a.findGroup(seq2, 5), sg2);
+ assertSame(a.findGroup(seq2, 6), sg2);
+ assertSame(a.findGroup(seq2, 7), sg2);
+ assertNull(a.findGroup(seq2, 3));
+ assertNull(a.findGroup(seq2, 8));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testDeleteSequenceByIndex()
+ {
+ // create random alignment
+ AlignmentGenerator gen = new AlignmentGenerator(false);
+ AlignmentI a = gen.generate(20, 15, 123, 5, 5);
+
+ // delete sequence 10, alignment reduced by 1
+ int height = a.getAbsoluteHeight();
+ a.deleteSequence(10);
+ assertEquals(a.getAbsoluteHeight(), height - 1);
+
+ // try to delete -ve index, nothing happens
+ a.deleteSequence(-1);
+ assertEquals(a.getAbsoluteHeight(), height - 1);
+
+ // try to delete beyond end of alignment, nothing happens
+ a.deleteSequence(14);
+ assertEquals(a.getAbsoluteHeight(), height - 1);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testDeleteSequenceBySeq()
+ {
+ // create random alignment
+ AlignmentGenerator gen = new AlignmentGenerator(false);
+ AlignmentI a = gen.generate(20, 15, 123, 5, 5);
+
+ // delete sequence 10, alignment reduced by 1
+ int height = a.getAbsoluteHeight();
+ SequenceI seq = a.getSequenceAt(10);
+ a.deleteSequence(seq);
+ assertEquals(a.getAbsoluteHeight(), height - 1);
+
+ // try to delete non-existent sequence, nothing happens
+ seq = new Sequence("cds", "GCCTCGGAT");
+ assertEquals(a.getAbsoluteHeight(), height - 1);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testDeleteHiddenSequence()
+ {
+ // create random alignment
+ AlignmentGenerator gen = new AlignmentGenerator(false);
+ AlignmentI a = gen.generate(20, 15, 123, 5, 5);
+
+ // delete a sequence which is hidden, check it is NOT removed from hidden
+ // sequences
+ int height = a.getAbsoluteHeight();
+ SequenceI seq = a.getSequenceAt(2);
+ a.getHiddenSequences().hideSequence(seq);
+ assertEquals(a.getHiddenSequences().getSize(), 1);
+ a.deleteSequence(2);
+ assertEquals(a.getAbsoluteHeight(), height - 1);
+ assertEquals(a.getHiddenSequences().getSize(), 1);
+
+ // delete a sequence which is not hidden, check hiddenSequences are not
+ // affected
+ a.deleteSequence(10);
+ assertEquals(a.getAbsoluteHeight(), height - 2);
+ assertEquals(a.getHiddenSequences().getSize(), 1);
+ }
+
+ @Test(
+ groups = "Functional",
+ expectedExceptions = { IllegalArgumentException.class })
+ public void testSetDataset_selfReference()
+ {
+ SequenceI seq = new Sequence("a", "a");
+ AlignmentI alignment = new Alignment(new SequenceI[] { seq });
+ alignment.setDataset(alignment);
+ }
+
+ @Test(groups = "Functional")
+ public void testAppend()
+ {
+ SequenceI seq = new Sequence("seq1", "FRMLPSRT-A--L-");
+ AlignmentI alignment = new Alignment(new SequenceI[] { seq });
+ alignment.setGapCharacter('-');
+ SequenceI seq2 = new Sequence("seq1", "KP..L.FQII.");
+ AlignmentI alignment2 = new Alignment(new SequenceI[] { seq2 });
+ alignment2.setGapCharacter('.');
+
+ alignment.append(alignment2);
+
+ assertEquals('-', alignment.getGapCharacter());
+ assertSame(seq, alignment.getSequenceAt(0));
+ assertEquals("KP--L-FQII-", alignment.getSequenceAt(1)
+ .getSequenceAsString());
+
+ // todo test coverage for annotations, mappings, groups,
+ // hidden sequences, properties
+ }
}