Mercurial > repos > bgruening > bismark
comparison bismark_deduplicate_wrapper.py @ 21:120b7b35e442 draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/bismark commit 8fdc76a99a9dcf34549898a208317607afd18798"
author | bgruening |
---|---|
date | Thu, 22 Apr 2021 17:05:46 +0000 |
parents | 9bfe38410155 |
children |
comparison
equal
deleted
inserted
replaced
20:ff6ee551b153 | 21:120b7b35e442 |
---|---|
2 | 2 |
3 import argparse | 3 import argparse |
4 import logging | 4 import logging |
5 import os | 5 import os |
6 import shutil | 6 import shutil |
7 import signal | |
7 import subprocess | 8 import subprocess |
8 import sys | 9 import sys |
9 import signal | |
10 import tempfile | 10 import tempfile |
11 from glob import glob | |
12 | 11 |
13 | 12 |
14 def stop_err(logger, msg): | 13 def stop_err(logger, msg): |
15 logger.critical(msg) | 14 logger.critical(msg) |
16 sys.exit(1) | 15 sys.exit(1) |
22 """ | 21 """ |
23 signal.signal(signal.SIGPIPE, signal.SIG_DFL) | 22 signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
24 | 23 |
25 | 24 |
26 def log_subprocess_output(logger, pipe): | 25 def log_subprocess_output(logger, pipe): |
27 for line in iter(pipe.readline, b''): | 26 for line in iter(pipe.readline, b""): |
28 logger.debug(line.decode().rstrip()) | 27 logger.debug(line.decode().rstrip()) |
29 | 28 |
30 | 29 |
31 def get_arg(): | 30 def get_arg(): |
32 parser = argparse.ArgumentParser() | 31 parser = argparse.ArgumentParser() |
33 parser.add_argument('--single_or_paired', dest='single_or_paired') | 32 parser.add_argument("--single_or_paired", dest="single_or_paired") |
34 parser.add_argument('--input', dest='input', metavar='input') | 33 parser.add_argument("--input", dest="input", metavar="input") |
35 parser.add_argument('--output_report', dest='output_report', metavar='output_report') | 34 parser.add_argument( |
36 parser.add_argument('--output_bam', dest='output_bam', metavar='output_report') | 35 "--output_report", dest="output_report", metavar="output_report" |
37 parser.add_argument('--log_report', dest='log_report', metavar='log_filename', type=str) | 36 ) |
37 parser.add_argument("--output_bam", dest="output_bam", metavar="output_report") | |
38 parser.add_argument( | |
39 "--log_report", dest="log_report", metavar="log_filename", type=str | |
40 ) | |
38 args = parser.parse_args() | 41 args = parser.parse_args() |
39 return args | 42 return args |
40 | 43 |
41 | 44 |
42 def __main__(): | 45 def __main__(): |
43 args = get_arg() | 46 args = get_arg() |
44 | 47 |
45 logger = logging.getLogger('bismark_deduplicate_wrapper') | 48 logger = logging.getLogger("bismark_deduplicate_wrapper") |
46 logger.setLevel(logging.DEBUG) | 49 logger.setLevel(logging.DEBUG) |
47 ch = logging.StreamHandler(sys.stdout) | 50 ch = logging.StreamHandler(sys.stdout) |
48 if args.log_report: | 51 if args.log_report: |
49 ch.setLevel(logging.WARNING) | 52 ch.setLevel(logging.WARNING) |
50 handler = logging.FileHandler(args.log_report) | 53 handler = logging.FileHandler(args.log_report) |
53 else: | 56 else: |
54 ch.setLevel(logging.DEBUG) | 57 ch.setLevel(logging.DEBUG) |
55 logger.addHandler(ch) | 58 logger.addHandler(ch) |
56 | 59 |
57 # ensure the input has a .bam suffix | 60 # ensure the input has a .bam suffix |
58 tmp_dir = tempfile.mkdtemp(prefix='tmp', suffix='') | 61 tmp_dir = tempfile.mkdtemp(prefix="tmp", suffix="") |
59 os.chdir(tmp_dir) | 62 os.chdir(tmp_dir) |
60 default_reads_name = 'submitted_reads.bam' | 63 default_reads_name = "submitted_reads.bam" |
61 os.symlink(args.input, default_reads_name) | 64 os.symlink(args.input, default_reads_name) |
62 | 65 |
63 single_or_paired = '-s' if args.single_or_paired == 'single' else '-p' | 66 single_or_paired = "-s" if args.single_or_paired == "single" else "-p" |
64 cmd = ['deduplicate_bismark', single_or_paired, default_reads_name, '--bam'] | 67 cmd = ["deduplicate_bismark", single_or_paired, default_reads_name, "--bam"] |
65 logger.info("Deduplicating with: '%s'", " ".join(cmd)) | 68 logger.info("Deduplicating with: '%s'", " ".join(cmd)) |
66 process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, | 69 process = subprocess.Popen( |
67 preexec_fn=restore_sigpipe) | 70 cmd, |
71 stdout=subprocess.PIPE, | |
72 stderr=subprocess.STDOUT, | |
73 preexec_fn=restore_sigpipe, | |
74 ) | |
68 proc_out, proc_err = process.communicate() | 75 proc_out, proc_err = process.communicate() |
69 logger.info(proc_out) | 76 logger.info(proc_out) |
70 if process.returncode != 0: | 77 if process.returncode != 0: |
71 stop_err(logger, "Bismark deduplication error (also check the log file if any)!\n%s" % proc_err) | 78 stop_err( |
79 logger, | |
80 "Bismark deduplication error (also check the log file if any)!\n%s" | |
81 % proc_err, | |
82 ) | |
72 | 83 |
73 deduplicated_out_name = 'submitted_reads.deduplicated.bam' | 84 deduplicated_out_name = "submitted_reads.deduplicated.bam" |
74 deduplicated_report_name = 'submitted_reads.deduplication_report.txt' | 85 deduplicated_report_name = "submitted_reads.deduplication_report.txt" |
75 logger.debug("Moving '%s' to galaxy: '%s'.", deduplicated_out_name, args.output_bam) | 86 logger.debug("Moving '%s' to galaxy: '%s'.", deduplicated_out_name, args.output_bam) |
76 shutil.move(deduplicated_out_name, args.output_bam ) | 87 shutil.move(deduplicated_out_name, args.output_bam) |
77 logger.debug("Moving '%s' to galaxy: '%s'.", deduplicated_report_name, args.output_report) | 88 logger.debug( |
78 shutil.move('submitted_reads.deduplication_report.txt', args.output_report) | 89 "Moving '%s' to galaxy: '%s'.", deduplicated_report_name, args.output_report |
90 ) | |
91 shutil.move("submitted_reads.deduplication_report.txt", args.output_report) | |
79 logger.debug("Done.") | 92 logger.debug("Done.") |
80 | 93 |
81 if __name__=="__main__": __main__() | 94 |
95 if __name__ == "__main__": | |
96 __main__() |