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