comparison bismark_pretty_report/bismark2report_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 shutil
6 import subprocess
7 import sys
8 import tempfile
9 import logging
10
11 def cleanup_before_exit(tmp_dir):
12 if tmp_dir and os.path.exists(tmp_dir):
13 shutil.rmtree(tmp_dir)
14
15 def get_arg():
16 parser = argparse.ArgumentParser()
17 parser.add_argument('--tool_dir', dest='tool_dir', action='store', nargs=1, metavar='tool_dir', type=str)
18 parser.add_argument('--alignment_report', dest='alignment_report', action='store', nargs=1, metavar='alignment_report', type=str)
19 parser.add_argument('--dedup_report', dest='dedup_report', action='store', nargs=1, metavar='dedup_report', type=str)
20 parser.add_argument('--splitting_report', dest='splitting_report', action='store', nargs=1, metavar='splitting_report', type=str)
21 parser.add_argument('--mbias_report', dest='mbias_report', action='store', nargs=1, metavar='mbias_report', type=str)
22 parser.add_argument('--nucleotide_report', dest='nucleotide_report', action='store', nargs=1, metavar='nucleotide_report', type=str)
23 parser.add_argument('--output_html_report', dest='output_html_report', action='store', nargs=1, metavar='output_html_report', type=str)
24 parser.add_argument('--output_html_report_link', dest='output_html_report_link', action='store', nargs=1, metavar='output_html_report_link', type=str)
25 parser.add_argument('--log_report', dest='log_report', action='store', nargs=1, metavar='log_report', type=str)
26 parser.add_argument('--output_dir', dest='job_dir', action='store', nargs=1, metavar='job_dir', type=str)
27 args = parser.parse_args()
28 return args
29
30 def create_and_write_html_link(job_dir, output_html_report_link, tmp_dir):
31 """
32 Web browsers don't allow to open a link pointing to the absolute path of a local html file FROM a website page;
33 The only way to make such link functional is to integrate the local file inside the web structure of the site.
34 Galaxy has been designed such that the child_dir <dataset_[0-9]+_files> of the output_dir is considered as the root
35 of the html base tag (i.e <base href="/" /> for the current job running.
36 The function proceeds the following steps:
37 #1. Extracts the galaxy dir where the output files are stored
38 #2. Creating a child dir <dataset_[0-9]+_files> in this output_dir is needed because it is considered as the root of the html base tag
39 # We can extract the exact name of this child dir from the jobs_directory name
40 #3. Moves the html file in this child_dir
41 """
42 output_path_list = output_html_report_link.split('/')
43 output_path = '/'.join(output_path_list[0:-1])
44 html_root = job_dir.split('/')[-1]
45 final_dir = os.path.join(output_path, html_root)
46 os.makedirs(final_dir)
47 shutil.move(os.path.join(tmp_dir, 'html_report'), os.path.join(final_dir, 'html_report.html'))
48
49 html_report = open(output_html_report_link, 'wb')
50 html_report.write('<!DOCTYPE html>\n')
51 html_report.write('<head>\n')
52 html_report.write('\t<meta http-equiv="content-type" content="text/html; charset=UTF-8">\n')
53 html_report.write('\t\t<base href="/" />\n')
54 html_report.write('\t\t<a href="html_report.html/" target="_blank">Link to Bismark Pretty Report Page</a>\n')
55 html_report.write('</head>')
56 html_report.close()
57
58 def __main__():
59 args = get_arg()
60
61 tmp_dir = tempfile.mkdtemp(prefix='tmp', suffix='')
62
63 if args.log_report:
64 logging.basicConfig(level=logging.INFO, filename=args.log_report[0], filemode="a+", format='%(message)s')
65 else:
66 logging.basicConfig(level=logging.INFO, filename=os.path.join(tmp_dir, 'log_report.txt'), filemode="a+", format='%(message)s')
67
68 alignment_option = '--alignment_report'
69 alignment_report = args.alignment_report[0]
70 if args.dedup_report:
71 dedup_option = '--dedup_report'
72 dedup_report = args.dedup_report[0]
73 else:
74 dedup_option = ''
75 dedup_report = ''
76 if args.splitting_report:
77 splitting_option = '--splitting_report'
78 splitting_report = args.splitting_report[0]
79 else:
80 splitting_option = ''
81 splitting_report = ''
82 if args.mbias_report:
83 mbias_option = '--mbias_report'
84 mbias_report = args.mbias_report[0]
85 else:
86 mbias_option = ''
87 mbias_report = ''
88 if args.nucleotide_report:
89 nucleotide_option = '--nucleotide_report'
90 nucleotide_report = args.nucleotide_report[0]
91 else:
92 nucleotide_option = ''
93 nucleotide_report = ''
94
95 proc = subprocess.Popen(['perl', os.path.join(args.tool_dir[0], 'bismark2report'), alignment_option, alignment_report, dedup_option, dedup_report,\
96 splitting_option, splitting_report, mbias_option, mbias_report, nucleotide_option, nucleotide_report,\
97 '--dir', tmp_dir, '--output', 'html_report'],\
98 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
99 proc_out, proc_err = proc.communicate()
100
101 cmd = 'perl %s %s %s %s %s %s %s %s %s %s %s --output html_report --dir %s'\
102 % (os.path.join(args.tool_dir[0], 'bismark2report'), alignment_option, alignment_report, dedup_option, dedup_report,\
103 splitting_option, splitting_report, mbias_option, mbias_report, nucleotide_option, nucleotide_report, tmp_dir)
104
105 logging.info('COMMAND LINE:\n\n%s' % cmd)
106 logging.info("__________________________________________________________________\n")
107 logging.info("BISMARK PRETTY REPORT STDOUT:\n\n%s" % proc_out)
108 if proc_err:
109 logging.critical("__________________________________________________________________\n")
110 logging.critical("BISMARK PRETTY REPORT ERROR:\n\n%s" % proc_err)
111 sys.exit("Bismark pretty report crashed with the folowing error message:\n%s" % proc_err)
112
113 if args.output_html_report:
114 shutil.copy(os.path.join(tmp_dir, 'html_report'), args.output_html_report[0])
115
116 #This function writes a link towards the Bismark html page inside an html file.
117 #This is needed because the direct visualization of the Bismark html report via Galaxy is ugly
118 create_and_write_html_link(args.job_dir[0], args.output_html_report_link[0], tmp_dir)
119
120 cleanup_before_exit(tmp_dir)
121
122 if __name__=="__main__": __main__()