CLIPSeqTools/CompareApp/libraries_relative_read_density.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 161 CC 17 SCAR 53.8
CC
17
Cov
0%
run
306.0
validate_args
2.0
read_rname_sizes
2.0
CRAP 217.3
1 =head1 NAME
2  
3 CLIPSeqTools::CompareApp::libraries_relative_read_density - Measure read
4 density around the reads of a reference library
5  
6 =head1 SYNOPSIS
7  
8 clipseqtools-compare libraries_relative_read_density [options/parameters]
9  
10 =head1 DESCRIPTION
11  
12 For a library A and a reference library B, measure the density of A reads
13 around the middle position of B reads.
14  
15 =head1 OPTIONS
16  
17 Input options for library.
18 --driver <Str> driver for database connection (eg. mysql,
19 SQLite).
20 --database <Str> database name or path to database file for
21 file based databases (eg. SQLite).
22 --table <Str> database table.
23 --host <Str> hostname for database connection.
24 --user <Str> username for database connection.
25 --password <Str> password for database connection.
26 --records_class <Str> type of records stored in database.
27 --filter <Filter> filter library. May be used multiple times.
28 Syntax: column_name="pattern"
29 e.g. keep reads with deletions AND not repeat
30 masked AND longer than 31
31 --filter deletion="def"
32 --filter rmsk="undef"
33 --filter query_length=">31"
34 Operators: >, >=, <, <=, =, !=, def, undef
35  
36 Input options for reference library.
37 --r_driver <Str> driver for database connection (eg. mysql,
38 SQLite).
39 --r_database <Str> database name or path to database file for
40 file based databases (eg. SQLite).
41 --r_table <Str> database table.
42 --r_host <Str> hostname for database connection.
43 --r_user <Str> username for database connection.
44 --r_password <Str> password for database connection.
45 --r_records_class <Str> type of records stored in database.
46 --r_filter <Filter> same as filter but for reference library.
47  
48 Other input.
49 --rname_sizes <Str> file with sizes for reference alignment
50 sequences (rnames). Must be tab delimited
51 (chromosome\tsize) with one line per rname.
52  
53 Output
54 --o_prefix <Str> output path prefix. Script will create and add
55 extension to path. [Default: ./]
56  
57 Other options.
58 --span <Int> the region around reference reads where density
59 is measured. [Default: 25]
60 --plot call plotting script to create plots.
61 -v --verbose print progress lines and extra information.
62 -h -? --usage --help print help message
63  
64 =cut
65  
66 package CLIPSeqTools::CompareApp::libraries_relative_read_density;
67 $CLIPSeqTools::CompareApp::libraries_relative_read_density::VERSION = '1.0.0';
68  
69 # Make it an app command
70 use MooseX::App::Command;
71 extends 'CLIPSeqTools::CompareApp';
72  
73  
74 #######################################################################
75 ####################### Load External modules #####################
76 #######################################################################
77 use Modern::Perl;
78 use autodie;
79 use namespace::autoclean;
80 use PDL::Lite; $PDL::BIGPDL = 0; $PDL::BIGPDL++; # enable huge pdls
81  
82  
83 #######################################################################
84 ####################### Command line options ######################
85 #######################################################################
86 option 'rname_sizes' => (
87 is => 'rw',
88 isa => 'Str',
89 required => 1,
90 documentation => 'file with sizes for reference alignment sequences '.
91 '(rnames). Must be tab delimited (chromosome\tsize) '.
92 'with one line per rname.',
93 );
94  
95 option 'span' => (
96 is => 'rw',
97 isa => 'Int',
98 default => 25,
99 documentation => 'the region around reference reads where density is '.
100 'measured.',
101 );
102  
103  
104 #######################################################################
105 ########################## Consume Roles ##########################
106 #######################################################################
107 with
108 "CLIPSeqTools::Role::Option::Library" => {
109 -alias => { validate_args => '_validate_args_for_library' },
110 -excludes => 'validate_args',
111 },
112 "CLIPSeqTools::Role::Option::ReferenceLibrary" => {
113 -alias => { validate_args => '_validate_args_for_reference_library' },
114 -excludes => 'validate_args',
115 },
116 "CLIPSeqTools::Role::Option::Plot" => {
117 -alias => { validate_args => '_validate_args_for_plot' },
118 -excludes => 'validate_args',
119 },
120 "CLIPSeqTools::Role::Option::OutputPrefix" => {
121 -alias => { validate_args => '_validate_args_for_output_prefix' },
122 -excludes => 'validate_args',
123 };
124  
125  
126 #######################################################################
127 ######################## Interface Methods ########################
128 #######################################################################
129 sub validate_args {
130 my ($self) = @_;
131  
132 $self->_validate_args_for_library;
133 $self->_validate_args_for_reference_library;
134 $self->_validate_args_for_plot;
135 $self->_validate_args_for_output_prefix;
136 }
137  
138 sub run {
139 my ($self) = @_;
140  
141 warn "Starting analysis: libraries_relative_read_density\n";
142  
143 warn "Validating arguments\n" if $self->verbose;
144 $self->validate_args();
145  
146 warn "Reading sizes for reference alignment sequences\n" if $self->verbose;
147 my %rname_sizes = $self->read_rname_sizes;
148  
149 warn "Creating reads collection\n" if $self->verbose;
150 my $reads_collection = $self->reads_collection;
151 my @rnames = $reads_collection->rnames_for_all_strands;
152  
153 warn "Creating reference reads collection\n" if $self->verbose;
154 my $r_reads_collection = $self->r_reads_collection;
155  
156 warn "Measuring read density around reference\n" if $self->verbose;
157 my $size = 2*$self->span+1;
158 my $counts_with_cn_sense = PDL->zeros(PDL::longlong(), $size);
159 my $counts_with_cn_antisense = PDL->zeros(PDL::longlong(), $size);
160 my $counts_no_cn_sense = PDL->zeros(PDL::longlong(), $size);
161 my $counts_no_cn_antisense = PDL->zeros(PDL::longlong(), $size);
162 foreach my $rname (@rnames) {
163 warn "Annotate $rname with primary records\n" if $self->verbose;
164 my $rname_size = $rname_sizes{$rname};
165 my $pdl_plus_with_cn = PDL->zeros(PDL::long(), $rname_size);
166 my $pdl_plus_no_cn = PDL->zeros(PDL::long(), $rname_size);
167 my $pdl_minus_with_cn = PDL->zeros(PDL::long(), $rname_size);
168 my $pdl_minus_no_cn = PDL->zeros(PDL::long(), $rname_size);
169 $reads_collection->foreach_record_on_rname_do($rname, sub {
170 my ($p_record) = @_;
171  
172 my $coords = [$p_record->start, $p_record->stop];
173 my $cn = $p_record->copy_number;
174 my $strand = $p_record->strand;
175  
176 if ($strand == 1) {
177 $pdl_plus_with_cn->slice($coords) += $cn;
178 $pdl_plus_no_cn->slice($coords) += 1;
179 }
180 elsif ($strand == -1) {
181 $pdl_minus_with_cn->slice($coords) += $cn;
182 $pdl_minus_no_cn->slice($coords) += 1;
183 }
184  
185 return 0;
186 });
187  
188 warn "Measuring density around reference ($rname)\n" if $self->verbose;
189 $r_reads_collection->foreach_record_on_rname_do($rname, sub {
190 my ($r_record) = @_;
191  
192 my $ref_pos = $r_record->mid_position;
193 my $begin = $ref_pos - $self->span;
194 my $end = $ref_pos + $self->span;
195 my $cn = $r_record->copy_number;
196 my $strand = $r_record->strand;
197  
198 return 0 if $begin < 0 or $end >= $rname_size;
199  
200 if ($strand == 1) {
201 my $coords = [$begin, $end];
202 $counts_with_cn_sense +=
203 PDL::longlong($pdl_plus_with_cn->slice($coords)) * $cn;
204 $counts_no_cn_sense +=
205 PDL::longlong($pdl_plus_no_cn->slice($coords));
206 $counts_with_cn_antisense +=
207 PDL::longlong($pdl_minus_with_cn->slice($coords)) * $cn;
208 $counts_no_cn_antisense +=
209 PDL::longlong($pdl_minus_no_cn->slice($coords));
210 }
211 elsif ($strand == -1) {
212 my $coords = [$end, $begin]; #reverse
213 $counts_with_cn_sense +=
214 PDL::longlong($pdl_minus_with_cn->slice($coords)) * $cn;
215 $counts_no_cn_sense +=
216 PDL::longlong($pdl_minus_no_cn->slice($coords));
217 $counts_with_cn_antisense +=
218 PDL::longlong($pdl_plus_with_cn->slice($coords)) * $cn;
219 $counts_no_cn_antisense +=
220 PDL::longlong($pdl_plus_no_cn->slice($coords));
221 }
222  
223 return 0;
224 });
225 }
226  
227 warn "Creating output path\n" if $self->verbose;
228 $self->make_path_for_output_prefix();
229  
230 warn "Printing results\n" if $self->verbose;
231 open (my $OUT, '>', $self->o_prefix.'libraries_relative_read_density.tab');
232 say $OUT join("\t", 'relative_position', 'counts_with_copy_number_sense',
233 'counts_no_copy_number_sense', 'counts_with_copy_number_antisense',
234 'counts_no_copy_number_antisense');
235 for (my $distance = 0-$self->span; $distance<=$self->span; $distance++) {
236 my $idx = $distance + $self->span;
237 say $OUT join("\t", $distance, $counts_with_cn_sense->at($idx),
238 $counts_no_cn_sense->at($idx),
239 $counts_with_cn_antisense->at($idx),
240 $counts_no_cn_antisense->at($idx));
241 }
242 close $OUT;
243  
244 if ($self->plot) {
245 warn "Creating plot\n" if $self->verbose;
246 CLIPSeqTools::PlotApp->initialize_command_class(
247 'CLIPSeqTools::PlotApp::libraries_relative_read_density',
248 file => $self->o_prefix.'libraries_relative_read_density.tab',
249 o_prefix => $self->o_prefix
250 )->run();
251 }
252 }
253  
254 sub read_rname_sizes {
255 my ($self) = @_;
256  
257 my %rname_size;
258 open (my $CHRSIZE, '<', $self->rname_sizes);
259 while (my $line = <$CHRSIZE>) {
260 chomp $line;
261 my ($chr, $size) = split(/\t/, $line);
262 $rname_size{$chr} = $size;
263 }
264 close $CHRSIZE;
265 return %rname_size;
266 }
267  
268 1;