CLIPSeqTools/PlotApp/distribution_on_introns_exons.pm untested

filter: Statementstmt 0.0% 0 / 48 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 / 65 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_introns_exons - Create plots for script
4 distribution_on_introns_exons.
5  
6 =head1 SYNOPSIS
7  
8 clipseqtools-plot distribution_on_introns_exons [options/parameters]
9  
10 =head1 DESCRIPTION
11  
12 Create plots for script distribution_on_introns_exons.
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_introns_exons;
29 $CLIPSeqTools::PlotApp::distribution_on_introns_exons::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_introns_exons.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 # Create 2 new tables for exons and introns
103 $R->run(q{exon_dat = subset(idata, idata$element == 'exon',
104 select=-c(element, location))});
105 $R->run(q{intron_dat = subset(idata, idata$element == 'intron',
106 select=-c(element, location))});
107  
108 # Calculate normalization factor
109 $R->run(q{norm_factor = sum(exon_dat, na.rm=TRUE) + sum(intron_dat,
110 na.rm=TRUE)});
111  
112 # Calculate colSums and stdv
113 $R->run(q{exon_dat.colSums = colSums(exon_dat, na.rm=TRUE)});
114 $R->run(q{intron_dat.colSums = colSums(intron_dat, na.rm=TRUE)});
115  
116 # Normalize
117 $R->run(q{exon_dat.colSums.norm = exon_dat.colSums / norm_factor});
118 $R->run(q{intron_dat.colSums.norm = intron_dat.colSums / norm_factor});
119  
120 # Calculate min/max for y axis
121 $R->run(q{max_y = max(exon_dat.colSums.norm, intron_dat.colSums.norm)});
122  
123 # Do plots
124 $R->run(q{pdf(figfile, width=14)});
125 $R->run(q{par(mfrow = c(1, 2), cex.lab=1.4, cex.axis=1.4, cex.main=1.4,
126 lwd=1.4, oma=c(0, 1, 2, 0), mar=c(5.1,4.1,4.1,2.1))});
127 $R->run(q{plot(intron_dat.colSums.norm, pch=19, type="b", col="darkred",
128 main="Intron", xlab="Binned length", ylab="Read density", ylim=c(0,
129 max_y))});
130 $R->run(q{plot(exon_dat.colSums.norm, pch=19, type="b", col="orange",
131 main="Exon", xlab="Binned length", ylab="Read density", ylim=c(0,
132 max_y))});
133 $R->run(q{graphics.off()});
134  
135 # Close R
136 $R->stop();
137 }
138  
139 1;