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