| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PlotApp::reads_long_gaps_size_distribution - Create plots for | ||
| 4 | script reads_long_gaps_size_distribution. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools-plot reads_long_gaps_size_distribution [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Create plots for script reads_long_gaps_size_distribution. | ||
| 13 | |||
| 14 | =head1 OPTIONS | ||
| 15 | |||
| 16 | Input. | ||
| 17 | --file <Str> file with long gaps distribution. | ||
| 18 | |||
| 19 | Output | ||
| 20 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 21 | extension to path. [Default: ./] | ||
| 22 | |||
| 23 | -v --verbose print progress lines and extra information. | ||
| 24 | -h -? --usage --help print help message | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | package CLIPSeqTools::PlotApp::reads_long_gaps_size_distribution; | ||
| 29 | $CLIPSeqTools::PlotApp::reads_long_gaps_size_distribution::VERSION = '1.0.0'; | ||
| 30 | |||
| 31 | # Make it an app command | ||
| 32 | use MooseX::App::Command; | ||
| 33 | extends 'CLIPSeqTools::PlotApp'; | ||
| 34 | |||
| 35 | |||
| 36 | ####################################################################### | ||
| 37 | ####################### Load External modules ##################### | ||
| 38 | ####################################################################### | ||
| 39 | use Modern::Perl; | ||
| 40 | use autodie; | ||
| 41 | use namespace::autoclean; | ||
| 42 | use Statistics::R; | ||
| 43 | |||
| 44 | |||
| 45 | ####################################################################### | ||
| 46 | ####################### Command line options ###################### | ||
| 47 | ####################################################################### | ||
| 48 | option 'file' => ( | ||
| 49 | is => 'rw', | ||
| 50 | isa => 'Str', | ||
| 51 | required => 1, | ||
| 52 | documentation => 'file with long gaps distribution.', | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | ####################################################################### | ||
| 57 | ########################## Consume Roles ########################## | ||
| 58 | ####################################################################### | ||
| 59 | with | ||
| 60 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 61 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 62 | -excludes => 'validate_args', | ||
| 63 | }; | ||
| 64 | |||
| 65 | |||
| 66 | ####################################################################### | ||
| 67 | ######################## Interface Methods ######################## | ||
| 68 | ####################################################################### | ||
| 69 | sub validate_args { | ||
| 70 | my ($self) = @_; | ||
| 71 | |||
| 72 | $self->_validate_args_for_output_prefix; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub run { | ||
| 76 | my ($self) = @_; | ||
| 77 | |||
| 78 | warn "Validating arguments\n" if $self->verbose; | ||
| 79 | $self->validate_args(); | ||
| 80 | |||
| 81 | warn "Creating output path\n" if $self->verbose; | ||
| 82 | $self->make_path_for_output_prefix(); | ||
| 83 | |||
| 84 | warn "Creating plots with R\n" if $self->verbose; | ||
| 85 | $self->run_R; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub run_R { | ||
| 89 | my ($self) = @_; | ||
| 90 | |||
| 91 | my $figfile = $self->o_prefix . 'reads_long_gaps_size_distribution.pdf'; | ||
| 92 | # Start R | ||
| 93 | my $R = Statistics::R->new(); | ||
| 94 | |||
| 95 | # Pass arguments to R | ||
| 96 | $R->set('ifile', $self->file); | ||
| 97 | $R->set('figfile', $figfile); | ||
| 98 | |||
| 99 | # Disable scientific notation | ||
| 100 | $R->run(q{options(scipen=999)}); | ||
| 101 | |||
| 102 | # Read table with data | ||
| 103 | $R->run(q{idata = read.delim(ifile)}); | ||
| 104 | |||
| 105 | # Create groups of scores | ||
| 106 | $R->run(q{mybreaks = c(seq(0,500,100), seq(1000,5000,2000), | ||
| 107 | seq(10000,50000,20000), Inf)}); | ||
| 108 | $R->run(q{idata$size_group = cut(idata$gap_size, breaks=mybreaks, | ||
| 109 | dig.lab=4)}); | ||
| 110 | |||
| 111 | # Aggregate (sum) counts and percents for size groups | ||
| 112 | $R->run(q{aggregate_counts = tapply(idata$count, idata$size_group , sum)}); | ||
| 113 | $R->run(q{aggregate_percents = tapply(idata$percent, idata$size_group , sum)}); | ||
| 114 | |||
| 115 | # Do plots | ||
| 116 | $R->run(q{pdf(figfile, width=21)}); | ||
| 117 | $R->run(q{par(mfrow = c(1, 3), cex.lab=1.6, cex.axis=1.2, cex.main=1.6, | ||
| 118 | lwd=1.5, oma=c(0, 0, 2, 0), mar=c(9.1, 5.1, 4.1, 2.1))}); | ||
| 119 | |||
| 120 | $R->run(q{plot(aggregate_counts, type="b", xaxt="n", pch=19, xlab = NA, | ||
| 121 | ylab="Number of reads", main="Number of reads with given gap size")}); | ||
| 122 | $R->run(q{axis(1, at=1:length(aggregate_counts), | ||
| 123 | labels=names(aggregate_counts), las=2)}); | ||
| 124 | $R->run(q{mtext(side = 1, "Gap size", line = 7)}); | ||
| 125 | |||
| 126 | $R->run(q{plot(aggregate_percents, type="b", xaxt="n", pch=19, xlab = NA, | ||
| 127 | ylab="Percent of reads (%)", main="Percent of reads with given gap size")}); | ||
| 128 | $R->run(q{axis(1, at=1:length(aggregate_percents), | ||
| 129 | labels=names(aggregate_percents), las=2)}); | ||
| 130 | $R->run(q{mtext(side = 1, "Gap size", line = 7)}); | ||
| 131 | |||
| 132 | $R->run(q{plot((aggregate_counts / sum(idata$count)) * 100, type="b", | ||
| 133 | xaxt="n", pch=19, xlab = NA, ylab="Percent of gaps (%)", main="Percent | ||
| 134 | of gaps with given size")}); | ||
| 135 | $R->run(q{axis(1, at=1:length(aggregate_counts), | ||
| 136 | labels=names(aggregate_counts), las=2)}); | ||
| 137 | $R->run(q{mtext(side = 1, "Gap size", line = 7)}); | ||
| 138 | |||
| 139 | $R->run(q{graphics.off()}); | ||
| 140 | |||
| 141 | # Close R | ||
| 142 | $R->stop(); | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | 1; |