JAL-2110 fixes and tests for synthesized EMBLCDSPROTEIN dbrefs
[jalview.git] / test / jalview / datamodel / xdb / embl / EmblEntryTest.java
1 package jalview.datamodel.xdb.embl;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertSame;
5
6 import jalview.analysis.SequenceIdMatcher;
7 import jalview.datamodel.DBRefEntry;
8 import jalview.datamodel.Sequence;
9 import jalview.datamodel.SequenceI;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.testng.annotations.Test;
16
17 public class EmblEntryTest
18 {
19   @Test(groups = "Functional")
20   public void testGetCdsRanges()
21   {
22     EmblEntry testee = new EmblEntry();
23
24     /*
25      * Make a (CDS) Feature with 4 locations
26      */
27     EmblFeature cds = new EmblFeature();
28     cds.setLocation("join(10..20,complement(30..40),50..60,70..80,complement(110..120))");
29
30     int[] exons = testee.getCdsRanges(cds);
31     assertEquals("[10, 20, 40, 30, 50, 60, 70, 80, 120, 110]",
32             Arrays.toString(exons));
33   }
34
35   @Test(groups = "Functional")
36   public void testParseCodingFeature()
37   {
38     // not the whole sequence but enough for this test...
39     SequenceI dna = new Sequence("J03321", "GGATCCGTAAGTTAGACGAAATT");
40     List<SequenceI> peptides = new ArrayList<SequenceI>();
41     SequenceIdMatcher matcher = new SequenceIdMatcher(peptides);
42     EmblFile ef = EmblTestHelper.getEmblFile();
43
44     /*
45      * parse three CDS features, with two/one/no Uniprot cross-refs
46      */
47     EmblEntry testee = new EmblEntry();
48     for (EmblFeature feature : ef.getEntries().get(0).getFeatures())
49     {
50       if ("CDS".equals(feature.getName()))
51       {
52         testee.parseCodingFeature(feature, "EMBL", dna, peptides, matcher);
53       }
54     }
55
56     /*
57      * peptides should now have five entries:
58      * EMBL product and two Uniprot accessions for the first CDS / translation
59      * EMBL product and one Uniprot accession for the second CDS / "
60      * EMBL product and synthesized EMBLCDSPROTEINaccession for the third
61      */
62     assertEquals(6, peptides.size());
63     assertEquals("CAA30420.1", peptides.get(0).getName());
64     assertEquals("MLCF", peptides.get(0).getSequenceAsString());
65     assertEquals("UNIPROT|B0BCM4", peptides.get(1).getName());
66     assertEquals("MLCF", peptides.get(1).getSequenceAsString());
67     assertEquals("UNIPROT|P0CE20", peptides.get(2).getName());
68     assertEquals("MLCF", peptides.get(2).getSequenceAsString());
69     assertEquals("CAA30421.1", peptides.get(3).getName());
70     assertEquals("MSSS", peptides.get(3).getSequenceAsString());
71     assertEquals("UNIPROT|B0BCM3", peptides.get(4).getName());
72     assertEquals("MSSS", peptides.get(4).getSequenceAsString());
73     assertEquals("CAA12345.6", peptides.get(5).getName());
74     assertEquals("MSS", peptides.get(5).getSequenceAsString());
75
76     /*
77      * verify dna sequence has dbrefs with mappings to the peptide 'products'
78      */
79     DBRefEntry[] dbrefs = dna.getDBRefs();
80     assertEquals(4, dbrefs.length);
81     DBRefEntry dbRefEntry = dbrefs[0];
82     assertEquals("UNIPROT", dbRefEntry.getSource());
83     assertEquals("B0BCM4", dbRefEntry.getAccessionId());
84     assertSame(peptides.get(1), dbRefEntry.getMap().getTo());
85     List<int[]> fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
86     assertEquals(1, fromRanges.size());
87     assertEquals(57, fromRanges.get(0)[0]);
88     assertEquals(46, fromRanges.get(0)[1]);
89     List<int[]> toRanges = dbRefEntry.getMap().getMap().getToRanges();
90     assertEquals(1, toRanges.size());
91     assertEquals(1, toRanges.get(0)[0]);
92     assertEquals(4, toRanges.get(0)[1]);
93
94     dbRefEntry = dbrefs[1];
95     assertEquals("UNIPROT", dbRefEntry.getSource());
96     assertEquals("P0CE20", dbRefEntry.getAccessionId());
97     assertSame(peptides.get(2), dbRefEntry.getMap().getTo());
98     fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
99     assertEquals(1, fromRanges.size());
100     assertEquals(57, fromRanges.get(0)[0]);
101     assertEquals(46, fromRanges.get(0)[1]);
102     toRanges = dbRefEntry.getMap().getMap().getToRanges();
103     assertEquals(1, toRanges.size());
104     assertEquals(1, toRanges.get(0)[0]);
105     assertEquals(4, toRanges.get(0)[1]);
106
107     dbRefEntry = dbrefs[2];
108     assertEquals("UNIPROT", dbRefEntry.getSource());
109     assertEquals("B0BCM3", dbRefEntry.getAccessionId());
110     assertSame(peptides.get(4), dbRefEntry.getMap().getTo());
111     fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
112     assertEquals(1, fromRanges.size());
113     assertEquals(4, fromRanges.get(0)[0]);
114     assertEquals(15, fromRanges.get(0)[1]);
115     toRanges = dbRefEntry.getMap().getMap().getToRanges();
116     assertEquals(1, toRanges.size());
117     assertEquals(1, toRanges.get(0)[0]);
118     assertEquals(4, toRanges.get(0)[1]);
119
120     dbRefEntry = dbrefs[3];
121     assertEquals("EMBLCDSPROTEIN", dbRefEntry.getSource());
122     assertEquals("CAA12345.6", dbRefEntry.getAccessionId());
123     assertSame(peptides.get(5), dbRefEntry.getMap().getTo());
124     fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
125     assertEquals(2, fromRanges.size());
126     assertEquals(4, fromRanges.get(0)[0]);
127     assertEquals(6, fromRanges.get(0)[1]);
128     assertEquals(10, fromRanges.get(1)[0]);
129     assertEquals(15, fromRanges.get(1)[1]);
130     toRanges = dbRefEntry.getMap().getMap().getToRanges();
131     assertEquals(1, toRanges.size());
132     assertEquals(1, toRanges.get(0)[0]);
133     assertEquals(3, toRanges.get(0)[1]);
134   }
135
136   @Test(groups = "Functional")
137   public void testAdjustForProteinLength()
138   {
139     int[] exons = new int[] { 11, 15, 21, 25, 31, 38 }; // 18 bp
140
141     // exact length match:
142     assertSame(exons, EmblEntry.adjustForProteinLength(6, exons));
143
144     // match if we assume exons include stop codon not in protein:
145     assertSame(exons, EmblEntry.adjustForProteinLength(5, exons));
146
147     // truncate last exon by 6bp
148     int[] truncated = EmblEntry.adjustForProteinLength(4, exons);
149     assertEquals("[11, 15, 21, 25, 31, 32]",
150             Arrays.toString(truncated));
151
152     // remove last exon and truncate preceding by 1bp
153     truncated = EmblEntry.adjustForProteinLength(3, exons);
154     assertEquals("[11, 15, 21, 24]", Arrays.toString(truncated));
155
156     // exact removal of exon case:
157     exons = new int[] { 11, 15, 21, 27, 33, 38 }; // 18 bp
158     truncated = EmblEntry.adjustForProteinLength(4, exons);
159     assertEquals("[11, 15, 21, 27]", Arrays.toString(truncated));
160
161     // what if exons are too short for protein?
162     truncated = EmblEntry.adjustForProteinLength(7, exons);
163     assertSame(exons, truncated);
164     // assertEquals("[11, 15, 21, 27]", Arrays.toString(truncated));
165   }
166 }