« CLIPSeqTools/App/nmer_enrichment_over_shuffled.pm
↑ Summary
CLIPSeqTools/PreprocessApp/sam_to_sqlite.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::distribution_on_introns_exons - Measure read distribution | ||
| 4 | on exons and introns. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools distribution_on_introns_exons [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Measure the distribution of reads along exons and introns. | ||
| 13 | Will split the exons and introns of coding transcripts in bins and measure the | ||
| 14 | read density in each bin. Will keep only unique introns and exons foreach | ||
| 15 | location. 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_introns_exons; | ||
| 57 | $CLIPSeqTools::App::distribution_on_introns_exons::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_introns_exons\n"; | ||
| 128 | |||
| 129 | warn "Validating arguments\n" if $self->verbose; | ||
| 130 | $self->validate_args(); | ||
| 131 | |||
| 132 | warn "Creating transcript collection\n" if $self->verbose; | ||
| 133 | my $transcript_collection = $self->transcript_collection; | ||
| 134 | my @coding_transcripts = | ||
| 135 | grep{$_->is_coding} $transcript_collection->all_records; | ||
| 136 | |||
| 137 | warn "Creating reads collection\n" if $self->verbose; | ||
| 138 | my $reads_col = $self->reads_collection; | ||
| 139 | |||
| 140 | warn "Measuring reads in bins of introns/exons\n" if $self->verbose; | ||
| 141 | my (%exons, %introns); | ||
| 142 | foreach my $transcript (@coding_transcripts) { | ||
| 143 | foreach my $exon (@{$transcript->exons}) { | ||
| 144 | next if exists $exons{$exon->location}; | ||
| 145 | |||
| 146 | my $exon_counts = copy_number_in_bins_of_element( | ||
| 147 | $exon, $reads_col, $self->bins); | ||
| 148 | $exon->extra({counts => $exon_counts}); | ||
| 149 | $exons{$exon->location} = $exon; | ||
| 150 | } | ||
| 151 | |||
| 152 | foreach my $intron (@{$transcript->introns}) { | ||
| 153 | next if exists $introns{$intron->location}; | ||
| 154 | |||
| 155 | my $intron_counts = copy_number_in_bins_of_element( | ||
| 156 | $intron, $reads_col, $self->bins); | ||
| 157 | $intron->extra({counts => $intron_counts}); | ||
| 158 | $introns{$intron->location} = $intron; | ||
| 159 | } | ||
| 160 | }; | ||
| 161 | warn "Counted exons: " . scalar(keys %exons) . "\n" if $self->verbose; | ||
| 162 | warn "Counted introns: " . scalar(keys %introns) . "\n" if $self->verbose; | ||
| 163 | |||
| 164 | warn "Creating output path\n" if $self->verbose; | ||
| 165 | $self->make_path_for_output_prefix(); | ||
| 166 | |||
| 167 | warn "Printing results\n" if $self->verbose; | ||
| 168 | open (my $OUT, '>', $self->o_prefix.'distribution_on_introns_exons.tab'); | ||
| 169 | say $OUT join( | ||
| 170 | "\t", 'element', 'location', (map {'bin_' . $_} (0..$self->bins-1))); | ||
| 171 | foreach my $exon_loc (keys %exons) { | ||
| 172 | my $exon = $exons{$exon_loc}; | ||
| 173 | say $OUT join( | ||
| 174 | "\t", 'exon', $exon->location, @{$exon->extra->{counts}}); | ||
| 175 | } | ||
| 176 | foreach my $intron_loc (keys %introns) { | ||
| 177 | my $intron = $introns{$intron_loc}; | ||
| 178 | say $OUT join( | ||
| 179 | "\t", 'intron', $intron->location, @{$intron->extra->{counts}}); | ||
| 180 | } | ||
| 181 | |||
| 182 | if ($self->plot) { | ||
| 183 | warn "Creating plot\n" if $self->verbose; | ||
| 184 | CLIPSeqTools::PlotApp->initialize_command_class( | ||
| 185 | 'CLIPSeqTools::PlotApp::distribution_on_introns_exons', | ||
| 186 | file => $self->o_prefix.'distribution_on_introns_exons.tab', | ||
| 187 | o_prefix => $self->o_prefix | ||
| 188 | )->run(); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | ####################################################################### | ||
| 194 | ############################ Functions ############################ | ||
| 195 | ####################################################################### | ||
| 196 | sub copy_number_in_bins_of_element { | ||
| 197 | my ($elm, $reads_col, $bins) = @_; | ||
| 198 | |||
| 199 | my @counts = map{0} 0..$bins-1; | ||
| 200 | my $max_length = $reads_col->longest_record->length; | ||
| 201 | my $span = int($max_length / 2); | ||
| 202 | |||
| 203 | $reads_col->foreach_contained_record_do( | ||
| 204 | $elm->strand, $elm->chromosome, $elm->start-$span, $elm->stop+$span, | ||
| 205 | sub { | ||
| 206 | my ($record) = @_; | ||
| 207 | |||
| 208 | my $record_mid = $record->mid_position; | ||
| 209 | return 0 if ( | ||
| 210 | $record_mid < $elm->start or $record_mid > $elm->stop); | ||
| 211 | |||
| 212 | my $bin = int($bins * | ||
| 213 | (abs($elm->head_mid_distance_from($record)) / $elm->length)); | ||
| 214 | |||
| 215 | $counts[$bin] += 1; # $record->copy_number; | ||
| 216 | }); | ||
| 217 | |||
| 218 | return \@counts; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; |