Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / test / jalview / io / vcf / VCFLoaderTest.java
index 808fe86..2b418de 100644 (file)
@@ -1,11 +1,32 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.io.vcf;
 
 import static jalview.io.gff.SequenceOntologyI.SEQUENCE_VARIANT;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
-import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
 
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.Mapping;
@@ -13,13 +34,11 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.features.FeatureAttributes;
-import jalview.datamodel.features.FeatureAttributes.Datatype;
 import jalview.datamodel.features.SequenceFeatures;
 import jalview.gui.AlignFrame;
 import jalview.io.DataSourceType;
 import jalview.io.FileLoader;
 import jalview.io.gff.Gff3Helper;
-import jalview.io.gff.SequenceOntologyI;
 import jalview.util.MapList;
 
 import java.io.File;
@@ -37,11 +56,10 @@ public class VCFLoaderTest
   private static final float DELTA = 0.00001f;
 
   // columns 9717- of gene P30419 from Ensembl (much modified)
-  private static final String FASTA = ""
-          +
-          /*
-           * forward strand 'gene' and 'transcript' with two exons
-           */
+  private static final String FASTA = "" +
+  /*
+   * forward strand 'gene' and 'transcript' with two exons
+   */
           ">gene1/1-25 chromosome:GRCh38:17:45051610:45051634:1\n"
           + "CAAGCTGGCGGACGAGAGTGTGACA\n"
           + ">transcript1/1-18\n--AGCTGGCG----AGAGTGTGAC-\n"
@@ -50,8 +68,8 @@ public class VCFLoaderTest
            * reverse strand gene and transcript (reverse complement alleles!)
            */
           + ">gene2/1-25 chromosome:GRCh38:17:45051610:45051634:-1\n"
-          + "TGTCACACTCTCGTCCGCCAGCTTG\n"
-          + ">transcript2/1-18\n" + "-GTCACACTCT----CGCCAGCT--\n"
+          + "TGTCACACTCTCGTCCGCCAGCTTG\n" + ">transcript2/1-18\n"
+          + "-GTCACACTCT----CGCCAGCT--\n"
 
           /*
            * 'gene' on chromosome 5 with two transcripts
@@ -62,7 +80,8 @@ public class VCFLoaderTest
           + ">transcript4/1-18\n-----TGG-GGACGAGAGTGTGA-A\n";
 
   private static final String[] VCF = { "##fileformat=VCFv4.2",
-      // fields other than AF are ignored when parsing as they have no INFO definition
+      // fields other than AF are ignored when parsing as they have no INFO
+      // definition
       "##INFO=<ID=AF,Number=A,Type=Float,Description=\"Allele Frequency, for each ALT allele, in the same order as listed\">",
       "##INFO=<ID=AC_Female,Number=A,Type=Integer,Description=\"Allele count in Female genotypes\"",
       "##INFO=<ID=AF_AFR,Number=A,Type=Float,Description=\"Allele Frequency among African/African American genotypes\"",
@@ -71,10 +90,10 @@ public class VCFLoaderTest
       // A/T,C variants in position 2 of gene sequence (precedes transcript)
       // should create 2 variant features with respective AF values
       // malformed values for AC_Female and AF_AFR should be ignored
-      "17\t45051611\t.\tA\tT,C\t1666.64\tRF\tAC=15;AF=5.0e-03,4.0e-03;AC_Female=12,3d;AF_AFR=low,2.3e-4",
+      "17\t45051611\trs384765\tA\tT,C\t1666.64\tRF;XYZ\tAC=15;AF=5.0e-03,4.0e-03;AC_Female=12,3d;AF_AFR=low,2.3e-4",
       // SNP G/C in position 4 of gene sequence, position 2 of transcript
       // insertion G/GA is transferred to nucleotide but not to peptide
-      "17\t45051613\t.\tG\tGA,C\t1666.65\tRF\tAC=15;AF=3.0e-03,2.0e-03",
+      "17\t45051613\t.\tG\tGA,C\t1666.65\t.\tAC=15;AF=3.0e-03,2.0e-03",
       // '.' in INFO field should be ignored
       "17\t45051615\t.\tG\tC\t1666.66\tRF\tAC=16;AF=." };
 
@@ -88,7 +107,7 @@ public class VCFLoaderTest
     Cache.setProperty("VCF_FIELDS", ".*");
     Cache.setProperty("VEP_FIELDS", ".*");
     Cache.setProperty("VCF_ASSEMBLY", "GRCh38=GRCh38");
-    Cache.initLogger();
+    Console.initLogger();
   }
 
   @BeforeTest(alwaysRun = true)
@@ -130,6 +149,10 @@ public class VCFLoaderTest
     assertEquals(sf.getValue("AF_AFR"), "2.3e-4");
     assertEquals(sf.getValue(Gff3Helper.ALLELES), "A,C");
     assertEquals(sf.getType(), SEQUENCE_VARIANT);
+    assertEquals(sf.getValue("POS"), "45051611");
+    assertEquals(sf.getValue("ID"), "rs384765");
+    assertEquals(sf.getValue("QUAL"), "1666.64");
+    assertEquals(sf.getValue("FILTER"), "RF;XYZ");
     // malformed integer for AC_Female is ignored (JAL-3375)
     assertNull(sf.getValue("AC_Female"));
 
@@ -165,6 +188,8 @@ public class VCFLoaderTest
     assertEquals(Float.parseFloat((String) sf.getValue("AF")), 3.0e-03,
             DELTA);
     assertEquals(sf.getValue(Gff3Helper.ALLELES), "G,GA");
+    assertNull(sf.getValue("ID")); // '.' is ignored
+    assertNull(sf.getValue("FILTER")); // '.' is ignored
 
     sf = geneFeatures.get(4);
     assertEquals(sf.getFeatureGroup(), "VCF");
@@ -205,7 +230,7 @@ public class VCFLoaderTest
      * verify SNP variant feature(s) computed and added to protein
      * first codon AGC varies to ACC giving S/T
      */
-    DBRefEntry[] dbRefs = al.getSequenceAt(1).getDBRefs();
+    List<DBRefEntry> dbRefs = al.getSequenceAt(1).getDBRefs();
     SequenceI peptide = null;
     for (DBRefEntry dbref : dbRefs)
     {
@@ -215,24 +240,11 @@ public class VCFLoaderTest
       }
     }
     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
-    assertEquals(proteinFeatures.size(), 3);
-    sf = proteinFeatures.get(0);
-    assertEquals(sf.getFeatureGroup(), "VCF");
-    assertEquals(sf.getBegin(), 1);
-    assertEquals(sf.getEnd(), 1);
-    assertEquals(sf.getType(), SequenceOntologyI.NONSYNONYMOUS_VARIANT);
-    assertEquals(sf.getDescription(), "p.Ser1Thr");
 
     /*
-     * check that sequence_variant attribute AF has been clocked as
-     * numeric with correct min and max values
-     * (i.e. invalid values have been ignored - JAL-3375)
+     * JAL-3187 don't precompute protein features, do dynamically instead
      */
-    FeatureAttributes fa = FeatureAttributes.getInstance();
-    assertSame(fa.getDatatype(SEQUENCE_VARIANT, "AF"), Datatype.Number);
-    float[] minmax = fa.getMinMax(SEQUENCE_VARIANT, "AF");
-    assertEquals(minmax[0], 0.002f);
-    assertEquals(minmax[1], 0.005f);
+    assertTrue(proteinFeatures.isEmpty());
   }
 
   private File makeVcfFile() throws IOException
@@ -266,8 +278,8 @@ public class VCFLoaderTest
     SequenceI gene1 = alignment.findName("gene1");
     int[] to = new int[] { 45051610, 45051634 };
     int[] from = new int[] { gene1.getStart(), gene1.getEnd() };
-    gene1.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(from, to,
-            1, 1));
+    gene1.setGeneLoci("homo_sapiens", "GRCh38", "17",
+            new MapList(from, to, 1, 1));
 
     /*
      * map 'transcript1' to chromosome via 'gene1'
@@ -277,9 +289,8 @@ public class VCFLoaderTest
     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
     SequenceI transcript1 = alignment.findName("transcript1");
     from = new int[] { transcript1.getStart(), transcript1.getEnd() };
-    transcript1.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(
-            from, to,
-            1, 1));
+    transcript1.setGeneLoci("homo_sapiens", "GRCh38", "17",
+            new MapList(from, to, 1, 1));
 
     /*
      * map gene2 to chromosome reverse strand
@@ -287,8 +298,8 @@ public class VCFLoaderTest
     SequenceI gene2 = alignment.findName("gene2");
     to = new int[] { 45051634, 45051610 };
     from = new int[] { gene2.getStart(), gene2.getEnd() };
-    gene2.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(from, to,
-            1, 1));
+    gene2.setGeneLoci("homo_sapiens", "GRCh38", "17",
+            new MapList(from, to, 1, 1));
 
     /*
      * map 'transcript2' to chromosome via 'gene2'
@@ -298,9 +309,8 @@ public class VCFLoaderTest
     to = new int[] { 45051633, 45051624, 45051619, 45051612 };
     SequenceI transcript2 = alignment.findName("transcript2");
     from = new int[] { transcript2.getStart(), transcript2.getEnd() };
-    transcript2.setGeneLoci("homo_sapiens", "GRCh38", "17", new MapList(
-            from, to,
-            1, 1));
+    transcript2.setGeneLoci("homo_sapiens", "GRCh38", "17",
+            new MapList(from, to, 1, 1));
 
     /*
      * add a protein product as a DBRef on transcript1
@@ -327,8 +337,8 @@ public class VCFLoaderTest
     SequenceI gene3 = alignment.findName("gene3");
     to = new int[] { 45051610, 45051634 };
     from = new int[] { gene3.getStart(), gene3.getEnd() };
-    gene3.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(from, to,
-            1, 1));
+    gene3.setGeneLoci("homo_sapiens", "GRCh38", "5",
+            new MapList(from, to, 1, 1));
 
     /*
      * map 'transcript3' to chromosome
@@ -336,9 +346,8 @@ public class VCFLoaderTest
     SequenceI transcript3 = alignment.findName("transcript3");
     to = new int[] { 45051612, 45051619, 45051624, 45051633 };
     from = new int[] { transcript3.getStart(), transcript3.getEnd() };
-    transcript3.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(
-            from, to,
-            1, 1));
+    transcript3.setGeneLoci("homo_sapiens", "GRCh38", "5",
+            new MapList(from, to, 1, 1));
 
     /*
      * map 'transcript4' to chromosome
@@ -347,9 +356,8 @@ public class VCFLoaderTest
     to = new int[] { 45051615, 45051617, 45051619, 45051632, 45051634,
         45051634 };
     from = new int[] { transcript4.getStart(), transcript4.getEnd() };
-    transcript4.setGeneLoci("homo_sapiens", "GRCh38", "5", new MapList(
-            from, to,
-            1, 1));
+    transcript4.setGeneLoci("homo_sapiens", "GRCh38", "5",
+            new MapList(from, to, 1, 1));
 
     /*
      * add a protein product as a DBRef on transcript3
@@ -484,7 +492,7 @@ public class VCFLoaderTest
      * verify variant feature(s) computed and added to protein
      * last codon GCT varies to GGT giving A/G in the last peptide position
      */
-    DBRefEntry[] dbRefs = al.getSequenceAt(3).getDBRefs();
+    List<DBRefEntry> dbRefs = al.getSequenceAt(3).getDBRefs();
     SequenceI peptide = null;
     for (DBRefEntry dbref : dbRefs)
     {
@@ -494,13 +502,11 @@ public class VCFLoaderTest
       }
     }
     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
-    assertEquals(proteinFeatures.size(), 3);
-    sf = proteinFeatures.get(0);
-    assertEquals(sf.getFeatureGroup(), "VCF");
-    assertEquals(sf.getBegin(), 6);
-    assertEquals(sf.getEnd(), 6);
-    assertEquals(sf.getType(), SequenceOntologyI.NONSYNONYMOUS_VARIANT);
-    assertEquals(sf.getDescription(), "p.Ala6Gly");
+
+    /*
+     * JAL-3187 don't precompute protein features, do dynamically instead
+     */
+    assertTrue(proteinFeatures.isEmpty());
   }
 
   /**
@@ -543,6 +549,7 @@ public class VCFLoaderTest
     // gene features include Consequence for all transcripts
     Map map = (Map) sf.getValue("CSQ");
     assertEquals(map.size(), 9);
+    assertEquals(map.get("PolyPhen"), "Bad");
 
     sf = geneFeatures.get(1);
     assertEquals(sf.getBegin(), 5);
@@ -552,6 +559,7 @@ public class VCFLoaderTest
     assertEquals(sf.getValue("alleles"), "C,T");
     map = (Map) sf.getValue("CSQ");
     assertEquals(map.size(), 9);
+    assertEquals(map.get("PolyPhen"), "Bad;;"); // %3B%3B decoded
 
     sf = geneFeatures.get(2);
     assertEquals(sf.getBegin(), 9);
@@ -644,7 +652,7 @@ public class VCFLoaderTest
      * and GAG/GGG which is E/G in position 4
      * the insertion variant is not transferred to the peptide
      */
-    DBRefEntry[] dbRefs = al.findName("transcript3").getDBRefs();
+    List<DBRefEntry> dbRefs = al.findName("transcript3").getDBRefs();
     SequenceI peptide = null;
     for (DBRefEntry dbref : dbRefs)
     {
@@ -654,20 +662,24 @@ public class VCFLoaderTest
       }
     }
     List<SequenceFeature> proteinFeatures = peptide.getSequenceFeatures();
-    SequenceFeatures.sortFeatures(proteinFeatures, true);
-    assertEquals(proteinFeatures.size(), 2);
-    sf = proteinFeatures.get(0);
-    assertEquals(sf.getFeatureGroup(), "VCF");
-    assertEquals(sf.getBegin(), 1);
-    assertEquals(sf.getEnd(), 1);
-    assertEquals(sf.getType(), SequenceOntologyI.SYNONYMOUS_VARIANT);
-    assertEquals(sf.getDescription(), "agC/agT");
-    sf = proteinFeatures.get(1);
-    assertEquals(sf.getFeatureGroup(), "VCF");
-    assertEquals(sf.getBegin(), 4);
-    assertEquals(sf.getEnd(), 4);
-    assertEquals(sf.getType(), SequenceOntologyI.NONSYNONYMOUS_VARIANT);
-    assertEquals(sf.getDescription(), "p.Glu4Gly");
+    /*
+     * JAL-3187 don't precompute protein features, do dynamically instead
+     */
+    assertTrue(proteinFeatures.isEmpty());
+    // SequenceFeatures.sortFeatures(proteinFeatures, true);
+    // assertEquals(proteinFeatures.size(), 2);
+    // sf = proteinFeatures.get(0);
+    // assertEquals(sf.getFeatureGroup(), "VCF");
+    // assertEquals(sf.getBegin(), 1);
+    // assertEquals(sf.getEnd(), 1);
+    // assertEquals(sf.getType(), SequenceOntologyI.SYNONYMOUS_VARIANT);
+    // assertEquals(sf.getDescription(), "agC/agT");
+    // sf = proteinFeatures.get(1);
+    // assertEquals(sf.getFeatureGroup(), "VCF");
+    // assertEquals(sf.getBegin(), 4);
+    // assertEquals(sf.getEnd(), 4);
+    // assertEquals(sf.getType(), SequenceOntologyI.NONSYNONYMOUS_VARIANT);
+    // assertEquals(sf.getDescription(), "p.Glu4Gly");
 
     /*
      * verify variant feature(s) added to transcript4
@@ -722,8 +734,7 @@ public class VCFLoaderTest
   @Test(groups = "Functional")
   public void testLoadVCFContig() throws IOException
   {
-    VCFLoader loader = new VCFLoader(
-            "test/jalview/io/vcf/testVcf2.vcf");
+    VCFLoader loader = new VCFLoader("test/jalview/io/vcf/testVcf2.vcf");
 
     SequenceI seq = loader.loadVCFContig("contig123");
     assertEquals(seq.getLength(), 15);