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.List;
30 import java.util.Map.Entry;
31 import java.util.regex.Pattern;
32 import java.util.regex.PatternSyntaxException;
34 import htsjdk.samtools.SAMException;
35 import htsjdk.samtools.SAMSequenceDictionary;
36 import htsjdk.samtools.SAMSequenceRecord;
37 import htsjdk.samtools.util.CloseableIterator;
38 import htsjdk.variant.variantcontext.Allele;
39 import htsjdk.variant.variantcontext.VariantContext;
40 import htsjdk.variant.vcf.VCFHeader;
41 import htsjdk.variant.vcf.VCFHeaderLine;
42 import htsjdk.variant.vcf.VCFHeaderLineCount;
43 import htsjdk.variant.vcf.VCFHeaderLineType;
44 import htsjdk.variant.vcf.VCFInfoHeaderLine;
47 * A class to read VCF data (using the htsjdk) and add variants as sequence
48 * features on dna and any related protein product sequences
52 public class VCFLoader
54 private static final String DEFAULT_SPECIES = "homo_sapiens";
57 * A class to model the mapping from sequence to VCF coordinates. Cases include
59 * <li>a direct 1:1 mapping where the sequence is one of the VCF contigs</li>
60 * <li>a mapping of sequence to chromosomal coordinates, where sequence and VCF
61 * use the same reference assembly</li>
62 * <li>a modified mapping of sequence to chromosomal coordinates, where sequence
63 * and VCF use different reference assembles</li>
68 final String chromosome;
72 VCFMap(String chr, MapList m)
79 public String toString()
81 return chromosome + ":" + map.toString();
86 * Lookup keys, and default values, for Preference entries that describe
87 * patterns for VCF and VEP fields to capture
89 private static final String VEP_FIELDS_PREF = "VEP_FIELDS";
91 private static final String VCF_FIELDS_PREF = "VCF_FIELDS";
93 private static final String DEFAULT_VCF_FIELDS = ".*";
95 private static final String DEFAULT_VEP_FIELDS = ".*";// "Allele,Consequence,IMPACT,SWISSPROT,SIFT,PolyPhen,CLIN_SIG";
98 * Lookup keys, and default values, for Preference entries that give
99 * mappings from tokens in the 'reference' header to species or assembly
101 private static final String VCF_ASSEMBLY = "VCF_ASSEMBLY";
103 private static final String DEFAULT_VCF_ASSEMBLY = "assembly19=GRCh38,hs37=GRCh37,grch37=GRCh37,grch38=GRCh38";
105 private static final String VCF_SPECIES = "VCF_SPECIES"; // default is human
108 * keys to fields of VEP CSQ consequence data
109 * see https://www.ensembl.org/info/docs/tools/vep/vep_formats.html
111 private static final String CSQ_CONSEQUENCE_KEY = "Consequence";
112 private static final String CSQ_ALLELE_KEY = "Allele";
113 private static final String CSQ_ALLELE_NUM_KEY = "ALLELE_NUM"; // 0 (ref), 1...
114 private static final String CSQ_FEATURE_KEY = "Feature"; // Ensembl stable id
117 * default VCF INFO key for VEP consequence data
118 * NB this can be overridden running VEP with --vcf_info_field
119 * - we don't handle this case (require identifier to be CSQ)
121 private static final String CSQ_FIELD = "CSQ";
124 * separator for fields in consequence data is '|'
126 private static final String PIPE_REGEX = "\\|";
129 * delimiter that separates multiple consequence data blocks
131 private static final String COMMA = ",";
134 * the feature group assigned to a VCF variant in Jalview
136 private static final String FEATURE_GROUP_VCF = "VCF";
139 * internal delimiter used to build keys for assemblyMappings
142 private static final String EXCL = "!";
145 * the VCF file we are processing
147 protected String vcfFilePath;
150 * mappings between VCF and sequence reference assembly regions, as
151 * key = "species!chromosome!fromAssembly!toAssembly
152 * value = Map{fromRange, toRange}
154 private Map<String, Map<int[], int[]>> assemblyMappings;
156 private VCFReader reader;
159 * holds details of the VCF header lines (metadata)
161 private VCFHeader header;
164 * species (as a valid Ensembl term) the VCF is for
166 private String vcfSpecies;
169 * genome assembly version (as a valid Ensembl identifier) the VCF is for
171 private String vcfAssembly;
174 * a Dictionary of contigs (if present) referenced in the VCF file
176 private SAMSequenceDictionary dictionary;
179 * the position (0...) of field in each block of
180 * CSQ (consequence) data (if declared in the VCF INFO header for CSQ)
181 * see http://www.ensembl.org/info/docs/tools/vep/vep_formats.html
183 private int csqConsequenceFieldIndex = -1;
184 private int csqAlleleFieldIndex = -1;
185 private int csqAlleleNumberFieldIndex = -1;
186 private int csqFeatureFieldIndex = -1;
188 // todo the same fields for SnpEff ANN data if wanted
189 // see http://snpeff.sourceforge.net/SnpEff_manual.html#input
192 * a unique identifier under which to save metadata about feature
193 * attributes (selected INFO field data)
195 private String sourceId;
198 * The INFO IDs of data that is both present in the VCF file, and
199 * also matched by any filters for data of interest
201 List<String> vcfFieldsOfInterest;
204 * The field offsets and identifiers for VEP (CSQ) data that is both present
205 * in the VCF file, and also matched by any filters for data of interest
206 * for example 0 -> Allele, 1 -> Consequence, ..., 36 -> SIFT, ...
208 Map<Integer, String> vepFieldsOfInterest;
211 * Constructor given a VCF file
215 public VCFLoader(String vcfFile)
220 } catch (IOException e)
222 System.err.println("Error opening VCF file: " + e.getMessage());
225 // map of species!chromosome!fromAssembly!toAssembly to {fromRange, toRange}
226 assemblyMappings = new HashMap<>();
230 * Starts a new thread to query and load VCF variant data on to the given
233 * This method is not thread safe - concurrent threads should use separate
234 * instances of this class.
239 public void loadVCF(SequenceI[] seqs, final AlignViewControllerGuiI gui)
243 gui.setStatus(MessageManager.getString("label.searching_vcf"));
251 VCFLoader.this.doLoad(seqs, gui);
257 * Reads the specified contig sequence and adds its VCF variants to it
260 * the id of a single sequence (contig) to load
263 public SequenceI loadVCFContig(String contig)
265 String ref = header.getOtherHeaderLine(VCFHeader.REFERENCE_KEY)
267 if (ref.startsWith("file://"))
269 ref = ref.substring(7);
271 setSpeciesAndAssembly(ref);
273 SequenceI seq = null;
274 File dbFile = new File(ref);
278 HtsContigDb db = new HtsContigDb("", dbFile);
279 seq = db.getSequenceProxy(contig);
280 loadSequenceVCF(seq);
285 System.err.println("VCF reference not found: " + ref);
292 * Loads VCF on to one or more sequences
296 * optional callback handler for messages
298 protected void doLoad(SequenceI[] seqs, AlignViewControllerGuiI gui)
302 VCFHeaderLine ref = header
303 .getOtherHeaderLine(VCFHeader.REFERENCE_KEY);
304 String reference = ref.getValue();
306 setSpeciesAndAssembly(reference);
312 * query for VCF overlapping each sequence in turn
314 for (SequenceI seq : seqs)
316 int added = loadSequenceVCF(seq);
321 transferAddedFeatures(seq);
326 String msg = MessageManager.formatMessage("label.added_vcf",
329 if (gui.getFeatureSettingsUI() != null)
331 gui.getFeatureSettingsUI().discoverAllFeatureData();
334 } catch (Throwable e)
336 System.err.println("Error processing VCF: " + e.getMessage());
340 gui.setStatus("Error occurred - see console for details");
349 } catch (IOException e)
360 * Attempts to determine and save the species and genome assembly version to
361 * which the VCF data applies. This may be done by parsing the {@code reference}
362 * header line, configured in a property file, or (potentially) confirmed
363 * interactively by the user.
365 * The saved values should be identifiers valid for Ensembl's REST service
366 * {@code map} endpoint, so they can be used (if necessary) to retrieve the
367 * mapping between VCF coordinates and sequence coordinates.
370 * @see https://rest.ensembl.org/documentation/info/assembly_map
371 * @see https://rest.ensembl.org/info/assembly/human?content-type=text/xml
372 * @see https://rest.ensembl.org/info/species?content-type=text/xml
374 protected void setSpeciesAndAssembly(String reference)
376 vcfSpecies = DEFAULT_SPECIES;
379 * for a non-human species, or other assembly identifier,
380 * specify as a Jalview property file entry e.g.
381 * VCF_ASSEMBLY = hs37=GRCh37,assembly19=GRCh37
382 * VCF_SPECIES = c_elegans=celegans
383 * to map a token in the reference header to a value
385 String prop = Cache.getDefault(VCF_ASSEMBLY, DEFAULT_VCF_ASSEMBLY);
386 for (String token : prop.split(","))
388 String[] tokens = token.split("=");
389 if (tokens.length == 2)
391 if (reference.contains(tokens[0].trim().toLowerCase()))
393 vcfAssembly = tokens[1].trim();
399 prop = Cache.getProperty(VCF_SPECIES);
402 for (String token : prop.split(","))
404 String[] tokens = token.split("=");
405 if (tokens.length == 2)
407 if (reference.contains(tokens[0].trim().toLowerCase()))
409 vcfSpecies = tokens[1].trim();
418 * Opens the VCF file and parses header data
421 * @throws IOException
423 private void initialise(String filePath) throws IOException
425 vcfFilePath = filePath;
427 reader = new VCFReader(filePath);
429 header = reader.getFileHeader();
433 dictionary = header.getSequenceDictionary();
434 } catch (SAMException e)
436 // ignore - thrown if any contig line lacks length info
441 saveMetadata(sourceId);
444 * get offset of CSQ ALLELE_NUM and Feature if declared
450 * Reads metadata (such as INFO field descriptions and datatypes) and saves
451 * them for future reference
455 void saveMetadata(String theSourceId)
457 List<Pattern> vcfFieldPatterns = getFieldMatchers(VCF_FIELDS_PREF,
459 vcfFieldsOfInterest = new ArrayList<>();
461 FeatureSource metadata = new FeatureSource(theSourceId);
463 for (VCFInfoHeaderLine info : header.getInfoHeaderLines())
465 String attributeId = info.getID();
466 String desc = info.getDescription();
467 VCFHeaderLineType type = info.getType();
468 FeatureAttributeType attType = null;
472 attType = FeatureAttributeType.Character;
475 attType = FeatureAttributeType.Flag;
478 attType = FeatureAttributeType.Float;
481 attType = FeatureAttributeType.Integer;
484 attType = FeatureAttributeType.String;
487 metadata.setAttributeName(attributeId, desc);
488 metadata.setAttributeType(attributeId, attType);
490 if (isFieldWanted(attributeId, vcfFieldPatterns))
492 vcfFieldsOfInterest.add(attributeId);
496 FeatureSources.getInstance().addSource(theSourceId, metadata);
500 * Answers true if the field id is matched by any of the filter patterns, else
501 * false. Matching is against regular expression patterns, and is not
508 private boolean isFieldWanted(String id, List<Pattern> filters)
510 for (Pattern p : filters)
512 if (p.matcher(id.toUpperCase()).matches())
521 * Records 'wanted' fields defined in the CSQ INFO header (if there is one).
522 * Also records the position of selected fields (Allele, ALLELE_NUM, Feature)
523 * required for processing.
525 * CSQ fields are declared in the CSQ INFO Description e.g.
527 * Description="Consequence ...from ... VEP. Format: Allele|Consequence|...
529 protected void parseCsqHeader()
531 List<Pattern> vepFieldFilters = getFieldMatchers(VEP_FIELDS_PREF,
533 vepFieldsOfInterest = new HashMap<>();
535 VCFInfoHeaderLine csqInfo = header.getInfoHeaderLine(CSQ_FIELD);
542 * parse out the pipe-separated list of CSQ fields; we assume here that
543 * these form the last part of the description, and contain no spaces
545 String desc = csqInfo.getDescription();
546 int spacePos = desc.lastIndexOf(" ");
547 desc = desc.substring(spacePos + 1);
551 String[] format = desc.split(PIPE_REGEX);
553 for (String field : format)
555 if (CSQ_CONSEQUENCE_KEY.equals(field))
557 csqConsequenceFieldIndex = index;
559 if (CSQ_ALLELE_NUM_KEY.equals(field))
561 csqAlleleNumberFieldIndex = index;
563 if (CSQ_ALLELE_KEY.equals(field))
565 csqAlleleFieldIndex = index;
567 if (CSQ_FEATURE_KEY.equals(field))
569 csqFeatureFieldIndex = index;
572 if (isFieldWanted(field, vepFieldFilters))
574 vepFieldsOfInterest.put(index, field);
583 * Reads the Preference value for the given key, with default specified if no
584 * preference set. The value is interpreted as a comma-separated list of
585 * regular expressions, and converted into a list of compiled patterns ready
586 * for matching. Patterns are forced to upper-case for non-case-sensitive
589 * This supports user-defined filters for fields of interest to capture while
590 * processing data. For example, VCF_FIELDS = AF,AC* would mean that VCF INFO
591 * fields with an ID of AF, or starting with AC, would be matched.
597 private List<Pattern> getFieldMatchers(String key, String def)
599 String pref = Cache.getDefault(key, def);
600 List<Pattern> patterns = new ArrayList<>();
601 String[] tokens = pref.split(",");
602 for (String token : tokens)
606 patterns.add(Pattern.compile(token.toUpperCase()));
607 } catch (PatternSyntaxException e)
609 System.err.println("Invalid pattern ignored: " + token);
616 * Transfers VCF features to sequences to which this sequence has a mapping.
617 * If the mapping is 3:1, computes peptide variants from nucleotide variants.
621 protected void transferAddedFeatures(SequenceI seq)
623 DBRefEntry[] dbrefs = seq.getDBRefs();
628 for (DBRefEntry dbref : dbrefs)
630 Mapping mapping = dbref.getMap();
631 if (mapping == null || mapping.getTo() == null)
636 SequenceI mapTo = mapping.getTo();
637 MapList map = mapping.getMap();
638 if (map.getFromRatio() == 3)
641 * dna-to-peptide product mapping
643 AlignmentUtils.computeProteinFeatures(seq, mapTo, map);
648 * nucleotide-to-nucleotide mapping e.g. transcript to CDS
650 List<SequenceFeature> features = seq.getFeatures()
651 .getPositionalFeatures(SequenceOntologyI.SEQUENCE_VARIANT);
652 for (SequenceFeature sf : features)
654 if (FEATURE_GROUP_VCF.equals(sf.getFeatureGroup()))
656 transferFeature(sf, mapTo, map);
664 * Tries to add overlapping variants read from a VCF file to the given sequence,
665 * and returns the number of variant features added
670 protected int loadSequenceVCF(SequenceI seq)
672 VCFMap vcfMap = getVcfMap(seq);
679 * work with the dataset sequence here
681 SequenceI dss = seq.getDatasetSequence();
686 return addVcfVariants(dss, vcfMap);
690 * Answers a map from sequence coordinates to VCF chromosome ranges
695 private VCFMap getVcfMap(SequenceI seq)
698 * simplest case: sequence has id and length matching a VCF contig
700 VCFMap vcfMap = null;
701 if (dictionary != null)
703 vcfMap = getContigMap(seq);
711 * otherwise, map to VCF from chromosomal coordinates
712 * of the sequence (if known)
714 GeneLociI seqCoords = seq.getGeneLoci();
715 if (seqCoords == null)
717 Cache.log.warn(String.format(
718 "Can't query VCF for %s as chromosome coordinates not known",
723 String species = seqCoords.getSpeciesId();
724 String chromosome = seqCoords.getChromosomeId();
725 String seqRef = seqCoords.getAssemblyId();
726 MapList map = seqCoords.getMap();
728 // note this requires the configured species to match that
729 // returned with the Ensembl sequence; todo: support aliases?
730 if (!vcfSpecies.equalsIgnoreCase(species))
732 Cache.log.warn("No VCF loaded to " + seq.getName()
733 + " as species not matched");
737 if (seqRef.equalsIgnoreCase(vcfAssembly))
739 return new VCFMap(chromosome, map);
743 * VCF data has a different reference assembly to the sequence:
744 * query Ensembl to map chromosomal coordinates from sequence to VCF
746 List<int[]> toVcfRanges = new ArrayList<>();
747 List<int[]> fromSequenceRanges = new ArrayList<>();
749 for (int[] range : map.getToRanges())
751 int[] fromRange = map.locateInFrom(range[0], range[1]);
752 if (fromRange == null)
758 int[] newRange = mapReferenceRange(range, chromosome, "human", seqRef,
760 if (newRange == null)
763 String.format("Failed to map %s:%s:%s:%d:%d to %s", species,
764 chromosome, seqRef, range[0], range[1],
770 toVcfRanges.add(newRange);
771 fromSequenceRanges.add(fromRange);
775 return new VCFMap(chromosome,
776 new MapList(fromSequenceRanges, toVcfRanges, 1, 1));
780 * If the sequence id matches a contig declared in the VCF file, and the
781 * sequence length matches the contig length, then returns a 1:1 map of the
782 * sequence to the contig, else returns null
787 private VCFMap getContigMap(SequenceI seq)
789 String id = seq.getName();
790 SAMSequenceRecord contig = dictionary.getSequence(id);
793 int len = seq.getLength();
794 if (len == contig.getSequenceLength())
796 MapList map = new MapList(new int[] { 1, len },
799 return new VCFMap(id, map);
806 * Answers true if the species inferred from the VCF reference identifier
807 * matches that for the sequence
813 boolean vcfSpeciesMatchesSequence(String vcfAssembly, String speciesId)
816 // there are many aliases for species - how to equate one with another?
818 // VCF ##reference header is an unstructured URI - how to extract species?
819 // perhaps check if ref includes any (Ensembl) alias of speciesId??
820 // TODO ask the user to confirm this??
822 if (vcfAssembly.contains("Homo_sapiens") // gnomAD exome data example
823 && "HOMO_SAPIENS".equals(speciesId)) // Ensembl species id
828 if (vcfAssembly.contains("c_elegans") // VEP VCF response example
829 && "CAENORHABDITIS_ELEGANS".equals(speciesId)) // Ensembl
834 // this is not a sustainable solution...
840 * Queries the VCF reader for any variants that overlap the mapped chromosome
841 * ranges of the sequence, and adds as variant features. Returns the number of
842 * overlapping variants found.
846 * mapping from sequence to VCF coordinates
849 protected int addVcfVariants(SequenceI seq, VCFMap map)
851 boolean forwardStrand = map.map.isToForwardStrand();
854 * query the VCF for overlaps of each contiguous chromosomal region
858 for (int[] range : map.map.getToRanges())
860 int vcfStart = Math.min(range[0], range[1]);
861 int vcfEnd = Math.max(range[0], range[1]);
862 CloseableIterator<VariantContext> variants = reader
863 .query(map.chromosome, vcfStart, vcfEnd);
864 while (variants.hasNext())
866 VariantContext variant = variants.next();
868 int[] featureRange = map.map.locateInFrom(variant.getStart(),
871 if (featureRange != null)
873 int featureStart = Math.min(featureRange[0], featureRange[1]);
874 int featureEnd = Math.max(featureRange[0], featureRange[1]);
875 count += addAlleleFeatures(seq, variant, featureStart, featureEnd,
886 * A convenience method to get an attribute value for an alternate allele
889 * @param attributeName
893 protected String getAttributeValue(VariantContext variant,
894 String attributeName, int alleleIndex)
896 Object att = variant.getAttribute(attributeName);
898 if (att instanceof String)
902 else if (att instanceof ArrayList)
904 return ((List<String>) att).get(alleleIndex);
911 * Adds one variant feature for each allele in the VCF variant record, and
912 * returns the number of features added.
916 * @param featureStart
918 * @param forwardStrand
921 protected int addAlleleFeatures(SequenceI seq, VariantContext variant,
922 int featureStart, int featureEnd, boolean forwardStrand)
927 * Javadoc says getAlternateAlleles() imposes no order on the list returned
928 * so we proceed defensively to get them in strict order
930 int altAlleleCount = variant.getAlternateAlleles().size();
931 for (int i = 0; i < altAlleleCount; i++)
933 added += addAlleleFeature(seq, variant, i, featureStart, featureEnd,
940 * Inspects one allele and attempts to add a variant feature for it to the
941 * sequence. The additional data associated with this allele is extracted to
942 * store in the feature's key-value map. Answers the number of features added (0
947 * @param altAlleleIndex
949 * @param featureStart
951 * @param forwardStrand
954 protected int addAlleleFeature(SequenceI seq, VariantContext variant,
955 int altAlleleIndex, int featureStart, int featureEnd,
956 boolean forwardStrand)
958 String reference = variant.getReference().getBaseString();
959 Allele alt = variant.getAlternateAllele(altAlleleIndex);
960 String allele = alt.getBaseString();
963 * insertion after a genomic base, if on reverse strand, has to be
964 * converted to insertion of complement after the preceding position
966 int referenceLength = reference.length();
967 if (!forwardStrand && allele.length() > referenceLength
968 && allele.startsWith(reference))
970 featureStart -= referenceLength;
971 featureEnd = featureStart;
972 char insertAfter = seq.getCharAt(featureStart - seq.getStart());
973 reference = Dna.reverseComplement(String.valueOf(insertAfter));
974 allele = allele.substring(referenceLength) + reference;
978 * build the ref,alt allele description e.g. "G,A", using the base
979 * complement if the sequence is on the reverse strand
981 StringBuilder sb = new StringBuilder();
982 sb.append(forwardStrand ? reference : Dna.reverseComplement(reference));
984 sb.append(forwardStrand ? allele : Dna.reverseComplement(allele));
985 String alleles = sb.toString(); // e.g. G,A
988 * pick out the consequence data (if any) that is for the current allele
989 * and feature (transcript) that matches the current sequence
991 String consequence = getConsequenceForAlleleAndFeature(variant, CSQ_FIELD,
992 altAlleleIndex, csqAlleleFieldIndex,
993 csqAlleleNumberFieldIndex, seq.getName().toLowerCase(),
994 csqFeatureFieldIndex);
997 * pick out the ontology term for the consequence type
999 String type = SequenceOntologyI.SEQUENCE_VARIANT;
1000 if (consequence != null)
1002 type = getOntologyTerm(consequence);
1005 SequenceFeature sf = new SequenceFeature(type, alleles, featureStart,
1006 featureEnd, FEATURE_GROUP_VCF);
1007 sf.setSource(sourceId);
1009 sf.setValue(Gff3Helper.ALLELES, alleles);
1011 addAlleleProperties(variant, sf, altAlleleIndex, consequence);
1013 seq.addSequenceFeature(sf);
1019 * Determines the Sequence Ontology term to use for the variant feature type in
1020 * Jalview. The default is 'sequence_variant', but a more specific term is used
1023 * <li>VEP (or SnpEff) Consequence annotation is included in the VCF</li>
1024 * <li>sequence id can be matched to VEP Feature (or SnpEff Feature_ID)</li>
1027 * @param consequence
1029 * @see http://www.sequenceontology.org/browser/current_svn/term/SO:0001060
1031 String getOntologyTerm(String consequence)
1033 String type = SequenceOntologyI.SEQUENCE_VARIANT;
1036 * could we associate Consequence data with this allele and feature (transcript)?
1037 * if so, prefer the consequence term from that data
1039 if (csqAlleleFieldIndex == -1) // && snpEffAlleleFieldIndex == -1
1042 * no Consequence data so we can't refine the ontology term
1047 if (consequence != null)
1049 String[] csqFields = consequence.split(PIPE_REGEX);
1050 if (csqFields.length > csqConsequenceFieldIndex)
1052 type = csqFields[csqConsequenceFieldIndex];
1057 // todo the same for SnpEff consequence data matching if wanted
1061 * if of the form (e.g.) missense_variant&splice_region_variant,
1062 * just take the first ('most severe') consequence
1066 int pos = type.indexOf('&');
1069 type = type.substring(0, pos);
1076 * Returns matched consequence data if it can be found, else null.
1078 * <li>inspects the VCF data for key 'vcfInfoId'</li>
1079 * <li>splits this on comma (to distinct consequences)</li>
1080 * <li>returns the first consequence (if any) where</li>
1082 * <li>the allele matches the altAlleleIndex'th allele of variant</li>
1083 * <li>the feature matches the sequence name (e.g. transcript id)</li>
1086 * If matched, the consequence is returned (as pipe-delimited fields).
1090 * @param altAlleleIndex
1091 * @param alleleFieldIndex
1092 * @param alleleNumberFieldIndex
1094 * @param featureFieldIndex
1097 private String getConsequenceForAlleleAndFeature(VariantContext variant,
1098 String vcfInfoId, int altAlleleIndex, int alleleFieldIndex,
1099 int alleleNumberFieldIndex,
1100 String seqName, int featureFieldIndex)
1102 if (alleleFieldIndex == -1 || featureFieldIndex == -1)
1106 Object value = variant.getAttribute(vcfInfoId);
1108 if (value == null || !(value instanceof List<?>))
1114 * inspect each consequence in turn (comma-separated blocks
1115 * extracted by htsjdk)
1117 List<String> consequences = (List<String>) value;
1119 for (String consequence : consequences)
1121 String[] csqFields = consequence.split(PIPE_REGEX);
1122 if (csqFields.length > featureFieldIndex)
1124 String featureIdentifier = csqFields[featureFieldIndex];
1125 if (featureIdentifier.length() > 4
1126 && seqName.indexOf(featureIdentifier.toLowerCase()) > -1)
1129 * feature (transcript) matched - now check for allele match
1131 if (matchAllele(variant, altAlleleIndex, csqFields,
1132 alleleFieldIndex, alleleNumberFieldIndex))
1142 private boolean matchAllele(VariantContext variant, int altAlleleIndex,
1143 String[] csqFields, int alleleFieldIndex,
1144 int alleleNumberFieldIndex)
1147 * if ALLELE_NUM is present, it must match altAlleleIndex
1148 * NB first alternate allele is 1 for ALLELE_NUM, 0 for altAlleleIndex
1150 if (alleleNumberFieldIndex > -1)
1152 if (csqFields.length <= alleleNumberFieldIndex)
1156 String alleleNum = csqFields[alleleNumberFieldIndex];
1157 return String.valueOf(altAlleleIndex + 1).equals(alleleNum);
1161 * else consequence allele must match variant allele
1163 if (alleleFieldIndex > -1 && csqFields.length > alleleFieldIndex)
1165 String csqAllele = csqFields[alleleFieldIndex];
1166 String vcfAllele = variant.getAlternateAllele(altAlleleIndex)
1168 return csqAllele.equals(vcfAllele);
1174 * Add any allele-specific VCF key-value data to the sequence feature
1178 * @param altAlelleIndex
1180 * @param consequence
1181 * if not null, the consequence specific to this sequence (transcript
1182 * feature) and allele
1184 protected void addAlleleProperties(VariantContext variant,
1185 SequenceFeature sf, final int altAlelleIndex, String consequence)
1187 Map<String, Object> atts = variant.getAttributes();
1189 for (Entry<String, Object> att : atts.entrySet())
1191 String key = att.getKey();
1194 * extract Consequence data (if present) that we are able to
1195 * associated with the allele for this variant feature
1197 if (CSQ_FIELD.equals(key))
1199 addConsequences(variant, sf, consequence);
1204 * filter out fields we don't want to capture
1206 if (!vcfFieldsOfInterest.contains(key))
1212 * filter out fields we don't want to capture
1214 if (!vcfFieldsOfInterest.contains(key))
1220 * we extract values for other data which are allele-specific;
1221 * these may be per alternate allele (INFO[key].Number = 'A')
1222 * or per allele including reference (INFO[key].Number = 'R')
1224 VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(key);
1225 if (infoHeader == null)
1228 * can't be sure what data belongs to this allele, so
1229 * play safe and don't take any
1234 VCFHeaderLineCount number = infoHeader.getCountType();
1235 int index = altAlelleIndex;
1236 if (number == VCFHeaderLineCount.R)
1239 * one value per allele including reference, so bump index
1240 * e.g. the 3rd value is for the 2nd alternate allele
1244 else if (number != VCFHeaderLineCount.A)
1247 * don't save other values as not allele-related
1253 * take the index'th value
1255 String value = getAttributeValue(variant, key, index);
1258 sf.setValue(key, value);
1264 * Inspects CSQ data blocks (consequences) and adds attributes on the sequence
1267 * If <code>myConsequence</code> is not null, then this is the specific
1268 * consequence data (pipe-delimited fields) that is for the current allele and
1269 * transcript (sequence) being processed)
1273 * @param myConsequence
1275 protected void addConsequences(VariantContext variant, SequenceFeature sf,
1276 String myConsequence)
1278 Object value = variant.getAttribute(CSQ_FIELD);
1280 if (value == null || !(value instanceof List<?>))
1285 List<String> consequences = (List<String>) value;
1288 * inspect CSQ consequences; restrict to the consequence
1289 * associated with the current transcript (Feature)
1291 Map<String, String> csqValues = new HashMap<>();
1293 for (String consequence : consequences)
1295 if (myConsequence == null || myConsequence.equals(consequence))
1297 String[] csqFields = consequence.split(PIPE_REGEX);
1300 * inspect individual fields of this consequence, copying non-null
1301 * values which are 'fields of interest'
1304 for (String field : csqFields)
1306 if (field != null && field.length() > 0)
1308 String id = vepFieldsOfInterest.get(i);
1311 csqValues.put(id, field);
1319 if (!csqValues.isEmpty())
1321 sf.setValue(CSQ_FIELD, csqValues);
1326 * A convenience method to complement a dna base and return the string value
1332 protected String complement(byte[] reference)
1334 return String.valueOf(Dna.getComplement((char) reference[0]));
1338 * Determines the location of the query range (chromosome positions) in a
1339 * different reference assembly.
1341 * If the range is just a subregion of one for which we already have a mapping
1342 * (for example, an exon sub-region of a gene), then the mapping is just
1343 * computed arithmetically.
1345 * Otherwise, calls the Ensembl REST service that maps from one assembly
1346 * reference's coordinates to another's
1349 * start-end chromosomal range in 'fromRef' coordinates
1353 * assembly reference for the query coordinates
1355 * assembly reference we wish to translate to
1356 * @return the start-end range in 'toRef' coordinates
1358 protected int[] mapReferenceRange(int[] queryRange, String chromosome,
1359 String species, String fromRef, String toRef)
1362 * first try shorcut of computing the mapping as a subregion of one
1363 * we already have (e.g. for an exon, if we have the gene mapping)
1365 int[] mappedRange = findSubsumedRangeMapping(queryRange, chromosome,
1366 species, fromRef, toRef);
1367 if (mappedRange != null)
1373 * call (e.g.) http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37
1375 EnsemblMap mapper = new EnsemblMap();
1376 int[] mapping = mapper.getAssemblyMapping(species, chromosome, fromRef,
1379 if (mapping == null)
1381 // mapping service failure
1386 * save mapping for possible future re-use
1388 String key = makeRangesKey(chromosome, species, fromRef, toRef);
1389 if (!assemblyMappings.containsKey(key))
1391 assemblyMappings.put(key, new HashMap<int[], int[]>());
1394 assemblyMappings.get(key).put(queryRange, mapping);
1400 * If we already have a 1:1 contiguous mapping which subsumes the given query
1401 * range, this method just calculates and returns the subset of that mapping,
1402 * else it returns null. In practical terms, if a gene has a contiguous
1403 * mapping between (for example) GRCh37 and GRCh38, then we assume that its
1404 * subsidiary exons occupy unchanged relative positions, and just compute
1405 * these as offsets, rather than do another lookup of the mapping.
1407 * If in future these assumptions prove invalid (e.g. for bacterial dna?!),
1408 * simply remove this method or let it always return null.
1410 * Warning: many rapid calls to the /map service map result in a 429 overload
1420 protected int[] findSubsumedRangeMapping(int[] queryRange, String chromosome,
1421 String species, String fromRef, String toRef)
1423 String key = makeRangesKey(chromosome, species, fromRef, toRef);
1424 if (assemblyMappings.containsKey(key))
1426 Map<int[], int[]> mappedRanges = assemblyMappings.get(key);
1427 for (Entry<int[], int[]> mappedRange : mappedRanges.entrySet())
1429 int[] fromRange = mappedRange.getKey();
1430 int[] toRange = mappedRange.getValue();
1431 if (fromRange[1] - fromRange[0] == toRange[1] - toRange[0])
1434 * mapping is 1:1 in length, so we trust it to have no discontinuities
1436 if (MappingUtils.rangeContains(fromRange, queryRange))
1439 * fromRange subsumes our query range
1441 int offset = queryRange[0] - fromRange[0];
1442 int mappedRangeFrom = toRange[0] + offset;
1443 int mappedRangeTo = mappedRangeFrom + (queryRange[1] - queryRange[0]);
1444 return new int[] { mappedRangeFrom, mappedRangeTo };
1453 * Transfers the sequence feature to the target sequence, locating its start
1454 * and end range based on the mapping. Features which do not overlap the
1455 * target sequence are ignored.
1458 * @param targetSequence
1460 * mapping from the feature's coordinates to the target sequence
1462 protected void transferFeature(SequenceFeature sf,
1463 SequenceI targetSequence, MapList mapping)
1465 int[] mappedRange = mapping.locateInTo(sf.getBegin(), sf.getEnd());
1467 if (mappedRange != null)
1469 String group = sf.getFeatureGroup();
1470 int newBegin = Math.min(mappedRange[0], mappedRange[1]);
1471 int newEnd = Math.max(mappedRange[0], mappedRange[1]);
1472 SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
1473 group, sf.getScore());
1474 targetSequence.addSequenceFeature(copy);
1479 * Formats a ranges map lookup key
1487 protected static String makeRangesKey(String chromosome, String species,
1488 String fromRef, String toRef)
1490 return species + EXCL + chromosome + EXCL + fromRef + EXCL