comparison bismark_deduplicate/bismark_deduplicate_wrapper.py @ 7:fcadce4d9a06 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/bismark commit b'e6ee273f75fff61d1e419283fa8088528cf59470\n'
author bgruening
date Sat, 06 May 2017 13:18:09 -0400
parents
children
comparison
equal deleted inserted replaced
6:0f8646f22b8d 7:fcadce4d9a06
1 #!/usr/bin/python
2
3 import argparse
4 import os
5 import re
6 import shutil
7 import subprocess
8 import sys
9 import tempfile
10 import logging
11 from glob import glob
12
13 def cleanup_before_exit(tmp_dir):
14 if tmp_dir and os.path.exists(tmp_dir):
15 shutil.rmtree(tmp_dir)
16
17 def get_arg():
18 parser = argparse.ArgumentParser()
19 parser.add_argument('--tool_dir', dest='tool_dir', action='store', nargs=1, metavar='tool_dir', type=str)
20 parser.add_argument('-p', action='store_true')
21 parser.add_argument('-s', action='store_true')
22 parser.add_argument('--input', dest='input', action='store', nargs=1, metavar='input', type=str)
23 parser.add_argument('--output_report', dest='output_report', action='store', nargs=1, metavar='output_report', type=str)
24 parser.add_argument('--output_bam', dest='output_bam', action='store', nargs=1, metavar='output_report', type=str)
25 parser.add_argument('--log_report', dest='log_report', action='store', nargs=1, metavar='log_filename', type=str)
26 args = parser.parse_args()
27 return args
28
29 def __main__():
30 args = get_arg()
31
32 tmp_dir = tempfile.mkdtemp(prefix='tmp', suffix='')
33 os.chdir(tmp_dir)
34
35 if args.log_report:
36 logging.basicConfig(level=logging.INFO, filename=args.log_report[0], filemode="a+", format='%(message)s')
37 else:
38 logging.basicConfig(level=logging.INFO, filename=os.path.join(tmp_dir, 'log_report.txt'), filemode="a+", format='%(message)s')
39
40 default_reads_name = 'submitted_reads.bam'
41 os.symlink(args.input[0], default_reads_name)
42
43 if args.p is True:
44 sPaired = '-p'
45 if args.s is True:
46 sPaired = '-s'
47
48 cmd = 'perl %s %s duplicated_reads.bam --bam' % (os.path.join(args.tool_dir[0], 'deduplicate_bismark'), sPaired)
49 logging.info('COMMAND LINE:\n\n%s' % cmd)
50
51 proc = subprocess.Popen(['perl', os.path.join(args.tool_dir[0], 'deduplicate_bismark'), sPaired, default_reads_name, '--bam'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
52 proc_out, proc_err = proc.communicate()
53
54 logging.info("__________________________________________________________________\n")
55 logging.info("BISMARK DEDUPLICATE STDOUT:\n\n%s" % proc_out)
56 if proc_err:
57 logging.critical("__________________________________________________________________\n")
58 logging.critical("BISMARK DEDUPLICATE WARNING:\n\n%s" % proc_err)
59 sys.exit("Dedpulicate Bismark crashed with the folowing error message:\n%s" % proc_err)
60
61 shutil.move( glob('*deduplicated.bam')[0], args.output_bam[0] )
62 shutil.move( glob('*deduplication_report.txt')[0], args.output_report[0])
63
64 cleanup_before_exit(tmp_dir)
65
66 if __name__=="__main__": __main__()