1 package jalview.analysis;
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertNotSame;
5 import static org.testng.AssertJUnit.assertNull;
6 import static org.testng.AssertJUnit.assertSame;
7 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.DBRefEntry;
12 import jalview.datamodel.Mapping;
13 import jalview.datamodel.Sequence;
14 import jalview.datamodel.SequenceFeature;
15 import jalview.datamodel.SequenceI;
16 import jalview.util.MapList;
17 import jalview.ws.SequenceFetcher;
18 import jalview.ws.SequenceFetcherFactory;
20 import java.util.List;
22 import org.testng.annotations.Test;
24 public class CrossRefsTest
28 * Test for finding 'product' sequences for the case where the selected
29 * sequence has a dbref with a mapping to a sequence
31 @Test(groups = { "Functional" })
32 public void testFindXrefSequences_fromDbRefMap()
35 * two peptide sequences each with a DBRef and SequenceFeature
37 SequenceI pep1 = new Sequence("Q9ZTS2", "MALFQRSV");
38 pep1.addDBRef(new DBRefEntry("Pfam", "0", "PF00111"));
39 pep1.addSequenceFeature(new SequenceFeature("type", "desc", 12, 14, 1f,
41 SequenceI pep2 = new Sequence("P30419", "MTRRSQIF");
42 pep2.addDBRef(new DBRefEntry("PDB", "0", "3JTK"));
43 pep2.addSequenceFeature(new SequenceFeature("type2", "desc2", 13, 15,
47 * nucleotide sequence (to go in the alignment)
49 SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
52 * add DBRefEntry's to dna1 with mappings from dna to both peptides
54 MapList mapList = new MapList(new int[] { 1, 24 }, new int[] { 1, 3 },
56 Mapping map = new Mapping(pep1, mapList);
57 DBRefEntry dbRef1 = new DBRefEntry("UNIPROT", "0", "Q9ZTS2", map);
58 dna1.addDBRef(dbRef1);
59 mapList = new MapList(new int[] { 1, 24 }, new int[] { 1, 3 }, 3, 1);
60 map = new Mapping(pep2, mapList);
61 DBRefEntry dbRef2 = new DBRefEntry("UNIPROT", "0", "P30419", map);
62 dna1.addDBRef(dbRef2);
65 * find UNIPROT xrefs for nucleotide sequence - it should pick up
68 AlignmentI al = new Alignment(new SequenceI[] { dna1 });
69 AlignmentI xrefs = CrossRefs.findXrefSequences(
70 new SequenceI[] { dna1 },
72 assertEquals(2, xrefs.getHeight());
75 * cross-refs alignment holds copies of the mapped sequences
76 * including copies of their dbrefs and features
78 checkCopySequence(pep1, xrefs.getSequenceAt(0));
79 checkCopySequence(pep2, xrefs.getSequenceAt(1));
83 * Test for finding 'product' sequences for the case where only an indirect
84 * xref is found - not on the peptide sequence but on a nucleotide sequence in
85 * the alignment which which it shares a protein dbref
87 @Test(groups = { "Functional" })
88 public void testFindXrefSequences_indirectDbrefToNucleotide()
92 * - peptide dbref UNIPROT|Q9ZTS2
93 * - nucleotide dbref EMBL|AF039662, UNIPROT|Q9ZTS2
95 SequenceI uniprotSeq = new Sequence("Q9ZTS2", "MASVSATMISTS");
96 uniprotSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
97 SequenceI emblSeq = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
98 emblSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
99 emblSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
102 * Find EMBL xrefs for peptide
103 * - it has no EMBL dbref of its own
104 * - but nucleotide with matching peptide dbref does, so is returned
106 AlignmentI al = new Alignment(new SequenceI[] { emblSeq, uniprotSeq });
107 AlignmentI xrefs = CrossRefs.findXrefSequences(
108 new SequenceI[] { uniprotSeq }, false, "EMBL", al);
109 assertEquals(1, xrefs.getHeight());
110 assertSame(emblSeq, xrefs.getSequenceAt(0));
114 * Test for finding 'product' sequences for the case where only an indirect
115 * xref is found - not on the nucleotide sequence but on a peptide sequence in
116 * the alignment which which it shares a nucleotide dbref
118 @Test(groups = { "Functional" })
119 public void testFindXrefSequences_indirectDbrefToProtein()
123 * - nucleotide dbref EMBL|AF039662
124 * - peptide dbrefs EMBL|AF039662, UNIPROT|Q9ZTS2
126 SequenceI emblSeq = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
127 emblSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
128 SequenceI uniprotSeq = new Sequence("Q9ZTS2", "MASVSATMISTS");
129 uniprotSeq.addDBRef(new DBRefEntry("EMBL", "0", "AF039662"));
130 uniprotSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
133 * Find UNIPROT xrefs for nucleotide
134 * - it has no UNIPROT dbref of its own
135 * - but peptide with matching nucleotide dbref does, so is returned
137 AlignmentI al = new Alignment(new SequenceI[] { emblSeq, uniprotSeq });
138 AlignmentI xrefs = CrossRefs.findXrefSequences(
139 new SequenceI[] { emblSeq }, true, "UNIPROT", al);
140 assertEquals(1, xrefs.getHeight());
141 assertSame(uniprotSeq, xrefs.getSequenceAt(0));
145 * Test for finding 'product' sequences for the case where the selected
146 * sequence has no dbref to the desired source, and there are no indirect
147 * references via another sequence in the alignment
149 @Test(groups = { "Functional" })
150 public void testFindXrefSequences_noDbrefs()
153 * two nucleotide sequences, one with UNIPROT dbref
155 SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
156 dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
157 SequenceI dna2 = new Sequence("AJ307031", "AAACCCTTT");
160 * find UNIPROT xrefs for peptide sequence - it has no direct
161 * dbrefs, and the other sequence (which has a UNIPROT dbref) is not
162 * equatable to it, so no results found
164 AlignmentI al = new Alignment(new SequenceI[] { dna1, dna2 });
165 AlignmentI xrefs = CrossRefs.findXrefSequences(
166 new SequenceI[] { dna2 },
167 true, "UNIPROT", al);
172 * Test for finding 'product' sequences for the case where the selected
173 * sequence has a dbref with no mapping, triggering a fetch from database
175 @Test(groups = { "Functional" })
176 public void testFindXrefSequences_withFetch()
178 SequenceI dna1 = new Sequence("AF039662", "GGGGCAGCACAAGAAC");
179 dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "Q9ZTS2"));
180 dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P30419"));
181 dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P00314"));
182 final SequenceI pep1 = new Sequence("Q9ZTS2", "MYQLIRSSW");
183 final SequenceI pep2 = new Sequence("P00314", "MRKLLAASG");
185 SequenceFetcher mockFetcher = new SequenceFetcher()
189 public boolean isFetchable(String source)
195 public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
197 return new SequenceI[] { pep1, pep2 };
200 SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
203 * find UNIPROT xrefs for nucleotide sequence
205 AlignmentI al = new Alignment(new SequenceI[] { dna1 });
206 AlignmentI xrefs = CrossRefs.findXrefSequences(
207 new SequenceI[] { dna1 },
208 true, "UNIPROT", al);
209 assertEquals(2, xrefs.getHeight());
210 assertSame(pep1, xrefs.getSequenceAt(0));
211 assertSame(pep2, xrefs.getSequenceAt(1));
215 * Helper method to assert seq1 looks like a copy of seq2
220 private void checkCopySequence(SequenceI seq1, SequenceI seq2)
222 assertNotSame(seq1, seq2);
223 assertEquals(seq1.getName(), seq2.getName());
224 assertEquals(seq1.getStart(), seq2.getStart());
225 assertEquals(seq1.getEnd(), seq2.getEnd());
226 assertEquals(seq1.getSequenceAsString(), seq2.getSequenceAsString());
231 assertArrayEquals(seq1.getDBRefs(), seq2.getDBRefs());
232 // check one to verify a copy, not the same object
233 if (seq1.getDBRefs().length > 0)
235 assertNotSame(seq1.getDBRefs()[0], seq2.getDBRefs()[0]);
241 assertArrayEquals(seq1.getSequenceFeatures(),
242 seq2.getSequenceFeatures());
243 if (seq1.getSequenceFeatures().length > 0)
245 assertNotSame(seq1.getSequenceFeatures()[0],
246 seq2.getSequenceFeatures()[0]);
251 * Test for finding 'product' sequences for the case where the selected
252 * sequence has two dbrefs with no mapping, triggering a fetch from database.
254 * @see http://issues.jalview.org/browse/JAL-2029
256 @Test(groups = { "Functional" })
257 public void testFindXrefSequences_withFetchMultipleRefs()
262 SequenceI dna1 = new Sequence("X07547", "GGGGCAGCACAAGAAC");
263 dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "B0BCM4"));
264 dna1.addDBRef(new DBRefEntry("UNIPROT", "0", "P0CE20"));
265 final SequenceI pep1 = new Sequence("B0BCM4", "MGKGIL");
266 final SequenceI pep2 = new Sequence("P0CE20", "MGKGIL");
268 SequenceFetcher mockFetcher = new SequenceFetcher()
273 public boolean isFetchable(String source)
278 public SequenceI[] getSequences(List<DBRefEntry> refs, boolean dna)
280 // pending Mockito with its thenReturn(pep1).thenReturn(pep2) syntax!
281 return new SequenceI[] { call++ == 0 ? pep1 : pep2 };
284 SequenceFetcherFactory.setSequenceFetcher(mockFetcher);
287 * find UNIPROT xrefs for nucleotide sequence
289 AlignmentI al = new Alignment(new SequenceI[] { dna1 });
290 AlignmentI xrefs = CrossRefs.findXrefSequences(
291 new SequenceI[] { dna1 },
292 true, "UNIPROT", al);
293 assertEquals(2, xrefs.getHeight());
294 assertSame(pep1, xrefs.getSequenceAt(0));
295 assertSame(pep2, xrefs.getSequenceAt(1));