Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.analysis

File CrossRefTest.java

 

Code metrics

2
276
21
1
776
446
22
0.08
13.14
21
1.05

Classes

Class Line # Actions
CrossRefTest 55 276 22
0.5752508657.5%
 

Contributing tests

This file is covered by 8 tests. .

Source view

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