CLIPSeqTools/PreprocessApp/annotate_with_conservation.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 164 CC 19 SCAR 40.0
CC
19
Cov
0%
run
110.0
create_new_column_if_required
56.0
plylop_pdl_for
20.0
CRAP 54.8
1 =head1 NAME
2  
3 CLIPSeqTools::PreprocessApp::annotate_with_conservation - Annotate alignments in a database table with conservation scores.
4  
5 =head1 SYNOPSIS
6  
7 clipseqtools-preprocess annotate_with_conservation [options/parameters]
8  
9 =head1 DESCRIPTION
10  
11 Annotate alignments in a database table with phastCons or phyloP
12 conservation scores. Adds a column named "conservation" with the average
13 conservation score for the nucleotides of each read.
14 To minimize storage needs, the conservation score is converted from
15 floating point number to integer by multiplying with 1000.
16  
17 =head1 OPTIONS
18  
19 Input options for library.
20 --driver <Str> driver for database connection (eg. mysql,
21 SQLite).
22 --database <Str> database name or path to database file for file
23 based databases (eg. SQLite).
24 --table <Str> database table.
25 --host <Str> hostname for database connection.
26 --user <Str> username for database connection.
27 --password <Str> password for database connection.
28 --records_class <Str> type of records stored in database.
29 --filter <Filter> filter library. May be used multiple times.
30 Syntax: column_name="pattern"
31 e.g. keep reads with deletions AND not repeat
32 masked AND longer than 31
33 --filter deletion="def"
34 --filter rmsk="undef" .
35 --filter query_length=">31".
36 Operators: >, >=, <, <=, =, !=, def, undef
37  
38 Other input
39 --rname_sizes <Str> file with sizes for reference alignment
40 sequences (rnames). Must be tab delimited
41 (chromosome\tsize) with one line per rname.
42 --cons_dir <Str> directory with phastCons or phyloP files.
43  
44  
45 Database options.
46 --drop drop column if it already exists (not
47 supported in SQlite).
48  
49 Other options.
50 -v --verbose print progress lines and extra information.
51 -h -? --usage --help print help message
52  
53 =cut
54  
55 package CLIPSeqTools::PreprocessApp::annotate_with_conservation;
56 $CLIPSeqTools::PreprocessApp::annotate_with_conservation::VERSION = '1.0.0';
57  
58 # Make it an app command
59 use MooseX::App::Command;
60 extends 'CLIPSeqTools::PreprocessApp';
61  
62  
63 #######################################################################
64 ####################### Load External modules #####################
65 #######################################################################
66 use Modern::Perl;
67 use autodie;
68 use namespace::autoclean;
69 use Try::Tiny;
70 use PDL::Lite; $PDL::BIGPDL = 0; $PDL::BIGPDL++; # enable huge pdls
71  
72  
73 #######################################################################
74 ####################### Command line options ######################
75 #######################################################################
76 option 'rname_sizes' => (
77 is => 'rw',
78 isa => 'Str',
79 required => 1,
80 documentation => 'file with sizes for reference alignment sequences (rnames). Must be tab delimited (chromosome\tsize) with one line per rname.',
81 );
82  
83 option 'cons_dir' => (
84 is => 'rw',
85 isa => 'Str',
86 required => 1,
87 documentation => 'directory with phastCons or phyloP files.',
88 );
89  
90 option 'drop' => (
91 is => 'rw',
92 isa => 'Bool',
93 documentation => 'drop column if they already exist (not supported in SQlite).',
94 );
95  
96  
97 #######################################################################
98 ########################## Consume Roles ##########################
99 #######################################################################
100 with
101 "CLIPSeqTools::Role::Option::Library" => {
102 -alias => { validate_args => '_validate_args_for_library' },
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 }
115  
116 sub run {
117 my ($self) = @_;
118  
119 warn "Starting: annotate_with_conservation\n";
120  
121 warn "Validating arguments\n" if $self->verbose;
122 $self->validate_args();
123  
124 warn "Reading sizes for reference alignment sequences\n" if $self->verbose;
125 my %rname_sizes = $self->read_rname_sizes;
126  
127 warn "Opening reads collection\n" if $self->verbose;
128 my $reads_collection = $self->reads_collection;
129 my @rnames = $reads_collection->rnames_for_all_strands;
130 $reads_collection->schema->storage->debug(1) if $self->verbose > 1;
131  
132 warn "Creating new column conservation if required\n" if $self->verbose;
133 $self->create_new_column_if_required($reads_collection);
134  
135 warn "Looping on annotation file to annotate records.\nThis might take a long time. Relax...\n" if $self->verbose;
136 foreach my $rname (@rnames) {
137 warn "Reading conservation data for $rname\n" if $self->verbose;
138 my $pdl = $self->plylop_pdl_for($rname, $rname_sizes{$rname});
139 if (!defined $pdl) {
140 warn "Could not find conservation file for $rname. Skipping.\n";
141 next;
142 }
143  
144 warn "Annotating records for $rname\n" if $self->verbose;
145 $reads_collection->schema->txn_do( sub {
146 $reads_collection->foreach_record_on_rname_do($rname, sub {
147 my ($record) = @_;
148  
149 my $record_plylop_avg = $pdl->slice([$record->start, $record->stop])->average();
150  
151 $record->conservation($record_plylop_avg);
152 $record->update();
153  
154 return 0;
155 });
156 });
157 }
158 }
159  
160 sub create_new_column_if_required {
161 my ($self, $reads_collection) = @_;
162  
163 if ($self->drop) {
164 warn "Droping column conservation\n" if $self->verbose;
165 try {
166 $reads_collection->schema->storage->dbh_do( sub {
167 my ($storage, $dbh, @cols) = @_;
168 $dbh->do('ALTER TABLE '.$self->table.' DROP COLUMN conservation');
169 });
170 }
171 catch {
172 warn "Warning: Column could not be dropped.\n" if $self->verbose;
173 };
174 }
175  
176 try {
177 warn "Creating column conservation\n" if $self->verbose;
178 $reads_collection->schema->storage->dbh_do( sub {
179 my ($storage, $dbh, @cols) = @_;
180 $dbh->do('ALTER TABLE '.$self->table.' ADD COLUMN conservation INT(5)');
181 });
182 }
183 catch {
184 warn "Warning: Column creation failed. Maybe column already exist.\n" if $self->verbose;
185 warn "$_\n" if $self->verbose > 1;
186 };
187  
188 # Make GenOO aware of the new column
189 my $conservation_params = {
190 data_type => 'decimal',
191 is_numeric => 1
192 };
193 $reads_collection->resultset->result_source->add_columns('conservation' => $conservation_params);
194 $reads_collection->resultset->result_class->add_columns('conservation' => $conservation_params);
195 $reads_collection->resultset->result_class->register_column('conservation');
196 }
197  
198 sub read_rname_sizes {
199 my ($self) = @_;
200  
201 my %rname_size;
202 open (my $CHRSIZE, '<', $self->rname_sizes);
203 while (my $line = <$CHRSIZE>) {
204 chomp $line;
205 my ($chr, $size) = split(/\t/, $line);
206 $rname_size{$chr} = $size;
207 }
208 close $CHRSIZE;
209 return %rname_size;
210 }
211  
212 sub plylop_pdl_for {
213 my ($self, $rname, $rname_size) = @_;
214  
215 my $pdl = PDL->zeros(PDL::short(), $rname_size);
216  
217 my @files = glob $self->cons_dir . '/' . $rname . '.*';
218 die "More than one matching files for $rname" if @files > 1;
219 my $file = $files[0];
220 if (!defined $file) {
221 return;
222 }
223 chomp $file;
224 open (my $H, "gzip -dc $file |");
225  
226 my ($start, $step);
227 while (my $line = $H->getline) {
228 chomp $line;
229 if ($line =~ /^fixedStep\schrom=(.+)\sstart=(.+)\sstep=(.+)/) { #fixedStep chrom=chr1 start=10918 step=1
230 ($start, $step) = ($2 - 1, $3);
231 }
232 else {
233 my $piddle_tag_region = $pdl->slice([$start, $start + $step - 1]) .= int(1000 * $line);
234 $start += $step;
235 }
236 }
237 close $H;
238  
239 return $pdl;
240 }
241  
242 1;