Commit eb0112bb authored by Luis Pedro Coelho's avatar Luis Pedro Coelho
Browse files

ENH Make metaSNP_COV multithreaded

parent 8374f3f2
......@@ -5,9 +5,25 @@ from sys import stderr, exit
from os import path
import argparse
import subprocess
import multiprocessing
basedir = path.dirname(__file__)
def exit_worker():
import sys
sys.exit(1)
def init_worker():
import signal
signal.signal(signal.SIGINT, exit_worker)
def run_sample(sample, command):
'''Simply wraps subprocess.call and returns contextual information in order
to provide a good error message (if necessary)'''
ret = subprocess.call(command)
return sample, command, ret
def main():
parser = argparse.ArgumentParser(description='Compute coverages')
parser.add_argument('project_dir', metavar='DIR',
......@@ -16,6 +32,8 @@ def main():
help='File with an input list of bam files, one file per line')
parser.add_argument('--print-commands', default=False, action='store_true',
help='Instead of executing the commands, simply print them out')
parser.add_argument('--threads', metavar='INT', default=1, type=int,
help='Number of jobs to run simmultaneously')
args = parser.parse_args()
if not path.isdir(args.project_dir):
stderr.write("Cannot find project directory '{}'".format(args.project_dir))
......@@ -26,6 +44,8 @@ def main():
os.makedirs(out_dir)
except:
pass
p = multiprocessing.Pool(args.threads, init_worker)
results = []
for line in open(args.all_samples):
line = line.rstrip()
name = path.basename(line)
......@@ -35,10 +55,16 @@ def main():
if args.print_commands:
print(" ".join(cmd))
else:
if subprocess.call(cmd):
stderr.write("Call to {} failed.".format(' '.join(cmd)))
exit(1)
results.append(p.apply_async(run_sample, (name, cmd)))
p.close()
p.join()
for r in results:
sample, command, ret = r.get()
if ret:
stderr.write("Failure in sample {}".format(sample))
stderr.write("Call to {} failed.".format(' '.join(cmd)))
exit(1)
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment