annotate bismark_deduplicate_wrapper.py @ 38:b2d0e92f81c2 draft default tip

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