CLIPSeqTools/App/cluster_size_and_score_distribution.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 124 CC 16 SCAR 52.0
CC
16
Cov
0%
run
272.0
validate_args
2.0
_create_new_cluster_from_record
2.0
CRAP 182.0
1 =head1 NAME
2  
3 CLIPSeqTools::App::cluster_size_and_score_distribution - Assemble reads in clusters and measure their size and number of contained reads distribution
4  
5 =head1 SYNOPSIS
6  
7 clipseqtools cluster_size_and_score_distribution [options/parameters]
8  
9 =head1 DESCRIPTION
10  
11 Assemble reads in clusters and measure their size and number of contained reads distribution.
12 Reads that are closer than a user defined threshold are assembled in clusters.
13 Cluster size and number of reads (score) contained in each cluster is measured.
14 Output: Distribution of cluster size (cluster_size_distribution.tab). Distribution of cluster scores (cluster_score_distribution.tab).
15  
16 =head1 OPTIONS
17  
18 Input options for library.
19 --driver <Str> driver for database connection (eg. mysql,
20 SQLite).
21 --database <Str> database name or path to database file for file
22 based databases (eg. SQLite).
23 --table <Str> database table.
24 --host <Str> hostname for database connection.
25 --user <Str> username for database connection.
26 --password <Str> password for database connection.
27 --records_class <Str> type of records stored in database.
28 --filter <Filter> filter library. May be used multiple times.
29 Syntax: column_name="pattern"
30 e.g. keep reads with deletions AND not repeat
31 masked AND longer than 31
32 --filter deletion="def"
33 --filter rmsk="undef" .
34 --filter query_length=">31".
35 Operators: >, >=, <, <=, =, !=, def, undef
36  
37 Output
38 --o_prefix <Str> output path prefix. Script will create and add
39 extension to path. [Default: ./]
40  
41 Other options.
42 --allowed_dis <Int> reads closer than this value are assembled in
43 clusters. Default: 0
44 --plot call plotting script to create plots.
45 -v --verbose print progress lines and extra information.
46 -h -? --usage --help print help message
47  
48 =cut
49  
50 package CLIPSeqTools::App::cluster_size_and_score_distribution;
51 $CLIPSeqTools::App::cluster_size_and_score_distribution::VERSION = '1.0.0';
52  
53 # Make it an app command
54 use MooseX::App::Command;
55 extends 'CLIPSeqTools::App';
56  
57  
58 #######################################################################
59 ####################### Load External modules #####################
60 #######################################################################
61 use Modern::Perl;
62 use autodie;
63 use namespace::autoclean;
64 use File::Spec;
65  
66  
67 #######################################################################
68 ######################## Load GenOO modules #######################
69 #######################################################################
70 use GenOO::GenomicRegion;
71  
72  
73 #######################################################################
74 ####################### Command line options ######################
75 #######################################################################
76 option 'allowed_dis' => (
77 is => 'rw',
78 isa => 'Int',
79 default => 0,
80 documentation => 'reads closer than this value are assembled in clusters.',
81 );
82  
83  
84 #######################################################################
85 ########################## Consume Roles ##########################
86 #######################################################################
87 with
88 "CLIPSeqTools::Role::Option::Library" => {
89 -alias => { validate_args => '_validate_args_for_library' },
90 -excludes => 'validate_args',
91 },
92 "CLIPSeqTools::Role::Option::Plot" => {
93 -alias => { validate_args => '_validate_args_for_plot' },
94 -excludes => 'validate_args',
95 },
96 "CLIPSeqTools::Role::Option::OutputPrefix" => {
97 -alias => { validate_args => '_validate_args_for_output_prefix' },
98 -excludes => 'validate_args',
99 };
100  
101
102 #######################################################################
103 ######################## Interface Methods ########################
104 #######################################################################
105 sub validate_args {
106 my ($self) = @_;
107
108 $self->_validate_args_for_library;
109 $self->_validate_args_for_plot;
110 $self->_validate_args_for_output_prefix;
111 }
112  
113 sub run {
114 my ($self) = @_;
115
116 warn "Starting analysis: cluster_size_and_score_distribution\n";
117
118 warn "Validating arguments\n" if $self->verbose;
119 $self->validate_args();
120
121 warn "Creating reads collection\n" if $self->verbose;
122 my $reads_collection = $self->reads_collection;
123 $reads_collection->schema->storage->debug(1) if $self->verbose > 1;
124
125 warn "Creating clusters and measuring size and score\n" if $self->verbose;
126 my $assembled_cluster;
127 my %cluster_size_count;
128 my %cluster_score_count;
129 my @assembled_clusters;
130 $reads_collection->foreach_record_sorted_by_location_do( sub {
131 my ($rec) = @_;
132
133 return 0 if $rec->cigar =~ /N/; # throw away reads with huge gaps (introns)
134
135 if (!defined $assembled_cluster) {
136 $assembled_cluster = $self->_create_new_cluster_from_record($rec);
137 return 0;
138 }
139
140 if ($rec->overlaps_with_offset($assembled_cluster, 1, $self->allowed_dis)) {
141 if ($rec->start < $assembled_cluster->start) {
142 $assembled_cluster->start($rec->start);
143 }
144 if ($rec->stop > $assembled_cluster->stop) {
145 $assembled_cluster->stop($rec->stop);
146 }
147 $assembled_cluster->copy_number($assembled_cluster->copy_number + $rec->copy_number);
148 }
149 else {
150 $cluster_size_count{$assembled_cluster->length}++;
151 $cluster_score_count{$assembled_cluster->copy_number}++;
152 $assembled_cluster = $self->_create_new_cluster_from_record($rec);
153 push @assembled_clusters, $assembled_cluster;
154 }
155
156 return 0;
157 });
158  
159 warn "Creating output path\n" if $self->verbose;
160 $self->make_path_for_output_prefix();
161
162 warn "Printing results for cluster sizes\n" if $self->verbose;
163 open (my $OUT1, '>', $self->o_prefix.'cluster_size_distribution.tab');
164 say $OUT1 join("\t", 'cluster_size', 'count');
165 say $OUT1 join("\t", $_, $cluster_size_count{$_}) for sort {$a <=> $b} keys %cluster_size_count;
166 close $OUT1;
167
168 warn "Printing results for cluster scores\n" if $self->verbose;
169 open (my $OUT2, '>', $self->o_prefix.'cluster_score_distribution.tab');
170 say $OUT2 join("\t", 'cluster_score', 'count');
171 say $OUT2 join("\t", $_, $cluster_score_count{$_}) for sort {$a <=> $b} keys %cluster_score_count;
172 close $OUT2;
173  
174 warn "Printing results for cluster coordinates\n" if $self->verbose;
175 open (my $OUT3, '>', $self->o_prefix.'clusters.bed');
176 say $OUT3 join("\t", $_->rname, $_->start, $_->stop+1, $_->location,
177 $_->copy_number, $_->strand_symbol) for @assembled_clusters;
178 close $OUT3;
179
180 if ($self->plot) {
181 warn "Creating plot\n" if $self->verbose;
182 CLIPSeqTools::PlotApp->initialize_command_class('CLIPSeqTools::PlotApp::cluster_size_and_score_distribution',
183 cluster_sizes_file => $self->o_prefix.'cluster_size_distribution.tab',
184 cluster_scores_file => $self->o_prefix.'cluster_score_distribution.tab',
185 o_prefix => $self->o_prefix
186 )->run();
187 }
188 }
189  
190  
191 #######################################################################
192 ######################### Private Methods #########################
193 #######################################################################
194 sub _create_new_cluster_from_record {
195 my ($self, $rec) = @_;
196
197 return GenOO::GenomicRegion->new(
198 strand => $rec->strand,
199 chromosome => $rec->rname,
200 start => $rec->start,
201 stop => $rec->stop,
202 copy_number => $rec->copy_number,
203 );
204 }
205 1;