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