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