| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::distribution_on_genic_elements - Measure read distribution | ||
| 4 | on 5'UTR, CDS and 3'UTR. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools distribution_on_genic_elements [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Measure the distribution of reads along 5'UTR, CDS and 3'UTR. | ||
| 13 | Will split the 5'UTR, CDS and 3'UTR of coding transcripts in bins and measure | ||
| 14 | the read density in each bin. It will randomly keep a single transcript for | ||
| 15 | each gene. The read copy number is not used in this analysis. | ||
| 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 | --gtf <Str> GTF file with genes/transcripts. | ||
| 40 | |||
| 41 | Output | ||
| 42 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 43 | extension to path. [Default: ./] | ||
| 44 | |||
| 45 | Other options. | ||
| 46 | --bins <Int> number of bins each element is split into. | ||
| 47 | [Default: 10] | ||
| 48 | --length_thres <Int> genic elements shorter than this are skipped. | ||
| 49 | [Default: 300] | ||
| 50 | --plot call plotting script to create plots. | ||
| 51 | -v --verbose print progress lines and extra information. | ||
| 52 | -h -? --usage --help print help message | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | package CLIPSeqTools::App::distribution_on_genic_elements; | ||
| 57 | $CLIPSeqTools::App::distribution_on_genic_elements::VERSION = '1.0.0'; | ||
| 58 | |||
| 59 | # Make it an app command | ||
| 60 | use MooseX::App::Command; | ||
| 61 | extends 'CLIPSeqTools::App'; | ||
| 62 | |||
| 63 | |||
| 64 | ####################################################################### | ||
| 65 | ####################### Load External modules ##################### | ||
| 66 | ####################################################################### | ||
| 67 | use Modern::Perl; | ||
| 68 | use autodie; | ||
| 69 | use namespace::autoclean; | ||
| 70 | |||
| 71 | |||
| 72 | ####################################################################### | ||
| 73 | ####################### Command line options ###################### | ||
| 74 | ####################################################################### | ||
| 75 | option 'bins' => ( | ||
| 76 | is => 'rw', | ||
| 77 | isa => 'Int', | ||
| 78 | default => 5, | ||
| 79 | documentation => 'number of bins each element is split into.', | ||
| 80 | ); | ||
| 81 | |||
| 82 | option 'length_thres' => ( | ||
| 83 | is => 'rw', | ||
| 84 | isa => 'Int', | ||
| 85 | default => 300, | ||
| 86 | documentation => 'genic elements shorter than this are skipped.', | ||
| 87 | ); | ||
| 88 | |||
| 89 | |||
| 90 | ####################################################################### | ||
| 91 | ########################## Consume Roles ########################## | ||
| 92 | ####################################################################### | ||
| 93 | with | ||
| 94 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 95 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 96 | -excludes => 'validate_args', | ||
| 97 | }, | ||
| 98 | "CLIPSeqTools::Role::Option::Genes" => { | ||
| 99 | -alias => { validate_args => '_validate_args_for_genes' }, | ||
| 100 | -excludes => 'validate_args', | ||
| 101 | }, | ||
| 102 | "CLIPSeqTools::Role::Option::Plot" => { | ||
| 103 | -alias => { validate_args => '_validate_args_for_plot' }, | ||
| 104 | -excludes => 'validate_args', | ||
| 105 | }, | ||
| 106 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 107 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 108 | -excludes => 'validate_args', | ||
| 109 | }; | ||
| 110 | |||
| 111 | |||
| 112 | ####################################################################### | ||
| 113 | ######################## Interface Methods ######################## | ||
| 114 | ####################################################################### | ||
| 115 | sub validate_args { | ||
| 116 | my ($self) = @_; | ||
| 117 | |||
| 118 | $self->_validate_args_for_library; | ||
| 119 | $self->_validate_args_for_genes; | ||
| 120 | $self->_validate_args_for_plot; | ||
| 121 | $self->_validate_args_for_output_prefix; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub run { | ||
| 125 | my ($self) = @_; | ||
| 126 | |||
| 127 | warn "Starting analysis: distribution_on_genic_elements\n"; | ||
| 128 | |||
| 129 | warn "Validating arguments\n" if $self->verbose; | ||
| 130 | $self->validate_args(); | ||
| 131 | |||
| 132 | warn "Creating gene collection\n" if $self->verbose; | ||
| 133 | my $gene_collection = $self->gene_collection; | ||
| 134 | my @genes = $gene_collection->all_records; | ||
| 135 | |||
| 136 | warn "Creating transcript collection\n" if $self->verbose; | ||
| 137 | my @coding_transcripts; | ||
| 138 | foreach my $gene (@genes) { | ||
| 139 | my @transcripts = grep{$_->is_coding} @{$gene->transcripts}; | ||
| 140 | next if !@transcripts; | ||
| 141 | |||
| 142 | my $random_pick = int(rand(@transcripts)); | ||
| 143 | push @coding_transcripts, $transcripts[$random_pick]; | ||
| 144 | } | ||
| 145 | |||
| 146 | warn "Creating reads collection\n" if $self->verbose; | ||
| 147 | my $reads_col = $self->reads_collection; | ||
| 148 | |||
| 149 | warn "Measuring reads in bins for genic elements\n" if $self->verbose; | ||
| 150 | my ($counted_utr5s, $counted_cdss, $counted_utr3s) = (0, 0, 0); | ||
| 151 | my $total_copy_number = $reads_col->total_copy_number; | ||
| 152 | foreach my $tran (@coding_transcripts) { | ||
| 153 | $tran->extra({ | ||
| 154 | utr5_counts => [map {'NA'} (0..$self->bins-1)], | ||
| 155 | cds_counts => [map {'NA'} (0..$self->bins-1)], | ||
| 156 | utr3_counts => [map {'NA'} (0..$self->bins-1)], | ||
| 157 | }); | ||
| 158 | |||
| 159 | if (defined $tran->utr5 | ||
| 160 | and $tran->utr5->exonic_length > $self->length_thres) { | ||
| 161 | |||
| 162 | $tran->extra->{utr5_counts} = | ||
| 163 | count_copy_number_in_bins_of_element( | ||
| 164 | $tran->utr5, $reads_col, $self->bins); | ||
| 165 | $counted_utr5s++; | ||
| 166 | } | ||
| 167 | |||
| 168 | if (defined $tran->cds | ||
| 169 | and $tran->cds->exonic_length > $self->length_thres) { | ||
| 170 | |||
| 171 | $tran->extra->{cds_counts} = | ||
| 172 | count_copy_number_in_bins_of_element( | ||
| 173 | $tran->cds, $reads_col, $self->bins); | ||
| 174 | $counted_cdss++; | ||
| 175 | } | ||
| 176 | |||
| 177 | if (defined $tran->utr3 | ||
| 178 | and $tran->utr3->exonic_length > $self->length_thres) { | ||
| 179 | |||
| 180 | $tran->extra->{utr3_counts} = | ||
| 181 | count_copy_number_in_bins_of_element( | ||
| 182 | $tran->utr3, $reads_col, $self->bins); | ||
| 183 | $counted_utr3s++; | ||
| 184 | } | ||
| 185 | }; | ||
| 186 | warn "Counted UTR5s: $counted_utr5s\n" if $self->verbose; | ||
| 187 | warn "Counted CDSs: $counted_cdss\n" if $self->verbose; | ||
| 188 | warn "Counted UTR3s: $counted_utr3s\n" if $self->verbose; | ||
| 189 | |||
| 190 | warn "Creating output path\n" if $self->verbose; | ||
| 191 | $self->make_path_for_output_prefix(); | ||
| 192 | |||
| 193 | warn "Printing results\n" if $self->verbose; | ||
| 194 | open (my $OUT, '>', $self->o_prefix.'distribution_on_genic_elements.tab'); | ||
| 195 | say $OUT join( | ||
| 196 | "\t", | ||
| 197 | 'transcript_id', | ||
| 198 | (map {'utr5_bin_' . $_} (0..$self->bins-1)), | ||
| 199 | (map {'cds_bin_' . $_} (0..$self->bins-1)), | ||
| 200 | (map {'utr3_bin_' . $_} (0..$self->bins-1))); | ||
| 201 | foreach my $tran (@coding_transcripts) { | ||
| 202 | next if !defined $tran->extra; | ||
| 203 | |||
| 204 | say $OUT join( | ||
| 205 | "\t", | ||
| 206 | $tran->id, | ||
| 207 | @{$tran->extra->{utr5_counts}}, | ||
| 208 | @{$tran->extra->{cds_counts}}, | ||
| 209 | @{$tran->extra->{utr3_counts}}); | ||
| 210 | } | ||
| 211 | close $OUT; | ||
| 212 | |||
| 213 | if ($self->plot) { | ||
| 214 | warn "Creating plot\n" if $self->verbose; | ||
| 215 | CLIPSeqTools::PlotApp->initialize_command_class( | ||
| 216 | 'CLIPSeqTools::PlotApp::distribution_on_genic_elements', | ||
| 217 | file => $self->o_prefix.'distribution_on_genic_elements.tab', | ||
| 218 | o_prefix => $self->o_prefix | ||
| 219 | )->run(); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | ####################################################################### | ||
| 225 | ############################ Functions ############################ | ||
| 226 | ####################################################################### | ||
| 227 | sub count_copy_number_in_bins_of_element { | ||
| 228 | my ($elm, $reads_col, $bins) = @_; | ||
| 229 | |||
| 230 | my @counts = map{0} 0..$bins-1; | ||
| 231 | my $max_length = $reads_col->longest_record->length; | ||
| 232 | my $elm_exon_length = $elm->exonic_length; | ||
| 233 | my $span = int($max_length / 2); | ||
| 234 | |||
| 235 | $reads_col->foreach_contained_record_do( | ||
| 236 | $elm->strand, $elm->chromosome, $elm->start-$span, $elm->stop+$span, | ||
| 237 | sub { | ||
| 238 | my ($read) = @_; | ||
| 239 | |||
| 240 | my $pos = $elm->relative_exonic_position($read->mid_position); | ||
| 241 | return 0 if !defined $pos; | ||
| 242 | |||
| 243 | $pos = $elm_exon_length - $pos - 1 if $elm->strand == -1; | ||
| 244 | |||
| 245 | my $bin = int($bins * ($pos / $elm_exon_length)); | ||
| 246 | $counts[$bin] += 1; #$read->copy_number; | ||
| 247 | } | ||
| 248 | ); | ||
| 249 | |||
| 250 | return \@counts; | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | 1; |