-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·252 lines (232 loc) · 6.12 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs')
const minimist = require('minimist')
const through = require('through2')
const ndjson = require('ndjson')
const seq = require('./index.js')
const argv = minimist(process.argv.slice(2), {
alias: {
input: 'i',
output: 'o',
help: 'h'
}
})
// contains the available commands, as well as the function and necessary parameters
const commands = {
'check-type': {
'func': seq.checkType,
'requiredParams': [
'sequence'
],
'optionalParams': [
'threshold',
'length',
'index'
]
},
'reverse': {
'func': seq.reverse,
'requiredParams': [
'sequence'
],
'optionalParams': []
},
'revese-comp': {
'func': seq.reverseComplement,
'requiredParams': [
'sequence'
],
'optionalParams': []
},
'remove-introns': {
'func': seq.removeIntrons,
'requiredParams': [
'sequence',
'exonsRanges'
],
'optionalParams': []
},
'transcribe': {
'func': seq.transcribe,
'requiredParams': [
'sequence'
],
'optionalParams': [
'exonsRanges'
]
},
'translate': {
'func': seq.translate,
'requiredParams': [
'sequence'
],
'optionalParams': [
'exonsRanges'
]
},
'reverse-exons': {
'func': seq.reverseExons,
'requiredParams': [
'reverseExons',
'referenceLength'
],
'optionalParams': []
},
'non-canonical-splices': {
'func': seq.findNonCanonicalSplices,
'requiredParams': [
'sequence',
'exonsRanges'
],
'optionalParams': []
},
'check-canonical-start': {
'func': seq.checkCanonicalTranslationStartSite,
'requiredParams': [
'sequence'
],
'optionalParams': []
},
'get-reading-frames': {
'func': seq.getReadingFrames,
'requiredParams': [
'sequence'
],
'optionalParams': []
},
'get-open-reading-frames': {
'func': seq.getOpenReadingFrames,
'requiredParams': [
'sequence'
],
'optionalParams': []
},
'get-all-open-reading-frames': {
'func': seq.getAllOpenReadingFrames,
'requiredParams': [
'sequence'
],
'optionalParams': []
},
'find-longest-open-reading-frame': {
'func': seq.findLongestOpenReadingFrame,
'requiredParams': [
'sequence'
],
'optionalParams': [
'frameSymbol'
]
}
}
const options = function () {
console.log(
'\nOptions:\n' +
'-i, --input\t input file\n' +
'-o, --output\t output file\n' +
'-h, --help\t display this message'
)
}
const usage = function () {
console.log(
'Usage: bionode-seq <command> <options>\n' +
'If no output is provided, the result will be printed to stdout\n' +
'To view help for a specific command: bionode-seq <command>\n\n' +
'Commands:'
)
for (const command in commands) {
console.log('\t' + command)
}
options()
process.exit(0)
}
const commandUsage = function (command) {
console.log(
'Usage: bionode-seq ' + command + ' <options>\n\n' +
'Required parameters supplied in ndJSON format:'
)
const requiredParams = commands[command].requiredParams
for (const i in requiredParams) {
console.log('\t' + requiredParams[i])
}
console.log('\nOptional parameters supplied in ndJSON format:')
const optionalParams = commands[command].optionalParams
if (optionalParams.length === 0) {
console.log('\tNone')
} else {
for (const i in optionalParams) {
console.log('\t' + optionalParams[i])
}
}
options()
console.log('Check bionode-seq/lib/bionode-seq.js for specific formats for paramters.')
process.exit(0)
}
const RequiredParamException = function (command, param) {
this.message = 'The paramter: "' + param + '" was not provided and is required for ' + command
}
const runSeqFunction = function (command, params) {
// check if the required parameters are satisfied for this command
const requiredParams = commands[command].requiredParams
for (const i in requiredParams) {
if (!params.hasOwnProperty(requiredParams[i])) {
throw new RequiredParamException(command, requiredParams[i])
}
}
const optionalParams = commands[command].optionalParams
let result
// call the function!
const seqFunc = commands[command].func
if (requiredParams.length === 1 && optionalParams.length === 0) {
// only one required parameter, no optional
result = seqFunc(params[requiredParams[0]])
} else if (requiredParams.length === 1 && optionalParams.length === 1) {
// only one required parameter, one optional parameter
result = seqFunc(params[requiredParams[0]], params[optionalParams[0]])
} else if (requiredParams.length === 2 && optionalParams.length === 0) {
// two required parameters, no optional
result = seqFunc(params[requiredParams[0]], params[requiredParams[1]])
} else if (requiredParams.length === 1 && optionalParams.length === 3) {
// one required parameter, 3 optional parameters
result = seqFunc(
params[requiredParams[0]],
params[optionalParams[0]],
params[optionalParams[1]],
params[optionalParams[2]]
)
}
return result
}
// display general usage message
if (argv.help || argv._.length === 0) {
usage()
}
const command = argv._[0]
// check if a valid command is provided
if (!commands.hasOwnProperty(command)) {
console.log(command + ' is not a valid command.\n')
usage()
}
// display specific command usage message
if (argv._.length === 1 && !argv.input) {
commandUsage(command)
}
// if there is an output file present, write to the file. Otherwise write to stdout
const output = argv.output ? fs.createWriteStream(argv.output) : process.stdout
const serialize = ndjson.serialize()
// stream the input file into the seq parser
fs.createReadStream(argv.input)
.pipe(ndjson.parse())
.pipe(through.obj(function (data, enc, cb) {
this.push(runSeqFunction(command, data))
// get next object in stream
cb()
}))
.pipe(serialize)
.pipe(output)
.on('error', function (err) {
console.log('There was an error:\n', err)
})
.on('end', function () {
serialize.end()
output.close()
})