| 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::MoreUtils qw(mesh); | ||
| 10 | use IO::Prompt::Tiny qw(prompt); | ||
| 11 | use Bio::ToolBox::Data; | ||
| 12 | use Bio::ToolBox::utility qw( ask_user_for_index format_with_commas ); | ||
| 13 | use Bio::ToolBox::big_helper qw(bed_to_bigbed_conversion); | ||
| 14 | |||
| 15 | our $VERSION = '2.03'; | ||
| 16 | |||
| 17 | print "\n This program will write a BED file\n"; | ||
| 18 | |||
| 19 | ### Quick help | ||
| 20 | unless (@ARGV) { | ||
| 21 | |||
| 22 | # when no command line options are present | ||
| 23 | # print SYNOPSIS | ||
| 24 | pod2usage( | ||
| 25 | { | ||
| 26 | '-verbose' => 0, | ||
| 27 | '-exitval' => 1, | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | } | ||
| 31 | |||
| 32 | ### Get command line options and initialize values | ||
| 33 | my ( | ||
| 34 | $infile, $outfile, $bed, $chr_index, $start_index, | ||
| 35 | $stop_index, $name, $score_index, $strand_index, $zero_based, | ||
| 36 | $no_header, $ask, $bigbed, $bb_app_path, $database, | ||
| 37 | $chromo_file, $sort_data, $gz, $bgz, $help, | ||
| 38 | $print_version, | ||
| 39 | ); | ||
| 40 | |||
| 41 | # Command line options | ||
| 42 | GetOptions( | ||
| 43 | 'i|in=s' => \$infile, # the solexa data file | ||
| 44 | 'o|out=s' => \$outfile, # name of output file | ||
| 45 | 'H|noheader' => \$no_header, # source has no header line | ||
| 46 | 'bed=i' => \$bed, # number of bed columns | ||
| 47 | 'a|ask' => \$ask, # request help in assigning indices | ||
| 48 | 'c|chr=i' => \$chr_index, # index of the chromosome column | ||
| 49 | 'b|begin|start=i' => \$start_index, # index of the start position column | ||
| 50 | 'e|stop|end=i' => \$stop_index, # index of the stop position coloumn | ||
| 51 | 'n|name=s' => \$name, # index for the name column | ||
| 52 | 's|score=i' => \$score_index, # index for the score column | ||
| 53 | 't|strand=i' => \$strand_index, # index for the strand column | ||
| 54 | '0|zero!' => \$zero_based, # source is 0-based numbering, convert | ||
| 55 | 'B|bigbed|bb' => \$bigbed, # generate a binary bigbed file | ||
| 56 | 'd|db=s' => \$database, # database for bigbed file generation | ||
| 57 | 'chromof=s' => \$chromo_file, # name of a chromosome file | ||
| 58 | 'bbapp=s' => \$bb_app_path, # path to bedToBigBed utility | ||
| 59 | 'sort!' => \$sort_data, # sort the output file | ||
| 60 | 'z|gz!' => \$gz, # compress output | ||
| 61 | 'Z|bgz!' => \$bgz, # compress with bgzip | ||
| 62 | 'h|help' => \$help, # request help | ||
| 63 | 'v|version' => \$print_version, # print the version | ||
| 64 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 65 | |||
| 66 | # Print help | ||
| 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 data2bed.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 requirements | ||
| 90 | unless ($infile) { | ||
| 91 | if (@ARGV) { | ||
| 92 | $infile = shift @ARGV; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | print STDERR " FATAL: no input file! use --help for more information\n"; | ||
| 96 | exit 1; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | if ($bed) { | ||
| 100 | unless ( $bed == 3 or $bed == 4 or $bed == 5 or $bed == 6 ) { | ||
| 101 | print STDERR " FATAL: bed must be 3, 4, 5, or 6!\n"; | ||
| 102 | exit 1; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | if ($bigbed) { | ||
| 106 | |||
| 107 | # do not allow compression when converting to bigbed | ||
| 108 | $gz = 0; | ||
| 109 | $bgz = 0; | ||
| 110 | $sort_data = 1 if not defined $sort_data; | ||
| 111 | } | ||
| 112 | if ($bgz) { | ||
| 113 | $gz = 2; | ||
| 114 | $sort_data = 1 if not defined $sort_data; | ||
| 115 | } | ||
| 116 | |||
| 117 | # define name base or index | ||
| 118 | my $name_index = 0; | ||
| 119 | my $name_base = q(); | ||
| 120 | if ( defined $name ) { | ||
| 121 | if ( $name =~ /^(\d+)$/ ) { | ||
| 122 | |||
| 123 | # looks like an index was provided | ||
| 124 | $name_index = $1; | ||
| 125 | } | ||
| 126 | elsif ( $name =~ /(\w+)/i ) { | ||
| 127 | |||
| 128 | # text that will be used as the name base when autogenerating | ||
| 129 | $name_base = $1; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | ### Load file | ||
| 134 | my $Input = Bio::ToolBox::Data->new( | ||
| 135 | in => $infile, | ||
| 136 | noheader => $no_header, | ||
| 137 | stream => 1, | ||
| 138 | ) or die "Unable to open file '$infile'!\n"; | ||
| 139 | |||
| 140 | if ($bigbed) { | ||
| 141 | |||
| 142 | # identify database if needed | ||
| 143 | unless ( $database or $chromo_file ) { | ||
| 144 | $database = $Input->database | ||
| 145 | or die | ||
| 146 | " FATAL: No database name or chromosome file provided for generating bigbed file!\n"; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | ### Determine indices | ||
| 151 | |||
| 152 | # Ask user interactively | ||
| 153 | if ($ask) { | ||
| 154 | |||
| 155 | # the user has specified that we should ask for specific indices | ||
| 156 | print " Press Return to accept the suggested index\n"; | ||
| 157 | |||
| 158 | # request chromosome index | ||
| 159 | unless ($chr_index) { | ||
| 160 | my $suggestion = $Input->chromo_column || q(); | ||
| 161 | $chr_index = ask_user_for_index( $Input, | ||
| 162 | " Enter the index for the chromosome column [$suggestion] " ); | ||
| 163 | $chr_index = $chr_index ? $chr_index : $suggestion; | ||
| 164 | unless ( defined $chr_index ) { | ||
| 165 | print STDERR " FATAL: No identifiable chromosome column index!\n"; | ||
| 166 | exit 1; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | # request start index | ||
| 171 | unless ($start_index) { | ||
| 172 | my $suggestion = $Input->start_column || q(); | ||
| 173 | $start_index = ask_user_for_index( $Input, | ||
| 174 | " Enter the index for the start column [$suggestion] " ); | ||
| 175 | $start_index = $start_index ? $start_index : $suggestion; | ||
| 176 | unless ( defined $start_index ) { | ||
| 177 | print STDERR " FATAL: No identifiable start position column index!\n"; | ||
| 178 | exit 1; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | # request stop index | ||
| 183 | unless ($stop_index) { | ||
| 184 | my $suggestion = $Input->stop_column || q(); | ||
| 185 | $stop_index = ask_user_for_index( $Input, | ||
| 186 | " Enter the index for the stop or end column [$suggestion] " ); | ||
| 187 | $stop_index = $stop_index ? $stop_index : $suggestion; | ||
| 188 | unless ( defined $stop_index ) { | ||
| 189 | print STDERR " FATAL: No identifiable stop position column index!\n"; | ||
| 190 | exit 1; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | # request name index or text | ||
| 195 | unless ($name) { | ||
| 196 | |||
| 197 | # this is a special input, can't use the ask_user_for_index sub | ||
| 198 | # accepts either index or text string | ||
| 199 | my $suggestion = $Input->name_column || q(); | ||
| 200 | my $prompt = " Enter the index for the feature name column or\n" | ||
| 201 | . " the base text for auto-generated names [$suggestion] "; | ||
| 202 | my $in = prompt($prompt); | ||
| 203 | if ( $in =~ /^(\d+)$/ ) { | ||
| 204 | $name_index = $1; | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | $name_base = $in; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # request score index | ||
| 212 | unless ($score_index) { | ||
| 213 | my $suggestion = $Input->find_column('^score$') || q(); | ||
| 214 | $score_index = ask_user_for_index( $Input, | ||
| 215 | " Enter the index for the feature score column [$suggestion] " ); | ||
| 216 | $score_index = $score_index ? $score_index : $suggestion; | ||
| 217 | } | ||
| 218 | |||
| 219 | # request strand index | ||
| 220 | unless ($strand_index) { | ||
| 221 | my $suggestion = $Input->strand_column || q(); | ||
| 222 | $strand_index = ask_user_for_index( $Input, | ||
| 223 | " Enter the index for the feature strand column [$suggestion] " ); | ||
| 224 | $strand_index = $strand_index ? $strand_index : $suggestion; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | # or else the indices need to be automatically identified | ||
| 229 | unless ( $Input->feature_type eq 'coordinate' | ||
| 230 | or ( $chr_index and $start_index and $stop_index ) | ||
| 231 | or ( $Input->feature_type eq 'named' and ( $database or $Input->database ) ) ) | ||
| 232 | { | ||
| 233 | print STDERR | ||
| 234 | " FATAL: Not enough information has been provided to convert to bed file.\n" | ||
| 235 | . "Coordinate column names must be recognizable or specified. Use --help\n"; | ||
| 236 | exit 1; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | # print summary of columns | ||
| 241 | printf " Converting using \n - chromosome index %s\n - start index %s\n" | ||
| 242 | . " - stop index %s\n - name index %s\n - score index %s\n - strand index %s\n", | ||
| 243 | $chr_index ? $chr_index | ||
| 244 | : $Input->chromo_column ? $Input->chromo_column | ||
| 245 | : '-', $start_index ? $start_index | ||
| 246 | : $Input->start_column ? $Input->start_column | ||
| 247 | : '-', $stop_index ? $stop_index | ||
| 248 | : $Input->stop_column ? $Input->stop_column | ||
| 249 | : '-', $name_index ? $name_index | ||
| 250 | : $name_base ? $name_base | ||
| 251 | : $Input->name_column ? $Input->name_column | ||
| 252 | : '-', $score_index ? $score_index | ||
| 253 | : $Input->score_column ? $Input->score_column | ||
| 254 | : '-', $strand_index ? $strand_index | ||
| 255 | : $Input->strand_column ? $Input->strand_column | ||
| 256 | : '-'; | ||
| 257 | |||
| 258 | # generate arguments list | ||
| 259 | my @arg_keys; | ||
| 260 | my @arg_indices; | ||
| 261 | if ($chr_index) { | ||
| 262 | push @arg_keys, 'chromo'; | ||
| 263 | push @arg_indices, $chr_index; | ||
| 264 | } | ||
| 265 | if ($start_index) { | ||
| 266 | push @arg_keys, 'start'; | ||
| 267 | push @arg_indices, $start_index; | ||
| 268 | } | ||
| 269 | if ($stop_index) { | ||
| 270 | push @arg_keys, 'stop'; | ||
| 271 | push @arg_indices, $stop_index; | ||
| 272 | } | ||
| 273 | if ($name_index) { | ||
| 274 | push @arg_keys, 'name'; | ||
| 275 | push @arg_indices, $name_index; | ||
| 276 | } | ||
| 277 | if ($score_index) { | ||
| 278 | push @arg_keys, 'score'; | ||
| 279 | push @arg_indices, $score_index; | ||
| 280 | } | ||
| 281 | if ($strand_index) { | ||
| 282 | push @arg_keys, 'strand'; | ||
| 283 | push @arg_indices, $strand_index; | ||
| 284 | } | ||
| 285 | |||
| 286 | # determine minimum bed | ||
| 287 | unless ($bed) { | ||
| 288 | if ( $strand_index or $Input->strand_column ) { | ||
| 289 | $bed = 6; | ||
| 290 | } | ||
| 291 | elsif ($score_index) { | ||
| 292 | $bed = 5; | ||
| 293 | } | ||
| 294 | elsif ( $name_index or $name_base or $Input->name_column ) { | ||
| 295 | $bed = 4; | ||
| 296 | } | ||
| 297 | else { | ||
| 298 | $bed = 3; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | printf " Writing as bed%d\n", $bed; | ||
| 302 | |||
| 303 | # check for zero start | ||
| 304 | if ( $start_index and substr( $Input->name($start_index), -1 ) eq '0' ) { | ||
| 305 | $zero_based = 1; # name suggests 0-based indexing | ||
| 306 | } | ||
| 307 | |||
| 308 | ### Convert the input stream | ||
| 309 | # Open output data | ||
| 310 | my $Output = Bio::ToolBox::Data->new( bed => $bed, ) | ||
| 311 | or die " unable to create output data strucutre!\n"; | ||
| 312 | |||
| 313 | my $count = 0; # the number of lines processed | ||
| 314 | while ( my $row = $Input->next_row ) { | ||
| 315 | |||
| 316 | # build the arguments | ||
| 317 | # retrieve information from row object if indices were provided | ||
| 318 | my @values = map { $row->value($_) } @arg_indices; | ||
| 319 | my %args = mesh( @arg_keys, @values ); | ||
| 320 | $args{bed} = $bed; | ||
| 321 | |||
| 322 | # extras | ||
| 323 | if ($name_base) { | ||
| 324 | $args{name} = sprintf( "%s_%d", $name_base, $count ); | ||
| 325 | } | ||
| 326 | if ( $zero_based and $start_index ) { | ||
| 327 | $args{start} += 1; | ||
| 328 | } | ||
| 329 | |||
| 330 | # write | ||
| 331 | my $string = $row->bed_string(%args); | ||
| 332 | $Output->add_row($string) if length($string); | ||
| 333 | |||
| 334 | # weirdly, this should work, as the add_row will split the columns of | ||
| 335 | # the gff string automatically | ||
| 336 | $count++; | ||
| 337 | } | ||
| 338 | |||
| 339 | ### Finish | ||
| 340 | $Input->close_fh; | ||
| 341 | if ($sort_data) { | ||
| 342 | print " Sorting data...\n"; | ||
| 343 | $Output->gsort_data; | ||
| 344 | if ($bigbed) { | ||
| 345 | |||
| 346 | # re-sort again by chromosome in ascibetical order | ||
| 347 | # because that is what bedToBigBed requires | ||
| 348 | $Output->sort_data( 1, 'i' ); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | unless ($outfile) { | ||
| 352 | $outfile = sprintf "%s%s.bed", $Input->path, $Input->basename; | ||
| 353 | } | ||
| 354 | $outfile = $Output->write_file( | ||
| 355 | filename => $outfile, | ||
| 356 | gz => $gz, | ||
| 357 | ); | ||
| 358 | |||
| 359 | ### Convert to BigBed format | ||
| 360 | if ($bigbed) { | ||
| 361 | |||
| 362 | # requested to continue and generate a binary bigbed file | ||
| 363 | printf " wrote %s lines to temporary bed file '%s'\n", | ||
| 364 | format_with_commas($count), $Output->filename; | ||
| 365 | print " converting to bigbed file....\n"; | ||
| 366 | |||
| 367 | # perform the conversion | ||
| 368 | my $bb_file = bed_to_bigbed_conversion( | ||
| 369 | 'bed' => $Output->filename, | ||
| 370 | 'db' => $database, | ||
| 371 | 'chromo' => $chromo_file, | ||
| 372 | 'bbapppath' => $bb_app_path, | ||
| 373 | ); | ||
| 374 | |||
| 375 | # confirm | ||
| 376 | if ($bb_file) { | ||
| 377 | print " BigBed file '$bb_file' generated\n"; | ||
| 378 | unlink $outfile; # remove the bed file | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | die " BigBed file not generated! see standard error\n"; | ||
| 382 | } | ||
| 383 | |||
| 384 | } | ||
| 385 | else { | ||
| 386 | printf " wrote %s lines to BED file '%s'\n", format_with_commas($count), $outfile; | ||
| 387 | } | ||
| 388 | |||
| 389 | __END__ |