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.AlignmentI;
8 import jalview.datamodel.DBRefEntry;
9 import jalview.datamodel.GeneLociI;
10 import jalview.datamodel.Mapping;
11 import jalview.datamodel.SequenceFeature;
12 import jalview.datamodel.SequenceI;
13 import jalview.datamodel.features.FeatureAttributeType;
14 import jalview.datamodel.features.FeatureSource;
15 import jalview.datamodel.features.FeatureSources;
16 import jalview.ext.ensembl.EnsemblMap;
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;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.SortedMap;
31 import java.util.TreeMap;
32 import java.util.regex.Pattern;
33 import java.util.regex.PatternSyntaxException;
35 import htsjdk.samtools.util.CloseableIterator;
36 import htsjdk.variant.variantcontext.Allele;
37 import htsjdk.variant.variantcontext.VariantContext;
38 import htsjdk.variant.vcf.VCFHeader;
39 import htsjdk.variant.vcf.VCFHeaderLine;
40 import htsjdk.variant.vcf.VCFHeaderLineCount;
41 import htsjdk.variant.vcf.VCFHeaderLineType;
42 import htsjdk.variant.vcf.VCFInfoHeaderLine;
45 * A class to read VCF data (using the htsjdk) and add variants as sequence
46 * features on dna and any related protein product sequences
50 public class VCFLoader
53 * Lookup keys, and default values, for Preference entries that describe
54 * patterns for VCF and VEP fields to capture
56 private static final String VEP_FIELDS_PREF = "VEP_FIELDS";
58 private static final String VCF_FIELDS_PREF = "VCF_FIELDS";
60 private static final String DEFAULT_VCF_FIELDS = ".*";
62 private static final String DEFAULT_VEP_FIELDS = ".*";// "Allele,Consequence,IMPACT,SWISSPROT,SIFT,PolyPhen,CLIN_SIG";
65 * keys to fields of VEP CSQ consequence data
66 * see https://www.ensembl.org/info/docs/tools/vep/vep_formats.html
68 private static final String ALLELE_KEY = "Allele";
70 private static final String ALLELE_NUM_KEY = "ALLELE_NUM"; // 0 (ref), 1...
71 private static final String FEATURE_KEY = "Feature"; // Ensembl stable id
74 * default VCF INFO key for VEP consequence data
75 * NB this can be overridden running VEP with --vcf_info_field
76 * - we don't handle this case (require identifier to be CSQ)
78 private static final String CSQ_FIELD = "CSQ";
81 * separator for fields in consequence data is '|'
83 private static final String PIPE_REGEX = "\\|";
86 * key for Allele Frequency output by VEP
87 * see http://www.ensembl.org/info/docs/tools/vep/vep_formats.html
89 private static final String ALLELE_FREQUENCY_KEY = "AF";
92 * delimiter that separates multiple consequence data blocks
94 private static final String COMMA = ",";
97 * the feature group assigned to a VCF variant in Jalview
99 private static final String FEATURE_GROUP_VCF = "VCF";
102 * internal delimiter used to build keys for assemblyMappings
105 private static final String EXCL = "!";
108 * the alignment we are associating VCF data with
110 private AlignmentI al;
113 * mappings between VCF and sequence reference assembly regions, as
114 * key = "species!chromosome!fromAssembly!toAssembly
115 * value = Map{fromRange, toRange}
117 private Map<String, Map<int[], int[]>> assemblyMappings;
120 * holds details of the VCF header lines (metadata)
122 private VCFHeader header;
125 * the position (0...) of field in each block of
126 * CSQ (consequence) data (if declared in the VCF INFO header for CSQ)
127 * see http://www.ensembl.org/info/docs/tools/vep/vep_formats.html
129 private int csqAlleleFieldIndex = -1;
130 private int csqAlleleNumberFieldIndex = -1;
131 private int csqFeatureFieldIndex = -1;
134 * a unique identifier under which to save metadata about feature
135 * attributes (selected INFO field data)
137 private String sourceId;
140 * The INFO IDs of data that is both present in the VCF file, and
141 * also matched by any filters for data of interest
143 List<String> vcfFieldsOfInterest;
146 * The field offsets and identifiers for VEP (CSQ) data that is both present
147 * in the VCF file, and also matched by any filters for data of interest
148 * for example 0 -> Allele, 1 -> Consequence, ..., 36 -> SIFT, ...
150 Map<Integer, String> vepFieldsOfInterest;
153 * Constructor given an alignment context
157 public VCFLoader(AlignmentI alignment)
161 // map of species!chromosome!fromAssembly!toAssembly to {fromRange, toRange}
162 assemblyMappings = new HashMap<>();
166 * Starts a new thread to query and load VCF variant data on to the alignment
168 * This method is not thread safe - concurrent threads should use separate
169 * instances of this class.
174 public void loadVCF(final String filePath,
175 final AlignViewControllerGuiI gui)
179 gui.setStatus(MessageManager.getString("label.searching_vcf"));
188 VCFLoader.this.doLoad(filePath, gui);
195 * Loads VCF on to an alignment - provided it can be related to one or more
196 * sequence's chromosomal coordinates
200 * optional callback handler for messages
202 protected void doLoad(String filePath, AlignViewControllerGuiI gui)
204 VCFReader reader = null;
207 // long start = System.currentTimeMillis();
208 reader = new VCFReader(filePath);
210 header = reader.getFileHeader();
214 saveMetadata(sourceId);
217 * get offset of CSQ ALLELE_NUM and Feature if declared
221 VCFHeaderLine ref = header
222 .getOtherHeaderLine(VCFHeader.REFERENCE_KEY);
223 String vcfAssembly = ref.getValue();
229 * query for VCF overlapping each sequence in turn
231 for (SequenceI seq : al.getSequences())
233 int added = loadSequenceVCF(seq, reader, vcfAssembly);
238 transferAddedFeatures(seq);
243 // long elapsed = System.currentTimeMillis() - start;
244 String msg = MessageManager.formatMessage("label.added_vcf",
247 if (gui.getFeatureSettingsUI() != null)
249 gui.getFeatureSettingsUI().discoverAllFeatureData();
252 } catch (Throwable e)
254 System.err.println("Error processing VCF: " + e.getMessage());
258 gui.setStatus("Error occurred - see console for details");
267 } catch (IOException e)
276 * Reads metadata (such as INFO field descriptions and datatypes) and saves
277 * them for future reference
281 void saveMetadata(String theSourceId)
283 List<Pattern> vcfFieldPatterns = getFieldMatchers(VCF_FIELDS_PREF,
285 vcfFieldsOfInterest = new ArrayList<>();
287 FeatureSource metadata = new FeatureSource(theSourceId);
289 for (VCFInfoHeaderLine info : header.getInfoHeaderLines())
291 String attributeId = info.getID();
292 String desc = info.getDescription();
293 VCFHeaderLineType type = info.getType();
294 FeatureAttributeType attType = null;
298 attType = FeatureAttributeType.Character;
301 attType = FeatureAttributeType.Flag;
304 attType = FeatureAttributeType.Float;
307 attType = FeatureAttributeType.Integer;
310 attType = FeatureAttributeType.String;
313 metadata.setAttributeName(attributeId, desc);
314 metadata.setAttributeType(attributeId, attType);
316 if (isFieldWanted(attributeId, vcfFieldPatterns))
318 vcfFieldsOfInterest.add(attributeId);
322 FeatureSources.getInstance().addSource(theSourceId, metadata);
326 * Answers true if the field id is matched by any of the filter patterns, else
327 * false. Matching is against regular expression patterns, and is not
334 private boolean isFieldWanted(String id, List<Pattern> filters)
336 for (Pattern p : filters)
338 if (p.matcher(id.toUpperCase()).matches())
347 * Records 'wanted' fields defined in the CSQ INFO header (if there is one).
348 * Also records the position of selected fields (Allele, ALLELE_NUM, Feature)
349 * required for processing.
351 * CSQ fields are declared in the CSQ INFO Description e.g.
353 * Description="Consequence ...from ... VEP. Format: Allele|Consequence|...
355 protected void parseCsqHeader()
357 List<Pattern> vepFieldFilters = getFieldMatchers(VEP_FIELDS_PREF,
359 vepFieldsOfInterest = new HashMap<>();
361 VCFInfoHeaderLine csqInfo = header.getInfoHeaderLine(CSQ_FIELD);
368 * parse out the pipe-separated list of CSQ fields; we assume here that
369 * these form the last part of the description, and contain no spaces
371 String desc = csqInfo.getDescription();
372 int spacePos = desc.lastIndexOf(" ");
373 desc = desc.substring(spacePos + 1);
377 String[] format = desc.split(PIPE_REGEX);
379 for (String field : format)
381 if (ALLELE_NUM_KEY.equals(field))
383 csqAlleleNumberFieldIndex = index;
385 if (ALLELE_KEY.equals(field))
387 csqAlleleFieldIndex = index;
389 if (FEATURE_KEY.equals(field))
391 csqFeatureFieldIndex = index;
394 if (isFieldWanted(field, vepFieldFilters))
396 vepFieldsOfInterest.put(index, field);
405 * Reads the Preference value for the given key, with default specified if no
406 * preference set. The value is interpreted as a comma-separated list of
407 * regular expressions, and converted into a list of compiled patterns ready
408 * for matching. Patterns are forced to upper-case for non-case-sensitive
411 * This supports user-defined filters for fields of interest to capture while
412 * processing data. For example, VCF_FIELDS = AF,AC* would mean that VCF INFO
413 * fields with an ID of AF, or starting with AC, would be matched.
419 private List<Pattern> getFieldMatchers(String key, String def)
421 String pref = Cache.getDefault(key, def);
422 List<Pattern> patterns = new ArrayList<>();
423 String[] tokens = pref.split(",");
424 for (String token : tokens)
428 patterns.add(Pattern.compile(token.toUpperCase()));
429 } catch (PatternSyntaxException e)
431 System.err.println("Invalid pattern ignored: " + token);
438 * Transfers VCF features to sequences to which this sequence has a mapping.
439 * If the mapping is 3:1, computes peptide variants from nucleotide variants.
443 protected void transferAddedFeatures(SequenceI seq)
445 DBRefEntry[] dbrefs = seq.getDBRefs();
450 for (DBRefEntry dbref : dbrefs)
452 Mapping mapping = dbref.getMap();
453 if (mapping == null || mapping.getTo() == null)
458 SequenceI mapTo = mapping.getTo();
459 MapList map = mapping.getMap();
460 if (map.getFromRatio() == 3)
463 * dna-to-peptide product mapping
465 AlignmentUtils.computeProteinFeatures(seq, mapTo, map);
470 * nucleotide-to-nucleotide mapping e.g. transcript to CDS
472 List<SequenceFeature> features = seq.getFeatures()
473 .getPositionalFeatures(SequenceOntologyI.SEQUENCE_VARIANT);
474 for (SequenceFeature sf : features)
476 if (FEATURE_GROUP_VCF.equals(sf.getFeatureGroup()))
478 transferFeature(sf, mapTo, map);
486 * Tries to add overlapping variants read from a VCF file to the given
487 * sequence, and returns the number of variant features added. Note that this
488 * requires the sequence to hold information as to its species, chromosomal
489 * positions and reference assembly, in order to be able to map the VCF
490 * variants to the sequence (or not)
497 protected int loadSequenceVCF(SequenceI seq, VCFReader reader,
501 GeneLociI seqCoords = seq.getGeneLoci();
502 if (seqCoords == null)
504 System.out.println(String.format(
505 "Can't query VCF for %s as chromosome coordinates not known",
510 if (!vcfSpeciesMatchesSequence(vcfAssembly, seqCoords.getSpeciesId()))
515 List<int[]> seqChromosomalContigs = seqCoords.getMap().getToRanges();
516 for (int[] range : seqChromosomalContigs)
518 count += addVcfVariants(seq, reader, range, vcfAssembly);
525 * Answers true if the species inferred from the VCF reference identifier
526 * matches that for the sequence
532 boolean vcfSpeciesMatchesSequence(String vcfAssembly, String speciesId)
535 // there are many aliases for species - how to equate one with another?
537 // VCF ##reference header is an unstructured URI - how to extract species?
538 // perhaps check if ref includes any (Ensembl) alias of speciesId??
539 // TODO ask the user to confirm this??
541 if (vcfAssembly.contains("Homo_sapiens") // gnomAD exome data example
542 && "HOMO_SAPIENS".equals(speciesId)) // Ensembl species id
547 if (vcfAssembly.contains("c_elegans") // VEP VCF response example
548 && "CAENORHABDITIS_ELEGANS".equals(speciesId)) // Ensembl
553 // this is not a sustainable solution...
559 * Queries the VCF reader for any variants that overlap the given chromosome
560 * region of the sequence, and adds as variant features. Returns the number of
561 * overlapping variants found.
566 * start-end range of a sequence region in its chromosomal
569 * the '##reference' identifier for the VCF reference assembly
572 protected int addVcfVariants(SequenceI seq, VCFReader reader,
573 int[] range, String vcfAssembly)
575 GeneLociI seqCoords = seq.getGeneLoci();
577 String chromosome = seqCoords.getChromosomeId();
578 String seqRef = seqCoords.getAssemblyId();
579 String species = seqCoords.getSpeciesId();
582 * map chromosomal coordinates from sequence to VCF if the VCF
583 * data has a different reference assembly to the sequence
585 // TODO generalise for non-human species
586 // - or get the user to choose in a dialog
589 if ("GRCh38".equalsIgnoreCase(seqRef) // Ensembl
590 && vcfAssembly.contains("Homo_sapiens_assembly19")) // gnomAD
592 String toRef = "GRCh37";
593 int[] newRange = mapReferenceRange(range, chromosome, "human",
595 if (newRange == null)
597 System.err.println(String.format(
598 "Failed to map %s:%s:%s:%d:%d to %s", species, chromosome,
599 seqRef, range[0], range[1], toRef));
602 offset = newRange[0] - range[0];
606 boolean forwardStrand = range[0] <= range[1];
609 * query the VCF for overlaps
610 * (convert a reverse strand range to forwards)
613 MapList mapping = seqCoords.getMap();
615 int fromLocus = Math.min(range[0], range[1]);
616 int toLocus = Math.max(range[0], range[1]);
617 CloseableIterator<VariantContext> variants = reader.query(chromosome,
619 while (variants.hasNext())
622 * get variant location in sequence chromosomal coordinates
624 VariantContext variant = variants.next();
626 int start = variant.getStart() - offset;
627 int end = variant.getEnd() - offset;
630 * convert chromosomal location to sequence coordinates
631 * - may be reverse strand (convert to forward for sequence feature)
632 * - null if a partially overlapping feature
634 int[] seqLocation = mapping.locateInFrom(start, end);
635 if (seqLocation != null)
637 int featureStart = Math.min(seqLocation[0], seqLocation[1]);
638 int featureEnd = Math.max(seqLocation[0], seqLocation[1]);
639 count += addAlleleFeatures(seq, variant, featureStart, featureEnd,
650 * A convenience method to get the AF value for the given alternate allele
657 protected float getAlleleFrequency(VariantContext variant, int alleleIndex)
660 String attributeValue = getAttributeValue(variant,
661 ALLELE_FREQUENCY_KEY, alleleIndex);
662 if (attributeValue != null)
666 score = Float.parseFloat(attributeValue);
667 } catch (NumberFormatException e)
677 * A convenience method to get an attribute value for an alternate allele
680 * @param attributeName
684 protected String getAttributeValue(VariantContext variant,
685 String attributeName, int alleleIndex)
687 Object att = variant.getAttribute(attributeName);
689 if (att instanceof String)
693 else if (att instanceof ArrayList)
695 return ((List<String>) att).get(alleleIndex);
702 * Adds one variant feature for each allele in the VCF variant record, and
703 * returns the number of features added.
707 * @param featureStart
709 * @param forwardStrand
712 protected int addAlleleFeatures(SequenceI seq, VariantContext variant,
713 int featureStart, int featureEnd, boolean forwardStrand)
718 * Javadoc says getAlternateAlleles() imposes no order on the list returned
719 * so we proceed defensively to get them in strict order
721 int altAlleleCount = variant.getAlternateAlleles().size();
722 for (int i = 0; i < altAlleleCount; i++)
724 added += addAlleleFeature(seq, variant, i, featureStart, featureEnd,
731 * Inspects one allele and attempts to add a variant feature for it to the
732 * sequence. We extract as much as possible of the additional data associated
733 * with this allele to store in the feature's key-value map. Answers the
734 * number of features added (0 or 1).
738 * @param altAlleleIndex
740 * @param featureStart
742 * @param forwardStrand
745 protected int addAlleleFeature(SequenceI seq, VariantContext variant,
746 int altAlleleIndex, int featureStart, int featureEnd,
747 boolean forwardStrand)
749 String reference = variant.getReference().getBaseString();
750 Allele alt = variant.getAlternateAllele(altAlleleIndex);
751 String allele = alt.getBaseString();
754 * build the ref,alt allele description e.g. "G,A", using the base
755 * complement if the sequence is on the reverse strand
757 // TODO check how structural variants are shown on reverse strand
758 StringBuilder sb = new StringBuilder();
759 sb.append(forwardStrand ? reference : Dna.reverseComplement(reference));
761 sb.append(forwardStrand ? allele : Dna.reverseComplement(allele));
762 String alleles = sb.toString(); // e.g. G,A
764 String type = SequenceOntologyI.SEQUENCE_VARIANT;
765 float score = getAlleleFrequency(variant, altAlleleIndex);
767 SequenceFeature sf = new SequenceFeature(type, alleles, featureStart,
768 featureEnd, score, FEATURE_GROUP_VCF);
769 sf.setSource(sourceId);
771 sf.setValue(Gff3Helper.ALLELES, alleles);
773 addAlleleProperties(variant, seq, sf, altAlleleIndex);
775 seq.addSequenceFeature(sf);
781 * Add any allele-specific VCF key-value data to the sequence feature
786 * @param altAlelleIndex
789 protected void addAlleleProperties(VariantContext variant, SequenceI seq,
790 SequenceFeature sf, final int altAlelleIndex)
792 Map<String, Object> atts = variant.getAttributes();
794 for (Entry<String, Object> att : atts.entrySet())
796 String key = att.getKey();
799 * extract Consequence data (if present) that we are able to
800 * associated with the allele for this variant feature
802 if (CSQ_FIELD.equals(key))
804 addConsequences(variant, seq, sf, altAlelleIndex);
809 * filter out fields we don't want to capture
811 if (!vcfFieldsOfInterest.contains(key))
817 * we extract values for other data which are allele-specific;
818 * these may be per alternate allele (INFO[key].Number = 'A')
819 * or per allele including reference (INFO[key].Number = 'R')
821 VCFInfoHeaderLine infoHeader = header.getInfoHeaderLine(key);
822 if (infoHeader == null)
825 * can't be sure what data belongs to this allele, so
826 * play safe and don't take any
831 VCFHeaderLineCount number = infoHeader.getCountType();
832 int index = altAlelleIndex;
833 if (number == VCFHeaderLineCount.R)
836 * one value per allele including reference, so bump index
837 * e.g. the 3rd value is for the 2nd alternate allele
841 else if (number != VCFHeaderLineCount.A)
844 * don't save other values as not allele-related
850 * take the index'th value
852 String value = getAttributeValue(variant, key, index);
855 sf.setValue(key, value);
861 * Inspects CSQ data blocks (consequences) and adds attributes on the sequence
862 * feature for the current allele (and transcript if applicable)
864 * Allele matching: if field ALLELE_NUM is present, it must match
865 * altAlleleIndex. If not present, then field Allele value must match the VCF
868 * Transcript matching: if sequence name can be identified to at least one of
869 * the consequences' Feature values, then select only consequences that match
870 * the value (i.e. consequences for the current transcript sequence). If not,
871 * take all consequences (this is the case when adding features to the gene
877 * @param altAlelleIndex
880 protected void addConsequences(VariantContext variant, SequenceI seq,
881 SequenceFeature sf, int altAlelleIndex)
883 Object value = variant.getAttribute(CSQ_FIELD);
885 if (value == null || !(value instanceof ArrayList<?>))
890 List<String> consequences = (List<String>) value;
893 * if CSQ data includes 'Feature', and any value matches the sequence name,
894 * then restrict consequence data to only the matching value (transcript)
895 * i.e. just pick out consequences for the transcript the variant feature is on
897 String seqName = seq.getName()== null ? "" : seq.getName().toLowerCase();
898 String matchFeature = null;
899 if (csqFeatureFieldIndex > -1)
901 for (String consequence : consequences)
903 String[] csqFields = consequence.split(PIPE_REGEX);
904 if (csqFields.length > csqFeatureFieldIndex)
906 String featureIdentifier = csqFields[csqFeatureFieldIndex];
907 if (featureIdentifier.length() > 4
908 && seqName.indexOf(featureIdentifier.toLowerCase()) > -1)
910 matchFeature = featureIdentifier;
917 * inspect CSQ consequences; where possible restrict to the consequence
918 * associated with the current transcript (Feature)
920 SortedMap<String, String> csqValues = new TreeMap<>(
921 String.CASE_INSENSITIVE_ORDER);
923 for (String consequence : consequences)
925 String[] csqFields = consequence.split(PIPE_REGEX);
927 if (includeConsequence(csqFields, matchFeature, variant,
931 * inspect individual fields of this consequence, copying non-null
932 * values which are 'fields of interest'
935 for (String field : csqFields)
937 if (field != null && field.length() > 0)
939 String id = vepFieldsOfInterest.get(i);
942 csqValues.put(id, field);
950 if (!csqValues.isEmpty())
952 sf.setValue(CSQ_FIELD, csqValues);
957 * Answers true if we want to associate this block of consequence data with
958 * the specified alternate allele of the VCF variant.
960 * If consequence data includes the ALLELE_NUM field, then this has to match
961 * altAlleleIndex. Otherwise the Allele field of the consequence data has to
962 * match the allele value.
964 * Optionally (if matchFeature is not null), restrict to only include
965 * consequences whose Feature value matches. This allows us to attach
966 * consequences to their respective transcripts.
969 * @param matchFeature
971 * @param altAlelleIndex
975 protected boolean includeConsequence(String[] csqFields,
976 String matchFeature, VariantContext variant, int altAlelleIndex)
979 * check consequence is for the current transcript
981 if (matchFeature != null)
983 if (csqFields.length <= csqFeatureFieldIndex)
987 String featureIdentifier = csqFields[csqFeatureFieldIndex];
988 if (!featureIdentifier.equals(matchFeature))
990 return false; // consequence is for a different transcript
995 * if ALLELE_NUM is present, it must match altAlleleIndex
996 * NB first alternate allele is 1 for ALLELE_NUM, 0 for altAlleleIndex
998 if (csqAlleleNumberFieldIndex > -1)
1000 if (csqFields.length <= csqAlleleNumberFieldIndex)
1004 String alleleNum = csqFields[csqAlleleNumberFieldIndex];
1005 return String.valueOf(altAlelleIndex + 1).equals(alleleNum);
1009 * else consequence allele must match variant allele
1011 if (csqAlleleFieldIndex > -1 && csqFields.length > csqAlleleFieldIndex)
1013 String csqAllele = csqFields[csqAlleleFieldIndex];
1014 String vcfAllele = variant.getAlternateAllele(altAlelleIndex)
1016 return csqAllele.equals(vcfAllele);
1023 * A convenience method to complement a dna base and return the string value
1029 protected String complement(byte[] reference)
1031 return String.valueOf(Dna.getComplement((char) reference[0]));
1035 * Determines the location of the query range (chromosome positions) in a
1036 * different reference assembly.
1038 * If the range is just a subregion of one for which we already have a mapping
1039 * (for example, an exon sub-region of a gene), then the mapping is just
1040 * computed arithmetically.
1042 * Otherwise, calls the Ensembl REST service that maps from one assembly
1043 * reference's coordinates to another's
1046 * start-end chromosomal range in 'fromRef' coordinates
1050 * assembly reference for the query coordinates
1052 * assembly reference we wish to translate to
1053 * @return the start-end range in 'toRef' coordinates
1055 protected int[] mapReferenceRange(int[] queryRange, String chromosome,
1056 String species, String fromRef, String toRef)
1059 * first try shorcut of computing the mapping as a subregion of one
1060 * we already have (e.g. for an exon, if we have the gene mapping)
1062 int[] mappedRange = findSubsumedRangeMapping(queryRange, chromosome,
1063 species, fromRef, toRef);
1064 if (mappedRange != null)
1070 * call (e.g.) http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37
1072 EnsemblMap mapper = new EnsemblMap();
1073 int[] mapping = mapper.getAssemblyMapping(species, chromosome, fromRef,
1076 if (mapping == null)
1078 // mapping service failure
1083 * save mapping for possible future re-use
1085 String key = makeRangesKey(chromosome, species, fromRef, toRef);
1086 if (!assemblyMappings.containsKey(key))
1088 assemblyMappings.put(key, new HashMap<int[], int[]>());
1091 assemblyMappings.get(key).put(queryRange, mapping);
1097 * If we already have a 1:1 contiguous mapping which subsumes the given query
1098 * range, this method just calculates and returns the subset of that mapping,
1099 * else it returns null. In practical terms, if a gene has a contiguous
1100 * mapping between (for example) GRCh37 and GRCh38, then we assume that its
1101 * subsidiary exons occupy unchanged relative positions, and just compute
1102 * these as offsets, rather than do another lookup of the mapping.
1104 * If in future these assumptions prove invalid (e.g. for bacterial dna?!),
1105 * simply remove this method or let it always return null.
1107 * Warning: many rapid calls to the /map service map result in a 429 overload
1117 protected int[] findSubsumedRangeMapping(int[] queryRange, String chromosome,
1118 String species, String fromRef, String toRef)
1120 String key = makeRangesKey(chromosome, species, fromRef, toRef);
1121 if (assemblyMappings.containsKey(key))
1123 Map<int[], int[]> mappedRanges = assemblyMappings.get(key);
1124 for (Entry<int[], int[]> mappedRange : mappedRanges.entrySet())
1126 int[] fromRange = mappedRange.getKey();
1127 int[] toRange = mappedRange.getValue();
1128 if (fromRange[1] - fromRange[0] == toRange[1] - toRange[0])
1131 * mapping is 1:1 in length, so we trust it to have no discontinuities
1133 if (MappingUtils.rangeContains(fromRange, queryRange))
1136 * fromRange subsumes our query range
1138 int offset = queryRange[0] - fromRange[0];
1139 int mappedRangeFrom = toRange[0] + offset;
1140 int mappedRangeTo = mappedRangeFrom + (queryRange[1] - queryRange[0]);
1141 return new int[] { mappedRangeFrom, mappedRangeTo };
1150 * Transfers the sequence feature to the target sequence, locating its start
1151 * and end range based on the mapping. Features which do not overlap the
1152 * target sequence are ignored.
1155 * @param targetSequence
1157 * mapping from the feature's coordinates to the target sequence
1159 protected void transferFeature(SequenceFeature sf,
1160 SequenceI targetSequence, MapList mapping)
1162 int[] mappedRange = mapping.locateInTo(sf.getBegin(), sf.getEnd());
1164 if (mappedRange != null)
1166 String group = sf.getFeatureGroup();
1167 int newBegin = Math.min(mappedRange[0], mappedRange[1]);
1168 int newEnd = Math.max(mappedRange[0], mappedRange[1]);
1169 SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
1170 group, sf.getScore());
1171 targetSequence.addSequenceFeature(copy);
1176 * Formats a ranges map lookup key
1184 protected static String makeRangesKey(String chromosome, String species,
1185 String fromRef, String toRef)
1187 return species + EXCL + chromosome + EXCL + fromRef + EXCL