1 package jalview.io.vcf;
3 import jalview.analysis.AlignmentUtils;
4 import jalview.analysis.Dna;
5 import jalview.api.AlignViewControllerGuiI;
6 import jalview.bin.Cache;
7 import jalview.datamodel.DBRefEntry;
8 import jalview.datamodel.GeneLociI;
9 import jalview.datamodel.Mapping;
10 import jalview.datamodel.SequenceFeature;
11 import jalview.datamodel.SequenceI;
12 import jalview.datamodel.features.FeatureAttributeType;
13 import jalview.datamodel.features.FeatureSource;
14 import jalview.datamodel.features.FeatureSources;
15 import jalview.ext.ensembl.EnsemblMap;
16 import jalview.ext.htsjdk.HtsContigDb;
17 import jalview.ext.htsjdk.VCFReader;
18 import jalview.io.gff.Gff3Helper;
19 import jalview.io.gff.SequenceOntologyI;
20 import jalview.util.MapList;
21 import jalview.util.MappingUtils;
22 import jalview.util.MessageManager;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import java.util.regex.Pattern;
35 import java.util.regex.PatternSyntaxException;
37 import htsjdk.samtools.SAMException;
38 import htsjdk.samtools.SAMSequenceDictionary;
39 import htsjdk.samtools.SAMSequenceRecord;
40 import htsjdk.samtools.util.CloseableIterator;
41 import htsjdk.tribble.TribbleException;
42 import htsjdk.variant.variantcontext.Allele;
43 import htsjdk.variant.variantcontext.VariantContext;
44 import htsjdk.variant.vcf.VCFConstants;
45 import htsjdk.variant.vcf.VCFHeader;
46 import htsjdk.variant.vcf.VCFHeaderLine;
47 import htsjdk.variant.vcf.VCFHeaderLineCount;
48 import htsjdk.variant.vcf.VCFHeaderLineType;
49 import htsjdk.variant.vcf.VCFInfoHeaderLine;
52 * A class to read VCF data (using the htsjdk) and add variants as sequence
53 * features on dna and any related protein product sequences
57 public class VCFLoader
60 * Jalview feature attributes for VCF fixed column data
62 private static final String VCF_POS = "POS";
64 private static final String VCF_ID = "ID";
66 private static final String VCF_QUAL = "QUAL";
68 private static final String VCF_FILTER = "FILTER";
70 private static final String NO_VALUE = VCFConstants.MISSING_VALUE_v4; // '.'
72 private static final String DEFAULT_SPECIES = "homo_sapiens";
75 * A class to model the mapping from sequence to VCF coordinates. Cases include
77 * <li>a direct 1:1 mapping where the sequence is one of the VCF contigs</li>
78 * <li>a mapping of sequence to chromosomal coordinates, where sequence and VCF
79 * use the same reference assembly</li>
80 * <li>a modified mapping of sequence to chromosomal coordinates, where sequence
81 * and VCF use different reference assembles</li>
86 final String chromosome;
90 VCFMap(String chr, MapList m)
97 public String toString()
99 return chromosome + ":" + map.toString();
104 * Lookup keys, and default values, for Preference entries that describe
105 * patterns for VCF and VEP fields to capture
107 private static final String VEP_FIELDS_PREF = "VEP_FIELDS";
109 private static final String VCF_FIELDS_PREF = "VCF_FIELDS";
111 private static final String DEFAULT_VCF_FIELDS = ".*";
113 private static final String DEFAULT_VEP_FIELDS = ".*";// "Allele,Consequence,IMPACT,SWISSPROT,SIFT,PolyPhen,CLIN_SIG";
116 * Lookup keys, and default values, for Preference entries that give
117 * mappings from tokens in the 'reference' header to species or assembly
119 private static final String VCF_ASSEMBLY = "VCF_ASSEMBLY";
121 private static final String DEFAULT_VCF_ASSEMBLY = "assembly19=GRCh37,hs37=GRCh37,grch37=GRCh37,grch38=GRCh38";
123 private static final String VCF_SPECIES = "VCF_SPECIES"; // default is human
125 private static final String DEFAULT_REFERENCE = "grch37"; // fallback default is human GRCh37
128 * keys to fields of VEP CSQ consequence data
129 * see https://www.ensembl.org/info/docs/tools/vep/vep_formats.html
131 private static final String CSQ_CONSEQUENCE_KEY = "Consequence";
132 private static final String CSQ_ALLELE_KEY = "Allele";
133 private static final String CSQ_ALLELE_NUM_KEY = "ALLELE_NUM"; // 0 (ref), 1...
134 private static final String CSQ_FEATURE_KEY = "Feature"; // Ensembl stable id
137 * default VCF INFO key for VEP consequence data
138 * NB this can be overridden running VEP with --vcf_info_field
139 * - we don't handle this case (require identifier to be CSQ)
141 private static final String CSQ_FIELD = "CSQ";
144 * separator for fields in consequence data is '|'
146 private static final String PIPE_REGEX = "\\|";
149 * delimiter that separates multiple consequence data blocks
151 private static final String COMMA = ",";
154 * the feature group assigned to a VCF variant in Jalview
156 private static final String FEATURE_GROUP_VCF = "VCF";
159 * internal delimiter used to build keys for assemblyMappings
162 private static final String EXCL = "!";
165 * the VCF file we are processing
167 protected String vcfFilePath;
170 * mappings between VCF and sequence reference assembly regions, as
171 * key = "species!chromosome!fromAssembly!toAssembly
172 * value = Map{fromRange, toRange}
174 private Map<String, Map<int[], int[]>> assemblyMappings;
176 private VCFReader reader;
179 * holds details of the VCF header lines (metadata)
181 private VCFHeader header;
184 * species (as a valid Ensembl term) the VCF is for
186 private String vcfSpecies;
189 * genome assembly version (as a valid Ensembl identifier) the VCF is for
191 private String vcfAssembly;
194 * a Dictionary of contigs (if present) referenced in the VCF file
196 private SAMSequenceDictionary dictionary;
199 * the position (0...) of field in each block of
200 * CSQ (consequence) data (if declared in the VCF INFO header for CSQ)
201 * see http://www.ensembl.org/info/docs/tools/vep/vep_formats.html
203 private int csqConsequenceFieldIndex = -1;
204 private int csqAlleleFieldIndex = -1;
205 private int csqAlleleNumberFieldIndex = -1;
206 private int csqFeatureFieldIndex = -1;
208 // todo the same fields for SnpEff ANN data if wanted
209 // see http://snpeff.sourceforge.net/SnpEff_manual.html#input
212 * a unique identifier under which to save metadata about feature
213 * attributes (selected INFO field data)
215 private String sourceId;
218 * The INFO IDs of data that is both present in the VCF file, and
219 * also matched by any filters for data of interest
221 List<String> vcfFieldsOfInterest;
224 * The field offsets and identifiers for VEP (CSQ) data that is both present
225 * in the VCF file, and also matched by any filters for data of interest
226 * for example 0 -> Allele, 1 -> Consequence, ..., 36 -> SIFT, ...
228 Map<Integer, String> vepFieldsOfInterest;
231 * key:value for which rejected data has been seen
232 * (the error is logged only once for each combination)
234 private Set<String> badData;
237 * Constructor given a VCF file
241 public VCFLoader(String vcfFile)
246 } catch (IOException e)
248 System.err.println("Error opening VCF file: " + e.getMessage());
251 // map of species!chromosome!fromAssembly!toAssembly to {fromRange, toRange}
252 assemblyMappings = new HashMap<>();
256 * Starts a new thread to query and load VCF variant data on to the given
259 * This method is not thread safe - concurrent threads should use separate
260 * instances of this class.
265 public void loadVCF(SequenceI[] seqs, final AlignViewControllerGuiI gui)
269 gui.setStatus(MessageManager.getString("label.searching_vcf"));
277 VCFLoader.this.doLoad(seqs, gui);
283 * Reads the specified contig sequence and adds its VCF variants to it
286 * the id of a single sequence (contig) to load
289 public SequenceI loadVCFContig(String contig)
291 VCFHeaderLine headerLine = header.getOtherHeaderLine(VCFHeader.REFERENCE_KEY);
292 if (headerLine == null)
294 Cache.log.error("VCF reference header not found");
297 String ref = headerLine.getValue();
298 if (ref.startsWith("file://"))
300 ref = ref.substring(7);
302 setSpeciesAndAssembly(ref);
304 SequenceI seq = null;
305 File dbFile = new File(ref);
309 HtsContigDb db = new HtsContigDb("", dbFile);
310 seq = db.getSequenceProxy(contig);
311 loadSequenceVCF(seq);
316 Cache.log.error("VCF reference not found: " + ref);
323 * Loads VCF on to one or more sequences
327 * optional callback handler for messages
329 protected void doLoad(SequenceI[] seqs, AlignViewControllerGuiI gui)
333 VCFHeaderLine ref = header
334 .getOtherHeaderLine(VCFHeader.REFERENCE_KEY);
335 String reference = ref == null ? null : ref.getValue();
337 setSpeciesAndAssembly(reference);
343 * query for VCF overlapping each sequence in turn
345 for (SequenceI seq : seqs)
347 int added = loadSequenceVCF(seq);
352 transferAddedFeatures(seq);
357 String msg = MessageManager.formatMessage("label.added_vcf",
360 if (gui.getFeatureSettingsUI() != null)
362 gui.getFeatureSettingsUI().discoverAllFeatureData();
365 } catch (Throwable e)
367 System.err.println("Error processing VCF: " + e.getMessage());
371 gui.setStatus("Error occurred - see console for details");
380 } catch (IOException e)
391 * Attempts to determine and save the species and genome assembly version to
392 * which the VCF data applies. This may be done by parsing the {@code reference}
393 * header line, configured in a property file, or (potentially) confirmed
394 * interactively by the user.
396 * The saved values should be identifiers valid for Ensembl's REST service
397 * {@code map} endpoint, so they can be used (if necessary) to retrieve the
398 * mapping between VCF coordinates and sequence coordinates.
401 * @see https://rest.ensembl.org/documentation/info/assembly_map
402 * @see https://rest.ensembl.org/info/assembly/human?content-type=text/xml
403 * @see https://rest.ensembl.org/info/species?content-type=text/xml
405 protected void setSpeciesAndAssembly(String reference)
407 if (reference == null)
409 Cache.log.error("No VCF ##reference found, defaulting to "
410 + DEFAULT_REFERENCE + ":" + DEFAULT_SPECIES);
411 reference = DEFAULT_REFERENCE; // default to GRCh37 if not specified
413 reference = reference.toLowerCase();
416 * for a non-human species, or other assembly identifier,
417 * specify as a Jalview property file entry e.g.
418 * VCF_ASSEMBLY = hs37=GRCh37,assembly19=GRCh37
419 * VCF_SPECIES = c_elegans=celegans
420 * to map a token in the reference header to a value
422 String prop = Cache.getDefault(VCF_ASSEMBLY, DEFAULT_VCF_ASSEMBLY);
423 for (String token : prop.split(","))
425 String[] tokens = token.split("=");
426 if (tokens.length == 2)
428 if (reference.contains(tokens[0].trim().toLowerCase()))
430 vcfAssembly = tokens[1].trim();
436 vcfSpecies = DEFAULT_SPECIES;
437 prop = Cache.getProperty(VCF_SPECIES);
440 for (String token : prop.split(","))
442 String[] tokens = token.split("=");
443 if (tokens.length == 2)
445 if (reference.contains(tokens[0].trim().toLowerCase()))
447 vcfSpecies = tokens[1].trim();
456 * Opens the VCF file and parses header data
459 * @throws IOException
461 private void initialise(String filePath) throws IOException
463 vcfFilePath = filePath;
465 reader = new VCFReader(filePath);
467 header = reader.getFileHeader();
471 dictionary = header.getSequenceDictionary();
472 } catch (SAMException e)
474 // ignore - thrown if any contig line lacks length info
479 saveMetadata(sourceId);
482 * get offset of CSQ ALLELE_NUM and Feature if declared
488 * Reads metadata (such as INFO field descriptions and datatypes) and saves
489 * them for future reference
493 void saveMetadata(String theSourceId)
495 List<Pattern> vcfFieldPatterns = getFieldMatchers(VCF_FIELDS_PREF,
497 vcfFieldsOfInterest = new ArrayList<>();
499 FeatureSource metadata = new FeatureSource(theSourceId);
501 for (VCFInfoHeaderLine info : header.getInfoHeaderLines())
503 String attributeId = info.getID();
504 String desc = info.getDescription();
505 VCFHeaderLineType type = info.getType();
506 FeatureAttributeType attType = null;
510 attType = FeatureAttributeType.Character;
513 attType = FeatureAttributeType.Flag;
516 attType = FeatureAttributeType.Float;
519 attType = FeatureAttributeType.Integer;
522 attType = FeatureAttributeType.String;
525 metadata.setAttributeName(attributeId, desc);
526 metadata.setAttributeType(attributeId, attType);
528 if (isFieldWanted(attributeId, vcfFieldPatterns))
530 vcfFieldsOfInterest.add(attributeId);
534 FeatureSources.getInstance().addSource(theSourceId, metadata);
538 * Answers true if the field id is matched by any of the filter patterns, else
539 * false. Matching is against regular expression patterns, and is not
546 private boolean isFieldWanted(String id, List<Pattern> filters)
548 for (Pattern p : filters)
550 if (p.matcher(id.toUpperCase()).matches())
559 * Records 'wanted' fields defined in the CSQ INFO header (if there is one).
560 * Also records the position of selected fields (Allele, ALLELE_NUM, Feature)
561 * required for processing.
563 * CSQ fields are declared in the CSQ INFO Description e.g.
565 * Description="Consequence ...from ... VEP. Format: Allele|Consequence|...
567 protected void parseCsqHeader()
569 List<Pattern> vepFieldFilters = getFieldMatchers(VEP_FIELDS_PREF,
571 vepFieldsOfInterest = new HashMap<>();
573 VCFInfoHeaderLine csqInfo = header.getInfoHeaderLine(CSQ_FIELD);
580 * parse out the pipe-separated list of CSQ fields; we assume here that
581 * these form the last part of the description, and contain no spaces
583 String desc = csqInfo.getDescription();
584 int spacePos = desc.lastIndexOf(" ");
585 desc = desc.substring(spacePos + 1);
589 String[] format = desc.split(PIPE_REGEX);
591 for (String field : format)
593 if (CSQ_CONSEQUENCE_KEY.equals(field))
595 csqConsequenceFieldIndex = index;
597 if (CSQ_ALLELE_NUM_KEY.equals(field))
599 csqAlleleNumberFieldIndex = index;
601 if (CSQ_ALLELE_KEY.equals(field))
603 csqAlleleFieldIndex = index;
605 if (CSQ_FEATURE_KEY.equals(field))
607 csqFeatureFieldIndex = index;
610 if (isFieldWanted(field, vepFieldFilters))
612 vepFieldsOfInterest.put(index, field);
621 * Reads the Preference value for the given key, with default specified if no
622 * preference set. The value is interpreted as a comma-separated list of
623 * regular expressions, and converted into a list of compiled patterns ready
624 * for matching. Patterns are forced to upper-case for non-case-sensitive
627 * This supports user-defined filters for fields of interest to capture while
628 * processing data. For example, VCF_FIELDS = AF,AC* would mean that VCF INFO
629 * fields with an ID of AF, or starting with AC, would be matched.
635 private List<Pattern> getFieldMatchers(String key, String def)
637 String pref = Cache.getDefault(key, def);
638 List<Pattern> patterns = new ArrayList<>();
639 String[] tokens = pref.split(",");
640 for (String token : tokens)
644 patterns.add(Pattern.compile(token.toUpperCase()));
645 } catch (PatternSyntaxException e)
647 System.err.println("Invalid pattern ignored: " + token);
654 * Transfers VCF features to sequences to which this sequence has a mapping.
655 * If the mapping is 3:1, computes peptide variants from nucleotide variants.
659 protected void transferAddedFeatures(SequenceI seq)
661 DBRefEntry[] dbrefs = seq.getDBRefs();
666 for (DBRefEntry dbref : dbrefs)
668 Mapping mapping = dbref.getMap();
669 if (mapping == null || mapping.getTo() == null)
674 SequenceI mapTo = mapping.getTo();
675 MapList map = mapping.getMap();
676 if (map.getFromRatio() == 3)
679 * dna-to-peptide product mapping
681 AlignmentUtils.computeProteinFeatures(seq, mapTo, map);
686 * nucleotide-to-nucleotide mapping e.g. transcript to CDS
688 List<SequenceFeature> features = seq.getFeatures()
689 .getPositionalFeatures(SequenceOntologyI.SEQUENCE_VARIANT);
690 for (SequenceFeature sf : features)
692 if (FEATURE_GROUP_VCF.equals(sf.getFeatureGroup()))
694 transferFeature(sf, mapTo, map);
702 * Tries to add overlapping variants read from a VCF file to the given sequence,
703 * and returns the number of variant features added
708 protected int loadSequenceVCF(SequenceI seq)
710 VCFMap vcfMap = getVcfMap(seq);
717 * work with the dataset sequence here
719 SequenceI dss = seq.getDatasetSequence();
724 return addVcfVariants(dss, vcfMap);
728 * Answers a map from sequence coordinates to VCF chromosome ranges
733 private VCFMap getVcfMap(SequenceI seq)
736 * simplest case: sequence has id and length matching a VCF contig
738 VCFMap vcfMap = null;
739 if (dictionary != null)
741 vcfMap = getContigMap(seq);
749 * otherwise, map to VCF from chromosomal coordinates
750 * of the sequence (if known)
752 GeneLociI seqCoords = seq.getGeneLoci();
753 if (seqCoords == null)
755 Cache.log.warn(String.format(
756 "Can't query VCF for %s as chromosome coordinates not known",
761 String species = seqCoords.getSpeciesId();
762 String chromosome = seqCoords.getChromosomeId();
763 String seqRef = seqCoords.getAssemblyId();
764 MapList map = seqCoords.getMapping();
766 // note this requires the configured species to match that
767 // returned with the Ensembl sequence; todo: support aliases?
768 if (!vcfSpecies.equalsIgnoreCase(species))
770 Cache.log.warn("No VCF loaded to " + seq.getName()
771 + " as species not matched");
775 if (seqRef.equalsIgnoreCase(vcfAssembly))
777 return new VCFMap(chromosome, map);
781 * VCF data has a different reference assembly to the sequence:
782 * query Ensembl to map chromosomal coordinates from sequence to VCF
784 List<int[]> toVcfRanges = new ArrayList<>();
785 List<int[]> fromSequenceRanges = new ArrayList<>();
787 for (int[] range : map.getToRanges())
789 int[] fromRange = map.locateInFrom(range[0], range[1]);
790 if (fromRange == null)
796 int[] newRange = mapReferenceRange(range, chromosome, "human", seqRef,
798 if (newRange == null)
801 String.format("Failed to map %s:%s:%s:%d:%d to %s", species,
802 chromosome, seqRef, range[0], range[1],
808 toVcfRanges.add(newRange);
809 fromSequenceRanges.add(fromRange);
813 return new VCFMap(chromosome,
814 new MapList(fromSequenceRanges, toVcfRanges, 1, 1));
818 * If the sequence id matches a contig declared in the VCF file, and the
819 * sequence length matches the contig length, then returns a 1:1 map of the
820 * sequence to the contig, else returns null
825 private VCFMap getContigMap(SequenceI seq)
827 String id = seq.getName();
828 SAMSequenceRecord contig = dictionary.getSequence(id);
831 int len = seq.getLength();
832 if (len == contig.getSequenceLength())
834 MapList map = new MapList(new int[] { 1, len },
837 return new VCFMap(id, map);
844 * Queries the VCF reader for any variants that overlap the mapped chromosome
845 * ranges of the sequence, and adds as variant features. Returns the number of
846 * overlapping variants found.
850 * mapping from sequence to VCF coordinates
853 protected int addVcfVariants(SequenceI seq, VCFMap map)
855 boolean forwardStrand = map.map.isToForwardStrand();
858 * query the VCF for overlaps of each contiguous chromosomal region
862 for (int[] range : map.map.getToRanges())
864 int vcfStart = Math.min(range[0], range[1]);
865 int vcfEnd = Math.max(range[0], range[1]);
868 CloseableIterator<VariantContext> variants = reader
869 .query(map.chromosome, vcfStart, vcfEnd);
870 while (variants.hasNext())
872 VariantContext variant = variants.next();
874 int[] featureRange = map.map.locateInFrom(variant.getStart(),
877 if (featureRange != null)
879 int featureStart = Math.min(featureRange[0], featureRange[1]);
880 int featureEnd = Math.max(featureRange[0], featureRange[1]);
881 count += addAlleleFeatures(seq, variant, featureStart,
882 featureEnd, forwardStrand);
886 } catch (TribbleException e)
889 * RuntimeException throwable by htsjdk
891 String msg = String.format("Error reading VCF for %s:%d-%d: %s ",
892 map.chromosome, vcfStart, vcfEnd);
893 Cache.log.error(msg);
901 * A convenience method to get an attribute value for an alternate allele
904 * @param attributeName
908 protected String getAttributeValue(VariantContext variant,
909 String attributeName, int alleleIndex)
911 Object att = variant.getAttribute(attributeName);
913 if (att instanceof String)
917 else if (att instanceof ArrayList)
919 return ((List<String>) att).get(alleleIndex);
926 * Adds one variant feature for each allele in the VCF variant record, and
927 * returns the number of features added.
931 * @param featureStart
933 * @param forwardStrand
936 protected int addAlleleFeatures(SequenceI seq, VariantContext variant,
937 int featureStart, int featureEnd, boolean forwardStrand)
942 * Javadoc says getAlternateAlleles() imposes no order on the list returned
943 * so we proceed defensively to get them in strict order
945 int altAlleleCount = variant.getAlternateAlleles().size();
946 for (int i = 0; i < altAlleleCount; i++)
948 added += addAlleleFeature(seq, variant, i, featureStart, featureEnd,
955 * Inspects one allele and attempts to add a variant feature for it to the
956 * sequence. The additional data associated with this allele is extracted to
957 * store in the feature's key-value map. Answers the number of features added (0
962 * @param altAlleleIndex
964 * @param featureStart
966 * @param forwardStrand
969 protected int addAlleleFeature(SequenceI seq, VariantContext variant,
970 int altAlleleIndex, int featureStart, int featureEnd,
971 boolean forwardStrand)
973 String reference = variant.getReference().getBaseString();
974 Allele alt = variant.getAlternateAllele(altAlleleIndex);
975 String allele = alt.getBaseString();
978 * insertion after a genomic base, if on reverse strand, has to be
979 * converted to insertion of complement after the preceding position
981 int referenceLength = reference.length();
982 if (!forwardStrand && allele.length() > referenceLength
983 && allele.startsWith(reference))
985 featureStart -= referenceLength;
986 featureEnd = featureStart;
987 char insertAfter = seq.getCharAt(featureStart - seq.getStart());
988 reference = Dna.reverseComplement(String.valueOf(insertAfter));
989 allele = allele.substring(referenceLength) + reference;
993 * build the ref,alt allele description e.g. "G,A", using the base
994 * complement if the sequence is on the reverse strand
996 StringBuilder sb = new StringBuilder();
997 sb.append(forwardStrand ? reference : Dna.reverseComplement(reference));
999 sb.append(forwardStrand ? allele : Dna.reverseComplement(allele));
1000 String alleles = sb.toString(); // e.g. G,A
1003 * pick out the consequence data (if any) that is for the current allele
1004 * and feature (transcript) that matches the current sequence
1006 String consequence = getConsequenceForAlleleAndFeature(variant, CSQ_FIELD,
1007 altAlleleIndex, csqAlleleFieldIndex,
1008 csqAlleleNumberFieldIndex, seq.getName().toLowerCase(),
1009 csqFeatureFieldIndex);
1012 * pick out the ontology term for the consequence type
1014 String type = SequenceOntologyI.SEQUENCE_VARIANT;
1015 if (consequence != null)
1017 type = getOntologyTerm(consequence);
1020 SequenceFeature sf = new SequenceFeature(type, alleles, featureStart,
1021 featureEnd, FEATURE_GROUP_VCF);
1022 sf.setSource(sourceId);
1025 * save the derived alleles as a named attribute; this will be
1026 * needed when Jalview computes derived peptide variants
1028 addFeatureAttribute(sf, Gff3Helper.ALLELES, alleles);
1031 * add selected VCF fixed column data as feature attributes
1033 addFeatureAttribute(sf, VCF_POS, String.valueOf(variant.getStart()));
1034 addFeatureAttribute(sf, VCF_ID, variant.getID());
1035 addFeatureAttribute(sf, VCF_QUAL,
1036 String.valueOf(variant.getPhredScaledQual()));
1037 addFeatureAttribute(sf, VCF_FILTER, getFilter(variant));
1039 addAlleleProperties(variant, sf, altAlleleIndex, consequence);
1041 seq.addSequenceFeature(sf);
1047 * Answers the VCF FILTER value for the variant - or an approximation to it.
1048 * This field is either PASS, or a semi-colon separated list of filters not
1049 * passed. htsjdk saves filters as a HashSet, so the order when reassembled into
1050 * a list may be different.
1055 String getFilter(VariantContext variant)
1057 Set<String> filters = variant.getFilters();
1058 if (filters.isEmpty())
1062 Iterator<String> iterator = filters.iterator();
1063 String first = iterator.next();
1064 if (filters.size() == 1)
1069 StringBuilder sb = new StringBuilder(first);
1070 while (iterator.hasNext())
1072 sb.append(";").append(iterator.next());
1075 return sb.toString();
1079 * Adds one feature attribute unless the value is null, empty or '.'
1085 void addFeatureAttribute(SequenceFeature sf, String key, String value)
1087 if (value != null && !value.isEmpty() && !NO_VALUE.equals(value))
1089 sf.setValue(key, value);
1094 * Determines the Sequence Ontology term to use for the variant feature type in
1095 * Jalview. The default is 'sequence_variant', but a more specific term is used
1098 * <li>VEP (or SnpEff) Consequence annotation is included in the VCF</li>
1099 * <li>sequence id can be matched to VEP Feature (or SnpEff Feature_ID)</li>
1102 * @param consequence
1104 * @see http://www.sequenceontology.org/browser/current_svn/term/SO:0001060
1106 String getOntologyTerm(String consequence)
1108 String type = SequenceOntologyI.SEQUENCE_VARIANT;
1111 * could we associate Consequence data with this allele and feature (transcript)?
1112 * if so, prefer the consequence term from that data
1114 if (csqAlleleFieldIndex == -1) // && snpEffAlleleFieldIndex == -1
1117 * no Consequence data so we can't refine the ontology term
1122 if (consequence != null)
1124 String[] csqFields = consequence.split(PIPE_REGEX);
1125 if (csqFields.length > csqConsequenceFieldIndex)
1127 type = csqFields[csqConsequenceFieldIndex];
1132 // todo the same for SnpEff consequence data matching if wanted
1136 * if of the form (e.g.) missense_variant&splice_region_variant,
1137 * just take the first ('most severe') consequence
1141 int pos = type.indexOf('&');
1144 type = type.substring(0, pos);
1151 * Returns matched consequence data if it can be found, else null.
1153 * <li>inspects the VCF data for key 'vcfInfoId'</li>
1154 * <li>splits this on comma (to distinct consequences)</li>
1155 * <li>returns the first consequence (if any) where</li>
1157 * <li>the allele matches the altAlleleIndex'th allele of variant</li>
1158 * <li>the feature matches the sequence name (e.g. transcript id)</li>
1161 * If matched, the consequence is returned (as pipe-delimited fields).
1165 * @param altAlleleIndex
1166 * @param alleleFieldIndex
1167 * @param alleleNumberFieldIndex
1169 * @param featureFieldIndex
1172 private String getConsequenceForAlleleAndFeature(VariantContext variant,
1173 String vcfInfoId, int altAlleleIndex, int alleleFieldIndex,
1174 int alleleNumberFieldIndex,
1175 String seqName, int featureFieldIndex)
1177 if (alleleFieldIndex == -1 || featureFieldIndex == -1)
1181 Object value = variant.getAttribute(vcfInfoId);
1183 if (value == null || !(value instanceof List<?>))
1189 * inspect each consequence in turn (comma-separated blocks
1190 * extracted by htsjdk)
1192 List<String> consequences = (List<String>) value;
1194 for (String consequence : consequences)
1196 String[] csqFields = consequence.split(PIPE_REGEX);
1197 if (csqFields.length > featureFieldIndex)
1199 String featureIdentifier = csqFields[featureFieldIndex];
1200 if (featureIdentifier.length() > 4
1201 && seqName.indexOf(featureIdentifier.toLowerCase()) > -1)
1204 * feature (transcript) matched - now check for allele match
1206 if (matchAllele(variant, altAlleleIndex, csqFields,
1207 alleleFieldIndex, alleleNumberFieldIndex))
1217 private boolean matchAllele(VariantContext variant, int altAlleleIndex,
1218 String[] csqFields, int alleleFieldIndex,
1219 int alleleNumberFieldIndex)
1222 * if ALLELE_NUM is present, it must match altAlleleIndex
1223 * NB first alternate allele is 1 for ALLELE_NUM, 0 for altAlleleIndex
1225 if (alleleNumberFieldIndex > -1)
1227 if (csqFields.length <= alleleNumberFieldIndex)
1231 String alleleNum = csqFields[alleleNumberFieldIndex];
1232 return String.valueOf(altAlleleIndex + 1).equals(alleleNum);
1236 * else consequence allele must match variant allele
1238 if (alleleFieldIndex > -1 && csqFields.length > alleleFieldIndex)
1240 String csqAllele = csqFields[alleleFieldIndex];
1241 String vcfAllele = variant.getAlternateAllele(altAlleleIndex)
1243 return csqAllele.equals(vcfAllele);
1249 * Add any allele-specific VCF key-value data to the sequence feature
1253 * @param altAlelleIndex
1255 * @param consequence
1256 * if not null, the consequence specific to this sequence (transcript
1257 * feature) and allele
1259 protected void addAlleleProperties(VariantContext variant,
1260 SequenceFeature sf, final int altAlelleIndex, String consequence)
1262 Map<String, Object> atts = variant.getAttributes();
1264 for (Entry<String, Object> att : atts.entrySet())
1266 String key = att.getKey();
1269 * extract Consequence data (if present) that we are able to
1270 * associated with the allele for this variant feature
1272 if (CSQ_FIELD.equals(key))
1274 addConsequences(variant, sf, consequence);
1279 * filter out fields we don't want to capture
1281 if (!vcfFieldsOfInterest.contains(key))
1287 * filter out fields we don't want to capture
1289 if (!vcfFieldsOfInterest.contains(key))
1295 * we extract values for other data which are allele-specific;
1296 * these may be per alternate allele (INFO[key].Number = 'A')
1297 * or per allele including reference (INFO[key].Number = 'R')
1299 VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(key);
1300 if (infoHeader == null)
1303 * can't be sure what data belongs to this allele, so
1304 * play safe and don't take any
1309 VCFHeaderLineCount number = infoHeader.getCountType();
1310 int index = altAlelleIndex;
1311 if (number == VCFHeaderLineCount.R)
1314 * one value per allele including reference, so bump index
1315 * e.g. the 3rd value is for the 2nd alternate allele
1319 else if (number != VCFHeaderLineCount.A)
1322 * don't save other values as not allele-related
1328 * take the index'th value
1330 String value = getAttributeValue(variant, key, index);
1331 if (value != null && isValid(variant, key, value))
1333 addFeatureAttribute(sf, key, value);
1339 * Answers true for '.', null, or an empty value, or if the INFO type is String.
1340 * If the INFO type is Integer or Float, answers false if the value is not in
1348 protected boolean isValid(VariantContext variant, String infoId,
1351 if (value == null || value.isEmpty() || NO_VALUE.equals(value))
1355 VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(infoId);
1356 if (infoHeader == null)
1358 Cache.log.error("Field " + infoId + " has no INFO header");
1361 VCFHeaderLineType infoType = infoHeader.getType();
1364 if (infoType == VCFHeaderLineType.Integer)
1366 Integer.parseInt(value);
1368 else if (infoType == VCFHeaderLineType.Float)
1370 Float.parseFloat(value);
1372 } catch (NumberFormatException e)
1374 logInvalidValue(variant, infoId, value);
1381 * Logs an error message for malformed data; duplicate messages (same id and
1382 * value) are not logged
1388 private void logInvalidValue(VariantContext variant, String infoId,
1391 if (badData == null)
1393 badData = new HashSet<>();
1395 String token = infoId + ":" + value;
1396 if (!badData.contains(token))
1399 Cache.log.error(String.format("Invalid VCF data at %s:%d %s=%s",
1400 variant.getContig(), variant.getStart(), infoId, value));
1405 * Inspects CSQ data blocks (consequences) and adds attributes on the sequence
1408 * If <code>myConsequence</code> is not null, then this is the specific
1409 * consequence data (pipe-delimited fields) that is for the current allele and
1410 * transcript (sequence) being processed)
1414 * @param myConsequence
1416 protected void addConsequences(VariantContext variant, SequenceFeature sf,
1417 String myConsequence)
1419 Object value = variant.getAttribute(CSQ_FIELD);
1421 if (value == null || !(value instanceof List<?>))
1426 List<String> consequences = (List<String>) value;
1429 * inspect CSQ consequences; restrict to the consequence
1430 * associated with the current transcript (Feature)
1432 Map<String, String> csqValues = new HashMap<>();
1434 for (String consequence : consequences)
1436 if (myConsequence == null || myConsequence.equals(consequence))
1438 String[] csqFields = consequence.split(PIPE_REGEX);
1441 * inspect individual fields of this consequence, copying non-null
1442 * values which are 'fields of interest'
1445 for (String field : csqFields)
1447 if (field != null && field.length() > 0)
1449 String id = vepFieldsOfInterest.get(i);
1452 csqValues.put(id, field);
1460 if (!csqValues.isEmpty())
1462 sf.setValue(CSQ_FIELD, csqValues);
1467 * A convenience method to complement a dna base and return the string value
1473 protected String complement(byte[] reference)
1475 return String.valueOf(Dna.getComplement((char) reference[0]));
1479 * Determines the location of the query range (chromosome positions) in a
1480 * different reference assembly.
1482 * If the range is just a subregion of one for which we already have a mapping
1483 * (for example, an exon sub-region of a gene), then the mapping is just
1484 * computed arithmetically.
1486 * Otherwise, calls the Ensembl REST service that maps from one assembly
1487 * reference's coordinates to another's
1490 * start-end chromosomal range in 'fromRef' coordinates
1494 * assembly reference for the query coordinates
1496 * assembly reference we wish to translate to
1497 * @return the start-end range in 'toRef' coordinates
1499 protected int[] mapReferenceRange(int[] queryRange, String chromosome,
1500 String species, String fromRef, String toRef)
1503 * first try shorcut of computing the mapping as a subregion of one
1504 * we already have (e.g. for an exon, if we have the gene mapping)
1506 int[] mappedRange = findSubsumedRangeMapping(queryRange, chromosome,
1507 species, fromRef, toRef);
1508 if (mappedRange != null)
1514 * call (e.g.) http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37
1516 EnsemblMap mapper = new EnsemblMap();
1517 int[] mapping = mapper.getAssemblyMapping(species, chromosome, fromRef,
1520 if (mapping == null)
1522 // mapping service failure
1527 * save mapping for possible future re-use
1529 String key = makeRangesKey(chromosome, species, fromRef, toRef);
1530 if (!assemblyMappings.containsKey(key))
1532 assemblyMappings.put(key, new HashMap<int[], int[]>());
1535 assemblyMappings.get(key).put(queryRange, mapping);
1541 * If we already have a 1:1 contiguous mapping which subsumes the given query
1542 * range, this method just calculates and returns the subset of that mapping,
1543 * else it returns null. In practical terms, if a gene has a contiguous
1544 * mapping between (for example) GRCh37 and GRCh38, then we assume that its
1545 * subsidiary exons occupy unchanged relative positions, and just compute
1546 * these as offsets, rather than do another lookup of the mapping.
1548 * If in future these assumptions prove invalid (e.g. for bacterial dna?!),
1549 * simply remove this method or let it always return null.
1551 * Warning: many rapid calls to the /map service map result in a 429 overload
1561 protected int[] findSubsumedRangeMapping(int[] queryRange, String chromosome,
1562 String species, String fromRef, String toRef)
1564 String key = makeRangesKey(chromosome, species, fromRef, toRef);
1565 if (assemblyMappings.containsKey(key))
1567 Map<int[], int[]> mappedRanges = assemblyMappings.get(key);
1568 for (Entry<int[], int[]> mappedRange : mappedRanges.entrySet())
1570 int[] fromRange = mappedRange.getKey();
1571 int[] toRange = mappedRange.getValue();
1572 if (fromRange[1] - fromRange[0] == toRange[1] - toRange[0])
1575 * mapping is 1:1 in length, so we trust it to have no discontinuities
1577 if (MappingUtils.rangeContains(fromRange, queryRange))
1580 * fromRange subsumes our query range
1582 int offset = queryRange[0] - fromRange[0];
1583 int mappedRangeFrom = toRange[0] + offset;
1584 int mappedRangeTo = mappedRangeFrom + (queryRange[1] - queryRange[0]);
1585 return new int[] { mappedRangeFrom, mappedRangeTo };
1594 * Transfers the sequence feature to the target sequence, locating its start
1595 * and end range based on the mapping. Features which do not overlap the
1596 * target sequence are ignored.
1599 * @param targetSequence
1601 * mapping from the feature's coordinates to the target sequence
1603 protected void transferFeature(SequenceFeature sf,
1604 SequenceI targetSequence, MapList mapping)
1606 int[] mappedRange = mapping.locateInTo(sf.getBegin(), sf.getEnd());
1608 if (mappedRange != null)
1610 String group = sf.getFeatureGroup();
1611 int newBegin = Math.min(mappedRange[0], mappedRange[1]);
1612 int newEnd = Math.max(mappedRange[0], mappedRange[1]);
1613 SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
1614 group, sf.getScore());
1615 targetSequence.addSequenceFeature(copy);
1620 * Formats a ranges map lookup key
1628 protected static String makeRangesKey(String chromosome, String species,
1629 String fromRef, String toRef)
1631 return species + EXCL + chromosome + EXCL + fromRef + EXCL