-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw2ysnps.js
169 lines (142 loc) · 3.68 KB
/
raw2ysnps.js
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
function cmp(a,b){
return a-b;
}
function readTxtFile(file, callback){
var reader = new FileReader();
reader.onload = function(){
callback(reader.result);
};
reader.readAsText(file);
}
function parseRaw(raw, callback){
console.log("parse raw");
var rows = raw.split("\n");
//remove commented section of 23andme raw data file
while(rows[0][0]=="#"){
rows.splice(0,1);
}
//remove non-Y rows from the top
while(rows[0].split("\t")[1] != "Y"){
rows.splice(0,1);
}
//remove non-Y rows from the bottom
while(rows[rows.length-1].split("\t")[1] != "Y"){
rows.splice(rows.length-1,1);
}
//pack the data into array
var data=[];
for(var i=0;i<rows.length;++i){
data.push(rows[i].split("\t"));
//delete "\r" from the code
data[i][2] = Number(data[i][2]);
data[i][3]=data[i][3].split("\r")[0];
}
//delete duplicates with same position
data.sort(function(a,b){
return a[2]>b[2];
});
for(var i=0;i<data.length-1;++i){
while(data[i][2]==data[i+1][2]){
data[i][0]+="/"+data[i+1][0];
data.splice(i+1,1);
}
}
if(data[data.length-1][0]=="") data.pop(); //trim the array
callback(data);
}
function parseDB(db, callback){
console.log("parse db");
var rows = db.split("\n");
//remove the label row
rows.splice(0,1);
var data=[];
for(var i=0;i<rows.length;++i){
data.push(rows[i].split(","));
data[i][1] = Number(data[i][1]);
}
data.sort(function(a,b){
return a[1]>b[1];
});
//merge duplicate positions
for(var i=0;i<data.length-1;++i){
while(data[i][1]==data[i+1][1]){
data[i][0]+="/"+data[i+1][0];
data.splice(i+1,1);
}
}
if(data[data.length-1][0]=="") data.pop(); //trim the array
callback(data);
}
function makeList(array, sign) {
var list = array[0]+sign;
for(var i = 1; i < array.length; ++i) {
list += ", " + array[i]+sign;
}
return list;
}
function filesReady(t1, t2){
console.log("files ready");
parseRaw(t1,function(raw){
parseDB(t2,function(db){
console.log("compare arrays");
//compare two arrays
//db[][1] vs raw[][2] for position
//db[][2] vs raw[][3] for snp
var positive=[];
var negative=[];
var nocall=[];
//var error=[];
var last=0;
for(var i=0;i<db.length;++i){
for(var j=last;j<raw.length;++j){
if(db[i][1]>raw[j][2]){
last=j+1;
break;
}else if(db[i][1]==raw[j][2]){
last=j+1;
switch(raw[j][3]){
case "--":
nocall.push(db[i][0]);
break;
case db[i][2].split("->")[1]:
positive.push(db[i][0]);
break;
case db[i][2].split("->")[0]:
negative.push(db[i][0]);
break;
default:
//error.push(db[i][0]);
break;
}
break;
}
}
}
//sort the arrays
positive.sort(cmp);
negative.sort(cmp);
nocall.sort(cmp);
//error.sort(cmp);
console.log("display results");
console.log("Positive: " + positive.length + ", Negative: " + negative.length + ", No call: " + nocall.length);
//display results
document.getElementById('results').textContent="Results (positive first):";
document.getElementById('res').textContent=makeList(positive, "+")+", "+ makeList(negative, "-");
document.getElementById('nocall').textContent="No call:";
document.getElementById('ncs').textContent=makeList(nocall, "");
//document.getElementById('err').textContent=makeList(error);
});
});
}
function start(){
var fraw = document.getElementById("raw").files[0];
var fdb = document.getElementById("db").files[0];
//read files
if(fraw && fdb){
readTxtFile(fraw, function(text1){
readTxtFile(fdb, function(text2){
filesReady(text1, text2);
});
});
}else alert("One or more files is missing!");
}