<commit_before>/**
 * This service contains utility function that are used in RNAcentral to pass data to angularjs-genoverse.
 *
 * Contains re-used functions. Those functions access data from surrounding controller's scope.
 * Thus, the intended use for this factory is as follows:
 *
 * sampleController.controller.js:
 * -------------------------------
 *
 * angular.module('sampleModule').controller('SampleController', function($scope, GenoverseUtils) {
 *     $scope.genoverseUtils = new GenoverseUtils($scope);
 * });
 *
 *
 *
 * sample.html:
 * ------------
 *
 * <genoverse genome="genome" chromosome="chromosome" start="start" end="end">
 *     <genoverse-track url="genoverseUtils.urls.sequence" name="'Sequence'" model="Genoverse.Track.Model.Sequence.Ensembl" view="Genoverse.Track.View.Sequence" controller="Genoverse.Track.Controller.Sequence" resizable="'auto'" auto-height="true" hide-empty="false" extra="{100000: false}"></genoverse-track>
 * </genoverse>
 */

angular.module("genomeBrowser").factory('GenoverseUtils', ['$filter', function($filter) {

    // Constructor
    // -----------

    function GenoverseUtils($scope) {
        var self = this;

        this.$scope = $scope;

        // Methods below need to be binded to 'this' object,
        // cause otherwise they get passed 'this' from event handler context.
        // We pass the $scope to these methods through 'this'.

        this.urls = {
            sequence: _.bind(function () {  // Sequence track configuration
                var species = this.$scope.browserLocation.genome;
                var endpoint = this.getEnsemblEndpoint(species);
                return '__ENDPOINT__/sequence/region/__SPECIES__/__CHR__:__START__-__END__?content-type=text/plain'
                    .replace('__ENDPOINT__', endpoint)
                    .replace('__SPECIES__', species);
            }, this),
            genes: _.bind(function () {  // Genes track configuration
                var species = this.$scope.browserLocation.genome;
                var endpoint = this.getEnsemblEndpoint(species);
                return '__ENDPOINT__/overlap/region/__SPECIES__/__CHR__:__START__-__END__?feature=gene;content-type=application/json'
                    .replace('__ENDPOINT__', endpoint)
                    .replace('__SPECIES__', species);
            }, this),
            transcripts: _.bind(function () {  // Transcripts track configuration
                var species = this.$scope.browserLocation.genome;
                var endpoint = this.getEnsemblEndpoint(species);
                return '__ENDPOINT__/overlap/region/__SPECIES__/__CHR__:__START__-__END__?feature=transcript;feature=exon;feature=cds;content-type=application/json'
                    .replace('__ENDPOINT__', endpoint)
                    .replace('__SPECIES__', species);
            }, this),
            RNAcentral: _.bind(function () {  // custom RNAcentral track
                var origin = window.location.origin ? window.location.origin : window.location.protocol + "//" + window.location.host + '/';
                return origin + '/api/v1/overlap/region/__SPECIES__/__CHR__:__START__-__END__'.replace('__SPECIES__', this.$scope.browserLocation.genome);
            }, this)
        };


        this.genesPopulateMenu = _.bind(function(feature) {
            var chrStartEnd = feature.seq_region_name + ':' + feature.start + '-' + feature.end; // string e.g. 'X:1-100000'
            var location = '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Location/View?r=' + chrStartEnd + '" id="ensembl-link" target="_blank">' + chrStartEnd + '</a>';

            var strand;
            if (feature.strand == 1) {
                strand = "forward >";
            }
            else if (feature.strand == -1) {
                strand = "< reverse";
            }

            var result = {
                title: '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Gene/Summary?g=' + feature.gene_id + '">' + feature.gene_id + '</a>',
                "Assembly name": feature.assembly_name,
                "Biotype": feature.biotype,
                "Description": feature.description,
                "Feature type": feature.feature_type,
                "Gene id": feature.gene_id,
                "Location": location,
                "Logic name": feature.logic_name,
                "Source": feature.source,
                "Strand": strand,
                "Version": feature.version
            };

            if (feature.havana_gene && feature.havana_version) {
                result["Havana gene"] = feature.havana_gene;
                result["Havana version"] = feature.havana_version;
            }

            if (feature.external_name) {
                result["HGNC symbol"] = feature.external_name;
            }

            return result;
        }, this);


        this.transcriptsPopulateMenu = _.bind(function(feature) {
            var chrStartEnd = feature.seq_region_name + ':' + feature.start + '-' + feature.end; // string e.g. 'X:1-100000'
            var location = '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Location/View?r=' + chrStartEnd + '" id="ensembl-link" target="_blank">' + chrStartEnd + '</a>';

            var strand;
            if (feature.strand == 1) {
                strand = "forward >";
            }
            else if (feature.strand == -1) {
                strand = "< reverse";
            }

            var result = {
                title: '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Transcript/Summary?db=core&t=' + feature.transcript_id + '">' + feature.transcript_id + '</a>',
                "Assembly name": feature.assembly_name,
                "Biotype": feature.biotype,
                "Feature type": feature.feature_type,
                "Location": location,
                "Logic name": feature.logic_name,
                "Parent": '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Gene/Summary?g=' + feature.Parent + '">' + feature.Parent + '</a>',
                "Source": feature.source,
                "Strand": strand,
                "Transcript id": feature.transcript_id,
                "Version": feature.version
            };

            if (feature.havana_transcript && feature.havana_version) {
                result["Havana transcript"] = feature.havana_transcript;
                result["Havana version"] = feature.havana_version;
            }

            if (feature.external_name) {
                result["HGNC symbol"] = feature.external_name;
            }

            if (feature.transcript_support_level) {
                result["Transcript support level"] = '<a href="http://www.ensembl.org/Help/Glossary?id=492">' + feature.transcript_support_level + '</a>';
            }

            if (feature.tag) {
                result["Tag"] = feature.tag;
            }

            if (feature.version) { result["Version"] = feature.version; }

            return result;
        }, this);


        this.RNAcentralPopulateMenu = _.bind(function(feature) {
            var chrStartEnd = feature.seq_region_name + ':' + feature.start + '-' + feature.end; // string e.g. 'X:1-100000'
            var location = '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Location/View?r=' + chrStartEnd + '" id="ensembl-link" target="_blank">' + chrStartEnd + '</a>';

            var strand;
            if (feature.strand == 1) {
                strand = "forward >";
            }
            else if (feature.strand == -1) {
                strand = "< reverse";
            }

            return {
                title: '<a target=_blank href="http://rnacentral.org/rna/' + feature.external_name + '/' + feature.taxid.toString() +'">'+ feature.label + '</a>',
                "RNAcentral id": '<a target=_blank href="http://rnacentral.org/rna/' + feature.external_name + '/' + feature.taxid.toString() +'">' + feature.external_name + '</a>',
                "Description": feature.description || "",
                "RNA type": feature.biotype,
                "Feature type": feature.feature_type,
                "Location": location,
                "Logic name": feature.logic_name,
                "Strand": strand
            };
        }, this);


        this.RNAcentralParseData = function(data) {
            var model = this;
            var featuresById = this.featuresById;
            var ids = [];

            data.filter(function (d) { return d.feature_type === 'transcript'; }).forEach(function (feature, i) {
                if (!featuresById[feature.id]) {
                    // prepare a label
                    var label = feature.description || feature.external_name;
                    if (label.length > 50) {
                        label = label.substr(0, 47) + "...";
                    }
                    if (feature.strand == 1) {
                        label = label + " >";
                    }
                    else if (feature.strand == -1) {
                        label = "< " + label;
                    }

                    feature.id = feature.ID;
                    feature.label = label; // used to be feature.external_name<commit_msg> #372 Show databases on frontend.<commit_after>/**
 * This service contains utility function that are used in RNAcentral to pass data to angularjs-genoverse.
 *
 * Contains re-used functions. Those functions access data from surrounding controller's scope.
 * Thus, the intended use for this factory is as follows:
 *
 * sampleController.controller.js:
 * -------------------------------
 *
 * angular.module('sampleModule').controller('SampleController', function($scope, GenoverseUtils) {
 *     $scope.genoverseUtils = new GenoverseUtils($scope);
 * });
 *
 *
 *
 * sample.html:
 * ------------
 *
 * <genoverse genome="genome" chromosome="chromosome" start="start" end="end">
 *     <genoverse-track url="genoverseUtils.urls.sequence" name="'Sequence'" model="Genoverse.Track.Model.Sequence.Ensembl" view="Genoverse.Track.View.Sequence" controller="Genoverse.Track.Controller.Sequence" resizable="'auto'" auto-height="true" hide-empty="false" extra="{100000: false}"></genoverse-track>
 * </genoverse>
 */

angular.module("genomeBrowser").factory('GenoverseUtils', ['$filter', function($filter) {

    // Constructor
    // -----------

    function GenoverseUtils($scope) {
        var self = this;

        this.$scope = $scope;

        // Methods below need to be binded to 'this' object,
        // cause otherwise they get passed 'this' from event handler context.
        // We pass the $scope to these methods through 'this'.

        this.urls = {
            sequence: _.bind(function () {  // Sequence track configuration
                var species = this.$scope.browserLocation.genome;
                var endpoint = this.getEnsemblEndpoint(species);
                return '__ENDPOINT__/sequence/region/__SPECIES__/__CHR__:__START__-__END__?content-type=text/plain'
                    .replace('__ENDPOINT__', endpoint)
                    .replace('__SPECIES__', species);
            }, this),
            genes: _.bind(function () {  // Genes track configuration
                var species = this.$scope.browserLocation.genome;
                var endpoint = this.getEnsemblEndpoint(species);
                return '__ENDPOINT__/overlap/region/__SPECIES__/__CHR__:__START__-__END__?feature=gene;content-type=application/json'
                    .replace('__ENDPOINT__', endpoint)
                    .replace('__SPECIES__', species);
            }, this),
            transcripts: _.bind(function () {  // Transcripts track configuration
                var species = this.$scope.browserLocation.genome;
                var endpoint = this.getEnsemblEndpoint(species);
                return '__ENDPOINT__/overlap/region/__SPECIES__/__CHR__:__START__-__END__?feature=transcript;feature=exon;feature=cds;content-type=application/json'
                    .replace('__ENDPOINT__', endpoint)
                    .replace('__SPECIES__', species);
            }, this),
            RNAcentral: _.bind(function () {  // custom RNAcentral track
                var origin = window.location.origin ? window.location.origin : window.location.protocol + "//" + window.location.host + '/';
                return origin + '/api/v1/overlap/region/__SPECIES__/__CHR__:__START__-__END__'.replace('__SPECIES__', this.$scope.browserLocation.genome);
            }, this)
        };


        this.genesPopulateMenu = _.bind(function(feature) {
            var chrStartEnd = feature.seq_region_name + ':' + feature.start + '-' + feature.end; // string e.g. 'X:1-100000'
            var location = '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Location/View?r=' + chrStartEnd + '" id="ensembl-link" target="_blank">' + chrStartEnd + '</a>';

            var strand;
            if (feature.strand == 1) {
                strand = "forward >";
            }
            else if (feature.strand == -1) {
                strand = "< reverse";
            }

            var result = {
                title: '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Gene/Summary?g=' + feature.gene_id + '">' + feature.gene_id + '</a>',
                "Assembly name": feature.assembly_name,
                "Biotype": feature.biotype,
                "Description": feature.description,
                "Feature type": feature.feature_type,
                "Gene id": feature.gene_id,
                "Location": location,
                "Logic name": feature.logic_name,
                "Source": feature.source,
                "Strand": strand,
                "Version": feature.version
            };

            if (feature.havana_gene && feature.havana_version) {
                result["Havana gene"] = feature.havana_gene;
                result["Havana version"] = feature.havana_version;
            }

            if (feature.external_name) {
                result["HGNC symbol"] = feature.external_name;
            }

            return result;
        }, this);


        this.transcriptsPopulateMenu = _.bind(function(feature) {
            var chrStartEnd = feature.seq_region_name + ':' + feature.start + '-' + feature.end; // string e.g. 'X:1-100000'
            var location = '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Location/View?r=' + chrStartEnd + '" id="ensembl-link" target="_blank">' + chrStartEnd + '</a>';

            var strand;
            if (feature.strand == 1) {
                strand = "forward >";
            }
            else if (feature.strand == -1) {
                strand = "< reverse";
            }

            var result = {
                title: '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Transcript/Summary?db=core&t=' + feature.transcript_id + '">' + feature.transcript_id + '</a>',
                "Assembly name": feature.assembly_name,
                "Biotype": feature.biotype,
                "Feature type": feature.feature_type,
                "Location": location,
                "Logic name": feature.logic_name,
                "Parent": '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Gene/Summary?g=' + feature.Parent + '">' + feature.Parent + '</a>',
                "Source": feature.source,
                "Strand": strand,
                "Transcript id": feature.transcript_id,
                "Version": feature.version
            };

            if (feature.havana_transcript && feature.havana_version) {
                result["Havana transcript"] = feature.havana_transcript;
                result["Havana version"] = feature.havana_version;
            }

            if (feature.external_name) {
                result["HGNC symbol"] = feature.external_name;
            }

            if (feature.transcript_support_level) {
                result["Transcript support level"] = '<a href="http://www.ensembl.org/Help/Glossary?id=492">' + feature.transcript_support_level + '</a>';
            }

            if (feature.tag) {
                result["Tag"] = feature.tag;
            }

            if (feature.version) { result["Version"] = feature.version; }

            return result;
        }, this);


        this.RNAcentralPopulateMenu = _.bind(function(feature) {
            var chrStartEnd = feature.seq_region_name + ':' + feature.start + '-' + feature.end; // string e.g. 'X:1-100000'
            var location = '<a href="http://' + this.$scope.browserLocation.domain + '/' + this.$scope.browserLocation.genome + '/Location/View?r=' + chrStartEnd + '" id="ensembl-link" target="_blank">' + chrStartEnd + '</a>';

            var strand;
            if (feature.strand == 1) {
                strand = "forward >";
            }
            else if (feature.strand == -1) {
                strand = "< reverse";
            }

            return {
                title: '<a target=_blank href="http://rnacentral.org/rna/' + feature.external_name + '/' + feature.taxid.toString() +'">'+ feature.label + '</a>',
                "RNAcentral id": '<a target=_blank href="http://rnacentral.org/rna/' + feature.external_name + '/' + feature.taxid.toString() +'">' + feature.external_name + '</a>',
                "Description": feature.description || "",
                "RNA type": feature.biotype,
                "Feature type": feature.feature_type,
                "Location": location,
                "Logic name": feature.logic_name,
                "Strand": strand,
                "Databases": feature.databases.join(', ')
            };
        }, this);


        this.RNAcentralParseData = function(data) {
            var model = this;
            var featuresById = this.featuresById;
            var ids = [];

            data.filter(function (d) { return d.feature_type === 'transcript'; }).forEach(function (feature, i) {
                if (!featuresById[feature.id]) {
                    // prepare a label
                    var label = feature.description || feature.external_name;
                    if (label.length > 50) {
                        label = label.substr(0, 47) + "...";
                    }
                    if (feature.strand == 1) {
                        label = label + " >";
                    }
                    else if (feature.strand == -1) {
                        label = "< " + label;
                    }

                    feature.id = feature.ID;
                    feature.label = label; // used to be feature.external_name
{'fraction_non_alphanumeric': 0.14444502700152048, 'fraction_numerical': 0.004771142452681802, 'mean_word_length': 1.8926296633303004, 'pattern_counts': {'":': 55, '<': 55, '<?xml version=': 0, '>': 57, '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': '1467301', 'n_tokens_mistral': 5411, 'n_tokens_neox': 4970, 'n_words': 988}