| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::count_reads_on_genic_elements - Count reads on transcripts, genes, exons, introns | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools count_reads_on_genic_elements [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Counts library reads on transcripts, genes, exons, introns. | ||
| 12 | |||
| 13 | * Transcript counts are measured only on their exons. | ||
| 14 | * Gene counts are measured only for exonic regions. | ||
| 15 | |||
| 16 | Output (4 files): counts.gene.tab, counts.transcript.tab, counts.exon.tab, counts.intron.tab - (name, location, length, count, count_per_nt, exonic_count, exonic_length, exonic_count_per_nt) | ||
| 17 | |||
| 18 | =head1 OPTIONS | ||
| 19 | |||
| 20 | Input options for library. | ||
| 21 | --driver <Str> driver for database connection (eg. mysql, | ||
| 22 | SQLite). | ||
| 23 | --database <Str> database name or path to database file for file | ||
| 24 | based databases (eg. SQLite). | ||
| 25 | --table <Str> database table. | ||
| 26 | --host <Str> hostname for database connection. | ||
| 27 | --user <Str> username for database connection. | ||
| 28 | --password <Str> password for database connection. | ||
| 29 | --records_class <Str> type of records stored in database. | ||
| 30 | --filter <Filter> filter library. May be used multiple times. | ||
| 31 | Syntax: column_name="pattern" | ||
| 32 | e.g. keep reads with deletions AND not repeat | ||
| 33 | masked AND longer than 31 | ||
| 34 | --filter deletion="def" | ||
| 35 | --filter rmsk="undef" . | ||
| 36 | --filter query_length=">31". | ||
| 37 | Operators: >, >=, <, <=, =, !=, def, undef | ||
| 38 | |||
| 39 | Other input | ||
| 40 | --gtf <Str> GTF file with genes/transcripts. | ||
| 41 | |||
| 42 | Output | ||
| 43 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 44 | extension to path. [Default: ./] | ||
| 45 | |||
| 46 | Other options. | ||
| 47 | -v --verbose print progress lines and extra information. | ||
| 48 | -h -? --usage --help print help message | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | |||
| 53 | package CLIPSeqTools::App::count_reads_on_genic_elements; | ||
| 54 | $CLIPSeqTools::App::count_reads_on_genic_elements::VERSION = '1.0.0'; | ||
| 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 | ########################## Consume Roles ########################## | ||
| 71 | ####################################################################### | ||
| 72 | with | ||
| 73 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 74 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 75 | -excludes => 'validate_args', | ||
| 76 | }, | ||
| 77 | "CLIPSeqTools::Role::Option::Genes" => { | ||
| 78 | -alias => { validate_args => '_validate_args_for_genes' }, | ||
| 79 | -excludes => 'validate_args', | ||
| 80 | }, | ||
| 81 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 82 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 83 | -excludes => 'validate_args', | ||
| 84 | }; | ||
| 85 | |||
| 86 | |||
| 87 | ####################################################################### | ||
| 88 | ######################## Interface Methods ######################## | ||
| 89 | ####################################################################### | ||
| 90 | sub validate_args { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | $self->_validate_args_for_library; | ||
| 94 | $self->_validate_args_for_genes; | ||
| 95 | $self->_validate_args_for_output_prefix; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub run { | ||
| 99 | my ($self) = @_; | ||
| 100 | |||
| 101 | warn "Starting analysis: count_reads_on_genic_elements\n"; | ||
| 102 | |||
| 103 | warn "Validating arguments\n" if $self->verbose; | ||
| 104 | $self->validate_args(); | ||
| 105 | |||
| 106 | warn "Creating gene collection\n" if $self->verbose; | ||
| 107 | my $gene_collection = $self->gene_collection; | ||
| 108 | |||
| 109 | warn "Creating transcript collection\n" if $self->verbose; | ||
| 110 | my $transcript_collection = $self->transcript_collection; | ||
| 111 | |||
| 112 | warn "Creating reads collection\n" if $self->verbose; | ||
| 113 | my $reads_collection = $self->reads_collection; | ||
| 114 | |||
| 115 | warn "Calculating transcript and exon/intron counts\n" if $self->verbose; | ||
| 116 | my $counter = 1; | ||
| 117 | $transcript_collection->foreach_record_do( sub { | ||
| 118 | my ($transcript) = @_; | ||
| 119 | |||
| 120 | # Calculate exon and transcript counts. Transcript counts is calculated by the exonic region only | ||
| 121 | my $transcript_exonic_count = 0; | ||
| 122 | foreach my $exon (@{$transcript->exons}) { | ||
| 123 | my $exon_count = $reads_collection->total_copy_number_for_records_contained_in_region($exon->strand, $exon->chromosome, $exon->start, $exon->stop); | ||
| 124 | $exon->extra({count => $exon_count}); | ||
| 125 | $transcript_exonic_count += $exon_count; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Calculate intron counts. | ||
| 129 | my $transcript_intronic_count = 0; | ||
| 130 | foreach my $intron (@{$transcript->introns}) { | ||
| 131 | my $intron_count = $reads_collection->total_copy_number_for_records_contained_in_region($intron->strand, $intron->chromosome, $intron->start, $intron->stop); | ||
| 132 | $intron->extra({count => $intron_count}); | ||
| 133 | $transcript_intronic_count += $intron_count; | ||
| 134 | } | ||
| 135 | |||
| 136 | my $transcript_count = $reads_collection->total_copy_number_for_records_contained_in_region($transcript->strand, $transcript->chromosome, $transcript->start, $transcript->stop); | ||
| 137 | |||
| 138 | $transcript->extra({ | ||
| 139 | count => $transcript_count, | ||
| 140 | exonic_count => $transcript_exonic_count, | ||
| 141 | intronic_count => $transcript_intronic_count | ||
| 142 | }); | ||
| 143 | |||
| 144 | if ($self->verbose and ($counter % 5000 == 0)){ | ||
| 145 | warn "Parsed $counter transcripts\n"; | ||
| 146 | } | ||
| 147 | $counter++; | ||
| 148 | |||
| 149 | }); | ||
| 150 | |||
| 151 | warn "Calculating gene counts\n" if $self->verbose; | ||
| 152 | $gene_collection->foreach_record_do( sub { | ||
| 153 | my ($gene) = @_; | ||
| 154 | |||
| 155 | my $gene_exonic_count = 0; | ||
| 156 | foreach my $exonic_region ($gene->all_exonic_regions) { | ||
| 157 | $gene_exonic_count += $reads_collection->total_copy_number_for_records_contained_in_region($exonic_region->strand, $exonic_region->chromosome, $exonic_region->start, $exonic_region->stop); | ||
| 158 | } | ||
| 159 | my $gene_count = $reads_collection->total_copy_number_for_records_contained_in_region($gene->strand, $gene->chromosome, $gene->start, $gene->stop); | ||
| 160 | |||
| 161 | $gene->extra({ | ||
| 162 | count => $gene_count, | ||
| 163 | exonic_count => $gene_exonic_count | ||
| 164 | }); | ||
| 165 | }); | ||
| 166 | |||
| 167 | warn "Creating output path\n" if $self->verbose; | ||
| 168 | $self->make_path_for_output_prefix(); | ||
| 169 | |||
| 170 | warn "Printing gene counts\n" if $self->verbose; | ||
| 171 | open(my $OUT1, '>', $self->o_prefix.'counts.gene.tab'); | ||
| 172 | say $OUT1 join("\t", 'gene_name', 'gene_location', 'gene_length', 'gene_count', 'gene_count_per_nt', 'gene_exonic_count', 'gene_exonic_length', 'gene_exonic_count_per_nt'); | ||
| 173 | $gene_collection->foreach_record_do( sub { | ||
| 174 | my ($gene) = @_; | ||
| 175 | |||
| 176 | say $OUT1 join("\t", $gene->name, $gene->location, $gene->length, $gene->extra->{count}, $gene->extra->{count} / $gene->length, $gene->extra->{exonic_count}, $gene->exonic_length, $gene->extra->{exonic_count} / $gene->exonic_length ); | ||
| 177 | }); | ||
| 178 | close $OUT1; | ||
| 179 | |||
| 180 | warn "Printing transcript counts\n" if $self->verbose; | ||
| 181 | open(my $OUT2, '>', $self->o_prefix.'counts.transcript.tab'); | ||
| 182 | say $OUT2 join("\t", 'transcript_id', 'transcript_location', 'transcript_length', 'gene_name', 'transcript_count', 'transcript_count_per_nt', 'transcript_exonic_count', 'transcript_exonic_length', 'transcript_exonic_count_per_nt', 'transcript_intronic_count', 'transcript_intronic_length', 'transcript_intronic_count_per_nt'); | ||
| 183 | $transcript_collection->foreach_record_do( sub { | ||
| 184 | my ($transcript) = @_; | ||
| 185 | |||
| 186 | my $intronic_count_per_nt = $transcript->intronic_length > 0 ? $transcript->extra->{intronic_count} / $transcript->intronic_length : 'NA'; | ||
| 187 | |||
| 188 | say $OUT2 join("\t", $transcript->id, $transcript->location, $transcript->length, $transcript->gene->name, $transcript->extra->{count}, $transcript->extra->{count} / $transcript->length, $transcript->extra->{exonic_count}, $transcript->exonic_length, $transcript->extra->{exonic_count} / $transcript->exonic_length, $transcript->extra->{intronic_count}, $transcript->intronic_length, $intronic_count_per_nt); | ||
| 189 | }); | ||
| 190 | close $OUT2; | ||
| 191 | |||
| 192 | warn "Printing exon counts\n" if $self->verbose; | ||
| 193 | open(my $OUT3, '>', $self->o_prefix.'counts.exon.tab'); | ||
| 194 | say $OUT3 join("\t", 'transcript_id', 'exon_location', 'exon_length', 'gene_name', 'exon_count', 'exon_count_per_nt'); | ||
| 195 | $transcript_collection->foreach_record_do( sub { | ||
| 196 | my ($transcript) = @_; | ||
| 197 | |||
| 198 | foreach my $part (@{$transcript->exons}) { | ||
| 199 | say $OUT3 join("\t", $transcript->id, $part->location, $part->length, $transcript->gene->name, $part->extra->{count}, $part->extra->{count} / $part->length); | ||
| 200 | } | ||
| 201 | }); | ||
| 202 | close $OUT3; | ||
| 203 | |||
| 204 | warn "Printing intron counts\n" if $self->verbose; | ||
| 205 | open(my $OUT4, '>', $self->o_prefix.'counts.intron.tab'); | ||
| 206 | say $OUT4 join("\t", 'transcript_id', 'intron_location', 'intron_length', 'gene_name', 'intron_count', 'intron_count_per_nt'); | ||
| 207 | $transcript_collection->foreach_record_do( sub { | ||
| 208 | my ($transcript) = @_; | ||
| 209 | |||
| 210 | foreach my $part (@{$transcript->introns}) { | ||
| 211 | say $OUT4 join("\t", $transcript->id, $part->location, $part->length, $transcript->gene->name, $part->extra->{count}, $part->extra->{count} / $part->length); | ||
| 212 | } | ||
| 213 | }); | ||
| 214 | close $OUT4; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | 1; |