« CLIPSeqTools/App/conservation_distribution.pm
↑ Summary
CLIPSeqTools/PreprocessApp/star_alignment.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::CompareApp::compare_counts - Compare tables with counts. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools compare_counts [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Do Upper Quartile normalization on specified value column of one or more | ||
| 12 | tables. For each table, all entries are sorted by descending value (specified | ||
| 13 | by val_col). All values are divided by the value of the 25%th entry. The | ||
| 14 | 25%th entry is specified by the table with the fewer entries after the entries | ||
| 15 | with value lower than val_thres in all tables are excluded. | ||
| 16 | |||
| 17 | =head1 OPTIONS | ||
| 18 | |||
| 19 | Input options. | ||
| 20 | --table <Str> input table file/files. Use option multiple | ||
| 21 | times to specify multiple table files. | ||
| 22 | --t_name <Str> table name. Use option multiple times to give names | ||
| 23 | to all the tables. The number of table names must | ||
| 24 | match the number of input table files. If not set, | ||
| 25 | numbers are used instead. These names will be used | ||
| 26 | in the output files. | ||
| 27 | --key_col <Str> name for the column/columns to use as a key. It | ||
| 28 | must be unique for each table row. Use option | ||
| 29 | multiple times to specify multiple columns. | ||
| 30 | --val_col <Str> name of column with values to be normalized. | ||
| 31 | |||
| 32 | Output. | ||
| 33 | --o_table <Str> output table file/files. Use option multiple | ||
| 34 | times to give multiple files. Must be given as | ||
| 35 | many times as the table option. | ||
| 36 | |||
| 37 | Other options. | ||
| 38 | --val_thres <Num> rows with value lower or equal than val_thres | ||
| 39 | are not used for normalization. Default: 0 | ||
| 40 | -v --verbose print progress lines and extra information. | ||
| 41 | -h -? --usage --help print help message | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | package CLIPSeqTools::CompareApp::compare_counts; | ||
| 46 | $CLIPSeqTools::CompareApp::compare_counts::VERSION = '1.0.0'; | ||
| 47 | |||
| 48 | # Make it an app command | ||
| 49 | use MooseX::App::Command; | ||
| 50 | extends 'CLIPSeqTools::CompareApp'; | ||
| 51 | |||
| 52 | |||
| 53 | ####################################################################### | ||
| 54 | ####################### Load External modules ##################### | ||
| 55 | ####################################################################### | ||
| 56 | use Modern::Perl; | ||
| 57 | use autodie; | ||
| 58 | use namespace::autoclean; | ||
| 59 | use File::Spec; | ||
| 60 | use File::Path qw(make_path); | ||
| 61 | use Data::Table; | ||
| 62 | use List::Util qw(min); | ||
| 63 | |||
| 64 | |||
| 65 | ####################################################################### | ||
| 66 | ####################### Command line options ###################### | ||
| 67 | ####################################################################### | ||
| 68 | option 'table' => ( | ||
| 69 | is => 'rw', | ||
| 70 | isa => 'ArrayRef[Str]', | ||
| 71 | required => 1, | ||
| 72 | documentation => 'input table files. Use option two or more times to '. | ||
| 73 | 'give multiple files.', | ||
| 74 | ); | ||
| 75 | |||
| 76 | option 'key_col' => ( | ||
| 77 | is => 'rw', | ||
| 78 | isa => 'ArrayRef[Str]', | ||
| 79 | required => 1, | ||
| 80 | documentation => 'name for the column/columns to use as a key. It must '. | ||
| 81 | 'be unique for each table row. Use option multiple '. | ||
| 82 | 'times to specify multiple columns.', | ||
| 83 | ); | ||
| 84 | |||
| 85 | option 'val_col' => ( | ||
| 86 | is => 'rw', | ||
| 87 | isa => 'Str', | ||
| 88 | required => 1, | ||
| 89 | documentation => 'name of column with values to be normalized.', | ||
| 90 | ); | ||
| 91 | |||
| 92 | option 'val_thres' => ( | ||
| 93 | is => 'rw', | ||
| 94 | isa => 'Num', | ||
| 95 | default => 0, | ||
| 96 | documentation => 'rows with value lower or equal than val_thres are not '. | ||
| 97 | 'used for normalization.', | ||
| 98 | ); | ||
| 99 | |||
| 100 | option 't_name' => ( | ||
| 101 | is => 'rw', | ||
| 102 | isa => 'ArrayRef[Str]', | ||
| 103 | documentation => 'table name. Use option multiple times to give names '. | ||
| 104 | 'to all the tables. The number of table names must '. | ||
| 105 | 'match the number of input table files. If not set, '. | ||
| 106 | 'numbers are used instead. These names will be used '. | ||
| 107 | 'in the output files.', | ||
| 108 | ); | ||
| 109 | |||
| 110 | ####################################################################### | ||
| 111 | ########################## Consume Roles ########################## | ||
| 112 | ####################################################################### | ||
| 113 | with | ||
| 114 | "CLIPSeqTools::Role::Option::Plot" => { | ||
| 115 | -alias => { validate_args => '_validate_args_for_plot' }, | ||
| 116 | -excludes => 'validate_args', | ||
| 117 | }, | ||
| 118 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 119 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 120 | -excludes => 'validate_args', | ||
| 121 | }; | ||
| 122 | |||
| 123 | ####################################################################### | ||
| 124 | ######################## Interface Methods ######################## | ||
| 125 | ####################################################################### | ||
| 126 | sub validate_args { | ||
| 127 | my ($self) = @_; | ||
| 128 | |||
| 129 | $self->_validate_args_for_plot; | ||
| 130 | $self->_validate_args_for_output_prefix; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub run { | ||
| 134 | my ($self) = @_; | ||
| 135 | |||
| 136 | warn "Starting analysis: compare_counts\n"; | ||
| 137 | |||
| 138 | warn "Validating arguments\n" if $self->verbose; | ||
| 139 | $self->validate_args(); | ||
| 140 | |||
| 141 | warn "Reading input files\n" if $self->verbose; | ||
| 142 | my @tables = map {Data::Table::fromFile($_)} @{$self->table}; | ||
| 143 | die "Error: Input table sizes differ. Aborting.\n" if not all_tables_of_equal_size(@tables); | ||
| 144 | $self->t_name([(1..@tables)]) if !defined $self->t_name; | ||
| 145 | |||
| 146 | warn "Searching for 25th percentile\n" if $self->verbose; | ||
| 147 | my $uq_idx = _quantile_idx_from_table_with_fewer( | ||
| 148 | $self->val_col, $self->val_thres, $self->key_col, @tables); | ||
| 149 | |||
| 150 | warn "Normalizing the values\n" if $self->verbose; | ||
| 151 | #Normalized column name is value column + "_uq" suffix | ||
| 152 | _build_normalized_column_in_tables($self->val_col, $uq_idx, @tables); | ||
| 153 | |||
| 154 | warn "Writing output files\n" if $self->verbose; | ||
| 155 | my @output_files = map { | ||
| 156 | $self->o_prefix . $_ . '.counts.uq.tab'} @{$self->t_name}; | ||
| 157 | for (my $i=0; $i<@output_files; $i++) { | ||
| 158 | my (undef, $dir, undef) = File::Spec->splitpath($output_files[$i]); | ||
| 159 | make_path($dir); | ||
| 160 | open (my $OUT, '>', $output_files[$i]); | ||
| 161 | print $OUT $tables[$i]->tsv; | ||
| 162 | close $OUT; | ||
| 163 | } | ||
| 164 | |||
| 165 | if ($self->plot) { | ||
| 166 | warn "Creating plots\n" if $self->verbose; | ||
| 167 | for (my $i = 0; $i < @output_files; $i++) { | ||
| 168 | for (my $j = $i+1; $j < @output_files; $j++) { | ||
| 169 | CLIPSeqTools::PlotApp->initialize_command_class( | ||
| 170 | 'CLIPSeqTools::PlotApp::scatterplot', | ||
| 171 | table1 => $output_files[$i], | ||
| 172 | table2 => $output_files[$j], | ||
| 173 | key_col => $self->key_col, | ||
| 174 | val_col => $self->val_col . '_uq', | ||
| 175 | name1 => $self->t_name->[$i], | ||
| 176 | name2 => $self->t_name->[$j], | ||
| 177 | verbose => $self->verbose, | ||
| 178 | o_prefix => $self->o_prefix . | ||
| 179 | $self->t_name->[$i].'_vs_'.$self->t_name->[$j]. | ||
| 180 | '.counts.uq.', | ||
| 181 | )->run(); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | ####################################################################### | ||
| 189 | ############################ Functions ############################ | ||
| 190 | ####################################################################### | ||
| 191 | sub _build_normalized_column_in_tables { | ||
| 192 | my ($value_col, $uq_idx, @tables) = @_; | ||
| 193 | |||
| 194 | # Sort the tables by descending value | ||
| 195 | map{$_->sort($value_col, Data::Table::NUMBER, Data::Table::DESC)} @tables; | ||
| 196 | foreach my $table (@tables) { | ||
| 197 | my $uq = $table->elm($uq_idx, $value_col); | ||
| 198 | my @normalized_values = map {$_ / $uq} $table->col($value_col); | ||
| 199 | $table->addCol(\@normalized_values, $value_col."_uq"); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _quantile_idx_from_table_with_fewer { | ||
| 204 | my ($value_col, $val_thres, $key_cols, @tables) = @_; | ||
| 205 | |||
| 206 | # Sort tables according to keys | ||
| 207 | my @rule = map{$_, Data::Table::STRING, Data::Table::ASC} @{$key_cols}; | ||
| 208 | map {$_->sort(@rule)} @tables; | ||
| 209 | |||
| 210 | my @rec_counts; | ||
| 211 | foreach my $table (@tables) { | ||
| 212 | my $records_count = 0; | ||
| 213 | my $row_count = $table->nofRow; | ||
| 214 | for (my $i=0; $i<$row_count; $i++) { | ||
| 215 | my $value = $table->elm($i, $value_col); | ||
| 216 | if ($value > $val_thres) { | ||
| 217 | $records_count++; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | push @rec_counts, $records_count; | ||
| 221 | } | ||
| 222 | |||
| 223 | my $min_count = min(@rec_counts); | ||
| 224 | my $quantile_idx = int($min_count * 0.25); | ||
| 225 | |||
| 226 | return $quantile_idx; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub all_tables_of_equal_size { | ||
| 230 | my @tables = @_; | ||
| 231 | |||
| 232 | my $row_count = $tables[0]->nofRow; | ||
| 233 | my $col_count = $tables[0]->nofCol; | ||
| 234 | |||
| 235 | foreach my $table (@tables) { | ||
| 236 | if ($table->nofRow != $row_count or $table->nofCol != $col_count) { | ||
| 237 | return 0; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | return 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | 1; |