CLIPSeqTools/CompareApp/libraries_overlap_stats.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 125 CC 15 SCAR 51.0
CC
15
Cov
0%
run
240.0
validate_args
2.0
read_rname_sizes
2.0
CRAP 164.3
1 =head1 NAME
2  
3 CLIPSeqTools::CompareApp::libraries_overlap_stats - Count reads of library A that overlap reads in reference library B.
4  
5 =head1 SYNOPSIS
6  
7 clipseqtools-compare libraries_overlap_stats [options/parameters]
8  
9 =head1 DESCRIPTION
10  
11 Count reads of library A that overlap reads in reference library B.
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
19 file 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 Input options for reference library.
35 --r_driver <Str> driver for database connection (eg. mysql,
36 SQLite).
37 --r_database <Str> database name or path to database file for
38 file based databases (eg. SQLite).
39 --r_table <Str> database table.
40 --r_host <Str> hostname for database connection.
41 --r_user <Str> username for database connection.
42 --r_password <Str> password for database connection.
43 --r_records_class <Str> type of records stored in database.
44 --r_filter <Filter> same as filter but for reference library.
45  
46 Other input.
47 --rname_sizes <Str> file with sizes for reference alignment
48 sequences (rnames). Must be tab delimited
49 (chromosome\tsize) with one line per rname.
50  
51 Output
52 --o_prefix <Str> output path prefix. Script will create and add
53 extension to path. [Default: ./]
54  
55 Other options.
56 -v --verbose print progress lines and extra information.
57 -h -? --usage --help print help message
58  
59 =cut
60  
61 package CLIPSeqTools::CompareApp::libraries_overlap_stats;
62 $CLIPSeqTools::CompareApp::libraries_overlap_stats::VERSION = '1.0.0';
63  
64 # Make it an app command
65 use MooseX::App::Command;
66 extends 'CLIPSeqTools::CompareApp';
67  
68  
69 #######################################################################
70 ####################### Load External modules #####################
71 #######################################################################
72 use Modern::Perl;
73 use autodie;
74 use namespace::autoclean;
75 use PDL::Lite; $PDL::BIGPDL = 0; $PDL::BIGPDL++; # enable huge pdls
76  
77  
78 #######################################################################
79 ####################### Command line options ######################
80 #######################################################################
81 option 'rname_sizes' => (
82 is => 'rw',
83 isa => 'Str',
84 required => 1,
85 documentation => 'file with sizes for reference alignment sequences (rnames). Must be tab delimited (chromosome\tsize) with one line per rname.',
86 );
87  
88  
89 #######################################################################
90 ########################## Consume Roles ##########################
91 #######################################################################
92 with
93 "CLIPSeqTools::Role::Option::Library" => {
94 -alias => { validate_args => '_validate_args_for_library' },
95 -excludes => 'validate_args',
96 },
97 "CLIPSeqTools::Role::Option::ReferenceLibrary" => {
98 -alias => { validate_args => '_validate_args_for_reference_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_reference_library;
115 $self->_validate_args_for_output_prefix;
116 }
117  
118 sub run {
119 my ($self) = @_;
120
121 warn "Starting analysis: libraries_overlap_stats\n";
122
123 warn "Validating arguments\n" if $self->verbose;
124 $self->validate_args();
125
126 warn "Reading sizes for reference alignment sequences\n" if $self->verbose;
127 my %rname_sizes = $self->read_rname_sizes;
128  
129 warn "Creating reads collection\n" if $self->verbose;
130 my $reads_collection = $self->reads_collection;
131 my @rnames = $reads_collection->rnames_for_all_strands;
132  
133 warn "Creating reference reads collection\n" if $self->verbose;
134 my $r_reads_collection = $self->r_reads_collection;
135  
136 warn "Measuring the overlap of the primary library with the reference\n" if $self->verbose;
137 my $total_copy_number = 0; # The total copy number of primary records
138 my $total_records = 0; # The total number of primary records
139 my $overlapping_copy_number = 0; # The total copy number of primary records that overlap the reference
140 my $overlapping_records = 0; # The total number of primary records that overlap the reference
141 foreach my $rname (@rnames) {
142 warn " Annotating $rname with reference records\n" if $self->verbose;
143 my $rname_size = $rname_sizes{$rname};
144 my $pdl_plus = PDL->zeros(PDL::byte(), $rname_size);
145 my $pdl_minus = PDL->zeros(PDL::byte(), $rname_size);
146
147 $r_reads_collection->foreach_record_on_rname_do($rname, sub {
148 my ($r_record) = @_;
149
150 my $coords = [$r_record->start, $r_record->stop];
151 $pdl_plus->slice($coords) .= 1 if $r_record->strand == 1;
152 $pdl_minus->slice($coords) .= 1 if $r_record->strand == -1;
153
154 return 0;
155 });
156
157
158 warn " Parsing primary records on $rname and checking for overlap with reference\n" if $self->verbose;
159 $reads_collection->foreach_record_on_rname_do($rname, sub {
160 my ($p_record) = @_;
161
162 my $overlap = 0;
163 my $coords = [$p_record->start, $p_record->stop];
164 $overlap = $pdl_plus->slice($coords)->sum() if $p_record->strand == 1;
165 $overlap = $pdl_minus->slice($coords)->sum() if $p_record->strand == -1;
166
167 if ($overlap) {
168 $overlapping_copy_number += $p_record->copy_number;
169 $overlapping_records += 1;
170 }
171
172 $total_copy_number += $p_record->copy_number;
173 $total_records += 1;
174
175 return 0;
176 });
177 }
178  
179 warn "Creating output path\n" if $self->verbose;
180 $self->make_path_for_output_prefix();
181  
182 warn "Printing results\n" if $self->verbose;
183 open (my $OUT, '>', $self->o_prefix.'libraries_overlap_stats.tab');
184 say $OUT join("\t", 'total_records', 'total_copy_number', 'overlapping_records', 'overlapping_copy_number', 'overlapping_records_percent', 'overlapping_copy_number_percent');
185 say $OUT join("\t", $total_records, $total_copy_number, $overlapping_records, $overlapping_copy_number, ($overlapping_records / $total_records) * 100, ($overlapping_copy_number / $total_copy_number) * 100);
186 close $OUT;
187 }
188  
189 sub read_rname_sizes {
190 my ($self) = @_;
191
192 my %rname_size;
193 open (my $CHRSIZE, '<', $self->rname_sizes);
194 while (my $line = <$CHRSIZE>) {
195 chomp $line;
196 my ($chr, $size) = split(/\t/, $line);
197 $rname_size{$chr} = $size;
198 }
199 close $CHRSIZE;
200 return %rname_size;
201 }
202  
203 1;