| 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 Bio::ToolBox::Data; | ||
| 10 | use Bio::ToolBox::utility qw(ask_user_for_index); | ||
| 11 | |||
| 12 | our $VERSION = '2.00'; | ||
| 13 | |||
| 14 | print "\n A script to pull out specific features from a data file\n"; | ||
| 15 | |||
| 16 | ### Quick help | ||
| 17 | unless (@ARGV) { | ||
| 18 | pod2usage( | ||
| 19 | { | ||
| 20 | '-verbose' => 0, | ||
| 21 | '-exitval' => 1, | ||
| 22 | } | ||
| 23 | ); | ||
| 24 | } | ||
| 25 | |||
| 26 | ### Get command line options | ||
| 27 | my ( | ||
| 28 | $datafile, $listfile, $outfile, $data_index, $list_index, | ||
| 29 | $group_index, $order, $sum, $sum_only, $startcolumn, | ||
| 30 | $stopcolumn, $log, $help, $print_version, | ||
| 31 | ); | ||
| 32 | GetOptions( | ||
| 33 | 'd|data=s' => \$datafile, # the input data file | ||
| 34 | 'l|list=s' => \$listfile, # the list file | ||
| 35 | 'o|out=s' => \$outfile, # the new output file name | ||
| 36 | 'x|dindex=i' => \$data_index, # index to look up in the data file | ||
| 37 | 'X|lindex=i' => \$list_index, # index of look up values in list file | ||
| 38 | 'g|gindex=i' => \$group_index, # index of group in list file | ||
| 39 | 'r|order=s' => \$order, # the order to keep the values | ||
| 40 | 'U|sum!' => \$sum, # flag to re-sum the pulled values | ||
| 41 | 'sumonly!' => \$sum_only, # only save the summary file | ||
| 42 | 'starti=i' => \$startcolumn, # index of column to start summarizing | ||
| 43 | 'stopi=i' => \$stopcolumn, # index of column to stop summarizing | ||
| 44 | 'log!' => \$log, # values are in log, respect log status | ||
| 45 | 'h|help' => \$help, # flag to print help | ||
| 46 | 'v|version' => \$print_version, # print the version | ||
| 47 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 48 | |||
| 49 | if ($help) { | ||
| 50 | |||
| 51 | # print entire POD | ||
| 52 | pod2usage( | ||
| 53 | { | ||
| 54 | '-verbose' => 2, | ||
| 55 | '-exitval' => 1, | ||
| 56 | } | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | # Print version | ||
| 61 | if ($print_version) { | ||
| 62 | print " Biotoolbox script pull_features.pl, version $VERSION\n"; | ||
| 63 | eval { | ||
| 64 | require Bio::ToolBox; | ||
| 65 | my $v = Bio::ToolBox->VERSION; | ||
| 66 | print " Biotoolbox package version $v\n"; | ||
| 67 | }; | ||
| 68 | exit; | ||
| 69 | } | ||
| 70 | |||
| 71 | ### Check for required values | ||
| 72 | |||
| 73 | unless ( defined $datafile ) { | ||
| 74 | print STDERR " FATAL: no input data file specified!\n"; | ||
| 75 | exit 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | unless ( defined $listfile ) { | ||
| 79 | print STDERR " FATAL: no list data specified!\n"; | ||
| 80 | exit 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | unless ( defined $outfile ) { | ||
| 84 | print " FATAL: no output data file name given!\n"; | ||
| 85 | exit 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | $sum = 1 if $sum_only; | ||
| 89 | |||
| 90 | my $list_order; | ||
| 91 | if ($order) { | ||
| 92 | if ( $order eq 'list' ) { | ||
| 93 | $list_order = 1; | ||
| 94 | } | ||
| 95 | elsif ( $order eq 'data' ) { | ||
| 96 | $list_order = 0; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | print STDERR " FATAL: unrecognized order request '$order'! Enter list or data\n"; | ||
| 100 | exit 1; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | # default is the list order | ||
| 105 | $list_order = 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | ### Open files | ||
| 109 | # file handles and metadata | ||
| 110 | my $List = Bio::ToolBox::Data->new( file => $listfile ) | ||
| 111 | or die " unable to open list file!\n"; | ||
| 112 | |||
| 113 | my $Data = Bio::ToolBox::Data->new( file => $datafile ) | ||
| 114 | or die " unable to open data file!\n"; | ||
| 115 | |||
| 116 | ### Determine indices | ||
| 117 | |||
| 118 | ### Load the list of specified values | ||
| 119 | print " Collecting lookup values from file '$listfile'...\n"; | ||
| 120 | identify_indices(); | ||
| 121 | unless ( defined $list_index and defined $data_index ) { | ||
| 122 | print " FATAL: data and list lookup indexes are still not defined!\n"; | ||
| 123 | exit 1; | ||
| 124 | } | ||
| 125 | my ( $requests, $pulled ) = collect_request_list(); | ||
| 126 | |||
| 127 | # these are global references to two data hashes | ||
| 128 | # the first is for a lookup for the feature requests and identify the group number | ||
| 129 | # the second is for storing all the found pulled data | ||
| 130 | |||
| 131 | ### Pull out the desired features | ||
| 132 | print " Pulling features from file '$datafile'...\n"; | ||
| 133 | my ( $found_count, $notfound_count ) = pull_requested_features(); | ||
| 134 | print " $found_count features were found and pulled\n"; | ||
| 135 | if ( $notfound_count > 0 ) { | ||
| 136 | print " $notfound_count features were not found\n"; | ||
| 137 | } | ||
| 138 | |||
| 139 | ### Write the output files | ||
| 140 | if ($found_count) { | ||
| 141 | |||
| 142 | # first generate output structures | ||
| 143 | generate_output_data_structures(); | ||
| 144 | |||
| 145 | # then | ||
| 146 | write_files(); | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | print " Nothing found! Nothing to write!\n"; | ||
| 150 | } | ||
| 151 | |||
| 152 | ######################## Subroutines ################################### | ||
| 153 | |||
| 154 | ### Identify the indices | ||
| 155 | sub identify_indices { | ||
| 156 | |||
| 157 | # first check whether we have a simple list file | ||
| 158 | if ( $List->number_columns == 1 ) { | ||
| 159 | |||
| 160 | # simple one-column file | ||
| 161 | # the answer is obvious | ||
| 162 | print " list file only has 1 column, using it\n"; | ||
| 163 | $list_index = 1; | ||
| 164 | |||
| 165 | # be VERY careful, though, it may not have a column header name | ||
| 166 | } | ||
| 167 | elsif ( $listfile =~ /\.kgg$/i ) { | ||
| 168 | |||
| 169 | # a cluster gene file | ||
| 170 | # the answer is obvious | ||
| 171 | print " using .kgg file as list\n"; | ||
| 172 | $list_index = 1; | ||
| 173 | $group_index = 2; | ||
| 174 | |||
| 175 | # KGG files have column headers | ||
| 176 | } | ||
| 177 | |||
| 178 | # look for the corresponding list index if data was specified | ||
| 179 | if ( defined $data_index and not defined $list_index ) { | ||
| 180 | |||
| 181 | # we have the data index but need the list index | ||
| 182 | |||
| 183 | # get the column header name | ||
| 184 | my $lookup = $Data->name($data_index); | ||
| 185 | |||
| 186 | # find it in the list | ||
| 187 | my $possible = $List->find_column("^$lookup\$"); | ||
| 188 | |||
| 189 | # check | ||
| 190 | if ( defined $possible ) { | ||
| 191 | |||
| 192 | # found something | ||
| 193 | $list_index = $possible; | ||
| 194 | printf " found column '%s', using List index %d\n", | ||
| 195 | $List->name($list_index), $list_index; | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | print " could not find corresponding List column index for '$lookup'\n"; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | # look for the corresponding data index if list was specified | ||
| 204 | elsif ( not defined $data_index and defined $list_index ) { | ||
| 205 | |||
| 206 | # we have the list index but need the data index | ||
| 207 | |||
| 208 | # get the column header name | ||
| 209 | my $lookup = $List->name($list_index); | ||
| 210 | |||
| 211 | # find it in the list | ||
| 212 | my $possible = $Data->find_column("^$lookup\$"); | ||
| 213 | |||
| 214 | # check | ||
| 215 | if ( defined $possible ) { | ||
| 216 | |||
| 217 | # found something | ||
| 218 | $data_index = $possible; | ||
| 219 | printf " found column '%s', using Data index %d\n", | ||
| 220 | $Data->name($data_index), $data_index; | ||
| 221 | return; | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | # did not find something | ||
| 225 | # is it possible it is a simple list of names? | ||
| 226 | print " could not find corresponding Data column index for '$lookup'\n"; | ||
| 227 | if ( $List->number_columns == 1 ) { | ||
| 228 | |||
| 229 | # we likely only have a simple list of features | ||
| 230 | # adjust the data table accordingly by adding a name | ||
| 231 | # this will avoid not finding the first element in the output file | ||
| 232 | # if this is not true, then the user will just get an error that | ||
| 233 | # one feature cannot be found..... | ||
| 234 | #### WE ARE MESSING WITH THE INTERNALS OF THE OBJECT HERE | ||
| 235 | #### DONT DO THIS!!!!!!! | ||
| 236 | unshift @{ $List->{'data_table'} }, 'Name'; | ||
| 237 | $List->{1}{'name'} = 'Name'; | ||
| 238 | $List->{'last_row'}++; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | # neither was specified | ||
| 244 | elsif ( not defined $data_index and not defined $list_index ) { | ||
| 245 | |||
| 246 | # try with common name columns | ||
| 247 | $data_index = $Data->name_column; | ||
| 248 | $list_index = $List->name_column; | ||
| 249 | |||
| 250 | if ( defined $data_index | ||
| 251 | and defined $list_index | ||
| 252 | and $Data->name($data_index) eq $List->name($list_index) ) | ||
| 253 | { | ||
| 254 | |||
| 255 | # report | ||
| 256 | printf " using List column '%s', index $list_index\n", | ||
| 257 | $List->name($list_index); | ||
| 258 | printf " using Data column '%s', index $data_index\n", | ||
| 259 | $Data->name($data_index); | ||
| 260 | return; | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | # nope, do not have a match, forget our guesses | ||
| 264 | undef $data_index; | ||
| 265 | undef $list_index; | ||
| 266 | } | ||
| 267 | |||
| 268 | # try with primary_id column | ||
| 269 | $data_index = $Data->id_column; | ||
| 270 | $list_index = $List->id_column; | ||
| 271 | |||
| 272 | if ( defined $data_index and defined $list_index ) { | ||
| 273 | |||
| 274 | # report | ||
| 275 | printf " using List column '%s', index $list_index\n", | ||
| 276 | $List->name($list_index); | ||
| 277 | printf " using Data column '%s', index $data_index\n", | ||
| 278 | $Data->name($data_index); | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | # nope, do not have a match, forget our guesses | ||
| 283 | undef $data_index; | ||
| 284 | undef $list_index; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | # check for group number | ||
| 289 | if ( $List->number_columns > 1 and not defined $group_index ) { | ||
| 290 | my $i = $List->find_column('group'); | ||
| 291 | if ( defined $i ) { | ||
| 292 | $group_index = $i; | ||
| 293 | } | ||
| 294 | |||
| 295 | # do we ask for a group or not????? probably not.... keep original functionality | ||
| 296 | } | ||
| 297 | if ( defined $group_index and not $List->name($group_index) ) { | ||
| 298 | die " invalid group index!\n"; | ||
| 299 | } | ||
| 300 | |||
| 301 | # End automatic guessing of index numbers, ask the user | ||
| 302 | unless ( defined $list_index ) { | ||
| 303 | $list_index = ask_user_for_index( $List, | ||
| 304 | " Enter the unique identifier lookup column index from the List file " | ||
| 305 | ); | ||
| 306 | } | ||
| 307 | unless ( defined $data_index ) { | ||
| 308 | $data_index = ask_user_for_index( $Data, | ||
| 309 | " Enter the unique identifier lookup column index from the Data file " | ||
| 310 | ); | ||
| 311 | } | ||
| 312 | printf " We are using\n List lookup index %d, '%s'\n Data lookup index %d, '%s'\n", | ||
| 313 | $list_index, $List->name($list_index), $data_index, $Data->name($data_index); | ||
| 314 | if ( defined $group_index ) { | ||
| 315 | printf " group index %d, '%s'\n", $group_index, $List->name($group_index); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | ### Subroutine to collect list values from a file | ||
| 320 | sub collect_request_list { | ||
| 321 | |||
| 322 | my %requests; # the identifier values to look up | ||
| 323 | # request{ unique_id } = group# | ||
| 324 | # for KGG lists where we are splitting each of the groups into | ||
| 325 | # separate files, we need to know how many and which ones | ||
| 326 | # can't trust whether all groups are in the KGG file or just a few | ||
| 327 | my %pulled; | ||
| 328 | |||
| 329 | # hash pulled{ group# } -> { unique_id } = [ line_data ] | ||
| 330 | # still need a list of the order: | ||
| 331 | # hash pulled{ group# } -> { 'feature_order' } = [unique_id,...] | ||
| 332 | |||
| 333 | # check if we have multiple groups to work with | ||
| 334 | if ( defined $group_index ) { | ||
| 335 | $List->iterate( | ||
| 336 | sub { | ||
| 337 | my $row = shift; | ||
| 338 | my $id = $row->value($list_index); | ||
| 339 | my $group = $row->value($group_index); | ||
| 340 | |||
| 341 | # store the identifier in the requests hash | ||
| 342 | # the gene identifier is the key, the cluster group number is the value | ||
| 343 | $requests{$id} = $group; | ||
| 344 | |||
| 345 | # prepare the pulled data hash | ||
| 346 | $pulled{$group}->{$id} = []; | ||
| 347 | |||
| 348 | # record the ID for use in writing in the file list order | ||
| 349 | if ( exists $pulled{$group}{'list_order'} ) { | ||
| 350 | push @{ $pulled{$group}{'list_order'} }, $id; | ||
| 351 | } | ||
| 352 | else { | ||
| 353 | $pulled{$group}{'list_order'} = [ ($id) ]; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | ); | ||
| 357 | } | ||
| 358 | |||
| 359 | # otherwise a simple one group list file | ||
| 360 | else { | ||
| 361 | # prepare the order array | ||
| 362 | # using group ID of 0 | ||
| 363 | $pulled{0}{'feature_order'} = []; | ||
| 364 | |||
| 365 | # collect the identifiers | ||
| 366 | $List->iterate( | ||
| 367 | sub { | ||
| 368 | my $row = shift; | ||
| 369 | my $id = $row->value($list_index); | ||
| 370 | |||
| 371 | $requests{$id} = 0; | ||
| 372 | $pulled{0}{$id} = []; | ||
| 373 | push @{ $pulled{0}{'list_order'} }, $id; | ||
| 374 | } | ||
| 375 | ); | ||
| 376 | } | ||
| 377 | |||
| 378 | # prepare the dump array for keeping the features in data file order | ||
| 379 | # instead of list file order | ||
| 380 | foreach my $group ( keys %pulled ) { | ||
| 381 | $pulled{$group}{'data_dump'} = []; | ||
| 382 | } | ||
| 383 | |||
| 384 | return ( \%requests, \%pulled ); | ||
| 385 | } | ||
| 386 | |||
| 387 | ### Subroutine to pull the requested features | ||
| 388 | sub pull_requested_features { | ||
| 389 | |||
| 390 | # we will now walk through the data file and pull out those lines | ||
| 391 | # which match the feature identifier | ||
| 392 | my $found = 0; | ||
| 393 | my $notfound = 0; | ||
| 394 | $Data->iterate( | ||
| 395 | sub { | ||
| 396 | my $row = shift; | ||
| 397 | |||
| 398 | # check if the identifier exists in our requests hash | ||
| 399 | if ( exists $requests->{ $row->value($data_index) } ) { | ||
| 400 | |||
| 401 | # found | ||
| 402 | my $id = $row->value($data_index); | ||
| 403 | my $group = $requests->{$id}; | ||
| 404 | |||
| 405 | # record the line data | ||
| 406 | if ($list_order) { | ||
| 407 | |||
| 408 | # store by ID so that it can be sorted later | ||
| 409 | push @{ $pulled->{$group}{$id} }, $row; | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | # just dump it in the same order as the data file | ||
| 413 | push @{ $pulled->{$group}{'data_dump'} }, $row; | ||
| 414 | } | ||
| 415 | $found++; | ||
| 416 | } | ||
| 417 | else { | ||
| 418 | # not found | ||
| 419 | $notfound++; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | ); | ||
| 423 | |||
| 424 | return ( $found, $notfound ); | ||
| 425 | } | ||
| 426 | |||
| 427 | ### Subroutine to generate the output data structures | ||
| 428 | sub generate_output_data_structures { | ||
| 429 | |||
| 430 | # we will simply duplicate the current data metadata structure | ||
| 431 | # for each output data structure | ||
| 432 | # there may be more than one output data structure, primarily with | ||
| 433 | # kgg source files where each group will be put into a new file | ||
| 434 | |||
| 435 | # we will store the output data structure in the %pulled hash structure | ||
| 436 | # under the key 'output_data' under the appropriate group id | ||
| 437 | |||
| 438 | # only one output data structure | ||
| 439 | if ( scalar keys %{$pulled} == 1 ) { | ||
| 440 | |||
| 441 | my $group = ( keys %{$pulled} )[0]; # use the actual group name | ||
| 442 | |||
| 443 | # generate new file name data | ||
| 444 | my $newfile = $outfile; | ||
| 445 | my $ext = $Data->extension; | ||
| 446 | unless ( $newfile =~ m/$ext\Z/ ) { | ||
| 447 | |||
| 448 | # add the current extension if necessary | ||
| 449 | $newfile .= $ext; | ||
| 450 | } | ||
| 451 | |||
| 452 | # duplicate and store away | ||
| 453 | my $New = $Data->duplicate; | ||
| 454 | $New->add_file_metadata($newfile); | ||
| 455 | $pulled->{$group}{'output_data'} = $New; | ||
| 456 | } | ||
| 457 | |||
| 458 | # multiple data structures | ||
| 459 | else { | ||
| 460 | foreach my $group ( keys %{$pulled} ) { | ||
| 461 | |||
| 462 | # generate new file name | ||
| 463 | my $newfile = $outfile; | ||
| 464 | my $ext = $Data->extension; | ||
| 465 | $newfile =~ s/$ext\Z//; # remove the extension if present | ||
| 466 | $newfile .= '_g' . $group . $ext; | ||
| 467 | |||
| 468 | # duplicate and store away | ||
| 469 | my $New = $Data->duplicate; | ||
| 470 | $New->add_file_metadata($newfile); | ||
| 471 | $pulled->{$group}{'output_data'} = $New; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | ### Subroutine to write the output files | ||
| 477 | sub write_files { | ||
| 478 | |||
| 479 | # Write the files | ||
| 480 | |||
| 481 | foreach my $group ( keys %{$pulled} ) { | ||
| 482 | |||
| 483 | # de-reference the output data for ease | ||
| 484 | my $group_Data = $pulled->{$group}{'output_data'}; | ||
| 485 | |||
| 486 | # copy the pulled data lines into the output data structure | ||
| 487 | if ($list_order) { | ||
| 488 | |||
| 489 | # we will use the list order in the output file | ||
| 490 | foreach my $id ( @{ $pulled->{$group}{'list_order'} } ) { | ||
| 491 | |||
| 492 | # this is a list of the feature ids in the same order as the input | ||
| 493 | # list file | ||
| 494 | foreach ( @{ $pulled->{$group}{$id} } ) { | ||
| 495 | $group_Data->add_row($_); | ||
| 496 | } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | else { | ||
| 500 | # we will use the data file order in the output file | ||
| 501 | # just copy the contents over | ||
| 502 | foreach ( @{ $pulled->{$group}{'data_dump'} } ) { | ||
| 503 | $group_Data->add_row($_); | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | # write the file | ||
| 508 | if ( not $sum_only ) { | ||
| 509 | my $write_results = $group_Data->save; | ||
| 510 | |||
| 511 | # no file name, using the filename recorded in the out data | ||
| 512 | if ($write_results) { | ||
| 513 | print " Wrote new datafile '$write_results'\n"; | ||
| 514 | } | ||
| 515 | else { | ||
| 516 | print " Unable to write datafile '$write_results'!!!\n"; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | # Summarize the pulled data | ||
| 521 | if ($sum) { | ||
| 522 | my $sumfile = $group_Data->summary_file( | ||
| 523 | 'startcolumn' => $startcolumn, | ||
| 524 | 'endcolumn' => $stopcolumn, | ||
| 525 | 'log' => $log, | ||
| 526 | ); | ||
| 527 | if ($sumfile) { | ||
| 528 | print " Wrote summary file '$sumfile'\n"; | ||
| 529 | } | ||
| 530 | else { | ||
| 531 | print " Unable to write summary file!\n"; | ||
| 532 | } | ||
| 533 | } | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | __END__ |