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