« CLIPSeqTools/PlotApp/libraries_relative_read_density.pm
↑ Summary
CLIPSeqTools/PlotApp/reads_long_gaps_size_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PlotApp::nucleotide_composition - Create plots for script | ||
| 4 | nucleotide_composition. | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | clipseqtools-plot nucleotide_composition [options/parameters] | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Create plots for script nucleotide_composition. | ||
| 13 | |||
| 14 | =head1 OPTIONS | ||
| 15 | |||
| 16 | Input. | ||
| 17 | --file <Str> input file. | ||
| 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::nucleotide_composition; | ||
| 29 | $CLIPSeqTools::PlotApp::nucleotide_composition::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 . 'nucleotide_composition.pdf'; | ||
| 92 | |||
| 93 | # Start R | ||
| 94 | my $R = Statistics::R->new(); | ||
| 95 | |||
| 96 | # Pass arguments to R | ||
| 97 | $R->set('ifile', $self->file); | ||
| 98 | $R->set('figfile', $figfile); | ||
| 99 | |||
| 100 | # Disable scientific notation | ||
| 101 | $R->run(q{options(scipen=999)}); | ||
| 102 | |||
| 103 | # Read table with data | ||
| 104 | $R->run(q{idata = read.delim(ifile)}); | ||
| 105 | |||
| 106 | # Measure percentages | ||
| 107 | $R->run(q{idata$A_percent = idata$A_count / idata$total_count * 100}); | ||
| 108 | $R->run(q{idata$C_percent = idata$C_count / idata$total_count * 100}); | ||
| 109 | $R->run(q{idata$G_percent = idata$G_count / idata$total_count * 100}); | ||
| 110 | $R->run(q{idata$T_percent = idata$T_count / idata$total_count * 100}); | ||
| 111 | $R->run(q{idata$N_percent = idata$N_count / idata$total_count * 100}); | ||
| 112 | |||
| 113 | # Do plots | ||
| 114 | $R->run(q{pdf(figfile, width=7)}); | ||
| 115 | $R->run(q{par(mfrow = c(1, 1), cex.lab=1.2, cex.axis=1.2, cex.main=1.2, | ||
| 116 | lwd=1.2, oma=c(0, 0, 0, 0), mar=c(5.1, 5.1, 4.1, 2.1))}); | ||
| 117 | $R->run(q{plot(idata$position, idata$A_percent, type="o", | ||
| 118 | col="darkred", ylim=c(0,100), xlab="Position in read", | ||
| 119 | ylab="Percent of nucleotides (%)", main="Nucleotide composition per | ||
| 120 | read position", cex=0.5)}); | ||
| 121 | $R->run(q{lines(idata$position, idata$C_percent, type="o", | ||
| 122 | col="orange", cex=0.5)}); | ||
| 123 | $R->run(q{lines(idata$position, idata$G_percent, type="o", | ||
| 124 | col="lightblue", cex=0.5)}); | ||
| 125 | $R->run(q{lines(idata$position, idata$T_percent, type="o", | ||
| 126 | col="darkblue", cex=0.5)}); | ||
| 127 | $R->run(q{lines(idata$position, idata$N_percent, type="o", col="black", | ||
| 128 | cex=0.5)}); | ||
| 129 | $R->run(q{legend( "topright" , c("A", "C", "G", "T", "N"), | ||
| 130 | fill=c("darkred", "orange", "lightblue", "darkblue", "black"))}); | ||
| 131 | $R->run(q{graphics.off()}); | ||
| 132 | |||
| 133 | # Close R | ||
| 134 | $R->stop(); | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | 1; |