# all files / src/ DataLoader.js

71% Statements 71/100
64.44% Branches 29/45
84.21% Functions 16/19
71% Lines 71/100

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182

    1×
    1×
    1×
    1×

    1×
    337×
    337×
    616×
    262×

    354×

    337×

    1×
    3×
    729×
    337×

    729×
    14×
    25×

    3×

    1×
    439×
    439×

    439×

    439×

    1×
    1×

    1×
    1×
    204×

    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    7×

    1×

    1×
    10×
    7×

    1×

    1×

    1×
    1×
    86×
    84×

    2×

    86×

    1×

    1×

    1×
    1×

    1×
    1×

    1×
    1×
    806×

    1×

    1×
    439×
    439×
    439×
    439×
    434×
    5×
    5×

    439×

    1×

    1×/*jslint node: true */
    /*jshint laxbreak: true */
    "use strict";

    var $ = require('jquery');
    var _ = require('underscore');
    var Evidence = require('./Evidence');
    var Constants = require('./Constants');

    var evidenceGrouping = function (ftEvidences) {
        var evidences = {};
        _.each(ftEvidences, function(ev) {
            if (evidences[ev.code]) {
                evidences[ev.code].push(ev.source);
            } else {
                evidences[ev.code] = [ev.source];
            }
        });
        return evidences;
    };

    var groupEvidencesByCode = function(features) {
        _.each(features, function(ft) {
            if (ft.evidences) {
                ft.evidences = evidenceGrouping(ft.evidences);
            }
            if (ft.association) {
                _.each(ft.association, function(disease) {
                    Iif (disease.evidences) {
                        disease.evidences = evidenceGrouping(disease.evidences);
                    }
                });
            }
        });
        return features;
    };

    var setVariantData = function(source, d) {
        var datum = {};
        Iif (source && (source !== Constants.getUniProtSource())) {
            datum.begin = d.begin;
            delete d.begin;
            datum.end = d.end;
            delete d.end;
            datum.wildType = d.wildType;
            delete d.wildType;
            datum.alternativeSequence = d.alternativeSequence;
            delete d.alternativeSequence;
            datum.sourceType = d.sourceType;
            delete d.sourceType;
            datum.type = d.type;
            delete d.type;
            datum.externalData = {};
            datum.externalData[source] = d;
        } else {
            datum = d;
        }
        return datum;
    };

    var DataLoader = function() {
        return {
            get: function(url) {
                return $.getJSON(url);
            },
            groupFeaturesByCategory: function(features, sequence, source, includeVariants) {
                features = groupEvidencesByCode(features);
                var categories = _.groupBy(features, function(d) {
                    return d.category;
                });
                var variants;
                Iif (source && (source !== Constants.getUniProtSource()) && (includeVariants === true)) {
                    variants = categories.VARIATION;
                    delete categories.VARIATION;
                } else {
                    delete categories.VARIANTS;
                }
                var orderedPairs = [];
                var categoriesNames = Constants.getCategoryNamesInOrder();
                categoriesNames = _.pluck(categoriesNames, 'name');
                var newCategoryNames = [];
                _.each(categories, function(catInfo, catKey) {
                    Iif (!_.contains(categoriesNames, catKey)) {
                        newCategoryNames.push({
                            name: catKey,
                            label: Constants.convertNameToLabel(catKey),
                            visualizationType: Constants.getVisualizationTypes().basic
                        });
                    }
                });
                Iif (newCategoryNames.length !== 0) {
                    Constants.addCategories(newCategoryNames);
                    categoriesNames = Constants.getCategoryNamesInOrder();
                    categoriesNames = _.pluck(categoriesNames, 'name');
                }
                _.each(categoriesNames, function(catName) {
                    if (categories[catName]) {
                        orderedPairs.push([
                            catName,
                            categories[catName]
                        ]);
                    }
                });
                Iif (variants) {
                    var orderedVariantPairs = DataLoader.processVariants(variants, sequence, source, true);
                    orderedPairs.push(orderedVariantPairs[0]);
                }
                return orderedPairs;
            },
            processProteomics: function(features) {
                features = groupEvidencesByCode(features);
                var types = _.map(features, function(d) {
                    if (d.unique) {
                        d.type = 'unique';
                    } else {
                        d.type = 'non_unique';
                    }
                    return d;
                });
                return [
                    ['PROTEOMICS', types]
                ];
            },
            processUngroupedFeatures: function(features) {
                features = groupEvidencesByCode(features);
                return [
                    [features[0].type, features]
                ];
            },
            processVariants: function(variants, sequence, source, evidenceAlreadyGrouped) {
                Iif (source && (source !== Constants.getUniProtSource())) {
                    _.each(variants, function(variant) {
                        delete variant.category;
                    });
                }
                Eif (!evidenceAlreadyGrouped) {
                    variants = groupEvidencesByCode(variants);
                }
                var mutationArray = [];
                mutationArray.push({
                    'type': 'VARIANT',
                    'normal': '-',
                    'pos': 0,
                    'variants': []
                });
                var seq = sequence.split('');
                _.each(seq, function(d, i) {
                    mutationArray.push({
                        'type': 'VARIANT',
                        'normal': seq[i],
                        'pos': i + 1,
                        'variants': []
                    });
                });
                mutationArray.push({
                    'type': 'VARIANT',
                    'normal': '-',
                    'pos': seq.length + 1,
                    'variants': []
                });

                _.each(variants, function(d) {
                    d.begin = +d.begin;
                    d.end = d.end ? +d.end : d.begin;
                    d.wildType = d.wildType ? d.wildType : sequence.substring(d.begin, d.end + 1);
                    if ((1 <= d.begin) && (d.begin <= seq.length)) {
                        mutationArray[d.begin].variants.push(setVariantData(source, d));
                    } else Eif ((seq.length + 1) === d.begin) {
                        mutationArray[d.begin - 1].variants.push(setVariantData(source, d));
                    }
                    Iif (d.consequence) {
                        Constants.addConsequenceType(d.consequence);
                    }
                });
                return [
                    ['VARIATION', mutationArray]
                ];
            }
        };
    }();

    module.exports = DataLoader;