<commit_before>/*jslint node: true */
/*jshint laxbreak: true */
/*jshint laxcomma: true */
"use strict";

var d3 = require("d3");
var _ = require("underscore");
var Evidence = require('./Evidence');

var createTooltipBox = function(container) {
    d3.select('.up_pftv_tooltip-container').remove();
    var tooltipContainer = container.append('div')
        .attr('class','up_pftv_tooltip-container');
    tooltipContainer.append('span')
        .text('x')
        .attr('class','up_pftv_tooltip-close')
        .on('click',function(){
            tooltipContainer.transition(20)
                .style('opacity',0)
                .style('display','none');
            tooltipContainer.remove();
        });
    return tooltipContainer;
};

var getEvidenceText = function(tooltip, code, sources) {
    var acronym = Evidence.acronym[code];
    var publications = _.where(sources, {name: 'PubMed'}).length;
    publications += _.where(sources, {name: 'Citation'}).length;
    if ((acronym === 'EXP') || (acronym === 'NAS')) {
        return publications + (publications > 1 ? ' Publications' : ' Publication');
    } else if (acronym === 'IC') {
        return publications === 0
            ? 'Curated'
            : publications + (publications > 1 ? ' Publications' : ' Publication');
    } else if (acronym === 'ISS') {
        return 'By similarity';
    } else if (acronym === 'ISM') {
        return !sources || sources.length === 0 ? 'Sequence Analysis': sources[0].name + ' annotation';
    } else if ((acronym === 'MIXM') || (acronym === 'MIXA')) {
        return 'Combined sources';
    } else if ((acronym === 'MI') || (acronym === 'AI')){
        return 'Imported';
    } else if (acronym === 'AA') {
        var unirule = sources
            ? _.find(sources, function(source) {
            return source.url && (sources.url.indexOf('unirule') !== -1);
        })
            : false;
        var saas = sources
            ? _.find(sources, function(source) {
            return source.url && (sources.url.indexOf('SAAS') !== -1);
        })
            : false;
        return unirule
            ? 'UniRule annotation'
            : saas
            ? 'SAAS annotation'
            : sources ? sources[0].name + ' annotation' : 'Automatic annotation';
    } else {
        return code;
    }
};

var Tooltip = function(fv, d, container) {
    var tooltip = this;
    tooltip.data = d;
    tooltip.sequence = fv.sequence;
    tooltip.tooltipViewer = undefined;
    tooltip.tooltipViewer = undefined;

    var tooltipContainer = createTooltipBox(container);

    tooltipContainer.style('left', (d3.mouse(container.node())[0] + 10) + 'px')
                .style('top', d3.mouse(container.node())[1] + 'px')
                .transition(200)
                .style('opacity',1)
                .style('display','block');

    d3.select('.up_pftv_tooltip-container table').remove();

    tooltip.table = d3.select('.up_pftv_tooltip-container').append('table');
    tooltip.table
        .on('mousedown', function() {
            fv.overTooltip = true;
        })
        .on('mouseup', function() {
            fv.overTooltip = false;
        });

    var descRow = tooltip.table.append('tr');

    descRow.append('th')
            .attr('colspan',2)
            .text(tooltip.data.type.label + ' ' + tooltip.data.begin + (tooltip.data.end ? '-' + tooltip.data.end : ''));

    if (tooltip.data.sp !== undefined) {
        var dataSource = tooltip.table.append('tr');
        dataSource.append('td').text('Source');
        dataSource.append('td').text(function() {
            if (tooltip.data.sp && Evidence.isLSS(tooltip.data.evidences)) {
                return 'UniProt and large scale studies';
            } else if (tooltip.data.sp) {
                return 'UniProt';
            } else {
                return 'Large scale studies';
            }
        });
    }

    if (tooltip.data.description) {
        var dataDes = tooltip.table.append('tr');
        dataDes.append('td').text('Description');
        dataDes.append('td').text(tooltip.data.description);
    }

    tooltip.addEvidences = function(evidences) {
        var self = this;
        _.each(evidences, function(e){
            var typeRow = self.table.append('tr')
                            .attr('class','up_pftv_evidence-col');
            typeRow.append('td')
                    .text('Evidence');
            typeRow.append('td')
                .text(getEvidenceText(tooltip, e.code, e.sources));

            var groupedSources = _.groupBy(e.sources, 'name');

            _.each(groupedSources, function(elem, index) {
                var sourceRow = self.table.append('tr')
                                    .attr('class','up_pftv_evidence-source');

                sourceRow.append('td')
                        .text(index);
                var list = sourceRow.append('td');
                _.each(elem, function(el, i) {
                    list.append('span').append('a')
                        .attr('href', el.url)
                        .attr('target', '_blank')
                        .text(el.id);
                    if (i !== (elem.length-1)) {
                        list.append('span').text(', ');
                    }
                });
            });
        });
    };
};

var BasicTooltipViewer = function(tooltip) {
    tooltip.addEvidences(tooltip.data.evidences);
};

var AlternativeTooltipViewer = function(tooltip, change, field) {
    if (tooltip.data[field]) {
        var seqRow = tooltip.table.append('tr');
        seqRow.append('td').text(change);
        seqRow.append('td')
            .text(function() {
                var original = tooltip.sequence.substring(+tooltip.data.begin - 1,
                    +tooltip.data.begin - 1 + tooltip.data[field].length);
                return original + ' > ' + tooltip.data[field];
            });
    }
    tooltip.addEvidences(tooltip.data.evidences);
};

var VariantTooltipViewer = function(tooltip) {
    var addPredictions = function() {
        if (tooltip.data.frequency && (tooltip.data.frequency !== 0)) {
            var freqRow = tooltip.table.append('tr');
            freqRow.append('td').text('Frequency');<commit_msg>Use end field to obtain original sequence for mutagen.<commit_after>/*jslint node: true */
/*jshint laxbreak: true */
/*jshint laxcomma: true */
"use strict";

var d3 = require("d3");
var _ = require("underscore");
var Evidence = require('./Evidence');

var createTooltipBox = function(container) {
    d3.select('.up_pftv_tooltip-container').remove();
    var tooltipContainer = container.append('div')
        .attr('class','up_pftv_tooltip-container');
    tooltipContainer.append('span')
        .text('x')
        .attr('class','up_pftv_tooltip-close')
        .on('click',function(){
            tooltipContainer.transition(20)
                .style('opacity',0)
                .style('display','none');
            tooltipContainer.remove();
        });
    return tooltipContainer;
};

var getEvidenceText = function(tooltip, code, sources) {
    var acronym = Evidence.acronym[code];
    var publications = _.where(sources, {name: 'PubMed'}).length;
    publications += _.where(sources, {name: 'Citation'}).length;
    if ((acronym === 'EXP') || (acronym === 'NAS')) {
        return publications + (publications > 1 ? ' Publications' : ' Publication');
    } else if (acronym === 'IC') {
        return publications === 0
            ? 'Curated'
            : publications + (publications > 1 ? ' Publications' : ' Publication');
    } else if (acronym === 'ISS') {
        return 'By similarity';
    } else if (acronym === 'ISM') {
        return !sources || sources.length === 0 ? 'Sequence Analysis': sources[0].name + ' annotation';
    } else if ((acronym === 'MIXM') || (acronym === 'MIXA')) {
        return 'Combined sources';
    } else if ((acronym === 'MI') || (acronym === 'AI')){
        return 'Imported';
    } else if (acronym === 'AA') {
        var unirule = sources
            ? _.find(sources, function(source) {
            return source.url && (sources.url.indexOf('unirule') !== -1);
        })
            : false;
        var saas = sources
            ? _.find(sources, function(source) {
            return source.url && (sources.url.indexOf('SAAS') !== -1);
        })
            : false;
        return unirule
            ? 'UniRule annotation'
            : saas
            ? 'SAAS annotation'
            : sources ? sources[0].name + ' annotation' : 'Automatic annotation';
    } else {
        return code;
    }
};

var Tooltip = function(fv, d, container) {
    var tooltip = this;
    tooltip.data = d;
    tooltip.sequence = fv.sequence;
    tooltip.tooltipViewer = undefined;
    tooltip.tooltipViewer = undefined;

    var tooltipContainer = createTooltipBox(container);

    tooltipContainer.style('left', (d3.mouse(container.node())[0] + 10) + 'px')
                .style('top', d3.mouse(container.node())[1] + 'px')
                .transition(200)
                .style('opacity',1)
                .style('display','block');

    d3.select('.up_pftv_tooltip-container table').remove();

    tooltip.table = d3.select('.up_pftv_tooltip-container').append('table');
    tooltip.table
        .on('mousedown', function() {
            fv.overTooltip = true;
        })
        .on('mouseup', function() {
            fv.overTooltip = false;
        });

    var descRow = tooltip.table.append('tr');

    descRow.append('th')
            .attr('colspan',2)
            .text(tooltip.data.type.label + ' ' + tooltip.data.begin + (tooltip.data.end ? '-' + tooltip.data.end : ''));

    if (tooltip.data.sp !== undefined) {
        var dataSource = tooltip.table.append('tr');
        dataSource.append('td').text('Source');
        dataSource.append('td').text(function() {
            if (tooltip.data.sp && Evidence.isLSS(tooltip.data.evidences)) {
                return 'UniProt and large scale studies';
            } else if (tooltip.data.sp) {
                return 'UniProt';
            } else {
                return 'Large scale studies';
            }
        });
    }

    if (tooltip.data.description) {
        var dataDes = tooltip.table.append('tr');
        dataDes.append('td').text('Description');
        dataDes.append('td').text(tooltip.data.description);
    }

    tooltip.addEvidences = function(evidences) {
        var self = this;
        _.each(evidences, function(e){
            var typeRow = self.table.append('tr')
                            .attr('class','up_pftv_evidence-col');
            typeRow.append('td')
                    .text('Evidence');
            typeRow.append('td')
                .text(getEvidenceText(tooltip, e.code, e.sources));

            var groupedSources = _.groupBy(e.sources, 'name');

            _.each(groupedSources, function(elem, index) {
                var sourceRow = self.table.append('tr')
                                    .attr('class','up_pftv_evidence-source');

                sourceRow.append('td')
                        .text(index);
                var list = sourceRow.append('td');
                _.each(elem, function(el, i) {
                    list.append('span').append('a')
                        .attr('href', el.url)
                        .attr('target', '_blank')
                        .text(el.id);
                    if (i !== (elem.length-1)) {
                        list.append('span').text(', ');
                    }
                });
            });
        });
    };
};

var BasicTooltipViewer = function(tooltip) {
    tooltip.addEvidences(tooltip.data.evidences);
};

var AlternativeTooltipViewer = function(tooltip, change, field) {
    if (tooltip.data[field]) {
        var seqRow = tooltip.table.append('tr');
        seqRow.append('td').text(change);
        seqRow.append('td')
            .text(function() {
                var end = tooltip.data.end ? tooltip.data.end : tooltip.data.begin;
                var original = tooltip.sequence.substring(+tooltip.data.begin - 1, +end);
                return original + ' > ' + tooltip.data[field];
            });
    }
    tooltip.addEvidences(tooltip.data.evidences);
};

var VariantTooltipViewer = function(tooltip) {
    var addPredictions = function() {
        if (tooltip.data.frequency && (tooltip.data.frequency !== 0)) {
            var freqRow = tooltip.table.append('tr');
            freqRow.append('td').text('Frequency');
{'fraction_non_alphanumeric': 0.15931823637673043, 'fraction_numerical': 0.0048811714811554776, 'mean_word_length': 2.003604902667628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2319309', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3529, 'n_words': 662}