CLIPSeqTools/PlotApp/conservation_distribution.pm untested

filter: Statementstmt 0.0% 0 / 55 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 / 72 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::conservation_distribution - Create plots for script
4 conservation_distribution.
5  
6 =head1 SYNOPSIS
7  
8 clipseqtools-plot conservation_distribution [options/parameters]
9  
10 =head1 DESCRIPTION
11  
12 Create plots for script conservation_distribution.
13  
14 =head1 OPTIONS
15  
16 Input.
17 --file <Str> file with conservation distribution.
18  
19 Output
20 --o_prefix <Str> output path prefix. Script will create and
21 add 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::conservation_distribution;
29 $CLIPSeqTools::PlotApp::conservation_distribution::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 => 'file with conservation distribution.',
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 for conservation 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 . 'conservation_distribution.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 # Disable scientific notation
100 $R->run(q{options(scipen=999)});
101  
102 # Read table with data
103 $R->run(q{idata = read.delim(ifile)});
104  
105 # Create groups of scores
106 $R->run(q{mybreaks = c(-Inf, seq(0,1000,100))});
107 $R->run(q{idata$score_group = cut(idata$conservation_score,
108 breaks=mybreaks, dig.lab=4)});
109  
110 # Aggregate (sum) counts for score groups
111 $R->run(q{aggregate_counts = tapply(idata$count, idata$score_group, sum)});
112 $R->run(q{aggregate_counts_no_copy_number =
113 tapply(idata$count_no_copy_number, idata$score_group , sum)});
114  
115 # Do plots
116 $R->run(q{pdf(figfile, width=14)});
117 $R->run(q{par(mfrow = c(1, 2), cex.lab=1.2, cex.axis=1.1, cex.main=1.2,
118 lwd=1.2, oma=c(0, 0, 2, 0), mar=c(9.1, 5.1, 4.1, 2.1))});
119  
120 $R->run(q{plot(aggregate_counts, type="b", xaxt="n", pch=19, xlab = NA,
121 log="y", ylab="Number of reads",
122 main="Number of reads with given conservation score")});
123 $R->run(q{axis(1, at=1:length(aggregate_counts),
124 labels=names(aggregate_counts), las=2)});
125 $R->run(q{mtext(side = 1, "Conservation score", line = 7, cex=1.2)});
126  
127 $R->run(q{plot((aggregate_counts / sum(aggregate_counts, na.rm=TRUE)) *
128 100, type="b", xaxt="n", pch=19, xlab = NA,
129 ylab="Percent of reads (%)",
130 main="Percent of reads with given conservation score")});
131 $R->run(q{axis(1, at=1:length(aggregate_counts),
132 labels=names(aggregate_counts), las=2)});
133 $R->run(q{mtext(side = 1, "Conservation score", line = 7, cex=1.2)});
134  
135 $R->run(q{plot(aggregate_counts_no_copy_number, type="b", xaxt="n",
136 pch=19, xlab = NA, log="y", ylab="Number of unique reads",
137 main="Number of unique reads with given conservation score")});
138 $R->run(q{axis(1, at=1:length(aggregate_counts_no_copy_number),
139 labels=names(aggregate_counts_no_copy_number), las=2)});
140 $R->run(q{mtext(side = 1, "Conservation score", line = 7, cex=1.2)});
141  
142 $R->run(q{plot((aggregate_counts_no_copy_number /
143 sum(aggregate_counts_no_copy_number, na.rm=TRUE)) * 100, type="b",
144 xaxt="n", pch=19, xlab = NA, ylab="Percent of unique reads (%)",
145 main="Percent of reads with given conservation score")});
146 $R->run(q{axis(1, at=1:length(aggregate_counts_no_copy_number),
147 labels=names(aggregate_counts_no_copy_number), las=2)});
148 $R->run(q{mtext(side = 1, "Conservation score", line = 7, cex=1.2)});
149  
150 $R->run(q{graphics.off()});
151  
152 # Close R
153 $R->stop();
154 }
155  
156  
157 1;