From f501ebf56839e4115d9d48d20a6c936856dc0e37 Mon Sep 17 00:00:00 2001 From: Mungo Carstairs Date: Sun, 27 Oct 2019 17:45:08 +0000 Subject: [PATCH] JAL-3469 add "Number=." fields (all values) to all alleles in VCF record --- src/jalview/io/vcf/VCFLoader.java | 32 +++++++++++++++++++++++++------- test/jalview/io/vcf/VCFLoaderTest.java | 10 ++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/jalview/io/vcf/VCFLoader.java b/src/jalview/io/vcf/VCFLoader.java index ac707d8..d3afc57 100644 --- a/src/jalview/io/vcf/VCFLoader.java +++ b/src/jalview/io/vcf/VCFLoader.java @@ -19,6 +19,7 @@ import jalview.io.gff.SequenceOntologyI; import jalview.util.MapList; import jalview.util.MappingUtils; import jalview.util.MessageManager; +import jalview.util.StringUtils; import java.io.File; import java.io.IOException; @@ -247,9 +248,9 @@ public class VCFLoader private Set badData; /** - * Constructor given a VCF file + * Constructor given a path to a VCF file * - * @param alignment + * @param vcfFile */ public VCFLoader(String vcfFile) { @@ -912,7 +913,11 @@ public class VCFLoader } /** - * A convenience method to get an attribute value for an alternate allele + * A convenience method to get an attribute value for an alternate allele. + * {@code alleleIndex} is the position in the list of values for the allele. + * If {@alleleIndex == -1} then all values are concatenated (comma-separated). + * This is the case for fields declared with "Number=." i.e. values are not + * related to specific alleles. * * @param variant * @param attributeName @@ -924,16 +929,25 @@ public class VCFLoader { Object att = variant.getAttribute(attributeName); + String result = null; if (att instanceof String) { - return (String) att; + result = (String) att; } - else if (att instanceof ArrayList) + else if (att instanceof List) { - return ((List) att).get(alleleIndex); + List theList = (List) att; + if (alleleIndex == -1) + { + result = StringUtils.listToDelimitedString(theList, ","); + } + else + { + result = theList.get(alleleIndex); + } } - return null; + return result; } /** @@ -1322,6 +1336,10 @@ public class VCFLoader */ index++; } + else if (number == VCFHeaderLineCount.UNBOUNDED) // . + { + index = -1; + } else if (number != VCFHeaderLineCount.A) { /* diff --git a/test/jalview/io/vcf/VCFLoaderTest.java b/test/jalview/io/vcf/VCFLoaderTest.java index 87cf727..c050029 100644 --- a/test/jalview/io/vcf/VCFLoaderTest.java +++ b/test/jalview/io/vcf/VCFLoaderTest.java @@ -61,16 +61,17 @@ 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 + // note fields with no INFO definition are ignored when parsing "##INFO=", "##INFO=