| 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; | ||
| 10 | use Bio::ToolBox::Parser; | ||
| 11 | use Bio::ToolBox::GeneTools qw(gtf_string); | ||
| 12 | use Bio::ToolBox::utility qw(format_with_commas sane_chromo_sort); | ||
| 13 | |||
| 14 | # check for additional requirements | ||
| 15 | my $net = 0; | ||
| 16 | eval { | ||
| 17 | require Net::FTP; | ||
| 18 | $net = 1; | ||
| 19 | }; | ||
| 20 | |||
| 21 | our $VERSION = '2.00'; | ||
| 22 | |||
| 23 | print "\n A script to convert UCSC tables to GFF3 files\n\n"; | ||
| 24 | |||
| 25 | ### Quick help | ||
| 26 | unless (@ARGV) { | ||
| 27 | |||
| 28 | # when no command line options are present | ||
| 29 | # print SYNOPSIS | ||
| 30 | pod2usage( | ||
| 31 | { | ||
| 32 | '-verbose' => 0, | ||
| 33 | '-exitval' => 1, | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | |||
| 38 | ### Command line options | ||
| 39 | my ( | ||
| 40 | $ftp_file, $database, $host, $do_chromo, $kgxreff, | ||
| 41 | $chromof, $user_source, $do_gene, $do_cds, $do_utr, | ||
| 42 | $do_codon, $share, $do_name, $do_gtf, $gz, | ||
| 43 | $help, $print_version, | ||
| 44 | ); | ||
| 45 | my @genetables; | ||
| 46 | GetOptions( | ||
| 47 | 'f|ftp=s' => \$ftp_file, # which database table to retrieve | ||
| 48 | 'd|db=s' => \$database, # which ucsc genome to use | ||
| 49 | 'h|host=s' => \$host, # the ftp server to connect to | ||
| 50 | 'chr!' => \$do_chromo, # include the chromosome file from ftp | ||
| 51 | 't|table=s' => \@genetables, # the input gene table files | ||
| 52 | 'k|kgxref=s' => \$kgxreff, # the kgXref info file | ||
| 53 | 'c|chromo=s' => \$chromof, # a chromosome file | ||
| 54 | 'source=s' => \$user_source, # user provided source | ||
| 55 | 'gene!' => \$do_gene, # include genes in output | ||
| 56 | 'cds!' => \$do_cds, # include CDS in output | ||
| 57 | 'utr!' => \$do_utr, # include UTRs in output | ||
| 58 | 'codon!' => \$do_codon, # include start & stop codons in output | ||
| 59 | 'share!' => \$share, # share common exons and UTRs | ||
| 60 | 'name!' => \$do_name, # assign names to CDSs, UTRs, and exons | ||
| 61 | 'g|gtf!' => \$do_gtf, # write a gtf file instead | ||
| 62 | 'z|gz!' => \$gz, # compress file | ||
| 63 | 'h|help' => \$help, # request help | ||
| 64 | 'v|version' => \$print_version, # print the version | ||
| 65 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 66 | |||
| 67 | # Print help | ||
| 68 | if ($help) { | ||
| 69 | |||
| 70 | # print entire POD | ||
| 71 | pod2usage( | ||
| 72 | { | ||
| 73 | '-verbose' => 2, | ||
| 74 | '-exitval' => 1, | ||
| 75 | } | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | # Print version | ||
| 80 | if ($print_version) { | ||
| 81 | print " Biotoolbox script ucsc_table2gff3.pl, version $VERSION\n"; | ||
| 82 | my $v = Bio::ToolBox->VERSION; | ||
| 83 | print " Biotoolbox package version $v\n"; | ||
| 84 | exit; | ||
| 85 | } | ||
| 86 | |||
| 87 | ### Check requirements and defaults | ||
| 88 | check_options(); | ||
| 89 | my $start_time = time; | ||
| 90 | |||
| 91 | ### Fetch files if requested | ||
| 92 | if ($ftp_file) { | ||
| 93 | |||
| 94 | # collect the requested files by ftp | ||
| 95 | my @files = fetch_files_by_ftp(); | ||
| 96 | |||
| 97 | # push file names into appropriate variables | ||
| 98 | foreach my $file (@files) { | ||
| 99 | if ( $file =~ m/refgene | knowngene /xi ) { | ||
| 100 | push @genetables, $file; | ||
| 101 | } | ||
| 102 | elsif ( $file =~ /kgxref/i ) { | ||
| 103 | $kgxreff = $file; | ||
| 104 | } | ||
| 105 | elsif ( $file =~ /chrom/i ) { | ||
| 106 | $chromof = $file; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | ### Walk through the input tables | ||
| 112 | foreach my $file (@genetables) { | ||
| 113 | |||
| 114 | # Initiate the parser | ||
| 115 | my $ucsc = Bio::ToolBox::Parser->new( | ||
| 116 | file => $file, | ||
| 117 | do_gene => $do_gene, | ||
| 118 | do_exon => 1, | ||
| 119 | do_cds => $do_cds, | ||
| 120 | do_utr => $do_utr, | ||
| 121 | do_codon => $do_codon, | ||
| 122 | do_name => $do_name, | ||
| 123 | share => $share, | ||
| 124 | ) or die "cannot initialize Parser!"; | ||
| 125 | |||
| 126 | # add options | ||
| 127 | if ($user_source) { | ||
| 128 | $ucsc->source($user_source); | ||
| 129 | } | ||
| 130 | if ( $file =~ /knowngene/i and $kgxreff ) { | ||
| 131 | my $c = $ucsc->load_extra_data( $kgxreff, 'kgxref' ); | ||
| 132 | printf " Loaded %s transcripts from supplemental data file '$kgxreff'\n", | ||
| 133 | format_with_commas($c); | ||
| 134 | } | ||
| 135 | |||
| 136 | # open output file | ||
| 137 | my ( $outfile, $gff_fh ) = open_output_gff($file); | ||
| 138 | |||
| 139 | # process chromosome | ||
| 140 | if ($chromof) { | ||
| 141 | |||
| 142 | # we will write sequence-region pragmas for every gff file automatically | ||
| 143 | # the current gff parser and Bio::DB::SeqFeature will correctly parse them | ||
| 144 | print_chromosomes($gff_fh); | ||
| 145 | } | ||
| 146 | |||
| 147 | # convert the table | ||
| 148 | print " Converting gene table '$file' features....\n"; | ||
| 149 | unless ( $ucsc->open_file($file) ) { | ||
| 150 | print " WARNING: Unable to open file!\n"; | ||
| 151 | next; | ||
| 152 | } | ||
| 153 | print_current_gene_list( $gff_fh, $ucsc ); | ||
| 154 | |||
| 155 | # report outcomes | ||
| 156 | my $count = $ucsc->counts; | ||
| 157 | print " converted ", format_with_commas( $count->{gene} ), " gene features\n" | ||
| 158 | if ( exists $count->{gene} and $count->{gene} > 0 ); | ||
| 159 | print " converted ", format_with_commas( $count->{mrna} ), " mRNA transcripts\n" | ||
| 160 | if ( exists $count->{mrna} and $count->{mrna} > 0 ); | ||
| 161 | print " converted ", format_with_commas( $count->{pseudogene} ), | ||
| 162 | " pseudogene transcripts\n" | ||
| 163 | if ( exists $count->{pseudogene} and $count->{pseudogene} > 0 ); | ||
| 164 | print " converted ", format_with_commas( $count->{ncrna} ), " ncRNA transcripts\n" | ||
| 165 | if ( exists $count->{ncrna} and $count->{ncrna} > 0 ); | ||
| 166 | print " converted ", format_with_commas( $count->{mirna} ), " miRNA transcripts\n" | ||
| 167 | if ( exists $count->{mirna} and $count->{mirna} > 0 ); | ||
| 168 | print " converted ", format_with_commas( $count->{snrna} ), " snRNA transcripts\n" | ||
| 169 | if ( exists $count->{snrna} and $count->{snrna} > 0 ); | ||
| 170 | print " converted ", format_with_commas( $count->{snorna} ), " snoRNA transcripts\n" | ||
| 171 | if ( exists $count->{snorna} and $count->{snorna} > 0 ); | ||
| 172 | print " converted ", format_with_commas( $count->{trna} ), " tRNA transcripts\n" | ||
| 173 | if ( exists $count->{trna} and $count->{trna} > 0 ); | ||
| 174 | print " converted ", format_with_commas( $count->{rrna} ), " rRNA transcripts\n" | ||
| 175 | if ( exists $count->{rrna} and $count->{rrna} > 0 ); | ||
| 176 | print " converted ", format_with_commas( $count->{other} ), " other transcripts\n" | ||
| 177 | if ( exists $count->{other} and $count->{other} > 0 ); | ||
| 178 | |||
| 179 | # Finished | ||
| 180 | printf " wrote file '$outfile' in %.1f minutes\n", ( time - $start_time ) / 60; | ||
| 181 | } | ||
| 182 | |||
| 183 | ### Finish | ||
| 184 | exit; | ||
| 185 | |||
| 186 | ######################### Subroutines ####################################### | ||
| 187 | |||
| 188 | sub check_options { | ||
| 189 | unless ( @genetables or $ftp_file or $chromof ) { | ||
| 190 | print STDERR | ||
| 191 | " FATAL: Specify either an input table file, chromosome file, or a FTP table!\n"; | ||
| 192 | exit 1; | ||
| 193 | } | ||
| 194 | if ($ftp_file) { | ||
| 195 | unless ($net) { | ||
| 196 | print STDERR | ||
| 197 | " FATAL: please install Perl module Net::FTP to download files\n"; | ||
| 198 | exit 1; | ||
| 199 | } | ||
| 200 | unless ( $ftp_file =~ m/^ refgene | known | all $/xi ) { | ||
| 201 | print STDERR | ||
| 202 | " FATAL: requested table '$ftp_file' by FTP not supported! see help\n"; | ||
| 203 | exit 1; | ||
| 204 | } | ||
| 205 | unless ( defined $database ) { | ||
| 206 | print STDERR " FATAL: a UCSC genome database must be provided! see help\n"; | ||
| 207 | exit 1; | ||
| 208 | } | ||
| 209 | unless ( defined $do_chromo ) { | ||
| 210 | $do_chromo = 1; | ||
| 211 | } | ||
| 212 | unless ( defined $host ) { | ||
| 213 | $host = 'hgdownload.cse.ucsc.edu'; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | unless ( defined $do_gene ) { | ||
| 217 | $do_gene = 1; | ||
| 218 | } | ||
| 219 | unless ( defined $do_utr ) { | ||
| 220 | $do_utr = 0; | ||
| 221 | } | ||
| 222 | unless ( defined $do_cds ) { | ||
| 223 | $do_cds = 1; | ||
| 224 | unless ( defined $do_codon ) { | ||
| 225 | $do_codon = 0; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | unless ( defined $share ) { | ||
| 229 | $share = 1; | ||
| 230 | } | ||
| 231 | unless ( defined $do_name ) { | ||
| 232 | $do_name = 0; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | sub fetch_files_by_ftp { | ||
| 237 | |||
| 238 | # generate ftp request list | ||
| 239 | my @ftp_files; | ||
| 240 | if ( $ftp_file eq 'all' ) { | ||
| 241 | @ftp_files = qw( | ||
| 242 | refgene | ||
| 243 | known | ||
| 244 | ); | ||
| 245 | } | ||
| 246 | elsif ( $ftp_file =~ /,/ ) { | ||
| 247 | @ftp_files = split /,/, $ftp_file; | ||
| 248 | } | ||
| 249 | else { | ||
| 250 | push @ftp_files, $ftp_file; | ||
| 251 | } | ||
| 252 | |||
| 253 | # generate list of files | ||
| 254 | my @files; | ||
| 255 | foreach my $item (@ftp_files) { | ||
| 256 | if ( $item =~ m/refgene/i ) { | ||
| 257 | push @files, qw( | ||
| 258 | refGene.txt.gz | ||
| 259 | ); | ||
| 260 | } | ||
| 261 | elsif ( $item =~ m/known/i ) { | ||
| 262 | push @files, qw( | ||
| 263 | knownGene.txt.gz | ||
| 264 | kgXref.txt.gz | ||
| 265 | ); | ||
| 266 | } | ||
| 267 | else { | ||
| 268 | print " unknown ftp request '$item'! skipping\n"; | ||
| 269 | next; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | # this might seem convulated.... | ||
| 274 | # but we're putting all the file names in a single array | ||
| 275 | # instead of specific global variables | ||
| 276 | # to make retrieving through FTP a little easier | ||
| 277 | # plus, not all files may be available for each species, e.g. knownGene | ||
| 278 | # we also rename the files after downloading them | ||
| 279 | |||
| 280 | # we will sort out the list of downloaded files later and assign them | ||
| 281 | # to specific global filename variables | ||
| 282 | |||
| 283 | # add chromosome file if requested | ||
| 284 | if ($do_chromo) { | ||
| 285 | push @files, 'chromInfo.txt.gz'; | ||
| 286 | } | ||
| 287 | |||
| 288 | # set the path based on user provided database | ||
| 289 | my $path = 'goldenPath/' . $database . '/database/'; | ||
| 290 | |||
| 291 | # initiate connection | ||
| 292 | print " Connecting to $host....\n"; | ||
| 293 | my $ftp = Net::FTP->new($host) or die "Cannot connect!"; | ||
| 294 | $ftp->login or die "Cannot login! " . $ftp->message; | ||
| 295 | |||
| 296 | # prepare for download | ||
| 297 | $ftp->cwd($path) | ||
| 298 | or die "Cannot change working directory to '$path'! " . $ftp->message; | ||
| 299 | $ftp->binary; | ||
| 300 | |||
| 301 | # download requested files | ||
| 302 | my @fetched_files; | ||
| 303 | foreach my $file (@files) { | ||
| 304 | print " fetching $file....\n"; | ||
| 305 | |||
| 306 | # prepend the local file name with the database | ||
| 307 | my $new_file = $database . '_' . $file; | ||
| 308 | |||
| 309 | # fetch | ||
| 310 | if ( $ftp->get( $file, $new_file ) ) { | ||
| 311 | push @fetched_files, $new_file; | ||
| 312 | } | ||
| 313 | else { | ||
| 314 | my $message = $ftp->message; | ||
| 315 | if ( $message =~ /no such file/i ) { | ||
| 316 | print " file unavailable\n"; | ||
| 317 | } | ||
| 318 | else { | ||
| 319 | warn $message; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | } | ||
| 323 | $ftp->quit; | ||
| 324 | |||
| 325 | print " Finished\n"; | ||
| 326 | return @fetched_files; | ||
| 327 | } | ||
| 328 | |||
| 329 | sub open_output_gff { | ||
| 330 | |||
| 331 | # prepare output file name | ||
| 332 | my $file = shift; | ||
| 333 | my $outfile = $file; | ||
| 334 | $outfile =~ s/\.txt (?:\.gz)? $//xi; # remove the extension | ||
| 335 | $outfile .= $do_gtf ? '.gtf' : '.gff3'; | ||
| 336 | if ($gz) { | ||
| 337 | $outfile .= '.gz'; | ||
| 338 | } | ||
| 339 | |||
| 340 | # open file handle | ||
| 341 | my $fh = Bio::ToolBox->write_file( $outfile, $gz ) | ||
| 342 | or die " unable to open file '$outfile' for writing!\n"; | ||
| 343 | |||
| 344 | # print comments | ||
| 345 | $fh->printf( "##gff-version %s\n", $do_gtf ? '2.5' : '3' ); | ||
| 346 | $fh->print("##genome-build UCSC $database\n") if $database; | ||
| 347 | $fh->print("# UCSC table file $file\n"); | ||
| 348 | |||
| 349 | # finish | ||
| 350 | return ( $outfile, $fh ); | ||
| 351 | } | ||
| 352 | |||
| 353 | sub print_current_gene_list { | ||
| 354 | my ( $gff_fh, $ucsc ) = @_; | ||
| 355 | |||
| 356 | # we need to sort the genes in genomic order before writing the GFF | ||
| 357 | # ucsc files are not always in a nice genomic order | ||
| 358 | # we will first put the features in a hash based on coordinates | ||
| 359 | my $top_features = $ucsc->top_features; | ||
| 360 | printf " Sorting %s top features....\n", | ||
| 361 | format_with_commas( scalar( @{$top_features} ) ); | ||
| 362 | my %pos2seqf; | ||
| 363 | foreach my $gene ( @{$top_features} ) { | ||
| 364 | |||
| 365 | # make sure start positions are unique, just in case | ||
| 366 | # these modifications won't make it into seqfeature object | ||
| 367 | my $start = $gene->start; | ||
| 368 | my $chr = $gene->seq_id; | ||
| 369 | while ( exists $pos2seqf{$chr}{$start} ) { | ||
| 370 | $start++; | ||
| 371 | } | ||
| 372 | |||
| 373 | # store the seqfeature | ||
| 374 | $pos2seqf{$chr}{$start} = $gene; | ||
| 375 | } | ||
| 376 | |||
| 377 | # print in genomic order | ||
| 378 | # the gff_string method is undocumented in the POD, but is a | ||
| 379 | # valid method. Passing 1 should force a recursive action to | ||
| 380 | # print both parent and children. | ||
| 381 | print " Writing features to GFF....\n"; | ||
| 382 | foreach my $chr ( sane_chromo_sort( keys %pos2seqf ) ) { | ||
| 383 | foreach my $start ( sort { $a <=> $b } keys %{ $pos2seqf{$chr} } ) { | ||
| 384 | |||
| 385 | # print the seqfeature recursively | ||
| 386 | if ($do_gtf) { | ||
| 387 | $gff_fh->print( gtf_string( $pos2seqf{$chr}{$start} ) ); | ||
| 388 | } | ||
| 389 | else { | ||
| 390 | $gff_fh->print( $pos2seqf{$chr}{$start}->gff3_string(1) ); | ||
| 391 | $gff_fh->print("###\n"); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | sub print_chromosomes { | ||
| 398 | |||
| 399 | my $out_fh = shift; | ||
| 400 | |||
| 401 | # open the chromosome file | ||
| 402 | my $chromo_fh = Bio::ToolBox->read_file($chromof) | ||
| 403 | or die "unable to open specified chromosome file '$chromof'!\n"; | ||
| 404 | |||
| 405 | # convert the chromosomes into GFF features | ||
| 406 | my @chromosomes; | ||
| 407 | while ( my $line = $chromo_fh->getline ) { | ||
| 408 | next if ( $line =~ /^#/ ); | ||
| 409 | chomp $line; | ||
| 410 | my ( $chr, $end, $path ) = split /\t/, $line; | ||
| 411 | unless ( defined $chr and $end =~ m/^\d+$/ ) { | ||
| 412 | die " format of chromsome doesn't seem right! Are you sure?\n"; | ||
| 413 | } | ||
| 414 | push @chromosomes, [ $chr, $end ]; | ||
| 415 | } | ||
| 416 | $chromo_fh->close; | ||
| 417 | |||
| 418 | # print the chromosomes | ||
| 419 | # UCSC orders their chromosomes by chromosome length | ||
| 420 | # I would prefer to order by ID if possible | ||
| 421 | foreach my $chr ( sane_chromo_sort(@chromosomes) ) { | ||
| 422 | $out_fh->printf( "##sequence-region %s 1 %d\n", $chr->[0], $chr->[1] ); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | __END__ |