2ca112ff94e84ecb08ee83e371785f6cd1cc027a
[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     SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
419     dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "Q9ZTS2"));
420     dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "P30419"));
421     dna1.addDBRef(new DBRefEntry("UNIPROT", "ENA:0", "P00314"));
422     final SequenceI pep1 = new Sequence("Q9ZTS2", "MYQLIRSSW");
423     pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
424
425     final SequenceI pep2 = new Sequence("P00314", "MRKLLAASG");
426     pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "P00314"));
427
428     /*
429      * argument false suppresses adding DAS sources
430      * todo: define an interface type SequenceFetcherI and mock that
431      */
432     SequenceFetcher mockFetcher = new SequenceFetcher()
433     {
434       @Override
435       public boolean isFetchable(String source)
436       {
437         return true;
438       }
439
440       @Override
441       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
442       {
443         return new SequenceI[] { pep1, pep2 };
444       }
445     };
446     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
447
448     /*
449      * find UNIPROT xrefs for nucleotide sequence
450      */
451     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
452     Alignment xrefs = new CrossRef(new SequenceI[] { dna1 }, al)
453             .findXrefSequences("UNIPROT", true);
454     assertEquals(2, xrefs.getHeight());
455     assertSame(pep1, xrefs.getSequenceAt(0));
456     assertSame(pep2, xrefs.getSequenceAt(1));
457   }
458
459   @AfterClass(alwaysRun = true)
460   public void tearDown()
461   {
462     SequenceFetcherFactory.setSequenceFetcher(null);
463   }
464
465   /**
466    * Test for finding 'product' sequences for the case where both gene and
467    * transcript sequences have dbrefs to Uniprot.
468    */
469   @Test(groups = { "Functional_Failing" })
470   public void testFindXrefSequences_forGeneAndTranscripts()
471   {
472     /*
473      * 'gene' sequence
474      */
475     SequenceI gene = new Sequence("ENSG00000157764", "CGCCTCCCTTCCCC");
476     gene.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
477     gene.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
478
479     /*
480      * 'transcript' with CDS feature (supports mapping to protein)
481      */
482     SequenceI braf001 = new Sequence("ENST00000288602", "taagATGGCGGCGCTGa");
483     braf001.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
484     braf001.addSequenceFeature(new SequenceFeature("CDS", "", 5, 16, 0f,
485             null));
486
487     /*
488      * 'spliced transcript' with CDS ranges
489      */
490     SequenceI braf002 = new Sequence("ENST00000497784", "gCAGGCtaTCTGTTCaa");
491     braf002.addDBRef(new DBRefEntry("UNIPROT", "ENSEMBL|0", "H7C5K3"));
492     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 2, 6, 0f,
493             null));
494     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, 0f,
495             null));
496
497     /*
498      * TODO code is fragile - use of SequenceIdMatcher depends on fetched
499      * sequences having a name starting Source|Accession
500      * which happens to be true for Uniprot,PDB,EMBL but not Pfam,Rfam,Ensembl 
501      */
502     final SequenceI pep1 = new Sequence("UNIPROT|P15056", "MAAL");
503     pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
504     final SequenceI pep2 = new Sequence("UNIPROT|H7C5K3", "QALF");
505     pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
506     /*
507      * argument false suppresses adding DAS sources
508      * todo: define an interface type SequenceFetcherI and mock that
509      */
510     SequenceFetcher mockFetcher = new SequenceFetcher()
511     {
512       @Override
513       public boolean isFetchable(String source)
514       {
515         return true;
516       }
517
518       @Override
519       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
520       {
521         return new SequenceI[] { pep1, pep2 };
522       }
523     };
524     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
525
526     /*
527      * find UNIPROT xrefs for gene and transcripts
528      * verify that
529      * - the two proteins are retrieved but not duplicated
530      * - mappings are built from transcript (CDS) to proteins
531      * - no mappings from gene to proteins
532      */
533     SequenceI[] seqs = new SequenceI[] { gene, braf001, braf002 };
534     AlignmentI al = new Alignment(seqs);
535     Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("UNIPROT",
536             true);
537     assertEquals(2, xrefs.getHeight());
538     assertSame(pep1, xrefs.getSequenceAt(0));
539     assertSame(pep2, xrefs.getSequenceAt(1));
540   }
541
542   /**
543    * <pre>
544    * Test that emulates this (real but simplified) case:
545    * Alignment:          DBrefs
546    *     UNIPROT|P0CE19  EMBL|J03321, EMBL|X06707, EMBL|M19487
547    *     UNIPROT|P0CE20  EMBL|J03321, EMBL|X06707, EMBL|X07547
548    * Find cross-references for EMBL. These are mocked here as
549    *     EMBL|J03321     with mappings to P0CE18, P0CE19, P0CE20
550    *     EMBL|X06707     with mappings to P0CE17, P0CE19, P0CE20
551    *     EMBL|M19487     with mappings to P0CE19, Q46432
552    *     EMBL|X07547     with mappings to P0CE20, B0BCM4
553    * EMBL sequences are first 'fetched' (mocked here) for P0CE19.
554    * The 3 EMBL sequences are added to the alignment dataset.
555    * Their dbrefs to Uniprot products P0CE19 and P0CE20 should be matched in the
556    * alignment dataset and updated to reference the original Uniprot sequences.
557    * For the second Uniprot sequence, the J03321 and X06707 xrefs should be 
558    * resolved from the dataset, and only the X07547 dbref fetched.
559    * So the end state to verify is:
560    * - 4 cross-ref sequences returned: J03321, X06707,  M19487, X07547
561    * - P0CE19/20 dbrefs to EMBL sequences now have mappings
562    * - J03321 dbrefs to P0CE19/20 mapped to original Uniprot sequences
563    * - X06707 dbrefs to P0CE19/20 mapped to original Uniprot sequences
564    * </pre>
565    */
566   @Test(groups = { "Functional_Failing" })
567   public void testFindXrefSequences_uniprotEmblManyToMany()
568   {
569     /*
570      * Uniprot sequences, both with xrefs to EMBL|J03321 
571      * and EMBL|X07547
572      */
573     SequenceI p0ce19 = new Sequence("UNIPROT|P0CE19", "KPFG");
574     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
575     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
576     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "M19487"));
577     SequenceI p0ce20 = new Sequence("UNIPROT|P0CE20", "PFGK");
578     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
579     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
580     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X07547"));
581
582     /*
583      * EMBL sequences to be 'fetched', complete with dbrefs and mappings
584      * to their protein products (CDS location  and translations  are provided
585      * in  EMBL XML); these should be matched to, and replaced with,
586      * the corresponding uniprot sequences after fetching
587      */
588
589     /*
590      * J03321 with mappings to P0CE19 and P0CE20
591      */
592     final SequenceI j03321 = new Sequence("EMBL|J03321", "AAACCCTTTGGGAAAA");
593     DBRefEntry dbref1 = new DBRefEntry("UNIPROT", "0", "P0CE19");
594     MapList mapList = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 },
595             3, 1);
596     Mapping map = new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
597             mapList);
598     // add a dbref to the mapped to sequence - should get copied to p0ce19
599     map.getTo().addDBRef(new DBRefEntry("PIR", "0", "S01875"));
600     dbref1.setMap(map);
601     j03321.addDBRef(dbref1);
602     DBRefEntry dbref2 = new DBRefEntry("UNIPROT", "0", "P0CE20");
603     mapList = new MapList(new int[] { 4, 15 }, new int[] { 2, 5 }, 3, 1);
604     dbref2.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
605             new MapList(mapList)));
606     j03321.addDBRef(dbref2);
607
608     /*
609      * X06707 with mappings to P0CE19 and P0CE20
610      */
611     final SequenceI x06707 = new Sequence("EMBL|X06707", "atgAAACCCTTTGGG");
612     DBRefEntry dbref3 = new DBRefEntry("UNIPROT", "0", "P0CE19");
613     MapList map2 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
614             1);
615     dbref3.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"), map2));
616     x06707.addDBRef(dbref3);
617     DBRefEntry dbref4 = new DBRefEntry("UNIPROT", "0", "P0CE20");
618     MapList map3 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
619             1);
620     dbref4.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"), map3));
621     x06707.addDBRef(dbref4);
622
623     /*
624      * M19487 with mapping to P0CE19 and Q46432
625      */
626     final SequenceI m19487 = new Sequence("EMBL|M19487", "AAACCCTTTGGG");
627     DBRefEntry dbref5 = new DBRefEntry("UNIPROT", "0", "P0CE19");
628     dbref5.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
629             new MapList(mapList)));
630     m19487.addDBRef(dbref5);
631     DBRefEntry dbref6 = new DBRefEntry("UNIPROT", "0", "Q46432");
632     dbref6.setMap(new Mapping(new Sequence("UNIPROT|Q46432", "KPFG"),
633             new MapList(mapList)));
634     m19487.addDBRef(dbref6);
635
636     /*
637      * X07547 with mapping to P0CE20 and B0BCM4
638      */
639     final SequenceI x07547 = new Sequence("EMBL|X07547", "cccAAACCCTTTGGG");
640     DBRefEntry dbref7 = new DBRefEntry("UNIPROT", "0", "P0CE20");
641     dbref7.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
642             new MapList(map2)));
643     x07547.addDBRef(dbref7);
644     DBRefEntry dbref8 = new DBRefEntry("UNIPROT", "0", "B0BCM4");
645     dbref8.setMap(new Mapping(new Sequence("UNIPROT|B0BCM4", "KPFG"),
646             new MapList(map2)));
647     x07547.addDBRef(dbref8);
648
649     /*
650      * mock sequence fetcher to 'return' the EMBL sequences
651      * TODO: Mockito would allow .thenReturn().thenReturn() here, 
652      * and also capture and verification of the parameters
653      * passed in calls to getSequences() - important to verify that
654      * duplicate sequence fetches are not requested
655      */
656     SequenceFetcher mockFetcher = new SequenceFetcher()
657     {
658       int call = 0;
659
660       @Override
661       public boolean isFetchable(String source)
662       {
663         return true;
664       }
665
666       @Override
667       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
668       {
669         call++;
670         if (call == 1)
671         {
672           assertEquals("Expected 3 embl seqs in first fetch", 3,
673                   refs.size());
674           return new SequenceI[] { j03321, x06707, m19487 };
675         }
676         else
677         {
678           assertEquals("Expected 1 embl seq in second fetch", 1,
679                   refs.size());
680           return new SequenceI[] { x07547 };
681         }
682       }
683     };
684     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
685
686     /*
687      * find EMBL xrefs for Uniprot seqs and verify that
688      * - the EMBL xref'd sequences are retrieved without duplicates
689      * - mappings are added to the Uniprot dbrefs
690      * - mappings in the EMBL-to-Uniprot dbrefs are updated to the 
691      *   alignment sequences
692      * - dbrefs on the EMBL sequences are added to the original dbrefs
693      */
694     SequenceI[] seqs = new SequenceI[] { p0ce19, p0ce20 };
695     AlignmentI al = new Alignment(seqs);
696     Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("EMBL",
697             false);
698
699     /*
700      * verify retrieved sequences
701      */
702     assertNotNull(xrefs);
703     assertEquals(4, xrefs.getHeight());
704     assertSame(j03321, xrefs.getSequenceAt(0));
705     assertSame(x06707, xrefs.getSequenceAt(1));
706     assertSame(m19487, xrefs.getSequenceAt(2));
707     assertSame(x07547, xrefs.getSequenceAt(3));
708
709     /*
710      * verify mappings added to Uniprot-to-EMBL dbrefs
711      */
712     Mapping mapping = p0ce19.getDBRefs().get(0).getMap();
713     assertSame(j03321, mapping.getTo());
714     mapping = p0ce19.getDBRefs().get(1).getMap();
715     assertSame(x06707, mapping.getTo());
716     mapping = p0ce20.getDBRefs().get(0).getMap();
717     assertSame(j03321, mapping.getTo());
718     mapping = p0ce20.getDBRefs().get(1).getMap();
719     assertSame(x06707, mapping.getTo());
720
721     /*
722      * verify dbrefs on EMBL are mapped to alignment seqs
723      */
724     
725     assertSame(p0ce19, j03321.getDBRefs().get(0).getMap().getTo());
726     assertSame(p0ce20, j03321.getDBRefs().get(1).getMap().getTo());
727     assertSame(p0ce19, x06707.getDBRefs().get(0).getMap().getTo());
728     assertSame(p0ce20, x06707.getDBRefs().get(1).getMap().getTo());
729
730     /*
731      * verify new dbref on EMBL dbref mapping is copied to the
732      * original Uniprot sequence
733      */
734     assertEquals(4, p0ce19.getDBRefs().size());
735     assertEquals("PIR", p0ce19.getDBRefs().get(3).getSource());
736     assertEquals("S01875", p0ce19.getDBRefs().get(3).getAccessionId());
737   }
738
739   @Test(groups = "Functional")
740   public void testSameSequence()
741   {
742     assertTrue(CrossRef.sameSequence(null, null));
743     SequenceI seq1 = new Sequence("seq1", "ABCDEF");
744     assertFalse(CrossRef.sameSequence(seq1, null));
745     assertFalse(CrossRef.sameSequence(null, seq1));
746     assertTrue(CrossRef.sameSequence(seq1, new Sequence("seq2", "ABCDEF")));
747     assertTrue(CrossRef.sameSequence(seq1, new Sequence("seq2", "abcdef")));
748     assertFalse(CrossRef
749             .sameSequence(seq1, new Sequence("seq2", "ABCDE-F")));
750     assertFalse(CrossRef.sameSequence(seq1, new Sequence("seq2", "BCDEF")));
751   }
752 }