1 package org.broadinstitute.hellbender.tools.funcotator.dataSources;
2 
3 import htsjdk.variant.variantcontext.Allele;
4 import org.broadinstitute.hellbender.GATKBaseTest;
5 import org.broadinstitute.hellbender.exceptions.GATKException;
6 import org.broadinstitute.hellbender.tools.funcotator.FuncotatorUtils;
7 import org.testng.Assert;
8 import org.testng.annotations.DataProvider;
9 import org.testng.annotations.Test;
10 
11 import java.util.*;
12 import java.util.stream.Collectors;
13 
14 /**
15  * A Unit Test class for {@link TableFuncotation}
16  * Created by jonn on 11/28/17.
17  */
18 public class TableFuncotationUnitTest extends GATKBaseTest {
19 
20     //==================================================================================================================
21     // Private Static Members:
22 
23     //==================================================================================================================
24     // Private Members:
25 
26     //==================================================================================================================
27     // Helper Methods:
28 
29     //==================================================================================================================
30     // Data Providers:
31 
32     @DataProvider
provideForTestGetAltAllele()33     Object[][] provideForTestGetAltAllele() {
34         return new Object[][] {
35                 {
36                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("A", false), "TableFuncotationUnitTest", null),
37                         Allele.create("A", false),
38                         true
39                 },
40                 {
41                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("C", false), "TableFuncotationUnitTest", null),
42                         Allele.create("C", false),
43                         true
44                 },
45                 {
46                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("G", false), "TableFuncotationUnitTest", null),
47                         Allele.create("G", false),
48                         true
49                 },
50                 {
51                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("T", false), "TableFuncotationUnitTest", null),
52                         Allele.create("T", false),
53                         true
54                 },
55                 {
56                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("C", false), "TableFuncotationUnitTest", null),
57                         Allele.create("A", false),
58                         false
59                 },
60                 {
61                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("G", false), "TableFuncotationUnitTest", null),
62                         Allele.create("C", false),
63                         false
64                 },
65                 {
66                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("T", false), "TableFuncotationUnitTest", null),
67                         Allele.create("G", false),
68                         false
69                 },
70                 {
71                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("G", true), "TableFuncotationUnitTest", null),
72                         Allele.create("G", false),
73                         false
74                 },
75                 {
76                         TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("T", true), "TableFuncotationUnitTest", null),
77                         Allele.create("T", false),
78                         false
79                 },
80         };
81     }
82 
83     @DataProvider
provideForTestGet()84     Object[][] provideForTestGet() {
85         return new Object[][]{
86                 { "X" },
87                 { "A" },
88                 { "B" },
89                 { "Y" },
90                 { "L" },
91                 { "R" },
92                 { "Select" },
93                 { "Start" },
94         };
95     }
96 
97     @DataProvider
provideDataForTestSetFieldSerializationOverrideValue()98     Object[][] provideDataForTestSetFieldSerializationOverrideValue() {
99 
100         final TableFuncotation funcotation =
101                 TableFuncotation.create(
102                     Arrays.asList("A", "B", "C"), Arrays.asList("1", "2", "3"), Allele.create("A", false), "TableFuncotation", null
103                 );
104 
105         return new Object[][] {
106                 { funcotation, "A", null },
107                 { funcotation, "A", "A" },
108                 { funcotation, "A", "B" },
109                 { funcotation, "A", "C" },
110                 { funcotation, "B", "A" },
111                 { funcotation, "B", "B" },
112                 { funcotation, "B", "C" },
113                 { funcotation, "C", "A" },
114                 { funcotation, "C", "B" },
115                 { funcotation, "C", "C" },
116         };
117     }
118 
119     @DataProvider
provideForTestSerializeToVcfString()120     Object[][] provideForTestSerializeToVcfString() {
121         return new Object[][] {
122                 {
123                     TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("A", false), "Empty", null),
124                     ""
125                 },
126                 {
127                     TableFuncotation.create(Collections.singletonList("A"), Collections.singletonList(("1")), Allele.create("A", false), "OneVal", null),
128                     "1"
129                 },
130                 {
131                     TableFuncotation.create(Arrays.asList("A", "B"), Arrays.asList("1", "2"), Allele.create("A", false), "TwoVals", null),
132                     "1|2"
133                 },
134                 {
135                     TableFuncotation.create(Arrays.asList("A", "B", "C"), Arrays.asList("1", "2", "3"), Allele.create("A", false), "ThreeVals", null),
136                     "1|2|3"
137                 },
138         };
139     }
140 
141     @DataProvider
provideListOfStrings()142     Object[][] provideListOfStrings() {
143         return new Object[][] {
144                 { Collections.singletonList("A") },
145                 { Arrays.asList("A", "B") },
146                 { Arrays.asList("A", "B", "C") },
147                 { Arrays.asList("A", "B", "C", "D") },
148                 { Arrays.asList("A", "B", "C", "D", "E") },
149         };
150     }
151 
152     @DataProvider
provideForTestGetFieldNames()153     Object[][] provideForTestGetFieldNames() {
154         //final TableFuncotation tableFuncotation, final LinkedHashSet<String> expected
155         return new Object[][] {
156                 {
157                     TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("A", false), "Empty", null),
158                     new LinkedHashSet<>(Collections.emptyList())
159                 },
160                 {
161                     TableFuncotation.create(Collections.singletonList("TESTFIELD"), Collections.singletonList("TESTVAL"), Allele.create("A", false), "OneField", null),
162                     new LinkedHashSet<>(Collections.singletonList("TESTFIELD"))
163                 },
164                 {
165                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2"), Arrays.asList("TESTVAL1", "TESTVAL2"), Allele.create("A", false), "TwoFields", null),
166                     new LinkedHashSet<>(Arrays.asList("TESTFIELD1", "TESTFIELD2"))
167                 },
168         };
169     }
170 
171     @DataProvider
provideForTestGetField()172     Object[][] provideForTestGetField() {
173         //final TableFuncotation tableFuncotation, final String fieldName, final String expected
174         return new Object[][] {
175                 {
176                     TableFuncotation.create(Collections.singletonList("TESTFIELD"), Collections.singletonList("TESTVAL"), Allele.create("A", false), "OneField", null),
177                     "TESTFIELD",
178                     "TESTVAL"
179                 },
180                 {
181                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2"), Arrays.asList("TESTVAL1", "TESTVAL2"), Allele.create("A", false), "TwoFields", null),
182                     "TESTFIELD1",
183                     "TESTVAL1"
184                 },
185                 {
186                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2"), Arrays.asList("TESTVAL1", "TESTVAL2"), Allele.create("A", false), "TwoFields", null),
187                     "TESTFIELD2",
188                     "TESTVAL2"
189                 },
190                 {
191                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2", "TESTFIELD3"), Arrays.asList("TESTVAL1", "TESTVAL2", "TESTVAL3"), Allele.create("A", false), "ThreeFields", null),
192                     "TESTFIELD1",
193                     "TESTVAL1"
194                 },
195                 {
196                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2", "TESTFIELD3"), Arrays.asList("TESTVAL1", "TESTVAL2", "TESTVAL3"), Allele.create("A", false), "ThreeFields", null),
197                     "TESTFIELD2",
198                     "TESTVAL2"
199                 },
200                 {
201                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2", "TESTFIELD3"), Arrays.asList("TESTVAL1", "TESTVAL2", "TESTVAL3"), Allele.create("A", false), "ThreeFields", null),
202                     "TESTFIELD3",
203                     "TESTVAL3"
204                 },
205         };
206     }
207 
208     @DataProvider
provideForTestGetFieldFail()209     Object[][] provideForTestGetFieldFail() {
210         //final TableFuncotation tableFuncotation, final String fieldName, final String expected
211         return new Object[][] {
212                 {
213                     TableFuncotation.create(Collections.emptyList(), Collections.emptyList(), Allele.create("A", false), "Empty", null),
214                     "TESTFIELD_OMICRON"
215                 },
216                 {
217                     TableFuncotation.create(Collections.singletonList("TESTFIELD"), Collections.singletonList("TESTVAL"), Allele.create("A", false), "OneField", null),
218                     "testfield"
219                 },
220                 {
221                     TableFuncotation.create(Collections.singletonList("TESTFIELD"), Collections.singletonList("TESTVAL"), Allele.create("A", false), "OneField", null),
222                     "table_TESTFIELD"
223                 },
224                 {
225                     TableFuncotation.create(Arrays.asList("TESTFIELD1", "TESTFIELD2", "TESTFIELD3"), Arrays.asList("TESTVAL1", "TESTVAL2", "TESTVAL3"), Allele.create("A", false), "ThreeFields", null),
226                     "TESTFIELD4"
227                 },
228         };
229     }
230 
231     //==================================================================================================================
232     // Tests:
233 
234     @Test(dataProvider = "provideForTestGetAltAllele")
testGetAltAllele( final TableFuncotation funcotation, final Allele altAllele, final boolean expected)235     public void testGetAltAllele( final TableFuncotation funcotation, final Allele altAllele, final boolean expected) {
236         Assert.assertEquals( funcotation.getAltAllele().equals(altAllele), expected );
237     }
238 
239     @Test(dataProvider = "provideForTestGet")
testGet(final String fieldValue)240     public void testGet(final String fieldValue) {
241 
242         final String fieldName = "PLACEHOLDER";
243 
244         final TableFuncotation funcotation = TableFuncotation.create( Collections.singletonList(fieldName), Collections.singletonList(fieldValue), Allele.create("A", false), fieldName , null);
245         Assert.assertEquals( funcotation.get(fieldName), fieldValue );
246     }
247 
248     @Test(dataProvider = "provideDataForTestSetFieldSerializationOverrideValue")
testSetFieldSerializationOverrideValue(final TableFuncotation funcotation, final String fieldName, String overrideValue)249     public void testSetFieldSerializationOverrideValue(final TableFuncotation funcotation,
250                                                        final String fieldName,
251                                                        String overrideValue) {
252         if ( overrideValue == null ) {
253             overrideValue = funcotation.get(fieldName);
254         }
255         else {
256             funcotation.setFieldSerializationOverrideValue(fieldName, overrideValue);
257         }
258         Assert.assertEquals( funcotation.get(fieldName), overrideValue );
259     }
260 
261     @Test(dataProvider = "provideForTestSerializeToVcfString")
testSerializeToVcfString(final TableFuncotation funcotation, final String expected)262     public void testSerializeToVcfString(final TableFuncotation funcotation,
263                                          final String expected) {
264         Assert.assertEquals(FuncotatorUtils.renderSanitizedFuncotationForVcf(funcotation, new ArrayList<>(funcotation.getFieldNames())), expected );
265     }
266 
267     @Test(dataProvider = "provideListOfStrings")
testKeySet(final List<String> kvNames)268     public void testKeySet(final List<String> kvNames) {
269         final TableFuncotation funcotation = TableFuncotation.create(kvNames, kvNames.stream().map(s -> s + "VVV").collect(Collectors.toList()), Allele.create("A", false), "ListFuncotation", null);
270         Assert.assertEquals( funcotation.keySet(), kvNames);
271     }
272 
273     @Test(dataProvider = "provideListOfStrings")
testValues(final List<String> kvNames)274     public void testValues(final List<String> kvNames) {
275         final TableFuncotation funcotation = TableFuncotation.create(kvNames.stream().map(s -> s + "KEY").collect(Collectors.toList()), kvNames, Allele.create("A", false), "ListFuncotation", null);
276         Assert.assertEquals( funcotation.values(), kvNames);
277     }
278 
279     @Test(dataProvider = "provideForTestGetFieldNames")
testGetFieldNames(final TableFuncotation tableFuncotation, final LinkedHashSet<String> expected)280     public void testGetFieldNames(final TableFuncotation tableFuncotation, final LinkedHashSet<String> expected) {
281         Assert.assertEquals(tableFuncotation.getFieldNames(), expected);
282     }
283 
284     @Test(dataProvider = "provideForTestGetField")
testGetField(final TableFuncotation tableFuncotation, final String fieldName, final String expected)285     public void testGetField(final TableFuncotation tableFuncotation, final String fieldName, final String expected) {
286         Assert.assertEquals(tableFuncotation.getField(fieldName), expected);
287     }
288 
289     @Test(dataProvider = "provideForTestGetFieldFail", expectedExceptions = GATKException.class)
testGetFieldFail(final TableFuncotation tableFuncotation, final String fieldName)290     public void testGetFieldFail(final TableFuncotation tableFuncotation, final String fieldName) {
291         tableFuncotation.getField(fieldName);
292     }
293 
294 }
295