Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / test / jalview / analysis / CrossRefTest.java
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 static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNotSame;
27 import static org.testng.AssertJUnit.assertNull;
28 import static org.testng.AssertJUnit.assertSame;
29 import static org.testng.AssertJUnit.assertTrue;
30
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.DBRefEntry;
36 import jalview.datamodel.Mapping;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceFeature;
39 import jalview.datamodel.SequenceI;
40 import jalview.gui.JvOptionPane;
41 import jalview.util.DBRefUtils;
42 import jalview.util.MapList;
43 import jalview.ws.SequenceFetcher;
44 import jalview.ws.SequenceFetcherFactory;
45
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.List;
49
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.BeforeClass;
52 import org.testng.annotations.Test;
53
54 public class CrossRefTest
55 {
56
57   @BeforeClass(alwaysRun = true)
58   public void setUpJvOptionPane()
59   {
60     JvOptionPane.setInteractiveMode(false);
61     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
62   }
63
64   @Test(groups = { "Functional" })
65   public void testFindXDbRefs()
66   {
67     DBRefEntry ref1 = new DBRefEntry("UNIPROT", "1", "A123");
68     DBRefEntry ref2 = new DBRefEntry("UNIPROTKB/TREMBL", "1", "A123");
69     DBRefEntry ref3 = new DBRefEntry("pdb", "1", "A123");
70     DBRefEntry ref4 = new DBRefEntry("EMBLCDSPROTEIN", "1", "A123");
71     DBRefEntry ref5 = new DBRefEntry("embl", "1", "A123");
72     DBRefEntry ref6 = new DBRefEntry("emblCDS", "1", "A123");
73     DBRefEntry ref7 = new DBRefEntry("GeneDB", "1", "A123");
74     DBRefEntry ref8 = new DBRefEntry("PFAM", "1", "A123");
75     // ENSEMBL is a source of either dna or protein sequence data
76     DBRefEntry ref9 = new DBRefEntry("ENSEMBL", "1", "A123");
77     List<DBRefEntry> refs = Arrays.asList(new DBRefEntry[] { ref1, ref2, ref3, ref4, ref5,
78             ref6, ref7, ref8, ref9 });
79
80     /*
81      * Just the DNA refs:
82      */
83     List<DBRefEntry> found = DBRefUtils.selectDbRefs(true, refs);
84     assertEquals(4, found.size());
85     assertSame(ref5, found.get(0));
86     assertSame(ref6, found.get(1));
87     assertSame(ref7, found.get(2));
88     assertSame(ref9, found.get(3));
89
90     /*
91      * Just the protein refs:
92      */
93     found = DBRefUtils.selectDbRefs(false, refs);
94     assertEquals(4, found.size());
95     assertSame(ref1, found.get(0));
96     assertSame(ref2, found.get(1));
97     assertSame(ref4, found.get(2));
98     assertSame(ref9, found.get(3));
99   }
100
101   /**
102    * Test the method that finds a sequence's "product" xref source databases,
103    * which may be direct (dbrefs on the sequence), or indirect (dbrefs on
104    * sequences which share a dbref with the sequence
105    */
106   @Test(groups = { "Functional" }, enabled = true)
107   public void testFindXrefSourcesForSequence_proteinToDna()
108   {
109     SequenceI seq = new Sequence("Seq1", "MGKYQARLSS");
110     List<String> sources = new ArrayList<>();
111     AlignmentI al = new Alignment(new SequenceI[] {});
112
113     /*
114      * first with no dbrefs to search
115      */
116     sources = new CrossRef(new SequenceI[] { seq }, al)
117             .findXrefSourcesForSequences(false);
118     assertTrue(sources.isEmpty());
119
120     /*
121      * add some dbrefs to sequence
122      */
123     // protein db is not a candidate for findXrefSources
124     seq.addDBRef(new DBRefEntry("UNIPROT", "0", "A1234"));
125     // dna coding databatases are
126     seq.addDBRef(new DBRefEntry("EMBL", "0", "E2345"));
127     // a second EMBL xref should not result in a duplicate
128     seq.addDBRef(new DBRefEntry("EMBL", "0", "E2346"));
129     seq.addDBRef(new DBRefEntry("EMBLCDS", "0", "E2347"));
130     seq.addDBRef(new DBRefEntry("GENEDB", "0", "E2348"));
131     seq.addDBRef(new DBRefEntry("ENSEMBL", "0", "E2349"));
132     seq.addDBRef(new DBRefEntry("ENSEMBLGENOMES", "0", "E2350"));
133     sources = new CrossRef(new SequenceI[] { seq }, al)
134             .findXrefSourcesForSequences(false);
135     // method is patched to remove EMBL from the sources to match
136     assertEquals(4, sources.size());
137     assertEquals("[EMBLCDS, GENEDB, ENSEMBL, ENSEMBLGENOMES]",
138             sources.toString());
139
140     /*
141      * add a sequence to the alignment which has a dbref to UNIPROT|A1234
142      * and others to dna coding databases
143      */
144     sources.clear();
145         seq.setDBRefs(null);
146     seq.addDBRef(new DBRefEntry("UNIPROT", "0", "A1234"));
147     seq.addDBRef(new DBRefEntry("EMBLCDS", "0", "E2347"));
148     SequenceI seq2 = new Sequence("Seq2", "MGKYQARLSS");
149     seq2.addDBRef(new DBRefEntry("UNIPROT", "0", "A1234"));
150     seq2.addDBRef(new DBRefEntry("EMBL", "0", "E2345"));
151     seq2.addDBRef(new DBRefEntry("GENEDB", "0", "E2348"));
152     // TODO include ENSEMBLGENOMES in DBRefSource.DNACODINGDBS ?
153     al.addSequence(seq2);
154     sources = new CrossRef(new SequenceI[] { seq, seq2 }, al)
155             .findXrefSourcesForSequences(false);
156     // method removed EMBL from sources to match
157     assertEquals(2, sources.size());
158     assertEquals("[EMBLCDS, GENEDB]", sources.toString());
159   }
160
161   /**
162    * Test for finding 'product' sequences for the case where only an indirect
163    * xref is found - not on the nucleotide sequence but on a peptide sequence in
164    * the alignment which which it shares a nucleotide dbref
165    */
166   @Test(groups = { "Functional" }, enabled = true)
167   public void testFindXrefSequences_indirectDbrefToProtein()
168   {
169     /*
170      * Alignment setup:
171      *   - nucleotide dbref  EMBL|AF039662
172      *   - peptide    dbrefs EMBL|AF039662, UNIPROT|Q9ZTS2
173      */
174     SequenceI emblSeq = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
175     emblSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
176     SequenceI uniprotSeq = new Sequence("Q9ZTS2", "MASVSATMISTS");
177     uniprotSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
178     uniprotSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
179
180     /*
181      * Find UNIPROT xrefs for nucleotide 
182      * - it has no UNIPROT dbref of its own
183      * - but peptide with matching nucleotide dbref does, so is returned
184      */
185     AlignmentI al = new Alignment(new SequenceI[] { emblSeq, uniprotSeq });
186     Alignment xrefs = new CrossRef(new SequenceI[] { emblSeq }, al)
187             .findXrefSequences("UNIPROT", true);
188     assertEquals(1, xrefs.getHeight());
189     assertSame(uniprotSeq, xrefs.getSequenceAt(0));
190   }
191
192   /**
193    * Test for finding 'product' sequences for the case where only an indirect
194    * xref is found - not on the peptide sequence but on a nucleotide sequence in
195    * the alignment which which it shares a protein dbref
196    */
197   @Test(groups = { "Functional" }, enabled = true)
198   public void testFindXrefSequences_indirectDbrefToNucleotide()
199   {
200     /*
201      * Alignment setup:
202      *   - peptide    dbref  UNIPROT|Q9ZTS2
203      *   - nucleotide dbref  EMBL|AF039662, UNIPROT|Q9ZTS2
204      */
205     SequenceI uniprotSeq = new Sequence("Q9ZTS2", "MASVSATMISTS");
206     uniprotSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
207     SequenceI emblSeq = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
208     emblSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
209     emblSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
210
211     /*
212      * find EMBL xrefs for peptide sequence - it has no direct
213      * dbrefs, but the 'corresponding' nucleotide sequence does, so is returned
214      */
215     /*
216      * Find EMBL xrefs for peptide 
217      * - it has no EMBL dbref of its own
218      * - but nucleotide with matching peptide dbref does, so is returned
219      */
220     AlignmentI al = new Alignment(new SequenceI[] { emblSeq, uniprotSeq });
221     Alignment xrefs = new CrossRef(new SequenceI[] { uniprotSeq }, al)
222             .findXrefSequences("EMBL", false);
223     assertEquals(1, xrefs.getHeight());
224     assertSame(emblSeq, xrefs.getSequenceAt(0));
225   }
226
227   /**
228    * Test for finding 'product' sequences for the case where the selected
229    * sequence has no dbref to the desired source, and there are no indirect
230    * references via another sequence in the alignment
231    */
232   @Test(groups = { "Functional" })
233   public void testFindXrefSequences_noDbrefs()
234   {
235     /*
236      * two nucleotide sequences, one with UNIPROT dbref
237      */
238     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
239     dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
240     SequenceI dna2 = new Sequence("AJ307031", "AAACCCTTT");
241
242     /*
243      * find UNIPROT xrefs for peptide sequence - it has no direct
244      * dbrefs, and the other sequence (which has a UNIPROT dbref) is not 
245      * equatable to it, so no results found
246      */
247     AlignmentI al = new Alignment(new SequenceI[] { dna1, dna2 });
248     Alignment xrefs = new CrossRef(new SequenceI[] { dna2 }, al)
249             .findXrefSequences("UNIPROT", true);
250     assertNull(xrefs);
251   }
252
253   /**
254    * Tests for the method that searches an alignment (with one sequence
255    * excluded) for protein/nucleotide sequences with a given cross-reference
256    */
257   @Test(groups = { "Functional" }, enabled = true)
258   public void testSearchDataset()
259   {
260     /*
261      * nucleotide sequence with UNIPROT AND EMBL dbref
262      * peptide sequence with UNIPROT dbref
263      */
264     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
265     Mapping map = new Mapping(new Sequence("pep2", "MLAVSRG"), new MapList(
266             new int[] { 1, 21 }, new int[] { 1, 7 }, 3, 1));
267     DBRefEntry dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2", map);
268     dna1.addDBRef(dbref);
269     dna1.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
270     SequenceI pep1 = new Sequence("Q9ZTS2", "MLAVSRGQ");
271     dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2");
272     pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
273     AlignmentI al = new Alignment(new SequenceI[] { dna1, pep1 });
274
275     List<SequenceI> result = new ArrayList<>();
276
277     /*
278      * first search for a dbref nowhere on the alignment:
279      */
280     dbref = new DBRefEntry("UNIPROT", "0", "P30419");
281     CrossRef testee = new CrossRef(al.getSequencesArray(), al);
282     AlignedCodonFrame acf = new AlignedCodonFrame();
283     boolean found = testee.searchDataset(true, dna1, dbref, result, acf,
284             true, DBRefUtils.SEARCH_MODE_FULL);
285     assertFalse(found);
286     assertTrue(result.isEmpty());
287     assertTrue(acf.isEmpty());
288
289     /*
290      * search for a protein sequence with dbref UNIPROT:Q9ZTS2
291      */
292     acf = new AlignedCodonFrame();
293     dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2");
294     found = testee.searchDataset(!dna1.isProtein(), dna1, dbref, result,
295             acf, false, DBRefUtils.SEARCH_MODE_FULL); // search dataset with a protein xref from a dna
296                          // sequence to locate the protein product
297     assertTrue(found);
298     assertEquals(1, result.size());
299     assertSame(pep1, result.get(0));
300     assertTrue(acf.isEmpty());
301
302     /*
303      * search for a nucleotide sequence with dbref UNIPROT:Q9ZTS2
304      */
305     result.clear();
306     acf = new AlignedCodonFrame();
307     dbref = new DBRefEntry("UNIPROT", "0", "Q9ZTS2");
308     found = testee.searchDataset(!pep1.isProtein(), pep1, dbref, result,
309             acf, false, DBRefUtils.SEARCH_MODE_FULL); // search dataset with a protein's direct dbref to
310                          // locate dna sequences with matching xref
311     assertTrue(found);
312     assertEquals(1, result.size());
313     assertSame(dna1, result.get(0));
314     // should now have a mapping from dna to pep1
315     List<SequenceToSequenceMapping> mappings = acf.getMappings();
316     assertEquals(1, mappings.size());
317     SequenceToSequenceMapping mapping = mappings.get(0);
318     assertSame(dna1, mapping.getFromSeq());
319     assertSame(pep1, mapping.getMapping().getTo());
320     MapList mapList = mapping.getMapping().getMap();
321     assertEquals(1, mapList.getToRatio());
322     assertEquals(3, mapList.getFromRatio());
323     assertEquals(1, mapList.getFromRanges().size());
324     assertEquals(1, mapList.getFromRanges().get(0)[0]);
325     assertEquals(21, mapList.getFromRanges().get(0)[1]);
326     assertEquals(1, mapList.getToRanges().size());
327     assertEquals(1, mapList.getToRanges().get(0)[0]);
328     assertEquals(7, mapList.getToRanges().get(0)[1]);
329   }
330
331   /**
332    * Test for finding 'product' sequences for the case where the selected
333    * sequence has a dbref with a mapping to a sequence. This represents the case
334    * where either
335    * <ul>
336    * <li>a fetched sequence is already decorated with its cross-reference (e.g.
337    * EMBL + translation), or</li>
338    * <li>Get Cross-References has been done once resulting in instantiated
339    * cross-reference mappings</li>
340    * </ul>
341    */
342   @Test(groups = { "Functional" })
343   public void testFindXrefSequences_fromDbRefMap()
344   {
345     /*
346      * scenario: nucleotide sequence AF039662
347      *   with dbref + mapping to Q9ZTS2 and P30419
348      *     which themselves each have a dbref and feature
349      */
350     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
351     SequenceI pep1 = new Sequence("Q9ZTS2", "MALFQRSV");
352     SequenceI pep2 = new Sequence("P30419", "MTRRSQIF");
353     dna1.createDatasetSequence();
354     pep1.createDatasetSequence();
355     pep2.createDatasetSequence();
356
357     pep1.getDatasetSequence().addDBRef(
358             new DBRefEntry("Pfam", "0", "PF00111"));
359     pep1.addSequenceFeature(new SequenceFeature("type", "desc", 12, 14, 1f,
360             "group"));
361     pep2.getDatasetSequence().addDBRef(new DBRefEntry("PDB", "0", "3JTK"));
362     pep2.addSequenceFeature(new SequenceFeature("type2", "desc2", 13, 15,
363             12f, "group2"));
364
365     MapList mapList = new MapList(new int[] { 1, 24 }, new int[] { 1, 3 },
366             3, 1);
367     Mapping map = new Mapping(pep1, mapList);
368     DBRefEntry dbRef1 = new DBRefEntry("UNIPROT", "0", "Q9ZTS2", map);
369     dna1.getDatasetSequence().addDBRef(dbRef1);
370     mapList = new MapList(new int[] { 1, 24 }, new int[] { 1, 3 }, 3, 1);
371     map = new Mapping(pep2, mapList);
372     DBRefEntry dbRef2 = new DBRefEntry("UNIPROT", "0", "P30419", map);
373     dna1.getDatasetSequence().addDBRef(dbRef2);
374
375     /*
376      * find UNIPROT xrefs for nucleotide sequence - it should pick up 
377      * mapped sequences
378      */
379     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
380     Alignment xrefs = new CrossRef(new SequenceI[] { dna1 }, al)
381             .findXrefSequences("UNIPROT", true);
382     assertEquals(2, xrefs.getHeight());
383
384     /*
385      * cross-refs alignment holds copies of the mapped sequences
386      * including copies of their dbrefs and features
387      */
388     checkCopySequence(pep1, xrefs.getSequenceAt(0));
389     checkCopySequence(pep2, xrefs.getSequenceAt(1));
390   }
391
392   /**
393    * Helper method that verifies that 'copy' has the same name, start, end,
394    * sequence and dataset sequence object as 'original' (but is not the same
395    * object)
396    * 
397    * @param copy
398    * @param original
399    */
400   private void checkCopySequence(SequenceI copy, SequenceI original)
401   {
402     assertNotSame(copy, original);
403     assertSame(copy.getDatasetSequence(), original.getDatasetSequence());
404     assertEquals(copy.getName(), original.getName());
405     assertEquals(copy.getStart(), original.getStart());
406     assertEquals(copy.getEnd(), original.getEnd());
407     assertEquals(copy.getSequenceAsString(), original.getSequenceAsString());
408   }
409
410   /**
411    * Test for finding 'product' sequences for the case where the selected
412    * sequence has a dbref with no mapping, triggering a fetch from database
413    */
414   @Test(groups = { "Functional_Failing" })
415   public void testFindXrefSequences_withFetch()
416   {
417     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
418     dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "Q9ZTS2"));
419     dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "P30419"));
420     dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "P00314"));
421     final SequenceI pep1 = new Sequence("Q9ZTS2", "MYQLIRSSW");
422     pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
423
424     final SequenceI pep2 = new Sequence("P00314", "MRKLLAASG");
425     pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "P00314"));
426
427     /*
428      * argument false suppresses adding DAS sources
429      * todo: define an interface type SequenceFetcherI and mock that
430      */
431     SequenceFetcher mockFetcher = new SequenceFetcher()
432     {
433       @Override
434       public boolean isFetchable(String source)
435       {
436         return true;
437       }
438
439       @Override
440       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
441       {
442         return new SequenceI[] { pep1, pep2 };
443       }
444     };
445     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
446
447     /*
448      * find UNIPROT xrefs for nucleotide sequence
449      */
450     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
451     Alignment xrefs = new CrossRef(new SequenceI[] { dna1 }, al)
452             .findXrefSequences("UNIPROT", true);
453     assertEquals(2, xrefs.getHeight());
454     assertSame(pep1, xrefs.getSequenceAt(0));
455     assertSame(pep2, xrefs.getSequenceAt(1));
456   }
457
458   @AfterClass(alwaysRun = true)
459   public void tearDown()
460   {
461     SequenceFetcherFactory.setSequenceFetcher(null);
462   }
463
464   /**
465    * Test for finding 'product' sequences for the case where both gene and
466    * transcript sequences have dbrefs to Uniprot.
467    */
468   @Test(groups = { "Functional_Failing" })
469   public void testFindXrefSequences_forGeneAndTranscripts()
470   {
471     /*
472      * 'gene' sequence
473      */
474     SequenceI gene = new Sequence("ENSG00000157764", "CGCCTCCCTTCCCC");
475     gene.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
476     gene.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
477
478     /*
479      * 'transcript' with CDS feature (supports mapping to protein)
480      */
481     SequenceI braf001 = new Sequence("ENST00000288602", "taagATGGCGGCGCTGa");
482     braf001.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
483     braf001.addSequenceFeature(new SequenceFeature("CDS", "", 5, 16, 0f,
484             null));
485
486     /*
487      * 'spliced transcript' with CDS ranges
488      */
489     SequenceI braf002 = new Sequence("ENST00000497784", "gCAGGCtaTCTGTTCaa");
490     braf002.addDBRef(new DBRefEntry("UNIPROT", "ENSEMBL|0", "H7C5K3"));
491     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 2, 6, 0f,
492             null));
493     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, 0f,
494             null));
495
496     /*
497      * TODO code is fragile - use of SequenceIdMatcher depends on fetched
498      * sequences having a name starting Source|Accession
499      * which happens to be true for Uniprot,PDB,EMBL but not Pfam,Rfam,Ensembl 
500      */
501     final SequenceI pep1 = new Sequence("UNIPROT|P15056", "MAAL");
502     pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
503     final SequenceI pep2 = new Sequence("UNIPROT|H7C5K3", "QALF");
504     pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
505     /*
506      * argument false suppresses adding DAS sources
507      * todo: define an interface type SequenceFetcherI and mock that
508      */
509     SequenceFetcher mockFetcher = new SequenceFetcher()
510     {
511       @Override
512       public boolean isFetchable(String source)
513       {
514         return true;
515       }
516
517       @Override
518       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
519       {
520         return new SequenceI[] { pep1, pep2 };
521       }
522     };
523     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
524
525     /*
526      * find UNIPROT xrefs for gene and transcripts
527      * verify that
528      * - the two proteins are retrieved but not duplicated
529      * - mappings are built from transcript (CDS) to proteins
530      * - no mappings from gene to proteins
531      */
532     SequenceI[] seqs = new SequenceI[] { gene, braf001, braf002 };
533     AlignmentI al = new Alignment(seqs);
534     Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("UNIPROT",
535             true);
536     assertEquals(2, xrefs.getHeight());
537     assertSame(pep1, xrefs.getSequenceAt(0));
538     assertSame(pep2, xrefs.getSequenceAt(1));
539   }
540
541   /**
542    * <pre>
543    * Test that emulates this (real but simplified) case:
544    * Alignment:          DBrefs
545    *     UNIPROT|P0CE19  EMBL|J03321, EMBL|X06707, EMBL|M19487
546    *     UNIPROT|P0CE20  EMBL|J03321, EMBL|X06707, EMBL|X07547
547    * Find cross-references for EMBL. These are mocked here as
548    *     EMBL|J03321     with mappings to P0CE18, P0CE19, P0CE20
549    *     EMBL|X06707     with mappings to P0CE17, P0CE19, P0CE20
550    *     EMBL|M19487     with mappings to P0CE19, Q46432
551    *     EMBL|X07547     with mappings to P0CE20, B0BCM4
552    * EMBL sequences are first 'fetched' (mocked here) for P0CE19.
553    * The 3 EMBL sequences are added to the alignment dataset.
554    * Their dbrefs to Uniprot products P0CE19 and P0CE20 should be matched in the
555    * alignment dataset and updated to reference the original Uniprot sequences.
556    * For the second Uniprot sequence, the J03321 and X06707 xrefs should be 
557    * resolved from the dataset, and only the X07547 dbref fetched.
558    * So the end state to verify is:
559    * - 4 cross-ref sequences returned: J03321, X06707,  M19487, X07547
560    * - P0CE19/20 dbrefs to EMBL sequences now have mappings
561    * - J03321 dbrefs to P0CE19/20 mapped to original Uniprot sequences
562    * - X06707 dbrefs to P0CE19/20 mapped to original Uniprot sequences
563    * </pre>
564    */
565   @Test(groups = { "Functional_Failing" })
566   public void testFindXrefSequences_uniprotEmblManyToMany()
567   {
568     /*
569      * Uniprot sequences, both with xrefs to EMBL|J03321 
570      * and EMBL|X07547
571      */
572     SequenceI p0ce19 = new Sequence("UNIPROT|P0CE19", "KPFG");
573     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
574     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
575     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "M19487"));
576     SequenceI p0ce20 = new Sequence("UNIPROT|P0CE20", "PFGK");
577     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
578     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
579     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X07547"));
580
581     /*
582      * EMBL sequences to be 'fetched', complete with dbrefs and mappings
583      * to their protein products (CDS location  and translations  are provided
584      * in  EMBL XML); these should be matched to, and replaced with,
585      * the corresponding uniprot sequences after fetching
586      */
587
588     /*
589      * J03321 with mappings to P0CE19 and P0CE20
590      */
591     final SequenceI j03321 = new Sequence("EMBL|J03321", "AAACCCTTTGGGAAAA");
592     DBRefEntry dbref1 = new DBRefEntry("UNIPROT", "0", "P0CE19");
593     MapList mapList = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 },
594             3, 1);
595     Mapping map = new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
596             mapList);
597     // add a dbref to the mapped to sequence - should get copied to p0ce19
598     map.getTo().addDBRef(new DBRefEntry("PIR", "0", "S01875"));
599     dbref1.setMap(map);
600     j03321.addDBRef(dbref1);
601     DBRefEntry dbref2 = new DBRefEntry("UNIPROT", "0", "P0CE20");
602     mapList = new MapList(new int[] { 4, 15 }, new int[] { 2, 5 }, 3, 1);
603     dbref2.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
604             new MapList(mapList)));
605     j03321.addDBRef(dbref2);
606
607     /*
608      * X06707 with mappings to P0CE19 and P0CE20
609      */
610     final SequenceI x06707 = new Sequence("EMBL|X06707", "atgAAACCCTTTGGG");
611     DBRefEntry dbref3 = new DBRefEntry("UNIPROT", "0", "P0CE19");
612     MapList map2 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
613             1);
614     dbref3.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"), map2));
615     x06707.addDBRef(dbref3);
616     DBRefEntry dbref4 = new DBRefEntry("UNIPROT", "0", "P0CE20");
617     MapList map3 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
618             1);
619     dbref4.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"), map3));
620     x06707.addDBRef(dbref4);
621
622     /*
623      * M19487 with mapping to P0CE19 and Q46432
624      */
625     final SequenceI m19487 = new Sequence("EMBL|M19487", "AAACCCTTTGGG");
626     DBRefEntry dbref5 = new DBRefEntry("UNIPROT", "0", "P0CE19");
627     dbref5.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
628             new MapList(mapList)));
629     m19487.addDBRef(dbref5);
630     DBRefEntry dbref6 = new DBRefEntry("UNIPROT", "0", "Q46432");
631     dbref6.setMap(new Mapping(new Sequence("UNIPROT|Q46432", "KPFG"),
632             new MapList(mapList)));
633     m19487.addDBRef(dbref6);
634
635     /*
636      * X07547 with mapping to P0CE20 and B0BCM4
637      */
638     final SequenceI x07547 = new Sequence("EMBL|X07547", "cccAAACCCTTTGGG");
639     DBRefEntry dbref7 = new DBRefEntry("UNIPROT", "0", "P0CE20");
640     dbref7.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
641             new MapList(map2)));
642     x07547.addDBRef(dbref7);
643     DBRefEntry dbref8 = new DBRefEntry("UNIPROT", "0", "B0BCM4");
644     dbref8.setMap(new Mapping(new Sequence("UNIPROT|B0BCM4", "KPFG"),
645             new MapList(map2)));
646     x07547.addDBRef(dbref8);
647
648     /*
649      * mock sequence fetcher to 'return' the EMBL sequences
650      * TODO: Mockito would allow .thenReturn().thenReturn() here, 
651      * and also capture and verification of the parameters
652      * passed in calls to getSequences() - important to verify that
653      * duplicate sequence fetches are not requested
654      */
655     SequenceFetcher mockFetcher = new SequenceFetcher()
656     {
657       int call = 0;
658
659       @Override
660       public boolean isFetchable(String source)
661       {
662         return true;
663       }
664
665       @Override
666       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
667       {
668         call++;
669         if (call == 1)
670         {
671           assertEquals("Expected 3 embl seqs in first fetch", 3,
672                   refs.size());
673           return new SequenceI[] { j03321, x06707, m19487 };
674         }
675         else
676         {
677           assertEquals("Expected 1 embl seq in second fetch", 1,
678                   refs.size());
679           return new SequenceI[] { x07547 };
680         }
681       }
682     };
683     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
684
685     /*
686      * find EMBL xrefs for Uniprot seqs and verify that
687      * - the EMBL xref'd sequences are retrieved without duplicates
688      * - mappings are added to the Uniprot dbrefs
689      * - mappings in the EMBL-to-Uniprot dbrefs are updated to the 
690      *   alignment sequences
691      * - dbrefs on the EMBL sequences are added to the original dbrefs
692      */
693     SequenceI[] seqs = new SequenceI[] { p0ce19, p0ce20 };
694     AlignmentI al = new Alignment(seqs);
695     Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("EMBL",
696             false);
697
698     /*
699      * verify retrieved sequences
700      */
701     assertNotNull(xrefs);
702     assertEquals(4, xrefs.getHeight());
703     assertSame(j03321, xrefs.getSequenceAt(0));
704     assertSame(x06707, xrefs.getSequenceAt(1));
705     assertSame(m19487, xrefs.getSequenceAt(2));
706     assertSame(x07547, xrefs.getSequenceAt(3));
707
708     /*
709      * verify mappings added to Uniprot-to-EMBL dbrefs
710      */
711     Mapping mapping = p0ce19.getDBRefs().get(0).getMap();
712     assertSame(j03321, mapping.getTo());
713     mapping = p0ce19.getDBRefs().get(1).getMap();
714     assertSame(x06707, mapping.getTo());
715     mapping = p0ce20.getDBRefs().get(0).getMap();
716     assertSame(j03321, mapping.getTo());
717     mapping = p0ce20.getDBRefs().get(1).getMap();
718     assertSame(x06707, mapping.getTo());
719
720     /*
721      * verify dbrefs on EMBL are mapped to alignment seqs
722      */
723     
724     assertSame(p0ce19, j03321.getDBRefs().get(0).getMap().getTo());
725     assertSame(p0ce20, j03321.getDBRefs().get(1).getMap().getTo());
726     assertSame(p0ce19, x06707.getDBRefs().get(0).getMap().getTo());
727     assertSame(p0ce20, x06707.getDBRefs().get(1).getMap().getTo());
728
729     /*
730      * verify new dbref on EMBL dbref mapping is copied to the
731      * original Uniprot sequence
732      */
733     assertEquals(4, p0ce19.getDBRefs().size());
734     assertEquals("PIR", p0ce19.getDBRefs().get(3).getSource());
735     assertEquals("S01875", p0ce19.getDBRefs().get(3).getAccessionId());
736   }
737
738   @Test(groups = "Functional")
739   public void testSameSequence()
740   {
741     assertTrue(CrossRef.sameSequence(null, null));
742     SequenceI seq1 = new Sequence("seq1", "ABCDEF");
743     assertFalse(CrossRef.sameSequence(seq1, null));
744     assertFalse(CrossRef.sameSequence(null, seq1));
745     assertTrue(CrossRef.sameSequence(seq1, new Sequence("seq2", "ABCDEF")));
746     assertTrue(CrossRef.sameSequence(seq1, new Sequence("seq2", "abcdef")));
747     assertFalse(CrossRef
748             .sameSequence(seq1, new Sequence("seq2", "ABCDE-F")));
749     assertFalse(CrossRef.sameSequence(seq1, new Sequence("seq2", "BCDEF")));
750   }
751 }