« CLIPSeqTools/PreprocessApp/cleanup_alignment.pm
↑ Summary
CLIPSeqTools/PreprocessApp/collapse_fastq.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::genome_coverage - Measure percent of genome covered by reads. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools genome_coverage [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Measure the percent of genome that is covered by the reads of a library. | ||
| 12 | |||
| 13 | =head1 OPTIONS | ||
| 14 | |||
| 15 | Input options for library. | ||
| 16 | --driver <Str> driver for database connection (eg. mysql, | ||
| 17 | SQLite). | ||
| 18 | --database <Str> database name or path to database file for file | ||
| 19 | based databases (eg. SQLite). | ||
| 20 | --table <Str> database table. | ||
| 21 | --host <Str> hostname for database connection. | ||
| 22 | --user <Str> username for database connection. | ||
| 23 | --password <Str> password for database connection. | ||
| 24 | --records_class <Str> type of records stored in database. | ||
| 25 | --filter <Filter> filter library. May be used multiple times. | ||
| 26 | Syntax: column_name="pattern" | ||
| 27 | e.g. keep reads with deletions AND not repeat | ||
| 28 | masked AND longer than 31 | ||
| 29 | --filter deletion="def" | ||
| 30 | --filter rmsk="undef" . | ||
| 31 | --filter query_length=">31". | ||
| 32 | Operators: >, >=, <, <=, =, !=, def, undef | ||
| 33 | |||
| 34 | Other input. | ||
| 35 | --rname_sizes <Str> file with sizes for reference alignment | ||
| 36 | sequences (rnames). Must be tab delimited | ||
| 37 | (chromosome\tsize) with one line per rname. | ||
| 38 | |||
| 39 | Output | ||
| 40 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 41 | extension to path. [Default: ./] | ||
| 42 | |||
| 43 | Other options. | ||
| 44 | -v --verbose print progress lines and extra information. | ||
| 45 | -h -? --usage --help print help message | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | |||
| 50 | package CLIPSeqTools::App::genome_coverage; | ||
| 51 | $CLIPSeqTools::App::genome_coverage::VERSION = '1.0.0'; | ||
| 52 | |||
| 53 | # Make it an app command | ||
| 54 | use MooseX::App::Command; | ||
| 55 | extends 'CLIPSeqTools::App'; | ||
| 56 | |||
| 57 | |||
| 58 | ####################################################################### | ||
| 59 | ####################### Load External modules ##################### | ||
| 60 | ####################################################################### | ||
| 61 | use Modern::Perl; | ||
| 62 | use autodie; | ||
| 63 | use namespace::autoclean; | ||
| 64 | use PDL::Lite; | ||
| 65 | |||
| 66 | |||
| 67 | ####################################################################### | ||
| 68 | ####################### Command line options ###################### | ||
| 69 | ####################################################################### | ||
| 70 | option 'rname_sizes' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Str', | ||
| 73 | required => 1, | ||
| 74 | documentation => 'file with sizes for reference alignment sequences (rnames). Must be tab delimited (chromosome\tsize) with one line per rname.', | ||
| 75 | ); | ||
| 76 | |||
| 77 | |||
| 78 | ####################################################################### | ||
| 79 | ########################## Consume Roles ########################## | ||
| 80 | ####################################################################### | ||
| 81 | with | ||
| 82 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 83 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 84 | -excludes => 'validate_args', | ||
| 85 | }, | ||
| 86 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 87 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 88 | -excludes => 'validate_args', | ||
| 89 | }; | ||
| 90 | |||
| 91 | |||
| 92 | ####################################################################### | ||
| 93 | ######################## Interface Methods ######################## | ||
| 94 | ####################################################################### | ||
| 95 | sub validate_args { | ||
| 96 | my ($self) = @_; | ||
| 97 | |||
| 98 | $self->_validate_args_for_library; | ||
| 99 | $self->_validate_args_for_output_prefix; | ||
| 100 | $self->usage_error('File with sizes for reference alignment sequences is required') if !$self->rname_sizes; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub run { | ||
| 104 | my ($self) = @_; | ||
| 105 | |||
| 106 | warn "Starting analysis: genome_coverage\n"; | ||
| 107 | |||
| 108 | warn "Validating arguments\n" if $self->verbose; | ||
| 109 | $self->validate_args(); | ||
| 110 | |||
| 111 | warn "Reading sizes for reference alignment sequences\n" if $self->verbose; | ||
| 112 | my %rname_sizes = $self->read_rname_sizes; | ||
| 113 | |||
| 114 | warn "Creating reads collection\n" if $self->verbose; | ||
| 115 | my $reads_collection = $self->reads_collection; | ||
| 116 | my @rnames = $reads_collection->rnames_for_all_strands; | ||
| 117 | |||
| 118 | warn "Creating output path\n" if $self->verbose; | ||
| 119 | $self->make_path_for_output_prefix(); | ||
| 120 | |||
| 121 | warn "Calculating genome coverage\n" if $self->verbose; | ||
| 122 | my $total_genome_coverage = 0; | ||
| 123 | my $total_genome_length = 0; | ||
| 124 | open(my $OUT, '>', $self->o_prefix.'genome_coverage.tab'); | ||
| 125 | say $OUT join("\t", 'rname', 'covered_area', 'size', 'percent_covered'); | ||
| 126 | foreach my $rname (@rnames) { | ||
| 127 | warn "Working for $rname\n" if $self->verbose; | ||
| 128 | my $pdl = PDL->zeros(PDL::byte(), $rname_sizes{$rname}); | ||
| 129 | |||
| 130 | $reads_collection->foreach_record_on_rname_do($rname, sub { | ||
| 131 | $pdl->slice([$_[0]->start, $_[0]->stop]) .= 1; | ||
| 132 | return 0; | ||
| 133 | }); | ||
| 134 | |||
| 135 | my $covered_area = $pdl->sum(); | ||
| 136 | say $OUT join("\t", $rname, $covered_area, $rname_sizes{$rname}, $covered_area/$rname_sizes{$rname}*100); | ||
| 137 | |||
| 138 | $total_genome_coverage += $covered_area; | ||
| 139 | $total_genome_length += $rname_sizes{$rname}; | ||
| 140 | } | ||
| 141 | say $OUT join("\t", 'Total', $total_genome_coverage, $total_genome_length, $total_genome_coverage/$total_genome_length*100); | ||
| 142 | close $OUT; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub read_rname_sizes { | ||
| 146 | my ($self) = @_; | ||
| 147 | |||
| 148 | my %rname_size; | ||
| 149 | open (my $CHRSIZE, '<', $self->rname_sizes); | ||
| 150 | while (my $line = <$CHRSIZE>) { | ||
| 151 | chomp $line; | ||
| 152 | my ($chr, $size) = split(/\t/, $line); | ||
| 153 | $rname_size{$chr} = $size; | ||
| 154 | } | ||
| 155 | close $CHRSIZE; | ||
| 156 | return %rname_size; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | 1; |