| 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; | ||
| 11 | use Bio::ToolBox::utility qw(parse_list ask_user_for_index format_with_commas); | ||
| 12 | |||
| 13 | our $VERSION = '2.03'; | ||
| 14 | |||
| 15 | print "\n This script will convert a data file to a GFF\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 ( | ||
| 32 | $infile, $outfile, $no_header, $chr_index, $start_index, | ||
| 33 | $stop_index, $score_index, $strand_index, $use_name, $id_index, | ||
| 34 | $source, $type, $tag, $ask, $unique, | ||
| 35 | $interbase, $sort_data, $gz, $bgz, $help, | ||
| 36 | $print_version, | ||
| 37 | ); | ||
| 38 | |||
| 39 | # Command line options | ||
| 40 | GetOptions( | ||
| 41 | 'i|in=s' => \$infile, # specify the input data file | ||
| 42 | 'o|out=s' => \$outfile, # name of output gff file | ||
| 43 | 'H|noheader' => \$no_header, # source has no header line | ||
| 44 | 'c|chr=i' => \$chr_index, # index of the chromosome column | ||
| 45 | 'b|begin|start=i' => \$start_index, # index of the start position column | ||
| 46 | 'e|stop|end=i' => \$stop_index, # index of the stop position coloumn | ||
| 47 | 's|score=i' => \$score_index, # index for the score column | ||
| 48 | 't|strand=i' => \$strand_index, # index for the strand column | ||
| 49 | 'n|name=s' => \$use_name, # index for the name column or the name text | ||
| 50 | 'd|id=i' => \$id_index, # index for the ID column | ||
| 51 | 'r|source=s' => \$source, # text to put in the source column | ||
| 52 | 'y|type=s' => \$type, # test to put in the type column | ||
| 53 | 'g|tag|tags=s' => \$tag, # comma list of tag column indices | ||
| 54 | 'a|ask' => \$ask, # request help in assigning indices | ||
| 55 | 'unique!' => \$unique, # make the names unique | ||
| 56 | '0|zero!' => \$interbase, # input file is interbase format | ||
| 57 | 'sort!' => \$sort_data, # sort the output file | ||
| 58 | 'z|gz!' => \$gz, # boolean to compress output file | ||
| 59 | 'Z|bgz!' => \$bgz, # compress with bgzip | ||
| 60 | 'h|help' => \$help, # request help | ||
| 61 | 'v|version' => \$print_version, # print the version | ||
| 62 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 63 | |||
| 64 | # Print help | ||
| 65 | if ($help) { | ||
| 66 | |||
| 67 | # print entire POD | ||
| 68 | pod2usage( | ||
| 69 | { | ||
| 70 | '-verbose' => 2, | ||
| 71 | '-exitval' => 1, | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | # Print version | ||
| 77 | if ($print_version) { | ||
| 78 | print " Biotoolbox script data2gff.pl, version $VERSION\n"; | ||
| 79 | eval { | ||
| 80 | require Bio::ToolBox; | ||
| 81 | my $v = Bio::ToolBox->VERSION; | ||
| 82 | print " Biotoolbox package version $v\n"; | ||
| 83 | }; | ||
| 84 | exit; | ||
| 85 | } | ||
| 86 | |||
| 87 | ### Check for required values | ||
| 88 | unless ($infile) { | ||
| 89 | if (@ARGV) { | ||
| 90 | $infile = shift @ARGV; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | print STDERR " FATAL: no input file! use --help for more information\n"; | ||
| 94 | exit 1; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | if ($bgz) { | ||
| 98 | $gz = 2; | ||
| 99 | $sort_data = 1; | ||
| 100 | } | ||
| 101 | |||
| 102 | # define name base or index | ||
| 103 | my ( $name_index, $name_base ); | ||
| 104 | if ( defined $use_name ) { | ||
| 105 | if ( $use_name =~ /^(\d+)$/ ) { | ||
| 106 | |||
| 107 | # looks like an index was provided | ||
| 108 | $name_index = $1; | ||
| 109 | } | ||
| 110 | elsif ( $use_name =~ /(\w+)/i ) { | ||
| 111 | |||
| 112 | # text that will be used as the name base when autogenerating | ||
| 113 | $name_base = $1; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | # define type base or index | ||
| 118 | my ( $type_index, $type_base ); | ||
| 119 | if ( defined $type ) { | ||
| 120 | if ( $type =~ /^(\d+)$/ ) { | ||
| 121 | |||
| 122 | # looks like an index was provided | ||
| 123 | $type_index = $1; | ||
| 124 | } | ||
| 125 | elsif ( $type =~ /(\w+)/i ) { | ||
| 126 | |||
| 127 | # text that will be used as the type base when autogenerating | ||
| 128 | $type_base = $1; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | # define source base or index | ||
| 133 | my ( $source_index, $source_base ); | ||
| 134 | if ( defined $source ) { | ||
| 135 | if ( $source =~ /^(\d+)$/ ) { | ||
| 136 | |||
| 137 | # looks like an index was provided | ||
| 138 | $source_index = $1; | ||
| 139 | } | ||
| 140 | elsif ( $source =~ /(\w+)/i ) { | ||
| 141 | |||
| 142 | # text that will be used as the source base when autogenerating | ||
| 143 | $source_base = $1; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | # gff attribute tag indices | ||
| 148 | my @tag_indices; | ||
| 149 | if ($tag) { | ||
| 150 | @tag_indices = parse_list($tag); | ||
| 151 | } | ||
| 152 | |||
| 153 | ### Load file | ||
| 154 | my $Input = Bio::ToolBox::Data->new( | ||
| 155 | in => $infile, | ||
| 156 | noheader => $no_header, | ||
| 157 | stream => 1, | ||
| 158 | ) or die "Unable to open file '$infile'!\n"; | ||
| 159 | |||
| 160 | ### Determine indices | ||
| 161 | if ($ask) { | ||
| 162 | |||
| 163 | # the user has specified that we should ask for specific indices | ||
| 164 | print " Press Return to accept the suggested index\n"; | ||
| 165 | |||
| 166 | # request chromosome index | ||
| 167 | unless ( defined $chr_index ) { | ||
| 168 | my $suggestion = $Input->chromo_column; | ||
| 169 | $chr_index = ask_user_for_index( $Input, | ||
| 170 | " Enter the index for the chromosome column [$suggestion]: " ); | ||
| 171 | $chr_index = defined $chr_index ? $chr_index : $suggestion; | ||
| 172 | unless ( defined $chr_index ) { | ||
| 173 | print STDERR " FATAL: No identifiable chromosome column index!\n"; | ||
| 174 | exit 1; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | # request start index | ||
| 179 | unless ( defined $start_index ) { | ||
| 180 | my $suggestion = $Input->start_column; | ||
| 181 | $start_index = ask_user_for_index( $Input, | ||
| 182 | " Enter the index for the start column [$suggestion]: " ); | ||
| 183 | $start_index = defined $start_index ? $start_index : $suggestion; | ||
| 184 | unless ( defined $start_index ) { | ||
| 185 | print STDERR " FATAL: No identifiable start position column index!\n"; | ||
| 186 | exit 1; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | # request stop index | ||
| 191 | unless ( defined $stop_index ) { | ||
| 192 | my $suggestion = $Input->stop_column; | ||
| 193 | $stop_index = ask_user_for_index( $Input, | ||
| 194 | " Enter the index for the stop or end column [$suggestion]: " ); | ||
| 195 | $stop_index = defined $stop_index ? $stop_index : $suggestion; | ||
| 196 | unless ( defined $stop_index ) { | ||
| 197 | print STDERR " FATAL: No identifiable stop position column index!\n"; | ||
| 198 | exit 1; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | # request source text | ||
| 203 | unless ( defined $source ) { | ||
| 204 | |||
| 205 | # this is a special input, can't use the ask_user_for_index sub | ||
| 206 | # accepts either index or text string | ||
| 207 | my $default = $Input->basename; | ||
| 208 | my $p = " Enter the text string or column index for the GFF source [$default]: "; | ||
| 209 | my $in = prompt( $p, $default ); | ||
| 210 | if ( $in =~ /^(\d+)$/ ) { | ||
| 211 | $source_index = $1; | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | $source_base = $in; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | # request type text | ||
| 219 | unless ( defined $type ) { | ||
| 220 | |||
| 221 | # this is a special input, can't use the ask_user_for_index sub | ||
| 222 | # accepts either index or text string | ||
| 223 | my $default = $Input->feature || 'feature'; | ||
| 224 | my $p = " Enter the text string or column index for the GFF type [$default]: "; | ||
| 225 | my $in = prompt( $p, $default ); | ||
| 226 | if ( $in =~ /^(\d+)$/ ) { | ||
| 227 | $type_index = $1; | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | $type_base = $in; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # request score index | ||
| 235 | unless ( defined $score_index ) { | ||
| 236 | my $suggestion = $Input->find_column('^score$'); | ||
| 237 | $score_index = ask_user_for_index( $Input, | ||
| 238 | " Enter the index for the feature score column [$suggestion]: " ); | ||
| 239 | $score_index = defined $score_index ? $score_index : $suggestion; | ||
| 240 | } | ||
| 241 | |||
| 242 | # request strand index | ||
| 243 | unless ( defined $strand_index ) { | ||
| 244 | my $suggestion = $Input->strand_column; | ||
| 245 | $strand_index = ask_user_for_index( $Input, | ||
| 246 | " Enter the index for the feature strand column [$suggestion]: " ); | ||
| 247 | $strand_index = defined $strand_index ? $strand_index : $suggestion; | ||
| 248 | } | ||
| 249 | |||
| 250 | # request name index or text | ||
| 251 | unless ( defined $use_name ) { | ||
| 252 | |||
| 253 | # this is a special input, can't use the ask_user_for_index sub | ||
| 254 | # accepts either index or text string | ||
| 255 | my $suggestion = $Input->name_column; | ||
| 256 | my $p = " Enter the index for the feature name column or\n" | ||
| 257 | . " the base text for auto-generated names: "; | ||
| 258 | my $in = prompt( $p, $suggestion ); | ||
| 259 | if ( $in =~ /^(\d+)$/ ) { | ||
| 260 | $name_index = $1; | ||
| 261 | } | ||
| 262 | elsif ( $in =~ /\w+/ ) { | ||
| 263 | $name_base = $in; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | # request ID index | ||
| 268 | unless ( defined $id_index ) { | ||
| 269 | my $suggestion = $Input->id_column; | ||
| 270 | $suggestion = q() unless $suggestion; | ||
| 271 | $id_index = ask_user_for_index( $Input, | ||
| 272 | " Enter the index for the feature unique ID column [$suggestion]: " ); | ||
| 273 | $id_index = defined $id_index ? $id_index : $suggestion; | ||
| 274 | } | ||
| 275 | |||
| 276 | # request tags | ||
| 277 | unless ( defined $tag ) { | ||
| 278 | @tag_indices = ask_user_for_index( $Input, | ||
| 279 | " Enter zero or more column indices for GFF group tags " ); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | else { | ||
| 283 | # or else the indices need to be automatically identified | ||
| 284 | unless ( $Input->feature_type eq 'coordinate' | ||
| 285 | or ( defined $chr_index and defined $start_index and defined $stop_index ) | ||
| 286 | or ( $Input->feature_type eq 'named' and $Input->database ) ) | ||
| 287 | { | ||
| 288 | print STDERR | ||
| 289 | " FATAL: Not enough information has been provided to convert to GFF file.\n" | ||
| 290 | . "Coordinate column names must be recognizable or specified. Use --help\n"; | ||
| 291 | exit; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | ### Open output data | ||
| 296 | my $Output = Bio::ToolBox::Data->new( gff => 3, ) | ||
| 297 | or die " unable to create output data strucutre!\n"; | ||
| 298 | |||
| 299 | ### Convert the input stream | ||
| 300 | # check some things first | ||
| 301 | my $do_feature = $Input->feature_type eq 'named' ? 1 : 0; # get features from db? | ||
| 302 | if ( defined $start_index and substr( $Input->name($start_index), -1 ) eq '0' ) { | ||
| 303 | |||
| 304 | # start column name suggests it is 0-based | ||
| 305 | $interbase = 1; | ||
| 306 | } | ||
| 307 | if ( $unique and not( defined $name_index or $name_base ) ) { | ||
| 308 | print STDERR | ||
| 309 | " FATAL: must provide a name index or name base to make unique feature names!\n"; | ||
| 310 | exit 1; | ||
| 311 | } | ||
| 312 | my $unique_name_counter = {}; # hash for making unique feature names | ||
| 313 | my $unique_id_counter = {}; # same for IDs | ||
| 314 | my $count = 0; # the number of lines processed | ||
| 315 | while ( my $row = $Input->next_row ) { | ||
| 316 | |||
| 317 | # get the feature from the db if necessary | ||
| 318 | if ($do_feature) { | ||
| 319 | my $f = $row->feature; | ||
| 320 | } | ||
| 321 | |||
| 322 | # build the arguments | ||
| 323 | # retrieve information from row object if indices were provided | ||
| 324 | my @args; | ||
| 325 | if ($chr_index) { | ||
| 326 | my $c = $row->value($chr_index); | ||
| 327 | next if $c eq '.'; | ||
| 328 | push @args, 'chromo', $c; | ||
| 329 | } | ||
| 330 | if ($start_index) { | ||
| 331 | my $s = $row->value($start_index); | ||
| 332 | next if $s eq '.'; | ||
| 333 | $s += 1 if $interbase; | ||
| 334 | push @args, 'start', $s; | ||
| 335 | } | ||
| 336 | if ($stop_index) { | ||
| 337 | push @args, 'stop', $row->value($stop_index); | ||
| 338 | } | ||
| 339 | if ($strand_index) { | ||
| 340 | push @args, 'strand', $row->value($strand_index); | ||
| 341 | } | ||
| 342 | if ($score_index) { | ||
| 343 | push @args, 'score', $row->value($score_index); | ||
| 344 | } | ||
| 345 | if ($name_index) { | ||
| 346 | my $name = | ||
| 347 | $unique | ||
| 348 | ? generate_unique_name( $row->value($name_index), $unique_name_counter ) | ||
| 349 | : $row->value($name_index); | ||
| 350 | push @args, 'name', $name; | ||
| 351 | } | ||
| 352 | elsif ($name_base) { | ||
| 353 | push @args, 'name', sprintf( "%s_%d", $name_base, $count ); | ||
| 354 | } | ||
| 355 | if ($id_index) { | ||
| 356 | my $id = $row->value($id_index); | ||
| 357 | push @args, 'id', generate_unique_name( $id, $unique_id_counter ); | ||
| 358 | } | ||
| 359 | if ($type_index) { | ||
| 360 | push @args, 'type', $row->value($type_index); | ||
| 361 | } | ||
| 362 | elsif ($type_base) { | ||
| 363 | push @args, 'type', $type_base; | ||
| 364 | } | ||
| 365 | if ($source_index) { | ||
| 366 | push @args, 'source', $row->value($source_index); | ||
| 367 | } | ||
| 368 | elsif ($source_base) { | ||
| 369 | push @args, 'source', $source_base; | ||
| 370 | } | ||
| 371 | if (@tag_indices) { | ||
| 372 | push @args, 'attributes', \@tag_indices; | ||
| 373 | } | ||
| 374 | |||
| 375 | # add to output | ||
| 376 | my $string = $row->gff_string(@args); | ||
| 377 | $Output->add_row($string) if length($string); | ||
| 378 | |||
| 379 | # weirdly, this should work, as the add_row will split the columns of | ||
| 380 | # the gff string automatically | ||
| 381 | $count++; | ||
| 382 | } | ||
| 383 | |||
| 384 | ### Finish | ||
| 385 | $Input->close_fh; | ||
| 386 | if ($sort_data) { | ||
| 387 | print " Sorting data...\n"; | ||
| 388 | $Output->gsort_data; | ||
| 389 | } | ||
| 390 | unless ($outfile) { | ||
| 391 | $outfile = sprintf "%s%s.gff", $Input->path, $Input->basename; | ||
| 392 | } | ||
| 393 | $outfile = $Output->write_file( | ||
| 394 | filename => $outfile, | ||
| 395 | gz => $gz, | ||
| 396 | ); | ||
| 397 | |||
| 398 | printf " Converted %s lines of input data to GFF file '%s'\n", | ||
| 399 | format_with_commas($count), $outfile; | ||
| 400 | |||
| 401 | sub generate_unique_name { | ||
| 402 | my ( $name, $counter ) = @_; | ||
| 403 | my $new_name; | ||
| 404 | |||
| 405 | # check uniqueness | ||
| 406 | if ( exists $counter->{$name} ) { | ||
| 407 | |||
| 408 | # we've encountered this name before | ||
| 409 | # generate a unique name by appending the count number | ||
| 410 | $counter->{$name} += 1; | ||
| 411 | $new_name = $name . '.' . $counter->{$name}; | ||
| 412 | } | ||
| 413 | else { | ||
| 414 | # first time for this name | ||
| 415 | # record in the hash | ||
| 416 | $new_name = $name; | ||
| 417 | $counter->{$name} = 0; | ||
| 418 | } | ||
| 419 | return $new_name; | ||
| 420 | } | ||
| 421 | |||
| 422 | __END__ |