UNPKG

hierarchic-heatmap-table-component

Version:

A visualisation tool for displaying rows in a hiearchic manner with heatmap capabilities

292 lines (234 loc) 11.2 kB
/** * A neXtProt js client */ (function (root) { // 'use strict'; if (root.Nextprot === undefined) { root.Nextprot = {}; } (function () { //Utility methods var _getURLParameter = function (name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); }; function _changeParamOrAddParamByName(href, paramName, newVal) { var tmpRegex = new RegExp("(" + paramName + "=)[a-zA-Z0-9_]+", 'ig'); if (href.match(tmpRegex) !== null) { return href.replace(tmpRegex, '$1' + newVal); } return href += (((href.indexOf("?") !== -1) ? "&" : "?") + paramName + "=" + newVal); } var _convertToTupleMap = function (data) { var publiMap = {}; var xrefMap = {}; if (data.entry.publications){ data.entry.publications.forEach(function (p) { publiMap[p.md5] = p; }); } data.entry.xrefs.forEach(function (p) { xrefMap[p.dbXrefId] = p; }); //return data.entry.annotations; return { annot: data.entry.annotations, publi: publiMap, xrefs: xrefMap }; }; var normalizeEntry = function (entry) { if (entry.substring(0, 3) !== "NX_") { entry = "NX_" + entry; } return entry; }; var goldOnly = function (annotations) { annotations.forEach(function(a){a.evidences = a.evidences.filter(function(e){return e.qualityQualifier === "GOLD"})}); return annotations.filter(function(a){ return a.evidences.length > 0 }); } var environment = _getURLParameter("env") || 'pro'; //By default returns the production var apiBaseUrl = "https://api.nextprot.org"; if (environment !== 'pro') { apiBaseUrl = "http://" + environment + "-api.nextprot.org"; } var sparqlEndpoint = apiBaseUrl + "/sparql"; var sparqlFormat = "?output=json"; var applicationName = null; var clientInfo = null; var goldOnly = null; // var onlyGoldQuality = _getURLParameter("onlyGold"); function _getJSON(url) { var finalURL = url; finalURL = _changeParamOrAddParamByName(finalURL, "clientInfo", clientInfo); finalURL = _changeParamOrAddParamByName(finalURL, "applicationName", applicationName); if (goldOnly) finalURL = _changeParamOrAddParamByName(finalURL, "goldOnly", goldOnly); return Promise.resolve($.getJSON(finalURL)); //return get(url).then(JSON.parse); } var _getEntry = function (entry, context) { var entryName = normalizeEntry(entry || (_getURLParameter("nxentry") || 'NX_P01308')); var url = apiBaseUrl + "/entry/" + entryName; if (context) { url += "/" + context; } return _getJSON(url); }; var _callPepX = function (seq, mode) { var url = apiBaseUrl + "/entries/search/peptide?peptide=" + seq + "&modeIL=" + mode; return _getJSON(url); }; var NextprotClient = function (appName, clientInformation) { applicationName = appName; clientInfo = clientInformation; goldOnly = _getURLParameter("goldOnly"); if (!appName) { throw "Please provide some application name ex: new Nextprot.Client('demo application for visualizing peptides', clientInformation);"; } if (!clientInformation) { throw "Please provide some client information ex: new Nextprot.Client(applicationName, 'Calipho SIB at Geneva');"; } }; //////////////// BEGIN Setters //////////////////////////////////////////////////////////////////////// /** By default it is set to https://api.nextprot.org */ NextprotClient.prototype.setApiBaseUrl = function (_apiBaseUrl) { apiBaseUrl = _apiBaseUrl; sparqlEndpoint = apiBaseUrl + "/sparql"; }; /** By default it is set to https://api.nextprot.org/sparql */ NextprotClient.prototype.setSparqlEndpoint = function (_sparqlEndpoint) { sparqlEndpoint = _sparqlEndpoint; }; //////////////// END Setters //////////////////////////////////////////////////////////////////////// //Gets the entry set in the parameter NextprotClient.prototype.getEnvironment = function () { return _getURLParameter("env") || 'pro'; //By default returns the insulin }; NextprotClient.prototype.getQualityParam = function () { return _getURLParameter("goldOnly") || ''; // GOLD || GOLD & SILVER }; NextprotClient.prototype.getApiBaseUrl = function () { return apiBaseUrl; }; //Gets the entry set in the parameter NextprotClient.prototype.getEntryName = function () { return normalizeEntry(_getURLParameter("nxentry") || 'NX_P01308'); //By default returns the insulin }; NextprotClient.prototype.getInputOption = function () { return _getURLParameter("inputOption") || ''; //By default returns the insulin }; NextprotClient.prototype.changeEntry = function (elem) { var new_url = _changeParamOrAddParamByName(window.location.href, "nxentry", elem.value); window.location.href = new_url; }; NextprotClient.prototype.getEntryforPeptide = function (seq) { return _callPepX(seq, "true").then(function (data) { return data; }); }; var _transformPrefixesFunction = function (result) { var sparqlPrefixes = ""; result.map(function (p) { sparqlPrefixes += (p + "\n"); }); return sparqlPrefixes; }; // Keeps SPARQL prefixes in cache var sparqlPrefixPromise; NextprotClient.prototype.getSparqlPrefixes = function () { sparqlPrefixPromise = sparqlPrefixPromise || _getJSON(apiBaseUrl + "/sparql-prefixes").then(_transformPrefixesFunction); return sparqlPrefixPromise; }; NextprotClient.prototype.executeSparql = function (sparql, includePrefixes) { return this.getSparqlPrefixes().then(function (sparqlPrefixes) { var incPrefs = (includePrefixes === undefined) ? true : includePrefixes; var sparqlQuery = incPrefs ? sparqlPrefixes + sparql : sparql; //add SPARQL prefixes if flag not set to false var url = sparqlEndpoint + sparqlFormat + "&query=" + encodeURIComponent(sparqlQuery); return _getJSON(url); }); }; NextprotClient.prototype.getEntryProperties = function (entry) { return _getEntry(entry, "accession").then(function (data) { return data.entry.properties; }); }; NextprotClient.prototype.getProteinOverview = function (entry) { return _getEntry(entry, "overview").then(function (data) { return data.entry.overview; }); }; NextprotClient.prototype.getProteinSequence = function (entry) { return _getEntry(entry, "isoform").then(function (data) { return data.entry.isoforms; }); }; NextprotClient.prototype.getXrefs = function (entry) { return _getEntry(entry, "xref").then(function (data) { return data.entry.xrefs; }); }; /** USE THIS INSTEAD OF THE OTHERS for example getEntryPart(NX_1038042, "ptm") */ NextprotClient.prototype.getAnnotationsByCategory = function (entry, category) { return _getEntry(entry, category).then(function (data) { return _convertToTupleMap(data); }); }; NextprotClient.prototype.getFullAnnotationsByCategory = function (entry, category) { return _getEntry(entry, category).then(function (data) { return data.entry; }); }; NextprotClient.prototype.getEntry = function (entry, category) { return _getEntry(entry, category).then(function (data) { return data.entry; }); }; NextprotClient.prototype.getEntryProperties = function (entry) { return _getEntry(entry, "accession").then(function (data) { return data.entry.properties; }); }; NextprotClient.prototype.filterGoldOnlyAnnotations = function (annotations) { return goldOnly(annotations); }; /* Special method to retrieve isoforms mapping on the master sequence (should not be used by public) */ NextprotClient.prototype.getIsoformMapping = function (entry) { return _getEntry(entry, "isoform/mapping").then(function (data) { return data; }); }; NextprotClient.prototype.getGenomicMappings = function (entry) { return _getEntry(entry, "genomic-mapping").then(function (data) { return data.entry.genomicMappings; }); }; // BEGIN Special cases to be deprecated ////////////////////////////////////////////////////////////////////////////// NextprotClient.prototype.getPeptide = function (entry) { console.warn("getPeptide is deprecated. use getAnnotationsByCategory(entry, 'peptide-mapping') instead "); return _getEntry(entry, "peptide-mapping").then(function (data) { return data.entry.peptideMappings; }); }; NextprotClient.prototype.getSrmPeptide = function (entry) { console.warn("getSrmPeptide is deprecated. use getAnnotationsByCategory(entry, 'srm-peptide-mapping') instead "); return _getEntry(entry, "srm-peptide-mapping").then(function (data) { return data.entry.srmPeptideMappings; }); }; NextprotClient.prototype.getAntibody = function (entry) { //this is not deprecated yet return _getEntry(entry, "antibody").then(function (data) { return data.entry.antibodyMappings; }); }; // END Special cases to be deprecated ////////////////////////////////////////////////////////////////////////////// //node.js compatibility if (typeof exports !== 'undefined') { exports.Client = NextprotClient; } root.Nextprot.Client = NextprotClient; }()); }(this));