-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (142 loc) · 4.73 KB
/
index.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
#!/usr/bin/env node
/**
* Created by alo on 2/20/17.
*/
var fs = require('fs'),
byline = require('byline');
var isaCache = {};
var fsns = {};
var relationshipsSnapshot = "[Enter RF2 Relationships Snapshot location with -r argument]";
var descriptionsSnapshot = "[Enter RF2 Descriptions Snapshot location with -r argument]";
var inputList = "[Enter input list with -i argument]";
process.argv.forEach(function (val, index, array) {
if (val == "-r") {
if (!process.argv[index+1]) {
throw ("Missing release file parameter after -r");
} else {
relationshipsSnapshot = process.argv[index+1];
}
}
if (val == "-d") {
if (!process.argv[index+1]) {
throw ("Missing release file parameter after -r");
} else {
descriptionsSnapshot = process.argv[index+1];
}
}
if (val == "-i") {
if (!process.argv[index+1]) {
throw ("Missing input file parameter after -r");
} else {
inputList = process.argv[index+1];
}
}
});
var stream = byline(fs.createReadStream(relationshipsSnapshot, { encoding: 'utf8' }));
var count = 0;
stream.on('data', function(line) {
count++;
if (count%100000==0) console.log("Relationships:",count);
var columns = line.split("\t");
if (columns[7] == "116680003" && columns[2] == "1") {
if (!isaCache[columns[4]]) {
isaCache[columns[4]] = {isas : []};
}
isaCache[columns[4]].isas.push(columns[5]);
}
});
stream.on('end', function() {
console.log("Isa Cache ready");
var stream = byline(fs.createReadStream(descriptionsSnapshot, { encoding: 'utf8' }));
var count = 0;
stream.on('data', function(line) {
count++;
if (count%100000==0) console.log("Descriptions:",count);
var columns = line.split("\t");
if (columns[6] == "900000000000003001" && columns[2] == "1") {
if (!fsns[columns[4]]) {
fsns[columns[4]] = {};
}
fsns[columns[4]].fsn = columns[7];
}
});
var report = "ConceptId\tFSN\tAncestor ConceptId\tAncestor FSN\n";
stream.on('end', function() {
console.log("FSNs Cache ready");
var stream = byline(fs.createReadStream(inputList, { encoding: 'utf8' }));
var count = 0;
stream.on('data', function(line) {
count++;
if (count%100==0) console.log("Substances:",count);
var columns = line.split("\t");
var descendantName = (fsns[columns[0]]) ? fsns[columns[0]].fsn : columns[1];
getAncestors(columns[0]).forEach(function(loopAncestor) {
report+=columns[0] + "\t" + descendantName +
"\t" + loopAncestor + "\t" + fsns[loopAncestor].fsn + "\n";
});
});
stream.on('end', function() {
fs.writeFile("ancestorsForList.txt", report, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
});
});
var getAncestors = function(descendant, ancestorsList) {
if (!ancestorsList) ancestorsList = [];
if (isaCache[descendant]) {
isaCache[descendant].isas.forEach(function(loopParent) {
if (!ancestorsList.includes(loopParent)) {
ancestorsList.push(loopParent);
}
});
isaCache[descendant].isas.forEach(function(loopParent) {
ancestorsList = getAncestors(loopParent, ancestorsList);
});
}
return ancestorsList;
};
var isDescendant = function(ancestor, descendant) {
var result = false;
if (isaCache[descendant]) {
if (isaCache[descendant].isas.includes(ancestor)) {
result = true;
} else {
isaCache[descendant].isas.forEach(function(loopParent) {
result = isDescendant(ancestor, loopParent);
});
}
}
return result;
};
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) {
return true;
}
k++;
}
return false;
};
}