| 1 |
|
| #!/usr/local/bin/perl |
| 2 |
|
| |
| 3 |
|
| # documentation at end of file |
| 4 |
|
| |
| 5 |
|
| use warnings; |
| 6 |
|
| use strict; |
| 7 |
|
| use Getopt::Long qw(:config no_ignore_case bundling); |
| 8 |
|
| use Pod::Usage; |
| 9 |
|
| use List::Util qw(uniqstr); |
| 10 |
|
| use IO::Prompt::Tiny qw(prompt); |
| 11 |
|
| use Bio::ToolBox::Data; |
| 12 |
|
| |
| 13 |
|
| our $VERSION = '2.03'; |
| 14 |
|
| |
| 15 |
|
| print "\n A progam to merge datasets from two files\n"; |
| 16 |
|
| |
| 17 |
|
| # Print help |
| 18 |
|
| unless (@ARGV) { |
| 19 |
|
| |
| 20 |
|
| # when no command line options are present |
| 21 |
|
| # print SYNOPSIS |
| 22 |
|
| pod2usage( |
| 23 |
|
| { |
| 24 |
|
| '-verbose' => 0, |
| 25 |
|
| '-exitval' => 1, |
| 26 |
|
| } |
| 27 |
|
| ); |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
|
| ### Get command line options and initialize values |
| 31 |
|
| my ( |
| 32 |
|
| $lookup, $automatic, $manual, |
| 33 |
|
| $user_lookup_name, $use_coordinate, $outfile, |
| 34 |
|
| $gz, $help, $print_version, |
| 35 |
|
| ); |
| 36 |
|
| my @order_requests; |
| 37 |
|
| |
| 38 |
|
| # Command line options |
| 39 |
|
| GetOptions( |
| 40 |
|
| 'l|lookup!' => \$lookup, # force merging by value lookup |
| 41 |
|
| 'a|auto!' => \$automatic, # select columns automatically |
| 42 |
|
| 'm|manual!' => \$manual, # always run interactively |
| 43 |
|
| 'n|lookupname|lun=s' => \$user_lookup_name, # alternate lookup column name |
| 44 |
|
| 'c|coordinate!' => \$use_coordinate, # use coordinates for lookup |
| 45 |
|
| 'x|index=s' => \@order_requests, # determine order in advance |
| 46 |
|
| 'o|out=s' => \$outfile, # name of output file |
| 47 |
|
| 'z|gz!' => \$gz, # compress output |
| 48 |
|
| 'h|help' => \$help, # request help |
| 49 |
|
| 'v|version' => \$print_version, # print the version |
| 50 |
|
| ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; |
| 51 |
|
| |
| 52 |
|
| # Print help |
| 53 |
|
| if ($help) { |
| 54 |
|
| |
| 55 |
|
| # print entire POD |
| 56 |
|
| pod2usage( |
| 57 |
|
| { |
| 58 |
|
| '-verbose' => 2, |
| 59 |
|
| '-exitval' => 1, |
| 60 |
|
| } |
| 61 |
|
| ); |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| # Print version |
| 65 |
|
| if ($print_version) { |
| 66 |
|
| print " Biotoolbox script merge_datasets.pl, version $VERSION\n"; |
| 67 |
|
| eval { |
| 68 |
|
| require Bio::ToolBox; |
| 69 |
|
| my $v = Bio::ToolBox->VERSION; |
| 70 |
|
| print " Biotoolbox package version $v\n"; |
| 71 |
|
| }; |
| 72 |
|
| exit; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| ### Check for requirements and set defaults |
| 76 |
|
| |
| 77 |
|
| # input files |
| 78 |
|
| unless ( scalar @ARGV >= 2 ) { |
| 79 |
|
| print STDERR " FATAL: At least two input files are required!\n"; |
| 80 |
|
| exit 1; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| # user provided index list |
| 84 |
|
| my $user_list = @order_requests ? 1 : 0; |
| 85 |
|
| |
| 86 |
|
| # automatic mode |
| 87 |
|
| if ( $automatic and $manual ) { |
| 88 |
|
| print STDERR " FATAL: Cannot specify both manual and automatic at the same time!\n"; |
| 89 |
|
| exit 1; |
| 90 |
|
| } |
| 91 |
|
| if ( $automatic or $user_list ) { |
| 92 |
|
| unless ($outfile) { |
| 93 |
|
| print STDERR " FATAL: Must provide an output file name in automatic mode!\n"; |
| 94 |
|
| exit 1; |
| 95 |
|
| } |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
|
| # Get the alphabet-number conversion hashes |
| 99 |
|
| my ( $letter_of, $number_of ) = get_number_letters(); # convert numbers into letters |
| 100 |
|
| |
| 101 |
|
| # Set up output |
| 102 |
|
| my $output_data; # the reference scalar for the output data structure |
| 103 |
|
| |
| 104 |
|
| # name of lookup column to be used for all files |
| 105 |
|
| my $lookup_name; |
| 106 |
|
| my $output_lookup_i; |
| 107 |
|
| if ($use_coordinate) { |
| 108 |
|
| $lookup_name = 'MergeDatasetCoordinate'; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| ### Process and merge the files |
| 112 |
|
| |
| 113 |
|
| # exactly two filenames are provided |
| 114 |
|
| if ( scalar @ARGV == 2 ) { |
| 115 |
|
| my $input_data1 = read_file( shift @ARGV ); |
| 116 |
|
| my $input_data2 = read_file( shift @ARGV ); |
| 117 |
|
| merge_two_datasets( $input_data1, $input_data2 ); |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| # more than two filenames |
| 121 |
|
| else { |
| 122 |
|
| # merge the first two |
| 123 |
|
| my $input_data1 = read_file( shift @ARGV ); |
| 124 |
|
| my $input_data2 = read_file( shift @ARGV ); |
| 125 |
|
| merge_two_datasets( $input_data1, $input_data2 ); |
| 126 |
|
| |
| 127 |
|
| # undefine these data sources, hopefully to release memory |
| 128 |
|
| undef $input_data1; |
| 129 |
|
| undef $input_data2; |
| 130 |
|
| |
| 131 |
|
| # merge the subsequent files |
| 132 |
|
| while (@ARGV) { |
| 133 |
|
| my $input_data = read_file( shift @ARGV ); |
| 134 |
|
| add_datasets($input_data); |
| 135 |
|
| } |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| # clean up coordinate column if it was generated |
| 139 |
|
| if ( my $c = $output_data->find_column('MergeDatasetCoordinate') ) { |
| 140 |
|
| $output_data->delete_column($c); |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
|
| ### Offer to re-name the datasets |
| 144 |
|
| unless ( $automatic or $user_list ) { |
| 145 |
|
| my $p = ' Do you wish to rename the headers? y/n (default n): '; |
| 146 |
|
| my $response = prompt( $p, 'n' ); |
| 147 |
|
| |
| 148 |
|
| if ( lc $response eq 'y' ) { |
| 149 |
|
| rename_dataset_names(); |
| 150 |
|
| } |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
|
| # clean up metadata we added but shouldn't keep |
| 154 |
|
| # this was put in here for remembering original file names for renaming purposes |
| 155 |
|
| for my $i ( 1 .. $output_data->number_columns ) { |
| 156 |
|
| $output_data->delete_metadata( $i, 'original_file' ); |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
|
| # remove duplicate comments |
| 160 |
|
| { |
| 161 |
|
| my @comments = $output_data->comments; |
| 162 |
|
| if (@comments) { |
| 163 |
|
| $output_data->delete_comment(); # should delete all |
| 164 |
|
| foreach my $c ( uniqstr @comments ) { |
| 165 |
|
| $output_data->add_comment($c); |
| 166 |
|
| } |
| 167 |
|
| } |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
|
| ### Print the output |
| 171 |
|
| # Request the output file name |
| 172 |
|
| # default is to simply overwrite file1 |
| 173 |
|
| unless ($outfile) { |
| 174 |
|
| my $p = sprintf " Enter the output file name [%s]: ", $output_data->{'filename'}; |
| 175 |
|
| $outfile = prompt( $p, $output_data->{'filename'} ); |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| # write the file |
| 179 |
|
| my $file_written = $output_data->write_file( |
| 180 |
|
| 'filename' => $outfile, |
| 181 |
|
| 'gz' => $gz, |
| 182 |
|
| ); |
| 183 |
|
| if ($file_written) { |
| 184 |
|
| print " Wrote file '$file_written'\n"; |
| 185 |
|
| } |
| 186 |
|
| else { |
| 187 |
|
| print " No file written!\n"; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| ######################## Subroutines ################################### |
| 191 |
|
| |
| 192 |
|
| ### Read the input data file |
| 193 |
|
| sub read_file { |
| 194 |
|
| |
| 195 |
|
| # subroutine to load the time data file and print basic stats about it |
| 196 |
|
| my $filename = shift; |
| 197 |
|
| |
| 198 |
|
| # load the file data |
| 199 |
|
| my $Data = Bio::ToolBox::Data->new( file => $filename ); |
| 200 |
|
| unless ($Data) { |
| 201 |
|
| die " file '$filename' not loaded!"; |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| # print the results |
| 205 |
|
| printf "\n Loaded '$filename' with %s rows and %s columns\n", |
| 206 |
|
| $Data->last_row, $Data->number_columns; |
| 207 |
|
| |
| 208 |
|
| # delete any pre-existing original_file metadata |
| 209 |
|
| # we'll be writing new |
| 210 |
|
| for my $i ( 1 .. $Data->number_columns ) { |
| 211 |
|
| $Data->delete_metadata( $i, 'original_file' ); |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
|
| # add coordinates if necessary |
| 215 |
|
| if ( $Data->bed or $Data->gff or $use_coordinate ) { |
| 216 |
|
| |
| 217 |
|
| # refuse if user does not want to use coordinates |
| 218 |
|
| if ( defined $use_coordinate and $use_coordinate == 0 ) { |
| 219 |
|
| return $Data; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| # identify coordinate columns |
| 223 |
|
| if ( ( defined $Data->chromo_column and defined $Data->start_column ) |
| 224 |
|
| or defined $Data->coord_column ) |
| 225 |
|
| { |
| 226 |
|
| my @columns; |
| 227 |
|
| push @columns, $Data->chromo_column; |
| 228 |
|
| push @columns, $Data->start_column; |
| 229 |
|
| push @columns, $Data->stop_column; |
| 230 |
|
| push @columns, $Data->coord_column; |
| 231 |
|
| printf " generating coordinate lookup string from columns %s\n", |
| 232 |
|
| join( ', ', grep {defined} @columns ); |
| 233 |
|
| } |
| 234 |
|
| else { |
| 235 |
|
| print |
| 236 |
|
| " cannot generate coordinates for file!! No chromosome, start, or coordinate columns\n"; |
| 237 |
|
| return $Data; |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| # add new column |
| 241 |
|
| my $coord_i = $Data->add_column('MergeDatasetCoordinate'); |
| 242 |
|
| |
| 243 |
|
| # generate two coordinate strings for lookup |
| 244 |
|
| # one should be 1-based, the other 0-based, hopefully we will match on one |
| 245 |
|
| $Data->iterate( |
| 246 |
|
| sub { |
| 247 |
|
| my $row = shift; |
| 248 |
|
| my $c = $row->seq_id; |
| 249 |
|
| my $e = $row->end; |
| 250 |
|
| my $v; |
| 251 |
|
| if ( defined $e ) { |
| 252 |
|
| $v = sprintf "%s:%d-%d,%s:%d-%d", $c, $row->start, $e, |
| 253 |
|
| $c, $row->start - 1, $e; |
| 254 |
|
| } |
| 255 |
|
| else { |
| 256 |
|
| $v = sprintf "%s:%d,%s:%d", $c, $row->start, $c, $row->start - 1; |
| 257 |
|
| } |
| 258 |
|
| $row->value( $coord_i, $v ); |
| 259 |
|
| } |
| 260 |
|
| ); |
| 261 |
|
| } |
| 262 |
|
| |
| 263 |
|
| # return |
| 264 |
|
| return $Data; |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
|
| ### Merge two datasets together |
| 268 |
|
| sub merge_two_datasets { |
| 269 |
|
| my ( $input_data1, $input_data2 ) = @_; |
| 270 |
|
| |
| 271 |
|
| # Check the input data |
| 272 |
|
| my $check = check_data_tables( $input_data1, $input_data2 ); |
| 273 |
|
| if ( $check and not $lookup ) { |
| 274 |
|
| print "\n Files have non-equal numbers of data rows! Enabling lookup\n"; |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
|
| # Merge by lookup values |
| 278 |
|
| if ( $lookup or $check ) { |
| 279 |
|
| |
| 280 |
|
| # we need to merge by lookup values |
| 281 |
|
| return merge_two_datasets_by_lookup( $input_data1, $input_data2 ); |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
|
| # Otherwise Merge the two datasets blindly |
| 285 |
|
| # determine the new order |
| 286 |
|
| my @order; |
| 287 |
|
| if ($automatic) { |
| 288 |
|
| |
| 289 |
|
| # automatic selection |
| 290 |
|
| @order = automatically_determine_order( $input_data1, $input_data2 ); |
| 291 |
|
| } |
| 292 |
|
| else { |
| 293 |
|
| # manual selection from user |
| 294 |
|
| @order = request_new_order( $input_data1, $input_data2 ); |
| 295 |
|
| } |
| 296 |
|
| |
| 297 |
|
| # Initialize the output data structure if necessary |
| 298 |
|
| $output_data = initialize_output_data_structure($input_data1); |
| 299 |
|
| |
| 300 |
|
| # add comment lines from second files, first was take care of during initialization |
| 301 |
|
| foreach ( $input_data2->comments ) { |
| 302 |
|
| $output_data->add_comment($_); |
| 303 |
|
| } |
| 304 |
|
| |
| 305 |
|
| # assign the datasets to the output data in requested order |
| 306 |
|
| foreach my $request (@order) { |
| 307 |
|
| |
| 308 |
|
| # add the dataset from the appropriate input file |
| 309 |
|
| if ( $request =~ /^\d+$/ ) { |
| 310 |
|
| |
| 311 |
|
| # a digit indicates a dataset from file1 |
| 312 |
|
| |
| 313 |
|
| # print dataset name in automatic mode |
| 314 |
|
| if ($automatic) { |
| 315 |
|
| printf " Merging column %s\n", $input_data1->name($request); |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
|
| # copy the dataset |
| 319 |
|
| my $values = $input_data1->column_values($request); |
| 320 |
|
| my $index = $output_data->add_column($values); |
| 321 |
|
| |
| 322 |
|
| # copy the metadata |
| 323 |
|
| copy_metadata( $input_data1, $request, $index ); |
| 324 |
|
| } |
| 325 |
|
| elsif ( $request =~ /^[a-z]+$/i ) { |
| 326 |
|
| |
| 327 |
|
| # a letter indicates a dataset from file2 |
| 328 |
|
| |
| 329 |
|
| # first convert back to number |
| 330 |
|
| my $number = $number_of->{$request}; |
| 331 |
|
| |
| 332 |
|
| # print dataset name in automatic mode |
| 333 |
|
| if ($automatic) { |
| 334 |
|
| printf " Merging column %s\n", $input_data2->name($number); |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| # copy the dataset |
| 338 |
|
| my $values = $input_data2->column_values($number); |
| 339 |
|
| my $index = $output_data->add_column($values); |
| 340 |
|
| |
| 341 |
|
| # copy the metadata |
| 342 |
|
| copy_metadata( $input_data2, $number, $index ); |
| 343 |
|
| } |
| 344 |
|
| else { |
| 345 |
|
| # we should never get here |
| 346 |
|
| die " unrecognized symbol '$request' in request! nothing done!\n"; |
| 347 |
|
| } |
| 348 |
|
| } |
| 349 |
|
| } |
| 350 |
|
| |
| 351 |
|
| ### merge two datasets by lookup value |
| 352 |
|
| sub merge_two_datasets_by_lookup { |
| 353 |
|
| my ( $input_data1, $input_data2 ) = @_; |
| 354 |
|
| |
| 355 |
|
| # determine lookup indices |
| 356 |
|
| my ( $lookup_i1, $lookup_i2 ) = request_lookup_indices( $input_data1, $input_data2 ); |
| 357 |
|
| |
| 358 |
|
| # determine order |
| 359 |
|
| my @order; |
| 360 |
|
| if ($automatic) { |
| 361 |
|
| |
| 362 |
|
| # automatic selection |
| 363 |
|
| @order = automatically_determine_order( $input_data1, $input_data2 ); |
| 364 |
|
| } |
| 365 |
|
| else { |
| 366 |
|
| # manual selection from user |
| 367 |
|
| @order = request_new_order( $input_data1, $input_data2 ); |
| 368 |
|
| } |
| 369 |
|
| |
| 370 |
|
| # add coordinate column to output if necessary |
| 371 |
|
| if ( $lookup_name |
| 372 |
|
| and ( $lookup_name eq 'MergeDatasetCoordinate' and not $automatic ) ) |
| 373 |
|
| { |
| 374 |
|
| |
| 375 |
|
| # check if we taking from file 1 or 2 |
| 376 |
|
| if ( $order[0] =~ /[a-z]+/i ) { |
| 377 |
|
| if ( $input_data2->name($lookup_i2) eq 'MergeDatasetCoordinate' ) { |
| 378 |
|
| unshift @order, $letter_of->{$lookup_i2}; |
| 379 |
|
| } |
| 380 |
|
| } |
| 381 |
|
| else { |
| 382 |
|
| if ( $input_data1->name($lookup_i1) eq 'MergeDatasetCoordinate' ) { |
| 383 |
|
| unshift @order, $lookup_i1; |
| 384 |
|
| } |
| 385 |
|
| } |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
|
| # rearrange as necessary to make the first data structure dominant |
| 389 |
|
| if ( $order[0] =~ /[a-z]+/ ) { |
| 390 |
|
| |
| 391 |
|
| # it looks like the user wants the second file to be dominant |
| 392 |
|
| # the dominant file is where we'll be taking all of the values |
| 393 |
|
| |
| 394 |
|
| # switch the references |
| 395 |
|
| ( $input_data1, $input_data2 ) = ( $input_data2, $input_data1 ); |
| 396 |
|
| |
| 397 |
|
| # switch the lookup indices |
| 398 |
|
| ( $lookup_i1, $lookup_i2 ) = ( $lookup_i2, $lookup_i1 ); |
| 399 |
|
| |
| 400 |
|
| # switch the numbers and letters |
| 401 |
|
| foreach (@order) { |
| 402 |
|
| if (/\d+/) { |
| 403 |
|
| $_ = $letter_of->{$_}; |
| 404 |
|
| } |
| 405 |
|
| else { |
| 406 |
|
| $_ = $number_of->{$_}; |
| 407 |
|
| } |
| 408 |
|
| } |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
|
| # Index the second dataset |
| 412 |
|
| # we'll be putting the lookup values into an index hash |
| 413 |
|
| # where the lookup value is the key and the row number is the value |
| 414 |
|
| my $index = index_dataset( $input_data2, $lookup_i2 ); |
| 415 |
|
| |
| 416 |
|
| # Initialize the output data structure if necessary |
| 417 |
|
| $output_data = initialize_output_data_structure($input_data1); |
| 418 |
|
| |
| 419 |
|
| # add comment lines from second files, first was take care of during initialization |
| 420 |
|
| foreach ( $input_data2->comments ) { |
| 421 |
|
| $output_data->add_comment($_); |
| 422 |
|
| } |
| 423 |
|
| |
| 424 |
|
| # assign the datasets to the output data in requested order |
| 425 |
|
| foreach my $request (@order) { |
| 426 |
|
| |
| 427 |
|
| # add the dataset from the appropriate input file based on the type of request |
| 428 |
|
| my $column; |
| 429 |
|
| |
| 430 |
|
| # a digit indicates a dataset from file1 |
| 431 |
|
| if ( $request =~ /\d+/ ) { |
| 432 |
|
| |
| 433 |
|
| # we're assuming that file1 is dominant, we copy all the |
| 434 |
|
| # rows from file1 into output, no lookup required |
| 435 |
|
| |
| 436 |
|
| # print dataset name in automatic mode |
| 437 |
|
| if ($automatic) { |
| 438 |
|
| printf " Merging column %s\n", $input_data1->name($request); |
| 439 |
|
| } |
| 440 |
|
| |
| 441 |
|
| # copy the dataset including header |
| 442 |
|
| my $values = $input_data1->column_values($request); |
| 443 |
|
| $column = $output_data->add_column($values); |
| 444 |
|
| |
| 445 |
|
| # copy the metadata |
| 446 |
|
| copy_metadata( $input_data1, $request, $column ); |
| 447 |
|
| } |
| 448 |
|
| |
| 449 |
|
| # a letter indicates a dataset from file2 |
| 450 |
|
| elsif ( $request =~ /[a-z]+/i ) { |
| 451 |
|
| |
| 452 |
|
| # we will have to perform the lookup here |
| 453 |
|
| |
| 454 |
|
| # first convert back to number |
| 455 |
|
| my $request_number = $number_of->{$request}; |
| 456 |
|
| |
| 457 |
|
| # print dataset name in automatic mode |
| 458 |
|
| if ($automatic) { |
| 459 |
|
| printf " Merging column %s\n", $input_data2->name($request_number); |
| 460 |
|
| } |
| 461 |
|
| |
| 462 |
|
| # add new empty column |
| 463 |
|
| $column = $output_data->add_column( $input_data2->name($request_number) ); |
| 464 |
|
| |
| 465 |
|
| # copy the dataset via lookup process |
| 466 |
|
| if ( $input_data1->name($lookup_i1) eq 'MergeDatasetCoordinate' ) { |
| 467 |
|
| foreach my $r1 ( 1 .. $input_data1->last_row ) { |
| 468 |
|
| |
| 469 |
|
| # identify the appropriate row in file2 by lookup value |
| 470 |
|
| # using both primary and alternate coordinate string |
| 471 |
|
| my ( $k1, $k2 ) = split /,/, $input_data1->value( $r1, $lookup_i1 ); |
| 472 |
|
| my $r2 = $index->{$k1} || $index->{$k2} || undef; |
| 473 |
|
| |
| 474 |
|
| # copy the appropriate value |
| 475 |
|
| if ( defined $r2 ) { |
| 476 |
|
| $output_data->value( $r1, $column, |
| 477 |
|
| $input_data2->value( $r2, $request_number ) ); |
| 478 |
|
| } |
| 479 |
|
| else { |
| 480 |
|
| $output_data->value( $r1, $column, '.' ); # null value |
| 481 |
|
| } |
| 482 |
|
| } |
| 483 |
|
| } |
| 484 |
|
| else { |
| 485 |
|
| foreach my $r1 ( 1 .. $input_data1->last_row ) { |
| 486 |
|
| |
| 487 |
|
| # identify the appropriate row in file2 by lookup value |
| 488 |
|
| my $lookup_val = $input_data1->value( $r1, $lookup_i1 ); |
| 489 |
|
| my $r2 = $index->{$lookup_val} || undef; |
| 490 |
|
| |
| 491 |
|
| # copy the appropriate value |
| 492 |
|
| if ( defined $r2 ) { |
| 493 |
|
| $output_data->value( $r1, $column, |
| 494 |
|
| $input_data2->value( $r2, $request_number ) ); |
| 495 |
|
| } |
| 496 |
|
| else { |
| 497 |
|
| $output_data->value( $r1, $column, '.' ); # null value |
| 498 |
|
| } |
| 499 |
|
| } |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
|
| # copy the metadata |
| 503 |
|
| copy_metadata( $input_data2, $request_number, $column ); |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
|
| # something else |
| 507 |
|
| else { |
| 508 |
|
| print " WARNING: unknown request '$request'. skipping this column\n"; |
| 509 |
|
| next; |
| 510 |
|
| } |
| 511 |
|
| |
| 512 |
|
| # make sure we remember the lookup_column_index in the new output |
| 513 |
|
| # if it is necessary |
| 514 |
|
| if ( $lookup_name and not defined $output_lookup_i ) { |
| 515 |
|
| |
| 516 |
|
| # we have a lookup name, but the index for this lookup in |
| 517 |
|
| # the output data structure is not yet defined |
| 518 |
|
| |
| 519 |
|
| # check if the current output index is it |
| 520 |
|
| if ( $output_data->name($column) =~ /\A $lookup_name \Z/xi ) { |
| 521 |
|
| |
| 522 |
|
| # this current column matches the lookup name |
| 523 |
|
| # so we will use it |
| 524 |
|
| $output_lookup_i = $column; |
| 525 |
|
| } |
| 526 |
|
| } |
| 527 |
|
| } |
| 528 |
|
| } |
| 529 |
|
| |
| 530 |
|
| ### Add datasets from one data file to the output data structure |
| 531 |
|
| sub add_datasets { |
| 532 |
|
| my $data = shift; |
| 533 |
|
| |
| 534 |
|
| # Check the input data |
| 535 |
|
| my $check = check_data_tables( $output_data, $data ); |
| 536 |
|
| |
| 537 |
|
| # Determine if we need to do lookup |
| 538 |
|
| my $lookup_i; |
| 539 |
|
| my $index; # lookup hash for the current data table |
| 540 |
|
| if ( $check or $lookup ) { |
| 541 |
|
| |
| 542 |
|
| # we need to merge by lookup values |
| 543 |
|
| |
| 544 |
|
| # check whether we need to do the lookup manually or automatically |
| 545 |
|
| if ( $manual or not $lookup_name ) { |
| 546 |
|
| |
| 547 |
|
| # we don't have an automatic lookup name defined or user wants to do it manually |
| 548 |
|
| # so we do this by treating as two new datasets |
| 549 |
|
| merge_two_datasets_by_lookup( $output_data, $data ); |
| 550 |
|
| return; |
| 551 |
|
| } |
| 552 |
|
| |
| 553 |
|
| # index the new data reference |
| 554 |
|
| # we'll be putting the lookup values into an index hash |
| 555 |
|
| # where the lookup value is the key and the row number is the value |
| 556 |
|
| $lookup_i = request_lookup_indices($data); |
| 557 |
|
| $index = index_dataset( $data, $lookup_i ); |
| 558 |
|
| } |
| 559 |
|
| |
| 560 |
|
| # add comment lines from second files, first was take care of during initialization |
| 561 |
|
| foreach ( $data->comments ) { |
| 562 |
|
| $output_data->add_comment($_); |
| 563 |
|
| } |
| 564 |
|
| |
| 565 |
|
| # Merge the new datasets with current output |
| 566 |
|
| # determine the new order |
| 567 |
|
| my @order; |
| 568 |
|
| if ($automatic) { |
| 569 |
|
| |
| 570 |
|
| # automatic selection |
| 571 |
|
| @order = automatically_determine_order($data); |
| 572 |
|
| |
| 573 |
|
| } |
| 574 |
|
| else { |
| 575 |
|
| # manual selection from user |
| 576 |
|
| @order = request_new_order($data); |
| 577 |
|
| } |
| 578 |
|
| |
| 579 |
|
| # assign the datasets to the output data in requested order |
| 580 |
|
| foreach my $request (@order) { |
| 581 |
|
| |
| 582 |
|
| # check the request |
| 583 |
|
| if ( $request !~ /^\d+$/ ) { |
| 584 |
|
| print " WARNING: unknown request '$request'. skipping this column\n"; |
| 585 |
|
| next; |
| 586 |
|
| } |
| 587 |
|
| |
| 588 |
|
| # print dataset name in automatic mode |
| 589 |
|
| if ($automatic) { |
| 590 |
|
| printf " Merging column %s\n", $data->name($request); |
| 591 |
|
| } |
| 592 |
|
| |
| 593 |
|
| # copy the dataset by lookup or blindly |
| 594 |
|
| my $column; |
| 595 |
|
| if ( defined $lookup_i ) { |
| 596 |
|
| |
| 597 |
|
| # merging by lookup |
| 598 |
|
| |
| 599 |
|
| # check that we have the output lookup index |
| 600 |
|
| unless ( defined $output_lookup_i ) { |
| 601 |
|
| print <<MESSAGE; |
| 602 |
|
| FATAL: The lookup index for column '$lookup_name' is not defined in the output! |
| 603 |
|
| Please ensure you include column '$lookup_name' in the output file. |
| 604 |
|
| MESSAGE |
| 605 |
|
| exit 1; |
| 606 |
|
| } |
| 607 |
|
| |
| 608 |
|
| # add new empty column |
| 609 |
|
| $column = $output_data->add_column( $data->name($request) ); |
| 610 |
|
| |
| 611 |
|
| # copy the dataset via lookup process |
| 612 |
|
| if ( $output_data->name($output_lookup_i) eq 'MergeDatasetCoordinate' ) { |
| 613 |
|
| $output_data->iterate( |
| 614 |
|
| sub { |
| 615 |
|
| my $row = shift; |
| 616 |
|
| |
| 617 |
|
| # lookup the row using either primary or alternate coordinates |
| 618 |
|
| # then replace if a value is found |
| 619 |
|
| my ( $key1, $key2 ) = split /,/, |
| 620 |
|
| $row->value($output_lookup_i); |
| 621 |
|
| my $r = $index->{$key1} || $index->{$key2} || undef; |
| 622 |
|
| if ($r) { |
| 623 |
|
| $row->value( $column, $data->value( $r, $request ) ); |
| 624 |
|
| } |
| 625 |
|
| else { |
| 626 |
|
| $row->value( $column, '.' ); # null value |
| 627 |
|
| } |
| 628 |
|
| } |
| 629 |
|
| ); |
| 630 |
|
| } |
| 631 |
|
| else { |
| 632 |
|
| $output_data->iterate( |
| 633 |
|
| sub { |
| 634 |
|
| my $row = shift; |
| 635 |
|
| |
| 636 |
|
| # identify the appropriate row in source data table by lookup value |
| 637 |
|
| my $r = $index->{ $row->value($output_lookup_i) } || undef; |
| 638 |
|
| if ($r) { |
| 639 |
|
| $row->value( $column, $data->value( $r, $request ) ); |
| 640 |
|
| } |
| 641 |
|
| else { |
| 642 |
|
| $row->value( $column, '.' ); # null value |
| 643 |
|
| } |
| 644 |
|
| } |
| 645 |
|
| ); |
| 646 |
|
| } |
| 647 |
|
| } |
| 648 |
|
| |
| 649 |
|
| # merging blindly |
| 650 |
|
| else { |
| 651 |
|
| # copy the dataset including header |
| 652 |
|
| my $values = $data->column_values($request); |
| 653 |
|
| $column = $output_data->add_column($values); |
| 654 |
|
| } |
| 655 |
|
| |
| 656 |
|
| # copy the metadata |
| 657 |
|
| copy_metadata( $data, $request, $column ); |
| 658 |
|
| } |
| 659 |
|
| } |
| 660 |
|
| |
| 661 |
|
| ### Request the new order |
| 662 |
|
| sub request_new_order { |
| 663 |
|
| my @data_refs = @_; |
| 664 |
|
| |
| 665 |
|
| # Either one or two data file references may be passed |
| 666 |
|
| # The dataset names will be printed listed by their index. Numbers will be |
| 667 |
|
| # used for the first data file, letters for the second, if present. |
| 668 |
|
| my $list; |
| 669 |
|
| |
| 670 |
|
| # Check if user supplied the order by command line |
| 671 |
|
| if (@order_requests) { |
| 672 |
|
| $list = shift @order_requests; |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
|
| # else request interactively |
| 676 |
|
| else { |
| 677 |
|
| # Print the column headers |
| 678 |
|
| if ( scalar @data_refs == 1 ) { |
| 679 |
|
| |
| 680 |
|
| # Only one data file |
| 681 |
|
| # we'll be using only numbers to list the datasets |
| 682 |
|
| print_datasets( $data_refs[0], 'number' ); |
| 683 |
|
| } |
| 684 |
|
| elsif ( scalar @data_refs == 2 ) { |
| 685 |
|
| |
| 686 |
|
| # Two data files |
| 687 |
|
| # use numbers for the first one |
| 688 |
|
| print_datasets( $data_refs[0], 'number' ); |
| 689 |
|
| |
| 690 |
|
| # use letters for the second one |
| 691 |
|
| print_datasets( $data_refs[1], 'letter' ); |
| 692 |
|
| |
| 693 |
|
| } |
| 694 |
|
| |
| 695 |
|
| # Request response from user |
| 696 |
|
| my $p = "\n Enter the columns' indices in the desired final order.\n" |
| 697 |
|
| . " Separate indices with commas or specify a range (start - stop): "; |
| 698 |
|
| $list = prompt($p); |
| 699 |
|
| $list =~ s/\s+//g; |
| 700 |
|
| } |
| 701 |
|
| |
| 702 |
|
| # Parse response |
| 703 |
|
| my @order = parse_list($list); |
| 704 |
|
| while ( scalar @order == 0 ) { |
| 705 |
|
| |
| 706 |
|
| # an empty order is returned if the list was unparseable |
| 707 |
|
| # allow user to try again |
| 708 |
|
| my $p = " Unable to parse your list into valid indices. Please try again\n "; |
| 709 |
|
| $list = prompt($p); |
| 710 |
|
| $list =~ s/\s+//g; |
| 711 |
|
| @order = parse_list($list); |
| 712 |
|
| } |
| 713 |
|
| |
| 714 |
|
| # done |
| 715 |
|
| printf " using order: %s\n", join( ', ', @order ); |
| 716 |
|
| return @order; |
| 717 |
|
| } |
| 718 |
|
| |
| 719 |
|
| ### Request lookup indices for two datasets |
| 720 |
|
| sub request_lookup_indices { |
| 721 |
|
| my ( $data1, $data2 ) = @_; |
| 722 |
|
| |
| 723 |
|
| # One or two data references may be passed |
| 724 |
|
| # We'll ask for the lookup value indices for each |
| 725 |
|
| |
| 726 |
|
| # Determine the lookup index values |
| 727 |
|
| my $index1; |
| 728 |
|
| my $index2; |
| 729 |
|
| |
| 730 |
|
| # Automatic methods of determining the lookup index |
| 731 |
|
| if ( not $manual ) { |
| 732 |
|
| |
| 733 |
|
| # Only one data table that we're adding, find the lookup column |
| 734 |
|
| if ( not defined $data2 and $lookup_name ) { |
| 735 |
|
| |
| 736 |
|
| # we already have a lookup name |
| 737 |
|
| # just need to find same column for one dataset |
| 738 |
|
| $index1 = $data1->find_column("^$lookup_name\$"); |
| 739 |
|
| if ( defined $index1 ) { |
| 740 |
|
| printf "\n using column $index1 (%s) as lookup index for file '%s'\n", |
| 741 |
|
| $data1->name($index1), $data1->filename; |
| 742 |
|
| return $index1; |
| 743 |
|
| } |
| 744 |
|
| else { |
| 745 |
|
| printf "\n Cannot find lookup column '%s' in file '%s'!!!\n", |
| 746 |
|
| $lookup_name, $data1->filename; |
| 747 |
|
| |
| 748 |
|
| # try some other possibilities? |
| 749 |
|
| if ( $lookup_name eq 'MergeDatasetCoordinate' ) { |
| 750 |
|
| foreach my $name (qw(coordinate primary_id)) { |
| 751 |
|
| $index1 = $data1->find_column("^$name\$"); |
| 752 |
|
| if ( defined $index1 ) { |
| 753 |
|
| printf " Trying column $index1 (%s) as lookup index\n", |
| 754 |
|
| $data1->name($index1); |
| 755 |
|
| return $index1; |
| 756 |
|
| } |
| 757 |
|
| } |
| 758 |
|
| } |
| 759 |
|
| exit 1; |
| 760 |
|
| } |
| 761 |
|
| } |
| 762 |
|
| |
| 763 |
|
| # First try some known column identifiers we could use automatically |
| 764 |
|
| # don't forget to add the user-requested lookup name |
| 765 |
|
| my @name_list; |
| 766 |
|
| if ($use_coordinate) { |
| 767 |
|
| @name_list = qw(MergeDatasetCoordinate coordinate id); |
| 768 |
|
| } |
| 769 |
|
| else { |
| 770 |
|
| @name_list = qw(name primary_id gene.?id transcript.?id gene transcript); |
| 771 |
|
| } |
| 772 |
|
| if ($user_lookup_name) { |
| 773 |
|
| unshift @name_list, $user_lookup_name; |
| 774 |
|
| } |
| 775 |
|
| foreach my $name (@name_list) { |
| 776 |
|
| |
| 777 |
|
| # identify possibilities |
| 778 |
|
| my $possible_index1 = $data1->find_column("^$name\$"); |
| 779 |
|
| my $possible_index2 = $data2->find_column("^$name\$"); |
| 780 |
|
| |
| 781 |
|
| # check if something was found |
| 782 |
|
| if ( defined $possible_index1 and defined $possible_index2 ) { |
| 783 |
|
| |
| 784 |
|
| # assign |
| 785 |
|
| $index1 = $possible_index1; |
| 786 |
|
| $index2 = $possible_index2; |
| 787 |
|
| $lookup_name = $name; # for future lookups |
| 788 |
|
| |
| 789 |
|
| # report |
| 790 |
|
| printf "\n using column $index1 (%s) as lookup index for file '%s'\n", |
| 791 |
|
| $data1->name($index1), $data1->filename; |
| 792 |
|
| printf " using column $index2 (%s) as lookup index for file '%s'\n", |
| 793 |
|
| $data2->name($index2), $data2->filename; |
| 794 |
|
| |
| 795 |
|
| # don't go through remaining list |
| 796 |
|
| last; |
| 797 |
|
| } |
| 798 |
|
| } |
| 799 |
|
| } |
| 800 |
|
| |
| 801 |
|
| if ( not defined $index1 and not defined $index2 ) { |
| 802 |
|
| |
| 803 |
|
| # Automatic identification didn't work |
| 804 |
|
| # must bother the user for help |
| 805 |
|
| if ($automatic) { |
| 806 |
|
| print <<MESSAGE; |
| 807 |
|
| |
| 808 |
|
| Unable to identify appropriate lookup columns automatically! |
| 809 |
|
| Use --coordinate option if coordinates should be used for lookup. |
| 810 |
|
| Otherwise, please execute interactively to identify lookup columns. |
| 811 |
|
| MESSAGE |
| 812 |
|
| exit 1; |
| 813 |
|
| } |
| 814 |
|
| |
| 815 |
|
| unless ($manual) { |
| 816 |
|
| print <<MESSAGE; |
| 817 |
|
| |
| 818 |
|
| Unable to identify appropriate lookup columns automatically! |
| 819 |
|
| If using coordinates, re-execute with the --coordinate option. |
| 820 |
|
| MESSAGE |
| 821 |
|
| } |
| 822 |
|
| |
| 823 |
|
| # Print the index headers |
| 824 |
|
| # use numbers for the first one |
| 825 |
|
| print_datasets( $data1, 'number', 1 ); |
| 826 |
|
| |
| 827 |
|
| # use letters for the second one |
| 828 |
|
| print_datasets( $data2, 'letter', 1 ); |
| 829 |
|
| |
| 830 |
|
| # Request first index responses from user |
| 831 |
|
| my $p = ' Enter the unique identifier index for lookup in the first file: '; |
| 832 |
|
| while ( not defined $index1 ) { |
| 833 |
|
| $index1 = prompt($p); |
| 834 |
|
| |
| 835 |
|
| # check that it's valid |
| 836 |
|
| unless ( $index1 =~ /^\d+$/ and exists $data1->{$index1} ) { |
| 837 |
|
| print " unknown index value! Try again\n"; |
| 838 |
|
| undef $index1; |
| 839 |
|
| } |
| 840 |
|
| } |
| 841 |
|
| |
| 842 |
|
| # Request second index responses from user |
| 843 |
|
| $p = ' Enter the unique identifier index for lookup in the second file: '; |
| 844 |
|
| while ( not defined $index2 ) { |
| 845 |
|
| $index2 = prompt($p); |
| 846 |
|
| |
| 847 |
|
| # check that it's valid |
| 848 |
|
| unless ( $index2 =~ /^[a-z]+$/i ) { |
| 849 |
|
| print " index value must be a letter! Try again\n"; |
| 850 |
|
| undef $index2; |
| 851 |
|
| next; |
| 852 |
|
| } |
| 853 |
|
| $index2 = $number_of->{$index2}; # convert to a number |
| 854 |
|
| # check that it's valid |
| 855 |
|
| unless ( exists $data2->{$index2} ) { |
| 856 |
|
| print " unknown index value! Try again\n"; |
| 857 |
|
| undef $index2; |
| 858 |
|
| } |
| 859 |
|
| } |
| 860 |
|
| } |
| 861 |
|
| |
| 862 |
|
| # done |
| 863 |
|
| return $index1, $index2; |
| 864 |
|
| } |
| 865 |
|
| |
| 866 |
|
| ### Automatically determine the order |
| 867 |
|
| sub automatically_determine_order { |
| 868 |
|
| |
| 869 |
|
| # get the two data structures |
| 870 |
|
| # we could have two, or only one |
| 871 |
|
| my ( $data1, $data2, $number ); |
| 872 |
|
| if ( scalar @_ == 1 ) { |
| 873 |
|
| |
| 874 |
|
| # comparing new data with the output data we are building |
| 875 |
|
| $data1 = $output_data; |
| 876 |
|
| $data2 = shift @_; |
| 877 |
|
| $number = 1; |
| 878 |
|
| } |
| 879 |
|
| else { |
| 880 |
|
| # two new datasets |
| 881 |
|
| $data1 = shift @_; |
| 882 |
|
| $data2 = shift @_; |
| 883 |
|
| $number = 2; |
| 884 |
|
| } |
| 885 |
|
| |
| 886 |
|
| # generate quick hash of names to exclude from data1 |
| 887 |
|
| my %exclude; |
| 888 |
|
| for my $i ( 1 .. $data1->number_columns ) { |
| 889 |
|
| $exclude{ $data1->name($i) } = 1; |
| 890 |
|
| } |
| 891 |
|
| |
| 892 |
|
| # generate the order |
| 893 |
|
| my @order; |
| 894 |
|
| |
| 895 |
|
| # add the first dataset if provided |
| 896 |
|
| if ( $number == 2 ) { |
| 897 |
|
| |
| 898 |
|
| # we will automatically take all of the columns from the first data |
| 899 |
|
| for my $i ( 1 .. $data1->number_columns ) { |
| 900 |
|
| push @order, $i; |
| 901 |
|
| } |
| 902 |
|
| } |
| 903 |
|
| |
| 904 |
|
| # automatically identify those columns in second data not present in |
| 905 |
|
| # the first one to take |
| 906 |
|
| for my $i ( 1 .. $data2->number_columns ) { |
| 907 |
|
| |
| 908 |
|
| # name is score, take it |
| 909 |
|
| if ( $data2->name($i) eq 'Score' ) { |
| 910 |
|
| |
| 911 |
|
| # take number or letter depending on number of data files |
| 912 |
|
| if ( $number == 1 ) { |
| 913 |
|
| push @order, $i; |
| 914 |
|
| } |
| 915 |
|
| else { |
| 916 |
|
| # two data files, push letter |
| 917 |
|
| push @order, $letter_of->{$i}; |
| 918 |
|
| } |
| 919 |
|
| } |
| 920 |
|
| |
| 921 |
|
| elsif ( exists $exclude{ $data2->name($i) } ) { |
| 922 |
|
| |
| 923 |
|
| # non-unique column, skip |
| 924 |
|
| next; |
| 925 |
|
| } |
| 926 |
|
| |
| 927 |
|
| else { |
| 928 |
|
| # must be a unique column, take it |
| 929 |
|
| # take number or letter depending on number of data files |
| 930 |
|
| if ( $number == 1 ) { |
| 931 |
|
| push @order, $i; |
| 932 |
|
| } |
| 933 |
|
| else { |
| 934 |
|
| push @order, $letter_of->{$i}; |
| 935 |
|
| } |
| 936 |
|
| } |
| 937 |
|
| } |
| 938 |
|
| |
| 939 |
|
| # done |
| 940 |
|
| return @order; |
| 941 |
|
| } |
| 942 |
|
| |
| 943 |
|
| ### Parse a string into a list |
| 944 |
|
| sub parse_list { |
| 945 |
|
| my $string = shift; |
| 946 |
|
| |
| 947 |
|
| # Parse the string |
| 948 |
|
| my @list; |
| 949 |
|
| foreach my $item ( split /,/, $string ) { |
| 950 |
|
| |
| 951 |
|
| # comma-delimited list of items |
| 952 |
|
| # may contain ranges of incremental items, check for those |
| 953 |
|
| |
| 954 |
|
| if ( $item =~ /\-/ ) { |
| 955 |
|
| |
| 956 |
|
| # item is a range, specified as 'start - stop' |
| 957 |
|
| |
| 958 |
|
| if ( $item =~ /^(\d+) \- (\d+)$/x ) { |
| 959 |
|
| |
| 960 |
|
| # range contains numbers, so must be from file1 |
| 961 |
|
| for ( my $i = $1; $i <= $2; $i++ ) { |
| 962 |
|
| |
| 963 |
|
| # we will loop through from specified start to stop |
| 964 |
|
| # add each number to the order |
| 965 |
|
| push @list, $i; |
| 966 |
|
| } |
| 967 |
|
| } |
| 968 |
|
| |
| 969 |
|
| elsif ( $item =~ /^ ([a-z]+) \- ([a-z]+) $/x ) { |
| 970 |
|
| |
| 971 |
|
| # range does not contain numbers, so must be from file2 |
| 972 |
|
| for ( my $i = $number_of->{$1}; $i <= $number_of->{$2}; $i++ ) { |
| 973 |
|
| |
| 974 |
|
| # we will loop through from specified start to stop |
| 975 |
|
| # converting each letter to the corresponding number for |
| 976 |
|
| # use in the for loop |
| 977 |
|
| # then convert the number in $i back to a letter to add |
| 978 |
|
| # to the order |
| 979 |
|
| push @list, $letter_of->{$i}; |
| 980 |
|
| } |
| 981 |
|
| } |
| 982 |
|
| |
| 983 |
|
| else { |
| 984 |
|
| # unrecognizable |
| 985 |
|
| return; |
| 986 |
|
| } |
| 987 |
|
| } |
| 988 |
|
| |
| 989 |
|
| else { |
| 990 |
|
| # item is not a range but a single number/letter |
| 991 |
|
| # add it to the order |
| 992 |
|
| push @list, $item; |
| 993 |
|
| } |
| 994 |
|
| |
| 995 |
|
| } |
| 996 |
|
| |
| 997 |
|
| return @list; |
| 998 |
|
| } |
| 999 |
|
| |
| 1000 |
|
| ### Print the dataset indices and names for the passed data file |
| 1001 |
|
| sub print_datasets { |
| 1002 |
|
| |
| 1003 |
|
| # pass the data structure reference and the index type ('number' or 'letter') |
| 1004 |
|
| my ( $data, $index_type, $include_coordinate ) = @_; |
| 1005 |
|
| |
| 1006 |
|
| # array to be used in the default order |
| 1007 |
|
| my @order; |
| 1008 |
|
| |
| 1009 |
|
| # print the dataset names for this datafile |
| 1010 |
|
| printf "\n These are the headers in file '%s'\n", $data->filename; |
| 1011 |
|
| |
| 1012 |
|
| # print Numbers |
| 1013 |
|
| if ( $index_type eq 'number' ) { |
| 1014 |
|
| for my $i ( 1 .. $data->number_columns ) { |
| 1015 |
|
| |
| 1016 |
|
| # skip the coordinate |
| 1017 |
|
| if ( $data->name($i) eq 'MergeDatasetCoordinate' |
| 1018 |
|
| and not $include_coordinate ) |
| 1019 |
|
| { |
| 1020 |
|
| next; |
| 1021 |
|
| } |
| 1022 |
|
| |
| 1023 |
|
| # print the dataset name and it's index |
| 1024 |
|
| # and record the index in the order array for the default order |
| 1025 |
|
| printf " %4s %s\n", $i, $data->name($i); |
| 1026 |
|
| push @order, $i; |
| 1027 |
|
| } |
| 1028 |
|
| } |
| 1029 |
|
| |
| 1030 |
|
| # print letters |
| 1031 |
|
| else { |
| 1032 |
|
| for my $i ( 1 .. $data->number_columns ) { |
| 1033 |
|
| |
| 1034 |
|
| # skip the coordinate |
| 1035 |
|
| if ( $data->name($i) eq 'MergeDatasetCoordinate' |
| 1036 |
|
| and not $include_coordinate ) |
| 1037 |
|
| { |
| 1038 |
|
| next; |
| 1039 |
|
| } |
| 1040 |
|
| |
| 1041 |
|
| # use letters instead of numbers as the index key |
| 1042 |
|
| my $letter = $letter_of->{$i}; |
| 1043 |
|
| printf " %4s %s\n", $letter, $data->name($i); |
| 1044 |
|
| push @order, $letter; |
| 1045 |
|
| } |
| 1046 |
|
| } |
| 1047 |
|
| return @order; |
| 1048 |
|
| } |
| 1049 |
|
| |
| 1050 |
|
| ### Check the data tables for similarity |
| 1051 |
|
| sub check_data_tables { |
| 1052 |
|
| my ( $input_data1, $input_data2 ) = @_; |
| 1053 |
|
| |
| 1054 |
|
| # Check the feature types |
| 1055 |
|
| if ( $input_data1->feature |
| 1056 |
|
| and $input_data2->feature |
| 1057 |
|
| and $input_data1->feature ne $input_data2->feature ) |
| 1058 |
|
| { |
| 1059 |
|
| # Each file has feature type defined, but they're not the same |
| 1060 |
|
| # probably really don't want to combine these |
| 1061 |
|
| print " WARNING! The metadata feature types for both files don't match!!!\n"; |
| 1062 |
|
| print " Continuing anyway...\n"; |
| 1063 |
|
| } |
| 1064 |
|
| |
| 1065 |
|
| # check line numbers |
| 1066 |
|
| my $status = 0; |
| 1067 |
|
| if ( $input_data1->last_row != $input_data2->last_row ) { |
| 1068 |
|
| |
| 1069 |
|
| # the number of rows in each data table don't equal |
| 1070 |
|
| # we will need to do this by lookup |
| 1071 |
|
| $status = 1; |
| 1072 |
|
| } |
| 1073 |
|
| return $status; |
| 1074 |
|
| } |
| 1075 |
|
| |
| 1076 |
|
| ### Index the values in a data table |
| 1077 |
|
| sub index_dataset { |
| 1078 |
|
| my ( $data, $lookup_i ) = @_; |
| 1079 |
|
| |
| 1080 |
|
| # load up the lookup index hash for the current data table |
| 1081 |
|
| my %index; |
| 1082 |
|
| my $index_warning = 0; |
| 1083 |
|
| if ( $data->name($lookup_i) eq 'MergeDatasetCoordinate' ) { |
| 1084 |
|
| $data->iterate( |
| 1085 |
|
| sub { |
| 1086 |
|
| my $row = shift; |
| 1087 |
|
| my ( $key1, $key2 ) = split /,/, $row->value($lookup_i); |
| 1088 |
|
| die "no primary,alternate coordinates!" |
| 1089 |
|
| unless ( defined $key1 and defined $key2 ); |
| 1090 |
|
| if ( exists $index{$key1} ) { |
| 1091 |
|
| $index_warning++; |
| 1092 |
|
| } |
| 1093 |
|
| else { |
| 1094 |
|
| $index{$key1} = $row->row_index; |
| 1095 |
|
| } |
| 1096 |
|
| if ( exists $index{$key2} ) { |
| 1097 |
|
| $index_warning++; |
| 1098 |
|
| } |
| 1099 |
|
| else { |
| 1100 |
|
| $index{$key2} = $row->row_index; |
| 1101 |
|
| } |
| 1102 |
|
| } |
| 1103 |
|
| ); |
| 1104 |
|
| } |
| 1105 |
|
| else { |
| 1106 |
|
| $data->iterate( |
| 1107 |
|
| sub { |
| 1108 |
|
| my $row = shift; |
| 1109 |
|
| my $key = $row->value($lookup_i); |
| 1110 |
|
| if ( exists $index{$key} ) { |
| 1111 |
|
| $index_warning++; |
| 1112 |
|
| } |
| 1113 |
|
| else { |
| 1114 |
|
| $index{$key} = $row->row_index; |
| 1115 |
|
| } |
| 1116 |
|
| } |
| 1117 |
|
| ); |
| 1118 |
|
| } |
| 1119 |
|
| if ($index_warning) { |
| 1120 |
|
| my $f = $data->filename; |
| 1121 |
|
| my $n = $data->name($lookup_i); |
| 1122 |
|
| print <<MESSAGE; |
| 1123 |
|
| WARNING: $index_warning rows had two or more duplicate lookup values |
| 1124 |
|
| for column '$n' (index $lookup_i) in file '$f'. |
| 1125 |
|
| Only the first occurence was used. |
| 1126 |
|
| MESSAGE |
| 1127 |
|
| |
| 1128 |
|
| } |
| 1129 |
|
| return \%index; |
| 1130 |
|
| } |
| 1131 |
|
| |
| 1132 |
|
| ### Initialize the output data structure |
| 1133 |
|
| sub initialize_output_data_structure { |
| 1134 |
|
| my $Data1 = shift; |
| 1135 |
|
| |
| 1136 |
|
| # generate brand new output data structure |
| 1137 |
|
| my $output_data1 = Bio::ToolBox::Data->new( |
| 1138 |
|
| feature => $Data1->feature, # re-use the same feature as file1 |
| 1139 |
|
| ); |
| 1140 |
|
| |
| 1141 |
|
| # we'll re-use the values from file1 |
| 1142 |
|
| $output_data1->program( $Data1->program ); # force overwrite value |
| 1143 |
|
| $output_data1->database( $Data1->database ); |
| 1144 |
|
| foreach ( $Data1->comments ) { |
| 1145 |
|
| $output_data1->add_comment($_); |
| 1146 |
|
| } |
| 1147 |
|
| |
| 1148 |
|
| # assign the filename |
| 1149 |
|
| if ($outfile) { |
| 1150 |
|
| |
| 1151 |
|
| # use the new output file name |
| 1152 |
|
| $output_data1->add_file_metadata($outfile); |
| 1153 |
|
| } |
| 1154 |
|
| else { |
| 1155 |
|
| # borrow the first file name |
| 1156 |
|
| $output_data1->add_file_metadata( $Data1->filename ); # borrow file name |
| 1157 |
|
| } |
| 1158 |
|
| |
| 1159 |
|
| return $output_data1; |
| 1160 |
|
| } |
| 1161 |
|
| |
| 1162 |
|
| ### Copy the dataset metadata |
| 1163 |
|
| sub copy_metadata { |
| 1164 |
|
| |
| 1165 |
|
| # collect arguments |
| 1166 |
|
| my ( $data, $request, $index ) = @_; |
| 1167 |
|
| |
| 1168 |
|
| # copy the metadata |
| 1169 |
|
| my %md = $data->metadata($request); |
| 1170 |
|
| foreach my $k ( keys %md ) { |
| 1171 |
|
| next if $k eq 'index'; |
| 1172 |
|
| next if $k eq 'name'; |
| 1173 |
|
| $output_data->metadata( $index, $k, $md{$k} ); |
| 1174 |
|
| } |
| 1175 |
|
| |
| 1176 |
|
| # check if should rename the dataset |
| 1177 |
|
| if ( $automatic and $data->name($request) eq 'Score' ) { |
| 1178 |
|
| |
| 1179 |
|
| # only in automatic mode and the dataset is a generic Score |
| 1180 |
|
| # no opportunity to rename interactively |
| 1181 |
|
| # use file basename appended with Score |
| 1182 |
|
| $output_data->name( $index, $data->basename . '_Score' ); |
| 1183 |
|
| } |
| 1184 |
|
| |
| 1185 |
|
| # add original filename |
| 1186 |
|
| $output_data->metadata( $index, 'original_file', $data->filename ); |
| 1187 |
|
| } |
| 1188 |
|
| |
| 1189 |
|
| ### Re-name the dataset names |
| 1190 |
|
| sub rename_dataset_names { |
| 1191 |
|
| print " For each header, type a new name or push enter to accept the current name\n"; |
| 1192 |
|
| |
| 1193 |
|
| for my $i ( 1 .. $output_data->number_columns ) { |
| 1194 |
|
| |
| 1195 |
|
| # walk through the list of columns |
| 1196 |
|
| |
| 1197 |
|
| # request user input |
| 1198 |
|
| # print the current name and it's originating file name |
| 1199 |
|
| my $p = sprintf " %s: %s -> ", $output_data->metadata( $i, 'original_file' ), |
| 1200 |
|
| $output_data->name($i); |
| 1201 |
|
| my $new_name = prompt( $p, q() ); |
| 1202 |
|
| |
| 1203 |
|
| # rename as requested |
| 1204 |
|
| if ($new_name) { |
| 1205 |
|
| $output_data->name( $i, $new_name ); |
| 1206 |
|
| } |
| 1207 |
|
| } |
| 1208 |
|
| } |
| 1209 |
|
| |
| 1210 |
|
| ### Generate lookup hashes for converting numbers to letters and back |
| 1211 |
|
| sub get_number_letters { |
| 1212 |
|
| |
| 1213 |
|
| # hashes |
| 1214 |
|
| my %n2l; # numbers to letters |
| 1215 |
|
| my %l2n; # letters to numbers |
| 1216 |
|
| my %lookup = ( |
| 1217 |
|
| 0 => 'a', |
| 1218 |
|
| 1 => 'b', |
| 1219 |
|
| 2 => 'c', |
| 1220 |
|
| 3 => 'd', |
| 1221 |
|
| 4 => 'e', |
| 1222 |
|
| 5 => 'f', |
| 1223 |
|
| 6 => 'g', |
| 1224 |
|
| 7 => 'h', |
| 1225 |
|
| 8 => 'i', |
| 1226 |
|
| 9 => 'j', |
| 1227 |
|
| 10 => 'k', |
| 1228 |
|
| 11 => 'l', |
| 1229 |
|
| 12 => 'm', |
| 1230 |
|
| 13 => 'n', |
| 1231 |
|
| 14 => 'o', |
| 1232 |
|
| 15 => 'p', |
| 1233 |
|
| 16 => 'q', |
| 1234 |
|
| 17 => 'r', |
| 1235 |
|
| 18 => 's', |
| 1236 |
|
| 19 => 't', |
| 1237 |
|
| 20 => 'u', |
| 1238 |
|
| 21 => 'v', |
| 1239 |
|
| 22 => 'w', |
| 1240 |
|
| 23 => 'x', |
| 1241 |
|
| 24 => 'y', |
| 1242 |
|
| 25 => 'z', |
| 1243 |
|
| ); |
| 1244 |
|
| |
| 1245 |
|
| # fill up the lookup hashes |
| 1246 |
|
| my $first; |
| 1247 |
|
| for my $i ( 0 .. 701 ) { |
| 1248 |
|
| |
| 1249 |
|
| # this gives range from a to zz |
| 1250 |
|
| |
| 1251 |
|
| # check the first letter |
| 1252 |
|
| if ( $i % 26 == 0 ) { |
| 1253 |
|
| $first = $lookup{ ( $i / 26 ) - 1 }; |
| 1254 |
|
| } |
| 1255 |
|
| |
| 1256 |
|
| # generate the letter |
| 1257 |
|
| # two letters [null..z][a..z] |
| 1258 |
|
| my $letter; |
| 1259 |
|
| if ( defined $first ) { |
| 1260 |
|
| $letter = sprintf "%s%s", $first, $lookup{ $i % 26 }; |
| 1261 |
|
| } |
| 1262 |
|
| else { |
| 1263 |
|
| $letter = $lookup{ $i % 26 }; |
| 1264 |
|
| } |
| 1265 |
|
| |
| 1266 |
|
| # store in hashes, converting 0-base numbers to 1-base numbers |
| 1267 |
|
| $n2l{ $i + 1 } = $letter; |
| 1268 |
|
| $l2n{$letter} = $i + 1; |
| 1269 |
|
| } |
| 1270 |
|
| |
| 1271 |
|
| return ( \%n2l, \%l2n ); |
| 1272 |
|
| } |
| 1273 |
|
| |
| 1274 |
|
| __END__ |