« CLIPSeqTools/PreprocessApp/annotate_with_genic_elements.pm
↑ Summary
CLIPSeqTools/App/reads_long_gaps_size_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::genomic_distribution - Count reads on genes, repeats, exons, introns, 5'UTRs, ... | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools genomic_distribution [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Measure the number of reads that align to each genome wide annotation | ||
| 12 | such as genic, intergenic, repeats, exonic, intronic, etc. | ||
| 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 | Output | ||
| 36 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 37 | extension to path. [Default: ./] | ||
| 38 | |||
| 39 | Other options. | ||
| 40 | --plot call plotting script to create plots. | ||
| 41 | -v --verbose print progress lines and extra information. | ||
| 42 | -h -? --usage --help print help message | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | package CLIPSeqTools::App::genomic_distribution; | ||
| 47 | $CLIPSeqTools::App::genomic_distribution::VERSION = '1.0.0'; | ||
| 48 | |||
| 49 | # Make it an app command | ||
| 50 | use MooseX::App::Command; | ||
| 51 | extends 'CLIPSeqTools::App'; | ||
| 52 | |||
| 53 | |||
| 54 | ####################################################################### | ||
| 55 | ####################### Load External modules ##################### | ||
| 56 | ####################################################################### | ||
| 57 | use Modern::Perl; | ||
| 58 | use autodie; | ||
| 59 | use namespace::autoclean; | ||
| 60 | |||
| 61 | |||
| 62 | ####################################################################### | ||
| 63 | ########################## Consume Roles ########################## | ||
| 64 | ####################################################################### | ||
| 65 | with | ||
| 66 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 67 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 68 | -excludes => 'validate_args', | ||
| 69 | }, | ||
| 70 | "CLIPSeqTools::Role::Option::Plot" => { | ||
| 71 | -alias => { validate_args => '_validate_args_for_plot' }, | ||
| 72 | -excludes => 'validate_args', | ||
| 73 | }, | ||
| 74 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 75 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 76 | -excludes => 'validate_args', | ||
| 77 | }; | ||
| 78 | |||
| 79 | |||
| 80 | ####################################################################### | ||
| 81 | ######################## Interface Methods ######################## | ||
| 82 | ####################################################################### | ||
| 83 | sub validate_args { | ||
| 84 | my ($self) = @_; | ||
| 85 | |||
| 86 | $self->_validate_args_for_library; | ||
| 87 | $self->_validate_args_for_plot; | ||
| 88 | $self->_validate_args_for_output_prefix; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub run { | ||
| 92 | my ($self) = @_; | ||
| 93 | |||
| 94 | warn "Starting analysis: genomic_distribution\n"; | ||
| 95 | |||
| 96 | warn "Validating arguments\n" if $self->verbose; | ||
| 97 | $self->validate_args(); | ||
| 98 | |||
| 99 | warn "Creating reads collection\n" if $self->verbose; | ||
| 100 | my $reads_collection = $self->reads_collection; | ||
| 101 | $reads_collection->schema->storage->debug(1) if $self->verbose > 1; | ||
| 102 | |||
| 103 | warn "Preparing reads resultset\n" if $self->verbose; | ||
| 104 | my $reads_rs = $reads_collection->resultset; | ||
| 105 | |||
| 106 | warn "Counting reads for each annotation\n" if $self->verbose; | ||
| 107 | my %counts; | ||
| 108 | |||
| 109 | $counts{'total'} = $reads_rs->get_column('copy_number')->sum || 0; | ||
| 110 | |||
| 111 | $counts{'repeats'} = $reads_rs->search({ | ||
| 112 | rmsk => {'!=', undef} | ||
| 113 | })->get_column('copy_number')->sum || 0; | ||
| 114 | |||
| 115 | $counts{'intergenic'} = $reads_rs->search({ | ||
| 116 | transcript => undef, | ||
| 117 | rmsk => undef | ||
| 118 | })->get_column('copy_number')->sum || 0; | ||
| 119 | |||
| 120 | $counts{'genic'} = $reads_rs->search({ | ||
| 121 | transcript => {'!=', undef}, | ||
| 122 | })->get_column('copy_number')->sum || 0; | ||
| 123 | |||
| 124 | $counts{'exonic'} = $reads_rs->search({ | ||
| 125 | transcript => {'!=', undef}, | ||
| 126 | exon => {'!=', undef}, | ||
| 127 | })->get_column('copy_number')->sum || 0; | ||
| 128 | |||
| 129 | $counts{'intronic'} = $reads_rs->search({ | ||
| 130 | transcript => {'!=', undef}, | ||
| 131 | exon => undef, | ||
| 132 | })->get_column('copy_number')->sum || 0; | ||
| 133 | |||
| 134 | $counts{'genic-norepeat'} = $reads_rs->search({ | ||
| 135 | transcript => {'!=', undef}, | ||
| 136 | rmsk => undef | ||
| 137 | })->get_column('copy_number')->sum || 0; | ||
| 138 | |||
| 139 | $counts{'exonic-norepeat'} = $reads_rs->search({ | ||
| 140 | exon => {'!=', undef}, | ||
| 141 | rmsk => undef | ||
| 142 | })->get_column('copy_number')->sum || 0; | ||
| 143 | |||
| 144 | $counts{'intronic-norepeat'} = $reads_rs->search({ | ||
| 145 | transcript => {'!=', undef}, | ||
| 146 | exon => undef, | ||
| 147 | rmsk => undef | ||
| 148 | })->get_column('copy_number')->sum || 0; | ||
| 149 | |||
| 150 | # Coding transcripts | ||
| 151 | $counts{'genic-coding-norepeat'} = $reads_rs->search({ | ||
| 152 | coding_transcript => {'!=', undef}, | ||
| 153 | rmsk => undef | ||
| 154 | })->get_column('copy_number')->sum || 0; | ||
| 155 | |||
| 156 | $counts{'intronic-coding-norepeat'} = $reads_rs->search({ | ||
| 157 | coding_transcript => {'!=', undef}, | ||
| 158 | exon => undef, | ||
| 159 | rmsk => undef | ||
| 160 | })->get_column('copy_number')->sum || 0; | ||
| 161 | |||
| 162 | $counts{'exonic-coding-norepeat'} = $reads_rs->search({ | ||
| 163 | coding_transcript => {'!=', undef}, | ||
| 164 | exon => {'!=', undef}, | ||
| 165 | rmsk => undef | ||
| 166 | })->get_column('copy_number')->sum || 0; | ||
| 167 | |||
| 168 | $counts{'utr5-exonic-coding-norepeat'} = $reads_rs->search({ | ||
| 169 | coding_transcript => {'!=', undef}, | ||
| 170 | exon => {'!=', undef}, | ||
| 171 | utr5 => {'!=', undef}, | ||
| 172 | rmsk => undef | ||
| 173 | })->get_column('copy_number')->sum || 0; | ||
| 174 | |||
| 175 | $counts{'cds-exonic-coding-norepeat'} = $reads_rs->search({ | ||
| 176 | coding_transcript => {'!=', undef}, | ||
| 177 | exon => {'!=', undef}, | ||
| 178 | cds => {'!=', undef}, | ||
| 179 | rmsk => undef | ||
| 180 | })->get_column('copy_number')->sum || 0; | ||
| 181 | |||
| 182 | $counts{'utr3-exonic-coding-norepeat'} = $reads_rs->search({ | ||
| 183 | coding_transcript => {'!=', undef}, | ||
| 184 | exon => {'!=', undef}, | ||
| 185 | utr3 => {'!=', undef}, | ||
| 186 | rmsk => undef | ||
| 187 | })->get_column('copy_number')->sum || 0; | ||
| 188 | |||
| 189 | |||
| 190 | warn "Creating output path\n" if $self->verbose; | ||
| 191 | $self->make_path_for_output_prefix(); | ||
| 192 | |||
| 193 | |||
| 194 | warn "Printing results\n" if $self->verbose; | ||
| 195 | open (my $OUT, '>', $self->o_prefix.'genomic_distribution.tab'); | ||
| 196 | print $OUT | ||
| 197 | join("\t", 'category', 'count', 'total')."\n". | ||
| 198 | |||
| 199 | join("\t", 'Repeat', $counts{'repeats'}, $counts{'total'})."\n". | ||
| 200 | join("\t", 'Intergenic (-repeat)', $counts{'intergenic'}, $counts{'total'})."\n". | ||
| 201 | join("\t", 'Genic', $counts{'genic'}, $counts{'total'})."\n". | ||
| 202 | join("\t", 'Intronic', $counts{'intronic'}, $counts{'total'})."\n". | ||
| 203 | join("\t", 'Exonic', $counts{'exonic'}, $counts{'total'})."\n". | ||
| 204 | join("\t", 'Genic (-repeat)', $counts{'genic-norepeat'}, $counts{'total'})."\n". | ||
| 205 | join("\t", 'Intronic (-repeat)', $counts{'intronic-norepeat'}, $counts{'total'})."\n". | ||
| 206 | join("\t", 'Exonic (-repeat)', $counts{'exonic-norepeat'}, $counts{'total'})."\n". | ||
| 207 | join("\t", 'Genic (+code -repeat)', $counts{'genic-coding-norepeat'}, $counts{'total'})."\n". | ||
| 208 | join("\t", 'Intronic (+code -repeat)', $counts{'intronic-coding-norepeat'}, $counts{'total'})."\n". | ||
| 209 | join("\t", 'Exonic (+code -repeat)', $counts{'exonic-coding-norepeat'}, $counts{'total'})."\n". | ||
| 210 | join("\t", '5UTR (+exonic +code -repeat)', $counts{'utr5-exonic-coding-norepeat'}, $counts{'total'})."\n". | ||
| 211 | join("\t", 'CDS (+exonic +code -repeat)', $counts{'cds-exonic-coding-norepeat'}, $counts{'total'})."\n". | ||
| 212 | join("\t", '3UTR (+exonic +code -repeat)', $counts{'utr3-exonic-coding-norepeat'}, $counts{'total'})."\n"; | ||
| 213 | close $OUT; | ||
| 214 | |||
| 215 | if ($self->plot) { | ||
| 216 | warn "Creating plot\n" if $self->verbose; | ||
| 217 | CLIPSeqTools::PlotApp->initialize_command_class('CLIPSeqTools::PlotApp::genomic_distribution', | ||
| 218 | file => $self->o_prefix.'genomic_distribution.tab', | ||
| 219 | o_prefix => $self->o_prefix | ||
| 220 | )->run(); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | 1; |