| 1 |
|
| #!/usr/local/bin/perl |
| 2 |
|
| |
| 3 |
|
| # documentation at end of file |
| 4 |
|
| |
| 5 |
|
| use warnings; |
| 6 |
|
| use strict; |
| 7 |
|
| use Pod::Usage; |
| 8 |
|
| use Getopt::Long qw(:config no_ignore_case bundling); |
| 9 |
|
| use IO::Prompt::Tiny qw(prompt); |
| 10 |
|
| use Scalar::Util qw(looks_like_number); |
| 11 |
|
| use List::Util qw(min max sum0); |
| 12 |
|
| use Statistics::Lite qw(median range stddevp mode); |
| 13 |
|
| use Bio::ToolBox::Data; |
| 14 |
|
| use Bio::ToolBox::utility qw(format_with_commas parse_list ask_user_for_index); |
| 15 |
|
| |
| 16 |
|
| our $VERSION = '2.03'; |
| 17 |
|
| |
| 18 |
|
| print "\n A tool for manipulating datasets in data files\n"; |
| 19 |
|
| |
| 20 |
|
| ### Quick help |
| 21 |
|
| unless (@ARGV) { # when no command line options are present |
| 22 |
|
| # print SYNOPSIS |
| 23 |
|
| pod2usage( |
| 24 |
|
| { |
| 25 |
|
| '-verbose' => 0, |
| 26 |
|
| '-exitval' => 1, |
| 27 |
|
| } |
| 28 |
|
| ); |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
|
| ### Get command line options and initialize values |
| 32 |
|
| my ( |
| 33 |
|
| $infile, $outfile, $function, $opt_index, |
| 34 |
|
| $opt_numerator, $opt_denominator, $opt_target, $opt_placement, |
| 35 |
|
| $opt_zero, $opt_direction, $opt_name, $opt_log, |
| 36 |
|
| $noheader, $gz, $bgz, $help, |
| 37 |
|
| $print_version, |
| 38 |
|
| ); |
| 39 |
|
| |
| 40 |
|
| # Command line options |
| 41 |
|
| GetOptions( |
| 42 |
|
| 'i|in=s' => \$infile, # name of input file |
| 43 |
|
| 'o|out=s' => \$outfile, # name of new output file |
| 44 |
|
| 'f|func=s' => \$function, # name of the function to perform |
| 45 |
|
| 'x|index=s' => \$opt_index, # index number(s) of the dataset to work on |
| 46 |
|
| 'n|exp|num=i' => \$opt_numerator, # index number of numerator dataset |
| 47 |
|
| 'd|con|den=i' => \$opt_denominator, # index number of denominator dataset |
| 48 |
|
| 't|target=s' => \$opt_target, # target |
| 49 |
|
| 'place=s' => \$opt_placement, # placement of transformed dataset |
| 50 |
|
| 'zero!' => \$opt_zero, # include 0 values |
| 51 |
|
| 'dir=s' => \$opt_direction, # sort order |
| 52 |
|
| 'name=s' => \$opt_name, # new dataset name |
| 53 |
|
| 'log!' => \$opt_log, # data values are in log2 space |
| 54 |
|
| 'H|noheader' => \$noheader, # file has no headers |
| 55 |
|
| 'z|gz!' => \$gz, # write gzipped data file |
| 56 |
|
| 'Z|bgz!' => \$bgz, # compress with bgzip |
| 57 |
|
| 'h|help' => \$help, # request help |
| 58 |
|
| 'v|version' => \$print_version, # print the version |
| 59 |
|
| ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; |
| 60 |
|
| |
| 61 |
|
| # Get file name |
| 62 |
|
| unless ($infile) { |
| 63 |
|
| $infile = shift @ARGV; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| ### Print help if requested |
| 67 |
|
| if ($help) { |
| 68 |
|
| |
| 69 |
|
| # print entire POD |
| 70 |
|
| pod2usage( |
| 71 |
|
| { |
| 72 |
|
| '-verbose' => 2, |
| 73 |
|
| '-exitval' => 1, |
| 74 |
|
| } |
| 75 |
|
| ); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| # Print version |
| 79 |
|
| if ($print_version) { |
| 80 |
|
| print " Biotoolbox script manipulate_datasets.pl, version $VERSION\n"; |
| 81 |
|
| eval { |
| 82 |
|
| require Bio::ToolBox; |
| 83 |
|
| my $v = Bio::ToolBox->VERSION; |
| 84 |
|
| print " Biotoolbox package version $v\n"; |
| 85 |
|
| }; |
| 86 |
|
| exit; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| ### Check for required values |
| 90 |
|
| unless ($infile) { |
| 91 |
|
| print STDERR " FATAL: No file name supplied!\n"; |
| 92 |
|
| exit 1; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| ### Load file |
| 96 |
|
| $noheader = defined $noheader ? $noheader : 0; |
| 97 |
|
| my $Data = Bio::ToolBox::Data->new( file => $infile, noheader => $noheader ) |
| 98 |
|
| or die "no data loaded from file '$infile'!"; |
| 99 |
|
| printf " Loaded '$infile' with %s data rows and %s columns\n\n", |
| 100 |
|
| format_with_commas( $Data->last_row ), $Data->number_columns; |
| 101 |
|
| |
| 102 |
|
| ### Initialize more variables |
| 103 |
|
| # parse the requested index string in to an array of indices |
| 104 |
|
| my @opt_indices; |
| 105 |
|
| if ( defined $opt_index ) { |
| 106 |
|
| @opt_indices = parse_list($opt_index) |
| 107 |
|
| or die " requested index '$opt_index' cannot be parsed!"; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| # initialize modification count |
| 111 |
|
| my $modification = 0; |
| 112 |
|
| |
| 113 |
|
| # the function subroutines return a value indicating success (the data |
| 114 |
|
| # table has been modified) or failure (or at least no changes has been |
| 115 |
|
| # written to the data array) |
| 116 |
|
| # the $modification variable stores the sum of these return values and |
| 117 |
|
| # hence indicates whether a new file needs to be written or not |
| 118 |
|
| |
| 119 |
|
| # set menu hashes |
| 120 |
|
| my %letter_to_function = _get_letter_to_function_hash(); |
| 121 |
|
| |
| 122 |
|
| # these are hashes that convert a single letter to the function name |
| 123 |
|
| |
| 124 |
|
| # set the function to subroutine hash |
| 125 |
|
| my %function_to_subroutine = _get_function_to_subroutine_hash(); |
| 126 |
|
| |
| 127 |
|
| # this hash will convert the function name to the actual subroutine that |
| 128 |
|
| # does the work |
| 129 |
|
| |
| 130 |
|
| ### Determine execution mode |
| 131 |
|
| if ($function) { |
| 132 |
|
| |
| 133 |
|
| # Function was requested upon execution |
| 134 |
|
| automatic_execution(); |
| 135 |
|
| write_and_quit_function(); |
| 136 |
|
| } |
| 137 |
|
| else { |
| 138 |
|
| # otherwise interactive execution |
| 139 |
|
| interactive_execution(); |
| 140 |
|
| write_and_quit_function(); |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
|
| ################################################################################ |
| 144 |
|
| ############## Main Program Subroutines ############## |
| 145 |
|
| ################################################################################ |
| 146 |
|
| |
| 147 |
|
| sub print_menu { |
| 148 |
|
| print <<MENU; |
| 149 |
|
| These are the functions available: |
| 150 |
|
| |
| 151 |
|
| Column manipulation |
| 152 |
|
| R (R)eorder columns in a different order |
| 153 |
|
| D (D)elete a column |
| 154 |
|
| n Re(n)ame a column |
| 155 |
|
| w Generate a ne(w) column with a single identical value |
| 156 |
|
| b Num(b)er the rows |
| 157 |
|
| C (C)oncatenate columns |
| 158 |
|
| T spli(T) column into new columns |
| 159 |
|
| O Generate c(O)ordinate string |
| 160 |
|
| Row manipulation |
| 161 |
|
| o S(o)rt all rows by a specific column |
| 162 |
|
| g (g)enomic sort by chromosome, start |
| 163 |
|
| N Delete rows with (N)ull values |
| 164 |
|
| P Delete rows with du(P)licate values, keeping first |
| 165 |
|
| A Delete rows with values (A)bove threshold |
| 166 |
|
| B Delete rows with values (B)elow threshold |
| 167 |
|
| S Delete rows with (S)pecific values |
| 168 |
|
| K (K)eep only rows with specific values |
| 169 |
|
| E Filter rows by feature l(E)ngth |
| 170 |
|
| M Add new unique na(M)e to each row |
| 171 |
|
| Conversions |
| 172 |
|
| U Convert n(U)ll values to a specific value |
| 173 |
|
| G Convert si(G)ned values to an absolute value |
| 174 |
|
| I Set a m(I)nimum value |
| 175 |
|
| X Set a ma(X)imum value |
| 176 |
|
| l (l)og convert the column |
| 177 |
|
| L De-(L)og the column |
| 178 |
|
| f (f)ormat decimal numbers in a column |
| 179 |
|
| p (p)ercentile rank convert a column |
| 180 |
|
| Mathematical manipulation |
| 181 |
|
| a (a)dd a specific value to a column |
| 182 |
|
| u S(u)btract a specific value from a column |
| 183 |
|
| y Multipl(y) a specific value with a column |
| 184 |
|
| v Di(v)ide a column by a specific value |
| 185 |
|
| c (c)ombine columns with math operation |
| 186 |
|
| s Median (s)cale a column |
| 187 |
|
| Z Generate (Z)-score values of a column |
| 188 |
|
| r Generate a (r)atio between two columns |
| 189 |
|
| d Generate a (d)ifference between two columns |
| 190 |
|
| e Median c(e)nter row values |
| 191 |
|
| File |
| 192 |
|
| W Re(W)rite the file |
| 193 |
|
| x E(x)port into a simple tab-delimited text file |
| 194 |
|
| Y Write out a mean summar(Y) profile of the data |
| 195 |
|
| Other |
| 196 |
|
| t Print S(t)atistics on a column |
| 197 |
|
| k Print length statistics of feature intervals |
| 198 |
|
| V (V)iew table contents |
| 199 |
|
| h (h)elp |
| 200 |
|
| q (q)uit, saving changes if necessary |
| 201 |
|
| Q (Q)uit without saving changes |
| 202 |
|
| MENU |
| 203 |
|
| |
| 204 |
|
| # unlisted option: print this (m)enu |
| 205 |
|
| # unused letters: F H i jJ z |
| 206 |
|
| return 0; # return 0, nothing done |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
|
| sub print_online_help { |
| 210 |
|
| |
| 211 |
|
| # display FUNCTIONS from POD for online help |
| 212 |
|
| # upon quitting perldoc it should return to the program |
| 213 |
|
| pod2usage( |
| 214 |
|
| { |
| 215 |
|
| '-verbose' => 99, |
| 216 |
|
| '-sections' => 'FUNCTIONS', |
| 217 |
|
| '-exitval' => 'NOEXIT', |
| 218 |
|
| } |
| 219 |
|
| ); |
| 220 |
|
| return 0; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| sub automatic_execution { |
| 224 |
|
| |
| 225 |
|
| # use global variables defined by command-line arguments to automatically |
| 226 |
|
| # execute the manipulations |
| 227 |
|
| |
| 228 |
|
| # print options as a record for when I keep the output |
| 229 |
|
| print " Executing automatic manipulation with function '$function'\n"; |
| 230 |
|
| my %option_values = ( |
| 231 |
|
| 'index' => $opt_index, |
| 232 |
|
| 'experiment' => $opt_numerator, |
| 233 |
|
| 'control' => $opt_denominator, |
| 234 |
|
| 'target' => $opt_target, |
| 235 |
|
| 'place' => $opt_placement, |
| 236 |
|
| 'include zero' => $opt_zero, |
| 237 |
|
| 'direction' => $opt_direction, |
| 238 |
|
| 'name' => $opt_name, |
| 239 |
|
| 'log' => $opt_log, |
| 240 |
|
| ); |
| 241 |
|
| foreach my $value ( sort { $a cmp $b } keys %option_values ) { |
| 242 |
|
| if ( defined $option_values{$value} ) { |
| 243 |
|
| print " using '$option_values{$value}' for option $value\n"; |
| 244 |
|
| } |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| # set default placement to new |
| 248 |
|
| # I hate having a shell script stall asking the user for placement |
| 249 |
|
| unless ( defined $opt_placement ) { |
| 250 |
|
| $opt_placement = 'n'; |
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
|
| # execute |
| 254 |
|
| if ( exists $function_to_subroutine{$function} ) { |
| 255 |
|
| |
| 256 |
|
| # the function is recognized as legitimate |
| 257 |
|
| $modification += &{ $function_to_subroutine{$function} }; |
| 258 |
|
| } |
| 259 |
|
| else { |
| 260 |
|
| print STDERR " FATAL: unknown function '$function'!\n"; |
| 261 |
|
| exit 1; |
| 262 |
|
| } |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| sub interactive_execution { |
| 266 |
|
| |
| 267 |
|
| # interact with the user to perform an unlimited number of executions |
| 268 |
|
| |
| 269 |
|
| # Ask for the function |
| 270 |
|
| print " Functions are chosen by symbol. Use [m] for menu or [h] for help\n"; |
| 271 |
|
| my $p = ' Enter the symbol for the function you would like to perform: '; |
| 272 |
|
| my $request = prompt($p); |
| 273 |
|
| while (1) { |
| 274 |
|
| if ( exists $letter_to_function{$request} ) { |
| 275 |
|
| |
| 276 |
|
| # first check that the letter corresponds to a function |
| 277 |
|
| |
| 278 |
|
| # perform the function |
| 279 |
|
| $modification += |
| 280 |
|
| &{ $function_to_subroutine{ $letter_to_function{$request} } }; |
| 281 |
|
| |
| 282 |
|
| # prepare for the next function |
| 283 |
|
| $p = ' Which function is next? [m]enu, [h]elp, just [Q]uit, save & [q]uit: '; |
| 284 |
|
| $request = prompt($p); |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
|
| else { |
| 288 |
|
| # unrecognized command |
| 289 |
|
| my $p2 = |
| 290 |
|
| ' unrecognized command. [m]enu, [h]elp, just [Q]uit, save & [q]uit: '; |
| 291 |
|
| $request = prompt($p2); |
| 292 |
|
| } |
| 293 |
|
| } |
| 294 |
|
| return; |
| 295 |
|
| } |
| 296 |
|
| |
| 297 |
|
| ################################################################################ |
| 298 |
|
| ############## Function Subroutines ############## |
| 299 |
|
| ################################################################################ |
| 300 |
|
| |
| 301 |
|
| sub write_and_quit_function { |
| 302 |
|
| |
| 303 |
|
| # write out file as necessary and quit |
| 304 |
|
| |
| 305 |
|
| if ( $modification > 0 ) { |
| 306 |
|
| |
| 307 |
|
| # a value greater than 0 indicates that changes to the data array have been |
| 308 |
|
| # made and that we need to write an output file |
| 309 |
|
| |
| 310 |
|
| # write the file |
| 311 |
|
| my $write_results = $Data->write_file( |
| 312 |
|
| 'filename' => $outfile, |
| 313 |
|
| 'gz' => $bgz ? 2 : $gz ? 1 : undef, |
| 314 |
|
| ); |
| 315 |
|
| |
| 316 |
|
| # report write results |
| 317 |
|
| print " $modification manipulations performed\n"; |
| 318 |
|
| if ($write_results) { |
| 319 |
|
| print " Wrote datafile $write_results\n"; |
| 320 |
|
| } |
| 321 |
|
| else { |
| 322 |
|
| print " WARNING: Failed to write datafile\n"; |
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
|
| else { |
| 326 |
|
| # no need to write output |
| 327 |
|
| print " No changes written\n"; |
| 328 |
|
| } |
| 329 |
|
| exit; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
|
| sub quit_function { |
| 333 |
|
| |
| 334 |
|
| # do not write file |
| 335 |
|
| print " No changes written\n"; |
| 336 |
|
| exit; |
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
|
| sub print_statistics_function { |
| 340 |
|
| |
| 341 |
|
| # print simple statistics for the dataset |
| 342 |
|
| |
| 343 |
|
| # request dataset(s) |
| 344 |
|
| my @indices = _request_indices( |
| 345 |
|
| " Enter one or more column index numbers to calculate statistics "); |
| 346 |
|
| unless (@indices) { |
| 347 |
|
| print " WARNING: unknown index number(s).\n"; |
| 348 |
|
| return 0; |
| 349 |
|
| } |
| 350 |
|
| |
| 351 |
|
| # get statistics and print |
| 352 |
|
| foreach my $index (@indices) { |
| 353 |
|
| my %statdata = _get_statistics_hash($index); |
| 354 |
|
| unless (%statdata) { |
| 355 |
|
| print " WARNING: unable to get statistics for column index $index!\n"; |
| 356 |
|
| return 0; |
| 357 |
|
| } |
| 358 |
|
| |
| 359 |
|
| # print the metadata and the calculated statistics |
| 360 |
|
| printf " Statistics for column $index, '%s'\n", $Data->name($index); |
| 361 |
|
| my %metadata = $Data->metadata($index); |
| 362 |
|
| foreach my $key ( keys %metadata ) { |
| 363 |
|
| |
| 364 |
|
| # print the metadata |
| 365 |
|
| if ( $key eq 'name' ) { |
| 366 |
|
| next; # skip the name, it's been done |
| 367 |
|
| } |
| 368 |
|
| elsif ( $key eq 'index' ) { |
| 369 |
|
| next; # skip the index, it's been done |
| 370 |
|
| } |
| 371 |
|
| elsif ( $key eq 'AUTO' ) { |
| 372 |
|
| next; # skip the name, it's been done |
| 373 |
|
| } |
| 374 |
|
| else { |
| 375 |
|
| printf " $key => '%s'\n", $metadata{$key}; |
| 376 |
|
| } |
| 377 |
|
| } |
| 378 |
|
| print " count = $statdata{count}\n"; |
| 379 |
|
| print " mean = $statdata{mean}\n",; |
| 380 |
|
| print " median = $statdata{median}\n",; |
| 381 |
|
| print " std dev = $statdata{stddevp}\n",; |
| 382 |
|
| print " min = $statdata{min}\n",; |
| 383 |
|
| print " max = $statdata{max}\n"; |
| 384 |
|
| print " mode = $statdata{mode}\n"; |
| 385 |
|
| print " sum = $statdata{sum}\n"; |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
|
| # we're returning 0 because the data file has not been modified |
| 389 |
|
| return 0; |
| 390 |
|
| } |
| 391 |
|
| |
| 392 |
|
| sub reorder_function { |
| 393 |
|
| |
| 394 |
|
| # this subroutine will re-order the datasets (columns) in the file |
| 395 |
|
| |
| 396 |
|
| # determine the new order for the columns |
| 397 |
|
| my @order; # array for the new order |
| 398 |
|
| my $sub_request = 0; # a flag to indicate a request from another subroutine |
| 399 |
|
| if (@_) { |
| 400 |
|
| |
| 401 |
|
| # the new order may be passed from another subroutine |
| 402 |
|
| @order = @_; |
| 403 |
|
| $sub_request = 1; # set to true |
| 404 |
|
| } |
| 405 |
|
| else { |
| 406 |
|
| # otherwise request from user |
| 407 |
|
| my $line = |
| 408 |
|
| " Enter the indices in the desired order." |
| 409 |
|
| . " Indices may skipped or duplicated.\n" |
| 410 |
|
| . " Enter as comma delimited list, and/or range (start - stop)\n "; |
| 411 |
|
| @order = _request_indices($line); |
| 412 |
|
| } |
| 413 |
|
| unless (@order) { |
| 414 |
|
| print " WARNING: No order! Nothing done!\n"; |
| 415 |
|
| return 0; |
| 416 |
|
| } |
| 417 |
|
| |
| 418 |
|
| # re-order the data columns |
| 419 |
|
| $Data->reorder_column(@order); |
| 420 |
|
| |
| 421 |
|
| # completion |
| 422 |
|
| if ($sub_request) { |
| 423 |
|
| |
| 424 |
|
| # the re-ordering was requested by another subroutine |
| 425 |
|
| # suppress the completion statement |
| 426 |
|
| return 1; |
| 427 |
|
| } |
| 428 |
|
| else { |
| 429 |
|
| # explicit user request, print completion statement |
| 430 |
|
| print " re-ordered data as '" . join( ", ", @order ) . "\n"; |
| 431 |
|
| return 1; |
| 432 |
|
| } |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
|
| sub delete_function { |
| 436 |
|
| |
| 437 |
|
| # this will delete datasets (columns) |
| 438 |
|
| |
| 439 |
|
| # request the datasets to be deleted |
| 440 |
|
| my @deletion_list; |
| 441 |
|
| if (@_) { |
| 442 |
|
| |
| 443 |
|
| # the deletion may be passed on from another subroutine |
| 444 |
|
| @deletion_list = @_; |
| 445 |
|
| } |
| 446 |
|
| else { |
| 447 |
|
| # otherwise request list from user |
| 448 |
|
| @deletion_list = _request_indices( |
| 449 |
|
| " Enter one or more column index numbers to be deleted.\n "); |
| 450 |
|
| } |
| 451 |
|
| unless (@deletion_list) { |
| 452 |
|
| print " WARNING: No list for deletion! Nothing done!\n"; |
| 453 |
|
| return 0; |
| 454 |
|
| } |
| 455 |
|
| |
| 456 |
|
| my $names = join ", ", map { sprintf "'%s'", $Data->name($_) } @deletion_list; |
| 457 |
|
| $Data->delete_column(@deletion_list); |
| 458 |
|
| print " datasets $names deleted\n"; |
| 459 |
|
| |
| 460 |
|
| return 1; |
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
|
| sub rename_function { |
| 464 |
|
| |
| 465 |
|
| # this subroutine will re-name a dataset name |
| 466 |
|
| |
| 467 |
|
| # determine or request dataset indices and newname |
| 468 |
|
| my @indices; |
| 469 |
|
| my @newnames; |
| 470 |
|
| if ( scalar @_ == 2 ) { |
| 471 |
|
| |
| 472 |
|
| # passed from internal subroutine |
| 473 |
|
| $indices[0] = $_[0]; |
| 474 |
|
| $newnames[0] = $_[1]; |
| 475 |
|
| } |
| 476 |
|
| else { |
| 477 |
|
| # request index from user |
| 478 |
|
| @indices = |
| 479 |
|
| _request_indices(" Enter the index numbers of the columns to rename "); |
| 480 |
|
| } |
| 481 |
|
| |
| 482 |
|
| # check if user supplied name |
| 483 |
|
| if ( $function and $opt_name ) { |
| 484 |
|
| |
| 485 |
|
| # new name is specified from the command line during automatic execution |
| 486 |
|
| # use this global value |
| 487 |
|
| # it might be a comma delimited name, so split it |
| 488 |
|
| my @provided = split /,/, $opt_name; |
| 489 |
|
| if ( scalar @provided == scalar @indices ) { |
| 490 |
|
| |
| 491 |
|
| # this is great - same number |
| 492 |
|
| @newnames = @provided; |
| 493 |
|
| } |
| 494 |
|
| elsif ( scalar @provided == 1 and scalar @indices > 1 ) { |
| 495 |
|
| |
| 496 |
|
| # use the same one for each of them!? |
| 497 |
|
| @newnames = map {$opt_name} @indices; |
| 498 |
|
| } |
| 499 |
|
| else { |
| 500 |
|
| print |
| 501 |
|
| " WARNING: unequal number of provided names and indices!? nothing done\n"; |
| 502 |
|
| return 0; |
| 503 |
|
| } |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
|
| # rename each of the requested indices |
| 507 |
|
| my $count = 0; |
| 508 |
|
| foreach my $i (@indices) { |
| 509 |
|
| my $name = shift @newnames || undef; |
| 510 |
|
| my $oldname = $Data->name($i); |
| 511 |
|
| unless ($name) { |
| 512 |
|
| |
| 513 |
|
| # request a new name from the user |
| 514 |
|
| my $p = " ($oldname) Enter a new name: "; |
| 515 |
|
| $name = prompt($p); |
| 516 |
|
| } |
| 517 |
|
| |
| 518 |
|
| # assign new name |
| 519 |
|
| $Data->name( $i, $name ); |
| 520 |
|
| print " $oldname re-named to $name\n"; |
| 521 |
|
| $count++; |
| 522 |
|
| } |
| 523 |
|
| |
| 524 |
|
| # reset the noheader flag if it was originally specified in command line options |
| 525 |
|
| if ($noheader) { |
| 526 |
|
| $Data->headers(1); |
| 527 |
|
| } |
| 528 |
|
| |
| 529 |
|
| return $count; |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
|
| sub concatenate_function { |
| 533 |
|
| |
| 534 |
|
| # this subroutine will concatenate two or more column values into a new column |
| 535 |
|
| |
| 536 |
|
| # request datasets |
| 537 |
|
| my @indices = |
| 538 |
|
| _request_indices(" Enter two or more column index numbers to concatenate "); |
| 539 |
|
| unless (@indices) { |
| 540 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 541 |
|
| return 0; |
| 542 |
|
| } |
| 543 |
|
| unless ( scalar @indices >= 2 ) { |
| 544 |
|
| print |
| 545 |
|
| " WARNING: at least two columns are required to concatenate! nothing done\n"; |
| 546 |
|
| return 0; |
| 547 |
|
| } |
| 548 |
|
| |
| 549 |
|
| # identify joining character |
| 550 |
|
| my $glue; |
| 551 |
|
| if ($opt_target) { |
| 552 |
|
| $glue = $opt_target; |
| 553 |
|
| } |
| 554 |
|
| elsif ($function) { |
| 555 |
|
| |
| 556 |
|
| # automatic execution, don't bother user, default is _ |
| 557 |
|
| print " Joining values with character '_'\n"; |
| 558 |
|
| $glue = '_'; |
| 559 |
|
| } |
| 560 |
|
| else { |
| 561 |
|
| my $p = ' Enter a character to join values, return for nothing: '; |
| 562 |
|
| my $default = q(); |
| 563 |
|
| $glue = prompt( $p, $default ); |
| 564 |
|
| } |
| 565 |
|
| |
| 566 |
|
| # generate new column |
| 567 |
|
| my $new_name; |
| 568 |
|
| if ( $function and $opt_name ) { |
| 569 |
|
| |
| 570 |
|
| # automatic execution and new name was specifically given |
| 571 |
|
| $new_name = $opt_name; |
| 572 |
|
| } |
| 573 |
|
| else { |
| 574 |
|
| $new_name = join( '_', map { $Data->name($_) } @indices ); |
| 575 |
|
| if ( length($new_name) > 30 ) { |
| 576 |
|
| |
| 577 |
|
| # I don't like long names! |
| 578 |
|
| $new_name = 'cat_' . join( ',', @indices ); |
| 579 |
|
| } |
| 580 |
|
| } |
| 581 |
|
| my $new_position = $Data->add_column($new_name); |
| 582 |
|
| |
| 583 |
|
| # concatenate values |
| 584 |
|
| $Data->iterate( |
| 585 |
|
| sub { |
| 586 |
|
| my $row = shift; |
| 587 |
|
| my $cat = join( $glue, map { $row->value($_) } @indices ); |
| 588 |
|
| $row->value( $new_position, $cat ); |
| 589 |
|
| } |
| 590 |
|
| ); |
| 591 |
|
| |
| 592 |
|
| printf " Values from %s concatenated into new column '$new_name'\n", |
| 593 |
|
| join( ", ", map { $Data->name($_) } @indices ); |
| 594 |
|
| return 1; |
| 595 |
|
| } |
| 596 |
|
| |
| 597 |
|
| sub split_function { |
| 598 |
|
| |
| 599 |
|
| # split a column into multiple new columns |
| 600 |
|
| # this is tricky as we must know how many columns to create |
| 601 |
|
| |
| 602 |
|
| # Request dataset |
| 603 |
|
| my $index = _request_index(" Enter the index number of a column to split "); |
| 604 |
|
| if ( $index == -1 ) { |
| 605 |
|
| print " WARNING: unknown index number. nothing done\n"; |
| 606 |
|
| return 0; |
| 607 |
|
| } |
| 608 |
|
| |
| 609 |
|
| # identify joining character |
| 610 |
|
| my $glue; |
| 611 |
|
| if ($opt_target) { |
| 612 |
|
| $glue = $opt_target; |
| 613 |
|
| } |
| 614 |
|
| elsif ($function) { |
| 615 |
|
| |
| 616 |
|
| # automatic execution, don't bother user, default is _ |
| 617 |
|
| print " Splitting on default character '_'\n"; |
| 618 |
|
| $glue = '_'; |
| 619 |
|
| } |
| 620 |
|
| else { |
| 621 |
|
| my $p = ' Enter the character to split the values, return for nothing: '; |
| 622 |
|
| my $default = q(); |
| 623 |
|
| $glue = prompt( $p, $default ); |
| 624 |
|
| chomp $glue; |
| 625 |
|
| } |
| 626 |
|
| |
| 627 |
|
| # add new column |
| 628 |
|
| my $new_position = $Data->add_column( $Data->name($index) . '_array' ); |
| 629 |
|
| |
| 630 |
|
| # split values |
| 631 |
|
| my $number = 1; # maximum number of split values found |
| 632 |
|
| $Data->iterate( |
| 633 |
|
| sub { |
| 634 |
|
| my $row = shift; |
| 635 |
|
| my @values = split( $glue, $row->value($index) ); |
| 636 |
|
| if ( scalar @values > $number ) { |
| 637 |
|
| $number = scalar @values; |
| 638 |
|
| } |
| 639 |
|
| $row->value( $new_position, \@values ); # store array reference for now |
| 640 |
|
| } |
| 641 |
|
| ); |
| 642 |
|
| |
| 643 |
|
| # add more columns as necessary |
| 644 |
|
| my @positions; |
| 645 |
|
| for my $i ( 1 .. $number ) { |
| 646 |
|
| my $p = $Data->add_column( $Data->name($index) . '_' . $i ); |
| 647 |
|
| push @positions, $p; |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| # now store the split values from the array ref into the new columns |
| 651 |
|
| $Data->iterate( |
| 652 |
|
| sub { |
| 653 |
|
| my $row = shift; |
| 654 |
|
| my $values = $row->value($new_position); # an array ref |
| 655 |
|
| my $i = 0; |
| 656 |
|
| foreach my $p (@positions) { |
| 657 |
|
| $row->value( $p, $values->[$i] || '.' ); |
| 658 |
|
| $i++; |
| 659 |
|
| } |
| 660 |
|
| } |
| 661 |
|
| ); |
| 662 |
|
| |
| 663 |
|
| # delete the array reference Column |
| 664 |
|
| $Data->delete_column($new_position); |
| 665 |
|
| |
| 666 |
|
| printf " Split column %s into $number new columns\n", $Data->name($index); |
| 667 |
|
| return 1; |
| 668 |
|
| } |
| 669 |
|
| |
| 670 |
|
| sub coordinate_function { |
| 671 |
|
| |
| 672 |
|
| # this subroutine will generate a coordinate string from coordinate values |
| 673 |
|
| |
| 674 |
|
| # check for coordinates |
| 675 |
|
| unless ( $Data->feature_type eq 'coordinate' ) { |
| 676 |
|
| |
| 677 |
|
| # cannot add coordinate column, do without ? |
| 678 |
|
| print |
| 679 |
|
| " WARNING: cannot generate coordinates, no chromosome or start column found\n"; |
| 680 |
|
| return 0; |
| 681 |
|
| } |
| 682 |
|
| |
| 683 |
|
| # the new index position |
| 684 |
|
| my $new_position = $Data->add_column('Coordinate'); |
| 685 |
|
| |
| 686 |
|
| # generate coordinates |
| 687 |
|
| $Data->iterate( |
| 688 |
|
| sub { |
| 689 |
|
| my $row = shift; |
| 690 |
|
| $row->value( $new_position, $row->coordinate ); |
| 691 |
|
| } |
| 692 |
|
| ); |
| 693 |
|
| |
| 694 |
|
| print " Coordinate string generated as new column $new_position\n"; |
| 695 |
|
| return 1; |
| 696 |
|
| } |
| 697 |
|
| |
| 698 |
|
| sub median_scale_function { |
| 699 |
|
| |
| 700 |
|
| # this subroutine will median scale a dataset |
| 701 |
|
| |
| 702 |
|
| # request datasets |
| 703 |
|
| my @indices; |
| 704 |
|
| if (@_) { |
| 705 |
|
| |
| 706 |
|
| # provided from an internal subroutine |
| 707 |
|
| @indices = @_; |
| 708 |
|
| } |
| 709 |
|
| else { |
| 710 |
|
| # otherwise request from user |
| 711 |
|
| @indices = |
| 712 |
|
| _request_indices(" Enter one or more column index numbers to median scale "); |
| 713 |
|
| } |
| 714 |
|
| unless (@indices) { |
| 715 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 716 |
|
| return 0; |
| 717 |
|
| } |
| 718 |
|
| |
| 719 |
|
| # Where to put new values? |
| 720 |
|
| my $placement = _request_placement(); |
| 721 |
|
| |
| 722 |
|
| # Obtain the target median value |
| 723 |
|
| my $target; |
| 724 |
|
| if ( defined $opt_target ) { |
| 725 |
|
| |
| 726 |
|
| # use the command line specified target |
| 727 |
|
| $target = $opt_target; |
| 728 |
|
| } |
| 729 |
|
| else { |
| 730 |
|
| # request target from user |
| 731 |
|
| my $p = ' Enter the new median target: '; |
| 732 |
|
| $target = prompt($p); |
| 733 |
|
| chomp $target; |
| 734 |
|
| } |
| 735 |
|
| |
| 736 |
|
| # Work through the requested datasets |
| 737 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 738 |
|
| INDEX_LOOP: foreach my $index (@indices) { |
| 739 |
|
| |
| 740 |
|
| # Retrieve values and calculate median |
| 741 |
|
| my @cv = $Data->column_values($index); |
| 742 |
|
| shift @cv; # skip header |
| 743 |
|
| my @values = grep { looks_like_number($_) and $_ != 0 } @cv; |
| 744 |
|
| |
| 745 |
|
| # I had historically always skipped zero values before, so continue to do so? |
| 746 |
|
| unless (@values) { |
| 747 |
|
| printf " WARNING: no numeric values dataset %s, index %d!\n", |
| 748 |
|
| $Data->name($index), $index; |
| 749 |
|
| next INDEX_LOOP; |
| 750 |
|
| } |
| 751 |
|
| my $median = median(@values); |
| 752 |
|
| printf " The median value for dataset %s is %s\n", $Data->name($index), $median; |
| 753 |
|
| |
| 754 |
|
| # Calculate correction value |
| 755 |
|
| my $correction_value = $target / $median; |
| 756 |
|
| |
| 757 |
|
| # Replace values |
| 758 |
|
| my $name = $Data->name($index); |
| 759 |
|
| $index = _prepare_new_destination( $index, '_scaled' ) if $placement =~ /^n/i; |
| 760 |
|
| $Data->iterate( |
| 761 |
|
| sub { |
| 762 |
|
| my $row = shift; |
| 763 |
|
| my $v = $row->value($index); |
| 764 |
|
| next unless looks_like_number($v); |
| 765 |
|
| $v *= $correction_value; |
| 766 |
|
| $row->value( $index, $v ); |
| 767 |
|
| } |
| 768 |
|
| ); |
| 769 |
|
| |
| 770 |
|
| # annotate metadata |
| 771 |
|
| $Data->metadata( $index, 'median_scaled', $target ); |
| 772 |
|
| |
| 773 |
|
| # results |
| 774 |
|
| push @datasets_modified, $name; |
| 775 |
|
| } |
| 776 |
|
| |
| 777 |
|
| # report results |
| 778 |
|
| if (@datasets_modified) { |
| 779 |
|
| printf " %s were median scaled to $target\n", join( ", ", @datasets_modified ); |
| 780 |
|
| } |
| 781 |
|
| return scalar(@datasets_modified); |
| 782 |
|
| } |
| 783 |
|
| |
| 784 |
|
| sub percentile_rank_function { |
| 785 |
|
| |
| 786 |
|
| # this subroutine will convert a dataset into a percentile rank |
| 787 |
|
| |
| 788 |
|
| # request datasets |
| 789 |
|
| my @indices; |
| 790 |
|
| if (@_) { |
| 791 |
|
| |
| 792 |
|
| # provided from an internal subroutine |
| 793 |
|
| @indices = @_; |
| 794 |
|
| } |
| 795 |
|
| else { |
| 796 |
|
| # otherwise request from user |
| 797 |
|
| @indices = _request_indices( |
| 798 |
|
| " Enter one or more column index numbers to convert to percentile rank " |
| 799 |
|
| ); |
| 800 |
|
| } |
| 801 |
|
| unless (@indices) { |
| 802 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 803 |
|
| return 0; |
| 804 |
|
| } |
| 805 |
|
| |
| 806 |
|
| # Where to put new values? |
| 807 |
|
| my $placement = _request_placement(); |
| 808 |
|
| |
| 809 |
|
| # Process each index request |
| 810 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 811 |
|
| foreach my $index (@indices) { |
| 812 |
|
| |
| 813 |
|
| # Calculate percent rank of values |
| 814 |
|
| my @cv = $Data->column_values($index); |
| 815 |
|
| shift @cv; # skip header |
| 816 |
|
| my @values = grep { looks_like_number($_) } @cv; |
| 817 |
|
| unless (@values) { |
| 818 |
|
| printf " WARNING: no numeric values in dataset %d, %s! Skipping\n", |
| 819 |
|
| $index, $Data->name($index); |
| 820 |
|
| next; |
| 821 |
|
| } |
| 822 |
|
| my $total = scalar @values; |
| 823 |
|
| my %percentrank; |
| 824 |
|
| my $n = 1; |
| 825 |
|
| foreach ( sort { $a <=> $b } @values ) { |
| 826 |
|
| |
| 827 |
|
| # sort by increasing hash values, not hash keys |
| 828 |
|
| # percentrank is key value (index) divided by total |
| 829 |
|
| $percentrank{$_} = $n / $total; |
| 830 |
|
| $n++; |
| 831 |
|
| } |
| 832 |
|
| |
| 833 |
|
| # Replace the contents with the calculated percent rank |
| 834 |
|
| my $name = $Data->name($index); |
| 835 |
|
| $index = _prepare_new_destination( $index, '_pr' ) if $placement =~ /^n/i; |
| 836 |
|
| $Data->iterate( |
| 837 |
|
| sub { |
| 838 |
|
| my $row = shift; |
| 839 |
|
| my $v = $row->value($index); |
| 840 |
|
| next unless looks_like_number($v); |
| 841 |
|
| $row->value( $index, $percentrank{$v} ); |
| 842 |
|
| } |
| 843 |
|
| ); |
| 844 |
|
| |
| 845 |
|
| # update metadata |
| 846 |
|
| $Data->metadata( $index, 'converted', 'percent_rank' ); |
| 847 |
|
| |
| 848 |
|
| # done |
| 849 |
|
| push @datasets_modified, $name; |
| 850 |
|
| } |
| 851 |
|
| |
| 852 |
|
| # report results |
| 853 |
|
| if (@datasets_modified) { |
| 854 |
|
| printf " %s were converted to percent rank\n", join( ", ", @datasets_modified ); |
| 855 |
|
| } |
| 856 |
|
| return scalar(@datasets_modified); |
| 857 |
|
| } |
| 858 |
|
| |
| 859 |
|
| sub zscore_function { |
| 860 |
|
| |
| 861 |
|
| # this subroutine will generate a z-score for each value in a dataset |
| 862 |
|
| |
| 863 |
|
| # identify the datasets to convert |
| 864 |
|
| my @indices; |
| 865 |
|
| if (@_) { |
| 866 |
|
| |
| 867 |
|
| # provided from an internal subroutine |
| 868 |
|
| @indices = @_; |
| 869 |
|
| } |
| 870 |
|
| else { |
| 871 |
|
| # otherwise request from user |
| 872 |
|
| @indices = _request_indices( |
| 873 |
|
| " Enter one or more column index numbers to convert to z-scores "); |
| 874 |
|
| } |
| 875 |
|
| unless (@indices) { |
| 876 |
|
| print " WARNING: Unknown columns. Nothing done.\n"; |
| 877 |
|
| return 0; |
| 878 |
|
| } |
| 879 |
|
| |
| 880 |
|
| # Where to put new values? |
| 881 |
|
| my $placement = _request_placement(); |
| 882 |
|
| |
| 883 |
|
| # Process each index request |
| 884 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 885 |
|
| foreach my $index (@indices) { |
| 886 |
|
| |
| 887 |
|
| # generate statistics on the dataset |
| 888 |
|
| my @cv = $Data->column_values($index); |
| 889 |
|
| shift @cv; # skip header |
| 890 |
|
| my @values = grep { looks_like_number($_) } @cv; |
| 891 |
|
| unless (@values) { |
| 892 |
|
| printf " WARNING: no numeric values for index %d, %s! skipping\n", |
| 893 |
|
| $index, $Data->name($index); |
| 894 |
|
| next; |
| 895 |
|
| } |
| 896 |
|
| my $mean = sum0(@values) / scalar(@values); |
| 897 |
|
| my $std = stddevp(@values); |
| 898 |
|
| printf " Column %d is %.6f ± %.6f\n", $index, $mean, $std; |
| 899 |
|
| |
| 900 |
|
| # Replace the current values |
| 901 |
|
| my $name = $Data->name($index); |
| 902 |
|
| $index = _prepare_new_destination( $index, '_Zscore' ) if $placement =~ /^n/i; |
| 903 |
|
| $Data->iterate( |
| 904 |
|
| sub { |
| 905 |
|
| my $row = shift; |
| 906 |
|
| my $v = $row->value($index); |
| 907 |
|
| next unless looks_like_number($v); |
| 908 |
|
| $v = ( $v - $mean ) / $std; |
| 909 |
|
| $row->value( $index, $v ); |
| 910 |
|
| } |
| 911 |
|
| ); |
| 912 |
|
| |
| 913 |
|
| # update metadata |
| 914 |
|
| $Data->metadata( $index, 'converted', 'Z-score' ); |
| 915 |
|
| |
| 916 |
|
| # done |
| 917 |
|
| push @datasets_modified, $name; |
| 918 |
|
| } |
| 919 |
|
| |
| 920 |
|
| # report results |
| 921 |
|
| if (@datasets_modified) { |
| 922 |
|
| printf " %s were converted to Z-scores\n", join( ", ", @datasets_modified ); |
| 923 |
|
| } |
| 924 |
|
| return scalar(@datasets_modified); |
| 925 |
|
| } |
| 926 |
|
| |
| 927 |
|
| sub sort_function { |
| 928 |
|
| |
| 929 |
|
| # This will sort the entire data table by the values in one dataset |
| 930 |
|
| |
| 931 |
|
| # Request dataset |
| 932 |
|
| my @indices; |
| 933 |
|
| if (@_) { |
| 934 |
|
| |
| 935 |
|
| # from another subroutine |
| 936 |
|
| @indices = @_; |
| 937 |
|
| } |
| 938 |
|
| else { |
| 939 |
|
| @indices = _request_indices( |
| 940 |
|
| " Enter column index number (or column ranges for mean) to sort by "); |
| 941 |
|
| } |
| 942 |
|
| unless ( scalar(@indices) ) { |
| 943 |
|
| print " WARNING: no index provided. nothing done\n"; |
| 944 |
|
| return 0; |
| 945 |
|
| } |
| 946 |
|
| |
| 947 |
|
| # Ask the sort direction |
| 948 |
|
| my $direction; |
| 949 |
|
| if ($opt_direction) { |
| 950 |
|
| |
| 951 |
|
| # direction was specified on the command line |
| 952 |
|
| $direction = $opt_direction; |
| 953 |
|
| } |
| 954 |
|
| else { |
| 955 |
|
| # otherwise ask the user for the direction |
| 956 |
|
| my $p = ' Sort by (i)ncreasing or (d)ecreasing order?: '; |
| 957 |
|
| $direction = prompt($p); |
| 958 |
|
| unless ( $direction =~ /^[id]$/i ) { |
| 959 |
|
| print " WARNING: unknown order; nothing done\n"; |
| 960 |
|
| return 0; |
| 961 |
|
| } |
| 962 |
|
| } |
| 963 |
|
| |
| 964 |
|
| # sort |
| 965 |
|
| if ( scalar(@indices) == 1 ) { |
| 966 |
|
| |
| 967 |
|
| # excellent! only one column index to sort by |
| 968 |
|
| $Data->sort_data( $indices[0], $direction ); |
| 969 |
|
| } |
| 970 |
|
| else { |
| 971 |
|
| # need to sort by the mean of provided column indices |
| 972 |
|
| # we will generate a temporary column of the mean |
| 973 |
|
| # first need to set the target of mean which is needed by combine function |
| 974 |
|
| my $original = $opt_target; # keep a backup just in case |
| 975 |
|
| $opt_target = 'mean'; |
| 976 |
|
| combine_function(@indices); |
| 977 |
|
| my $i = $Data->last_column; |
| 978 |
|
| $opt_target = $original; # restore backup just in case |
| 979 |
|
| $Data->sort_data( $i, $direction ); |
| 980 |
|
| $Data->delete_column($i); # delete the temporary column |
| 981 |
|
| } |
| 982 |
|
| |
| 983 |
|
| # remove any pre-existing sorted metadata since no longer valid |
| 984 |
|
| for ( my $i = 0; $i < $Data->number_columns; $i++ ) { |
| 985 |
|
| $Data->delete_metadata( $i, 'sorted' ); |
| 986 |
|
| } |
| 987 |
|
| |
| 988 |
|
| # annotate metadata, but only if there was one index |
| 989 |
|
| if ( scalar(@indices) == 1 ) { |
| 990 |
|
| if ( lc $direction eq 'i' ) { |
| 991 |
|
| $Data->metadata( $indices[0], 'sorted', "increasing" ) |
| 992 |
|
| unless $Data->metadata( $indices[0], 'AUTO' ) |
| 993 |
|
| ; # internal flag to not accept metadata |
| 994 |
|
| } |
| 995 |
|
| else { |
| 996 |
|
| $Data->metadata( $indices[0], 'sorted', "decreasing" ) |
| 997 |
|
| unless $Data->metadata( $indices[0], 'AUTO' ) |
| 998 |
|
| ; # internal flag to not accept metadata |
| 999 |
|
| } |
| 1000 |
|
| } |
| 1001 |
|
| |
| 1002 |
|
| return 1; |
| 1003 |
|
| } |
| 1004 |
|
| |
| 1005 |
|
| sub genomic_sort_function { |
| 1006 |
|
| |
| 1007 |
|
| # This will sort the entire data table by chromosome and start position |
| 1008 |
|
| |
| 1009 |
|
| my $result = $Data->gsort_data; |
| 1010 |
|
| unless ($result) { |
| 1011 |
|
| print " WARNING: Data table not sorted\n"; |
| 1012 |
|
| return 0; |
| 1013 |
|
| } |
| 1014 |
|
| |
| 1015 |
|
| # remove any pre-existing sorted metadata since no longer valid |
| 1016 |
|
| for ( my $i = 0; $i < $Data->number_columns; $i++ ) { |
| 1017 |
|
| $Data->delete_metadata( $i, 'sorted' ); |
| 1018 |
|
| } |
| 1019 |
|
| |
| 1020 |
|
| # annotate metadata |
| 1021 |
|
| my $chr_i = $Data->chromo_column; |
| 1022 |
|
| my $start_i = $Data->start_column; |
| 1023 |
|
| $Data->metadata( $chr_i, 'sorted', 'genomic' ) |
| 1024 |
|
| unless $Data->metadata( $chr_i, 'AUTO' ); |
| 1025 |
|
| $Data->metadata( $start_i, 'sorted', 'genomic' ) |
| 1026 |
|
| unless $Data->metadata( $start_i, 'AUTO' ); |
| 1027 |
|
| |
| 1028 |
|
| print " Data table is sorted by genomic order\n"; |
| 1029 |
|
| return 1; |
| 1030 |
|
| } |
| 1031 |
|
| |
| 1032 |
|
| sub toss_nulls_function { |
| 1033 |
|
| |
| 1034 |
|
| # Toss out datapoints (lines) that have a non-value in the specified dataset |
| 1035 |
|
| |
| 1036 |
|
| # generate the list of datasets to check |
| 1037 |
|
| my @order = _request_indices( |
| 1038 |
|
| " Enter one or more column index numbers to check for non-values\n "); |
| 1039 |
|
| unless (@order) { |
| 1040 |
|
| print " WARNING: No valid columns! Nothing done!\n"; |
| 1041 |
|
| return 0; |
| 1042 |
|
| } |
| 1043 |
|
| |
| 1044 |
|
| # Collection exception rule from commandline |
| 1045 |
|
| my $zero = $opt_zero; |
| 1046 |
|
| |
| 1047 |
|
| # Identify those rows that need to be deleted |
| 1048 |
|
| my @todelete; |
| 1049 |
|
| $Data->iterate( |
| 1050 |
|
| sub { |
| 1051 |
|
| my $row = shift; |
| 1052 |
|
| my $check = 0; |
| 1053 |
|
| foreach my $i (@order) { |
| 1054 |
|
| my $v = $row->value($i); |
| 1055 |
|
| if ( _is_null($v) ) { |
| 1056 |
|
| $check++; |
| 1057 |
|
| } |
| 1058 |
|
| elsif ( $v eq '0' ) { |
| 1059 |
|
| |
| 1060 |
|
| # we have a 0 value, what to do? |
| 1061 |
|
| if ( not defined $zero and $function ) { |
| 1062 |
|
| |
| 1063 |
|
| # running automatically, do not both user |
| 1064 |
|
| $zero = 0; |
| 1065 |
|
| } |
| 1066 |
|
| elsif ( not defined $zero and not defined $function ) { |
| 1067 |
|
| |
| 1068 |
|
| # ask the user if we haven't already |
| 1069 |
|
| my $p = ' Also toss values of 0? y/n, default n : '; |
| 1070 |
|
| my $answer = prompt( $p, 'n' ); |
| 1071 |
|
| if ( lc $answer eq 'y' ) { |
| 1072 |
|
| $zero = 1; |
| 1073 |
|
| } |
| 1074 |
|
| elsif ( lc $answer eq 'n' ) { |
| 1075 |
|
| $zero = 0; |
| 1076 |
|
| } |
| 1077 |
|
| else { |
| 1078 |
|
| print |
| 1079 |
|
| " WARNING: unrecognized answer, defaulting to 'n'\n"; |
| 1080 |
|
| $zero = 0; |
| 1081 |
|
| } |
| 1082 |
|
| } |
| 1083 |
|
| if ($zero) { |
| 1084 |
|
| $check++; |
| 1085 |
|
| } |
| 1086 |
|
| } |
| 1087 |
|
| } |
| 1088 |
|
| |
| 1089 |
|
| # mark for deletion if the row fails the check |
| 1090 |
|
| push @todelete, $row->row_index if $check; |
| 1091 |
|
| } |
| 1092 |
|
| ); |
| 1093 |
|
| |
| 1094 |
|
| # Delete |
| 1095 |
|
| $Data->delete_row(@todelete); |
| 1096 |
|
| |
| 1097 |
|
| # update metadata |
| 1098 |
|
| foreach my $index (@order) { |
| 1099 |
|
| $Data->metadata( $index, 'deleted_non_value_features', scalar(@todelete) ) |
| 1100 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1101 |
|
| } |
| 1102 |
|
| |
| 1103 |
|
| # report |
| 1104 |
|
| printf " %s rows with null values in %s were deleted.\n", scalar(@todelete), |
| 1105 |
|
| join( ', ', map { $Data->name($_) } @order ); |
| 1106 |
|
| printf " %s rows are remaining\n", $Data->last_row; |
| 1107 |
|
| return 1; |
| 1108 |
|
| } |
| 1109 |
|
| |
| 1110 |
|
| sub toss_duplicates_function { |
| 1111 |
|
| |
| 1112 |
|
| # Toss out datapoints (lines) that have a duplicate values |
| 1113 |
|
| |
| 1114 |
|
| # generate the list of datasets to check |
| 1115 |
|
| my @order = _request_indices( |
| 1116 |
|
| " Enter one or more column index numbers to check for duplicates\n "); |
| 1117 |
|
| unless (@order) { |
| 1118 |
|
| print " WARNING: No valid columns! Nothing done!\n"; |
| 1119 |
|
| return 0; |
| 1120 |
|
| } |
| 1121 |
|
| |
| 1122 |
|
| # initialize variables |
| 1123 |
|
| my %values2check; |
| 1124 |
|
| my @todelete; |
| 1125 |
|
| |
| 1126 |
|
| # check values |
| 1127 |
|
| $Data->iterate( |
| 1128 |
|
| sub { |
| 1129 |
|
| my $row = shift; |
| 1130 |
|
| |
| 1131 |
|
| # we will simply concatenate all values to check for duplicity |
| 1132 |
|
| my $value = join( '_', map { $row->value($_) } @order ); |
| 1133 |
|
| if ( exists $values2check{$value} ) { |
| 1134 |
|
| |
| 1135 |
|
| # yes, it exists, mark for destruction |
| 1136 |
|
| $values2check{$value}++; |
| 1137 |
|
| push @todelete, $row->row_index; |
| 1138 |
|
| } |
| 1139 |
|
| else { |
| 1140 |
|
| # nope, it's good |
| 1141 |
|
| $values2check{$value} = 1; |
| 1142 |
|
| } |
| 1143 |
|
| } |
| 1144 |
|
| ); |
| 1145 |
|
| |
| 1146 |
|
| # delete |
| 1147 |
|
| $Data->delete_row(@todelete); |
| 1148 |
|
| |
| 1149 |
|
| # update metadata |
| 1150 |
|
| foreach my $index (@order) { |
| 1151 |
|
| $Data->metadata( $index, 'deleted_duplicate_features', scalar(@todelete) ) |
| 1152 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1153 |
|
| } |
| 1154 |
|
| |
| 1155 |
|
| # print result |
| 1156 |
|
| printf " %s rows with duplicate values in %s were deleted.\n", |
| 1157 |
|
| scalar(@todelete), join( ', ', map { $Data->name($_) } @order ); |
| 1158 |
|
| printf " %s rows are remaining\n", $Data->last_row; |
| 1159 |
|
| return 1; |
| 1160 |
|
| } |
| 1161 |
|
| |
| 1162 |
|
| sub toss_above_threshold_function { |
| 1163 |
|
| |
| 1164 |
|
| # Toss out datapoints (lines) whose value is above a certain threshold |
| 1165 |
|
| return toss_threshold_function('above'); |
| 1166 |
|
| } |
| 1167 |
|
| |
| 1168 |
|
| sub toss_below_threshold_function { |
| 1169 |
|
| |
| 1170 |
|
| # Toss out datapoints (lines) whose value is below a certain threshold |
| 1171 |
|
| return toss_threshold_function('below'); |
| 1172 |
|
| } |
| 1173 |
|
| |
| 1174 |
|
| sub toss_threshold_function { |
| 1175 |
|
| |
| 1176 |
|
| # Toss out lines whose specified datapoint value is above or below a |
| 1177 |
|
| # specified threshold |
| 1178 |
|
| |
| 1179 |
|
| # get the direction for excluding datapoints, above or below |
| 1180 |
|
| my $direction = shift; |
| 1181 |
|
| |
| 1182 |
|
| # generate the list of datasets to check |
| 1183 |
|
| my @order = _request_indices( |
| 1184 |
|
| " Enter one or more column index numbers to toss values $direction a value\n "); |
| 1185 |
|
| unless (@order) { |
| 1186 |
|
| print " WARNING: No valid datasets! Nothing done!\n"; |
| 1187 |
|
| return 0; |
| 1188 |
|
| } |
| 1189 |
|
| |
| 1190 |
|
| # identify the threshold |
| 1191 |
|
| my $threshold; |
| 1192 |
|
| if ( defined $opt_target ) { |
| 1193 |
|
| |
| 1194 |
|
| # specified on the command line |
| 1195 |
|
| $threshold = $opt_target; |
| 1196 |
|
| } |
| 1197 |
|
| else { |
| 1198 |
|
| # interactively ask the user |
| 1199 |
|
| my $p; |
| 1200 |
|
| if ( $direction eq 'above' ) { |
| 1201 |
|
| $p = ' Toss values that exceed this value: '; |
| 1202 |
|
| } |
| 1203 |
|
| else { |
| 1204 |
|
| $p = ' Toss values that are below this value: '; |
| 1205 |
|
| } |
| 1206 |
|
| $threshold = prompt($p); |
| 1207 |
|
| } |
| 1208 |
|
| |
| 1209 |
|
| # Check values |
| 1210 |
|
| my @todelete; |
| 1211 |
|
| if ( $direction eq 'above' ) { |
| 1212 |
|
| $Data->iterate( |
| 1213 |
|
| sub { |
| 1214 |
|
| my $row = shift; |
| 1215 |
|
| my $check = 0; |
| 1216 |
|
| foreach my $i (@order) { |
| 1217 |
|
| my $v = $row->value($i); |
| 1218 |
|
| next unless looks_like_number($v); |
| 1219 |
|
| $check++ if $v > $threshold; |
| 1220 |
|
| } |
| 1221 |
|
| |
| 1222 |
|
| # mark for deletion if the row fails the check |
| 1223 |
|
| push @todelete, $row->row_index if $check; |
| 1224 |
|
| } |
| 1225 |
|
| ); |
| 1226 |
|
| } |
| 1227 |
|
| elsif ( $direction eq 'below' ) { |
| 1228 |
|
| $Data->iterate( |
| 1229 |
|
| sub { |
| 1230 |
|
| my $row = shift; |
| 1231 |
|
| my $check = 0; |
| 1232 |
|
| foreach my $i (@order) { |
| 1233 |
|
| my $v = $row->value($i); |
| 1234 |
|
| next unless looks_like_number($v); |
| 1235 |
|
| $check++ if $v < $threshold; |
| 1236 |
|
| } |
| 1237 |
|
| |
| 1238 |
|
| # mark for deletion if the row fails the check |
| 1239 |
|
| push @todelete, $row->row_index if $check; |
| 1240 |
|
| } |
| 1241 |
|
| ); |
| 1242 |
|
| } |
| 1243 |
|
| |
| 1244 |
|
| # Delete |
| 1245 |
|
| $Data->delete_row(@todelete); |
| 1246 |
|
| |
| 1247 |
|
| # update metadata |
| 1248 |
|
| foreach my $index (@order) { |
| 1249 |
|
| $Data->metadata( $index, "deleted_$direction\_$threshold", scalar(@todelete) ) |
| 1250 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1251 |
|
| } |
| 1252 |
|
| |
| 1253 |
|
| # report |
| 1254 |
|
| printf " %s rows with values $direction $threshold in %s were deleted.\n", |
| 1255 |
|
| scalar(@todelete), join( ', ', map { $Data->name($_) } @order ); |
| 1256 |
|
| printf " %s rows are remaining\n", $Data->last_row; |
| 1257 |
|
| return 1; |
| 1258 |
|
| } |
| 1259 |
|
| |
| 1260 |
|
| sub toss_specific_values_function { |
| 1261 |
|
| return do_specific_values_function(1); |
| 1262 |
|
| } |
| 1263 |
|
| |
| 1264 |
|
| sub keep_specific_values_function { |
| 1265 |
|
| return do_specific_values_function(0); |
| 1266 |
|
| } |
| 1267 |
|
| |
| 1268 |
|
| sub do_specific_values_function { |
| 1269 |
|
| |
| 1270 |
|
| # toss specific values |
| 1271 |
|
| my $toss = shift; # boolean to toss (1) or keep (0) |
| 1272 |
|
| |
| 1273 |
|
| # generate the list of datasets to check |
| 1274 |
|
| my @list = _request_indices( |
| 1275 |
|
| " Enter one or more column index numbers to check for specific values\n "); |
| 1276 |
|
| unless (@list) { |
| 1277 |
|
| print " WARNING: No valid columns! Nothing done!\n"; |
| 1278 |
|
| return 0; |
| 1279 |
|
| } |
| 1280 |
|
| |
| 1281 |
|
| # determine values |
| 1282 |
|
| my %wanted; |
| 1283 |
|
| if ($opt_target) { |
| 1284 |
|
| |
| 1285 |
|
| # list provided |
| 1286 |
|
| %wanted = map { $_ => 0 } split( /,/, $opt_target ); |
| 1287 |
|
| } |
| 1288 |
|
| else { |
| 1289 |
|
| # generate potential values, hope there aren't too many |
| 1290 |
|
| my %possibilities; |
| 1291 |
|
| $Data->iterate( |
| 1292 |
|
| sub { |
| 1293 |
|
| my $row = shift; |
| 1294 |
|
| foreach my $l (@list) { |
| 1295 |
|
| $possibilities{ $row->value($l) } += 1; |
| 1296 |
|
| } |
| 1297 |
|
| } |
| 1298 |
|
| ); |
| 1299 |
|
| |
| 1300 |
|
| # present list to user |
| 1301 |
|
| print " These are the values (occurrences) in the indicated columns\n"; |
| 1302 |
|
| my %lookup; |
| 1303 |
|
| my $i = 1; |
| 1304 |
|
| foreach ( sort { $a cmp $b } keys %possibilities ) { |
| 1305 |
|
| |
| 1306 |
|
| # sort asciibetically by name |
| 1307 |
|
| printf " $i\t$_ (%s)\n", $possibilities{$_}; |
| 1308 |
|
| $lookup{$i} = $_; |
| 1309 |
|
| $i++; |
| 1310 |
|
| } |
| 1311 |
|
| my $p = |
| 1312 |
|
| sprintf |
| 1313 |
|
| " Enter the number(s) corresponding to the values to %s. Enter as a \n" |
| 1314 |
|
| . " comma-delimited list or range. ", $toss ? 'toss' : 'keep'; |
| 1315 |
|
| my $response = prompt($p); |
| 1316 |
|
| foreach ( parse_list($response) ) { |
| 1317 |
|
| if ( exists $lookup{$_} ) { |
| 1318 |
|
| $wanted{ $lookup{$_} } = 0; |
| 1319 |
|
| } |
| 1320 |
|
| } |
| 1321 |
|
| } |
| 1322 |
|
| unless (%wanted) { |
| 1323 |
|
| print " WARNING: No specific values provided! Nothing done!\n"; |
| 1324 |
|
| return 0; |
| 1325 |
|
| } |
| 1326 |
|
| |
| 1327 |
|
| # Identify rows to delete |
| 1328 |
|
| my @todelete; |
| 1329 |
|
| my $start_number = $Data->last_row; |
| 1330 |
|
| if ($toss) { |
| 1331 |
|
| |
| 1332 |
|
| # we are tossing lines that contain the specific value |
| 1333 |
|
| $Data->iterate( |
| 1334 |
|
| sub { |
| 1335 |
|
| my $row = shift; |
| 1336 |
|
| foreach my $l (@list) { |
| 1337 |
|
| if ( exists $wanted{ $row->value($l) } ) { |
| 1338 |
|
| push @todelete, $row->row_index; |
| 1339 |
|
| last; |
| 1340 |
|
| } |
| 1341 |
|
| } |
| 1342 |
|
| } |
| 1343 |
|
| ); |
| 1344 |
|
| } |
| 1345 |
|
| else { |
| 1346 |
|
| # we are keeping only those lines that contain the specific value |
| 1347 |
|
| $Data->iterate( |
| 1348 |
|
| sub { |
| 1349 |
|
| my $row = shift; |
| 1350 |
|
| my $check = 1; # default is to delete this row, so check starts true |
| 1351 |
|
| foreach my $l (@list) { |
| 1352 |
|
| if ( exists $wanted{ $row->value($l) } ) { |
| 1353 |
|
| $check = 0; |
| 1354 |
|
| last; |
| 1355 |
|
| } |
| 1356 |
|
| } |
| 1357 |
|
| push @todelete, $row->row_index if $check; |
| 1358 |
|
| } |
| 1359 |
|
| ); |
| 1360 |
|
| } |
| 1361 |
|
| |
| 1362 |
|
| # Delete |
| 1363 |
|
| $Data->delete_row(@todelete); |
| 1364 |
|
| |
| 1365 |
|
| # update metadata |
| 1366 |
|
| foreach my $index (@list) { |
| 1367 |
|
| if ($toss) { |
| 1368 |
|
| $Data->metadata( $index, "deleted_specific_values", |
| 1369 |
|
| join( ',', keys %wanted ) ) |
| 1370 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1371 |
|
| } |
| 1372 |
|
| else { |
| 1373 |
|
| $Data->metadata( $index, "kept_specific_values", join( ',', keys %wanted ) ) |
| 1374 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1375 |
|
| } |
| 1376 |
|
| } |
| 1377 |
|
| |
| 1378 |
|
| # report |
| 1379 |
|
| if ($toss) { |
| 1380 |
|
| printf " %s rows with specific values of %s were deleted in columns %s.\n", |
| 1381 |
|
| format_with_commas( scalar(@todelete) ), join( ', ', keys %wanted ), |
| 1382 |
|
| join( ', ', map { $Data->name($_) } @list ); |
| 1383 |
|
| printf " %s data lines are remaining\n", format_with_commas( $Data->last_row ); |
| 1384 |
|
| } |
| 1385 |
|
| else { |
| 1386 |
|
| printf " %s rows were deleted in columns %s\n", |
| 1387 |
|
| format_with_commas( scalar(@todelete) ), |
| 1388 |
|
| join( ', ', map { $Data->name($_) } @list ); |
| 1389 |
|
| printf " %s rows with specific values of %s were retained.\n", |
| 1390 |
|
| format_with_commas( $start_number - scalar(@todelete) ), |
| 1391 |
|
| join( ', ', keys %wanted ); |
| 1392 |
|
| } |
| 1393 |
|
| return 1; |
| 1394 |
|
| } |
| 1395 |
|
| |
| 1396 |
|
| sub convert_nulls_function { |
| 1397 |
|
| |
| 1398 |
|
| # Convert null values to something else |
| 1399 |
|
| |
| 1400 |
|
| # identify the datasets to check |
| 1401 |
|
| my @indices; |
| 1402 |
|
| if (@_) { |
| 1403 |
|
| |
| 1404 |
|
| # provided from an internal subroutine |
| 1405 |
|
| @indices = @_; |
| 1406 |
|
| } |
| 1407 |
|
| else { |
| 1408 |
|
| # otherwise request from user |
| 1409 |
|
| @indices = _request_indices( |
| 1410 |
|
| " Enter one or more dataset index numbers to convert null values "); |
| 1411 |
|
| } |
| 1412 |
|
| unless (@indices) { |
| 1413 |
|
| print " WARNING: no valid indices. Nothing done.\n"; |
| 1414 |
|
| return 0; |
| 1415 |
|
| } |
| 1416 |
|
| |
| 1417 |
|
| # request replacement value |
| 1418 |
|
| my $new_value; |
| 1419 |
|
| if ( defined $opt_target ) { |
| 1420 |
|
| |
| 1421 |
|
| # command line option |
| 1422 |
|
| $new_value = $opt_target; |
| 1423 |
|
| } |
| 1424 |
|
| else { |
| 1425 |
|
| # interactively ask the user |
| 1426 |
|
| my $p = ' Enter the new value to convert nulls to: '; |
| 1427 |
|
| $new_value = prompt($p); |
| 1428 |
|
| } |
| 1429 |
|
| |
| 1430 |
|
| # check zero status |
| 1431 |
|
| my $zero; |
| 1432 |
|
| if ( defined $opt_zero ) { |
| 1433 |
|
| $zero = $opt_zero; |
| 1434 |
|
| } |
| 1435 |
|
| |
| 1436 |
|
| # request placement |
| 1437 |
|
| my $placement = _request_placement(); |
| 1438 |
|
| |
| 1439 |
|
| ## Process the datasets and subtract their values |
| 1440 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1441 |
|
| my $total_count = 0; # total number of resets done |
| 1442 |
|
| foreach my $index (@indices) { |
| 1443 |
|
| |
| 1444 |
|
| # number of resets we do for this index |
| 1445 |
|
| my $count = 0; |
| 1446 |
|
| |
| 1447 |
|
| # reset values |
| 1448 |
|
| my $name = $Data->name($index); |
| 1449 |
|
| $index = _prepare_new_destination( $index, '_convert_nulls' ) |
| 1450 |
|
| if $placement =~ /^n/i; |
| 1451 |
|
| $Data->iterate( |
| 1452 |
|
| sub { |
| 1453 |
|
| my $row = shift; |
| 1454 |
|
| my $v = $row->value($index); |
| 1455 |
|
| if ( _is_null($v) ) { |
| 1456 |
|
| $row->value( $index, $new_value ); |
| 1457 |
|
| $count++; |
| 1458 |
|
| } |
| 1459 |
|
| elsif ( $v == 0 ) { |
| 1460 |
|
| |
| 1461 |
|
| # zero value, what to do? |
| 1462 |
|
| if ( not defined $zero and $function ) { |
| 1463 |
|
| |
| 1464 |
|
| # running automatically, do not both user |
| 1465 |
|
| $zero = 0; |
| 1466 |
|
| } |
| 1467 |
|
| elsif ( not defined $zero and not defined $function ) { |
| 1468 |
|
| |
| 1469 |
|
| # wasn't defined on the command line, running interactively, |
| 1470 |
|
| # so stop the program and ask the user |
| 1471 |
|
| my $p = ' Include 0 values to convert? y/n, default n: '; |
| 1472 |
|
| my $answer = prompt( $p, 'n' ); |
| 1473 |
|
| if ( lc $answer eq 'y' ) { |
| 1474 |
|
| $zero = 1; |
| 1475 |
|
| } |
| 1476 |
|
| elsif ( lc $answer eq 'n' ) { |
| 1477 |
|
| $zero = 0; |
| 1478 |
|
| } |
| 1479 |
|
| else { |
| 1480 |
|
| print |
| 1481 |
|
| " WARNING: unrecognized answer, defaulting to 'n'\n"; |
| 1482 |
|
| $zero = 0; |
| 1483 |
|
| } |
| 1484 |
|
| |
| 1485 |
|
| # remember for next time, chances are user may still want this |
| 1486 |
|
| # value again in the future |
| 1487 |
|
| $opt_zero = $zero; |
| 1488 |
|
| } |
| 1489 |
|
| |
| 1490 |
|
| if ($zero) { |
| 1491 |
|
| $row->value( $index, $new_value ); |
| 1492 |
|
| $count++; |
| 1493 |
|
| } |
| 1494 |
|
| } |
| 1495 |
|
| } |
| 1496 |
|
| ); |
| 1497 |
|
| |
| 1498 |
|
| # update metadata |
| 1499 |
|
| if ($count) { |
| 1500 |
|
| $Data->metadata( $index, 'null_value', $new_value ) |
| 1501 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1502 |
|
| $total_count += $count; |
| 1503 |
|
| push @datasets_modified, $name; |
| 1504 |
|
| } |
| 1505 |
|
| } |
| 1506 |
|
| |
| 1507 |
|
| # report results |
| 1508 |
|
| if (@datasets_modified) { |
| 1509 |
|
| printf " $total_count null values were converted for %s\n", |
| 1510 |
|
| join( ", ", @datasets_modified ); |
| 1511 |
|
| } |
| 1512 |
|
| return scalar(@datasets_modified); |
| 1513 |
|
| } |
| 1514 |
|
| |
| 1515 |
|
| sub convert_absolute_function { |
| 1516 |
|
| |
| 1517 |
|
| # Convert signed values to their absolute value |
| 1518 |
|
| |
| 1519 |
|
| # identify the datasets to check |
| 1520 |
|
| my @indices; |
| 1521 |
|
| if (@_) { |
| 1522 |
|
| |
| 1523 |
|
| # provided from an internal subroutine |
| 1524 |
|
| @indices = @_; |
| 1525 |
|
| } |
| 1526 |
|
| else { |
| 1527 |
|
| # otherwise request from user |
| 1528 |
|
| @indices = _request_indices( |
| 1529 |
|
| " Enter one or more dataset index numbers to make absolute "); |
| 1530 |
|
| } |
| 1531 |
|
| unless (@indices) { |
| 1532 |
|
| print " WARNING: no valid indices. Nothing done.\n"; |
| 1533 |
|
| return 0; |
| 1534 |
|
| } |
| 1535 |
|
| |
| 1536 |
|
| # request placement |
| 1537 |
|
| my $placement = _request_placement(); |
| 1538 |
|
| |
| 1539 |
|
| ## Process the datasets and subtract their values |
| 1540 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1541 |
|
| my $total_count = 0; # total number of conversions done |
| 1542 |
|
| my $total_failed = 0; |
| 1543 |
|
| foreach my $index (@indices) { |
| 1544 |
|
| |
| 1545 |
|
| # number of resets we do |
| 1546 |
|
| my $count = 0; |
| 1547 |
|
| my $failed = 0; |
| 1548 |
|
| |
| 1549 |
|
| # reset minimum values |
| 1550 |
|
| my $name = $Data->name($index); |
| 1551 |
|
| $index = _prepare_new_destination( $index, '_absolute' ) if $placement =~ /^n/i; |
| 1552 |
|
| $Data->iterate( |
| 1553 |
|
| sub { |
| 1554 |
|
| my $row = shift; |
| 1555 |
|
| my $v = $row->value($index); |
| 1556 |
|
| unless ( looks_like_number($v) ) { |
| 1557 |
|
| $failed++; |
| 1558 |
|
| next; |
| 1559 |
|
| } |
| 1560 |
|
| $row->value( $index, abs($v) ); |
| 1561 |
|
| $count++; |
| 1562 |
|
| } |
| 1563 |
|
| ); |
| 1564 |
|
| |
| 1565 |
|
| # update metadata |
| 1566 |
|
| |
| 1567 |
|
| # results |
| 1568 |
|
| if ($count) { |
| 1569 |
|
| $Data->metadata( $index, 'convert', 'absolute' ) |
| 1570 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1571 |
|
| $total_count += $count; |
| 1572 |
|
| push @datasets_modified, $name; |
| 1573 |
|
| } |
| 1574 |
|
| else { |
| 1575 |
|
| $total_failed += $failed; |
| 1576 |
|
| } |
| 1577 |
|
| } |
| 1578 |
|
| |
| 1579 |
|
| # report results |
| 1580 |
|
| if (@datasets_modified) { |
| 1581 |
|
| printf " $total_count values were converted to absolute values for %s\n", |
| 1582 |
|
| join( ", ", @datasets_modified ); |
| 1583 |
|
| } |
| 1584 |
|
| if ($total_failed) { |
| 1585 |
|
| print " $total_failed values could not be converted\n"; |
| 1586 |
|
| } |
| 1587 |
|
| return scalar(@datasets_modified); |
| 1588 |
|
| } |
| 1589 |
|
| |
| 1590 |
|
| sub minimum_function { |
| 1591 |
|
| |
| 1592 |
|
| # Set a minimum value |
| 1593 |
|
| |
| 1594 |
|
| # request datasets |
| 1595 |
|
| my @indices = _request_indices( |
| 1596 |
|
| " Enter one or more dataset index numbers to reset minimum values "); |
| 1597 |
|
| unless (@indices) { |
| 1598 |
|
| print " WARNING: no valid indices. nothing done\n"; |
| 1599 |
|
| return 0; |
| 1600 |
|
| } |
| 1601 |
|
| |
| 1602 |
|
| # request value |
| 1603 |
|
| my $value; |
| 1604 |
|
| if ( defined $opt_target ) { |
| 1605 |
|
| |
| 1606 |
|
| # command line option |
| 1607 |
|
| $value = $opt_target; |
| 1608 |
|
| } |
| 1609 |
|
| else { |
| 1610 |
|
| # interactively ask the user |
| 1611 |
|
| my $p = ' Enter the minimum value to accept: '; |
| 1612 |
|
| $value = prompt($p); |
| 1613 |
|
| } |
| 1614 |
|
| |
| 1615 |
|
| # request placement |
| 1616 |
|
| my $placement = _request_placement(); |
| 1617 |
|
| |
| 1618 |
|
| ## Process the datasets and subtract their values |
| 1619 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1620 |
|
| my $total_count = 0; # total number of conversions done |
| 1621 |
|
| foreach my $index (@indices) { |
| 1622 |
|
| |
| 1623 |
|
| # number of resets we do |
| 1624 |
|
| my $count = 0; |
| 1625 |
|
| |
| 1626 |
|
| # reset minimum values |
| 1627 |
|
| my $name = $Data->name($index); |
| 1628 |
|
| if ( $placement =~ /^n/i ) { |
| 1629 |
|
| $index = _prepare_new_destination( $index, '_minimum_reset' ); |
| 1630 |
|
| } |
| 1631 |
|
| $Data->iterate( |
| 1632 |
|
| sub { |
| 1633 |
|
| my $row = shift; |
| 1634 |
|
| my $v = $row->value($index); |
| 1635 |
|
| next unless looks_like_number($v); |
| 1636 |
|
| if ( $v < $value ) { |
| 1637 |
|
| $row->value( $index, $value ); |
| 1638 |
|
| $count++; |
| 1639 |
|
| } |
| 1640 |
|
| } |
| 1641 |
|
| ); |
| 1642 |
|
| |
| 1643 |
|
| # results |
| 1644 |
|
| if ($count) { |
| 1645 |
|
| $Data->metadata( $index, 'minimum_value', $value ) |
| 1646 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1647 |
|
| $total_count += $count; |
| 1648 |
|
| push @datasets_modified, $name; |
| 1649 |
|
| } |
| 1650 |
|
| } |
| 1651 |
|
| |
| 1652 |
|
| # report results |
| 1653 |
|
| if (@datasets_modified) { |
| 1654 |
|
| printf " $total_count values were reset to a minimum value for %s\n", |
| 1655 |
|
| join( ", ", @datasets_modified ); |
| 1656 |
|
| } |
| 1657 |
|
| return scalar(@datasets_modified); |
| 1658 |
|
| } |
| 1659 |
|
| |
| 1660 |
|
| sub maximum_function { |
| 1661 |
|
| |
| 1662 |
|
| # Set a maximum value |
| 1663 |
|
| |
| 1664 |
|
| # request datasets |
| 1665 |
|
| my @indices = _request_indices( |
| 1666 |
|
| " Enter one or more dataset index numbers to reset maximum values "); |
| 1667 |
|
| unless (@indices) { |
| 1668 |
|
| print " WARNING: no valid indices. nothing done\n"; |
| 1669 |
|
| return 0; |
| 1670 |
|
| } |
| 1671 |
|
| |
| 1672 |
|
| # request value |
| 1673 |
|
| my $value; |
| 1674 |
|
| if ( defined $opt_target ) { |
| 1675 |
|
| |
| 1676 |
|
| # command line option |
| 1677 |
|
| $value = $opt_target; |
| 1678 |
|
| } |
| 1679 |
|
| else { |
| 1680 |
|
| # interactively ask the user |
| 1681 |
|
| my $p = ' Enter the maximum value to accept: '; |
| 1682 |
|
| $value = prompt($p); |
| 1683 |
|
| } |
| 1684 |
|
| |
| 1685 |
|
| # request placement |
| 1686 |
|
| my $placement = _request_placement(); |
| 1687 |
|
| |
| 1688 |
|
| ## Process the datasets and subtract their values |
| 1689 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1690 |
|
| my $total_count = 0; # total number of conversions done |
| 1691 |
|
| foreach my $index (@indices) { |
| 1692 |
|
| |
| 1693 |
|
| # number of resets we do |
| 1694 |
|
| my $count = 0; |
| 1695 |
|
| |
| 1696 |
|
| # reset minimum values |
| 1697 |
|
| my $name = $Data->name($index); |
| 1698 |
|
| if ( $placement =~ /^n/i ) { |
| 1699 |
|
| $index = _prepare_new_destination( $index, '_maximum_reset' ); |
| 1700 |
|
| } |
| 1701 |
|
| $Data->iterate( |
| 1702 |
|
| sub { |
| 1703 |
|
| my $row = shift; |
| 1704 |
|
| my $v = $row->value($index); |
| 1705 |
|
| next unless looks_like_number($v); |
| 1706 |
|
| if ( $v > $value ) { |
| 1707 |
|
| $row->value( $index, $value ); |
| 1708 |
|
| $count++; |
| 1709 |
|
| } |
| 1710 |
|
| } |
| 1711 |
|
| ); |
| 1712 |
|
| |
| 1713 |
|
| # results |
| 1714 |
|
| if ($count) { |
| 1715 |
|
| $Data->metadata( $index, 'maximum_value', $value ) |
| 1716 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1717 |
|
| $total_count += $count; |
| 1718 |
|
| push @datasets_modified, $name; |
| 1719 |
|
| } |
| 1720 |
|
| } |
| 1721 |
|
| |
| 1722 |
|
| # report results |
| 1723 |
|
| if (@datasets_modified) { |
| 1724 |
|
| printf " $total_count values were reset to a maximum value for %s\n", |
| 1725 |
|
| join( ", ", @datasets_modified ); |
| 1726 |
|
| } |
| 1727 |
|
| return scalar(@datasets_modified); |
| 1728 |
|
| } |
| 1729 |
|
| |
| 1730 |
|
| sub log_function { |
| 1731 |
|
| |
| 1732 |
|
| # this subroutine will convert dataset values to log2 space |
| 1733 |
|
| |
| 1734 |
|
| # request datasets |
| 1735 |
|
| my @indices; |
| 1736 |
|
| my $base; |
| 1737 |
|
| if (@_) { |
| 1738 |
|
| |
| 1739 |
|
| # provided from an internal subroutine |
| 1740 |
|
| ( $base, @indices ) = @_; |
| 1741 |
|
| } |
| 1742 |
|
| else { |
| 1743 |
|
| # otherwise request from user |
| 1744 |
|
| @indices = _request_indices( |
| 1745 |
|
| " Enter one or more dataset index numbers to convert to log "); |
| 1746 |
|
| if ( defined $opt_target ) { |
| 1747 |
|
| $base = $opt_target; |
| 1748 |
|
| } |
| 1749 |
|
| else { |
| 1750 |
|
| # interactively ask the user |
| 1751 |
|
| my $p = ' What log base to use? [2 10]: '; |
| 1752 |
|
| $base = prompt($p); |
| 1753 |
|
| } |
| 1754 |
|
| |
| 1755 |
|
| } |
| 1756 |
|
| unless (@indices) { |
| 1757 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 1758 |
|
| return 0; |
| 1759 |
|
| } |
| 1760 |
|
| unless ( $base =~ /^\d+$/ ) { |
| 1761 |
|
| print " WARNING: unrecognized base number. nothing done\n"; |
| 1762 |
|
| return 0; |
| 1763 |
|
| } |
| 1764 |
|
| my $factor = log($base); |
| 1765 |
|
| |
| 1766 |
|
| # request placement |
| 1767 |
|
| my $placement = _request_placement(); |
| 1768 |
|
| |
| 1769 |
|
| # process each index request |
| 1770 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1771 |
|
| my $total_count = 0; # total number of conversions done |
| 1772 |
|
| my $total_failed = 0; |
| 1773 |
|
| foreach my $index (@indices) { |
| 1774 |
|
| |
| 1775 |
|
| # check the current metadata status |
| 1776 |
|
| my $check = |
| 1777 |
|
| $Data->metadata( $index, 'log' ) || $Data->metadata( $index, 'log2' ) || 0; |
| 1778 |
|
| if ( $check != 0 ) { |
| 1779 |
|
| printf " dataset %s metadata reports it is currently in log scale.", |
| 1780 |
|
| $Data->name($index); |
| 1781 |
|
| my $p = ' Continue? y/n: '; |
| 1782 |
|
| my $response = prompt($p); |
| 1783 |
|
| if ( lc $response eq 'n' ) { |
| 1784 |
|
| next; |
| 1785 |
|
| } |
| 1786 |
|
| } |
| 1787 |
|
| |
| 1788 |
|
| # Placement dictates method |
| 1789 |
|
| my $count = 0; # conversion count |
| 1790 |
|
| my $failed = 0; |
| 1791 |
|
| |
| 1792 |
|
| # perform log conversions |
| 1793 |
|
| my $name = $Data->name($index); |
| 1794 |
|
| $index = _prepare_new_destination( $index, "_log$base" ) if $placement =~ /^n/i; |
| 1795 |
|
| $Data->iterate( |
| 1796 |
|
| sub { |
| 1797 |
|
| my $row = shift; |
| 1798 |
|
| my $v = $row->value($index); |
| 1799 |
|
| |
| 1800 |
|
| # check the value contents and process appropriately |
| 1801 |
|
| if ( not looks_like_number($v) ) { |
| 1802 |
|
| |
| 1803 |
|
| # not a number, do nothing |
| 1804 |
|
| $failed++; |
| 1805 |
|
| } |
| 1806 |
|
| elsif ( $v == 0 ) { |
| 1807 |
|
| |
| 1808 |
|
| # cannot take log of 0, change to null |
| 1809 |
|
| $row->value( $index, '.' ); |
| 1810 |
|
| $failed++; |
| 1811 |
|
| } |
| 1812 |
|
| else { |
| 1813 |
|
| my $new_value = log($v) / $factor; |
| 1814 |
|
| $row->value( $index, $new_value ); |
| 1815 |
|
| $count++; |
| 1816 |
|
| } |
| 1817 |
|
| } |
| 1818 |
|
| ); |
| 1819 |
|
| |
| 1820 |
|
| # update metadata |
| 1821 |
|
| $Data->metadata( $index, 'log', $base ) |
| 1822 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1823 |
|
| |
| 1824 |
|
| # results |
| 1825 |
|
| if ($count) { |
| 1826 |
|
| $total_count += $count; |
| 1827 |
|
| push @datasets_modified, $name; |
| 1828 |
|
| } |
| 1829 |
|
| $total_failed += $failed; |
| 1830 |
|
| } |
| 1831 |
|
| |
| 1832 |
|
| # report results |
| 1833 |
|
| if (@datasets_modified) { |
| 1834 |
|
| printf " $total_count values were converted to log$base for %s\n", |
| 1835 |
|
| join( ", ", @datasets_modified ); |
| 1836 |
|
| } |
| 1837 |
|
| if ($total_failed) { |
| 1838 |
|
| print " $total_failed values could not be converted\n"; |
| 1839 |
|
| } |
| 1840 |
|
| return scalar(@datasets_modified); |
| 1841 |
|
| } |
| 1842 |
|
| |
| 1843 |
|
| sub delog_function { |
| 1844 |
|
| |
| 1845 |
|
| # this subroutine will convert a dataset from log to normal numbers |
| 1846 |
|
| |
| 1847 |
|
| # request datasets |
| 1848 |
|
| my @indices; |
| 1849 |
|
| if (@_) { |
| 1850 |
|
| |
| 1851 |
|
| # provided from an internal subroutine |
| 1852 |
|
| @indices = @_; |
| 1853 |
|
| } |
| 1854 |
|
| else { |
| 1855 |
|
| # otherwise request from user |
| 1856 |
|
| @indices = _request_indices( |
| 1857 |
|
| " Enter one or more dataset index numbers to convert from log "); |
| 1858 |
|
| } |
| 1859 |
|
| unless (@indices) { |
| 1860 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 1861 |
|
| return 0; |
| 1862 |
|
| } |
| 1863 |
|
| |
| 1864 |
|
| # request placement |
| 1865 |
|
| my $placement = _request_placement(); |
| 1866 |
|
| |
| 1867 |
|
| # process each index request |
| 1868 |
|
| my $base; |
| 1869 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1870 |
|
| my $total_count = 0; # total number of conversions done |
| 1871 |
|
| my $total_failed = 0; |
| 1872 |
|
| foreach my $index (@indices) { |
| 1873 |
|
| |
| 1874 |
|
| # check the log metadata status |
| 1875 |
|
| $base = $Data->metadata( $index, 'log' ) || 0; |
| 1876 |
|
| unless ($base) { |
| 1877 |
|
| $base = 2 if ( $Data->metadata( $index, 'log2' ) ); |
| 1878 |
|
| $base = 10 if ( $Data->metadata( $index, 'log10' ) ); |
| 1879 |
|
| } |
| 1880 |
|
| if ( $base == 0 ) { |
| 1881 |
|
| if ( defined $opt_target ) { |
| 1882 |
|
| $base = $opt_target; |
| 1883 |
|
| } |
| 1884 |
|
| else { |
| 1885 |
|
| my $p = ' What log base is the data in? [2 10]: '; |
| 1886 |
|
| $base = prompt($p); |
| 1887 |
|
| } |
| 1888 |
|
| } |
| 1889 |
|
| unless ( $base =~ /^\d+$/ ) { |
| 1890 |
|
| print " WARNING: Unrecognized base integer '$base'. Nothing done.\n"; |
| 1891 |
|
| return scalar(@datasets_modified); |
| 1892 |
|
| } |
| 1893 |
|
| |
| 1894 |
|
| # Placement dictates method |
| 1895 |
|
| my $count = 0; # conversion count |
| 1896 |
|
| my $failed = 0; |
| 1897 |
|
| my $name = $Data->name($index); |
| 1898 |
|
| $index = _prepare_new_destination( $index, "_delog$base" ) if $placement =~ /^n/i; |
| 1899 |
|
| $Data->iterate( |
| 1900 |
|
| sub { |
| 1901 |
|
| my $row = shift; |
| 1902 |
|
| my $v = $row->value($index); |
| 1903 |
|
| |
| 1904 |
|
| # check the value contents and process appropriately |
| 1905 |
|
| if ( looks_like_number($v) ) { |
| 1906 |
|
| $row->value( $index, $base**$v ); |
| 1907 |
|
| $count++; |
| 1908 |
|
| } |
| 1909 |
|
| else { |
| 1910 |
|
| $failed++; |
| 1911 |
|
| } |
| 1912 |
|
| } |
| 1913 |
|
| ); |
| 1914 |
|
| |
| 1915 |
|
| # update metadata |
| 1916 |
|
| $Data->metadata( $index, 'log', 0 ) |
| 1917 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1918 |
|
| $Data->delete_metadata( $index, 'log2' ); |
| 1919 |
|
| $Data->delete_metadata( $index, 'log10' ); |
| 1920 |
|
| |
| 1921 |
|
| # results |
| 1922 |
|
| if ($count) { |
| 1923 |
|
| $total_count += $count; |
| 1924 |
|
| push @datasets_modified, $name; |
| 1925 |
|
| } |
| 1926 |
|
| $total_failed += $failed; |
| 1927 |
|
| } |
| 1928 |
|
| |
| 1929 |
|
| # report results |
| 1930 |
|
| if (@datasets_modified) { |
| 1931 |
|
| printf " $total_count values were converted from log$base for %s\n", |
| 1932 |
|
| join( ", ", @datasets_modified ); |
| 1933 |
|
| } |
| 1934 |
|
| if ($total_failed) { |
| 1935 |
|
| print " $total_failed values could not be converted\n"; |
| 1936 |
|
| } |
| 1937 |
|
| return scalar(@datasets_modified); |
| 1938 |
|
| } |
| 1939 |
|
| |
| 1940 |
|
| sub format_function { |
| 1941 |
|
| |
| 1942 |
|
| # this subroutine will format the numbers in a dataset using sprintf |
| 1943 |
|
| |
| 1944 |
|
| # request datasets |
| 1945 |
|
| my @indices; |
| 1946 |
|
| if (@_) { |
| 1947 |
|
| |
| 1948 |
|
| # provided from an internal subroutine |
| 1949 |
|
| @indices = @_; |
| 1950 |
|
| } |
| 1951 |
|
| else { |
| 1952 |
|
| # otherwise request from user |
| 1953 |
|
| @indices = |
| 1954 |
|
| _request_indices(" Enter one or more dataset index numbers to format "); |
| 1955 |
|
| } |
| 1956 |
|
| unless (@indices) { |
| 1957 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 1958 |
|
| return 0; |
| 1959 |
|
| } |
| 1960 |
|
| |
| 1961 |
|
| # ask for the number of decimal positions to format to |
| 1962 |
|
| my $positions; |
| 1963 |
|
| if ( defined $opt_target ) { |
| 1964 |
|
| |
| 1965 |
|
| # specified on the command line |
| 1966 |
|
| $positions = $opt_target; |
| 1967 |
|
| } |
| 1968 |
|
| else { |
| 1969 |
|
| # interactively ask the user |
| 1970 |
|
| my $p = ' Format the numbers to how many decimal positions? '; |
| 1971 |
|
| $positions = prompt($p); |
| 1972 |
|
| } |
| 1973 |
|
| unless ( $positions =~ /^\d+$/ ) { |
| 1974 |
|
| print " WARNING: Unknown number of positions; formatting NOT done\n"; |
| 1975 |
|
| return 0; |
| 1976 |
|
| } |
| 1977 |
|
| my $format_string = '%.' . $positions . 'f'; |
| 1978 |
|
| |
| 1979 |
|
| # ask for placement |
| 1980 |
|
| my $placement = _request_placement(); |
| 1981 |
|
| |
| 1982 |
|
| # format each index request |
| 1983 |
|
| my @datasets_modified; # a list of which datasets were modified |
| 1984 |
|
| foreach my $index (@indices) { |
| 1985 |
|
| my $name = $Data->name($index); |
| 1986 |
|
| $index = _prepare_new_destination( $index, '_formatted' ) if $placement =~ /^n/i; |
| 1987 |
|
| $Data->iterate( |
| 1988 |
|
| sub { |
| 1989 |
|
| my $row = shift; |
| 1990 |
|
| my $v = $row->value($index); |
| 1991 |
|
| if ( looks_like_number($v) ) { |
| 1992 |
|
| $row->value( $index, sprintf( $format_string, $v ) ); |
| 1993 |
|
| } |
| 1994 |
|
| } |
| 1995 |
|
| ); |
| 1996 |
|
| |
| 1997 |
|
| $Data->metadata( $index, 'formatted', $positions ) |
| 1998 |
|
| unless $Data->metadata( $index, 'AUTO' ); |
| 1999 |
|
| push @datasets_modified, $name; |
| 2000 |
|
| } |
| 2001 |
|
| |
| 2002 |
|
| # report results |
| 2003 |
|
| if (@datasets_modified) { |
| 2004 |
|
| printf " formatted values to $positions decimal positions for %s\n", |
| 2005 |
|
| join( ", ", @datasets_modified ); |
| 2006 |
|
| } |
| 2007 |
|
| return scalar(@datasets_modified); |
| 2008 |
|
| } |
| 2009 |
|
| |
| 2010 |
|
| sub combine_function { |
| 2011 |
|
| |
| 2012 |
|
| # mathematically combine two or more datasets |
| 2013 |
|
| |
| 2014 |
|
| # request datasets |
| 2015 |
|
| my @indices; |
| 2016 |
|
| if (@_) { |
| 2017 |
|
| |
| 2018 |
|
| # provided from an internal subroutine |
| 2019 |
|
| @indices = @_; |
| 2020 |
|
| } |
| 2021 |
|
| else { |
| 2022 |
|
| # otherwise request from user |
| 2023 |
|
| @indices = _request_indices( |
| 2024 |
|
| " Enter two or more dataset index numbers to mathematically combine "); |
| 2025 |
|
| } |
| 2026 |
|
| unless (@indices) { |
| 2027 |
|
| print " WARNING: unknown index number(s). nothing done\n"; |
| 2028 |
|
| return 0; |
| 2029 |
|
| } |
| 2030 |
|
| unless ( scalar @indices >= 2 ) { |
| 2031 |
|
| print " WARNING: two or more indices are required to combine. nothing done.\n"; |
| 2032 |
|
| return 0; |
| 2033 |
|
| } |
| 2034 |
|
| |
| 2035 |
|
| # request value |
| 2036 |
|
| my $method; |
| 2037 |
|
| if ( defined $opt_target ) { |
| 2038 |
|
| |
| 2039 |
|
| # command line option |
| 2040 |
|
| $method = $opt_target; |
| 2041 |
|
| } |
| 2042 |
|
| else { |
| 2043 |
|
| # interactively ask the user |
| 2044 |
|
| my $p = ' Enter the method [mean median min max stdev sum], default mean: '; |
| 2045 |
|
| $method = prompt( $p, 'mean' ); |
| 2046 |
|
| } |
| 2047 |
|
| my $combine_method = |
| 2048 |
|
| $method eq 'mean' ? sub { return sum0(@_) / ( scalar(@_) || 1 ); } |
| 2049 |
|
| : $method eq 'median' ? \&median |
| 2050 |
|
| : $method eq 'min' ? \&min |
| 2051 |
|
| : $method eq 'max' ? \&max |
| 2052 |
|
| : $method eq 'stdev' ? \&stddevp |
| 2053 |
|
| : $method eq 'sum' ? \&sum0 |
| 2054 |
|
| : undef; |
| 2055 |
|
| unless ($combine_method) { |
| 2056 |
|
| print " WARNING: unknown method. nothing done\n"; |
| 2057 |
|
| return 0; |
| 2058 |
|
| } |
| 2059 |
|
| |
| 2060 |
|
| # generate new column |
| 2061 |
|
| my $new_name; |
| 2062 |
|
| if ( $function and $opt_name ) { |
| 2063 |
|
| |
| 2064 |
|
| # automatic execution and new name was specifically given |
| 2065 |
|
| $new_name = $opt_name; |
| 2066 |
|
| } |
| 2067 |
|
| else { |
| 2068 |
|
| $new_name = $method; |
| 2069 |
|
| } |
| 2070 |
|
| my $new_position = $Data->add_column($new_name); |
| 2071 |
|
| |
| 2072 |
|
| # combine datasets |
| 2073 |
|
| my $failure_count = 0; # number of failures |
| 2074 |
|
| $Data->iterate( |
| 2075 |
|
| sub { |
| 2076 |
|
| my $row = shift; |
| 2077 |
|
| |
| 2078 |
|
| # collect data |
| 2079 |
|
| my @data; |
| 2080 |
|
| foreach (@indices) { |
| 2081 |
|
| my $v = $row->value($_); |
| 2082 |
|
| next unless looks_like_number($v); |
| 2083 |
|
| push @data, $v; |
| 2084 |
|
| } |
| 2085 |
|
| |
| 2086 |
|
| # combine the data |
| 2087 |
|
| my $v; |
| 2088 |
|
| if (@data) { |
| 2089 |
|
| $v = &{$combine_method}(@data); |
| 2090 |
|
| } |
| 2091 |
|
| else { |
| 2092 |
|
| $v = $method eq 'sum' ? 0 : '.'; |
| 2093 |
|
| $failure_count++; |
| 2094 |
|
| } |
| 2095 |
|
| $row->value( $new_position, $v ); |
| 2096 |
|
| } |
| 2097 |
|
| ); |
| 2098 |
|
| |
| 2099 |
|
| # generate new metadata |
| 2100 |
|
| $Data->metadata( $new_position, 'datasets', |
| 2101 |
|
| join( ',', map { $Data->name($_) } @indices ) ); |
| 2102 |
|
| $Data->metadata( $new_position, 'combine', $method ); |
| 2103 |
|
| |
| 2104 |
|
| # finish |
| 2105 |
|
| printf " combined datasets %s by $method\n", |
| 2106 |
|
| join( ', ', map { $Data->name($_) } @indices ); |
| 2107 |
|
| print " $failure_count data rows could not be combined\n" if $failure_count; |
| 2108 |
|
| return 1; |
| 2109 |
|
| } |
| 2110 |
|
| |
| 2111 |
|
| sub ratio_function { |
| 2112 |
|
| |
| 2113 |
|
| # Generate a new ratio between two datasets |
| 2114 |
|
| |
| 2115 |
|
| # request dataset |
| 2116 |
|
| my ( $numerator, $denominator ); |
| 2117 |
|
| if ( defined $opt_numerator and defined $opt_denominator ) { |
| 2118 |
|
| if ( _validate_index_list( $opt_numerator, $opt_denominator ) ) { |
| 2119 |
|
| $numerator = $opt_numerator; |
| 2120 |
|
| $denominator = $opt_denominator; |
| 2121 |
|
| } |
| 2122 |
|
| else { |
| 2123 |
|
| print " WARNING: unknown index number(s); nothing done\n"; |
| 2124 |
|
| return 0; |
| 2125 |
|
| } |
| 2126 |
|
| } |
| 2127 |
|
| else { |
| 2128 |
|
| ( $numerator, $denominator ) = |
| 2129 |
|
| _request_indices( " Enter two dataset index numbers for the ratio as " |
| 2130 |
|
| . "'numerator, denominator'\n " ); |
| 2131 |
|
| unless ( defined $numerator and defined $denominator ) { |
| 2132 |
|
| print " WARNING: unknown index number(s); nothing done\n"; |
| 2133 |
|
| return 0; |
| 2134 |
|
| } |
| 2135 |
|
| } |
| 2136 |
|
| |
| 2137 |
|
| # check if log2 numbers |
| 2138 |
|
| # log2 ratios performed by subtraction, regular number ratios by division |
| 2139 |
|
| my $log; |
| 2140 |
|
| if ( defined $opt_log ) { |
| 2141 |
|
| |
| 2142 |
|
| # if unable to identify the log2 status in metadata |
| 2143 |
|
| # use the command line option variable if set |
| 2144 |
|
| $log = $opt_log; |
| 2145 |
|
| } |
| 2146 |
|
| else { |
| 2147 |
|
| # set log status based on mutual metadata flags |
| 2148 |
|
| my $numerator_log = $Data->metadata( $numerator, 'log2' ) |
| 2149 |
|
| || $Data->metadata( $numerator, 'log' ); |
| 2150 |
|
| my $denominator_log = $Data->metadata( $denominator, 'log2' ) |
| 2151 |
|
| || $Data->metadata( $denominator, 'log' ); |
| 2152 |
|
| |
| 2153 |
|
| if ( $numerator_log and $denominator_log ) { |
| 2154 |
|
| if ( $numerator_log == $denominator_log ) { |
| 2155 |
|
| |
| 2156 |
|
| # both set, both equal, great! |
| 2157 |
|
| $log = $numerator_log; |
| 2158 |
|
| } |
| 2159 |
|
| else { |
| 2160 |
|
| print " WARNING: Columns appear to have different log status!\n" |
| 2161 |
|
| . " numerator is $numerator_log and denominator is " |
| 2162 |
|
| . " $denominator_log\n Nothing done."; |
| 2163 |
|
| return 0; |
| 2164 |
|
| } |
| 2165 |
|
| } |
| 2166 |
|
| elsif ( $numerator_log or $denominator_log ) { |
| 2167 |
|
| print " WARNING: Columns appear to have different log status!\n" |
| 2168 |
|
| . " numerator is $numerator_log and denominator is " |
| 2169 |
|
| . " $denominator_log\n Nothing done."; |
| 2170 |
|
| return 0; |
| 2171 |
|
| } |
| 2172 |
|
| else { |
| 2173 |
|
| # looks like neither is log state |
| 2174 |
|
| $log = 0; |
| 2175 |
|
| } |
| 2176 |
|
| } |
| 2177 |
|
| $log = 2 if $log == 1; # assume log2 status if simply true |
| 2178 |
|
| |
| 2179 |
|
| # generate new column |
| 2180 |
|
| my $new_name; |
| 2181 |
|
| if ( $function and $opt_name ) { |
| 2182 |
|
| |
| 2183 |
|
| # automatic execution and new name was specifically given |
| 2184 |
|
| $new_name = $opt_name; |
| 2185 |
|
| } |
| 2186 |
|
| else { |
| 2187 |
|
| $new_name = $Data->name($numerator) . '_' . $Data->name($denominator) . '_ratio'; |
| 2188 |
|
| } |
| 2189 |
|
| my $new_position = $Data->add_column($new_name); |
| 2190 |
|
| |
| 2191 |
|
| # generate ratio |
| 2192 |
|
| my $failure_count = 0; # number of failures |
| 2193 |
|
| $Data->iterate( |
| 2194 |
|
| sub { |
| 2195 |
|
| my $row = shift; |
| 2196 |
|
| my $n = $row->value($numerator); |
| 2197 |
|
| my $d = $row->value($denominator); |
| 2198 |
|
| |
| 2199 |
|
| # add new value |
| 2200 |
|
| if ( _is_null($n) or _is_null($d) ) { |
| 2201 |
|
| |
| 2202 |
|
| # either value is null |
| 2203 |
|
| $row->value( $new_position, '.' ); |
| 2204 |
|
| $failure_count++; |
| 2205 |
|
| } |
| 2206 |
|
| elsif ( $d == 0 and not $log ) { |
| 2207 |
|
| |
| 2208 |
|
| # denominator is 0, avoid div by 0 errors, assign null |
| 2209 |
|
| $row->value( $new_position, '.' ); |
| 2210 |
|
| $failure_count++; |
| 2211 |
|
| } |
| 2212 |
|
| else { |
| 2213 |
|
| # both values good, calculate ratio |
| 2214 |
|
| if ($log) { |
| 2215 |
|
| |
| 2216 |
|
| # perform a subtraction with log values |
| 2217 |
|
| $row->value( $new_position, ( $n - $d ) ); |
| 2218 |
|
| } |
| 2219 |
|
| else { |
| 2220 |
|
| # perform a division with non-log values |
| 2221 |
|
| $row->value( $new_position, ( $n / $d ) ); |
| 2222 |
|
| } |
| 2223 |
|
| } |
| 2224 |
|
| } |
| 2225 |
|
| ); |
| 2226 |
|
| |
| 2227 |
|
| # annotate the new medadata hash |
| 2228 |
|
| $Data->metadata( $new_position, 'method', 'ratio' ); |
| 2229 |
|
| $Data->metadata( $new_position, 'log', $log ); |
| 2230 |
|
| $Data->metadata( $new_position, 'denominator', $Data->name($denominator) ); |
| 2231 |
|
| $Data->metadata( $new_position, 'numerator', $Data->name($numerator) ); |
| 2232 |
|
| |
| 2233 |
|
| # print conclusion |
| 2234 |
|
| printf " ratio between %s and %s\n generated as new dataset\n", |
| 2235 |
|
| $Data->name($numerator), $Data->name($denominator); |
| 2236 |
|
| if ($failure_count) { |
| 2237 |
|
| print " $failure_count datapoints could not generate ratios\n"; |
| 2238 |
|
| } |
| 2239 |
|
| return 1; |
| 2240 |
|
| } |
| 2241 |
|
| |
| 2242 |
|
| sub difference_function { |
| 2243 |
|
| |
| 2244 |
|
| # Generate a new difference between two datasets |
| 2245 |
|
| |
| 2246 |
|
| # Request datasets |
| 2247 |
|
| my ( $experiment_index, $control_index ); |
| 2248 |
|
| if ( defined $opt_numerator and defined $opt_denominator ) { |
| 2249 |
|
| if ( _validate_index_list( $opt_numerator, $opt_denominator ) ) { |
| 2250 |
|
| |
| 2251 |
|
| # defined as command line options |
| 2252 |
|
| $experiment_index = $opt_numerator; |
| 2253 |
|
| $control_index = $opt_denominator; |
| 2254 |
|
| } |
| 2255 |
|
| else { |
| 2256 |
|
| print " WARNING: unknown index number(s); nothing done\n"; |
| 2257 |
|
| return 0; |
| 2258 |
|
| } |
| 2259 |
|
| } |
| 2260 |
|
| else { |
| 2261 |
|
| # request from user |
| 2262 |
|
| my $line = " Enter two dataset index numbers to generate the difference\n" |
| 2263 |
|
| . " as 'experiment, control'\n "; |
| 2264 |
|
| ( $experiment_index, $control_index ) = _request_indices($line); |
| 2265 |
|
| unless ( defined $experiment_index and defined $control_index ) { |
| 2266 |
|
| print " WARNING: unknown index number(s); nothing done\n"; |
| 2267 |
|
| return 0; |
| 2268 |
|
| } |
| 2269 |
|
| } |
| 2270 |
|
| |
| 2271 |
|
| # Generate new column |
| 2272 |
|
| my $new_name; |
| 2273 |
|
| if ( $function and $opt_name ) { |
| 2274 |
|
| |
| 2275 |
|
| # this was an automatically executed function |
| 2276 |
|
| # and a new name was specified on the command line |
| 2277 |
|
| $new_name = $opt_name; |
| 2278 |
|
| } |
| 2279 |
|
| else { |
| 2280 |
|
| $new_name = |
| 2281 |
|
| $Data->name($experiment_index) . '_' . $Data->name($control_index) . '_diff'; |
| 2282 |
|
| } |
| 2283 |
|
| my $new_position = $Data->add_column($new_name); |
| 2284 |
|
| |
| 2285 |
|
| # Generate difference |
| 2286 |
|
| my $failure_count = 0; # number of failures |
| 2287 |
|
| $Data->iterate( |
| 2288 |
|
| sub { |
| 2289 |
|
| my $row = shift; |
| 2290 |
|
| my $e = $row->value($experiment_index); |
| 2291 |
|
| my $c = $row->value($control_index); |
| 2292 |
|
| |
| 2293 |
|
| # calculate difference |
| 2294 |
|
| if ( looks_like_number($e) and looks_like_number($c) ) { |
| 2295 |
|
| $row->value( $new_position, $e - $c ); |
| 2296 |
|
| } |
| 2297 |
|
| else { |
| 2298 |
|
| # can't do anything with non-numbers, new value will be null |
| 2299 |
|
| $row->value( $new_position, '.' ); |
| 2300 |
|
| $failure_count++; |
| 2301 |
|
| } |
| 2302 |
|
| } |
| 2303 |
|
| ); |
| 2304 |
|
| |
| 2305 |
|
| # Annotate the new medadata hash |
| 2306 |
|
| $Data->metadata( $new_position, 'method', 'difference' ); |
| 2307 |
|
| $Data->metadata( $new_position, 'experiment', $Data->name($experiment_index) ); |
| 2308 |
|
| $Data->metadata( $new_position, 'control', $Data->name($control_index) ); |
| 2309 |
|
| |
| 2310 |
|
| # Print conclusion |
| 2311 |
|
| printf " difference between %s and %s\n generated as new column\n", |
| 2312 |
|
| $Data->name($experiment_index), $Data->name($control_index); |
| 2313 |
|
| if ($failure_count) { |
| 2314 |
|
| print " $failure_count rows could not generate ratios\n"; |
| 2315 |
|
| } |
| 2316 |
|
| |
| 2317 |
|
| return 1; |
| 2318 |
|
| } |
| 2319 |
|
| |
| 2320 |
|
| sub add_function { |
| 2321 |
|
| |
| 2322 |
|
| # add a specific value to dataset values |
| 2323 |
|
| return math_function( 'add', @_ ); |
| 2324 |
|
| } |
| 2325 |
|
| |
| 2326 |
|
| sub subtract_function { |
| 2327 |
|
| |
| 2328 |
|
| # subtract a specific value from dataset values |
| 2329 |
|
| return math_function( 'subtract', @_ ); |
| 2330 |
|
| } |
| 2331 |
|
| |
| 2332 |
|
| sub multiply_function { |
| 2333 |
|
| |
| 2334 |
|
| # multiply dataset values by a specific value |
| 2335 |
|
| return math_function( 'multiply', @_ ); |
| 2336 |
|
| } |
| 2337 |
|
| |
| 2338 |
|
| sub divide_function { |
| 2339 |
|
| |
| 2340 |
|
| # divide dataset values by a specific value |
| 2341 |
|
| return math_function( 'divide', @_ ); |
| 2342 |
|
| } |
| 2343 |
|
| |
| 2344 |
|
| sub math_function { |
| 2345 |
|
| |
| 2346 |
|
| # General function to perform simple math on each datapoint value |
| 2347 |
|
| # in a dataset by a specific value |
| 2348 |
|
| my $math = shift; |
| 2349 |
|
| |
| 2350 |
|
| # request dataset indices |
| 2351 |
|
| my $line = " Enter one or more dataset index numbers to $math "; |
| 2352 |
|
| my @indices; |
| 2353 |
|
| if (@_) { |
| 2354 |
|
| |
| 2355 |
|
| # provided from an internal subroutine |
| 2356 |
|
| @indices = @_; |
| 2357 |
|
| } |
| 2358 |
|
| else { |
| 2359 |
|
| # otherwise request from user |
| 2360 |
|
| @indices = _request_indices($line); |
| 2361 |
|
| } |
| 2362 |
|
| unless (@indices) { |
| 2363 |
|
| print " WARNING: no valid indices. nothing done\n"; |
| 2364 |
|
| return 0; |
| 2365 |
|
| } |
| 2366 |
|
| |
| 2367 |
|
| # request the value to perform the mathematical function |
| 2368 |
|
| my $request_value; |
| 2369 |
|
| if ( defined $opt_target ) { |
| 2370 |
|
| |
| 2371 |
|
| # command line option |
| 2372 |
|
| $request_value = $opt_target; |
| 2373 |
|
| } |
| 2374 |
|
| else { |
| 2375 |
|
| # interactively ask the user |
| 2376 |
|
| my $p = " Enter the numeric value, 'mean', 'median', or 'sum' to $math: "; |
| 2377 |
|
| $request_value = prompt($p); |
| 2378 |
|
| } |
| 2379 |
|
| |
| 2380 |
|
| # request placement |
| 2381 |
|
| my $placement = _request_placement(); |
| 2382 |
|
| |
| 2383 |
|
| # determine the mathematical function |
| 2384 |
|
| # function is one of add, subtract, multiply, divide |
| 2385 |
|
| my $calculate; # a reference to the appropriate function |
| 2386 |
|
| if ( $math eq 'add' ) { |
| 2387 |
|
| $calculate = sub { |
| 2388 |
|
| return $_[0] + $_[1]; |
| 2389 |
|
| }; |
| 2390 |
|
| } |
| 2391 |
|
| elsif ( $math eq 'subtract' ) { |
| 2392 |
|
| $calculate = sub { |
| 2393 |
|
| return $_[0] - $_[1]; |
| 2394 |
|
| }; |
| 2395 |
|
| } |
| 2396 |
|
| elsif ( $math eq 'multiply' ) { |
| 2397 |
|
| $calculate = sub { |
| 2398 |
|
| return $_[0] * $_[1]; |
| 2399 |
|
| }; |
| 2400 |
|
| } |
| 2401 |
|
| elsif ( $math eq 'divide' ) { |
| 2402 |
|
| $calculate = sub { |
| 2403 |
|
| return $_[0] / $_[1]; |
| 2404 |
|
| }; |
| 2405 |
|
| } |
| 2406 |
|
| |
| 2407 |
|
| # generate past tense verb |
| 2408 |
|
| my $mathed = $math; |
| 2409 |
|
| $mathed =~ s/^(multipl) y $ / $1ied/x; |
| 2410 |
|
| $mathed =~ s/^(add)$ / $1ed/x; |
| 2411 |
|
| $mathed =~ s/^(divide)$ / $1d/x; |
| 2412 |
|
| $mathed =~ s/^(subtract)$ / $1ed/x; |
| 2413 |
|
| |
| 2414 |
|
| ## Process the datasets and subtract their values |
| 2415 |
|
| my $dataset_modification_count = 0; # a count of how many processed |
| 2416 |
|
| foreach my $index (@indices) { |
| 2417 |
|
| |
| 2418 |
|
| # determine the actual value to divide by |
| 2419 |
|
| my $value; # the actual number to divide by |
| 2420 |
|
| if ( $request_value and $request_value =~ /median | mean | sum/xi ) { |
| 2421 |
|
| |
| 2422 |
|
| # collect dataset statistics |
| 2423 |
|
| my @cv = $Data->column_values($index); |
| 2424 |
|
| shift @cv; # skip header |
| 2425 |
|
| my @values = grep { looks_like_number($_) } @cv; |
| 2426 |
|
| unless (@values) { |
| 2427 |
|
| printf " WARNING: no numeric values for index $index, %s\n", |
| 2428 |
|
| $Data->name($index); |
| 2429 |
|
| next; |
| 2430 |
|
| } |
| 2431 |
|
| |
| 2432 |
|
| # assign the appropriate value |
| 2433 |
|
| if ( $request_value eq 'median' ) { |
| 2434 |
|
| $value = median(@values); |
| 2435 |
|
| printf " median value for %s is $value\n", $Data->name($index); |
| 2436 |
|
| } |
| 2437 |
|
| elsif ( $request_value eq 'mean' ) { |
| 2438 |
|
| $value = sum0(@values) / scalar(@values); |
| 2439 |
|
| printf " mean value for %s is $value\n", $Data->name($index); |
| 2440 |
|
| } |
| 2441 |
|
| elsif ( $request_value eq 'sum' ) { |
| 2442 |
|
| $value = sum0(@values); |
| 2443 |
|
| printf " sum value for %s is $value\n", $Data->name($index); |
| 2444 |
|
| } |
| 2445 |
|
| } |
| 2446 |
|
| elsif ( $request_value and looks_like_number($request_value) ) { |
| 2447 |
|
| |
| 2448 |
|
| # a numeric value, allowing for negatives, decimals, exponents |
| 2449 |
|
| $value = $request_value; |
| 2450 |
|
| } |
| 2451 |
|
| else { |
| 2452 |
|
| print " WARNING: unrecognized value '$request_value'; nothing done\n"; |
| 2453 |
|
| return 0; |
| 2454 |
|
| } |
| 2455 |
|
| |
| 2456 |
|
| # generate subtraction product |
| 2457 |
|
| my $failed_count = 0; # failed count |
| 2458 |
|
| my $name = $Data->name($index); |
| 2459 |
|
| if ( $placement =~ /^n/i ) { |
| 2460 |
|
| $index = _prepare_new_destination( $index, "_$mathed\_$value" ); |
| 2461 |
|
| } |
| 2462 |
|
| $Data->iterate( |
| 2463 |
|
| sub { |
| 2464 |
|
| my $row = shift; |
| 2465 |
|
| my $v = $row->value($index); |
| 2466 |
|
| if ( looks_like_number($v) ) { |
| 2467 |
|
| $row->value( $index, &{$calculate}( $v, $value ) ); |
| 2468 |
|
| } |
| 2469 |
|
| else { |
| 2470 |
|
| $failed_count++; |
| 2471 |
|
| } |
| 2472 |
|
| } |
| 2473 |
|
| ); |
| 2474 |
|
| |
| 2475 |
|
| # print conclusion |
| 2476 |
|
| printf " dataset %s was %s by %s\n", $name, $mathed, $value; |
| 2477 |
|
| if ($failed_count) { |
| 2478 |
|
| printf " %d datapoints could not be %s\n", $failed_count, $mathed; |
| 2479 |
|
| } |
| 2480 |
|
| $dataset_modification_count++; |
| 2481 |
|
| } |
| 2482 |
|
| |
| 2483 |
|
| # done |
| 2484 |
|
| return $dataset_modification_count; |
| 2485 |
|
| } |
| 2486 |
|
| |
| 2487 |
|
| sub number_function { |
| 2488 |
|
| |
| 2489 |
|
| # This subroutine will number the datapoints or lines |
| 2490 |
|
| |
| 2491 |
|
| # request dataset |
| 2492 |
|
| my $line = |
| 2493 |
|
| " The numbers will be entered as a dataset in front of " |
| 2494 |
|
| . "which current dataset?\n Enter nothing to place the numbers " |
| 2495 |
|
| . "at the end. "; |
| 2496 |
|
| my $index = _request_index($line); |
| 2497 |
|
| |
| 2498 |
|
| # a return of -1 indicates the numbers will be put at the end |
| 2499 |
|
| |
| 2500 |
|
| # generate new column |
| 2501 |
|
| # we'll put the numbers at the end for now |
| 2502 |
|
| my $new_name; |
| 2503 |
|
| if ( $function and $opt_name ) { |
| 2504 |
|
| $new_name = $opt_name; |
| 2505 |
|
| } |
| 2506 |
|
| else { |
| 2507 |
|
| $new_name = 'Numbers'; |
| 2508 |
|
| } |
| 2509 |
|
| my $new_position = $Data->add_column($new_name); |
| 2510 |
|
| |
| 2511 |
|
| # put in numbers for each line |
| 2512 |
|
| $Data->iterate( |
| 2513 |
|
| sub { |
| 2514 |
|
| my $row = shift; |
| 2515 |
|
| $row->value( $new_position, $row->row_index ); |
| 2516 |
|
| } |
| 2517 |
|
| ); |
| 2518 |
|
| |
| 2519 |
|
| # Reorder the columns |
| 2520 |
|
| if ( $index >= 0 ) { |
| 2521 |
|
| my @new_order; |
| 2522 |
|
| for ( my $i = 0; $i < $new_position; $i++ ) { |
| 2523 |
|
| |
| 2524 |
|
| # we will walk through the list of index numbers sequentially |
| 2525 |
|
| # put each index into the new_order array |
| 2526 |
|
| # when we encounter the requested index to put the new dataset, |
| 2527 |
|
| # we'll put the new_position, and then the requested index |
| 2528 |
|
| if ( $i == $index ) { |
| 2529 |
|
| |
| 2530 |
|
| # we want to put the new Numbers dataset before this index |
| 2531 |
|
| push @new_order, $new_position; |
| 2532 |
|
| push @new_order, $i; |
| 2533 |
|
| } |
| 2534 |
|
| else { |
| 2535 |
|
| # otherwise, put the current index in the order |
| 2536 |
|
| push @new_order, $i; |
| 2537 |
|
| } |
| 2538 |
|
| } |
| 2539 |
|
| $Data->reorder_column(@new_order); |
| 2540 |
|
| } |
| 2541 |
|
| |
| 2542 |
|
| print " Lines numbered at column $index\n"; |
| 2543 |
|
| return 1; |
| 2544 |
|
| } |
| 2545 |
|
| |
| 2546 |
|
| sub filter_length_function { |
| 2547 |
|
| |
| 2548 |
|
| unless ( $Data->feature_type eq 'coordinate' or $Data->coord_column ) { |
| 2549 |
|
| print " Data table does not have coordinates to filter! Nothing done.\n"; |
| 2550 |
|
| return 0; |
| 2551 |
|
| } |
| 2552 |
|
| |
| 2553 |
|
| # identify the thresholds |
| 2554 |
|
| my ($min, $max); |
| 2555 |
|
| if ( defined $opt_target ) { |
| 2556 |
|
| |
| 2557 |
|
| # specified on the command line |
| 2558 |
|
| ($min, $max) = split /,|\-/, $opt_target, 2; |
| 2559 |
|
| } |
| 2560 |
|
| else { |
| 2561 |
|
| # interactively ask the user |
| 2562 |
|
| my $p = ' Enter the minimum,maximum range of lengths to keep: '; |
| 2563 |
|
| my $threshold = prompt($p); |
| 2564 |
|
| ($min, $max) = split /,|\-/, $threshold, 2; |
| 2565 |
|
| } |
| 2566 |
|
| unless ( defined $min and defined $max ) { |
| 2567 |
|
| print " Both minimum and maximum lengths must be defined. Nothing done.\n"; |
| 2568 |
|
| return 0; |
| 2569 |
|
| } |
| 2570 |
|
| |
| 2571 |
|
| # identify features to delete |
| 2572 |
|
| my @to_delete; |
| 2573 |
|
| $Data->iterate( sub { |
| 2574 |
|
| my $row = shift; |
| 2575 |
|
| my $length = $row->length; |
| 2576 |
|
| if ( $length < $min or $length > $max ) { |
| 2577 |
|
| push @to_delete, $row->row_index; |
| 2578 |
|
| } |
| 2579 |
|
| } ); |
| 2580 |
|
| if (@to_delete) { |
| 2581 |
|
| $Data->delete_row(@to_delete); |
| 2582 |
|
| } |
| 2583 |
|
| printf " %d rows were deleted\n", scalar(@to_delete); |
| 2584 |
|
| printf " %d rows with size %d-%d bp inclusive were retained\n", $Data->number_rows, |
| 2585 |
|
| $min, $max; |
| 2586 |
|
| return 1; |
| 2587 |
|
| } |
| 2588 |
|
| |
| 2589 |
|
| sub write_summary_function { |
| 2590 |
|
| |
| 2591 |
|
| # this will write out a summary file of the data |
| 2592 |
|
| |
| 2593 |
|
| # determine indices to summarize |
| 2594 |
|
| my ( $startcolumn, $stopcolumn, $method ); |
| 2595 |
|
| if ($function) { |
| 2596 |
|
| |
| 2597 |
|
| # running under automatic mode |
| 2598 |
|
| # check if user supplied indices |
| 2599 |
|
| if ( scalar @opt_indices >= 2 ) { |
| 2600 |
|
| |
| 2601 |
|
| # assume contiguous indices, use the first and last one |
| 2602 |
|
| # it may also be simply the start and stop indices |
| 2603 |
|
| $startcolumn = $opt_indices[0]; |
| 2604 |
|
| $stopcolumn = $opt_indices[-1]; |
| 2605 |
|
| } |
| 2606 |
|
| |
| 2607 |
|
| # otherwise the summary module will automatically deduce the columns |
| 2608 |
|
| |
| 2609 |
|
| if ($opt_target) { |
| 2610 |
|
| $method = $opt_target; |
| 2611 |
|
| } |
| 2612 |
|
| else { |
| 2613 |
|
| $method = 'mean'; |
| 2614 |
|
| } |
| 2615 |
|
| } |
| 2616 |
|
| else { |
| 2617 |
|
| # request indices only when running interactively and not automatically |
| 2618 |
|
| ( $startcolumn, $stopcolumn ) = _request_indices( |
| 2619 |
|
| " Enter the starting and/or ending indices of the datasets to summarize\n" |
| 2620 |
|
| . " Or nothing for automatic detection " ); |
| 2621 |
|
| my $p = " Enter the method [mean (default), trimmean, median]: "; |
| 2622 |
|
| $method = prompt($p); |
| 2623 |
|
| unless ($method) { |
| 2624 |
|
| $method = 'mean'; |
| 2625 |
|
| } |
| 2626 |
|
| } |
| 2627 |
|
| |
| 2628 |
|
| # write the summary |
| 2629 |
|
| my $sumfile = $Data->summary_file( |
| 2630 |
|
| 'filename' => $outfile, |
| 2631 |
|
| 'startcolumn' => $startcolumn, |
| 2632 |
|
| 'endcolumn' => $stopcolumn, |
| 2633 |
|
| 'method' => $method, |
| 2634 |
|
| ); |
| 2635 |
|
| |
| 2636 |
|
| # report outcome |
| 2637 |
|
| if ($sumfile) { |
| 2638 |
|
| print " wrote summary file '$sumfile'\n"; |
| 2639 |
|
| } |
| 2640 |
|
| else { |
| 2641 |
|
| print " unable to write summary file!\n"; |
| 2642 |
|
| } |
| 2643 |
|
| |
| 2644 |
|
| # since no changes have been made to the data structure, return 0 |
| 2645 |
|
| return 0; |
| 2646 |
|
| } |
| 2647 |
|
| |
| 2648 |
|
| sub export_function { |
| 2649 |
|
| |
| 2650 |
|
| # this will export the file into an even simpler text format |
| 2651 |
|
| |
| 2652 |
|
| # generate a possible new name based on the input name |
| 2653 |
|
| my $possible_name = $Data->path . $Data->basename . '.tsv'; |
| 2654 |
|
| |
| 2655 |
|
| # determine the export file name |
| 2656 |
|
| my $outfilename; |
| 2657 |
|
| if ( defined $outfile ) { |
| 2658 |
|
| |
| 2659 |
|
| # use the outfile name specified on the command line |
| 2660 |
|
| # note that this could be overwritten later if $modification > 0 |
| 2661 |
|
| # but to allow for automated execution, we can't ask the user |
| 2662 |
|
| # for verification |
| 2663 |
|
| $outfilename = $outfile; |
| 2664 |
|
| } |
| 2665 |
|
| elsif ($function) { |
| 2666 |
|
| |
| 2667 |
|
| # automatic execution, don't ask the user |
| 2668 |
|
| $outfilename = $possible_name; |
| 2669 |
|
| } |
| 2670 |
|
| else { |
| 2671 |
|
| # ask for new filename |
| 2672 |
|
| my $p = " Provide an exported file name [$possible_name] "; |
| 2673 |
|
| $outfilename = prompt( $p, $possible_name ); |
| 2674 |
|
| } |
| 2675 |
|
| |
| 2676 |
|
| # write the file |
| 2677 |
|
| my $write_results = $Data->write_file( |
| 2678 |
|
| 'filename' => $outfilename, |
| 2679 |
|
| 'gz' => $gz, |
| 2680 |
|
| 'simple' => 1, |
| 2681 |
|
| ); |
| 2682 |
|
| |
| 2683 |
|
| # report write results |
| 2684 |
|
| if ($write_results) { |
| 2685 |
|
| print " Exported data to simple text file '$write_results'\n"; |
| 2686 |
|
| } |
| 2687 |
|
| else { |
| 2688 |
|
| print " Unable to export data to file!\n"; |
| 2689 |
|
| } |
| 2690 |
|
| |
| 2691 |
|
| # since no changes have been made to the data structure, return |
| 2692 |
|
| return 0; |
| 2693 |
|
| } |
| 2694 |
|
| |
| 2695 |
|
| sub center_function { |
| 2696 |
|
| |
| 2697 |
|
| # this will center the datapoints in each row |
| 2698 |
|
| |
| 2699 |
|
| # identify the datasets to be centered |
| 2700 |
|
| my @datasets; |
| 2701 |
|
| if (@_) { |
| 2702 |
|
| |
| 2703 |
|
| # provided from an internal subroutine |
| 2704 |
|
| @datasets = @_; |
| 2705 |
|
| } |
| 2706 |
|
| else { |
| 2707 |
|
| # otherwise request from user |
| 2708 |
|
| @datasets = |
| 2709 |
|
| _request_indices(" Enter the range of dataset indices to be centered "); |
| 2710 |
|
| } |
| 2711 |
|
| unless (@datasets) { |
| 2712 |
|
| print " WARNING: Unknown datasets. Nothing done.\n"; |
| 2713 |
|
| return 0; |
| 2714 |
|
| } |
| 2715 |
|
| |
| 2716 |
|
| # Center the data |
| 2717 |
|
| # We will determine the median value for all the datapoints in the |
| 2718 |
|
| # indicated datasets for each feature (row). Then the median value will |
| 2719 |
|
| # be subtracted from each value. The median value of the adjusted |
| 2720 |
|
| # datapoints should then be effectively 0. |
| 2721 |
|
| $Data->iterate( |
| 2722 |
|
| sub { |
| 2723 |
|
| my $row = shift; |
| 2724 |
|
| |
| 2725 |
|
| # collect the datapoint values in each dataset for the feature |
| 2726 |
|
| my @values; |
| 2727 |
|
| foreach my $d (@datasets) { |
| 2728 |
|
| my $v = $row->value($d); |
| 2729 |
|
| if ( looks_like_number($v) ) { |
| 2730 |
|
| push @values, $v; |
| 2731 |
|
| } |
| 2732 |
|
| } |
| 2733 |
|
| next unless (@values); |
| 2734 |
|
| |
| 2735 |
|
| # determine median value |
| 2736 |
|
| my $median_value = median(@values); |
| 2737 |
|
| |
| 2738 |
|
| # adjust the datapoints |
| 2739 |
|
| foreach my $d (@datasets) { |
| 2740 |
|
| my $v = $row->value($d); |
| 2741 |
|
| next unless looks_like_number($v); |
| 2742 |
|
| $row->value( $d, ( $v - $median_value ) ); |
| 2743 |
|
| } |
| 2744 |
|
| } |
| 2745 |
|
| ); |
| 2746 |
|
| |
| 2747 |
|
| # annotate metadata |
| 2748 |
|
| foreach my $d (@datasets) { |
| 2749 |
|
| $Data->metadata( $d, 'centered', 'median' ); |
| 2750 |
|
| } |
| 2751 |
|
| |
| 2752 |
|
| # print conclusion |
| 2753 |
|
| printf " Datasets %s median centered\n", join( ", ", @datasets ); |
| 2754 |
|
| return 1; |
| 2755 |
|
| } |
| 2756 |
|
| |
| 2757 |
|
| sub new_column_function { |
| 2758 |
|
| |
| 2759 |
|
| # this will generate a new dataset |
| 2760 |
|
| |
| 2761 |
|
| # request column name |
| 2762 |
|
| my $name; |
| 2763 |
|
| if ( defined $opt_name ) { |
| 2764 |
|
| $name = $opt_name; |
| 2765 |
|
| } |
| 2766 |
|
| elsif ($function) { |
| 2767 |
|
| $name = "$function\_Column"; |
| 2768 |
|
| } |
| 2769 |
|
| else { |
| 2770 |
|
| |
| 2771 |
|
| # interactively ask the user |
| 2772 |
|
| $name = $function ? "$function\_Column" : 'New'; |
| 2773 |
|
| my $p = " Enter the name for the new column [$name]: "; |
| 2774 |
|
| $name = prompt( $p, $name ); |
| 2775 |
|
| } |
| 2776 |
|
| |
| 2777 |
|
| # request value |
| 2778 |
|
| my $value; |
| 2779 |
|
| if ( defined $opt_target ) { |
| 2780 |
|
| $value = $opt_target; |
| 2781 |
|
| } |
| 2782 |
|
| elsif ($function) { |
| 2783 |
|
| $value = '.'; # null |
| 2784 |
|
| } |
| 2785 |
|
| else { |
| 2786 |
|
| # interactively ask the user |
| 2787 |
|
| my $p = ' Enter the common value to be assigned in the new column: '; |
| 2788 |
|
| $value = prompt($p); |
| 2789 |
|
| } |
| 2790 |
|
| |
| 2791 |
|
| # generate the new dataset |
| 2792 |
|
| my $new_position = $Data->add_column($name); |
| 2793 |
|
| $Data->iterate( |
| 2794 |
|
| sub { |
| 2795 |
|
| shift->value( $new_position, $value ); |
| 2796 |
|
| } |
| 2797 |
|
| ); |
| 2798 |
|
| |
| 2799 |
|
| # done |
| 2800 |
|
| print " Added new dataset '$name' at index $new_position with value '$value'\n"; |
| 2801 |
|
| return 1; |
| 2802 |
|
| } |
| 2803 |
|
| |
| 2804 |
|
| sub addname_function { |
| 2805 |
|
| |
| 2806 |
|
| # this will add a name column and uniquely name the rows |
| 2807 |
|
| |
| 2808 |
|
| # request column name |
| 2809 |
|
| my $prefix = $Data->feature || 'feature'; |
| 2810 |
|
| if ( defined $opt_target ) { |
| 2811 |
|
| $prefix = $opt_target; |
| 2812 |
|
| } |
| 2813 |
|
| elsif ( not $function ) { |
| 2814 |
|
| |
| 2815 |
|
| # interactively ask the user |
| 2816 |
|
| my $p = " Enter the name prefix for each feature [$prefix]: "; |
| 2817 |
|
| $prefix = prompt( $p, $prefix ); |
| 2818 |
|
| } |
| 2819 |
|
| |
| 2820 |
|
| # Identify column |
| 2821 |
|
| # look specifically for the "name" column as the name_column function can |
| 2822 |
|
| # find look-alike names, like geneName, leading to disastrous consequences |
| 2823 |
|
| my $idx = $Data->find_column('^name$'); |
| 2824 |
|
| if ( defined $idx ) { |
| 2825 |
|
| printf " Replacing name contents in column %d, '%s'\n", $idx, $Data->name($idx); |
| 2826 |
|
| } |
| 2827 |
|
| else { |
| 2828 |
|
| |
| 2829 |
|
| # we are adding a new column |
| 2830 |
|
| my $name; |
| 2831 |
|
| if ( defined $opt_name ) { |
| 2832 |
|
| $name = $opt_name; |
| 2833 |
|
| } |
| 2834 |
|
| else { |
| 2835 |
|
| $name = 'Name'; |
| 2836 |
|
| } |
| 2837 |
|
| $idx = $Data->add_column($name); |
| 2838 |
|
| printf " Adding feature names to column %d, '%s'\n", $idx, $name; |
| 2839 |
|
| } |
| 2840 |
|
| |
| 2841 |
|
| # generate the new names |
| 2842 |
|
| my $number = 1; |
| 2843 |
|
| $Data->iterate( |
| 2844 |
|
| sub { |
| 2845 |
|
| shift->value( $idx, sprintf( "%s%d", $prefix, $number ) ); |
| 2846 |
|
| $number++; |
| 2847 |
|
| } |
| 2848 |
|
| ); |
| 2849 |
|
| |
| 2850 |
|
| # done |
| 2851 |
|
| return 1; |
| 2852 |
|
| } |
| 2853 |
|
| |
| 2854 |
|
| sub print_length_statistics_function { |
| 2855 |
|
| |
| 2856 |
|
| unless ( $Data->feature_type eq 'coordinate' or $Data->coord_column ) { |
| 2857 |
|
| print " Data table does not have coordinates to calculate lengths\n"; |
| 2858 |
|
| return 0; |
| 2859 |
|
| } |
| 2860 |
|
| my @lengths; |
| 2861 |
|
| $Data->iterate( sub { |
| 2862 |
|
| push @lengths, shift->length; |
| 2863 |
|
| } ); |
| 2864 |
|
| |
| 2865 |
|
| # calculate and print the statistics |
| 2866 |
|
| print " Statistics on intervals lengths:\n"; |
| 2867 |
|
| my $sum = sum0(@lengths); |
| 2868 |
|
| printf " count = %d\n", scalar(@lengths); |
| 2869 |
|
| printf " mean = %.1f\n", $sum / ( scalar(@lengths) || 1 ); |
| 2870 |
|
| printf " median = %.1f\n", median(@lengths); |
| 2871 |
|
| printf " std dev = %.1f\n", stddevp(@lengths); |
| 2872 |
|
| printf " min = %d\n", min(@lengths); |
| 2873 |
|
| printf " max = %d\n", max(@lengths); |
| 2874 |
|
| printf " mode = %d\n", mode(@lengths); |
| 2875 |
|
| printf " sum = %d\n", $sum; |
| 2876 |
|
| return 0; |
| 2877 |
|
| } |
| 2878 |
|
| |
| 2879 |
|
| sub rewrite_function { |
| 2880 |
|
| |
| 2881 |
|
| # check output file name |
| 2882 |
|
| my $rewrite_filename; |
| 2883 |
|
| if ( defined $outfile ) { |
| 2884 |
|
| |
| 2885 |
|
| # use the defined outfile name given |
| 2886 |
|
| $rewrite_filename = $outfile; |
| 2887 |
|
| } |
| 2888 |
|
| else { |
| 2889 |
|
| # ask the user for a new name |
| 2890 |
|
| my $p = " Enter a new file name [$infile] "; |
| 2891 |
|
| $rewrite_filename = prompt( $p, $infile ); |
| 2892 |
|
| } |
| 2893 |
|
| |
| 2894 |
|
| # write the file |
| 2895 |
|
| my $write_results = $Data->write_file( |
| 2896 |
|
| 'filename' => $rewrite_filename, |
| 2897 |
|
| 'gz' => $gz, |
| 2898 |
|
| ); |
| 2899 |
|
| |
| 2900 |
|
| # report write results |
| 2901 |
|
| if ($write_results) { |
| 2902 |
|
| print " Wrote datafile $write_results\n"; |
| 2903 |
|
| } |
| 2904 |
|
| else { |
| 2905 |
|
| print " Unable to re-write to file '$rewrite_filename'!\n"; |
| 2906 |
|
| } |
| 2907 |
|
| |
| 2908 |
|
| return 0; |
| 2909 |
|
| } |
| 2910 |
|
| |
| 2911 |
|
| sub view_function { |
| 2912 |
|
| |
| 2913 |
|
| # print header |
| 2914 |
|
| printf "%s\n", join( q( ), $Data->list_columns ); |
| 2915 |
|
| |
| 2916 |
|
| # print the contents |
| 2917 |
|
| my $start_row = 1; |
| 2918 |
|
| my $stop_row = 10; |
| 2919 |
|
| while ($stop_row) { |
| 2920 |
|
| |
| 2921 |
|
| # print the table contents |
| 2922 |
|
| $stop_row = $Data->last_row if $stop_row > $Data->last_row; |
| 2923 |
|
| for my $i ( $start_row .. $stop_row ) { |
| 2924 |
|
| printf "%s\n", join( q( ), $Data->row_values($i) ); |
| 2925 |
|
| } |
| 2926 |
|
| |
| 2927 |
|
| # continue or return the next menu response |
| 2928 |
|
| my $p = "\nPress Return for next 10 lines, or enter a (m)enu command "; |
| 2929 |
|
| my $answer = prompt( $p, q() ); |
| 2930 |
|
| if ($answer) { |
| 2931 |
|
| if ( exists $letter_to_function{$answer} ) { |
| 2932 |
|
| $modification += |
| 2933 |
|
| &{ $function_to_subroutine{ $letter_to_function{$answer} } }; |
| 2934 |
|
| } |
| 2935 |
|
| else { |
| 2936 |
|
| print "unkown response\n"; |
| 2937 |
|
| } |
| 2938 |
|
| return 0; |
| 2939 |
|
| } |
| 2940 |
|
| else { |
| 2941 |
|
| # print next 10 lines |
| 2942 |
|
| $start_row += 10; |
| 2943 |
|
| $stop_row += 10; |
| 2944 |
|
| } |
| 2945 |
|
| } |
| 2946 |
|
| return 0; |
| 2947 |
|
| } |
| 2948 |
|
| |
| 2949 |
|
| ################################################################################ |
| 2950 |
|
| ############## Internal Subroutines ############## |
| 2951 |
|
| ################################################################################ |
| 2952 |
|
| |
| 2953 |
|
| sub _get_letter_to_function_hash { |
| 2954 |
|
| |
| 2955 |
|
| # this hash converts the one-letter menu key to the function name |
| 2956 |
|
| # the key is the menu letter |
| 2957 |
|
| # the value is the function name |
| 2958 |
|
| return ( |
| 2959 |
|
| 't' => 'stat', |
| 2960 |
|
| 'k' => 'lengthstat', |
| 2961 |
|
| 'R' => 'reorder', |
| 2962 |
|
| 'D' => 'delete', |
| 2963 |
|
| 'n' => 'rename', |
| 2964 |
|
| 'b' => 'number', |
| 2965 |
|
| 'C' => 'concatenate', |
| 2966 |
|
| 'T' => 'split', |
| 2967 |
|
| 'O' => 'coordinate', |
| 2968 |
|
| 'o' => 'sort', |
| 2969 |
|
| 'g' => 'gsort', |
| 2970 |
|
| 'N' => 'null', |
| 2971 |
|
| 'P' => 'duplicate', |
| 2972 |
|
| 'A' => 'above', |
| 2973 |
|
| 'B' => 'below', |
| 2974 |
|
| 'S' => 'specific', |
| 2975 |
|
| 'K' => 'keep', |
| 2976 |
|
| 'E' => 'lengthfilt', |
| 2977 |
|
| 'M' => 'addname', |
| 2978 |
|
| 'U' => 'cnull', |
| 2979 |
|
| 'G' => 'absolute', |
| 2980 |
|
| 'I' => 'minimum', |
| 2981 |
|
| 'X' => 'maximum', |
| 2982 |
|
| 'a' => 'add', |
| 2983 |
|
| 'u' => 'subtract', |
| 2984 |
|
| 'y' => 'multiply', |
| 2985 |
|
| 'v' => 'divide', |
| 2986 |
|
| 's' => 'scale', |
| 2987 |
|
| 'p' => 'pr', |
| 2988 |
|
| 'Z' => 'zscore', |
| 2989 |
|
| 'l' => 'log', |
| 2990 |
|
| 'L' => 'delog', |
| 2991 |
|
| 'f' => 'format', |
| 2992 |
|
| 'c' => 'combine', |
| 2993 |
|
| 'r' => 'ratio', |
| 2994 |
|
| 'd' => 'diff', |
| 2995 |
|
| 'e' => 'center', |
| 2996 |
|
| 'w' => 'new', |
| 2997 |
|
| 'Y' => 'summary', |
| 2998 |
|
| 'x' => 'export', |
| 2999 |
|
| 'W' => 'rewrite', |
| 3000 |
|
| 'h' => 'help', |
| 3001 |
|
| 'V' => 'view', |
| 3002 |
|
| 'q' => 'write_quit', |
| 3003 |
|
| 'Q' => 'quit', |
| 3004 |
|
| 'm' => 'menu', |
| 3005 |
|
| ); |
| 3006 |
|
| } |
| 3007 |
|
| |
| 3008 |
|
| sub _get_function_to_subroutine_hash { |
| 3009 |
|
| |
| 3010 |
|
| # this hash converts the function name to the actual subroutine for the function |
| 3011 |
|
| # the key is the function name |
| 3012 |
|
| # the value is a scalar reference to the subroutine |
| 3013 |
|
| return ( |
| 3014 |
|
| 'stat' => \&print_statistics_function, |
| 3015 |
|
| 'lengthstat' => \&print_length_statistics_function, |
| 3016 |
|
| 'reorder' => \&reorder_function, |
| 3017 |
|
| 'delete' => \&delete_function, |
| 3018 |
|
| 'rename' => \&rename_function, |
| 3019 |
|
| 'number' => \&number_function, |
| 3020 |
|
| 'concatenate' => \&concatenate_function, |
| 3021 |
|
| 'split' => \&split_function, |
| 3022 |
|
| 'coordinate' => \&coordinate_function, |
| 3023 |
|
| 'sort' => \&sort_function, |
| 3024 |
|
| 'gsort' => \&genomic_sort_function, |
| 3025 |
|
| 'null' => \&toss_nulls_function, |
| 3026 |
|
| 'duplicate' => \&toss_duplicates_function, |
| 3027 |
|
| 'above' => \&toss_above_threshold_function, |
| 3028 |
|
| 'below' => \&toss_below_threshold_function, |
| 3029 |
|
| 'specific' => \&toss_specific_values_function, |
| 3030 |
|
| 'keep' => \&keep_specific_values_function, |
| 3031 |
|
| 'lengthfilt' => \&filter_length_function, |
| 3032 |
|
| 'addname' => \&addname_function, |
| 3033 |
|
| 'cnull' => \&convert_nulls_function, |
| 3034 |
|
| 'absolute' => \&convert_absolute_function, |
| 3035 |
|
| 'minimum' => \&minimum_function, |
| 3036 |
|
| 'maximum' => \&maximum_function, |
| 3037 |
|
| 'add' => \&add_function, |
| 3038 |
|
| 'subtract' => \&subtract_function, |
| 3039 |
|
| 'multiply' => \&multiply_function, |
| 3040 |
|
| 'divide' => \÷_function, |
| 3041 |
|
| 'scale' => \&median_scale_function, |
| 3042 |
|
| 'pr' => \&percentile_rank_function, |
| 3043 |
|
| 'zscore' => \&zscore_function, |
| 3044 |
|
| 'log' => \&log_function, |
| 3045 |
|
| 'log2' => \&log_function, # holdover from previous |
| 3046 |
|
| 'delog' => \&delog_function, |
| 3047 |
|
| 'delog2' => \&delog_function, |
| 3048 |
|
| 'format' => \&format_function, |
| 3049 |
|
| 'combine' => \&combine_function, |
| 3050 |
|
| 'ratio' => \&ratio_function, |
| 3051 |
|
| 'diff' => \&difference_function, |
| 3052 |
|
| 'center' => \¢er_function, |
| 3053 |
|
| 'new' => \&new_column_function, |
| 3054 |
|
| 'summary' => \&write_summary_function, |
| 3055 |
|
| 'export' => \&export_function, |
| 3056 |
|
| 'rewrite' => \&rewrite_function, |
| 3057 |
|
| 'view' => \&view_function, |
| 3058 |
|
| 'help' => \&print_online_help, |
| 3059 |
|
| 'menu' => \&print_menu, |
| 3060 |
|
| 'write_quit' => \&write_and_quit_function, |
| 3061 |
|
| 'quit' => \&quit_function, |
| 3062 |
|
| ); |
| 3063 |
|
| } |
| 3064 |
|
| |
| 3065 |
|
| sub _request_index { |
| 3066 |
|
| |
| 3067 |
|
| # this subroutine will determine which dataset index to use |
| 3068 |
|
| # if index is specified on the command line, that will be used |
| 3069 |
|
| # alternatively, it will ask the user which dataset to process. |
| 3070 |
|
| # it will return the index number |
| 3071 |
|
| my $line = shift; # the custom request line to give the user |
| 3072 |
|
| if (@opt_indices) { |
| 3073 |
|
| |
| 3074 |
|
| # index array is specified on the command line |
| 3075 |
|
| # use the first element in the global index array |
| 3076 |
|
| my $index = $opt_indices[0]; |
| 3077 |
|
| unless ( _validate_index_list($index) ) { |
| 3078 |
|
| return -1; # error value |
| 3079 |
|
| } |
| 3080 |
|
| return $index; |
| 3081 |
|
| } |
| 3082 |
|
| else { |
| 3083 |
|
| # request interactively from the user |
| 3084 |
|
| my $index = ask_user_for_index( $Data, $line ); |
| 3085 |
|
| unless ( defined $index ) { |
| 3086 |
|
| |
| 3087 |
|
| # return an error value |
| 3088 |
|
| return -1; |
| 3089 |
|
| } |
| 3090 |
|
| return $index; |
| 3091 |
|
| } |
| 3092 |
|
| } |
| 3093 |
|
| |
| 3094 |
|
| sub _request_indices { |
| 3095 |
|
| |
| 3096 |
|
| # this subroutine will determine which datasets are to be used |
| 3097 |
|
| # if the indices are specified on the command line, those will be used |
| 3098 |
|
| # alternatively, it will ask the user for the indices interactively |
| 3099 |
|
| my $line = shift; # the custom request line to give the user |
| 3100 |
|
| |
| 3101 |
|
| # get list of indices |
| 3102 |
|
| if (@opt_indices) { |
| 3103 |
|
| |
| 3104 |
|
| # index array is specified on the command line |
| 3105 |
|
| my @indices = @opt_indices; |
| 3106 |
|
| unless ( _validate_index_list(@indices) ) { |
| 3107 |
|
| return; |
| 3108 |
|
| } |
| 3109 |
|
| return @indices; |
| 3110 |
|
| } |
| 3111 |
|
| else { |
| 3112 |
|
| # request interactively from the user |
| 3113 |
|
| return ask_user_for_index( $Data, $line ); |
| 3114 |
|
| } |
| 3115 |
|
| } |
| 3116 |
|
| |
| 3117 |
|
| sub _request_placement { |
| 3118 |
|
| |
| 3119 |
|
| # this subroutine will determine where to put a new dataset |
| 3120 |
|
| # replace current or generate new |
| 3121 |
|
| |
| 3122 |
|
| my $placement; |
| 3123 |
|
| if ( defined $opt_placement ) { |
| 3124 |
|
| $placement = $opt_placement; |
| 3125 |
|
| } |
| 3126 |
|
| else { |
| 3127 |
|
| # request placement from user |
| 3128 |
|
| my $p = ' (r)eplace dataset or generate (n)ew dataset?: '; |
| 3129 |
|
| $placement = prompt($p); |
| 3130 |
|
| } |
| 3131 |
|
| |
| 3132 |
|
| return $placement; |
| 3133 |
|
| } |
| 3134 |
|
| |
| 3135 |
|
| sub _prepare_new_destination { |
| 3136 |
|
| |
| 3137 |
|
| # User requested new column placement |
| 3138 |
|
| my ( $index, $suffix ) = @_; |
| 3139 |
|
| |
| 3140 |
|
| my $new_index = $Data->copy_column($index); |
| 3141 |
|
| my $new_name; |
| 3142 |
|
| if ( $function and $opt_name ) { |
| 3143 |
|
| |
| 3144 |
|
| # automatic execution and new name was specifically given |
| 3145 |
|
| $new_name = $opt_name; |
| 3146 |
|
| } |
| 3147 |
|
| else { |
| 3148 |
|
| $new_name = $Data->name($index) . $suffix; |
| 3149 |
|
| } |
| 3150 |
|
| $Data->name( $new_index, $new_name ); |
| 3151 |
|
| |
| 3152 |
|
| return $new_index; |
| 3153 |
|
| } |
| 3154 |
|
| |
| 3155 |
|
| sub _validate_index_list { |
| 3156 |
|
| |
| 3157 |
|
| # this subroutine will ensure that the list of index numbers represent |
| 3158 |
|
| # actual columns, i.e., is each number < than the number of datasets in |
| 3159 |
|
| # the data table |
| 3160 |
|
| |
| 3161 |
|
| foreach my $num (@_) { |
| 3162 |
|
| |
| 3163 |
|
| # check that each number represents a metadata hash |
| 3164 |
|
| # and presumably dataset |
| 3165 |
|
| unless ( $num =~ /^\d+$/ and $num <= $Data->number_columns ) { |
| 3166 |
|
| print " WARNING: requested index number '$num' is not valid!\n"; |
| 3167 |
|
| return; |
| 3168 |
|
| } |
| 3169 |
|
| } |
| 3170 |
|
| return 1; |
| 3171 |
|
| } |
| 3172 |
|
| |
| 3173 |
|
| sub _get_statistics_hash { |
| 3174 |
|
| |
| 3175 |
|
| # internal subroutine to get statistics |
| 3176 |
|
| |
| 3177 |
|
| my ( $index, $zero ) = @_; |
| 3178 |
|
| |
| 3179 |
|
| # the index number (column) of the dataset |
| 3180 |
|
| # the exception rule to work with 0 numbers |
| 3181 |
|
| unless ( defined $zero ) { |
| 3182 |
|
| |
| 3183 |
|
| # use global command-line specified value if present |
| 3184 |
|
| $zero = $opt_zero; |
| 3185 |
|
| } |
| 3186 |
|
| |
| 3187 |
|
| # collect the values in the dataset |
| 3188 |
|
| my @invalues = $Data->column_values($index); |
| 3189 |
|
| shift @invalues; # remove header |
| 3190 |
|
| my @goodvalues; |
| 3191 |
|
| foreach my $v (@invalues) { |
| 3192 |
|
| $v =~ s/[, ]+//g; |
| 3193 |
|
| next unless looks_like_number($v); |
| 3194 |
|
| if ( $v == 0 ) { |
| 3195 |
|
| |
| 3196 |
|
| # we need to determine whether we can accept 0 values |
| 3197 |
|
| unless ( defined $zero ) { |
| 3198 |
|
| |
| 3199 |
|
| # wasn't defined on the command line, so stop the program and ask the user |
| 3200 |
|
| my $p = ' Include zero values in the statistics? y/n (default y): '; |
| 3201 |
|
| my $answer = prompt( $p, 'y' ); |
| 3202 |
|
| if ( lc $answer eq 'y' ) { |
| 3203 |
|
| $zero = 1; |
| 3204 |
|
| } |
| 3205 |
|
| elsif ( lc $answer eq 'n' ) { |
| 3206 |
|
| $zero = 0; |
| 3207 |
|
| } |
| 3208 |
|
| else { |
| 3209 |
|
| print " WARNING: unrecognized answer! defaulting to y\n"; |
| 3210 |
|
| $zero = 0; |
| 3211 |
|
| } |
| 3212 |
|
| |
| 3213 |
|
| # remember for next time, chances are user may still want this |
| 3214 |
|
| # value again in the future |
| 3215 |
|
| $opt_zero = $zero; |
| 3216 |
|
| } |
| 3217 |
|
| next unless $zero; |
| 3218 |
|
| } |
| 3219 |
|
| push @goodvalues, $v; |
| 3220 |
|
| } |
| 3221 |
|
| |
| 3222 |
|
| # check that we have values |
| 3223 |
|
| unless (@goodvalues) { |
| 3224 |
|
| print " WARNING: no valid values collected from dataset index '$index'!"; |
| 3225 |
|
| return; |
| 3226 |
|
| } |
| 3227 |
|
| |
| 3228 |
|
| # calculate the statistics hash |
| 3229 |
|
| my $sum = sum0(@goodvalues); |
| 3230 |
|
| my %statshash = ( |
| 3231 |
|
| 'count' => scalar(@goodvalues), |
| 3232 |
|
| 'sum' => $sum, |
| 3233 |
|
| 'mean' => ( $sum / ( scalar(@goodvalues) || 1 ) ), |
| 3234 |
|
| 'min' => min(@goodvalues), |
| 3235 |
|
| 'max' => max(@goodvalues), |
| 3236 |
|
| 'mode' => mode(@goodvalues), |
| 3237 |
|
| 'stddevp' => stddevp(@goodvalues), |
| 3238 |
|
| 'median' => median(@goodvalues) |
| 3239 |
|
| ); |
| 3240 |
|
| |
| 3241 |
|
| return %statshash; |
| 3242 |
|
| } |
| 3243 |
|
| |
| 3244 |
|
| sub _is_null { |
| 3245 |
|
| my $v = shift; |
| 3246 |
|
| return 1 if not defined $v; |
| 3247 |
|
| return 1 if ( $v eq '.' or $v =~ /^(?: n\/?a | nan | \-? inf )$/xi ); |
| 3248 |
|
| return 0; |
| 3249 |
|
| } |
| 3250 |
|
| |
| 3251 |
|
| __END__ |