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