| 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 Bio::ToolBox::Data::Stream; | ||
| 10 | use Bio::ToolBox::utility qw(ask_user_for_index format_with_commas); | ||
| 11 | |||
| 12 | our $VERSION = '2.01'; | ||
| 13 | |||
| 14 | print "\n This script will split a data file by features\n\n"; | ||
| 15 | |||
| 16 | ### Quick help | ||
| 17 | unless (@ARGV) { | ||
| 18 | |||
| 19 | # when no command line options are present | ||
| 20 | # print SYNOPSIS | ||
| 21 | pod2usage( | ||
| 22 | { | ||
| 23 | '-verbose' => 0, | ||
| 24 | '-exitval' => 1, | ||
| 25 | } | ||
| 26 | ); | ||
| 27 | } | ||
| 28 | |||
| 29 | ### Get command line options and initialize values | ||
| 30 | my ( $infile, $index, $tag, $max, $gz, $prefix, $noheader, $help, $print_version, ); | ||
| 31 | |||
| 32 | # Command line options | ||
| 33 | GetOptions( | ||
| 34 | 'i|in=s' => \$infile, # specify the input data file | ||
| 35 | 'x|index|col=i' => \$index, # index for the column to use for splitting | ||
| 36 | 't|tag=s' => \$tag, # attribute tag name | ||
| 37 | 'm|max=i' => \$max, # maximum number of lines per file | ||
| 38 | 'p|prefix=s' => \$prefix, # output file prefix | ||
| 39 | 'H|noheader' => \$noheader, # file has no headers | ||
| 40 | 'z|gz!' => \$gz, # compress output files | ||
| 41 | 'h|help' => \$help, # request help | ||
| 42 | 'v|version' => \$print_version, # print the version | ||
| 43 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 44 | |||
| 45 | # Print help | ||
| 46 | if ($help) { | ||
| 47 | |||
| 48 | # print entire POD | ||
| 49 | pod2usage( | ||
| 50 | { | ||
| 51 | '-verbose' => 2, | ||
| 52 | '-exitval' => 1, | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | |||
| 57 | # Print version | ||
| 58 | if ($print_version) { | ||
| 59 | print " Biotoolbox script split_data_file.pl, version $VERSION\n"; | ||
| 60 | eval { | ||
| 61 | require Bio::ToolBox; | ||
| 62 | my $v = Bio::ToolBox->VERSION; | ||
| 63 | print " Biotoolbox package version $v\n"; | ||
| 64 | }; | ||
| 65 | exit; | ||
| 66 | } | ||
| 67 | |||
| 68 | ### Check for required values | ||
| 69 | unless ($infile) { | ||
| 70 | if (@ARGV) { | ||
| 71 | $infile = shift @ARGV; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | print STDERR " FATAL: no input file! use --help for more information\n"; | ||
| 75 | exit 1; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | unless ( defined $gz ) { | ||
| 79 | if ( $infile =~ /\.gz$/ ) { | ||
| 80 | |||
| 81 | # input file is compressed, so keep it that way | ||
| 82 | $gz = 1; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $gz = 0; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | $noheader = defined $noheader ? $noheader : 0; | ||
| 89 | |||
| 90 | ### Load Input file | ||
| 91 | my $Input = Bio::ToolBox::Data::Stream->new( in => $infile, noheader => $noheader ) | ||
| 92 | or die "Unable to open input file!\n"; | ||
| 93 | |||
| 94 | # Identify the column | ||
| 95 | unless ( defined $index or defined $tag ) { | ||
| 96 | $index = ask_user_for_index( $Input, | ||
| 97 | " Enter the column index number containing the values to split by " ); | ||
| 98 | unless ( defined $index ) { | ||
| 99 | print STDERR " FATAL: Must provide a valid index!\n"; | ||
| 100 | exit 1; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | if ($tag) { | ||
| 104 | unless ( $Input->gff or $Input->vcf ) { | ||
| 105 | print STDERR | ||
| 106 | " FATAL: Input file must be in GFF or VCF format to use attribute tags!"; | ||
| 107 | exit 1; | ||
| 108 | } | ||
| 109 | if ( $Input->vcf and not defined $index ) { | ||
| 110 | print STDERR | ||
| 111 | " FATAL: Please provide a column index for accessing VCF attributes.\n" | ||
| 112 | . " The INFO column is index 8, and sample columns begin\n" | ||
| 113 | . " at index 10.\n"; | ||
| 114 | exit 1; | ||
| 115 | } | ||
| 116 | elsif ( $Input->gff ) { | ||
| 117 | $index = 8; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | ### Split the file | ||
| 122 | printf " Splitting file by elements in column %s%s...\n", | ||
| 123 | $Input->name($index), | ||
| 124 | $tag ? ', attribute tag $tag' : q(); | ||
| 125 | my %out_files; # a hash of the file names written | ||
| 126 | # we can't assume that all the data elements we're splitting on are | ||
| 127 | # contiguous in the file | ||
| 128 | # if they're not, then we would be simply re-writing over the | ||
| 129 | # previous block | ||
| 130 | # also, we're enforcing a maximum number of lines per file | ||
| 131 | # so we'll remember the files we've written, and re-open that file | ||
| 132 | # to write the next block of data | ||
| 133 | my $split_count = 0; | ||
| 134 | |||
| 135 | while ( my $row = $Input->next_row ) { | ||
| 136 | |||
| 137 | # Get the check value | ||
| 138 | my $check; | ||
| 139 | if ($tag) { | ||
| 140 | my $attrib = $row->attributes; | ||
| 141 | $check = $attrib->{$tag} || $attrib->{$index}{$tag} || undef; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $check = $row->value($index); | ||
| 145 | } | ||
| 146 | unless ( exists $out_files{$check}{'stream'} ) { | ||
| 147 | request_new_file_name($check); | ||
| 148 | } | ||
| 149 | |||
| 150 | # write the row | ||
| 151 | $out_files{$check}{'stream'}->add_row($row); | ||
| 152 | $out_files{$check}{'number'} += 1; | ||
| 153 | $out_files{$check}{'total'} += 1; | ||
| 154 | |||
| 155 | # Check the number of lines collected, close if necessary | ||
| 156 | if ( defined $max and $out_files{$check}{'number'} == $max ) { | ||
| 157 | |||
| 158 | # we've reached the maximum number of data lines for this current data | ||
| 159 | $out_files{$check}{'stream'}->close_fh; | ||
| 160 | delete $out_files{$check}{'stream'}; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | ### Finish | ||
| 165 | # Properly close out all file handles | ||
| 166 | $Input->close_fh; | ||
| 167 | foreach my $value ( keys %out_files ) { | ||
| 168 | $out_files{$value}{'stream'}->close_fh if exists $out_files{$value}{'stream'}; | ||
| 169 | } | ||
| 170 | |||
| 171 | # report | ||
| 172 | print " Split '$infile' into $split_count files\n"; | ||
| 173 | foreach my $value ( sort { $a cmp $b } keys %out_files ) { | ||
| 174 | printf " wrote %s lines in %d file%s for '$value'\n", | ||
| 175 | format_with_commas( $out_files{$value}{total} ), $out_files{$value}{parts}, | ||
| 176 | $out_files{$value}{parts} > 1 ? 's' : q(); | ||
| 177 | } | ||
| 178 | |||
| 179 | sub request_new_file_name { | ||
| 180 | |||
| 181 | # calculate a new file name based on the current check value and part number | ||
| 182 | my $value = shift; | ||
| 183 | my $filename_value = $value; | ||
| 184 | $filename_value =~ s/[\: \| \\ \/ \+ \* \? \# \( \) \[ \] \{ \} \ ]+/_/xg; | ||
| 185 | |||
| 186 | # replace unsafe characters | ||
| 187 | |||
| 188 | my $file; | ||
| 189 | if ( $prefix and $prefix eq 'none' ) { | ||
| 190 | $file = $Input->path . $filename_value; | ||
| 191 | } | ||
| 192 | elsif ($prefix) { | ||
| 193 | $file = $prefix . '#' . $filename_value; | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $file = $Input->path . $Input->basename . '#' . $filename_value; | ||
| 197 | } | ||
| 198 | |||
| 199 | # add the file part number, if we're working with maximum line files | ||
| 200 | # padded for proper sorting | ||
| 201 | if ( defined $max ) { | ||
| 202 | if ( defined $out_files{$value}{'parts'} ) { | ||
| 203 | $out_files{$value}{'parts'} += 1; # increment | ||
| 204 | $file .= '_' . sprintf( "%03d", $out_files{$value}{'parts'} ); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | $out_files{$value}{'parts'} = 1; # initial | ||
| 208 | $file .= '_' . sprintf( "%03d", $out_files{$value}{'parts'} ); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | # only 1 part is necessary | ||
| 213 | $out_files{$value}{'parts'} = 1; | ||
| 214 | } | ||
| 215 | |||
| 216 | # finish the file name | ||
| 217 | $file .= $Input->extension; | ||
| 218 | $out_files{$value}{'number'} = 0; | ||
| 219 | |||
| 220 | # open an output Stream | ||
| 221 | if ( exists $out_files{$value}{'stream'} ) { | ||
| 222 | |||
| 223 | # an open stream, close it | ||
| 224 | $out_files{$value}{'stream'}->close_fh; | ||
| 225 | } | ||
| 226 | my $Stream = $Input->duplicate($file); | ||
| 227 | $out_files{$value}{'stream'} = $Stream; | ||
| 228 | |||
| 229 | # check the total | ||
| 230 | unless ( exists $out_files{$value}{'total'} ) { | ||
| 231 | $out_files{$value}{'total'} = 0; | ||
| 232 | } | ||
| 233 | |||
| 234 | # keept track of the number of files opened | ||
| 235 | $split_count++; | ||
| 236 | } | ||
| 237 | |||
| 238 | __END__ |