-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarvis-download.js
364 lines (330 loc) · 11.7 KB
/
varvis-download.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env node
const { CookieJar } = require('tough-cookie');
const { CookieClient } = require('http-cookie-agent/undici');
const yargs = require('yargs');
const fs = require('fs'); // Import the fs module
const path = require('path');
const readline = require('readline');
const { ProxyAgent, Agent } = require('undici');
const { version, name, author, license, repository } = require('./package.json');
const { loadConfig, loadLogo, getLastModifiedDate } = require('./js/configUtils');
const createLogger = require('./js/logger');
const AuthService = require('./js/authService');
const { fetchAnalysisIds, getDownloadLinks, listAvailableFiles, generateReport, metrics } = require('./js/fetchUtils');
const { downloadFile } = require('./js/fileUtils');
const { checkToolAvailability, ensureIndexFile, rangedDownloadBAM, indexBAM, generateOutputFileName } = require('./js/rangedUtils');
// Command line arguments setup
const argv = yargs
.usage('$0 <command> [args]')
.version(false)
.option('config', {
alias: 'c',
describe: 'Path to the configuration file',
type: 'string',
default: '.config.json'
})
.option('username', {
alias: 'u',
describe: 'Varvis API username',
type: 'string'
})
.option('password', {
alias: 'p',
describe: 'Varvis API password',
type: 'string'
})
.option('target', {
alias: 't',
describe: 'Target for the Varvis API',
type: 'string'
})
.option('analysisIds', {
alias: 'a',
describe: 'Analysis IDs to download files for (comma-separated)',
type: 'string'
})
.option('sampleIds', {
alias: 's',
describe: 'Sample IDs to filter analyses (comma-separated)',
type: 'string'
})
.option('limsIds', {
alias: 'l',
describe: 'LIMS IDs to filter analyses (comma-separated)',
type: 'string'
})
.option('list', {
alias: 'L',
describe: 'List available files for the specified analysis IDs',
type: 'boolean'
})
.option('destination', {
alias: 'd',
describe: 'Destination folder for the downloaded files',
type: 'string',
default: '.'
})
.option('proxy', {
alias: 'x',
describe: 'Proxy URL',
type: 'string'
})
.option('proxyUsername', {
alias: 'pxu',
describe: 'Proxy username',
type: 'string'
})
.option('proxyPassword', {
alias: 'pxp',
describe: 'Proxy password',
type: 'string'
})
.option('overwrite', {
alias: 'o',
describe: 'Overwrite existing files',
type: 'boolean',
default: false
})
.option('filetypes', {
alias: 'f',
describe: 'File types to download (comma-separated)',
type: 'string',
default: 'bam,bam.bai'
})
.option('loglevel', {
alias: 'll',
describe: 'Logging level (info, warn, error, debug)',
type: 'string',
default: 'info'
})
.option('logfile', {
alias: 'lf',
describe: 'Path to the log file',
type: 'string'
})
.option('reportfile', {
alias: 'r',
describe: 'Path to the report file',
type: 'string'
})
.option('filter', {
alias: 'F',
describe: 'Filter expressions (e.g., "analysisType=SNV", "sampleId>LB24-0001")',
type: 'array',
default: []
})
.option('range', {
alias: 'g',
describe: 'Genomic range for ranged download (e.g., chr1:1-100000)',
type: 'string',
})
.option('bed', {
alias: 'b',
describe: 'Path to BED file containing multiple regions',
type: 'string'
})
.option('version', {
alias: 'v',
type: 'boolean',
description: 'Show version information',
default: false
})
.help()
.alias('help', 'h')
.argv;
// Create logger instance
const logger = createLogger(argv);
// Show version information if the --version flag is set
if (argv.version) {
const logo = loadLogo();
console.log(logo);
console.log(`${name} - Version ${version}`);
console.log(`Date Last Modified: ${getLastModifiedDate(__filename)}`);
console.log(`Author: ${author}`);
console.log(`Repository: ${repository.url}`);
console.log(`License: ${license}`);
process.exit(0);
}
// Load configuration file settings
const configFilePath = path.resolve(argv.config);
const config = loadConfig(configFilePath);
// Merge command line arguments with configuration file settings
const finalConfig = {
...config,
...argv,
filetypes: (argv.filetypes || config.filetypes || 'bam,bam.bai').split(',').map(ft => ft.trim()),
analysisIds: (argv.analysisIds || config.analysisIds || '').split(',').map(id => id.trim()).filter(id => id),
sampleIds: (argv.sampleIds || config.sampleIds || '').split(',').map(id => id.trim()).filter(id => id),
limsIds: (
(typeof argv.limsIds === 'string' ? argv.limsIds : config.limsIds || '')
).split(',').map(id => id.trim()).filter(id => id),
filters: (argv.filter || config.filter || []).map(filter => filter.trim()),
destination: argv.destination !== '.' ? argv.destination : (config.destination || '.')
};
// Validate the final configuration
const requiredFields = ['username', 'password', 'target'];
for (const field of requiredFields) {
if (!finalConfig[field]) {
logger.error(`Error: Missing required argument --${field}`);
process.exit(1);
}
}
// Extract the final configuration values
const target = finalConfig.target;
const userName = finalConfig.username;
const password = finalConfig.password;
const analysisIds = finalConfig.analysisIds;
const sampleIds = finalConfig.sampleIds;
const limsIds = finalConfig.limsIds;
const destination = finalConfig.destination;
const proxy = finalConfig.proxy;
const proxyUsername = finalConfig.proxyUsername;
const proxyPassword = finalConfig.proxyPassword;
const overwrite = finalConfig.overwrite;
const filetypes = finalConfig.filetypes;
const reportfile = finalConfig.reportfile;
const filters = finalConfig.filters;
// Setup HTTP agent for proxy and cookie handling
const jar = new CookieJar();
const agentOptions = proxy ? { uri: proxy } : {};
if (proxyUsername && proxyPassword) {
agentOptions.auth = `${proxyUsername}:${proxyPassword}`;
}
const agent = proxy
? new ProxyAgent({
...agentOptions,
factory: (origin, opts) => new CookieClient(origin, {
...opts,
cookies: { jar },
}),
})
: new Agent({
factory: (origin, opts) => new CookieClient(origin, {
...opts,
cookies: { jar },
}),
});
// Initialize AuthService instance
const authService = new AuthService(logger, agent);
// Initialize readline interface for user prompts
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Main function to orchestrate the login and download process
const os = require('os'); // Import for generating temp file paths
// Main function to orchestrate the login and download process
async function main() {
try {
logger.debug('Starting main function');
// Ensure the destination directory exists
if (!fs.existsSync(destination)) {
logger.debug(`Creating destination directory: ${destination}`);
fs.mkdirSync(destination, { recursive: true });
}
logger.debug('Attempting to log in');
await authService.login({ username: userName, password: password }, target);
logger.debug('Login successful');
// Handle regions from command line or BED file
let regions = [];
let tempBedPath; // Initialize tempBedPath
if (argv.range) {
regions = argv.range.split(' ');
logger.info(`Using regions from command line: ${regions}`);
// Create a temporary BED file for samtools to read
tempBedPath = path.join(os.tmpdir(), 'regions.bed');
const bedContent = regions.map(region => {
const [chr, pos] = region.split(':');
const [start, end] = pos.split('-');
return `${chr}\t${start}\t${end}`;
}).join('\n');
fs.writeFileSync(tempBedPath, bedContent);
logger.info(`Generated temporary BED file: ${tempBedPath}`);
} else if (argv.bed) {
try {
const bedFileContent = fs.readFileSync(argv.bed, 'utf8');
regions = bedFileContent
.split('\n')
.filter(line => line && !line.startsWith('#')) // Filter out comments and empty lines
.map(line => {
const [chr, start, end] = line.split('\t');
return `${chr}:${start}-${end}`;
});
logger.info(`Using regions from BED file: ${regions}`);
// Create a temporary BED file for samtools to read
tempBedPath = path.join(os.tmpdir(), 'regions.bed');
fs.writeFileSync(tempBedPath, bedFileContent);
logger.info(`Generated temporary BED file: ${tempBedPath}`);
} catch (error) {
logger.error(`Error reading BED file: ${error.message}`);
process.exit(1);
}
} else {
logger.info('No regions provided. Proceeding with full file download.');
}
// Generate output file name using the function based on regions
const outputFile = path.join(destination, generateOutputFileName('download.bam', regions, logger));
logger.info(`Output file: ${outputFile}`);
// Fetch analysis IDs based on filters or sample IDs
const ids = analysisIds.length > 0
? analysisIds
: await fetchAnalysisIds(target, authService.token, agent, sampleIds, limsIds, filters, logger);
logger.info(`Fetched analysis IDs: ${ids}`);
for (const analysisId of ids) {
logger.info(`Processing analysis ID: ${analysisId}`);
const fileDict = await getDownloadLinks(analysisId, filetypes, target, authService.token, agent, logger);
logger.debug(`Fetched download links for analysis ID ${analysisId}`);
for (const [fileName, file] of Object.entries(fileDict)) {
const downloadLink = file.downloadLink;
const indexFileUrl = fileDict[`${fileName}.bai`]?.downloadLink;
const indexFilePath = path.join(destination, `${fileName}.bai`);
if (!indexFileUrl) {
logger.error(`Index file for BAM (${fileName}) not found.`);
continue;
}
// Ensure index file is downloaded
await ensureIndexFile(downloadLink, indexFileUrl, indexFilePath, agent, rl, logger, metrics, overwrite);
// Now pass the actual fileName instead of hardcoded 'download.bam'
const outputFile = path.join(destination, generateOutputFileName(fileName, regions, logger));
if (regions.length > 0) {
// Perform ranged download using the temporary BED file
try {
logger.info(`Performing ranged download for file: ${fileName}`);
await rangedDownloadBAM(downloadLink, tempBedPath, outputFile, indexFilePath, logger, overwrite);
await indexBAM(outputFile, logger, overwrite);
} catch (error) {
logger.error(`Error during ranged download for ${fileName}: ${error.message}`);
}
} else {
// Perform full download
try {
logger.info(`Performing full download for file: ${fileName}`);
await downloadFile(downloadLink, outputFile, overwrite, agent, rl, logger, metrics);
await indexBAM(outputFile, logger, overwrite);
} catch (error) {
logger.error(`Error during full download for ${fileName}: ${error.message}`);
}
}
}
}
logger.info('Download complete.');
generateReport(reportfile, logger);
// Clean up the temporary BED file if it was created
if (tempBedPath) {
fs.unlinkSync(tempBedPath);
logger.info(`Deleted temporary BED file: ${tempBedPath}`);
}
} catch (error) {
logger.error('An error occurred:', error.message);
logger.debug(error.stack);
} finally {
rl.close();
process.exit(1);
}
}
main().catch(error => {
logger.error('An unexpected error occurred:', error.message);
logger.debug(error.stack);
rl.close();
process.exit(1);
});