Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

刘莉莉 实现empirical p value #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gene_expression_scatter_plot/gene_scatter_plot_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<script src="js/tableExport.js"></script>
<script src="js/jquery.base64.js"></script>
<!-- 刘莉莉添加epvalue.js -->
<script src="js/epvalue.js"></script>
<script src="js/empirical_pvalue.js"></script>
<link rel="stylesheet" href="css/animate.min.css">
<link rel="stylesheet" href="css/jquery.gDialog.css">
<script src="js/jquery.gDialog.js"></script>
Expand Down
74 changes: 74 additions & 0 deletions gene_expression_scatter_plot/js/empirical_pvalue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// JavaScript Document
/*
计算 Empirical P-Value,无返回值,计算得到的Empirical P-Value均存储到传过来的数组中,groups与totals.
变量gene1,gene2表示json格式的G1,G2数据数组;
变量datas是main.js里的,存储每个tissue下的坐标;
变量groups是main.js里的,存储每个datas的tissue,相关系数,个数,pvalue信息;
变量totals是main.js里的,记录所有点数的pvalue,Empirical P-Value.
*/
function empirical_p(gene1,gene2,datas,groups,totals){
// count数组用来统计随机排列后每组数据的相关系数大于原排列后的个数
var count = new Array(groups.length+1).fill(0);
// total随机排列后所有点数的pvalue与Empirical P-Value,对应totals
var total = new Array();
// console.log(totals);
// 重复10000次
var round = 10000;
console.log("计算 10000 次");
for(var m = 0;m<round ;m++){
// 随机排列后的data与group,对应datas与groups
var data = new Array();
var group = new Array();
// 得到新的gene1/gene2随机排列后的数组
var newGene1 = randomJson(gene1);
var newGene2 = randomJson(gene2);
// console.log(newGene1);
// num用来遍历点,不断累加,直到最后一个点
var num = 0;
// 计算随机排序后的data
for(var i=0;i<datas.length;i++){
// 用数组记录随机排序后每个tissue里所有点的坐标
data[i] = new Array();
// 遍历坐标值,生成随机排序后每个点的新坐标
for(var j=0;j<datas[i].length;j = j+2){
data[i].push(newGene1[num]);
data[i].push(newGene2[num]);
num++;
}
}
// 使用main.js里的getCorrelationCoefficientAndSetGroups函数,填充group与total数组,获得相关系数
getCorrelationCoefficientAndSetGroups (data,group,total);
// 遍历计算随机排列后每组数据的相关系数大于原排列后的个数
for(var k=0;k<count.length-1;k++){
// 比较的相关系数是绝对值
if(group[k][1]>groups[k][1]){
count[k]++;
}
//比较的是非绝对值的相关系数 k<count.length
// if(total[k]>totals[k]){
// count[k]++;
// }
}
// m是随机循环的参数;在每次循环都比较一下随机排序和原顺序的所有点的相关系数
if(total[m]>totals[0]){
count[groups.length]++;
}
}
// 计算每个tissue的Empirical P-Value,并放在传参的groups里
for(var i=0;i<groups.length;i++){
count[i]=count[i]/round;
groups[i].push(count[i]);
}
// 计算所有点的Empirical P-Value,并放在传参的totals里
totals.push(count[groups.length]/round);
}
// 随机排列数组内的数据
function randomJson(arr){
for (let i = 1; i < arr.length; i++) {
// 产生有效随机数
const random = Math.floor(Math.random() * (i + 1));
// 随机交换数组元素数据
[arr[i], arr[random]] = [arr[random], arr[i]];
}
return arr;
}
71 changes: 51 additions & 20 deletions gene_expression_scatter_plot/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ function condition_specific_correlation(g1, g2, info){
if(a){
//使用json文件内容计算并赋值给tissues, datas, details,gene1,gene2等全局变量,返回值为可绘制种类的数量
var numberOfExecuted = makeJsonIntoArray(G1, G2, INFO, a);
//表格排序
lastLineCorrelationCoefficient = getCorrelationCoefficientAndSetGroups();
//表格排序,传入参数
lastLineCorrelationCoefficient = getCorrelationCoefficientAndSetGroups(datas,groups,totals);
//计算运行时间
console.time("empirical_p");
// 计算Empirical P-Value
empirical_p(gene1,gene2,datas,groups,totals);
console.timeEnd("empirical_p");
// 表格最后一行
lastLineCorrelationCoefficient = "<tr>" + lastLineCorrelationCoefficient + "<td><b>" + totals[1].toFixed(4)+ "<b></td></tr>"
if (numberOfExecuted != 0) {
// 以第二排的相关系数进行排序
sortByColumn(2);
Expand Down Expand Up @@ -299,6 +306,7 @@ function setAndDisplayUtilization (numberOfExecuted, numberOfExecutedInfor, numb
numberOfExecutedInfor + "/" + numberOfInformation;
}
}
// 根据column进行排序
function sortByColumn(numForChangingTheOrderByColumn) {
var str = "";
for (var i = 0; i < groups.length - 1; i++) {
Expand All @@ -309,74 +317,93 @@ function sortByColumn(numForChangingTheOrderByColumn) {
var temp3 = groups[i][2];
var temp4 = groups[i][3];
var temp5 = groups[i][4];
// 给表格添加新的数据,Empirical P-Value
var temp6 = groups[i][5];
groups[i][0] = groups[j][0];
groups[i][1] = groups[j][1];
groups[i][2] = groups[j][2];
groups[i][3] = groups[j][3];
groups[i][4] = groups[j][4];
groups[i][5] = groups[j][5];
groups[j][0] = temp1;
groups[j][1] = temp2;
groups[j][2] = temp3;
groups[j][3] = temp4;
groups[j][4] = temp5;
groups[j][5] = temp6;
}
}
}
//根据选择的列进行排序
switch (numForChangingTheOrderByColumn) {
case 1:
if (numForChangingTheOrderByTissue % 2 == 0) {
for (var i = 0; i < groups.length; i++) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
else {
for (var i = groups.length - 1; i >= 0; i--) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
numForChangingTheOrderByTissue++;
break;
case 2:
if (numForChangingTheOrderByCoefficient % 2 == 0) {
for (var i = 0; i < groups.length; i++) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
else {
for (var i = groups.length - 1; i >= 0; i--) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
numForChangingTheOrderByCoefficient++;
break;
case 3:
if (numForChangingTheOrderByNumber % 2 == 0) {
for (var i = 0; i < groups.length; i++) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
else {
for (var i = groups.length - 1; i >= 0; i--) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
numForChangingTheOrderByNumber++;
break;
case 4:
if (numForChangingTheOrderByP % 2 == 0) {
for (var i = 0; i < groups.length; i++) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
else {
for (var i = groups.length - 1; i >= 0; i--) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td></tr>";
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
numForChangingTheOrderByP++;
break;
case 6:
if (numForChangingTheOrderByEP % 2 == 0) {
for (var i = 0; i < groups.length; i++) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
else {
for (var i = groups.length - 1; i >= 0; i--) {
str = str + "<tr><td>" + groups[i][0] + "</td><td>" + groups[i][1].toFixed(5) + groups[i][4] + "</td><td>" + groups[i][2] + "</td><td>" + makeNumberToStringAndExponential(groups[i][3]) + "</td><td>" +groups[i][5].toFixed(4) + "</td></tr>";
}
}
numForChangingTheOrderByEP++;
break;
}
str = "<tr><th><p onclick='sortByColumn(1);'>" + strTissueSettings + "</p></th><th><p onclick='sortByColumn(2);'>Correlation Coefficient</p></th><th><p onclick='sortByColumn(3);'>Number</p></th><th style='width: 200px;'><p onclick='sortByColumn(4);'>P Value (one-tail)</p></th></tr>" +
// 点击相应的column就可以进行该column的排序
str = "<tr><th><p onclick='sortByColumn(1);'>" + strTissueSettings + "</p></th><th><p onclick='sortByColumn(2);'>Correlation Coefficient</p></th><th><p onclick='sortByColumn(3);'>Number</p></th><th style='width: 200px;'><p onclick='sortByColumn(4);'>P Value (one-tail)</p></th><th><p onclick='sortByColumn(6);'>Empirical P-Value</p></th></tr>" +
str + lastLineCorrelationCoefficient;
document.getElementById("id_main_output_table").innerHTML = str;
}
Expand Down Expand Up @@ -659,7 +686,8 @@ function isExistInDetailMatchs(detail) {
}
return false;
}
function getCorrelationCoefficientAndSetGroups () {
// 计算相关系数并为groups赋值,并用形参来代替全局变量
function getCorrelationCoefficientAndSetGroups (data,group,total) {
var tail = 1;
var str = "";
var correlationCoefficient1 = 0;
Expand All @@ -672,7 +700,7 @@ function getCorrelationCoefficientAndSetGroups () {
var squareOfAverageY1 = 0;
var averageXTimesAverageY1 = 0;
var allN = 0;
for (var i = 0; i < datas.length; i++) {
for (var i = 0; i < data.length; i++) {
var correlationCoefficient = 0;
var sumOfSquareOfX = 0;
var sumOfSquareOfY = 0;
Expand All @@ -682,9 +710,9 @@ function getCorrelationCoefficientAndSetGroups () {
var squareOfAverageX = 0;
var squareOfAverageY = 0;
var averageXTimesAverageY = 0;
for (var j = 0; j < datas[i].length; j = j + 2) {
var x = datas[i][j];
var y = datas[i][j + 1];
for (var j = 0; j < data[i].length; j = j + 2) {
var x = data[i][j];
var y = data[i][j + 1];
sumOfSquareOfX += x * x;
sumOfSquareOfY += y * y;
sumOfXTimesY += x * y;
Expand All @@ -697,7 +725,8 @@ function getCorrelationCoefficientAndSetGroups () {
averageY1 += y;
allN++;
}
var doN = datas[i].length / 2;
// doN为同个issue的点的个数
var doN = data[i].length / 2;
averageX /= doN;
averageY /= doN;
squareOfAverageX = averageX * averageX;
Expand All @@ -713,7 +742,8 @@ function getCorrelationCoefficientAndSetGroups () {
sign = " (NaN)";
}
var pValue = correlationCoefficientToPValue(correlationCoefficient, doN, 1);
groups[i] = new Array(tissues[i], Math.abs(correlationCoefficient), doN, pValue, sign);
//total.push(correlationCoefficient); //如果比较非绝对值的相关系数
group[i] = new Array(tissues[i], Math.abs(correlationCoefficient), doN, pValue, sign);
}
averageX1 /= allN;
averageY1 /= allN;
Expand All @@ -729,8 +759,9 @@ function getCorrelationCoefficientAndSetGroups () {
correlationCoefficient1 = 0;
sign = " (NaN)";
}
var pTemp = correlationCoefficientToPValue(correlationCoefficient1, allN, 1);
return "<tr><td><b>total</b></td><td><b>" + correlationCoefficient1.toFixed(5) + sign + "</b></td><td><b>" + allN + "</b></td><td><b>" + makeNumberToStringAndExponential(pTemp) + "<b></td></tr>";
var totalPTemp = correlationCoefficientToPValue(correlationCoefficient1, allN, 1);
total.push(correlationCoefficient1);
return "<td><b>total</b></td><td><b>" + correlationCoefficient1.toFixed(5) + sign + "</b></td><td><b>" + allN + "</b></td><td><b>" + makeNumberToStringAndExponential(totalPTemp) + "<b></td>";
}
function getTissues() {
return this.tissues;
Expand Down