| 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 IO::Prompt::Tiny qw(prompt); | ||
| 10 | use Bio::ToolBox::Data::Stream; | ||
| 11 | use Bio::ToolBox::utility qw(format_with_commas); | ||
| 12 | |||
| 13 | our $VERSION = '2.00'; | ||
| 14 | |||
| 15 | print "\n This script will concatenate two or more data files\n\n"; | ||
| 16 | |||
| 17 | ### Quick 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 ( $outfile, $gz, $help, $print_version, ); | ||
| 32 | |||
| 33 | # Command line options | ||
| 34 | GetOptions( | ||
| 35 | 'o|out=s' => \$outfile, # specify the input data file | ||
| 36 | 'z|gz!' => \$gz, # compress output files | ||
| 37 | 'h|help' => \$help, # request help | ||
| 38 | 'v|version' => \$print_version, # print the version | ||
| 39 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 40 | |||
| 41 | # Print help | ||
| 42 | if ($help) { | ||
| 43 | |||
| 44 | # print entire POD | ||
| 45 | pod2usage( | ||
| 46 | { | ||
| 47 | '-verbose' => 2, | ||
| 48 | '-exitval' => 1, | ||
| 49 | } | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | |||
| 53 | # Print version | ||
| 54 | if ($print_version) { | ||
| 55 | print " Biotoolbox script join_data_file.pl, version $VERSION\n"; | ||
| 56 | eval { | ||
| 57 | require Bio::ToolBox; | ||
| 58 | my $v = Bio::ToolBox->VERSION; | ||
| 59 | print " Biotoolbox package version $v\n"; | ||
| 60 | }; | ||
| 61 | exit; | ||
| 62 | } | ||
| 63 | |||
| 64 | ### Check for required values | ||
| 65 | unless ( scalar @ARGV > 1 ) { | ||
| 66 | print " WARNING: Only one file was provided to join!\n"; | ||
| 67 | exit; | ||
| 68 | } | ||
| 69 | |||
| 70 | ### Load first file | ||
| 71 | my $first_file = shift @ARGV; | ||
| 72 | my $first_data = Bio::ToolBox::Data::Stream->new( in => $first_file ) | ||
| 73 | or die "Unable to open first file '$first_file'!\n"; | ||
| 74 | |||
| 75 | ### Prepare output file name | ||
| 76 | # get outfile name | ||
| 77 | unless ($outfile) { | ||
| 78 | |||
| 79 | # if the file was a set generated by split_data_file.pl | ||
| 80 | # then it may use the # as a demarcation symbol for the basename and split value | ||
| 81 | # look for it and regenerate the original basename | ||
| 82 | if ( $first_data->basename =~ /^(.+) \# \w+ $/x ) { | ||
| 83 | $outfile = $first_data->path . $1 . $first_data->extension; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | # ask the user for input, what else to do? | ||
| 87 | my $p = ' please enter the output file name: '; | ||
| 88 | my $default = File::Spec->catfile( $first_data->path, | ||
| 89 | sprintf( "%s.joined%s", $first_data->basename, $first_data->extension ) ); | ||
| 90 | $outfile = prompt( $p, $default ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # check extension | ||
| 95 | unless ( defined $gz ) { | ||
| 96 | if ( $outfile =~ /\.gz$/ ) { | ||
| 97 | $gz = 1; | ||
| 98 | } | ||
| 99 | elsif ( $first_file =~ /\.gz$/ ) { | ||
| 100 | |||
| 101 | # first input file is compressed, so keep it that way | ||
| 102 | $gz = 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | # otherwise, keep it undefined | ||
| 106 | } | ||
| 107 | |||
| 108 | ### Begin writing output joined file | ||
| 109 | my $Output = $first_data->duplicate($outfile) | ||
| 110 | or die " unable to write output file!\n"; | ||
| 111 | my $line_count = 0; | ||
| 112 | while ( my $row = $first_data->next_row ) { | ||
| 113 | $Output->add_row($row); | ||
| 114 | $line_count++; | ||
| 115 | } | ||
| 116 | $first_data->close_fh; | ||
| 117 | printf " merged %s lines from %s\n", format_with_commas($line_count), | ||
| 118 | $first_data->filename; | ||
| 119 | |||
| 120 | ### Now write the remaining files | ||
| 121 | foreach my $file (@ARGV) { | ||
| 122 | |||
| 123 | # open the file | ||
| 124 | my $Data = Bio::ToolBox::Data::Stream->new( in => $file ) | ||
| 125 | or die "\n Unable to open file '$file'! Unable to proceed!\n"; | ||
| 126 | |||
| 127 | # check that file extension matches | ||
| 128 | if ( $Output->extension ne $Data->extension ) { | ||
| 129 | |||
| 130 | # double-check we don't have gz extensions confounding our comparison | ||
| 131 | my $outext = $Output->extension; | ||
| 132 | $outext =~ s/\.gz$//i; | ||
| 133 | my $inext = $Data->extension; | ||
| 134 | $inext =~ s/\.gz$//i; | ||
| 135 | if ( $outext ne $inext ) { | ||
| 136 | printf( "\n WARNING: File extensions do not match! Compare %s with %s!\n", | ||
| 137 | $Output->extension, $Data->extension ); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | # check for equal number of columns | ||
| 142 | unless ( $Output->number_columns == $Data->number_columns ) { | ||
| 143 | printf STDERR | ||
| 144 | "\n FATAL: Column number mismatch!\n %s has %d columns instead of %d!\n Unable to proceed!\n", | ||
| 145 | $file, $Data->number_columns, $Output->number_columns; | ||
| 146 | exit 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | # check first and last column names | ||
| 150 | my @output_names = $Output->list_columns; | ||
| 151 | my @data_names = $Data->list_columns; | ||
| 152 | if ( join( q( ), @output_names ) ne join( q( ), @data_names ) ) { | ||
| 153 | print "\n WARNING! Column header names don't match!!\n"; | ||
| 154 | for my $i ( 0 .. $#output_names ) { | ||
| 155 | if ( $output_names[$i] ne $data_names[$i] ) { | ||
| 156 | printf " compare index $i, '%s' with '%s'\n ", $output_names[$i], | ||
| 157 | $data_names[$i]; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | # continue writing the file | ||
| 163 | # for the sake of speed over data checking, we're taking raw input lines | ||
| 164 | # and writing directly to the output | ||
| 165 | # write to the raw output filehandle if we can get it | ||
| 166 | # going through Data::Feature objects is considerably slower and not necessary | ||
| 167 | my $count = 0; | ||
| 168 | my $in_fh = $Data->fh; | ||
| 169 | my $out_fh = $Output->fh; # only get a filehandle if we've started writing the file | ||
| 170 | if ($out_fh) { | ||
| 171 | while ( my $line = $in_fh->getline ) { | ||
| 172 | $out_fh->print($line); | ||
| 173 | $count++; | ||
| 174 | $line_count++; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | # not as fast but fast enough | ||
| 179 | while ( my $line = $in_fh->getline ) { | ||
| 180 | $Output->write_row($line); | ||
| 181 | $count++; | ||
| 182 | $line_count++; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | printf " merged %s lines from %s\n", format_with_commas($count), $Data->filename; | ||
| 186 | $Data->close_fh; | ||
| 187 | } | ||
| 188 | |||
| 189 | ### Finish | ||
| 190 | $Output->close_fh; | ||
| 191 | printf " Wrote %s lines to combined file '%s'\n", format_with_commas($line_count), | ||
| 192 | $Output->filename; | ||
| 193 | |||
| 194 | __END__ |