« CLIPSeqTools/PreprocessApp/sam_to_sqlite.pm
↑ Summary
CLIPSeqTools/PreprocessApp/annotate_with_file.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::distribution_on_introns_exons - Measure read distribution on exons and introns. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools distribution_on_introns_exons [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Measure the distribution of reads along exons and introns as part of their length. | ||
| 12 | Split the exons and introns 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_introns_exons; | ||
| 54 | $CLIPSeqTools::App::distribution_on_introns_exons::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_introns_exons\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 "Measuring reads in bins of introns/exons per transcript\n" if $self->verbose; | ||
| 137 | my (%exons, %introns); | ||
| 138 | foreach my $transcript (@coding_transcripts) { | ||
| 139 | foreach my $exon (@{$transcript->exons}) { | ||
| 140 | next if exists $exons{$exon->location}; | ||
| 141 | my $exon_counts = copy_number_in_bins_of_element($exon, $reads_col, $self->bins); | ||
| 142 | $exon->extra({counts => $exon_counts}); | ||
| 143 | $exons{$exon->location} = $exon; | ||
| 144 | } | ||
| 145 | |||
| 146 | foreach my $intron (@{$transcript->introns}) { | ||
| 147 | next if exists $introns{$intron->location}; | ||
| 148 | my $intron_counts = copy_number_in_bins_of_element($intron, $reads_col, $self->bins); | ||
| 149 | $intron->extra({counts => $intron_counts}); | ||
| 150 | $introns{$intron->location} = $intron; | ||
| 151 | } | ||
| 152 | }; | ||
| 153 | warn "Counted exons: " . scalar(keys %exons) . "\n" if $self->verbose; | ||
| 154 | warn "Counted introns: " . scalar(keys %introns) . "\n" if $self->verbose; | ||
| 155 | |||
| 156 | warn "Creating output path\n" if $self->verbose; | ||
| 157 | $self->make_path_for_output_prefix(); | ||
| 158 | |||
| 159 | warn "Printing results\n" if $self->verbose; | ||
| 160 | open (my $OUT, '>', $self->o_prefix.'distribution_on_introns_exons.tab'); | ||
| 161 | say $OUT join("\t", 'element', 'location', (map {'bin_' . $_} (0..$self->bins-1))); | ||
| 162 | foreach my $exon_loc (keys %exons) { | ||
| 163 | my $exon = $exons{$exon_loc}; | ||
| 164 | say $OUT join("\t", 'exon', $exon->location, @{$exon->extra->{counts}}); | ||
| 165 | } | ||
| 166 | foreach my $intron_loc (keys %introns) { | ||
| 167 | my $intron = $introns{$intron_loc}; | ||
| 168 | say $OUT join("\t", 'intron', $intron->location, @{$intron->extra->{counts}}); | ||
| 169 | } | ||
| 170 | |||
| 171 | if ($self->plot) { | ||
| 172 | warn "Creating plot\n" if $self->verbose; | ||
| 173 | CLIPSeqTools::PlotApp->initialize_command_class('CLIPSeqTools::PlotApp::distribution_on_introns_exons', | ||
| 174 | file => $self->o_prefix.'distribution_on_introns_exons.tab', | ||
| 175 | o_prefix => $self->o_prefix | ||
| 176 | )->run(); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | ####################################################################### | ||
| 182 | ############################ Functions ############################ | ||
| 183 | ####################################################################### | ||
| 184 | sub copy_number_in_bins_of_element { | ||
| 185 | my ($part, $reads_col, $bins) = @_; | ||
| 186 | |||
| 187 | my @counts = map{0} 0..$bins-1; | ||
| 188 | my $longest_record_length = $reads_col->longest_record->length; | ||
| 189 | my $margin = int($longest_record_length/2); | ||
| 190 | $reads_col->foreach_contained_record_do($part->strand, $part->chromosome, $part->start-$margin, $part->stop+$margin, sub { | ||
| 191 | my ($record) = @_; | ||
| 192 | |||
| 193 | my $record_mid = $record->mid_position; | ||
| 194 | return 0 if ($record_mid < $part->start or $record_mid > $part->stop); | ||
| 195 | |||
| 196 | my $bin = int($bins * (abs($part->head_mid_distance_from($record)) / $part->length)); | ||
| 197 | if ($bin >= $bins) { | ||
| 198 | warn join("\t", $part->location, $record->location, $part->head_mid_distance_from($record),$record->mid_position, $record->cigar)."\n"; | ||
| 199 | } | ||
| 200 | $counts[$bin] += $record->copy_number; | ||
| 201 | }); | ||
| 202 | |||
| 203 | return \@counts; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; |