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