CLIPSeqTools/PreprocessApp/annotate_with_conservation.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 155 CC 17 SCAR 38.4
CC
17
Cov
0%
run
90.0
create_new_column_if_required
56.0
plylop_pdl_for
12.0
CRAP 46.4
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 = '0.1.1';
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  
140 warn "Annotating records for $rname\n" if $self->verbose;
141 $reads_collection->schema->txn_do( sub {
142 $reads_collection->foreach_record_on_rname_do($rname, sub {
143 my ($record) = @_;
144  
145 my $record_plylop_avg = $pdl->slice([$record->start, $record->stop])->average();
146  
147 $record->conservation($record_plylop_avg);
148 $record->update();
149  
150 return 0;
151 });
152 });
153 }
154 }
155  
156 sub create_new_column_if_required {
157 my ($self, $reads_collection) = @_;
158  
159 if ($self->drop) {
160 warn "Droping column conservation\n" if $self->verbose;
161 try {
162 $reads_collection->schema->storage->dbh_do( sub {
163 my ($storage, $dbh, @cols) = @_;
164 $dbh->do('ALTER TABLE '.$self->table.' DROP COLUMN conservation');
165 });
166 }
167 catch {
168 warn "Warning: Column could not be dropped.\n" if $self->verbose;
169 };
170 }
171  
172 try {
173 warn "Creating column conservation\n" if $self->verbose;
174 $reads_collection->schema->storage->dbh_do( sub {
175 my ($storage, $dbh, @cols) = @_;
176 $dbh->do('ALTER TABLE '.$self->table.' ADD COLUMN conservation INT(5)');
177 });
178 }
179 catch {
180 warn "Warning: Column creation failed. Maybe column already exist.\n" if $self->verbose;
181 warn "$_\n" if $self->verbose > 1;
182 };
183  
184 # Make GenOO aware of the new column
185 my $conservation_params = {
186 data_type => 'decimal',
187 is_numeric => 1
188 };
189 $reads_collection->resultset->result_source->add_columns('conservation' => $conservation_params);
190 $reads_collection->resultset->result_class->add_columns('conservation' => $conservation_params);
191 $reads_collection->resultset->result_class->register_column('conservation');
192 }
193  
194 sub read_rname_sizes {
195 my ($self) = @_;
196  
197 my %rname_size;
198 open (my $CHRSIZE, '<', $self->rname_sizes);
199 while (my $line = <$CHRSIZE>) {
200 chomp $line;
201 my ($chr, $size) = split(/\t/, $line);
202 $rname_size{$chr} = $size;
203 }
204 close $CHRSIZE;
205 return %rname_size;
206 }
207  
208 sub plylop_pdl_for {
209 my ($self, $rname, $rname_size) = @_;
210  
211 my $pdl = PDL->zeros(PDL::short(), $rname_size);
212  
213 my @files = glob $self->cons_dir . '/' . $rname . '.*';
214 die "More than one matching files for $rname" if @files > 1;
215 my $file = $files[0]; chomp $file;
216 open (my $H, "gzip -dc $file |");
217  
218 my ($start, $step);
219 while (my $line = $H->getline) {
220 chomp $line;
221 if ($line =~ /^fixedStep\schrom=(.+)\sstart=(.+)\sstep=(.+)/) { #fixedStep chrom=chr1 start=10918 step=1
222 ($start, $step) = ($2 - 1, $3);
223 }
224 else {
225 my $piddle_tag_region = $pdl->slice([$start, $start + $step - 1]) .= int(1000 * $line);
226 $start += $step;
227 }
228 }
229 close $H;
230  
231 return $pdl;
232 }
233  
234 1;