--- /dev/null
+package jalview.datamodel;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import jalview.bin.Console;
+import jalview.ext.ensembl.EnsemblMap;
+import jalview.io.vcf.VCFLoader;
+import jalview.util.MapList;
+import jalview.util.MappingUtils;
+
+/**
+ * Holds mappings between gemomic assemblies Lazily populated as required from
+ * Ensembl Liftover and other datasources
+ *
+ * @author gmungoc
+ *
+ */
+public class GenomicAssemblies
+{
+
+ /*
+ * mappings between VCF and sequence reference assembly regions, as
+ * key = "species!chromosome!fromAssembly!toAssembly
+ * value = Map{fromRange, toRange}
+ */
+ private static Map<String, Map<int[], int[]>> assemblyMappings = new HashMap<>();
+
+ /*
+ * internal delimiter used to build keys for assemblyMappings
+ *
+ */
+ private static final String EXCL = "!";
+
+ /**
+ * Formats a ranges map lookup key
+ *
+ * @param chromosome
+ * @param species
+ * @param fromRef
+ * @param toRef
+ * @return
+ */
+ private static String makeRangesKey(String chromosome, String species,
+ String fromRef, String toRef)
+ {
+ return species + EXCL + chromosome + EXCL + fromRef + EXCL + toRef;
+ }
+
+ /**
+ * Determines the location of the query range (chromosome positions) in a
+ * different reference assembly.
+ * <p>
+ * If the range is just a subregion of one for which we already have a mapping
+ * (for example, an exon sub-region of a gene), then the mapping is just
+ * computed arithmetically.
+ * <p>
+ * Otherwise, calls the Ensembl REST service that maps from one assembly
+ * reference's coordinates to another's
+ *
+ * @param queryRange
+ * start-end chromosomal range in 'fromRef' coordinates
+ * @param chromosome
+ * @param species
+ * @param fromRef
+ * assembly reference for the query coordinates
+ * @param toRef
+ * assembly reference we wish to translate to
+ * @return the start-end range in 'toRef' coordinates
+ */
+ private static int[] mapReferenceRange(int[] queryRange,
+ String chromosome, String species, String fromRef, String toRef)
+ {
+ /*
+ * first try shorcut of computing the mapping as a subregion of one
+ * we already have (e.g. for an exon, if we have the gene mapping)
+ */
+ int[] mappedRange = findSubsumedRangeMapping(queryRange, chromosome,
+ species, fromRef, toRef);
+ if (mappedRange != null)
+ {
+ return mappedRange;
+ }
+
+ /*
+ * call (e.g.) http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37
+ */
+ EnsemblMap mapper = new EnsemblMap();
+ int[] mapping = mapper.getAssemblyMapping(species, chromosome, fromRef,
+ toRef, queryRange);
+
+ if (mapping == null)
+ {
+ // mapping service failure
+ return null;
+ }
+
+ /*
+ * save mapping for possible future re-use
+ */
+ String key = GenomicAssemblies.makeRangesKey(chromosome, species,
+ fromRef, toRef);
+ if (!assemblyMappings.containsKey(key))
+ {
+ assemblyMappings.put(key, new HashMap<int[], int[]>());
+ }
+
+ assemblyMappings.get(key).put(queryRange, mapping);
+
+ return mapping;
+ }
+
+ /**
+ * If we already have a 1:1 contiguous mapping which subsumes the given query
+ * range, this method just calculates and returns the subset of that mapping,
+ * else it returns null. In practical terms, if a gene has a contiguous
+ * mapping between (for example) GRCh37 and GRCh38, then we assume that its
+ * subsidiary exons occupy unchanged relative positions, and just compute
+ * these as offsets, rather than do another lookup of the mapping.
+ * <p>
+ * If in future these assumptions prove invalid (e.g. for bacterial dna?!),
+ * simply remove this method or let it always return null.
+ * <p>
+ * Warning: many rapid calls to the /map service map result in a 429 overload
+ * error response
+ *
+ * @param queryRange
+ * @param chromosome
+ * @param species
+ * @param fromRef
+ * @param toRef
+ * @return
+ */
+ private static int[] findSubsumedRangeMapping(int[] queryRange,
+ String chromosome, String species, String fromRef, String toRef)
+ {
+ String key = GenomicAssemblies.makeRangesKey(chromosome, species,
+ fromRef, toRef);
+ if (assemblyMappings.containsKey(key))
+ {
+ Map<int[], int[]> mappedRanges = assemblyMappings.get(key);
+ for (Entry<int[], int[]> mappedRange : mappedRanges.entrySet())
+ {
+ int[] fromRange = mappedRange.getKey();
+ int[] toRange = mappedRange.getValue();
+ if (fromRange[1] - fromRange[0] == toRange[1] - toRange[0])
+ {
+ /*
+ * mapping is 1:1 in length, so we trust it to have no discontinuities
+ */
+ if (MappingUtils.rangeContains(fromRange, queryRange))
+ {
+ /*
+ * fromRange subsumes our query range
+ */
+ int offset = queryRange[0] - fromRange[0];
+ int mappedRangeFrom = toRange[0] + offset;
+ int mappedRangeTo = mappedRangeFrom
+ + (queryRange[1] - queryRange[0]);
+ return new int[] { mappedRangeFrom, mappedRangeTo };
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * query Ensembl to map chromosomal coordinates between different
+ * assemblies<br>
+ * <em>will most likely fail for species other than human</em>
+ */
+ public static MapList mapAssemblyFor(String localAssembly, String species,
+ MapList map, String chromosome, String vcfAssembly)
+ {
+ List<int[]> toVcfRanges = new ArrayList<>();
+ List<int[]> fromSequenceRanges = new ArrayList<>();
+
+ for (int[] range : map.getToRanges())
+ {
+ int[] fromRange = map.locateInFrom(range[0], range[1]);
+ if (fromRange == null)
+ {
+ // corrupted map?!?
+ continue;
+ }
+
+ int[] newRange = mapReferenceRange(range, chromosome, species, localAssembly,
+ vcfAssembly);
+ if (newRange == null)
+ {
+ Console.error(String.format("Failed to map %s:%s:%s:%d:%d to %s",
+ species, chromosome, localAssembly, range[0], range[1],
+ vcfAssembly));
+ continue;
+ }
+ else
+ {
+ toVcfRanges.add(newRange);
+ fromSequenceRanges.add(fromRange);
+ }
+ }
+
+ return new MapList(fromSequenceRanges, toVcfRanges, 1, 1);
+ }
+
+ /**
+ * get a maplist for positions in the given assembly for the given locus
+ * @param locus
+ * @param assembly
+ * @return
+ */
+ public static MapList mapAssemblyFor(GeneLociI locus, String assembly)
+ {
+ return mapAssemblyFor(assembly,locus.getSpeciesId(),locus.getMapping(),locus.getChromosomeId(),locus.getAssemblyId());
+ }
+
+}
import jalview.bin.Console;
import jalview.datamodel.DBRefEntry;
import jalview.datamodel.GeneLociI;
+import jalview.datamodel.GenomicAssemblies;
import jalview.datamodel.Mapping;
import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceI;
private static final String FEATURE_GROUP_VCF = "VCF";
/*
- * internal delimiter used to build keys for assemblyMappings
- *
- */
- private static final String EXCL = "!";
-
- /*
* the VCF file we are processing
*/
protected String vcfFilePath;
- /*
- * mappings between VCF and sequence reference assembly regions, as
- * key = "species!chromosome!fromAssembly!toAssembly
- * value = Map{fromRange, toRange}
- */
- private Map<String, Map<int[], int[]>> assemblyMappings;
-
private VCFReader reader;
/*
jalview.bin.Console
.errPrintln("Error opening VCF file: " + e.getMessage());
}
-
- // map of species!chromosome!fromAssembly!toAssembly to {fromRange, toRange}
- assemblyMappings = new HashMap<>();
}
/**
return new VCFMap(chromosome, map);
}
- /*
- * VCF data has a different reference assembly to the sequence:
- * query Ensembl to map chromosomal coordinates from sequence to VCF
- */
- List<int[]> toVcfRanges = new ArrayList<>();
- List<int[]> fromSequenceRanges = new ArrayList<>();
-
- for (int[] range : map.getToRanges())
- {
- int[] fromRange = map.locateInFrom(range[0], range[1]);
- if (fromRange == null)
- {
- // corrupted map?!?
- continue;
- }
-
- int[] newRange = mapReferenceRange(range, chromosome, "human", seqRef,
- vcfAssembly);
- if (newRange == null)
- {
- Console.error(String.format("Failed to map %s:%s:%s:%d:%d to %s",
- species, chromosome, seqRef, range[0], range[1],
- vcfAssembly));
- continue;
- }
- else
- {
- toVcfRanges.add(newRange);
- fromSequenceRanges.add(fromRange);
- }
- }
-
- return new VCFMap(chromosome,
- new MapList(fromSequenceRanges, toVcfRanges, 1, 1));
+ return new VCFMap(chromosome,GenomicAssemblies.mapAssemblyFor(seqRef,"human",map,chromosome,vcfAssembly));
}
-
/**
* If the sequence id matches a contig declared in the VCF file, and the
* sequence length matches the contig length, then returns a 1:1 map of the
}
/**
- * Determines the location of the query range (chromosome positions) in a
- * different reference assembly.
- * <p>
- * If the range is just a subregion of one for which we already have a mapping
- * (for example, an exon sub-region of a gene), then the mapping is just
- * computed arithmetically.
- * <p>
- * Otherwise, calls the Ensembl REST service that maps from one assembly
- * reference's coordinates to another's
- *
- * @param queryRange
- * start-end chromosomal range in 'fromRef' coordinates
- * @param chromosome
- * @param species
- * @param fromRef
- * assembly reference for the query coordinates
- * @param toRef
- * assembly reference we wish to translate to
- * @return the start-end range in 'toRef' coordinates
- */
- protected int[] mapReferenceRange(int[] queryRange, String chromosome,
- String species, String fromRef, String toRef)
- {
- /*
- * first try shorcut of computing the mapping as a subregion of one
- * we already have (e.g. for an exon, if we have the gene mapping)
- */
- int[] mappedRange = findSubsumedRangeMapping(queryRange, chromosome,
- species, fromRef, toRef);
- if (mappedRange != null)
- {
- return mappedRange;
- }
-
- /*
- * call (e.g.) http://rest.ensembl.org/map/human/GRCh38/17:45051610..45109016:1/GRCh37
- */
- EnsemblMap mapper = new EnsemblMap();
- int[] mapping = mapper.getAssemblyMapping(species, chromosome, fromRef,
- toRef, queryRange);
-
- if (mapping == null)
- {
- // mapping service failure
- return null;
- }
-
- /*
- * save mapping for possible future re-use
- */
- String key = makeRangesKey(chromosome, species, fromRef, toRef);
- if (!assemblyMappings.containsKey(key))
- {
- assemblyMappings.put(key, new HashMap<int[], int[]>());
- }
-
- assemblyMappings.get(key).put(queryRange, mapping);
-
- return mapping;
- }
-
- /**
- * If we already have a 1:1 contiguous mapping which subsumes the given query
- * range, this method just calculates and returns the subset of that mapping,
- * else it returns null. In practical terms, if a gene has a contiguous
- * mapping between (for example) GRCh37 and GRCh38, then we assume that its
- * subsidiary exons occupy unchanged relative positions, and just compute
- * these as offsets, rather than do another lookup of the mapping.
- * <p>
- * If in future these assumptions prove invalid (e.g. for bacterial dna?!),
- * simply remove this method or let it always return null.
- * <p>
- * Warning: many rapid calls to the /map service map result in a 429 overload
- * error response
- *
- * @param queryRange
- * @param chromosome
- * @param species
- * @param fromRef
- * @param toRef
- * @return
- */
- protected int[] findSubsumedRangeMapping(int[] queryRange,
- String chromosome, String species, String fromRef, String toRef)
- {
- String key = makeRangesKey(chromosome, species, fromRef, toRef);
- if (assemblyMappings.containsKey(key))
- {
- Map<int[], int[]> mappedRanges = assemblyMappings.get(key);
- for (Entry<int[], int[]> mappedRange : mappedRanges.entrySet())
- {
- int[] fromRange = mappedRange.getKey();
- int[] toRange = mappedRange.getValue();
- if (fromRange[1] - fromRange[0] == toRange[1] - toRange[0])
- {
- /*
- * mapping is 1:1 in length, so we trust it to have no discontinuities
- */
- if (MappingUtils.rangeContains(fromRange, queryRange))
- {
- /*
- * fromRange subsumes our query range
- */
- int offset = queryRange[0] - fromRange[0];
- int mappedRangeFrom = toRange[0] + offset;
- int mappedRangeTo = mappedRangeFrom
- + (queryRange[1] - queryRange[0]);
- return new int[] { mappedRangeFrom, mappedRangeTo };
- }
- }
- }
- }
- return null;
- }
-
- /**
* Transfers the sequence feature to the target sequence, locating its start
* and end range based on the mapping. Features which do not overlap the
* target sequence are ignored.
targetSequence.addSequenceFeature(copy);
}
}
-
- /**
- * Formats a ranges map lookup key
- *
- * @param chromosome
- * @param species
- * @param fromRef
- * @param toRef
- * @return
- */
- protected static String makeRangesKey(String chromosome, String species,
- String fromRef, String toRef)
- {
- return species + EXCL + chromosome + EXCL + fromRef + EXCL + toRef;
- }
}
import jalview.datamodel.ContiguousI;
import jalview.datamodel.HiddenColumns;
import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SearchResultMatchI;
import jalview.datamodel.SearchResults;
import jalview.datamodel.SearchResultsI;
import jalview.datamodel.SequenceI;
{
seqPos = seq.findPosition(indexpos);
}
+
+ // precompute so we can also relay structure highlights
+ if (results == null)
+ {
+ results = MappingUtils.buildSearchResults(seq, seqPos,
+ seqmappings);
+ }
+ if (handlingVamsasMo)
+ {
+ results.addResult(seq, seqPos, seqPos);
+
+ }
+
for (int i = 0; i < listeners.size(); i++)
{
Object listener = listeners.elementAt(i);
}
if (listener instanceof StructureListener)
{
- highlightStructure((StructureListener) listener, seq, seqPos);
+ StructureListener sl = (StructureListener) listener;
+ // TODO: consider merging highlightStructure variants second call
+ // functionally same as first if seq/seqPos is part of the searchResults
+ if (highlightStructure(sl, seq, seqPos)==0 && relaySeqMappings)
+ {
+ // structure highlights for mapped sequences
+ highlightStructure(sl,results);
+ }
}
else
{
{
if (relaySeqMappings)
{
- if (results == null)
- {
- results = MappingUtils.buildSearchResults(seq, seqPos,
- seqmappings);
- }
- if (handlingVamsasMo)
- {
- results.addResult(seq, seqPos, seqPos);
-
- }
if (!results.isEmpty())
{
seqListener.highlightSequence(results);
}
/**
+ * highlights positions in a structure viewer corresponding to one or more positions on sequences
+ * @param sl
+ * @param searchResults
+ * @return 0 or number of structure regions highlighted
+ */
+ public int highlightStructure(StructureListener sl, SearchResultsI searchResults)
+ {
+ int atomNo;
+ List<AtomSpec> atoms = new ArrayList<>();
+
+ for (SearchResultMatchI sr: searchResults.getResults())
+ {
+ SequenceI seq = sr.getSequence();
+ for (StructureMapping sm : mappings)
+ {
+ if (sm.sequence == seq || sm.sequence == seq.getDatasetSequence()
+ || (sm.sequence.getDatasetSequence() != null
+ && (sm.sequence.getDatasetSequence() == seq
+ || sm.sequence.getDatasetSequence() == seq
+ .getDatasetSequence())))
+ {
+ for (int index=sr.getStart();index<=sr.getEnd();index++)
+ {
+ atomNo = sm.getAtomNum(index);
+
+ if (atomNo > 0)
+ {
+ atoms.add(new AtomSpec(sm.pdbfile, sm.pdbchain,
+ sm.getPDBResNum(index), atomNo));
+ }
+ }
+ }
+ }
+ }
+ sl.highlightAtoms(atoms);
+ return atoms.size();
+ }
+ /**
* Send suitable messages to a StructureListener to highlight atoms
* corresponding to the given sequence position(s)
*
* @param sl
* @param seq
* @param positions
+ * @return 0 if no atoms identified for position(s)
*/
- public void highlightStructure(StructureListener sl, SequenceI seq,
+ public int highlightStructure(StructureListener sl, SequenceI seq,
int... positions)
{
if (!sl.isListeningFor(seq))
{
- return;
+ return 0;
}
int atomNo;
List<AtomSpec> atoms = new ArrayList<>();
}
}
sl.highlightAtoms(atoms);
+ return atoms.size();
}
public void highlightStructureRegionsFor(StructureListener sl,