Commit bdf77514 authored by Marek Ostaszewski's avatar Marek Ostaszewski
Browse files

p val adj, sort working

parent eb781640
Pipeline #7235 failed with stages
in 3 minutes and 18 seconds
......@@ -9,8 +9,8 @@ const globals = {
selected: [],
allBioEntities: [],
pathways: [],
quickAliases: [],
pickedRandomly: undefined
groupedPathways: undefined,
quickAliases: []
};
// ******************************************************************************
......@@ -202,47 +202,10 @@ function pickRandom() {
}
}
function highlightSelected(pickedRandomly = false) {
const highlightDefs = [];
if (pickedRandomly) {
if (globals.pickedRandomly) {
highlightDefs.push({
element: {
id: globals.pickedRandomly.id,
modelId: globals.pickedRandomly.getModelId(),
type: globals.pickedRandomly.constructor.name.toUpperCase()
},
type: "SURFACE",
options: {
color: '#00FF00',
opacity: 0.5
}
});
}
} else {
globals.selected.forEach(e => {
highlightDefs.push({
element: {
id: e.id,
modelId: e.getModelId(),
type: "ALIAS"
},
type: "ICON"
});
});
}
minervaProxy.project.map.showBioEntity(highlightDefs);
}
function calculatePathways() {
const pathwayAliases = [];
const pathwayObjs = [];
function populatePathways(){
const pathwayObjs = [];
const pgrObjects = [];
// For the time being, handle only the main map
// minervaProxy.project.data.getModels() registered earlier
......@@ -257,9 +220,10 @@ function calculatePathways() {
}
}
});
globals.quickAliases = pgrObjects;
globals.groupedPathways = [];
pathwayObjs.forEach( function(e, i) {
const these_dims = new Array(e.getX(), e.getX()+e.getWidth(), e.getY(), e.getY()+e.getHeight());
const these_hits = [];
......@@ -272,10 +236,22 @@ function calculatePathways() {
}
});
e.pathwayAliases = these_hits;
var hit = globals.groupedPathways.findIndex(fi => fi.name == e.getName());
if(hit == -1) {
hit = globals.groupedPathways.length;
globals.groupedPathways.push({name: e.getName(), aliases: new Set(), elementAliases: new Set()});
}
globals.groupedPathways[hit].aliases.add(e.getId());
//globals.groupedPathways[e.getName()].elementNames.add(e.getName());
//temp1.map(ref => ref.getReferences().filter(tref => (tref.getType() == "HGNC_SYMBOL")).map(ann => ann.getResource()))
globals.groupedPathways[hit].elementAliases = new Set([...globals.groupedPathways[hit].elementAliases, ...these_hits]);
});
console.log(pathwayObjs);
console.log(pgrObjects);
console.log(globals.groupedPathways);
globals.pathways = pathwayObjs;
}
......@@ -292,31 +268,74 @@ function calculatePathways() {
}
function calculateGSEA() {
// Define factorial function, to calculate the hypergeometric test
var f = [];
function factorial (n) {
if (n == 0 || n == 1)
return 1;
if (f[n] > 0)
return f[n];
return f[n] = factorial(n-1) * n;
};
//Names of all considered aliases
const allNames = new Set(globals.quickAliases.map(ref => ref.getReferences().filter(tref => (tref.getType() == "HGNC_SYMBOL")).map(ann => ann.getResource())));
function kcomb (n,k) {
//return factorial(n)/(factorial(k)*(factorial(n - k)));
// Get active overlays
minervaProxy.project.map.getVisibleDataOverlays().then(function(overlays) {
let gsea_html = '';
overlays.forEach( e => {
//const these_ovnames = new Set(e.getAliases().map(ref => ref.getReferences().filter(tref => (tref.getType() == "HGNC_SYMBOL")).map(ann => ann.getResource())));
// e is a LayoutAlias, needs to be mapped back to map aliases, to retrieve HGNC; defined as set, .has() function looks better in filtering
const these_ovaliases = new Set([...e.getAliases().map(g => g.getId())]);
// grab aliases matching the overlay -> get their references -> filter by HGNC_SYMBOL -> get value
const these_ovnames = new Set(globals.quickAliases.filter(h => these_ovaliases.has(h.getId())).flatMap(ref =>
ref.getReferences().filter(tref => (tref.getType() == "HGNC_SYMBOL")).map(ann => ann.getResource())));
const overlay_pvals = [];
globals.groupedPathways.forEach ( g => {
// split into two steps for better clarity
// 1. get element aliases for a given key (pathway group), because it's a set, we use [...] notation to cast onto array
const these_pgaliases = [...g.elementAliases];
// 2. get element names for the alias array; define as set for unique values
const these_pgnames = new Set(these_pgaliases.flatMap(ref =>
ref.getReferences().filter(tref => (tref.getType() == "HGNC_SYMBOL")).map(ann => ann.getResource())));
//calculate intersect
const isect = [...these_ovnames].filter(value => these_pgnames.has(value));
var m = these_pgnames.size;
var k = isect.length;
var N = allNames.size;
var n = these_ovnames.size;
if(k > 0) {
//var px = (kcomb(m,k)*kcomb(N-m,n-k))/kcomb(N,n);
//Approximation using upper boud from Eq (2) in http://page.mi.fu-berlin.de/shagnik/notes/binomials.pdf
var ret = Math.pow(n*Math.E/k,k);
console.log(ret);
return ret;
//Simplified, props to Ewa Smula (ewa.smula@uni.lu)
var this_pval = Math.pow(((m*(n-k))/(k*(N-m))),k) * Math.pow(((n*(N-m))/(N*(n-k))),n);
overlay_pvals.push({pval: this_pval, pname: g.name})
};
});
overlay_pvals.sort( function (a, b) {
return a.pval - b.pval;
}).forEach(p => {
var adj_pval = Math.min(p.pval*globals.groupedPathways.length, 1).toFixed(4);
if(adj_pval < 0.1) {
if(adj_pval == 0) {
gsea_html += `pval < 0.0001 - ${p.pname};<br/>`;
} else {
gsea_html += `pval: ${adj_pval} - ${p.pname};<br/>`;
}
}
});
});
pluginContainer.find('.panel-events .panel-body').html(gsea_html);
});
}
function calculateGSEAbyId() {
// Get active overlays
minervaProxy.project.map.getVisibleDataOverlays().then(function(overlays) {
let gsea_html = '';
overlays.forEach( e => {
const these_ovaliases = e.getAliases().map( g => g.getId());
globals.pathways.forEach( g => {
const these_paliases = g.pathwayAliases.map(h => h.getId());
//const these_ovaliases = e.getAliases().map( g => g.getId());
const these_ovaliases = e.getAliases().map(g => g.getId());
globals.groupedPathways.forEach( g => {
const these_paliases = [...g.elementAliases].map(h => h.getId());
const isect = these_ovaliases.filter(value => -1 !== these_paliases.indexOf(value));
console.log(isect);
......@@ -333,13 +352,30 @@ function calculateGSEA() {
var px = Math.pow(((m*(n-k))/(k*(N-m))),k) * Math.pow(((n*(N-m))/(N*(n-k))),n);
console.log(px);
gsea_html += `${px} - ${g.getName()};<br/>`;
console.log(gsea_html);
};
});
});
pluginContainer.find('.panel-events .panel-body').html(gsea_html);
});
// // Define factorial function, to calculate the hypergeometric test
// var f = [];
// function factorial (n) {
// if (n == 0 || n == 1)
// return 1;
// if (f[n] > 0)
// return f[n];
// return f[n] = factorial(n-1) * n;
// };
// function kcomb (n,k) {
// //return factorial(n)/(factorial(k)*(factorial(n - k)));
// //Approximation using upper boud from Eq (2) in http://page.mi.fu-berlin.de/shagnik/notes/binomials.pdf
// var ret = Math.pow(n*Math.E/k,k);
// console.log(ret);
// return ret;
// };
// const highlightDefs = [];
// pathwayObjs.forEach( e => {
......@@ -421,6 +457,41 @@ function calculateGSEA() {
// if (pickedRandomly && globals.pickedRandomly) focus(globals.pickedRandomly);
}
// function highlightSelected(pickedRandomly = false) {
// const highlightDefs = [];
// if (pickedRandomly) {
// if (globals.pickedRandomly) {
// highlightDefs.push({
// element: {
// id: globals.pickedRandomly.id,
// modelId: globals.pickedRandomly.getModelId(),
// type: globals.pickedRandomly.constructor.name.toUpperCase()
// },
// type: "SURFACE",
// options: {
// color: '#00FF00',
// opacity: 0.5
// }
// });
// }
// } else {
// globals.selected.forEach(e => {
// highlightDefs.push({
// element: {
// id: e.id,
// modelId: e.getModelId(),
// type: "ALIAS"
// },
// type: "ICON"
// });
// });
// }
// minervaProxy.project.map.showBioEntity(highlightDefs);
// }
function retrieveUniprot() {
let query = pluginContainer.find('.panel-events .panel-body').text();
query = query.substring(0,query.indexOf(' - '));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment