CLIPSeqTools/PlotApp/distribution_on_genic_elements.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 67 CC 4 SCAR 21.5
CC
4
Cov
0%
run
20.0
validate_args
2.0
run_R
2.0
CRAP 8.5
1 =head1 NAME
2  
3 CLIPSeqTools::PlotApp::distribution_on_genic_elements - Create plots for
4 script distribution_on_genic_elements.
5  
6 =head1 SYNOPSIS
7  
8 clipseqtools-plot distribution_on_genic_elements [options/parameters]
9  
10 =head1 DESCRIPTION
11  
12 Create plots for script distribution_on_genic_elements.
13  
14 =head1 OPTIONS
15  
16 Input.
17 --file <Str> input file.
18  
19 Output
20 --o_prefix <Str> output path prefix. Script will create and add
21 extension to path. [Default: ./]
22  
23 -v --verbose print progress lines and extra information.
24 -h -? --usage --help print help message
25  
26 =cut
27  
28 package CLIPSeqTools::PlotApp::distribution_on_genic_elements;
29 $CLIPSeqTools::PlotApp::distribution_on_genic_elements::VERSION = '1.0.0';
30  
31 # Make it an app command
32 use MooseX::App::Command;
33 extends 'CLIPSeqTools::PlotApp';
34  
35  
36 #######################################################################
37 ####################### Load External modules #####################
38 #######################################################################
39 use Modern::Perl;
40 use autodie;
41 use namespace::autoclean;
42 use Statistics::R;
43  
44  
45 #######################################################################
46 ####################### Command line options ######################
47 #######################################################################
48 option 'file' => (
49 is => 'rw',
50 isa => 'Str',
51 required => 1,
52 documentation => 'input file.',
53 );
54  
55 #######################################################################
56 ########################## Consume Roles ##########################
57 #######################################################################
58 with
59 "CLIPSeqTools::Role::Option::OutputPrefix" => {
60 -alias => { validate_args => '_validate_args_for_output_prefix' },
61 -excludes => 'validate_args',
62 };
63  
64  
65 #######################################################################
66 ######################## Interface Methods ########################
67 #######################################################################
68 sub validate_args {
69 my ($self) = @_;
70  
71 $self->_validate_args_for_output_prefix;
72 }
73  
74 sub run {
75 my ($self) = @_;
76  
77 warn "Validating arguments\n" if $self->verbose;
78 $self->validate_args();
79  
80 warn "Creating output path\n" if $self->verbose;
81 $self->make_path_for_output_prefix();
82  
83 warn "Creating plots with R\n" if $self->verbose;
84 $self->run_R;
85 }
86  
87 sub run_R {
88 my ($self) = @_;
89  
90 my $figfile = $self->o_prefix . 'distribution_on_genic_elements.pdf';
91  
92 # Start R
93 my $R = Statistics::R->new();
94  
95 # Pass arguments to R
96 $R->set('ifile', $self->file);
97 $R->set('figfile', $figfile);
98  
99 # Read table with data
100 $R->run(q{idata = read.delim(ifile)});
101  
102 # Remove first column with ids
103 $R->run(q{idata = idata[, -grep("transcript_id", colnames(idata))]});
104  
105 # Calculate colSums and stdv
106 $R->run(q{idata.utr5.colSums = colSums(idata[, grep("utr5",
107 colnames(idata))], na.rm=TRUE)});
108 $R->run(q{idata.cds.colSums = colSums(idata[, grep("cds",
109 colnames(idata))], na.rm=TRUE)});
110 $R->run(q{idata.utr3.colSums = colSums(idata[, grep("utr3",
111 colnames(idata))], na.rm=TRUE)});
112  
113 # Calculate normalization factor
114 $R->run(q{norm_factor = sum(idata, na.rm=TRUE)});
115  
116 # Normalize
117 $R->run(q{idata.utr5.colSums.norm = idata.utr5.colSums / norm_factor});
118 $R->run(q{idata.cds.colSums.norm = idata.cds.colSums / norm_factor});
119 $R->run(q{idata.utr3.colSums.norm = idata.utr3.colSums / norm_factor});
120  
121 # Calculate min/max for y axis
122 $R->run(q{max_y = max(idata.utr5.colSums.norm, idata.cds.colSums.norm,
123 idata.utr3.colSums.norm)});
124  
125 # Do plots
126 $R->run(q{pdf(figfile, width=21)});
127 $R->run(q{par(mfrow = c(1, 3), cex.lab=1.8, cex.axis=1.8, cex.main=1.8,
128 lwd=1.8, oma=c(1, 1, 2, 0), mar=c(5.1,5.1,4.1,2.1))});
129 $R->run(q{plot(idata.utr5.colSums.norm, pch=19, type="b", col="darkred",
130 main="5'UTR", xlab="Binned exonic length", ylab="Read density",
131 ylim=c(0, max_y))});
132 $R->run(q{plot(idata.cds.colSums.norm, pch=19, type="b", col="orange",
133 main="CDS", xlab="Binned exonic length", ylab="Read density",
134 ylim=c(0, max_y))});
135 $R->run(q{plot(idata.utr3.colSums.norm, pch=19, type="b", col="darkblue",
136 main="3'UTR", xlab="Binned exonic length", ylab="Read density",
137 ylim=c(0, max_y))});
138 $R->run(q{graphics.off()});
139  
140 # Close R
141 $R->stop();
142 }
143  
144  
145 1;