import {isSimpleType} from "./util/igvUtils.js" import {FeatureUtils, FileUtils, StringUtils} from "../node_modules/igv-utils/src/index.js" import {createCheckbox} from "./igv-icons.js" import {findFeatureAfterCenter} from "./feature/featureUtils.js" import {isLocalFile} from "./util/sessionResourceValidator.js" const fixColor = (colorString) => { if (StringUtils.isString(colorString)) { return (colorString.indexOf(",") > 0 && !(colorString.startsWith("rgb(") || colorString.startsWith("rgba("))) ? `rgb(${colorString})` : colorString } else { return colorString } } /** * A collection of properties and methods shared by all (or most) track types. * * @param config * @param browser * @constructor */ class TrackBase { static defaultColor = 'rgb(150,150,150)' static defaults = { height: 50, autoHeight: false, visibilityWindow: undefined, // Identifies property that should be copied from config color: undefined, // Identifies property that should be copied from config altColor: undefined, // Identifies property that should be copied from config supportHiDPI: true, selected: false } constructor(config, browser) { this.browser = browser this.init(config) } /** * Initialize track properties from the config object. This method is typically overriden in subclasses, which * will call this implementation as super.init(config). * * @param config */ init(config) { this.config = config if (config.displayMode) { config.displayMode = config.displayMode.toUpperCase() } // Base default settings const defaults = Object.assign({}, TrackBase.defaults) // Overide with class specific default settings if (this.constructor.defaults) { for (let key of Object.keys(this.constructor.defaults)) { defaults[key] = this.constructor.defaults[key] } } for (let key of Object.keys(defaults)) { this[key] = config.hasOwnProperty(key) ? config[key] : defaults[key] if ((key === 'color' || key === 'altColor') && this[key]) { this[key] = fixColor(this[key]) } } // this._initialColor = this.color || this.constructor.defaultColor // this._initialAltColor = this.altColor || this.constructor.defaultColor if (config.name || config.label) { this.name = config.name || config.label } else if (FileUtils.isFile(config.url)) { this.name = config.url.name } else if (StringUtils.isString(config.url) && !config.url.startsWith("data:")) { this.name = FileUtils.getFilename(config.url) } this.url = config.url if (this.config.type) this.type = this.config.type this.id = this.config.id === undefined ? this.name : this.config.id this.order = config.order this.autoscaleGroup = config.autoscaleGroup this.removable = config.removable === undefined ? true : config.removable // Defaults to true this.minHeight = config.minHeight || Math.min(25, this.height) this.maxHeight = config.maxHeight || Math.max(1000, this.height) if (config.onclick) { this.onclick = config.onclick config.onclick = undefined // functions cannot be saved in sessions, clear it here. } if (config.description) { // Override description -- displayed when clicking on track label. Convert to function if neccessary if (typeof config.description === 'function') { this.description = config.description } else { this.description = () => config.description } } } /** * Perform any async initialization steps. This method is typically overriden in subclasses, which will call * this implementation as super.postInit(). * * NOTE: postInit is called before the trackView is added to the browser track list. If postInit throws an error * the track will not be added to the browser. This is important for error handling during track loading. * * @returns {Promise} */ async postInit() { this._initialColor = this.color || this.constructor.defaultColor this._initialAltColor = this.altColor || this.constructor.defaultColor return this } get name() { return this._name } set name(name) { this._name = name if (this.trackView) { this.trackView.setTrackLabelName(name) } } clearCachedFeatures() { if (this.trackView) { this.trackView.clearCachedFeatures() } } updateViews() { if (this.trackView) { this.trackView.updateViews() } } repaintViews() { if (this.trackView) { this.trackView.repaintViews() } } /** * Used to create session object for bookmarking, sharing. Only simple property values (string, number, boolean) * are saved. */ getState() { const isJSONable = item => !(item === undefined || typeof item === 'function' || item instanceof Promise) // Create copy of config, minus transient properties (convention is name starts with '_'). Also, all // function properties are transient as they cannot be saved in json const state = {} const jsonableConfigKeys = Object.keys(this.config).filter(key => isJSONable(this.config[key])) for (const key of jsonableConfigKeys) { if (!key.startsWith("_")) { state[key] = this.config[key] } } // Update original config values with any changes for (let key of Object.keys(state)) { if (key.startsWith("_")) continue // transient property const value = this[key] if (value !== undefined && (isSimpleType(value) || typeof value === "boolean" || key === "metadata")) { state[key] = value } } // If user has changed other properties from defaults update state. const defs = Object.assign({}, TrackBase.defaults) if (this.constructor.defaults) { for (let key of Object.keys(this.constructor.defaults)) { defs[key] = this.constructor.defaults[key] } } for (let key of Object.keys(defs)) { if (undefined !== this[key] && defs[key] !== this[key]) { state[key] = this[key] } } // Flatten dataRange if present if (!this.autoscale && this.dataRange) { state.min = this.dataRange.min state.max = this.dataRange.max } if (this.autoscaleGroup) { state.autoscaleGroup = this.autoscaleGroup } return state } get supportsWholeGenome() { return this.config.supportsWholeGenome === true } /** * Does the track support sample names. Current sample aware tracks include VCF (with genotypes), MUT, MAF, and SEG * @returns {boolean} */ hasSamples() { return false } getGenomeId() { return this.browser.genome ? this.browser.genome.id : undefined } /** * Set certain track properties, usually from a "track" line. Not all UCSC properties are supported. * * Track configuration settings have precendence over track line properties, so if both are present ignore the * track line. * * @param properties */ setTrackProperties(properties) { if (this.disposed) return // This track was removed during async load const tracklineConfg = {} let tokens for (let key of Object.keys(properties)) { switch (key.toLowerCase()) { case "usescore": tracklineConfg.useScore = ( properties[key] === 1 || properties[key] === "1" || properties[key] === "on" || properties[key] === true) break case "visibility": //0 - hide, 1 - dense, 2 - full, 3 - pack, and 4 - squish switch (properties[key]) { case "2": case "3": case "pack": case "full": tracklineConfg.displayMode = "EXPANDED" break case "4": case "squish": tracklineConfg.displayMode = "SQUISHED" break case "1": case "dense": tracklineConfg.displayMode = "COLLAPSED" } break case "color": case "altcolor": tracklineConfg[key] = properties[key].startsWith("rgb(") ? properties[key] : "rgb(" + properties[key] + ")" break case "featurevisiblitywindow": case "visibilitywindow": tracklineConfg.visibilityWindow = Number.parseInt(properties[key]) break case "maxheightpixels": tokens = properties[key].split(":") if (tokens.length === 3) { tracklineConfg.minHeight = Number.parseInt(tokens[2]) tracklineConfg.height = Number.parseInt(tokens[1]) tracklineConfg.maxHeight = Number.parseInt(tokens[0]) } break case "viewlimits": if (!this.config.autoscale && !this.config.max) { //config has precedence tokens = properties[key].split(":") let min = 0 let max if (tokens.length == 1) { max = Number(tokens[0]) } else if (tokens.length == 2) { min = Number(tokens[0]) max = Number(tokens[1]) } if (Number.isNaN(max) || Number.isNaN(min)) { console.warn(`Unexpected viewLimits value in track line: ${properties[key]}`) } else { tracklineConfg.autoscale = false tracklineConfg.dataRange = {min, max} this.viewLimitMin = min this.viewLimitMax = max } } break case "name": tracklineConfg[key] = properties[key] break case "url": tracklineConfg["infoURL"] = properties[key] break case "type": const v = properties[key] if (UCSCTypeMappings.has(v)) { tracklineConfg[key] = UCSCTypeMappings.get(v) } else { tracklineConfg[key] = v } break case "graphtype": tracklineConfg["graphType"] = properties[key] break default: tracklineConfg[key] = properties[key] } } // Track configuration objects have precedence over track line properties in general. The "name" property // is an exception if it was derived and not explicitly entered (that is derived from the web app from filename). for (let key of Object.keys(tracklineConfg)) { if (!this.config.hasOwnProperty(key) || (key === "name" && this.config._derivedName)) { let value = tracklineConfg[key] if ("true" === value) value = true if ("false" === value) value = false this[key] = value if (key === "height" && this.trackView) { try { const h = Number.parseInt(value) this.trackView.setTrackHeight(h) } catch (e) { console.error(e) } } } } } /** * Return the features clicked over. Default implementation assumes an array of features and only considers * the genomic location. Overriden by most subclasses. * * @param clickState * @returns {[]|*[]} */ clickedFeatures(clickState) { // We use the cached features rather than method to avoid async load. If the // feature is not already loaded this won't work, but the user wouldn't be mousing over it either. const features = clickState.viewport.cachedFeatures if (!features || !Array.isArray(features) || features.length === 0) { return [] } const genomicLocation = clickState.genomicLocation // When zoomed out we need some tolerance around genomicLocation const tolerance = (clickState.referenceFrame.bpPerPixel > 0.2) ? 3 * clickState.referenceFrame.bpPerPixel : 0.2 const ss = genomicLocation - tolerance const ee = genomicLocation + tolerance return (FeatureUtils.findOverlapping(features, ss, ee)) } /** * Default popup text function -- just extracts string and number properties in random order. * @param feature * @returns {Array} */ extractPopupData(feature, genomeId) { const filteredProperties = new Set(['row', 'color', 'chr', 'start', 'end', 'cdStart', 'cdEnd', 'strand', 'alpha', 'implicitExons']) const data = [] let alleles, alleleFreqs for (let property in feature) { if (feature.hasOwnProperty(property) && !filteredProperties.has(property) && isSimpleType(feature[property])) { let value = feature[property] data.push({name: StringUtils.capitalize(property), value: value}) if (property === "alleles") { alleles = feature[property] } else if (property === "alleleFreqs") { alleleFreqs = feature[property] } } } if (alleles && alleleFreqs) { if (alleles.endsWith(",")) { alleles = alleles.substr(0, alleles.length - 1) } if (alleleFreqs.endsWith(",")) { alleleFreqs = alleleFreqs.substr(0, alleleFreqs.length - 1) } let a = alleles.split(",") let af = alleleFreqs.split(",") if (af.length > 1) { let b = [] for (let i = 0; i < af.length; i++) { b.push({a: a[i], af: Number(af[i])}) } b.sort(function (x, y) { return x.af - y.af }) let ref = b[b.length - 1].a if (ref.length === 1) { for (let i = b.length - 2; i >= 0; i--) { let alt = b[i].a if (alt.length === 1) { if (!genomeId) genomeId = this.getGenomeId() const cravatLink = TrackBase.getCravatLink(feature.chr, feature.start + 1, ref, alt, genomeId) console.log(cravatLink) if (cravatLink) { data.push('
') data.push({html: cravatLink}) data.push('
') } } } } } } if (feature.attributes) { for (let key of Object.keys(feature.attributes)) { data.push({name: key, value: feature.attributes[key]}) } } // final chr position let posString = `${feature.chr}:${StringUtils.numberFormatter(feature.start + 1)}-${StringUtils.numberFormatter(feature.end)}` if (feature.strand) { posString += ` (${feature.strand})` } data.push({name: 'Location', value: posString}) return data } /** * Default track description -- displayed on click of track label. This can be overriden in the track * configuration, or in subclasses. */ description() { const createKeyValueRow = (key, value) => { const row = document.createElement('div') row.className = 'igv-track-label-popover__row' const keySpan = document.createElement('span') keySpan.className = 'igv-track-label-popover__key' keySpan.textContent = key + ':' const valueSpan = document.createElement('span') valueSpan.className = 'igv-track-label-popover__value' valueSpan.textContent = value row.appendChild(keySpan) row.appendChild(valueSpan) return row } const fragment = document.createDocumentFragment() if (this.url) { if (FileUtils.isFile(this.url)) { fragment.appendChild(createKeyValueRow('Filename', this.url.name)) } else { fragment.appendChild(createKeyValueRow('URL', this.url)) } } else { // If no URL, just return the name as a simple text node const nameDiv = document.createElement('div') nameDiv.className = 'igv-track-label-popover__row' nameDiv.textContent = this.name fragment.appendChild(nameDiv) return fragment } if (this.config) { if (this.config.metadata) { for (let key of Object.keys(this.config.metadata)) { const value = this.config.metadata[key] fragment.appendChild(createKeyValueRow(key, value)) } } // Add any config properties that are capitalized for (let key of Object.keys(this.config)) { if (key.startsWith("_")) continue // transient property let first = key.substr(0, 1) if (first !== first.toLowerCase()) { const value = this.config[key] if (value && isSimpleType(value)) { fragment.appendChild(createKeyValueRow(key, value)) } } } } return fragment } /** * Return color for a specific feature of this track. This default implementation is overriden by subclasses* * @param f * @returns {*|string|string} */ getColorForFeature(f) { return (typeof this.color === "function") ? this.color(f) : this.color } numericDataMenuItems() { const menuItems = [] // Data range or color scale if ("heatmap" !== this.graphType) { menuItems.push('
') function dialogPresentationHandler(e) { if (this.trackView.track.selected) { this.browser.dataRangeDialog.configure(this.trackView.browser.getSelectedTrackViews()) } else { this.browser.dataRangeDialog.configure(this.trackView) } this.browser.dataRangeDialog.present(e) } menuItems.push({label: 'Set data range', dialog: dialogPresentationHandler}) if (this.logScale !== undefined) { function logScaleHandler() { this.logScale = !this.logScale this.trackView.repaintViews() } menuItems.push({element: createCheckbox("Log scale", this.logScale), click: logScaleHandler}) } function autoScaleHandler() { this.autoscaleGroup = undefined this.autoscale = !this.autoscale this.browser.updateViews() } menuItems.push({element: createCheckbox("Autoscale", this.autoscale), click: autoScaleHandler}) } return menuItems } setDataRange({min, max}) { this.dataRange = {min, max} this.autoscale = false this.autoscaleGroup = undefined this.trackView.repaintViews() } /** * Return the first feature in this track whose start position is > position * @param chr * @param position * @param direction * @returns {Promise} */ async nextFeatureAfter(chr, position, direction) { const viewport = this.trackView.viewports[0] let features = viewport.cachedFeatures if (features && Array.isArray(features) && features.length > 0) { // Check chromosome, all cached features will share a chromosome const chrName = this.browser.genome.getChromosomeName(features[0].chr) if (chrName === chr) { const next = findFeatureAfterCenter(features, position, direction) if (next) { return next } } } if (typeof this.featureSource.nextFeature === 'function') { return this.featureSource.nextFeature(chr, position, direction, this.visibilityWindow) } } /** * Track has been permanently removed. Release resources and other cleanup */ dispose() { this.disposed = true // This should not be neccessary, but in case there is some unknown reference holding onto this track object, // for example in client code, release any resources here. for (let key of Object.keys(this)) { this[key] = undefined } } static getCravatLink(chr, position, ref, alt, genomeID) { if ("hg38" === genomeID || "GRCh38" === genomeID) { const cravatChr = chr.startsWith("chr") ? chr : "chr" + chr return `Cravat ${ref}->${alt}` } else { return undefined } } /** * Prepare a track configuration for session serialization by identifying and marking * problematic resources (local files). * * Local files are converted to {file: filename} or {indexFile: filename} * Google Drive URLs are kept in the url/indexURL fields as-is and detected when loading * * This allows the configuration to be serialized while preserving information * about resources that cannot be automatically loaded when the session is restored. * * @param {Object} config - Track configuration to prepare * @returns {Object} Cleaned configuration with problematic resources marked */ static prepareConfigForSession(config) { const cooked = Object.assign({}, config) const lut = { url: 'file', indexURL: 'indexFile' } // Check for local File objects and convert to filename strings for (const key of ['url', 'indexURL']) { if (cooked[key] && isLocalFile(cooked[key])) { cooked[lut[key]] = cooked[key].name delete cooked[key] } } return cooked } // Methods to support filtering api set filter(f) { this._filter = f this.trackView.repaintViews() } /** * Return features visible in current viewports. * * @returns {*[]} */ getInViewFeatures() { const inViewFeatures = [] for (let viewport of this.trackView.viewports) { if (viewport.isVisible()) { const referenceFrame = viewport.referenceFrame const chr = referenceFrame.chr const start = referenceFrame.start const end = start + referenceFrame.toBP(viewport.getWidth()) // We use the cached features to avoid async load. If the // feature is not already loaded it is by definition not in view. if (viewport.cachedFeatures) { const viewFeatures = FeatureUtils.findOverlapping(viewport.cachedFeatures, start, end) for (let f of viewFeatures) { if (!this._filter || this._filter(f)) { inViewFeatures.push(f) } } } } } return inViewFeatures } /** * Return an object enumerating filterable attributes, that is attributes that can be used to filter features. * The base implementation returns an empty object, overriden in subclasses. * * @returns {{}} */ getFilterableAttributes() { return {} } } /** * Map UCSC track line "type" setting to file format. In igv.js "type" refers to the track type, not the input file format * @type {Map} */ const UCSCTypeMappings = new Map([ ["wiggle_0", "wig"], ["bed", "bed"], ["bigBed", "bigBed"], ["bigWig", "bigWig"] ]) export default TrackBase