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