<commit_before>/*
    Custom code used to power credible sets demonstration example. This is not part of the core LocusZoom library,
    but can be included as a standalone file.

    The page must incorporate and load all libraries before this file can be used, including:
     - Vendor assets
     - LocusZoom
     - gwas-credible-sets (available via NPM or a related CDN)
*/
'use strict';

// This is defined as a UMD module, to work with multiple different module systems / bundlers
// Arcane build note: everything defined here gets registered globally. This is not a "pure" module, and some build
//  systems may require being told that this file has side effects.
/* global define, module, require, Promise */

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['locuszoom', 'gwas-credible-sets'] , function(LocusZoom, gwasCredibleSets) {  // amd
            return factory(LocusZoom, gwasCredibleSets);
        });
    } else if(typeof module === 'object' && module.exports) {  // commonJS
        module.exports = factory(require('locuszoom'), require('gwas-credible-sets'));
    } else {  // globals
        if (!root.LocusZoom.ext.Data) {
            root.LocusZoom.ext.Data = {};
        }
        root.LocusZoom.ext.Data.CredibleSetLZ = factory(root.LocusZoom, root.gwasCredibleSets);
    }
}(this, function(LocusZoom, gwasCredibleSets) {
    /**
     * Custom data source that calculates the 95% credible set based on provided data.
     * This source must be requested as the second step in a chain, after a previous step that returns fields required
     *  for the calculation.
     *
     * @param {Object} init.params
     * @param {Object} init.params.fields
     * @param {String} init.params.fields.log_pvalue The name of the field containing pvalue information
     * @param {Number} [init.params.threshold=0.95] The credible set threshold (eg 95%)
     *
     * @class
     * @public
     * @augments LocusZoom.Data.Source
     */
    var CredibleSetLZ = LocusZoom.Data.Source.extend(function(init) {
        this.parseInit(init);
        this.enableCache = true;
        this.dependentSource = true; // Don't do calcs for a region with no assoc data
    }, 'CredibleSetLZ');

    CredibleSetLZ.prototype.parseInit = function (init) {
        this.params = init.params;
        if (!(this.params.fields && this.params.fields.log_pvalue)) {
            throw new Error('Source config for ' + this.constructor.SOURCE_NAME + " must specify how to find 'fields.log_pvalue'");
        }
        if (!this.params.threshold) {
            this.params.threshold = 0.95;
        }
    };

    CredibleSetLZ.prototype.getCacheKey = function(state, chain, fields) {
        var threshold = state.credible_set_threshold || this.params.threshold;
        return [ threshold, state.chr, state.start, state.end ].join('_');
    };

    CredibleSetLZ.prototype.fetchRequest = function (state, chain) {
        var self = this;
        // The threshold can be overridden dynamically via `plot.state`, or set when the source is created
        var threshold = state.credible_set_threshold || this.params.threshold;
        // Calculate raw bayes factors and posterior probabilities based on information returned from the API
        if (!chain.body[0][self.params.fields.log_pvalue]) {
            throw new Error('Credible set source could not locate the required fields from a previous request.');
        }
        var nlogpvals = chain.body.map(function (item) { return item[self.params.fields.log_pvalue]; });
        var credset_data = [];
        try {
            var scores = gwasCredibleSets.scoring.bayesFactors(nlogpvals);
            var posteriorProbabilities = gwasCredibleSets.scoring.normalizeProbabilities(scores);

            // Use scores to mark the credible set in various ways (depending on your visualization preferences,
            //   some of these may not be needed)
            var credibleSet = gwasCredibleSets.marking.findCredibleSet(scores, threshold);
            var credSetScaled = gwasCredibleSets.marking.rescaleCredibleSet(credibleSet);
            var credSetBool = gwasCredibleSets.marking.markBoolean(credibleSet);

            // Annotate each response record based on credible set membership
            for (var i = 0; i < chain.body.length; i++) {
                credset_data.push({<commit_msg>Fix credset issue where some datasets allow `-log10p=0` values.<commit_after>/*
    Custom code used to power credible sets demonstration example. This is not part of the core LocusZoom library,
    but can be included as a standalone file.

    The page must incorporate and load all libraries before this file can be used, including:
     - Vendor assets
     - LocusZoom
     - gwas-credible-sets (available via NPM or a related CDN)
*/
'use strict';

// This is defined as a UMD module, to work with multiple different module systems / bundlers
// Arcane build note: everything defined here gets registered globally. This is not a "pure" module, and some build
//  systems may require being told that this file has side effects.
/* global define, module, require, Promise */

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['locuszoom', 'gwas-credible-sets'] , function(LocusZoom, gwasCredibleSets) {  // amd
            return factory(LocusZoom, gwasCredibleSets);
        });
    } else if(typeof module === 'object' && module.exports) {  // commonJS
        module.exports = factory(require('locuszoom'), require('gwas-credible-sets'));
    } else {  // globals
        if (!root.LocusZoom.ext.Data) {
            root.LocusZoom.ext.Data = {};
        }
        root.LocusZoom.ext.Data.CredibleSetLZ = factory(root.LocusZoom, root.gwasCredibleSets);
    }
}(this, function(LocusZoom, gwasCredibleSets) {
    /**
     * Custom data source that calculates the 95% credible set based on provided data.
     * This source must be requested as the second step in a chain, after a previous step that returns fields required
     *  for the calculation.
     *
     * @param {Object} init.params
     * @param {Object} init.params.fields
     * @param {String} init.params.fields.log_pvalue The name of the field containing pvalue information
     * @param {Number} [init.params.threshold=0.95] The credible set threshold (eg 95%)
     *
     * @class
     * @public
     * @augments LocusZoom.Data.Source
     */
    var CredibleSetLZ = LocusZoom.Data.Source.extend(function(init) {
        this.parseInit(init);
        this.enableCache = true;
        this.dependentSource = true; // Don't do calcs for a region with no assoc data
    }, 'CredibleSetLZ');

    CredibleSetLZ.prototype.parseInit = function (init) {
        this.params = init.params;
        if (!(this.params.fields && this.params.fields.log_pvalue)) {
            throw new Error('Source config for ' + this.constructor.SOURCE_NAME + " must specify how to find 'fields.log_pvalue'");
        }
        if (!this.params.threshold) {
            this.params.threshold = 0.95;
        }
    };

    CredibleSetLZ.prototype.getCacheKey = function(state, chain, fields) {
        var threshold = state.credible_set_threshold || this.params.threshold;
        return [ threshold, state.chr, state.start, state.end ].join('_');
    };

    CredibleSetLZ.prototype.fetchRequest = function (state, chain) {
        var self = this;
        // The threshold can be overridden dynamically via `plot.state`, or set when the source is created
        var threshold = state.credible_set_threshold || this.params.threshold;
        // Calculate raw bayes factors and posterior probabilities based on information returned from the API
        if (typeof chain.body[0][self.params.fields.log_pvalue] === 'undefined') {
            throw new Error('Credible set source could not locate the required fields from a previous request.');
        }
        var nlogpvals = chain.body.map(function (item) { return item[self.params.fields.log_pvalue]; });
        var credset_data = [];
        try {
            var scores = gwasCredibleSets.scoring.bayesFactors(nlogpvals);
            var posteriorProbabilities = gwasCredibleSets.scoring.normalizeProbabilities(scores);

            // Use scores to mark the credible set in various ways (depending on your visualization preferences,
            //   some of these may not be needed)
            var credibleSet = gwasCredibleSets.marking.findCredibleSet(scores, threshold);
            var credSetScaled = gwasCredibleSets.marking.rescaleCredibleSet(credibleSet);
            var credSetBool = gwasCredibleSets.marking.markBoolean(credibleSet);

            // Annotate each response record based on credible set membership
            for (var i = 0; i < chain.body.length; i++) {
                credset_data.push({
{'fraction_non_alphanumeric': 0.09848826714801444, 'fraction_numerical': 0.003046028880866426, 'mean_word_length': 2.8145438898450945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1764104', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2417, 'n_words': 777}