This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
313 lines (291 loc) · 13.2 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// Genonym (v1.0.1)
// Written and created by Will Brickner
//
// ----------------------------------------------------
//
// LICENSE
//
// This work uses the GPL-3.0 license.
// Please see (https://www.gnu.org/licenses/gpl-3.0.en.html)
// if you are not familiar with this license.
//
// ----------------------------------------------------
//
// DISCLAIMER
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
var fs = require("fs")
, path = require("path")
, moduleConfig = {
speciesPath: "./species/",
logLevel: "normal"
}
, species = { }
module.exports = {
init: function init(config) {
if (typeof config === "string") {
// lookup the config file
moduleConfig = JSON.parse(fs.readFileSync(config, "utf8"));
} else if (typeof config === "object") {
for (var setting in config) {
moduleConfig[setting] = config[setting];
}
}
// (The default config is used if none is provided)
// Manipulate the data to be more easily usable
moduleConfig.logLevel = moduleConfig.logLevel.toLowerCase();
// Now detect any species conversion files
var files = fs.readdirSync(moduleConfig.speciesPath);
for (var j = 0, jlen = files.length; j < jlen; ++j) {
// we only want JSON files
if (files[j].substr(files[j].length - 5, 5).toLowerCase() === ".json") {
// check that it really is a file (it could be a directory, eg ./directory.json/)
let stats = fs.statSync(path.join(moduleConfig.speciesPath, files[j]));
if (stats.isFile() === true) {
// Try to parse the file as JSON. If there is a name provided in the JSON, then use that name.
// If no name is provided or the name provided exists already in the `species` object,
// use the filename without the extension.
try {
let parsed = JSON.parse(fs.readFileSync(path.join(moduleConfig.speciesPath, files[j]), "utf8"))
, speciesName = parsed.speciesName || files[j].substr(0, files[j].length - 5)
if (typeof species[speciesName] !== "undefined") {
speciesName = files[j].substr(0, files[j].length - 5)
}
if (typeof parsed.codonToAmino !== "object" || typeof parsed.aminoToCodon !== "object") {
// TODO: try to fix broken species files automatically
if (moduleConfig.logLevel !== "silent") {
console.error(`[Genonym]\tError: The species file "${files[j]}" ${parsed.speciesName ? " (\"" + parsed.speciesName + "\")" : ""} was missing some critical data. This species was not loaded.`)
}
} else {
parsed.filepath = files[j]
species[speciesName] = parsed
}
} catch (e) {
// Assume any exception is because of invalid JSON
if (moduleConfig.logLevel !== "silent") {
console.error(`\n[Genonym]\tWarning: the file "${files[j]}" is not valid. It could not be added as a species.\n`)
}
}
}
}
}
if (moduleConfig.logLevel === "verbose") {
console.log(`[Genonym]\tDetected and loaded ${files.length} species from '${moduleConfig.speciesPath}'.`)
}
},
DNAToRNA: function DNAToRNA(obj, cb) {
let RNASequence = new Array(obj.sequence.length)
, DNAToRNA_map = species[obj.speciesName].DNAToRNA
for (var j = 0, jlen = obj.sequence.length; j < jlen; ++j) {
RNASequence[j] = DNAToRNA_map[obj.sequence[j]]
}
// provide the output
if (typeof cb === "function") {
return cb(null, RNASequence.join(""))
} else {
return {
err: null,
sequence: RNASequence.join("")
}
}
},
RNAToDNA: function RNAToDNA(obj, cb) {
let DNASequence = new Array(obj.sequence.length)
, RNAToDNA_map = species[obj.speciesName].RNAToDNA
for (var j = 0, jlen = obj.sequence.length; j < jlen; ++j) {
DNASequence[j] = RNAToDNA_map[obj.sequence[j]]
}
if (typeof cb === "function") {
return cb(null, DNASequence.join(""))
} else {
return {
err: null,
sequence: DNASequence.join("")
}
}
},
convert: function convert(obj, cb) {
// check for invalidity in the parameters
let errors = []
if (typeof obj.sequenceType !== "string") {
if (moduleConfig.logLevel !== "silent") {
console.error(`[Genonym]\tError: The 'sequenceType' property must be a String, it is of type "${typeof obj.sequenceType}".`)
}
errors.push("Invalid_Property")
}
if (typeof obj.outputType !== "string") {
if (moduleConfig.logLevel !== "silent") {
console.error(`[Genonym]\tError: The 'outputType' property must be a String, it is of type "${typeof obj.outputType}".`)
}
errors.push("Invalid_Property")
}
if (typeof obj.sequence !== "string") {
if (moduleConfig.logLevel !== "silent") {
console.error(`[Genonym]\tError: The 'sequence' property must be a String, it is of type "${typeof obj.sequence}".`)
}
errors.push("Invalid_Property")
}
if (typeof obj.inputSpecies !== "string") {
if (moduleConfig.logLevel !== "silent") {
console.error(`[Genonym]\tError: The 'inputSpecies' property must be a String, it is of type "${typeof obj.inputSpecies}".`)
}
errors.push("Invalid_Property")
}
if (typeof obj.outputSpecies !== "string") {
if (moduleConfig.logLevel !== "silent") {
console.error(`[Genonym]\tError: The 'outputSpecies' property must be a String, it is of type "${typeof obj.outputSpecies}".`)
}
errors.push("Invalid_Property")
}
// check that these species exist
if (typeof species[obj.inputSpecies] === "undefined") {
if (moduleConfig.logLevel !== "silent") {
console.error("[Genonym]\tError: The 'inputSpecies' you chose does not exist in the current context.")
}
errors.push("No_Such_Species")
}
if (typeof species[obj.outputSpecies] === "undefined") {
if (moduleConfig.logLevel !== "silent") {
console.error("[Genonym]\tError: The 'outputSpecies' you chose does not exist in the current context.")
}
errors.push("No_Such_Species")
}
// check for any errors that were detected
if (errors.length !== 0) {
if (typeof cb === "function") {
cb(errors, null)
} else {
return {
err: errors,
sequence: null
}
}
}
let sequenceType = obj.sequenceType.toUpperCase()
, outputType = obj.outputType.toUpperCase()
obj.sequence = obj.sequence.toUpperCase()
// now do the conversion
if (sequenceType === "DNA") {
// convert to RNA, then to amino acid representation, and then back to DNA
module.export.DNAToRNA({
speciesName: obj.inputSpecies,
sequence: obj.sequence
}, (err, RNA) => {
// cache this
let inputSpeciesCodonToAmino = species[obj.inputSpecies].codonToAmino
, outputSpeciesAminoToCodon = species[obj.outputSpecies].aminoToCodon;
// intermediate amino acid representation
let outputSequence = new Array(Math.floor(RNA.length / 3))
for (var j = 0, k = 0, jlen = Math.floor(RNA.length / 3); j < jlen; ++j, k += 3) {
outputSequence[j] = outputSpeciesAminoToCodon[inputSpeciesCodonToAmino[RNA.substr(k, 3)]]
}
// provide the output
if (typeof cb === "function") {
cb(null, outputSequence.join(""))
}
else {
return {
err: null,
sequence: outputSequence.join("")
}
}
})
}
else if (sequenceType === "RNA") {
// cache this
let inputSpeciesCodonToAmino = species[obj.inputSpecies].codonToAmino
, outputSpeciesAminoToCodon = species[obj.outputSpecies].aminoToCodon
// intermediate amino acid representation
let outputSequence = new Array(Math.floor(obj.sequence.length / 3))
for (var j = 0, k = 0, jlen = Math.floor(obj.sequence.length / 3); j < jlen; ++j, k += 3) {
outputSequence[j] = outputSpeciesAminoToCodon[inputSpeciesCodonToAmino[obj.sequence.substr(k, 3)]]
}
// if the user wants the optimized sequence in RNA, we already have it
if (outputType === "RNA") {
// provide the output
if (typeof cb === "function") {
cb(null, outputSequence.join(""))
}
else {
return {
err: null,
sequence: outputSequence.join("")
}
}
} else if (outputType === "DNA") {
// if the user wants the optimized sequence returned in DNA,
// convert the RNA back into DNA and then return it.
module.export.RNAToDNA({
speciesName: obj.outputSpecies,
sequence: outputSequence.join("")
}, (err, DNA) => {
// provide the output
if (typeof cb === "function") {
cb(null, DNA);
}
else {
return {
err: null,
sequence: DNA
}
}
})
}
}
else if (sequenceType === "PROTEIN") {
// the input is already a sequence of amino acids, and the user wants it returned
// as an optimized sequence of DNA or RNA
let outputSpeciesAminoToCodon = species[obj.outputSpecies].aminoToCodon
// intermediate amino acid representation
let outputSequence = new Array(Math.floor(obj.sequence.length / 3))
for (var j = 0, k = 0, jlen = Math.floor(obj.sequence.length / 3); j < jlen; ++j, k += 3) {
outputSequence[j] = outputSpeciesAminoToCodon[obj.sequence[j]]
}
// if the user wants the sequence returned as RNA, we already have it so simply provide it.
if (outputType === "RNA") {
// provide the output
if (typeof cb === "function") {
cb(null, outputSequence.join(""))
}
else {
return {
err: null,
sequence: outputSequence.join("")
}
}
} else if (outputType === "DNA") {
// the user wants the sequence returned as DNA, so we need to convert the RNA to a DNA sequence,
// and then return it.
module.export.RNAToDNA({
speciesName: obj.speciesName,
sequence: outputSequence.join("")
}, (err, DNA) => {
// provide the output
if (typeof cb === "function") {
cb(null, DNA);
}
else {
return {
err: null,
sequence: DNA
}
}
})
}
}
}
}