CLIPSeqTools/PlotApp/cluster_size_and_score_distribution.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 97 CC 5 SCAR 25.5
CC
5
Cov
0%
run
30.0
validate_args
2.0
run_R_for_cluster_scores
2.0
CRAP 12.8
1 =head1 NAME
2  
3 CLIPSeqTools::PlotApp::cluster_size_and_score_distribution - Create plots for
4 script cluster_size_and_score_distribution.
5  
6 =head1 SYNOPSIS
7  
8 clipseqtools-plot cluster_size_and_score_distribution [options/parameters]
9  
10 =head1 DESCRIPTION
11  
12 Create plots for script cluster_size_and_score_distribution.
13  
14 =head1 OPTIONS
15  
16 Input.
17 --cluster_sizes_file <Str> file with cluster sizes distribution.
18 --cluster_scores_file <Str> file with cluster scores distribution.
19  
20 Output
21 --o_prefix <Str> output path prefix. Script will create and
22 add extension to path. Default: ./
23  
24 -v --verbose print progress lines and extra information.
25 -h -? --usage --help print help message
26  
27 =cut
28  
29 package CLIPSeqTools::PlotApp::cluster_size_and_score_distribution;
30 $CLIPSeqTools::PlotApp::cluster_size_and_score_distribution::VERSION = '1.0.0';
31  
32 # Make it an app command
33 use MooseX::App::Command;
34 extends 'CLIPSeqTools::PlotApp';
35  
36  
37 #######################################################################
38 ####################### Load External modules #####################
39 #######################################################################
40 use Modern::Perl;
41 use autodie;
42 use namespace::autoclean;
43 use Statistics::R;
44  
45  
46 #######################################################################
47 ####################### Command line options ######################
48 #######################################################################
49 option 'cluster_sizes_file' => (
50 is => 'rw',
51 isa => 'Str',
52 required => 1,
53 documentation => 'file with cluster sizes distribution.',
54 );
55  
56 option 'cluster_scores_file' => (
57 is => 'rw',
58 isa => 'Str',
59 required => 1,
60 documentation => 'file with cluster scores distribution.',
61 );
62  
63 #######################################################################
64 ########################## Consume Roles ##########################
65 #######################################################################
66 with
67 "CLIPSeqTools::Role::Option::OutputPrefix" => {
68 -alias => { validate_args => '_validate_args_for_output_prefix' },
69 -excludes => 'validate_args',
70 };
71  
72  
73 #######################################################################
74 ######################## Interface Methods ########################
75 #######################################################################
76 sub validate_args {
77 my ($self) = @_;
78  
79 $self->_validate_args_for_output_prefix;
80 }
81  
82 sub run {
83 my ($self) = @_;
84  
85 warn "Validating arguments\n" if $self->verbose;
86 $self->validate_args();
87  
88 warn "Creating output path\n" if $self->verbose;
89 $self->make_path_for_output_prefix();
90  
91 warn "Creating plots for cluster sizes with R\n" if $self->verbose;
92 $self->run_R_for_cluster_sizes;
93  
94 warn "Creating plots for cluster scores with R\n" if $self->verbose;
95 $self->run_R_for_cluster_scores;
96 }
97  
98 sub run_R_for_cluster_scores {
99 my ($self) = @_;
100  
101 my $figfile = $self->o_prefix . 'cluster_score_distribution.pdf';
102  
103 # Start R
104 my $R = Statistics::R->new();
105  
106 # Pass arguments to R
107 $R->set('ifile', $self->cluster_scores_file);
108 $R->set('figfile', $figfile);
109  
110 # Disable scientific notation
111 $R->run(q{options(scipen=999)});
112  
113 # Read table with data
114 $R->run(q{idata = read.delim(ifile)});
115  
116 # Create column with total number of reads in clusters of each score
117 $R->run(q{idata$contained_reads_per_score = idata$cluster_score *
118 idata$count});
119  
120 # Create groups of scores
121 $R->run(q{mybreaks = c(seq(0,5,1), seq(10,20,5), seq(40,100,20),
122 seq(200,600,200), 1000, Inf)});
123 $R->run(q{idata$score_group = cut(idata$cluster_score, breaks=mybreaks,
124 dig.lab=4)});
125  
126 # Aggregate (sum) counts for score groups
127 $R->run(q{aggregate_counts = tapply(idata$count, idata$score_group, sum)});
128 $R->run(q{aggregate_contained_reads_per_score =
129 tapply(idata$contained_reads_per_score, idata$score_group , sum)});
130  
131 # Do plots
132 $R->run(q{pdf(figfile, width=21)});
133 $R->run(q{par(mfrow = c(1, 3), cex.lab=1.5, cex.axis=1.5, cex.main=1.5,
134 lwd=1.5, oma=c(0, 0, 2, 0), mar=c(9.1, 5.1, 4.1, 2.1))});
135  
136 $R->run(q{plot(aggregate_counts, type="b", xaxt="n", pch=19, xlab = NA,
137 log="y", ylab="Number of clusters",
138 main="Number of clusters with given score")});
139 $R->run(q{axis(1, at=1:length(aggregate_counts),
140 labels=names(aggregate_counts), las=2)});
141 $R->run(q{mtext(side = 1, "Cluster score", line = 7, cex=1.2)});
142  
143 $R->run(q{plot((aggregate_counts / sum(aggregate_counts, na.rm=TRUE)) *
144 100, type="b", xaxt="n", pch=19, xlab = NA, ylim=c(0,100),
145 ylab="Percent of clusters (%)",
146 main="Percent of clusters with given score")});
147 $R->run(q{axis(1, at=1:length(aggregate_counts),
148 labels=names(aggregate_counts), las=2)});
149 $R->run(q{mtext(side = 1, "Cluster score", line = 7, cex=1.2)});
150  
151 $R->run(q{plot((aggregate_contained_reads_per_score /
152 sum(aggregate_contained_reads_per_score, na.rm=TRUE)) * 100, type="b",
153 xaxt="n", pch=19, xlab = NA, ylim=c(0,100),
154 ylab="Percent of reads (%)",
155 main="Percent of reads contained in cluster of given score")});
156 $R->run(q{axis(1, at=1:length(aggregate_counts),
157 labels=names(aggregate_counts), las=2)});
158 $R->run(q{mtext(side = 1, "Cluster score", line = 7, cex=1.2)});
159  
160 $R->run(q{graphics.off()});
161  
162 # Close R
163 $R->stop();
164 }
165  
166 sub run_R_for_cluster_sizes {
167 my ($self) = @_;
168  
169 my $figfile = $self->o_prefix . 'cluster_size_distribution.pdf';
170  
171 # Start R
172 my $R = Statistics::R->new();
173  
174 # Pass arguments to R
175 $R->set('ifile', $self->cluster_sizes_file);
176 $R->set('figfile', $figfile);
177  
178 # Disable scientific notation
179 $R->run(q{options(scipen=999)});
180  
181 # Read table with data
182 $R->run(q{idata = read.delim(ifile)});
183  
184 # Create groups of scores
185 $R->run(q{mybreaks = c(10, seq(50,500,50), 1000, Inf)});
186 $R->run(q{idata$size_group = cut(idata$cluster_size, breaks=mybreaks,
187 dig.lab=4)});
188  
189 # Aggregate (sum) counts for size groups
190 $R->run(q{aggregate_counts = tapply(idata$count, idata$size_group, sum)});
191  
192 # Do plots
193 $R->run(q{pdf(figfile, width=14)});
194 $R->run(q{par(mfrow = c(1, 2), cex.lab=1.2, cex.axis=1.2, cex.main=1.2,
195 lwd=1.2, oma=c(0, 0, 2, 0), mar=c(8.1, 5.1, 4.1, 2.1))});
196  
197 $R->run(q{plot(aggregate_counts, type="b", xaxt="n", pch=19, xlab = NA,
198 log="y", ylab="Number of clusters",
199 main="Number of clusters with given size")});
200 $R->run(q{axis(1, at=1:length(aggregate_counts),
201 labels=names(aggregate_counts), las=2)});
202 $R->run(q{mtext(side = 1, "Cluster size", line = 6, cex=1.2)});
203  
204 $R->run(q{plot((aggregate_counts / sum(aggregate_counts, na.rm=TRUE)) *
205 100, type="b", xaxt="n", pch=19, xlab = NA, ylim=c(0, 100),
206 ylab="Percent of clusters (%)",
207 main="Percent of clusters with given size")});
208 $R->run(q{axis(1, at=1:length(aggregate_counts),
209 labels=names(aggregate_counts), las=2)});
210 $R->run(q{mtext(side = 1, "Cluster size", line = 6, cex=1.2)});
211  
212 $R->run(q{graphics.off()});
213  
214 # Close R
215 $R->stop();
216 }
217  
218  
219 1;