|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# @Time : 8/3/2020 4:37 PM |
| 4 | +# @Author : Runsheng |
| 5 | +# @File : pararun.py |
| 6 | + |
| 7 | +""" |
| 8 | +A general para runner for the functions with only one input file/para |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import print_function |
| 12 | +import os |
| 13 | +import argparse |
| 14 | +import subprocess |
| 15 | +from glob import glob |
| 16 | +import logging |
| 17 | +import sys |
| 18 | +import signal |
| 19 | +import multiprocessing |
| 20 | + |
| 21 | + |
| 22 | +def myexe(cmd, timeout=0): |
| 23 | + """ |
| 24 | + a simple wrap of the shell |
| 25 | + mainly used to run the bwa mem mapping and samtool orders |
| 26 | + """ |
| 27 | + def setupAlarm(): |
| 28 | + signal.signal(signal.SIGALRM, alarmHandler) |
| 29 | + signal.alarm(timeout) |
| 30 | + |
| 31 | + def alarmHandler(signum, frame): |
| 32 | + sys.exit(1) |
| 33 | + |
| 34 | + proc=subprocess.Popen(cmd, shell=True, preexec_fn=setupAlarm, |
| 35 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE,cwd=os.getcwd()) |
| 36 | + out, err=proc.communicate() |
| 37 | + print(err) |
| 38 | + return out, err, proc.returncode |
| 39 | + |
| 40 | + |
| 41 | +def parmap(f, X, nprocs=multiprocessing.cpu_count()): |
| 42 | + """ |
| 43 | + a function to use mutip map inside a function |
| 44 | + modified from stackoverflow, 3288595 |
| 45 | + :param f: |
| 46 | + :param X: |
| 47 | + :param nprocs: core, if not given, use all core |
| 48 | + :return: |
| 49 | + """ |
| 50 | + q_in = multiprocessing.Queue(1) |
| 51 | + q_out = multiprocessing.Queue() |
| 52 | + |
| 53 | + proc = [multiprocessing.Process(target=fun, args=(f, q_in, q_out)) |
| 54 | + for _ in range(nprocs)] |
| 55 | + for p in proc: |
| 56 | + p.daemon = True |
| 57 | + p.start() |
| 58 | + |
| 59 | + sent = [q_in.put((i, x)) for i, x in enumerate(X)] |
| 60 | + [q_in.put((None, None)) for _ in range(nprocs)] |
| 61 | + res = [q_out.get() for _ in range(len(sent))] |
| 62 | + |
| 63 | + [p.join() for p in proc] |
| 64 | + |
| 65 | + return [x for i, x in sorted(res)] |
| 66 | + |
| 67 | + |
| 68 | +def fun(f, q_in, q_out): |
| 69 | + """ |
| 70 | + for parmap |
| 71 | + :param f: |
| 72 | + :param q_in: |
| 73 | + :param q_out: |
| 74 | + :return: |
| 75 | + """ |
| 76 | + while True: |
| 77 | + i, x = q_in.get() |
| 78 | + if i is None: |
| 79 | + break |
| 80 | + q_out.put((i, f(x))) |
| 81 | + |
| 82 | + |
| 83 | +def path_split(pathstring): |
| 84 | + laststring = pathstring.split("/")[-1] |
| 85 | + if "." in laststring: |
| 86 | + return laststring.split(".")[0] |
| 87 | + else: |
| 88 | + return laststring.split(".")[0] |
| 89 | + |
| 90 | + |
| 91 | +def get_prefix(bamlist): |
| 92 | + prefix_l = [] |
| 93 | + |
| 94 | + for i in bamlist: |
| 95 | + prefix_l.append(path_split(i)) |
| 96 | + return prefix_l |
| 97 | + |
| 98 | + |
| 99 | +if __name__=="__main__": |
| 100 | + parser = argparse.ArgumentParser() |
| 101 | + parser.add_argument("-f", "--folder",help="the folder containing all files") |
| 102 | + parser.add_argument("-s", "--suffix", help="the suffix of the files") |
| 103 | + parser.add_argument("-p", "--core",default=10, help="the cores used") |
| 104 | + parser.add_argument("-c", "--cmd", default="ls", help="the cmd to run, with prefix as prefix") |
| 105 | + |
| 106 | + args = parser.parse_args() |
| 107 | + |
| 108 | + ## main run code |
| 109 | + seqdir = args.folder |
| 110 | + os.chdir(seqdir) |
| 111 | + logging.info("Move to {dir}".format(dir=seqdir)) |
| 112 | + file_s = glob("*."+args.suffix) |
| 113 | + prefix_l= (get_prefix(file_s)) |
| 114 | + |
| 115 | + def run_one(prefix): |
| 116 | + file_one=prefix+args.suffix |
| 117 | + cmd_run=args.cmd.replace("prefix", prefix) |
| 118 | + cmd_new = """cd {seqdir} |
| 119 | + {cmd_run}""".format(cmd_run=cmd_run, seqdir=seqdir) |
| 120 | + print(cmd_new) |
| 121 | + return myexe(cmd_new) |
| 122 | + |
| 123 | + parmap(run_one, prefix_l, nprocs=int(args.core)) |
| 124 | + |
0 commit comments