<commit_before>/*
 * Copyright 2015-2016 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import BioinfoUtils from "../../core/bioinfo/bioinfo-utils.js";


export default class VariantGridFormatter {

    // DEPRECATED: use new consequenceTypes.impact instead
    static assignColors(consequenceTypes, proteinSubstitutionScores) {
        let result = {};
        if (consequenceTypes) {
            const consequenceTypeToColor = {};
            const consequenceTypeToImpact = {};
            for (const category of consequenceTypes.categories) {
                if (category.terms) {
                    for (const term of category.terms) {
                        consequenceTypeToColor[term.name] = consequenceTypes.style[term.impact];
                        consequenceTypeToImpact[term.name] = term.impact;
                    }
                } else {
                    if (category.id && category.name) {
                        consequenceTypeToColor[category.name] = consequenceTypes[category.impact];
                        consequenceTypeToImpact[category.name] = category.impact;
                    }
                }
            }
            result = {
                consequenceTypeToColor: consequenceTypeToColor,
                consequenceTypeToImpact: consequenceTypeToImpact
            };
        }

        if (proteinSubstitutionScores) {
            const pssColor = new Map();
            for (const i in proteinSubstitutionScores) {
                if (Object.prototype.hasOwnProperty.call(proteinSubstitutionScores, i)) {

                    const obj = proteinSubstitutionScores[i];
                    Object.keys(obj).forEach(key => {
                        pssColor.set(key, obj[key]);
                    });
                }
            }
            result.pssColor = pssColor;
        }
        return result;
    }

    static variantFormatter(value, row, index, assembly, config) {
        if (!row) {
            return;
        }

        let ref = row.reference ? row.reference : "-";
        let alt = row.alternate ? row.alternate : "-";

        // Check size
        const maxAlleleLength = config?.alleleStringLengthMax ? config.alleleStringLengthMax : 20;
        ref = (ref.length > maxAlleleLength) ? ref.substring(0, 4) + "..." + ref.substring(ref.length - 4) : ref;
        alt = (alt.length > maxAlleleLength) ? alt.substring(0, 4) + "..." + alt.substring(alt.length - 4) : alt;

        // Ww need to escape < and > symbols from <INS>, <DEL>, ...
        alt = alt.replaceAll("<", "&lt;").replaceAll(">", "&gt;");

        // Create links for tooltip
        const variantRegion = row.chromosome + ":" + row.start + "-" + row.end;
        const tooltipText = `
            <div class="dropdown-header" style="padding-left: 5px">External Links</div>
            <div style="padding: 5px">
                <a target="_blank" href="${BioinfoUtils.getVariantLink(row.id, variantRegion, "ensembl_genome_browser", assembly)}">
                    Ensembl Genome Browser
                </a>
            </div>
            <div style="padding: 5px">
                <a target="_blank" href="${BioinfoUtils.getVariantLink(row.id, variantRegion, "ucsc_genome_browser")}">
                    UCSC Genome Browser
                </a>
            </div>`;

        const snpHtml = VariantGridFormatter.snpFormatter(value, row, index, assembly);

        return `
            <div style="margin: 5px 0px">
                <a tooltip-title='Links' tooltip-text='${tooltipText}'>
                    ${row.chromosome}:${row.start}&nbsp;&nbsp;${ref}/${alt}
                </a>
            </div>
            ${snpHtml ? `<div style="margin: 5px 0px">${snpHtml}</div>` : ""}
            `;
    }

    static snpFormatter(value, row, index, assembly) {
        // We try first to read SNP ID from the 'names' of the variant (this identifier comes from the file).
        // If this ID is not a "rs..." then we search the rs in the CellBase XRef annotations.
        // This field is in annotation.xref when source: "dbSNP".
        let snpId = "";
        if (row.names && row.names.length > 0) {
            for (const name of row.names) {
                if (name.startsWith("rs")) {
                    snpId = name;
                    break;
                }
            }
        } else {
            if (row.annotation) {
                if (row.annotation.id && row.annotation.id.startsWith("rs")) {
                    snpId = row.annotation.id;
                } else {
                    if (row.annotation.xrefs) {
                        for (const xref of row.annotation.xrefs) {
                            if (xref.source === "dbSNP") {
                                snpId = xref.id;
                                break;
                            }
                        }<commit_msg>wc: display highlight icons inline with variant id (#369)<commit_after>/*
 * Copyright 2015-2016 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import BioinfoUtils from "../../core/bioinfo/bioinfo-utils.js";


export default class VariantGridFormatter {

    // DEPRECATED: use new consequenceTypes.impact instead
    static assignColors(consequenceTypes, proteinSubstitutionScores) {
        let result = {};
        if (consequenceTypes) {
            const consequenceTypeToColor = {};
            const consequenceTypeToImpact = {};
            for (const category of consequenceTypes.categories) {
                if (category.terms) {
                    for (const term of category.terms) {
                        consequenceTypeToColor[term.name] = consequenceTypes.style[term.impact];
                        consequenceTypeToImpact[term.name] = term.impact;
                    }
                } else {
                    if (category.id && category.name) {
                        consequenceTypeToColor[category.name] = consequenceTypes[category.impact];
                        consequenceTypeToImpact[category.name] = category.impact;
                    }
                }
            }
            result = {
                consequenceTypeToColor: consequenceTypeToColor,
                consequenceTypeToImpact: consequenceTypeToImpact
            };
        }

        if (proteinSubstitutionScores) {
            const pssColor = new Map();
            for (const i in proteinSubstitutionScores) {
                if (Object.prototype.hasOwnProperty.call(proteinSubstitutionScores, i)) {

                    const obj = proteinSubstitutionScores[i];
                    Object.keys(obj).forEach(key => {
                        pssColor.set(key, obj[key]);
                    });
                }
            }
            result.pssColor = pssColor;
        }
        return result;
    }

    static variantFormatter(value, row, index, assembly, config) {
        if (!row) {
            return;
        }

        let ref = row.reference ? row.reference : "-";
        let alt = row.alternate ? row.alternate : "-";

        // Check size
        const maxAlleleLength = config?.alleleStringLengthMax ? config.alleleStringLengthMax : 20;
        ref = (ref.length > maxAlleleLength) ? ref.substring(0, 4) + "..." + ref.substring(ref.length - 4) : ref;
        alt = (alt.length > maxAlleleLength) ? alt.substring(0, 4) + "..." + alt.substring(alt.length - 4) : alt;

        // Ww need to escape < and > symbols from <INS>, <DEL>, ...
        alt = alt.replaceAll("<", "&lt;").replaceAll(">", "&gt;");

        // Create links for tooltip
        const variantRegion = row.chromosome + ":" + row.start + "-" + row.end;
        const tooltipText = `
            <div class="dropdown-header" style="padding-left: 5px">External Links</div>
            <div style="padding: 5px">
                <a target="_blank" href="${BioinfoUtils.getVariantLink(row.id, variantRegion, "ensembl_genome_browser", assembly)}">
                    Ensembl Genome Browser
                </a>
            </div>
            <div style="padding: 5px">
                <a target="_blank" href="${BioinfoUtils.getVariantLink(row.id, variantRegion, "ucsc_genome_browser")}">
                    UCSC Genome Browser
                </a>
            </div>
        `;

        const snpHtml = VariantGridFormatter.snpFormatter(value, row, index, assembly);
        const highlights = (config.activeHighlights || []).map(id => {
            const highlight = config.highlights.find(item => item.id === id);
            if (highlight.condition(row, index) && highlight.style?.icon) {
                const description = highlight.description || highlight.name || "";
                const icon = highlight.style.icon;
                const color = highlight.style.iconColor || "";

                return `<i title="${description}" class="fas fa-${icon}" style="color:${color};margin-left:4px;"></i>`;
            }
        });

        return `
            <div style="margin:5px 0px;white-space:nowrap;">
                <a tooltip-title='Links' tooltip-text='${tooltipText}'>
                    ${row.chromosome}:${row.start}&nbsp;&nbsp;${ref}/${alt}
                </a>
                ${highlights.join("")}
            </div>
            ${snpHtml ? `<div style="margin: 5px 0px">${snpHtml}</div>` : ""}
        `;
    }

    static snpFormatter(value, row, index, assembly) {
        // We try first to read SNP ID from the 'names' of the variant (this identifier comes from the file).
        // If this ID is not a "rs..." then we search the rs in the CellBase XRef annotations.
        // This field is in annotation.xref when source: "dbSNP".
        let snpId = "";
        if (row.names && row.names.length > 0) {
            for (const name of row.names) {
                if (name.startsWith("rs")) {
                    snpId = name;
                    break;
                }
            }
        } else {
            if (row.annotation) {
                if (row.annotation.id && row.annotation.id.startsWith("rs")) {
                    snpId = row.annotation.id;
                } else {
                    if (row.annotation.xrefs) {
                        for (const xref of row.annotation.xrefs) {
                            if (xref.source === "dbSNP") {
                                snpId = xref.id;
                                break;
                            }
                        }
{'fraction_non_alphanumeric': 0.11516264428121721, 'fraction_numerical': 0.005246589716684155, 'mean_word_length': 1.6772003745318351, 'pattern_counts': {'":': 2, '<': 45, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1686780', 'n_tokens_mistral': 3146, 'n_tokens_neox': 2923, 'n_words': 765}