Skip to content

Commit

Permalink
Allow rename of SnpSift fields in MultiSourceAnnotator (#287)
Browse files Browse the repository at this point in the history
* Allow renaming of fields in MultiSourceAnnotator
  • Loading branch information
bbimber authored Oct 3, 2023
1 parent d4e765c commit 3b48e10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/main/java/com/github/discvrseq/walkers/ClinvarAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* This tool compares an input VCF to the provided <a href=https://www.ncbi.nlm.nih.gov/clinvar/>ClinVar</a> VCF and adds annotations to
* any variant overlapping with a variant from ClinVar (matching both site and allele). Note, this requires the VCF to use ClinVar's 2.0 VCF format.
* The ClinVar VCFs can be foind here: <a href="https://www.ncbi.nlm.nih.gov/variation/docs/ClinVar_vcf_files/">https://www.ncbi.nlm.nih.gov/variation/docs/ClinVar_vcf_files/</a>
* The ClinVar VCFs can be found here: <a href="https://www.ncbi.nlm.nih.gov/variation/docs/ClinVar_vcf_files/">https://www.ncbi.nlm.nih.gov/variation/docs/ClinVar_vcf_files/</a>
* <p/>
*
* <h3>Usage example</h3>
Expand Down Expand Up @@ -60,7 +60,7 @@ public class ClinvarAnnotator extends VariantWalker {
/**
* INFO header fields in the new VCF for ClinVar annotations.
*/
private List<VCFInfoHeaderLine> HEADER_LINES = Arrays.asList(
private final List<VCFInfoHeaderLine> HEADER_LINES = Arrays.asList(
new VCFInfoHeaderLine("CLN_ALLELE", VCFHeaderLineCount.R, VCFHeaderLineType.Character, "Alternate alleles from Clinvar"),
new VCFInfoHeaderLine("CLN_ALLELEID", VCFHeaderLineCount.R, VCFHeaderLineType.Integer, "the ClinVar Allele ID"),
new VCFInfoHeaderLine("CLN_DN", VCFHeaderLineCount.R, VCFHeaderLineType.String, "ClinVar's preferred disease name for the concept specified by disease identifiers in CLNDISDB"),
Expand Down Expand Up @@ -120,7 +120,7 @@ public void apply(VariantContext variant, ReadsContext readsContext, ReferenceCo
if (cvAllele.equals(alt)){
//gather annotations, add to map by allele
foundHit = true;
annotationMap.put(alt, transferAnnotations(vc, alt, vcb));
annotationMap.put(alt, transferAnnotations(vc, alt));
}
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public void apply(VariantContext variant, ReadsContext readsContext, ReferenceCo
if (nonNull > 0){
if (!sb.isEmpty()){
hasAnnotation = true;
vcb.attribute(line.getID(), sb.stream().collect(Collectors.joining(",")));
vcb.attribute(line.getID(), String.join(",", sb));
}
}
}
Expand Down Expand Up @@ -187,11 +187,10 @@ public void apply(VariantContext variant, ReadsContext readsContext, ReferenceCo
* </p>
*
* @param source ClinVar VCF
* @param alt alternate allele from ClinVar
* @param vcb new VCF to annotate
* @param alt alternate allele from ClinVar*
* @return a map which maps ClinVar alleles with their corresponding ClinVar annotations
*/
private Map<String, String> transferAnnotations (VariantContext source, Allele alt, VariantContextBuilder vcb){
private Map<String, String> transferAnnotations(VariantContext source, Allele alt){
Map<String, String> annotations = new HashMap<>();

annotations.put("CLN_ALLELE", alt.getDisplayString());
Expand Down Expand Up @@ -229,7 +228,6 @@ private Map<String, String> transferAnnotations (VariantContext source, Allele a
*
* @param source ClinVar VCF
* @param ID ClinVar VCF Annotation ID
* @return
*/
private String annotateValue(VariantContext source, String ID){
if (source.getAttribute(ID) == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.broadinstitute.hellbender.exceptions.GATKException;
import org.broadinstitute.hellbender.utils.Utils;

import javax.annotation.Nullable;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* This is a fairly specialized tool, designed to take the VCFs annotated with ClinvarAnnotator (from DISCVR-seq Toolkit) and <a href="https://www.hgsc.bcm.edu/software/cassandra">Cassandra</a>,
Expand Down Expand Up @@ -66,6 +68,11 @@ public class MultiSourceAnnotator extends VariantWalker {
@Argument(doc="SnpSift Allowed INFO Fields", fullName = "snpsift-fields", shortName = "ssf", optional = true)
public List<String> snpSiftFields = new ArrayList<>(SNPSIFT_INFO);

@Argument(doc="Renamed SnpSift INFO Fields. If provided, this must be of equal length as snpsift-fields. Each source snpsift-field will be transferred to an annotation of this name", fullName = "renamed-snpsift-fields", shortName = "rssf", optional = true)
public List<String> renamedSnpSiftFields = new ArrayList<>();

final Map<String, String> snpSiftFieldMapping = new HashMap<>();

@Argument(doc="Funcotator Annotated VCF", fullName = "funcotator", shortName = "f", optional = true)
public FeatureInput<VariantContext> funcotatorVariants = null;

Expand Down Expand Up @@ -466,6 +473,14 @@ public void onTraversalStart() {
allAnnotationKeys.add(id);
}

if (renamedSnpSiftFields != null && !renamedSnpSiftFields.isEmpty()) {
if (renamedSnpSiftFields.size() != snpSiftFields.size()) {
throw new GATKException("--renamed-snpsift-fields must be the same length as --snpsift-fields");
}

IntStream.range(0, renamedSnpSiftFields.size()).forEach(j -> snpSiftFieldMapping.put(snpSiftFields.get(j), renamedSnpSiftFields.get(j)));
}

List<String> allKeys = new ArrayList<>(snpSiftHeader.getInfoHeaderLines().stream().map(VCFInfoHeaderLine::getID).toList());
allKeys.removeAll(snpSiftFields);
if (!allKeys.isEmpty()) {
Expand Down Expand Up @@ -533,7 +548,7 @@ public void apply(VariantContext variant, ReadsContext readsContext, ReferenceCo
}

snpSift++;
transferInfoData(vcb, vc, snpSiftFields);
transferInfoData(vcb, vc, snpSiftFields, snpSiftFieldMapping);
}
}

Expand Down Expand Up @@ -593,9 +608,18 @@ private boolean matches(VariantContext source, VariantContext annotation){
}

private void transferInfoData(VariantContextBuilder vcb, VariantContext source, List<String> ids){
transferInfoData(vcb, source, ids, null);
}

private void transferInfoData(VariantContextBuilder vcb, VariantContext source, List<String> ids, @Nullable Map<String, String> oldKeyToNewKey){
for (String id : ids){
if (source.hasAttribute(id) && source.getAttribute(id) != null){
vcb.attribute(id, source.getAttribute(id));
if (oldKeyToNewKey != null) {
vcb.attribute(oldKeyToNewKey.getOrDefault(id, id), source.getAttribute(id));
}
else {
vcb.attribute(id, source.getAttribute(id));
}
}
}
}
Expand Down

0 comments on commit 3b48e10

Please sign in to comment.