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