24ddb34ce9dd79428ff5e36e815f18bffde10e60
[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     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", "0", "Q9ZTS2"));
406     dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P30419"));
407     dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P00314"));
408     final SequenceI pep1 = new Sequence("Q9ZTS2", "MYQLIRSSW");
409     final SequenceI pep2 = new Sequence("P00314", "MRKLLAASG");
410
411     /*
412      * argument false suppresses adding DAS sources
413      * todo: define an interface type SequenceFetcherI and mock that
414      */
415     SequenceFetcher mockFetcher = new SequenceFetcher(false)
416     {
417       @Override
418       public boolean isFetchable(String source)
419       {
420         return true;
421       }
422
423       @Override
424       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
425       {
426         return new SequenceI[] { pep1, pep2 };
427       }
428     };
429     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
430
431     /*
432      * find UNIPROT xrefs for nucleotide sequence
433      */
434     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
435     Alignment xrefs = new CrossRef(new SequenceI[] { dna1 }, al)
436             .findXrefSequences("UNIPROT", true);
437     assertEquals(2, xrefs.getHeight());
438     assertSame(pep1, xrefs.getSequenceAt(0));
439     assertSame(pep2, xrefs.getSequenceAt(1));
440   }
441
442   @AfterClass
443   public void tearDown()
444   {
445     SequenceFetcherFactory.setSequenceFetcher(null);
446   }
447
448   /**
449    * Test for finding 'product' sequences for the case where both gene and
450    * transcript sequences have dbrefs to Uniprot.
451    */
452   @Test(groups = { "Functional" })
453   public void testFindXrefSequences_forGeneAndTranscripts()
454   {
455     /*
456      * 'gene' sequence
457      */
458     SequenceI gene = new Sequence("ENSG00000157764", "CGCCTCCCTTCCCC");
459     gene.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
460     gene.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
461
462     /*
463      * 'transcript' with CDS feature (supports mapping to protein)
464      */
465     SequenceI braf001 = new Sequence("ENST00000288602", "taagATGGCGGCGCTGa");
466     braf001.addDBRef(new DBRefEntry("UNIPROT", "0", "P15056"));
467     braf001.addSequenceFeature(new SequenceFeature("CDS", "", 5, 16, 0f,
468             null));
469
470     /*
471      * 'spliced transcript' with CDS ranges
472      */
473     SequenceI braf002 = new Sequence("ENST00000497784", "gCAGGCtaTCTGTTCaa");
474     braf002.addDBRef(new DBRefEntry("UNIPROT", "0", "H7C5K3"));
475     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 2, 6, 0f,
476             null));
477     braf002.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, 0f,
478             null));
479
480     /*
481      * TODO code is fragile - use of SequenceIdMatcher depends on fetched
482      * sequences having a name starting Source|Accession
483      * which happens to be true for Uniprot,PDB,EMBL but not Pfam,Rfam,Ensembl 
484      */
485     final SequenceI pep1 = new Sequence("UNIPROT|P15056", "MAAL");
486     final SequenceI pep2 = new Sequence("UNIPROT|H7C5K3", "QALF");
487
488     /*
489      * argument false suppresses adding DAS sources
490      * todo: define an interface type SequenceFetcherI and mock that
491      */
492     SequenceFetcher mockFetcher = new SequenceFetcher(false)
493     {
494       @Override
495       public boolean isFetchable(String source)
496       {
497         return true;
498       }
499
500       @Override
501       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
502       {
503         return new SequenceI[] { pep1, pep2 };
504       }
505     };
506     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
507
508     /*
509      * find UNIPROT xrefs for gene and transcripts
510      * verify that
511      * - the two proteins are retrieved but not duplicated
512      * - mappings are built from transcript (CDS) to proteins
513      * - no mappings from gene to proteins
514      */
515     SequenceI[] seqs = new SequenceI[] { gene, braf001, braf002 };
516     AlignmentI al = new Alignment(seqs);
517     Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("UNIPROT",
518             true);
519     assertEquals(2, xrefs.getHeight());
520     assertSame(pep1, xrefs.getSequenceAt(0));
521     assertSame(pep2, xrefs.getSequenceAt(1));
522   }
523
524   /**
525    * <pre>
526    * Test that emulates this (real but simplified) case:
527    * Alignment:          DBrefs
528    *     UNIPROT|P0CE19  EMBL|J03321, EMBL|X06707, EMBL|M19487
529    *     UNIPROT|P0CE20  EMBL|J03321, EMBL|X06707, EMBL|X07547
530    * Find cross-references for EMBL. These are mocked here as
531    *     EMBL|J03321     with mappings to P0CE18, P0CE19, P0CE20
532    *     EMBL|X06707     with mappings to P0CE17, P0CE19, P0CE20
533    *     EMBL|M19487     with mappings to P0CE19, Q46432
534    *     EMBL|X07547     with mappings to P0CE20, B0BCM4
535    * EMBL sequences are first 'fetched' (mocked here) for P0CE19.
536    * The 3 EMBL sequences are added to the alignment dataset.
537    * Their dbrefs to Uniprot products P0CE19 and P0CE20 should be matched in the
538    * alignment dataset and updated to reference the original Uniprot sequences.
539    * For the second Uniprot sequence, the J03321 and X06707 xrefs should be 
540    * resolved from the dataset, and only the X07547 dbref fetched.
541    * So the end state to verify is:
542    * - 4 cross-ref sequences returned: J03321, X06707,  M19487, X07547
543    * - P0CE19/20 dbrefs to EMBL sequences now have mappings
544    * - J03321 dbrefs to P0CE19/20 mapped to original Uniprot sequences
545    * - X06707 dbrefs to P0CE19/20 mapped to original Uniprot sequences
546    * </pre>
547    */
548   @Test(groups = { "Functional" })
549   public void testFindXrefSequences_uniprotEmblManyToMany()
550   {
551     /*
552      * Uniprot sequences, both with xrefs to EMBL|J03321 
553      * and EMBL|X07547
554      */
555     SequenceI p0ce19 = new Sequence("UNIPROT|P0CE19", "KPFG");
556     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
557     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
558     p0ce19.addDBRef(new DBRefEntry("EMBL", "0", "M19487"));
559     SequenceI p0ce20 = new Sequence("UNIPROT|P0CE20", "PFGK");
560     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "J03321"));
561     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X06707"));
562     p0ce20.addDBRef(new DBRefEntry("EMBL", "0", "X07547"));
563
564     /*
565      * EMBL sequences to be 'fetched', complete with dbrefs and mappings
566      * to their protein products (CDS location  and translations  are provided
567      * in  EMBL XML); these should be matched to, and replaced with,
568      * the corresponding uniprot sequences after fetching
569      */
570
571     /*
572      * J03321 with mappings to P0CE19 and P0CE20
573      */
574     final SequenceI j03321 = new Sequence("EMBL|J03321", "AAACCCTTTGGGAAAA");
575     DBRefEntry dbref1 = new DBRefEntry("UNIPROT", "0", "P0CE19");
576     MapList mapList = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 },
577             3, 1);
578     Mapping map = new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
579             mapList);
580     // add a dbref to the mapped to sequence - should get copied to p0ce19
581     map.getTo().addDBRef(new DBRefEntry("PIR", "0", "S01875"));
582     dbref1.setMap(map);
583     j03321.addDBRef(dbref1);
584     DBRefEntry dbref2 = new DBRefEntry("UNIPROT", "0", "P0CE20");
585     mapList = new MapList(new int[] { 4, 15 }, new int[] { 2, 5 }, 3, 1);
586     dbref2.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"),
587             new MapList(mapList)));
588     j03321.addDBRef(dbref2);
589
590     /*
591      * X06707 with mappings to P0CE19 and P0CE20
592      */
593     final SequenceI x06707 = new Sequence("EMBL|X06707", "atgAAACCCTTTGGG");
594     DBRefEntry dbref3 = new DBRefEntry("UNIPROT", "0", "P0CE19");
595     MapList map2 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
596             1);
597     dbref3.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"), map2));
598     x06707.addDBRef(dbref3);
599     DBRefEntry dbref4 = new DBRefEntry("UNIPROT", "0", "P0CE20");
600     MapList map3 = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 }, 3,
601             1);
602     dbref4.setMap(new Mapping(new Sequence("UNIPROT|P0CE20", "PFGK"), map3));
603     x06707.addDBRef(dbref4);
604
605     /*
606      * M19487 with mapping to P0CE19 and Q46432
607      */
608     final SequenceI m19487 = new Sequence("EMBL|M19487", "AAACCCTTTGGG");
609     DBRefEntry dbref5 = new DBRefEntry("UNIPROT", "0", "P0CE19");
610     dbref5.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
611             new MapList(mapList)));
612     m19487.addDBRef(dbref5);
613     DBRefEntry dbref6 = new DBRefEntry("UNIPROT", "0", "Q46432");
614     dbref6.setMap(new Mapping(new Sequence("UNIPROT|Q46432", "KPFG"),
615             new MapList(mapList)));
616     m19487.addDBRef(dbref6);
617
618     /*
619      * X07547 with mapping to P0CE20 and B0BCM4
620      */
621     final SequenceI x07547 = new Sequence("EMBL|X07547", "cccAAACCCTTTGGG");
622     DBRefEntry dbref7 = new DBRefEntry("UNIPROT", "0", "P0CE20");
623     dbref7.setMap(new Mapping(new Sequence("UNIPROT|P0CE19", "KPFG"),
624             new MapList(map2)));
625     x07547.addDBRef(dbref7);
626     DBRefEntry dbref8 = new DBRefEntry("UNIPROT", "0", "B0BCM4");
627     dbref8.setMap(new Mapping(new Sequence("UNIPROT|B0BCM4", "KPFG"),
628             new MapList(map2)));
629     x07547.addDBRef(dbref8);
630
631     /*
632      * mock sequence fetcher to 'return' the EMBL sequences
633      * TODO: Mockito would allow .thenReturn().thenReturn() here, 
634      * and also capture and verification of the parameters
635      * passed in calls to getSequences() - important to verify that
636      * duplicate sequence fetches are not requested
637      */
638     SequenceFetcher mockFetcher = new SequenceFetcher(false)
639     {
640       int call = 0;
641
642       @Override
643       public boolean isFetchable(String source)
644       {
645         return true;
646       }
647
648       @Override
649       public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
650       {
651         call++;
652         if (call == 1)
653         {
654           assertEquals("Expected 3 embl seqs in first fetch", 3,
655                   refs.size());
656           return new SequenceI[] { j03321, x06707, m19487 };
657         }
658         else
659         {
660           assertEquals("Expected 1 embl seq in second fetch", 1,
661                   refs.size());
662           return new SequenceI[] { x07547 };
663         }
664       }
665     };
666     SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
667
668     /*
669      * find EMBL xrefs for Uniprot seqs and verify that
670      * - the EMBL xref'd sequences are retrieved without duplicates
671      * - mappings are added to the Uniprot dbrefs
672      * - mappings in the EMBL-to-Uniprot dbrefs are updated to the 
673      *   alignment sequences
674      * - dbrefs on the EMBL sequences are added to the original dbrefs
675      */
676     SequenceI[] seqs = new SequenceI[] { p0ce19, p0ce20 };
677     AlignmentI al = new Alignment(seqs);
678     Alignment xrefs = new CrossRef(seqs, al).findXrefSequences("EMBL",
679             false);
680
681     /*
682      * verify retrieved sequences
683      */
684     assertNotNull(xrefs);
685     assertEquals(4, xrefs.getHeight());
686     assertSame(j03321, xrefs.getSequenceAt(0));
687     assertSame(x06707, xrefs.getSequenceAt(1));
688     assertSame(m19487, xrefs.getSequenceAt(2));
689     assertSame(x07547, xrefs.getSequenceAt(3));
690
691     /*
692      * verify mappings added to Uniprot-to-EMBL dbrefs
693      */
694     Mapping mapping = p0ce19.getDBRefs()[0].getMap();
695     assertSame(j03321, mapping.getTo());
696     mapping = p0ce19.getDBRefs()[1].getMap();
697     assertSame(x06707, mapping.getTo());
698     mapping = p0ce20.getDBRefs()[0].getMap();
699     assertSame(j03321, mapping.getTo());
700     mapping = p0ce20.getDBRefs()[1].getMap();
701     assertSame(x06707, mapping.getTo());
702
703     /*
704      * verify dbrefs on EMBL are mapped to alignment seqs
705      */
706     assertSame(p0ce19, j03321.getDBRefs()[0].getMap().getTo());
707     assertSame(p0ce20, j03321.getDBRefs()[1].getMap().getTo());
708     assertSame(p0ce19, x06707.getDBRefs()[0].getMap().getTo());
709     assertSame(p0ce20, x06707.getDBRefs()[1].getMap().getTo());
710
711     /*
712      * verify new dbref on EMBL dbref mapping is copied to the
713      * original Uniprot sequence
714      */
715     assertEquals(4, p0ce19.getDBRefs().length);
716     assertEquals("PIR", p0ce19.getDBRefs()[3].getSource());
717     assertEquals("S01875", p0ce19.getDBRefs()[3].getAccessionId());
718   }
719
720   @Test(groups = "Functional")
721   public void testSameSequence()
722   {
723     assertTrue(CrossRef.sameSequence(null, null));
724     SequenceI seq1 = new Sequence("seq1", "ABCDEF");
725     assertFalse(CrossRef.sameSequence(seq1, null));
726     assertFalse(CrossRef.sameSequence(null, seq1));
727     assertTrue(CrossRef.sameSequence(seq1, new Sequence("seq2", "ABCDEF")));
728     assertTrue(CrossRef.sameSequence(seq1, new Sequence("seq2", "abcdef")));
729     assertFalse(CrossRef
730             .sameSequence(seq1, new Sequence("seq2", "ABCDE-F")));
731     assertFalse(CrossRef.sameSequence(seq1, new Sequence("seq2", "BCDEF")));
732   }
733 }