« CLIPSeqTools/CompareApp/libraries_relative_read_density.pm
↑ Summary
CLIPSeqTools/CompareApp/libraries_overlap_stats.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::distribution_on_genic_elements - Measure read distribution on 5'UTR, CDS and 3'UTR. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools distribution_on_genic_elements [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Measure the distribution of reads along transcript elements in parts of their length. | ||
| 12 | Split the 5'UTR, CDS and 3'UTR of coding transcripts in bins and measure the read density in each bin. | ||
| 13 | |||
| 14 | =head1 OPTIONS | ||
| 15 | |||
| 16 | Input options for library. | ||
| 17 | --driver <Str> driver for database connection (eg. mysql, | ||
| 18 | SQLite). | ||
| 19 | --database <Str> database name or path to database file for file | ||
| 20 | based databases (eg. SQLite). | ||
| 21 | --table <Str> database table. | ||
| 22 | --host <Str> hostname for database connection. | ||
| 23 | --user <Str> username for database connection. | ||
| 24 | --password <Str> password for database connection. | ||
| 25 | --records_class <Str> type of records stored in database. | ||
| 26 | --filter <Filter> filter library. May be used multiple times. | ||
| 27 | Syntax: column_name="pattern" | ||
| 28 | e.g. keep reads with deletions AND not repeat | ||
| 29 | masked AND longer than 31 | ||
| 30 | --filter deletion="def" | ||
| 31 | --filter rmsk="undef" . | ||
| 32 | --filter query_length=">31". | ||
| 33 | Operators: >, >=, <, <=, =, !=, def, undef | ||
| 34 | |||
| 35 | Other input | ||
| 36 | --gtf <Str> GTF file with genes/transcripts. | ||
| 37 | |||
| 38 | Output | ||
| 39 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 40 | extension to path. [Default: ./] | ||
| 41 | |||
| 42 | Other options. | ||
| 43 | --bins <Int> number of bins each element is split into. | ||
| 44 | [Default: 10] | ||
| 45 | --length_thres <Int> genic elements shorter than this are skipped. | ||
| 46 | [Default: 300] | ||
| 47 | --plot call plotting script to create plots. | ||
| 48 | -v --verbose print progress lines and extra information. | ||
| 49 | -h -? --usage --help print help message | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | package CLIPSeqTools::App::distribution_on_genic_elements; | ||
| 54 | $CLIPSeqTools::App::distribution_on_genic_elements::VERSION = '0.1.1'; | ||
| 55 | |||
| 56 | # Make it an app command | ||
| 57 | use MooseX::App::Command; | ||
| 58 | extends 'CLIPSeqTools::App'; | ||
| 59 | |||
| 60 | |||
| 61 | ####################################################################### | ||
| 62 | ####################### Load External modules ##################### | ||
| 63 | ####################################################################### | ||
| 64 | use Modern::Perl; | ||
| 65 | use autodie; | ||
| 66 | use namespace::autoclean; | ||
| 67 | |||
| 68 | |||
| 69 | ####################################################################### | ||
| 70 | ####################### Command line options ###################### | ||
| 71 | ####################################################################### | ||
| 72 | option 'bins' => ( | ||
| 73 | is => 'rw', | ||
| 74 | isa => 'Int', | ||
| 75 | default => 10, | ||
| 76 | documentation => 'number of bins each element is split into.', | ||
| 77 | ); | ||
| 78 | |||
| 79 | option 'length_thres' => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => 'Int', | ||
| 82 | default => 300, | ||
| 83 | documentation => 'genic elements shorter than this are skipped.', | ||
| 84 | ); | ||
| 85 | |||
| 86 | |||
| 87 | ####################################################################### | ||
| 88 | ########################## Consume Roles ########################## | ||
| 89 | ####################################################################### | ||
| 90 | with | ||
| 91 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 92 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 93 | -excludes => 'validate_args', | ||
| 94 | }, | ||
| 95 | "CLIPSeqTools::Role::Option::Transcripts" => { | ||
| 96 | -alias => { validate_args => '_validate_args_for_transcripts' }, | ||
| 97 | -excludes => 'validate_args', | ||
| 98 | }, | ||
| 99 | "CLIPSeqTools::Role::Option::Plot" => { | ||
| 100 | -alias => { validate_args => '_validate_args_for_plot' }, | ||
| 101 | -excludes => 'validate_args', | ||
| 102 | }, | ||
| 103 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 104 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 105 | -excludes => 'validate_args', | ||
| 106 | }; | ||
| 107 | |||
| 108 | |||
| 109 | ####################################################################### | ||
| 110 | ######################## Interface Methods ######################## | ||
| 111 | ####################################################################### | ||
| 112 | sub validate_args { | ||
| 113 | my ($self) = @_; | ||
| 114 | |||
| 115 | $self->_validate_args_for_library; | ||
| 116 | $self->_validate_args_for_transcripts; | ||
| 117 | $self->_validate_args_for_plot; | ||
| 118 | $self->_validate_args_for_output_prefix; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub run { | ||
| 122 | my ($self) = @_; | ||
| 123 | |||
| 124 | warn "Starting analysis: distribution_on_genic_elements\n"; | ||
| 125 | |||
| 126 | warn "Validating arguments\n" if $self->verbose; | ||
| 127 | $self->validate_args(); | ||
| 128 | |||
| 129 | warn "Creating transcript collection\n" if $self->verbose; | ||
| 130 | my $transcript_collection = $self->transcript_collection; | ||
| 131 | my @coding_transcripts = grep{$_->is_coding} $transcript_collection->all_records; | ||
| 132 | |||
| 133 | warn "Creating reads collection\n" if $self->verbose; | ||
| 134 | my $reads_col = $self->reads_collection; | ||
| 135 | |||
| 136 | warn "Calculating counts in bins of genic elements per transcript\n" if $self->verbose; | ||
| 137 | my ($counted_utr5s, $counted_cdss, $counted_utr3s) = (0, 0, 0); | ||
| 138 | my $total_copy_number = $reads_col->total_copy_number; | ||
| 139 | foreach my $tran (@coding_transcripts) { | ||
| 140 | $tran->extra({ | ||
| 141 | utr5_counts => [map {'NA'} (0..$self->bins-1)], | ||
| 142 | cds_counts => [map {'NA'} (0..$self->bins-1)], | ||
| 143 | utr3_counts => [map {'NA'} (0..$self->bins-1)], | ||
| 144 | }); | ||
| 145 | |||
| 146 | if (defined $tran->utr5 and $tran->utr5->exonic_length > $self->length_thres) { | ||
| 147 | $tran->extra->{utr5_counts} = count_copy_number_in_percent_of_length_of_element( | ||
| 148 | $tran->utr5, $reads_col, $self->bins); | ||
| 149 | $counted_utr5s++; | ||
| 150 | } | ||
| 151 | |||
| 152 | if (defined $tran->cds and $tran->cds->exonic_length > $self->length_thres) { | ||
| 153 | $tran->extra->{cds_counts} = count_copy_number_in_percent_of_length_of_element( | ||
| 154 | $tran->cds, $reads_col, $self->bins); | ||
| 155 | $counted_cdss++; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (defined $tran->utr3 and $tran->utr3->exonic_length > $self->length_thres) { | ||
| 159 | $tran->extra->{utr3_counts} = count_copy_number_in_percent_of_length_of_element( | ||
| 160 | $tran->utr3, $reads_col, $self->bins); | ||
| 161 | $counted_utr3s++; | ||
| 162 | } | ||
| 163 | }; | ||
| 164 | warn "Counted UTR5s: $counted_utr5s\n" if $self->verbose; | ||
| 165 | warn "Counted CDSs: $counted_cdss\n" if $self->verbose; | ||
| 166 | warn "Counted UTR3s: $counted_utr3s\n" if $self->verbose; | ||
| 167 | |||
| 168 | warn "Creating output path\n" if $self->verbose; | ||
| 169 | $self->make_path_for_output_prefix(); | ||
| 170 | |||
| 171 | warn "Printing results\n" if $self->verbose; | ||
| 172 | open (my $OUT, '>', $self->o_prefix.'distribution_on_genic_elements.tab'); | ||
| 173 | say $OUT join("\t", 'transcript_id', | ||
| 174 | (map {'utr5_bin_' . $_} (0..$self->bins-1)), | ||
| 175 | (map {'cds_bin_' . $_} (0..$self->bins-1)), | ||
| 176 | (map {'utr3_bin_' . $_} (0..$self->bins-1))); | ||
| 177 | foreach my $tran (@coding_transcripts) { | ||
| 178 | next if !defined $tran->extra; | ||
| 179 | say $OUT join("\t", $tran->id, | ||
| 180 | @{$tran->extra->{utr5_counts}}, | ||
| 181 | @{$tran->extra->{cds_counts}}, | ||
| 182 | @{$tran->extra->{utr3_counts}}); | ||
| 183 | } | ||
| 184 | close $OUT; | ||
| 185 | |||
| 186 | if ($self->plot) { | ||
| 187 | warn "Creating plot\n" if $self->verbose; | ||
| 188 | CLIPSeqTools::PlotApp->initialize_command_class('CLIPSeqTools::PlotApp::distribution_on_genic_elements', | ||
| 189 | file => $self->o_prefix.'distribution_on_genic_elements.tab', | ||
| 190 | o_prefix => $self->o_prefix | ||
| 191 | )->run(); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | ####################################################################### | ||
| 197 | ############################ Functions ############################ | ||
| 198 | ####################################################################### | ||
| 199 | sub count_copy_number_in_percent_of_length_of_element { | ||
| 200 | my ($part, $reads_col, $bins) = @_; | ||
| 201 | |||
| 202 | my @counts = map{0} 0..$bins-1; | ||
| 203 | my $longest_record_length = $reads_col->longest_record->length; | ||
| 204 | my $part_exonic_length = $part->exonic_length; | ||
| 205 | my $margin = int($longest_record_length/2); | ||
| 206 | $reads_col->foreach_contained_record_do($part->strand, $part->chromosome, $part->start-$margin, $part->stop+$margin, sub { | ||
| 207 | my ($record) = @_; | ||
| 208 | |||
| 209 | my $relative_position_in_part = $part->relative_exonic_position($record->mid_position) // return 0; | ||
| 210 | $relative_position_in_part = $part_exonic_length - $relative_position_in_part if $part->strand == -1; | ||
| 211 | my $bin = int($bins * ($relative_position_in_part / $part->exonic_length)); | ||
| 212 | $counts[$bin] += $record->copy_number; | ||
| 213 | }); | ||
| 214 | |||
| 215 | return \@counts; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | 1; |