test package fix for List<DBRefEntry> DBrefs (untested)
[jalview.git] / test / jalview / analysis / CrossRefTest.java
index b2720f2..a398cc9 100644 (file)
@@ -27,8 +27,9 @@ import static org.testng.AssertJUnit.assertNotSame;
 import static org.testng.AssertJUnit.assertNull;
 import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
-import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
 
+import jalview.datamodel.AlignedCodonFrame;
+import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
@@ -36,19 +37,31 @@ import jalview.datamodel.Mapping;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.gui.JvOptionPane;
 import jalview.util.DBRefUtils;
 import jalview.util.MapList;
 import jalview.ws.SequenceFetcher;
 import jalview.ws.SequenceFetcherFactory;
+import jalview.ws.params.InvalidArgumentException;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class CrossRefTest
 {
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   @Test(groups = { "Functional" })
   public void testFindXDbRefs()
   {
@@ -62,29 +75,28 @@ public class CrossRefTest
     DBRefEntry ref8 = new DBRefEntry("PFAM", "1", "A123");
     // ENSEMBL is a source of either dna or protein sequence data
     DBRefEntry ref9 = new DBRefEntry("ENSEMBL", "1", "A123");
-    DBRefEntry[] refs = new DBRefEntry[] { ref1, ref2, ref3, ref4, ref5,
-        ref6, ref7, ref8, ref9 };
+    List<DBRefEntry> refs = Arrays.asList(new DBRefEntry[] { ref1, ref2, ref3, ref4, ref5,
+            ref6, ref7, ref8, ref9 });
 
     /*
      * Just the DNA refs:
      */
-    DBRefEntry[] found = DBRefUtils.selectDbRefs(true, refs);
-    assertEquals(4, found.length);
-    assertSame(ref5, found[0]);
-    assertSame(ref6, found[1]);
-    assertSame(ref7, found[2]);
-    assertSame(ref9, found[3]);
+    List<DBRefEntry> found = DBRefUtils.selectDbRefs(true, refs);
+    assertEquals(4, found.size());
+    assertSame(ref5, found.get(0));
+    assertSame(ref6, found.get(1));
+    assertSame(ref7, found.get(2));
+    assertSame(ref9, found.get(3));
 
     /*
      * Just the protein refs:
      */
     found = DBRefUtils.selectDbRefs(false, refs);
-    assertEquals(5, found.length);
-    assertSame(ref1, found[0]);
-    assertSame(ref2, found[1]);
-    assertSame(ref3, found[2]);
-    assertSame(ref4, found[3]);
-    assertSame(ref9, found[4]);
+    assertEquals(4, found.size());
+    assertSame(ref1, found.get(0));
+    assertSame(ref2, found.get(1));
+    assertSame(ref4, found.get(2));
+    assertSame(ref9, found.get(3));
   }
 
   /**
@@ -92,18 +104,18 @@ public class CrossRefTest
    * which may be direct (dbrefs on the sequence), or indirect (dbrefs on
    * sequences which share a dbref with the sequence
    */
-  @Test(groups = { "Functional" })
+  @Test(groups = { "Functional" }, enabled = true)
   public void testFindXrefSourcesForSequence_proteinToDna()
   {
     SequenceI seq = new Sequence("Seq1", "MGKYQARLSS");
-    List<String> sources = new ArrayList<String>();
+    List<String> sources = new ArrayList<>();
     AlignmentI al = new Alignment(new SequenceI[] {});
 
     /*
      * first with no dbrefs to search
      */
     sources = new CrossRef(new SequenceI[] { seq }, al)
-            .findXrefSourcesForSequences();
+            .findXrefSourcesForSequences(false);
     assertTrue(sources.isEmpty());
 
     /*
@@ -120,9 +132,10 @@ public class CrossRefTest
     seq.addDBRef(new DBRefEntry("ENSEMBL", "0", "E2349"));
     seq.addDBRef(new DBRefEntry("ENSEMBLGENOMES", "0", "E2350"));
     sources = new CrossRef(new SequenceI[] { seq }, al)
-            .findXrefSourcesForSequences();
+            .findXrefSourcesForSequences(false);
+    // method is patched to remove EMBL from the sources to match
     assertEquals(4, sources.size());
-    assertEquals("[EMBL, EMBLCDS, GENEDB, ENSEMBL]",
+    assertEquals("[EMBLCDS, GENEDB, ENSEMBL, ENSEMBLGENOMES]",
             sources.toString());
 
     /*
@@ -130,7 +143,12 @@ public class CrossRefTest
      * and others to dna coding databases
      */
     sources.clear();
-    seq.setDBRefs(null);
+    try {
+               seq.setDBRefs(null);
+       } catch (InvalidArgumentException e) {
+               // TODO Auto-generated catch block
+               e.printStackTrace();
+       }
     seq.addDBRef(new DBRefEntry("UNIPROT", "0", "A1234"));
     seq.addDBRef(new DBRefEntry("EMBLCDS", "0", "E2347"));
     SequenceI seq2 = new Sequence("Seq2", "MGKYQARLSS");
@@ -139,10 +157,11 @@ public class CrossRefTest
     seq2.addDBRef(new DBRefEntry("GENEDB", "0", "E2348"));
     // TODO include ENSEMBLGENOMES in DBRefSource.DNACODINGDBS ?
     al.addSequence(seq2);
-    sources = new CrossRef(new SequenceI[] { seq }, al)
-            .findXrefSourcesForSequences();
-    assertEquals(3, sources.size());
-    assertEquals("[EMBLCDS, EMBL, GENEDB]", sources.toString());
+    sources = new CrossRef(new SequenceI[] { seq, seq2 }, al)
+            .findXrefSourcesForSequences(false);
+    // method removed EMBL from sources to match
+    assertEquals(2, sources.size());
+    assertEquals("[EMBLCDS, GENEDB]", sources.toString());
   }
 
   /**
@@ -150,7 +169,7 @@ public class CrossRefTest
    * xref is found - not on the nucleotide sequence but on a peptide sequence in
    * the alignment which which it shares a nucleotide dbref
    */
-  @Test(groups = { "Functional" })
+  @Test(groups = { "Functional" }, enabled = true)
   public void testFindXrefSequences_indirectDbrefToProtein()
   {
     /*
@@ -171,7 +190,7 @@ public class CrossRefTest
      */
     AlignmentI al = new Alignment(new SequenceI[] { emblSeq, uniprotSeq });
     Alignment xrefs = new CrossRef(new SequenceI[] { emblSeq }, al)
-            .findXrefSequences("UNIPROT");
+            .findXrefSequences("UNIPROT", true);
     assertEquals(1, xrefs.getHeight());
     assertSame(uniprotSeq, xrefs.getSequenceAt(0));
   }
@@ -181,7 +200,7 @@ public class CrossRefTest
    * xref is found - not on the peptide sequence but on a nucleotide sequence in
    * the alignment which which it shares a protein dbref
    */
-  @Test(groups = { "Functional" })
+  @Test(groups = { "Functional" }, enabled = true)
   public void testFindXrefSequences_indirectDbrefToNucleotide()
   {
     /*
@@ -194,7 +213,7 @@ public class CrossRefTest
     SequenceI emblSeq = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
     emblSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
     emblSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
-  
+
     /*
      * find EMBL xrefs for peptide sequence - it has no direct
      * dbrefs, but the 'corresponding' nucleotide sequence does, so is returned
@@ -205,8 +224,8 @@ public class CrossRefTest
      * - but nucleotide with matching peptide dbref does, so is returned
      */
     AlignmentI al = new Alignment(new SequenceI[] { emblSeq, uniprotSeq });
-    Alignment xrefs = new CrossRef(new SequenceI[] { uniprotSeq },
-            al).findXrefSequences("EMBL");
+    Alignment xrefs = new CrossRef(new SequenceI[] { uniprotSeq }, al)
+            .findXrefSequences("EMBL", false);
     assertEquals(1, xrefs.getHeight());
     assertSame(emblSeq, xrefs.getSequenceAt(0));
   }
@@ -225,7 +244,7 @@ public class CrossRefTest
     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
     dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
     SequenceI dna2 = new Sequence("AJ307031", "AAACCCTTT");
-  
+
     /*
      * find UNIPROT xrefs for peptide sequence - it has no direct
      * dbrefs, and the other sequence (which has a UNIPROT dbref) is not 
@@ -233,7 +252,7 @@ public class CrossRefTest
      */
     AlignmentI al = new Alignment(new SequenceI[] { dna1, dna2 });
     Alignment xrefs = new CrossRef(new SequenceI[] { dna2 }, al)
-            .findXrefSequences("UNIPROT");
+            .findXrefSequences("UNIPROT", true);
     assertNull(xrefs);
   }
 
@@ -241,7 +260,7 @@ public class CrossRefTest
    * Tests for the method that searches an alignment (with one sequence
    * excluded) for protein/nucleotide sequences with a given cross-reference
    */
-  @Test(groups = { "Functional" })
+  @Test(groups = { "Functional" }, enabled = true)
   public void testSearchDataset()
   {
     /*
@@ -249,82 +268,115 @@ public class CrossRefTest
      * peptide sequence with UNIPROT dbref
      */
     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
-    dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
+    Mapping map = new Mapping(new Sequence("pep2", "MLAVSRG"), new MapList(
+            new int[] { 1, 21 }, new int[] { 1, 7 }, 3, 1));
+    DBRefEntry dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2", map);
+    dna1.addDBRef(dbref);
     dna1.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
     SequenceI pep1 = new Sequence("Q9ZTS2", "MLAVSRGQ");
+    dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2");
     pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
     AlignmentI al = new Alignment(new SequenceI[] { dna1, pep1 });
 
-    List<SequenceI> result = new ArrayList<SequenceI>();
+    List<SequenceI> result = new ArrayList<>();
 
     /*
      * first search for a dbref nowhere on the alignment:
      */
-    DBRefEntry dbref = new DBRefEntry("UNIPROT", "0", "P30419");
+    dbref = new DBRefEntry("UNIPROT", "0", "P30419");
     CrossRef testee = new CrossRef(al.getSequencesArray(), al);
-    boolean found = testee.searchDataset(dna1, dbref, result, null, true);
+    AlignedCodonFrame acf = new AlignedCodonFrame();
+    boolean found = testee.searchDataset(true, dna1, dbref, result, acf,
+            true, DBRefUtils.SEARCH_MODE_FULL);
     assertFalse(found);
     assertTrue(result.isEmpty());
-
-    // TODO we are setting direct=true here but it is set to
-    // false in Jalview code...
+    assertTrue(acf.isEmpty());
 
     /*
      * search for a protein sequence with dbref UNIPROT:Q9ZTS2
      */
+    acf = new AlignedCodonFrame();
     dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2");
-    found = testee.searchDataset(dna1, dbref, result, null, true);
+    found = testee.searchDataset(!dna1.isProtein(), dna1, dbref, result,
+            acf, false, DBRefUtils.SEARCH_MODE_FULL); // search dataset with a protein xref from a dna
+                         // sequence to locate the protein product
     assertTrue(found);
     assertEquals(1, result.size());
     assertSame(pep1, result.get(0));
+    assertTrue(acf.isEmpty());
 
     /*
      * search for a nucleotide sequence with dbref UNIPROT:Q9ZTS2
      */
     result.clear();
+    acf = new AlignedCodonFrame();
     dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2");
-    found = testee.searchDataset(pep1, dbref, result, null, false);
+    found = testee.searchDataset(!pep1.isProtein(), pep1, dbref, result,
+            acf, false, DBRefUtils.SEARCH_MODE_FULL); // search dataset with a protein's direct dbref to
+                         // locate dna sequences with matching xref
     assertTrue(found);
     assertEquals(1, result.size());
     assertSame(dna1, result.get(0));
+    // should now have a mapping from dna to pep1
+    List<SequenceToSequenceMapping> mappings = acf.getMappings();
+    assertEquals(1, mappings.size());
+    SequenceToSequenceMapping mapping = mappings.get(0);
+    assertSame(dna1, mapping.getFromSeq());
+    assertSame(pep1, mapping.getMapping().getTo());
+    MapList mapList = mapping.getMapping().getMap();
+    assertEquals(1, mapList.getToRatio());
+    assertEquals(3, mapList.getFromRatio());
+    assertEquals(1, mapList.getFromRanges().size());
+    assertEquals(1, mapList.getFromRanges().get(0)[0]);
+    assertEquals(21, mapList.getFromRanges().get(0)[1]);
+    assertEquals(1, mapList.getToRanges().size());
+    assertEquals(1, mapList.getToRanges().get(0)[0]);
+    assertEquals(7, mapList.getToRanges().get(0)[1]);
   }
 
   /**
    * Test for finding 'product' sequences for the case where the selected
-   * sequence has a dbref with a mapping to a sequence
+   * sequence has a dbref with a mapping to a sequence. This represents the case
+   * where either
+   * <ul>
+   * <li>a fetched sequence is already decorated with its cross-reference (e.g.
+   * EMBL + translation), or</li>
+   * <li>Get Cross-References has been done once resulting in instantiated
+   * cross-reference mappings</li>
+   * </ul>
    */
   @Test(groups = { "Functional" })
   public void testFindXrefSequences_fromDbRefMap()
   {
     /*
-     * two peptide sequences each with a DBRef and SequenceFeature
+     * scenario: nucleotide sequence AF039662
+     *   with dbref + mapping to Q9ZTS2 and P30419
+     *     which themselves each have a dbref and feature
      */
+    SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
     SequenceI pep1 = new Sequence("Q9ZTS2", "MALFQRSV");
-    pep1.addDBRef(new DBRefEntry("Pfam", "0", "PF00111"));
+    SequenceI pep2 = new Sequence("P30419", "MTRRSQIF");
+    dna1.createDatasetSequence();
+    pep1.createDatasetSequence();
+    pep2.createDatasetSequence();
+
+    pep1.getDatasetSequence().addDBRef(
+            new DBRefEntry("Pfam", "0", "PF00111"));
     pep1.addSequenceFeature(new SequenceFeature("type", "desc", 12, 14, 1f,
             "group"));
-    SequenceI pep2 = new Sequence("P30419", "MTRRSQIF");
-    pep2.addDBRef(new DBRefEntry("PDB", "0", "3JTK"));
+    pep2.getDatasetSequence().addDBRef(new DBRefEntry("PDB", "0", "3JTK"));
     pep2.addSequenceFeature(new SequenceFeature("type2", "desc2", 13, 15,
             12f, "group2"));
 
-    /*
-     * nucleotide sequence (to go in the alignment)
-     */
-    SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
-
-    /*
-     * add DBRefEntry's to dna1 with mappings from dna to both peptides
-     */
     MapList mapList = new MapList(new int[] { 1, 24 }, new int[] { 1, 3 },
             3, 1);
     Mapping map = new Mapping(pep1, mapList);
     DBRefEntry dbRef1 = new DBRefEntry("UNIPROT", "0", "Q9ZTS2", map);
-    dna1.addDBRef(dbRef1);
+    dna1.getDatasetSequence().addDBRef(dbRef1);
     mapList = new MapList(new int[] { 1, 24 }, new int[] { 1, 3 }, 3, 1);
     map = new Mapping(pep2, mapList);
     DBRefEntry dbRef2 = new DBRefEntry("UNIPROT", "0", "P30419", map);
-    dna1.addDBRef(dbRef2);
+    dna1.getDatasetSequence().addDBRef(dbRef2);
 
     /*
      * find UNIPROT xrefs for nucleotide sequence - it should pick up 
@@ -332,7 +384,7 @@ public class CrossRefTest
      */
     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
     Alignment xrefs = new CrossRef(new SequenceI[] { dna1 }, al)
-            .findXrefSequences("UNIPROT");
+            .findXrefSequences("UNIPROT", true);
     assertEquals(2, xrefs.getHeight());
 
     /*
@@ -344,39 +396,21 @@ public class CrossRefTest
   }
 
   /**
-   * Helper method to assert seq1 looks like a copy of seq2
+   * Helper method that verifies that 'copy' has the same name, start, end,
+   * sequence and dataset sequence object as 'original' (but is not the same
+   * object)
    * 
-   * @param seq1
-   * @param seq2
+   * @param copy
+   * @param original
    */
-  private void checkCopySequence(SequenceI seq1, SequenceI seq2)
+  private void checkCopySequence(SequenceI copy, SequenceI original)
   {
-    assertNotSame(seq1, seq2);
-    assertEquals(seq1.getName(), seq2.getName());
-    assertEquals(seq1.getStart(), seq2.getStart());
-    assertEquals(seq1.getEnd(), seq2.getEnd());
-    assertEquals(seq1.getSequenceAsString(), seq2.getSequenceAsString());
-
-    /*
-     * compare dbrefs
-     */
-    assertArrayEquals(seq1.getDBRefs(), seq2.getDBRefs());
-    // check one to verify a copy, not the same object
-    if (seq1.getDBRefs().length > 0)
-    {
-      assertNotSame(seq1.getDBRefs()[0], seq2.getDBRefs()[0]);
-    }
-
-    /*
-     * compare features
-     */
-    assertArrayEquals(seq1.getSequenceFeatures(),
-            seq2.getSequenceFeatures());
-    if (seq1.getSequenceFeatures().length > 0)
-    {
-      assertNotSame(seq1.getSequenceFeatures()[0],
-              seq2.getSequenceFeatures()[0]);
-    }
+    assertNotSame(copy, original);
+    assertSame(copy.getDatasetSequence(), original.getDatasetSequence());
+    assertEquals(copy.getName(), original.getName());
+    assertEquals(copy.getStart(), original.getStart());
+    assertEquals(copy.getEnd(), original.getEnd());
+    assertEquals(copy.getSequenceAsString(), original.getSequenceAsString());
   }
 
   /**
@@ -387,17 +421,20 @@ public class CrossRefTest
   public void testFindXrefSequences_withFetch()
   {
     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
-    dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
-    dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P30419"));
-    dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P00314"));
+    dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "Q9ZTS2"));
+    dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "P30419"));
+    dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "P00314"));
     final SequenceI pep1 = new Sequence("Q9ZTS2", "MYQLIRSSW");
+    pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
+
     final SequenceI pep2 = new Sequence("P00314", "MRKLLAASG");
-  
+    pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "P00314"));
+
     /*
      * argument false suppresses adding DAS sources
      * todo: define an interface type SequenceFetcherI and mock that
      */
-    SequenceFetcher mockFetcher = new SequenceFetcher(false)
+    SequenceFetcher mockFetcher = new SequenceFetcher()
     {
       @Override
       public boolean isFetchable(String source)
@@ -418,7 +455,7 @@ public class CrossRefTest
      */
     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
     Alignment xrefs = new CrossRef(new SequenceI[] { dna1 }, al)
-            .findXrefSequences("UNIPROT");
+            .findXrefSequences("UNIPROT", true);
     assertEquals(2, xrefs.getHeight());
     assertSame(pep1, xrefs.getSequenceAt(0));
     assertSame(pep2, xrefs.getSequenceAt(1));
@@ -456,7 +493,7 @@ public class CrossRefTest
      * 'spliced transcript' with CDS ranges
      */
     SequenceI braf002 = new Sequence("ENST00000497784", "gCAGGCtaTCTGTTCaa");
-    braf002.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
+    braf002.addDBRef(new DBRefEntry("UNIPROT", "ENSEMBL|0", "H7C5K3"));
     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 2, 6, 0f,
             null));
     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, 0f,
@@ -468,20 +505,21 @@ public class CrossRefTest
      * which happens to be true for Uniprot,PDB,EMBL but not Pfam,Rfam,Ensembl 
      */
     final SequenceI pep1 = new Sequence("UNIPROT|P15056", "MAAL");
+    pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
     final SequenceI pep2 = new Sequence("UNIPROT|H7C5K3", "QALF");
-  
+    pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
     /*
      * argument false suppresses adding DAS sources
      * todo: define an interface type SequenceFetcherI and mock that
      */
-    SequenceFetcher mockFetcher = new SequenceFetcher(false)
+    SequenceFetcher mockFetcher = new SequenceFetcher()
     {
       @Override
       public boolean isFetchable(String source)
       {
         return true;
       }
-  
+
       @Override
       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
       {
@@ -489,7 +527,7 @@ public class CrossRefTest
       }
     };
     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
-  
+
     /*
      * find UNIPROT xrefs for gene and transcripts
      * verify that
@@ -499,8 +537,8 @@ public class CrossRefTest
      */
     SequenceI[] seqs = new SequenceI[] { gene, braf001, braf002 };
     AlignmentI al = new Alignment(seqs);
-    Alignment xrefs = new CrossRef(seqs, al)
-            .findXrefSequences("UNIPROT");
+    Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("UNIPROT",
+            true);
     assertEquals(2, xrefs.getHeight());
     assertSame(pep1, xrefs.getSequenceAt(0));
     assertSame(pep2, xrefs.getSequenceAt(1));
@@ -536,59 +574,57 @@ public class CrossRefTest
     /*
      * Uniprot sequences, both with xrefs to EMBL|J03321 
      * and EMBL|X07547
-     * Sequences faked to ensure dna translates to protein
-     * (so that mappings can be made)
      */
     SequenceI p0ce19 = new Sequence("UNIPROT|P0CE19", "KPFG");
     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "M19487"));
-    SequenceI p0ce20 = new Sequence("UNIPROT|P0CE20", "KPFG");
+    SequenceI p0ce20 = new Sequence("UNIPROT|P0CE20", "PFGK");
     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X07547"));
-  
+
     /*
      * EMBL sequences to be 'fetched', complete with dbrefs and mappings
      * to their protein products (CDS location  and translations  are provided
      * in  EMBL XML); these should be matched to, and replaced with,
      * the corresponding uniprot sequences after fetching
      */
-    
+
     /*
      * J03321 with mappings to P0CE19 and P0CE20
      */
-    final SequenceI j03321 = new Sequence("EMBL|J03321", "AAACCCTTTGGG");
+    final SequenceI j03321 = new Sequence("EMBL|J03321", "AAACCCTTTGGGAAAA");
     DBRefEntry dbref1 = new DBRefEntry("UNIPROT", "0", "P0CE19");
-    MapList mapList = new MapList(new int[] { 1, 18 },
-            new int[] { 1, 6 }, 3, 1);
-    Mapping map = new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"), mapList);
+    MapList mapList = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 },
+            3, 1);
+    Mapping map = new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
+            mapList);
     // add a dbref to the mapped to sequence - should get copied to p0ce19
     map.getTo().addDBRef(new DBRefEntry("PIR", "0", "S01875"));
     dbref1.setMap(map);
     j03321.addDBRef(dbref1);
     DBRefEntry dbref2 = new DBRefEntry("UNIPROT", "0", "P0CE20");
-    dbref2.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "KPFG"),
+    mapList = new MapList(new int[] { 4, 15 }, new int[] { 2, 5 }, 3, 1);
+    dbref2.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
             new MapList(mapList)));
     j03321.addDBRef(dbref2);
-    
+
     /*
      * X06707 with mappings to P0CE19 and P0CE20
      */
     final SequenceI x06707 = new Sequence("EMBL|X06707", "atgAAACCCTTTGGG");
-    // TODO CrossRef.constructMapping ignores the reverse mapping ??
-    // should it not use its inverse if available?
-    // how does this work for real?
     DBRefEntry dbref3 = new DBRefEntry("UNIPROT", "0", "P0CE19");
-    MapList map2 = new MapList(new int[] { 4, 21 }, new int[] { 1, 6 }, 3,
+    MapList map2 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
             1);
     dbref3.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"), map2));
     x06707.addDBRef(dbref3);
     DBRefEntry dbref4 = new DBRefEntry("UNIPROT", "0", "P0CE20");
-    dbref4.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "KPFG"),
-            new MapList(mapList)));
+    MapList map3 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
+            1);
+    dbref4.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"), map3));
     x06707.addDBRef(dbref4);
-    
+
     /*
      * M19487 with mapping to P0CE19 and Q46432
      */
@@ -607,38 +643,51 @@ public class CrossRefTest
      */
     final SequenceI x07547 = new Sequence("EMBL|X07547", "cccAAACCCTTTGGG");
     DBRefEntry dbref7 = new DBRefEntry("UNIPROT", "0", "P0CE20");
-    dbref7.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
+    dbref7.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
             new MapList(map2)));
     x07547.addDBRef(dbref7);
     DBRefEntry dbref8 = new DBRefEntry("UNIPROT", "0", "B0BCM4");
     dbref8.setMap(new Mapping(new Sequence("UNIPROT|B0BCM4", "KPFG"),
             new MapList(map2)));
     x07547.addDBRef(dbref8);
-  
+
     /*
      * mock sequence fetcher to 'return' the EMBL sequences
      * TODO: Mockito would allow .thenReturn().thenReturn() here, 
      * and also capture and verification of the parameters
-     * passed in calls to getSequences() 
+     * passed in calls to getSequences() - important to verify that
+     * duplicate sequence fetches are not requested
      */
-    SequenceFetcher mockFetcher = new SequenceFetcher(false)
+    SequenceFetcher mockFetcher = new SequenceFetcher()
     {
-      int call  = 0;
+      int call = 0;
+
       @Override
       public boolean isFetchable(String source)
       {
         return true;
       }
+
       @Override
       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
       {
         call++;
-        return call == 1 ? new SequenceI[] { j03321, x06707, m19487 }
-                : new SequenceI[] { x07547 };
+        if (call == 1)
+        {
+          assertEquals("Expected 3 embl seqs in first fetch", 3,
+                  refs.size());
+          return new SequenceI[] { j03321, x06707, m19487 };
+        }
+        else
+        {
+          assertEquals("Expected 1 embl seq in second fetch", 1,
+                  refs.size());
+          return new SequenceI[] { x07547 };
+        }
       }
     };
     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
-  
+
     /*
      * find EMBL xrefs for Uniprot seqs and verify that
      * - the EMBL xref'd sequences are retrieved without duplicates
@@ -649,8 +698,8 @@ public class CrossRefTest
      */
     SequenceI[] seqs = new SequenceI[] { p0ce19, p0ce20 };
     AlignmentI al = new Alignment(seqs);
-    Alignment xrefs = new CrossRef(seqs, al)
-            .findXrefSequences("EMBL");
+    Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("EMBL",
+            false);
 
     /*
      * verify retrieved sequences
@@ -665,30 +714,31 @@ public class CrossRefTest
     /*
      * verify mappings added to Uniprot-to-EMBL dbrefs
      */
-    Mapping mapping = p0ce19.getDBRefs()[0].getMap();
+    Mapping mapping = p0ce19.getDBRefs().get(0).getMap();
     assertSame(j03321, mapping.getTo());
-    mapping = p0ce19.getDBRefs()[1].getMap();
+    mapping = p0ce19.getDBRefs().get(1).getMap();
     assertSame(x06707, mapping.getTo());
-    mapping = p0ce20.getDBRefs()[0].getMap();
+    mapping = p0ce20.getDBRefs().get(0).getMap();
     assertSame(j03321, mapping.getTo());
-    mapping = p0ce20.getDBRefs()[1].getMap();
+    mapping = p0ce20.getDBRefs().get(1).getMap();
     assertSame(x06707, mapping.getTo());
 
     /*
      * verify dbrefs on EMBL are mapped to alignment seqs
      */
-    assertSame(p0ce19, j03321.getDBRefs()[0].getMap().getTo());
-    assertSame(p0ce20, j03321.getDBRefs()[1].getMap().getTo());
-    assertSame(p0ce19, x06707.getDBRefs()[0].getMap().getTo());
-    assertSame(p0ce20, x06707.getDBRefs()[1].getMap().getTo());
+    
+    assertSame(p0ce19, j03321.getDBRefs().get(0).getMap().getTo());
+    assertSame(p0ce20, j03321.getDBRefs().get(1).getMap().getTo());
+    assertSame(p0ce19, x06707.getDBRefs().get(0).getMap().getTo());
+    assertSame(p0ce20, x06707.getDBRefs().get(1).getMap().getTo());
 
     /*
      * verify new dbref on EMBL dbref mapping is copied to the
      * original Uniprot sequence
      */
-    assertEquals(4, p0ce19.getDBRefs().length);
-    assertEquals("PIR", p0ce19.getDBRefs()[3].getSource());
-    assertEquals("S01875", p0ce19.getDBRefs()[3].getAccessionId());
+    assertEquals(4, p0ce19.getDBRefs().size());
+    assertEquals("PIR", p0ce19.getDBRefs().get(3).getSource());
+    assertEquals("S01875", p0ce19.getDBRefs().get(3).getAccessionId());
   }
 
   @Test(groups = "Functional")