annotate riboseqr/ribosome_profile.py @ 0:e01de823e919 draft default tip

Uploaded
author biotechcoder
date Fri, 01 May 2015 05:41:51 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
1 #!/usr/bin/env python
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
2 # TODO: check first if argument parameters are of the right type if given.
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
3 import os
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
4 import sys
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
5 import argparse
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
6 import logging
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
7 import rpy2.robjects as robjects
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
8 import utils
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
9
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
10 rscript = ''
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
11 R = robjects.r
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
12
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
13
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
14 def run_rscript(command=None):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
15 """Run R command, log it, append to rscript"""
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
16 global rscript
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
17 if not command:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
18 return
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
19 logging.debug(command)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
20 rscript += '{}\n'.format(command)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
21 R(command)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
22
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
23
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
24 def plot_transcript(rdata_load='Metagene.rda', transcript_name='',
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
25 transcript_length='27', transcript_cap='',
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
26 html_file='Plot-ribosome-profile.html',
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
27 output_path=os.getcwd()):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
28 """Plot ribosome profile for a given transcript. """
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
29 options = {}
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
30 for key, value, rtype, rmode in (
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
31 ('transcript_name', transcript_name, 'str', None),
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
32 ('transcript_length', transcript_length, 'int', 'charvector'),
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
33 ('transcript_cap', transcript_cap, 'int', None)):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
34 options[key] = utils.process_args(value, ret_type=rtype, ret_mode=rmode)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
35
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
36 run_rscript('library(riboSeqR)')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
37 run_rscript('load("{}")'.format(rdata_load))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
38
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
39 html = '<h2>Plot ribosome profile - results</h2><hr>'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
40 if len(transcript_name):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
41 cmd_args = (
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
42 '"{transcript_name}", main="{transcript_name}",'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
43 'coordinates=ffCs@CDS, riboData=riboDat,'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
44 'length={transcript_length}'.format(**options))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
45 if transcript_cap:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
46 cmd_args += ', cap={transcript_cap}'.format(**options)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
47 cmd = 'plotTranscript({})'.format(cmd_args)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
48
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
49 for fmat in ('pdf', 'png'):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
50 run_rscript('{0}(file="{1}")'.format(
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
51 fmat, os.path.join(
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
52 output_path, 'Ribosome-profile-plot.{}'.format(fmat))))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
53 run_rscript(cmd)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
54 run_rscript('dev.off()')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
55
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
56 html += ('<p>Selected ribosome footprint length: <strong>{0}</strong>'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
57 '</p><p><img border="1" src="Ribosome-profile-plot.png" '
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
58 'alt="Ribosome profile plot" /><br>'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
59 '<a href="Ribosome-profile-plot.pdf">PDF version</a>'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
60 '</p>'.format(transcript_length))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
61 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
62 msg = 'No transcript name was provided. Did not generate plot.'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
63 html += '<p>{}</p>'.format(msg)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
64 logging.debug(msg)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
65
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
66 logging.info('\n{:#^80}\n{}\n{:#^80}\n'.format(
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
67 ' R script for this session ', rscript, ' End R script '))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
68
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
69 with open(os.path.join(output_path, 'ribosome-profile.R'), 'w') as r:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
70 r.write(rscript)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
71
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
72 html += ('<h4>R script for this session</h4>'
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
73 '<p><a href="ribosome-profile.R">ribosome-profile.R</a></p>')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
74
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
75 with open(html_file, 'w') as f:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
76 f.write(html)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
77
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
78
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
79 if __name__ == '__main__':
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
80 parser = argparse.ArgumentParser(description='Plot Ribosome profile')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
81
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
82 # required arguments
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
83 flags = parser.add_argument_group('required arguments')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
84 flags.add_argument('--rdata_load', required=True,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
85 help='Saved riboSeqR data from Step 2')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
86 flags.add_argument('--transcript_name', required=True,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
87 help='Name of the transcript to be plotted')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
88 flags.add_argument(
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
89 '--transcript_length', required=True,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
90 help='Size class of ribosome footprint data to be plotted',
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
91 default='27')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
92 flags.add_argument(
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
93 '--transcript_cap', required=True,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
94 help=('Cap on the largest value that will be plotted as an abundance '
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
95 'of the ribosome footprint data'))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
96 parser.add_argument('--html_file', help='HTML file with reports')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
97 parser.add_argument('--output_path', help='Directory to save output files')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
98 parser.add_argument('--debug', help='Produce debug output',
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
99 action='store_true')
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
100
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
101 args = parser.parse_args()
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
102 if args.debug:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
103 level = logging.DEBUG
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
104 else:
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
105 level = logging.INFO
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
106
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
107 logging.basicConfig(format='%(levelname)s - %(message)s', level=level,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
108 stream=sys.stdout)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
109 logging.debug('Supplied Arguments\n{}\n'.format(vars(args)))
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
110
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
111 if not os.path.exists(args.output_path):
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
112 os.mkdir(args.output_path)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
113
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
114 plot_transcript(rdata_load=args.rdata_load,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
115 transcript_name=args.transcript_name,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
116 transcript_length=args.transcript_length,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
117 transcript_cap=args.transcript_cap,
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
118 html_file=args.html_file, output_path=args.output_path)
e01de823e919 Uploaded
biotechcoder
parents:
diff changeset
119 logging.debug('Done!')