-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAlgorithmUtilities.js
More file actions
243 lines (223 loc) · 6.58 KB
/
AlgorithmUtilities.js
File metadata and controls
243 lines (223 loc) · 6.58 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var start = process.hrtime();
var elapsed_time = function (note) {
var precision = 3; // 3 decimal places
var elapsed = process.hrtime(start)[1] / 1000000000; // divide by a billion to get nano to seconds
var time = process.hrtime(start)[0] + " s, " + elapsed.toFixed(precision) + " ms - " + note;
start = process.hrtime(); // reset the timer
return time;
}
/*
<summary>
Replaces all instances of the regex find by the string replace
</summary>
<param name='find'>The regex to look for</param>
<param name='replace'>The string to replace 'find' by</param>
<return>The modified string</return>
*/
String.prototype.replaceAll = function(find,replace)
{
return this.replace(new RegExp(find, 'g'), replace);
}
/*
<summary>
Complements the DNA or RNA string
</summary>
<param name='oligo'>The DNA/RNA string to complement</param>
<param name='isRna'>(Optional) Whether it is RNA. Defaults to true</param>
<return>The modified string</return>
*/
function Complement( oligo , isRna )
{
if(isRna == undefined)
isRna = true;
var res = new Array();
for(var ii = 0; ii < oligo.length; ++ii)
{
var c = oligo[ii];
switch(c)
{
case 'G':
res.push('C');
break;
case 'C':
res.push('G');
break;
case 'A':
if(isRna)
res.push('U');
else
res.push('T');
break;
case 'U':
case 'T':
res.push('A');
break;
default:
res.push(c);
}
}
return res.join('');
}
/*
<summary>
Reverses a string
</summary>
<param name='oligo'>String to reverse</param>
<param name='isRna'>(Optional) Whether it is RNA. Defaults to true</param>
<return>The modified string</return>
*/
function Reverse( oligo )
{
return oligo.split("").reverse().join("");
}
/*
<summary>
Reverse complements a RNA string
</summary>
<param name='oligo'>String to reverse complement</param>
<return>The modified string</return>
*/
function ReverseComplement(oligo)
{
return Complement(Reverse(oligo));
}
/*
<summary>
Finds the sequence length ignoring all whitespace and non-DNA/RNA characters
</summary>
<param name='oligo'>The string to find the number of base pairs</param>
<return>The number of base pairs</return>
*/
function SequenceLength (oligo)
{
var res = 0;
for(var ii = 0; ii < oligo.length; ++ii)
{
var c = oligo[ii];
switch(c)
{
case 'G':
res +=1;
break;
case 'C':
res +=1;
break;
case 'A':
res +=1;
break;
case 'U':
case 'T':
res +=1;
break;
}
}
return res;
}
/*
<summary>
Transforms a sequence of RNA to DNA
</summary>
<param name='seq'>The sequence to change</param>
<return>The changed string</return>
*/
function RnaToDna(seq)
{
return seq.replaceAll('U','T');
}
/*
<summary>
Transforms a sequence of DNA to RNA
</summary>
<param name='seq'>The sequence to change</param>
<return>The changed string</return>
*/
function DnaToRna(seq)
{
return seq.replaceAll('T','U');
}
/*
<summary>
Takes a StructureInfo object and compresses the object { left: '', right:'', type ''}
into a string "l,r,t" , which reduces bandwidth and memory space.
</summary>
<param name='structureInfo'>The sequence to change</param>
<return>The changed string</return>
*/
function CompressStructureInfo(structureInfo) {
var struct = structureInfo;
var pairs = struct.ConnectedPairs;
var compressed = new Array();
for (var mm = 0; mm < pairs.length; ++mm) {
compressed.push(pairs[mm].left + ',' + pairs[mm].right + ',' + pairs[mm].type);
}
struct.ConnectedPairs = compressed;
}
/*
<summary>
Compresses an array of similar objects into a table that does no information repetition.
</summary>
<param name='arrayOfObjects'>The sequence to change</param>
<param name='PropertiesToCompress'>The sequence to change</param>
<return>Changes the array into a compressed version</return>
*/
function CompressObjectArrayIntoTable(arrayOfObjects, PropertiesToCompress) {
var compressed = new Array();
//First row is table key (column names)
var compressedObj = new Array();
for (var jj = 0; jj < PropertiesToCompress.length; ++jj) {
compressedObj.push(PropertiesToCompress[jj]);
}
//Push Key
compressed.push(compressedObj);
for (var ii = 0; ii < arrayOfObjects.length; ++ii) {
var candidate = arrayOfObjects[ii];
compressedObj = new Array();
for (var jj = 0; jj < PropertiesToCompress.length; ++jj) {
compressedObj.push(candidate[PropertiesToCompress[jj]]);
}
compressed.push(compressedObj);
}
arrayOfObjects.length = 0;
for (var ii = 0 ; ii < compressed.length; ++ii) {
arrayOfObjects.push(compressed[ii]);
}
}
/*
<summary>
Compresses an array of candidates according to their properties
</summary>
<param name='candidates'>The array of candidates to compress</param>
<return>Changes the array into a compressed version</return>
*/
function CompressCandidates(candidates) {
for (var ii = 0; ii < candidates.length ; ++ii) {
var structuresFold = candidates[ii].StructuresSFold;
CompressObjectArrayIntoTable(structuresFold, ["EnergyInterval", "Frequency", "LowestFreeEnergy", "ConnectedPairs", "Fitness"]);
}
CompressObjectArrayIntoTable(candidates, ["Sequence", "CataliticCoreStart", "ID", "StructuresSFold", "StructureUnaFold", "Fitness_Shape", "Fitness_Target", "Fitness_Target_dG", "Fitness_Specificity", "CataliticCoreType", "cutSiteID", "cutSiteLocation", "requestID", "MeltingTemperature","MeltingTemperatureLeft","MeltingTemperatureRight", "LeftArmLength", "RightArmLength", "rank"]);
}
var deleteFolderRecursive = function (path) {
var fs = require('fs');
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
exports.SequenceLength = SequenceLength;
exports.ReverseComplement = ReverseComplement;
exports.Reverse = Reverse;
exports.Complement = Complement;
exports.DnaToRna = DnaToRna;
exports.RnaToDna = RnaToDna;
exports.ElapsedTime = elapsed_time;
exports.CompressStructureInfo = CompressStructureInfo;
exports.CompressObjectArrayIntoTable = CompressObjectArrayIntoTable;
exports.CompressCandidates = CompressCandidates;
exports.DeleteFolderRecursive = deleteFolderRecursive;