« CLIPSeqTools/PlotApp/reads_long_gaps_size_distribution.pm
↑ Summary
CLIPSeqTools/PlotApp/size_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PlotApp::scatterplot - Create scatterplot for two tables. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools-plot scatterplot [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Create scatterplot for the given column for two tables. | ||
| 12 | |||
| 13 | =head1 OPTIONS | ||
| 14 | |||
| 15 | Input. | ||
| 16 | --table1 <Str> first input table file. | ||
| 17 | --table2 <Str> second input table file. | ||
| 18 | |||
| 19 | --key_col <Str> name for the column/columns to use as a key. It | ||
| 20 | must be unique for each table row. Use option | ||
| 21 | multiple times to specify multiple columns. | ||
| 22 | --val_col <Str> name of column with values to be plotted. | ||
| 23 | The logarithm of the values is used. | ||
| 24 | |||
| 25 | Output | ||
| 26 | --name1 <Str> name to be used in plot for first table | ||
| 27 | --name2 <Str> name to be used in plot for second table | ||
| 28 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 29 | extension to path. Default: ./ | ||
| 30 | |||
| 31 | -v --verbose print progress lines and extra information. | ||
| 32 | -h -? --usage --help print help message | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | package CLIPSeqTools::PlotApp::scatterplot; | ||
| 37 | $CLIPSeqTools::PlotApp::scatterplot::VERSION = '1.0.0'; | ||
| 38 | |||
| 39 | # Make it an app command | ||
| 40 | use MooseX::App::Command; | ||
| 41 | extends 'CLIPSeqTools::PlotApp'; | ||
| 42 | |||
| 43 | |||
| 44 | ####################################################################### | ||
| 45 | ####################### Load External modules ##################### | ||
| 46 | ####################################################################### | ||
| 47 | use Modern::Perl; | ||
| 48 | use autodie; | ||
| 49 | use namespace::autoclean; | ||
| 50 | use Statistics::R; | ||
| 51 | |||
| 52 | |||
| 53 | ####################################################################### | ||
| 54 | ####################### Command line options ###################### | ||
| 55 | ####################################################################### | ||
| 56 | option 'table1' => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Str', | ||
| 59 | required => 1, | ||
| 60 | documentation => 'first input table file.' | ||
| 61 | ); | ||
| 62 | |||
| 63 | option 'table2' => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Str', | ||
| 66 | required => 1, | ||
| 67 | documentation => 'second input table file.' | ||
| 68 | ); | ||
| 69 | |||
| 70 | option 'key_col' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'ArrayRef[Str]', | ||
| 73 | required => 1, | ||
| 74 | documentation => 'name for the column/columns to use as a key. It must '. | ||
| 75 | 'be unique for each table row. Use option multiple '. | ||
| 76 | 'times to specify multiple columns.', | ||
| 77 | ); | ||
| 78 | |||
| 79 | option 'val_col' => ( | ||
| 80 | is => 'rw', | ||
| 81 | isa => 'Str', | ||
| 82 | required => 1, | ||
| 83 | documentation => 'name of column with values to be normalized.', | ||
| 84 | ); | ||
| 85 | |||
| 86 | option 'name1' => ( | ||
| 87 | is => 'rw', | ||
| 88 | isa => 'Str', | ||
| 89 | default => 'table1', | ||
| 90 | documentation => 'name to be used in plot for first table.' | ||
| 91 | ); | ||
| 92 | |||
| 93 | option 'name2' => ( | ||
| 94 | is => 'rw', | ||
| 95 | isa => 'Str', | ||
| 96 | default => 'table2', | ||
| 97 | documentation => 'name to be used in plot for second table.' | ||
| 98 | ); | ||
| 99 | |||
| 100 | |||
| 101 | ####################################################################### | ||
| 102 | ########################## Consume Roles ########################## | ||
| 103 | ####################################################################### | ||
| 104 | with | ||
| 105 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 106 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 107 | -excludes => 'validate_args', | ||
| 108 | }; | ||
| 109 | |||
| 110 | |||
| 111 | ####################################################################### | ||
| 112 | ######################## Interface Methods ######################## | ||
| 113 | ####################################################################### | ||
| 114 | sub validate_args { | ||
| 115 | my ($self) = @_; | ||
| 116 | |||
| 117 | $self->_validate_args_for_output_prefix; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub run { | ||
| 121 | my ($self) = @_; | ||
| 122 | |||
| 123 | warn "Validating arguments\n" if $self->verbose; | ||
| 124 | $self->validate_args(); | ||
| 125 | |||
| 126 | warn "Creating output path\n" if $self->verbose; | ||
| 127 | $self->make_path_for_output_prefix(); | ||
| 128 | |||
| 129 | warn "Creating plots with R\n" if $self->verbose; | ||
| 130 | $self->run_R; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub run_R { | ||
| 134 | my ($self) = @_; | ||
| 135 | |||
| 136 | my $figfile = $self->o_prefix . 'scatterplot.pdf'; | ||
| 137 | |||
| 138 | # Start R | ||
| 139 | my $R = Statistics::R->new(); | ||
| 140 | |||
| 141 | # Pass arguments to R | ||
| 142 | $R->set('ifile1', $self->table1); | ||
| 143 | $R->set('ifile2', $self->table2); | ||
| 144 | $R->set('figfile', $figfile); | ||
| 145 | |||
| 146 | # Read table with data | ||
| 147 | $R->run(q{idata1 = read.delim(ifile1)}); | ||
| 148 | $R->run(q{idata2 = read.delim(ifile2)}); | ||
| 149 | |||
| 150 | # Merge tables | ||
| 151 | $R->run(q{mdata = merge(idata1, idata2, by=c("} . | ||
| 152 | join('","', @{$self->key_col}) . q{"))}); | ||
| 153 | |||
| 154 | # Change symbol size depending on the number of records | ||
| 155 | $R->run(qq{cex_value = 1}); | ||
| 156 | $R->run(qq{if (dim(mdata)[1] > 50) {cex_value=.8}}); | ||
| 157 | $R->run(qq{if (dim(mdata)[1] > 500) {cex_value=.6}}); | ||
| 158 | $R->run(qq{if (dim(mdata)[1] > 2000) {cex_value=.2}}); | ||
| 159 | $R->run(qq{if (dim(mdata)[1] > 10000) {cex_value=.15}}); | ||
| 160 | |||
| 161 | # Do plots | ||
| 162 | $R->run(q{pdf(figfile, width=7)}); | ||
| 163 | $R->run(q{par(mfrow = c(1, 1), cex.lab=1.2, cex.axis=1.2, cex.main=1.2, | ||
| 164 | lwd=1.2)}); | ||
| 165 | |||
| 166 | my $val_col_x = $self->val_col . '.x'; | ||
| 167 | my $val_col_y = $self->val_col . '.y'; | ||
| 168 | my $name1 = $self->name1; | ||
| 169 | my $name2 = $self->name2; | ||
| 170 | $R->run(qq{plot(mdata\$$val_col_x, mdata\$$val_col_y, log="xy", pch=19, | ||
| 171 | cex=cex_value, xlab = "$name1", ylab="$name2")}); | ||
| 172 | $R->run(q{graphics.off()}); | ||
| 173 | |||
| 174 | # Close R | ||
| 175 | $R->stop(); | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; | ||
| 179 |