Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:

server:
image: zhandongliulab/marrvel-server
restart: unless-stopped
working_dir: /MARRVEL/server
volumes:
- "${LOCAL_BASE}/.env:/MARRVEL/server/.env"
Expand Down
145 changes: 80 additions & 65 deletions server/utils/liftover.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,97 @@
const fs = require('fs');
const config = require('../config');

const cleanupTempFile = (filePath) => {
try {
fs.unlinkSync(filePath);
} catch (error) {
if (error.code !== 'ENOENT') {
console.error(`Error cleaning up temp file ${filePath}: ${error.message}`);
}
}
};

exports.liftover = async (chr, pos, fromOrg, fromDb, toOrg, toDb,
minMatch, isMultiRegionAllowed, minQuery, minChain, minBlocks, isThickFudgeSet) => {
// generate input BED file with random name
const inputBed = `/tmp/${Math.random().toString(36).substring(2, 15)}.bed`;
fs.writeFileSync(inputBed, `chr${chr}\t${pos}\t${pos}\n`);
// generate output BED file with random name
const outputBed = `/tmp/${Math.random().toString(36).substring(2, 15)}.bed`;
// generate unlifted BED file with random name
const unliftedBed = `/tmp/${Math.random().toString(36).substring(2, 15)}.bed`;

if (!config.liftoverCmdTool[fromOrg][fromDb][toOrg][toDb]) {
throw new Error('liftOver chain file is not configured properly');
}
const cmdArgs = [
inputBed,
config.liftoverCmdTool[fromOrg][fromDb][toOrg][toDb],
outputBed,
unliftedBed
];
if (minBlocks) {
cmdArgs.push(`-minBlocks=${minBlocks}`);
}
if (isThickFudgeSet) {
cmdArgs.push('-fudgeThick');
}
if (minMatch) {
cmdArgs.push(`-minMatch=${minMatch}`);
}
if (isMultiRegionAllowed) {
cmdArgs.push('-multiple');
}
if (minQuery) {
cmdArgs.push(`-minSizeQ=${minQuery}`);
}
if (minChain) {
cmdArgs.push(`-minChainT=${minChain}`);
}

// run liftOver command line tool
try {
await runLiftover(cmdArgs);
} catch (error) {
console.error(`Error occurred while running liftOver: ${error.message}`);
return {
message: 'Error occurred while running liftOver'
};
}
// read output BED file
let lifted = null;
// generate input/output/unlifted BED files with random names
const inputBed = `/tmp/${Math.random().toString(36).substring(2, 15)}.bed`;
const outputBed = `/tmp/${Math.random().toString(36).substring(2, 15)}.bed`;
const unliftedBed = `/tmp/${Math.random().toString(36).substring(2, 15)}.bed`;

try {
const output = fs.readFileSync(outputBed, 'utf8');
lifted = (output.split('\n')[0] || '').split('\t');
} catch (error) {
console.error(`Error reading output BED file: ${error.message}`);
return {
message: 'Error reading output BED file'
};
}
if (!lifted || lifted.length < 2) {
fs.writeFileSync(inputBed, `chr${chr}\t${pos}\t${pos}\n`);

const cmdArgs = [
inputBed,
config.liftoverCmdTool[fromOrg][fromDb][toOrg][toDb],
outputBed,
unliftedBed
];
if (minBlocks) {
cmdArgs.push(`-minBlocks=${minBlocks}`);
}
if (isThickFudgeSet) {
cmdArgs.push('-fudgeThick');
}
if (minMatch) {
cmdArgs.push(`-minMatch=${minMatch}`);
}
if (isMultiRegionAllowed) {
cmdArgs.push('-multiple');
}
if (minQuery) {
cmdArgs.push(`-minSizeQ=${minQuery}`);
}
if (minChain) {
cmdArgs.push(`-minChainT=${minChain}`);
}

// run liftOver command line tool
try {
await runLiftover(cmdArgs);
} catch (error) {
console.error(`Error occurred while running liftOver: ${error.message}`);
return {
message: 'Error occurred while running liftOver'
};
}
// read output BED file
let lifted = null;
try {
const output = fs.readFileSync(outputBed, 'utf8');
lifted = (output.split('\n')[0] || '').split('\t');
} catch (error) {
console.error(`Error reading output BED file: ${error.message}`);
return {
message: 'Error reading output BED file'
};
}
if (!lifted || lifted.length < 2) {
return {
message: 'No lifted position found'
};
}
return {
message: 'No lifted position found'
inputChr: chr,
inputPos: pos,
chr: lifted[0].replace('chr', ''),
pos: parseInt(lifted[1])
};
} finally {
cleanupTempFile(inputBed);
cleanupTempFile(outputBed);
cleanupTempFile(unliftedBed);
}
// clean up temporary files
fs.unlinkSync(inputBed);
fs.unlinkSync(outputBed);
fs.unlinkSync(unliftedBed);
return {
inputChr: chr,
inputPos: pos,
chr: lifted[0].replace('chr', ''),
pos: parseInt(lifted[1])
};
};

const runLiftover = (args) => {
return new Promise((resolve, reject) => {
const spawn = require('child_process').spawn;
const child = spawn(config.liftoverCmdTool.path, args);
const child = spawn(config.liftoverCmdTool.path, args, { timeout: 15000, killSignal: 'SIGKILL' });
let stdout = '';
let stderr = '';
child.stdout.on('data', (data) => {
Expand All @@ -88,7 +100,10 @@ const runLiftover = (args) => {
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.on('close', (code) => {
child.on('close', (code, signal) => {
if (signal) {
return reject(new Error(`liftOver process was killed (signal: ${signal})`));
}
if (code !== 0) {
return reject(new Error(`liftOver process exited with code ${code}: ${stderr}`));
}
Expand Down
8 changes: 6 additions & 2 deletions server/utils/transvar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const appendGene = (data) => {
const executeTransvar = (option) => {
return new Promise((resolve, reject) => {
const { spawn } = require('child_process');
const proc = spawn(transvarPath, option);
const proc = spawn(transvarPath, option, { timeout: 30000, killSignal: 'SIGKILL' });

let stdout = '';
const stderr = [];
Expand All @@ -37,7 +37,11 @@ const executeTransvar = (option) => {
stderr.push(err);
});

proc.on('close', (code) => {
proc.on('close', (code, signal) => {
if (signal) {
reject(new Error(`transvar process was killed (signal: ${signal})`));
return;
}
resolve({
code,
stdout,
Expand Down
Loading