-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·173 lines (139 loc) · 4.4 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
#!/usr/bin/env node
'use strict'
const publicIp = require('public-ip')
const meow = require('meow')
const got = require('got')
const WebSocket = require('ws')
const prettyMs = require('pretty-ms')
const fs = require('fs')
const path = require('path')
const mkdir = require('make-dir')
const updateNotifier = require('update-notifier')
const pkg = require('./package.json')
// notify the user when updates are available
updateNotifier({pkg}).notify()
function args() {
let args = meow({
help: `
Usage
$ hybsearch [--server|--thing3] [--pipeline=mbnb] [--outDir] <file.gb>
Arguments
<file.gb|file.fasta> An input file, either in GenBank or FASTA format.
Options
--server A Hybsearch server URL (defaults to thing3.cs.stolaf.edu:80)
--thing3 Sets --server to thing3.cs.stolaf.edu:80
--thing3-dev Sets --server to thing3.cs.stolaf.edu:81
--localhost Sets --server to localhost:8080
--pipeline Picks a pipeline from the server (defaults to "mbnb")
--out-dir Where to save the results of each step. If omitted, results
are not saved to the local machine.
`,
flags: {
server: {
type: 'string',
default: 'thing3.cs.stolaf.edu:80',
},
thing3Dev: {
type: 'boolean',
default: false,
},
thing3: {
type: 'boolean',
default: false,
},
localhost: {
type: 'boolean',
default: false,
},
pipeline: {
type: 'string',
default: 'mbnb',
},
outDir: {
type: 'string',
},
},
})
if (args.flags.thing3) {
args.flags.server = 'thing3.cs.stolaf.edu:80'
} else if (args.flags.thing3Dev) {
args.flags.server = 'thing3.cs.stolaf.edu:81'
} else if (args.flags.localhost) {
args.flags.server = 'localhost:8080'
}
return args
}
async function initSocket(url) {
return new Promise((resolve, reject) => {
let wsUrl = `ws://${url}`
let socket = new WebSocket(wsUrl)
socket.addEventListener('open', async () => {
resolve(socket)
})
socket.addEventListener('error', reject)
return socket
})
}
function onMessage(packet, outDir, argv) {
let { type, payload } = JSON.parse(packet)
if (type === 'stage-start') {
const { stage } = payload
console.log(`${stage}: start`)
} else if (type === 'stage-complete') {
const { stage, timeTaken, result, cached } = payload
console.log(`${stage}: completed in ${prettyMs(timeTaken)}${cached ? ' (cached)' : ''}`)
if (outDir) {
if (typeof result === 'string') {
fs.writeFileSync(path.join(outDir, stage), result + '\n', 'utf-8')
} else {
fs.writeFileSync(path.join(outDir, stage), JSON.stringify(result, null, '\t') + '\n', 'utf-8')
}
}
} else if (type === 'error') {
let { error } = payload
console.error(error)
socket.close()
} else if (type === 'exit') {
console.info('server exited')
socket.close()
} else {
console.warn(`unknown cmd "${type}"`)
}
}
let socket
async function main() {
let argv = args()
let {server, outDir, pipeline} = argv.flags
let [file] = argv.input
console.log('connecting to', server, 'to run', pipeline)
socket = await initSocket(server)
try {
console.log('connected to', server)
let httpUrl = `http://${server}`
let [uptime, pipelines] = await Promise.all([
got(`${httpUrl}/uptime`, {json: true}).then(r => r.body.uptime),
got(`${httpUrl}/pipelines`, {json: true}).then(r => r.body.pipelines),
])
console.log('server uptime:', prettyMs(uptime))
if (!pipelines.includes(pipeline)) {
throw new Error(`${server} does not understand the pipeline "${pipeline}"`)
}
let data = fs.readFileSync(file, 'utf-8')
let destDir = outDir ? path.join(outDir, path.basename(file)) : null
if (destDir) {
await mkdir(destDir)
}
socket.addEventListener('message', packet => onMessage(packet.data, destDir, argv))
let ip = (await publicIp.v6()) || (await publicIp.v4())
let payload = { type: 'start-pipeline', pipeline, ip, filepath: file, data, options: { "outlierRemovalPercentage": 0.2, "beastChainLength": "10000000"} }
socket.send(JSON.stringify(payload), err => {
if (err) {
console.error('server error', err)
}
})
} catch (e) {
socket.close()
throw e
}
}
main()