CLIPSeqTools/App/nmer_enrichment_over_shuffled.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 144 CC 14 SCAR 49.0
CC
14
Cov
0%
run
210.0
validate_args
2.0
_equal_or_more
2.0
CRAP 134.4
1 =head1 NAME
2  
3 CLIPSeqTools::App::nmer_enrichment_over_shuffled - Measure Nmer enrichment over shuffled reads.
4  
5 =head1 SYNOPSIS
6  
7 clipseqtools nmer_enrichment_over_shuffled [options/parameters]
8  
9 =head1 DESCRIPTION
10  
11 Measure words of length N (Nmer) enrichment and compare to shuffled reads. Shuffling is done at the nucleotide level and p-values are calculated using permutations.
12  
13 =head1 OPTIONS
14  
15 Input options for library.
16 --driver <Str> driver for database connection (eg. mysql,
17 SQLite).
18 --database <Str> database name or path to database file for file
19 based databases (eg. SQLite).
20 --table <Str> database table.
21 --host <Str> hostname for database connection.
22 --user <Str> username for database connection.
23 --password <Str> password for database connection.
24 --records_class <Str> type of records stored in database.
25 --filter <Filter> filter library. May be used multiple times.
26 Syntax: column_name="pattern"
27 e.g. keep reads with deletions AND not repeat
28 masked AND longer than 31
29 --filter deletion="def"
30 --filter rmsk="undef" .
31 --filter query_length=">31".
32 Operators: >, >=, <, <=, =, !=, def, undef
33  
34 Output
35 --o_prefix <Str> output path prefix. Script will create and add
36 extension to path. [Default: ./]
37  
38 Other options.
39 --nmer_length <Int> length N of the Nmer. Default: 6
40 --permutations <Int> number of permutation to be performed.
41 Use more than 100 to get p-values < 0.01.
42 --subset <Num> run analysis on random subset. Option specifies
43 number (if integer) or percent (if % is used)
44 of data to be used.
45 -v --verbose print progress lines and extra information.
46 -h -? --usage --help print help message
47  
48 =cut
49  
50 package CLIPSeqTools::App::nmer_enrichment_over_shuffled;
51 $CLIPSeqTools::App::nmer_enrichment_over_shuffled::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 List::Util qw(sum max);
65 use List::Util qw(shuffle);
66  
67  
68 #######################################################################
69 ####################### Command line options ######################
70 #######################################################################
71 option 'nmer_length' => (
72 is => 'rw',
73 isa => 'Int',
74 default => 6,
75 documentation => 'the length N of the Nmer.',
76 );
77  
78 option 'permutations' => (
79 is => 'rw',
80 isa => 'Int',
81 default => 100,
82 documentation => 'the number of permutation to be performed. Use more than 100 to get p-values < 0.01.',
83 );
84  
85 option 'subset' => (
86 is => 'rw',
87 isa => 'Str',
88 default => '100%',
89 documentation => 'run analysis on random subset. Option specifies the number (if integer) or percent (if % is used) of data to be used.',
90 );
91  
92  
93 #######################################################################
94 ########################## Consume Roles ##########################
95 #######################################################################
96 with
97 "CLIPSeqTools::Role::Option::Library" => {
98 -alias => { validate_args => '_validate_args_for_library' },
99 -excludes => 'validate_args',
100 },
101 "CLIPSeqTools::Role::Option::OutputPrefix" => {
102 -alias => { validate_args => '_validate_args_for_output_prefix' },
103 -excludes => 'validate_args',
104 };
105  
106  
107 #######################################################################
108 ######################## Interface Methods ########################
109 #######################################################################
110 sub validate_args {
111 my ($self) = @_;
112
113 $self->_validate_args_for_library;
114 $self->_validate_args_for_output_prefix;
115 }
116  
117 sub run {
118 my ($self) = @_;
119
120 warn "Starting analysis: nmer_enrichment_over_shuffled\n";
121
122 warn "Validating arguments\n" if $self->verbose;
123 $self->validate_args();
124  
125 warn "Creating reads collection\n" if $self->verbose;
126 my $reads_collection = $self->reads_collection;
127  
128 my $subset_factor = 1;
129 if ($self->subset ne '100%') {
130 warn "Calculating subset parameters\n" if $self->verbose;
131 my $records_count = $reads_collection->records_count;
132 if ($self->subset =~ /(.+)%/) {
133 $subset_factor = $1 / 100;
134 }
135 else {
136 $subset_factor = $self->subset / $records_count;
137 }
138 warn "Program will run on approximatelly ".sprintf('%.0f', $subset_factor*100)."% of library\n" if $self->verbose;
139 }
140  
141 warn "Counting Nmer occurrences in original and shuffled reads\n" if $self->verbose;
142 my %nmer_stats;
143 $reads_collection->foreach_record_do(sub{
144 my ($record) = @_;
145
146 return 0 if rand > $subset_factor;
147
148 my $seq = $record->sequence;
149 for my $i (0..length($seq)-$self->nmer_length) {
150 my $nmer = substr($seq, $i, $self->nmer_length);
151 $nmer_stats{$nmer}->{'count'} += $record->copy_number;
152 $nmer_stats{$nmer}->{'collapsed_count'} += 1;
153 }
154
155 my @seq_array = split(//, $seq);
156 for (my $perm = 0; $perm < $self->permutations; $perm++) {
157 my @random_seq_array = shuffle(@seq_array);
158 for (my $i=0; $i < @seq_array - $self->nmer_length + 1; $i++) {
159 my $nmer = join('', @random_seq_array[$i..$i+$self->nmer_length-1]);
160 $nmer_stats{$nmer}->{'sh_count'}->[$perm] += $record->copy_number;
161 $nmer_stats{$nmer}->{'sh_collapsed_count'}->[$perm] += 1;
162 }
163 }
164
165 return 0;
166 });
167  
168 warn "Creating output path\n" if $self->verbose;
169 $self->make_path_for_output_prefix();
170  
171 warn "Calculating p-values and printing results.\n" if $self->verbose;
172 open (my $OUT, '>', $self->o_prefix.'nmer_enrichment_over_shuffled.tab');
173 say $OUT join("\t", 'nmer', 'count', 'collapsed_count', 'average_sh_count', 'average_sh_collapsed_count', 'enrichment', 'collapsed_enrichment', 'pvalue', 'collapsed_pvalue');
174 foreach my $nmer (keys %nmer_stats) {
175 next if $nmer =~ /N/;
176
177 my $count = $nmer_stats{$nmer}->{'count'} || 0;
178 my $collapsed_count = $nmer_stats{$nmer}->{'collapsed_count'} || 0;
179 my @sh_counts = @{$nmer_stats{$nmer}->{'sh_count'}};
180 my @sh_collapsed_counts = @{$nmer_stats{$nmer}->{'sh_collapsed_count'}};
181
182 map{$_ = 0 if !$_} @sh_counts; # make any undefined values 0
183 map{$_ = 0 if !$_} @sh_collapsed_counts; # make any undefined values 0
184
185 my $avg_sh_count = sum(@sh_counts) / $self->permutations || 1;
186 my $avg_collapsed_count = sum(@sh_collapsed_counts) / $self->permutations || 1;
187
188 my $enrichment = $count / $avg_sh_count;
189 my $collapsed_enrichment = $collapsed_count / $avg_collapsed_count;
190
191 my $pvalue = _equal_or_more($count, @sh_counts) / $self->permutations;
192 my $collapsed_pvalue = _equal_or_more($collapsed_count, @sh_collapsed_counts) / $self->permutations;
193
194 say $OUT join("\t", $nmer, $count, $collapsed_count, $avg_sh_count, $avg_collapsed_count, $enrichment, $collapsed_enrichment, $pvalue, $collapsed_pvalue);
195 }
196 }
197  
198  
199 #######################################################################
200 ######################## Private Functions ########################
201 #######################################################################
202 sub _equal_or_more {
203 my $value = shift;
204
205 return scalar(grep {$_ >= $value} @_);
206 }
207  
208  
209  
210  
211  
212  
213 1;