comparison bismark_deduplicate_wrapper.py @ 8:9bfe38410155 draft

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