| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp 'croak'; | ||
| 6 | use GBrowse::ConfigData; | ||
| 7 | use DBI; | ||
| 8 | use Bio::DB::Fasta; | ||
| 9 | use LWP::Simple 'mirror','is_error','is_success'; | ||
| 10 | use File::Basename 'dirname'; | ||
| 11 | use Getopt::Long; | ||
| 12 | |||
| 13 | use constant HOST=>'genome-mysql.cse.ucsc.edu'; | ||
| 14 | use constant USER=>'genome'; | ||
| 15 | |||
| 16 | my $host = HOST; | ||
| 17 | my $dbh = DBI->connect( | ||
| 18 | "DBI:mysql::host=$host;mysql_use_result=1", | ||
| 19 | USER,'', | ||
| 20 | {PrintError=>0,RaiseError=>0} | ||
| 21 | ); | ||
| 22 | |||
| 23 | my $USAGE = <<END; | ||
| 24 | Usage: $0 [options] <UCSC genome build> [<Description>] | ||
| 25 | |||
| 26 | Example: $0 hg19 'Human genome (hg19)' | ||
| 27 | |||
| 28 | This creates a framework data source for one of the genomes known to | ||
| 29 | the UCSC Genome Browser. You can then modify the data source | ||
| 30 | configuration file, add your own data, and so forth. Provide the name | ||
| 31 | of a UCSC genome build and optionally a description to display in | ||
| 32 | GBrowse. | ||
| 33 | |||
| 34 | To get started, find the desired data source by going to | ||
| 35 | http://genome.ucsc.edu/cgi-bin/hgGateway and using the "clade" and | ||
| 36 | "genome" menus to navigate to the desired species and build | ||
| 37 | number. You will find the data source name in the blue box below the | ||
| 38 | navigation controls. Look for something like this: | ||
| 39 | |||
| 40 | D. melanogaster Genome Browser – dm3 assembly (sequences) | ||
| 41 | |||
| 42 | The data source name appears before the word "assembly", in this case | ||
| 43 | "dm3". | ||
| 44 | |||
| 45 | To get a list of all sources recognized by UCSC appears type: | ||
| 46 | |||
| 47 | $0 --list | ||
| 48 | |||
| 49 | Options: | ||
| 50 | --remove-chr Remove the 'chr' prefix from all chromosome names | ||
| 51 | --list List data sources | ||
| 52 | END | ||
| 53 | ; | ||
| 54 | |||
| 55 | my ($REMOVE_CHR,$LIST); | ||
| 56 | |||
| 57 | GetOptions( | ||
| 58 | 'remove-chr' => \$REMOVE_CHR, | ||
| 59 | 'list' => \$LIST, | ||
| 60 | ) or die $USAGE; | ||
| 61 | |||
| 62 | if ($LIST) { | ||
| 63 | print_sources($dbh); | ||
| 64 | exit -1; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $dsn = shift; | ||
| 68 | my $description = shift || "Imported $dsn genome from UCSC"; | ||
| 69 | |||
| 70 | unless ($dsn) | ||
| 71 | { | ||
| 72 | print STDERR "usage: $0 <UCSC data source name>\n"; | ||
| 73 | print STDERR "Run $0 --help for details.\n"; | ||
| 74 | print STDERR "Run $0 --list for list of data sources.\n"; | ||
| 75 | exit -1; | ||
| 76 | } else { | ||
| 77 | $dbh->do("use $dsn") or die "Could not access $dsn database. Run this script with --list to see valid database names.\n"; | ||
| 78 | } | ||
| 79 | |||
| 80 | my $conf_dir = GBrowse::ConfigData->config('conf'); | ||
| 81 | my $data_dir = GBrowse::ConfigData->config('databases'); | ||
| 82 | |||
| 83 | print STDERR "** During database creation, you may be asked for your password in order to set file permissions correctly.\n\n"; | ||
| 84 | |||
| 85 | my $dir = create_database_dir($data_dir,$dsn); | ||
| 86 | my $scaffolds = create_scaffold_db($dir,$dsn); | ||
| 87 | my $genes = create_gene_db($dir,$dsn); | ||
| 88 | create_conf_file($conf_dir,$dsn,$description,$scaffolds,$genes); | ||
| 89 | create_source($conf_dir,$dsn,$description); | ||
| 90 | |||
| 91 | print STDERR <<END; | ||
| 92 | |||
| 93 | ** These files have been created for you: | ||
| 94 | |||
| 95 | $scaffolds -- database of chromosome sizes and sequences | ||
| 96 | $genes -- database of genes | ||
| 97 | $conf_dir/${dsn}.conf -- track configuration file for these databases | ||
| 98 | $conf_dir/GBrowse.conf -- updated data source configuration file | ||
| 99 | END | ||
| 100 | ; | ||
| 101 | |||
| 102 | if (-x '/usr/sbin/service') { | ||
| 103 | print STDERR "\n ** Restarting apache. You may be asked for your password.\n"; | ||
| 104 | system "sudo service apache2 restart"; | ||
| 105 | } elsif (-x '/etc/init.d/apache2') { | ||
| 106 | print STDERR "\n ** Restarting apache. You may be asked for your password.\n"; | ||
| 107 | system "sudo /etc/init.d/apache2 restart"; | ||
| 108 | } else { | ||
| 109 | print STDERR "\n** Please restart apache or other web server.\n"; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | exit 0; | ||
| 115 | |||
| 116 | sub print_sources { | ||
| 117 | my $dbh = shift; | ||
| 118 | my $s = $dbh->selectcol_arrayref('show databases'); | ||
| 119 | print STDERR join("\n",grep {!/information_schema/} @$s),"\n"; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub create_database_dir { | ||
| 123 | my ($data_dir,$dsn) = @_; | ||
| 124 | my $uid = $<; | ||
| 125 | my ($gid) = $( =~ /^(\d+)/; | ||
| 126 | my $dir = "$data_dir/$dsn"; | ||
| 127 | unless (-d $dir) { | ||
| 128 | print STDERR "Creating database directory for $dsn. You may be prompted for your password.\n"; | ||
| 129 | system "sudo mkdir -p $dir"; | ||
| 130 | } | ||
| 131 | unless (-w $dir) { | ||
| 132 | system "sudo chown $uid $dir"; | ||
| 133 | system "sudo chgrp $gid $dir"; | ||
| 134 | } | ||
| 135 | return $dir; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub create_scaffold_db { | ||
| 139 | my ($dir,$dsn) = @_; | ||
| 140 | my $path = "$dir/chromosomes"; | ||
| 141 | mkdir $path unless -e $path; | ||
| 142 | open my $db,">$path/chrom_sizes.gff3" or die "$path: $!"; | ||
| 143 | print $db <<END; | ||
| 144 | ##gff-version 3 | ||
| 145 | |||
| 146 | END | ||
| 147 | |||
| 148 | print STDERR "Fetching chromosome sizes...\n"; | ||
| 149 | |||
| 150 | my $query = $dbh->prepare('select chrom,size from chromInfo order by size') | ||
| 151 | or die $dbh->errstr; | ||
| 152 | $query->execute; | ||
| 153 | my @chroms; | ||
| 154 | while (my($chrom,$size) = $query->fetchrow_array) { | ||
| 155 | $chrom =~ s/^chr// if $REMOVE_CHR; | ||
| 156 | print $db join("\t", | ||
| 157 | $chrom, | ||
| 158 | $dsn, | ||
| 159 | 'chromosome', | ||
| 160 | 1, | ||
| 161 | $size, | ||
| 162 | '.','.','.', | ||
| 163 | "ID=$chrom;Name=$chrom"),"\n"; | ||
| 164 | push @chroms,$chrom; | ||
| 165 | } | ||
| 166 | |||
| 167 | $query->finish; | ||
| 168 | close $db; | ||
| 169 | |||
| 170 | print STDERR "Fetching FASTA files..."; | ||
| 171 | $ENV{FTP_PASSIVE}=1 unless exists $ENV{FTP_PASSIVE}; | ||
| 172 | my $prefix = $REMOVE_CHR ? 'chr' : ''; | ||
| 173 | for my $chr (sort @chroms) { | ||
| 174 | my $url = "ftp://hgdownload.cse.ucsc.edu/goldenPath/$dsn/chromosomes/$prefix$chr.fa.gz"; | ||
| 175 | my $file = "$path/$prefix$chr.fa.gz"; | ||
| 176 | print STDERR "$chr..."; | ||
| 177 | my $code = mirror($url=>$file); | ||
| 178 | warn "Fetch of $url returned error code $code\n" | ||
| 179 | if is_error($code); | ||
| 180 | } | ||
| 181 | print STDERR "done\n"; | ||
| 182 | print STDERR "Unpacking FASTA files..."; | ||
| 183 | unlink "$path/chromosomes.fa"; | ||
| 184 | for my $chr (sort @chroms) { | ||
| 185 | my $command = $REMOVE_CHR ? "gunzip -c $path/$prefix$chr.fa.gz | perl -p -e 's/^>chr/>/' >> $path/chromosomes.fa" | ||
| 186 | : "gunzip -c $path/$prefix$chr.fa.gz >> $path/chromosomes.fa" | ||
| 187 | unless -e "$path/$chr.fa" && -M "$path/chromosomes.fa" > -M "$path/$prefix$chr.fa.gz"; | ||
| 188 | system $command; | ||
| 189 | } | ||
| 190 | print STDERR "done\n"; | ||
| 191 | |||
| 192 | print STDERR "Creating FASTA index..."; | ||
| 193 | my $index = Bio::DB::Fasta->new($path) or die "Couldn't create index"; | ||
| 194 | print "done\n"; | ||
| 195 | |||
| 196 | my $wwwuser = GBrowse::ConfigData->config('wwwuser'); | ||
| 197 | system "sudo chown -R $wwwuser $path"; | ||
| 198 | return $path; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub create_gene_db { | ||
| 202 | my ($dir,$dsn) = @_; | ||
| 203 | my $path = "$dir/refGenes"; | ||
| 204 | mkdir $path unless -e $path; | ||
| 205 | |||
| 206 | my $src_path = "$path/genes.gff3"; | ||
| 207 | my $db_path = "$path/genes.sqlite"; | ||
| 208 | |||
| 209 | open my $db,'>',$src_path or die "$path: $!"; | ||
| 210 | print $db <<END; | ||
| 211 | ##gff-version 3 | ||
| 212 | |||
| 213 | END | ||
| 214 | print STDERR "Fetching genes..."; | ||
| 215 | my $query; | ||
| 216 | eval { | ||
| 217 | $query = $dbh->prepare('select * from refFlat order by geneName') or die $dbh->errstr; | ||
| 218 | $query->execute or die $dbh->errorstr; | ||
| 219 | } || eval { | ||
| 220 | $query = $dbh->prepare('select name2,name,chrom,strand,txStart,txEnd,cdsStart,cdsEnd,exonCount,exonStarts,exonEnds from ensGene order by name2') or die $dbh->errstr; | ||
| 221 | $query->execute or die $dbh->errorstr; | ||
| 222 | }; | ||
| 223 | die $@ if $@; | ||
| 224 | my $writer = GFFWriter->new($db,$dsn); | ||
| 225 | while (my $row = $query->fetchrow_arrayref) { | ||
| 226 | $writer->write_transcript($row); | ||
| 227 | } | ||
| 228 | $query->finish; | ||
| 229 | $writer->finish; | ||
| 230 | print STDERR "done\n"; | ||
| 231 | |||
| 232 | close $db; | ||
| 233 | |||
| 234 | print STDERR "Indexing..."; | ||
| 235 | system "bp_seqfeature_load.pl -f -c -a DBI::SQLite -d $db_path $src_path"; | ||
| 236 | print STDERR "done\n"; | ||
| 237 | |||
| 238 | return $db_path; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub log10 { log(shift())/log(10)} | ||
| 242 | |||
| 243 | sub create_conf_file { | ||
| 244 | my ($conf_dir,$dsn,$description,$scaffolds,$genes) = @_; | ||
| 245 | my $conf_path = "$conf_dir/${dsn}.conf"; | ||
| 246 | create_writable_file($conf_path); | ||
| 247 | |||
| 248 | # figure size of chromosomes | ||
| 249 | open my $fh1,"$scaffolds/chrom_sizes.gff3" or die "$scaffolds/chrom_sizes.gff3: $!"; | ||
| 250 | my $max_chrom = 0; | ||
| 251 | my ($first_chrom,$first_size); | ||
| 252 | while (<$fh1>) { | ||
| 253 | my ($chr,undef,undef,undef,$size) = split /\s+/; | ||
| 254 | next unless $size; | ||
| 255 | $max_chrom = $size if $max_chrom < $size; | ||
| 256 | $first_chrom ||= $chr; | ||
| 257 | $first_size ||= $size; | ||
| 258 | } | ||
| 259 | close $fh1; | ||
| 260 | |||
| 261 | # from the max chromosome, figure out reasonable default sizes and zoom levels | ||
| 262 | my $log = int log10($max_chrom); | ||
| 263 | my $max_segment = $max_chrom; | ||
| 264 | my $default_segment = 10**($log-3); | ||
| 265 | my $region_segment = $default_segment*10; | ||
| 266 | my @zoom_levels = qw(100 200 1000 2000 5000 10000 20000 50000 100000 200000 500000); | ||
| 267 | for (my $i=6; $i<=$log; $i++) { | ||
| 268 | push @zoom_levels,10**$i; | ||
| 269 | } | ||
| 270 | my @region_sizes = @zoom_levels; | ||
| 271 | my $default_region = $default_segment * 10; | ||
| 272 | my $summary_boundary= $default_segment * 100; | ||
| 273 | |||
| 274 | my $start = int(rand($first_size)); | ||
| 275 | my $end = $start + $default_segment -1; | ||
| 276 | my $initial_landmark = "$first_chrom:$start..$end"; | ||
| 277 | |||
| 278 | # pick some random examples from the gff3 file | ||
| 279 | my @names; | ||
| 280 | my $dir = dirname($genes); | ||
| 281 | open my $fh2,"$dir/genes.gff3" or die "$dir/genes.gff3: $!"; | ||
| 282 | while (<$fh2>) { | ||
| 283 | my ($name) = /Name=([^;]+)/ or next; | ||
| 284 | for (0..3) { | ||
| 285 | $names[$_] = $name if rand($.) < 4; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | close $fh2; | ||
| 289 | my $examples = "@names"; | ||
| 290 | |||
| 291 | open my $fh3,'>',$conf_path or die "$conf_path: $!"; | ||
| 292 | print $fh3 <<END; | ||
| 293 | [GENERAL] | ||
| 294 | description = $description | ||
| 295 | database = scaffolds | ||
| 296 | |||
| 297 | initial landmark = $initial_landmark | ||
| 298 | plugins = FilterTest RestrictionAnnotator TrackDumper FastaDumper | ||
| 299 | autocomplete = 1 | ||
| 300 | |||
| 301 | default tracks = Genes ncRNA | ||
| 302 | |||
| 303 | # examples to show in the introduction | ||
| 304 | examples = $examples | ||
| 305 | |||
| 306 | # "automatic" classes to try when an unqualified identifier is given | ||
| 307 | automatic classes = Gene | ||
| 308 | |||
| 309 | # Limits on genomic regions (can be overridden in datasource config files) | ||
| 310 | region segment = $region_segment | ||
| 311 | max segment = $max_segment | ||
| 312 | default segment = $default_segment | ||
| 313 | default region = $default_region | ||
| 314 | zoom levels = @zoom_levels | ||
| 315 | region sizes = @region_sizes | ||
| 316 | |||
| 317 | ################################# | ||
| 318 | # database definitions | ||
| 319 | ################################# | ||
| 320 | |||
| 321 | [scaffolds:database] | ||
| 322 | db_adaptor = Bio::DB::SeqFeature::Store | ||
| 323 | db_args = -adaptor memory | ||
| 324 | -dir $scaffolds | ||
| 325 | search options = default +autocomplete | ||
| 326 | |||
| 327 | [genes:database] | ||
| 328 | db_adaptor = Bio::DB::SeqFeature::Store | ||
| 329 | db_args = -adaptor DBI::SQLite | ||
| 330 | -dsn $genes | ||
| 331 | search options = default +autocomplete | ||
| 332 | |||
| 333 | # Default glyph settings | ||
| 334 | [TRACK DEFAULTS] | ||
| 335 | glyph = generic | ||
| 336 | database = scaffolds | ||
| 337 | height = 8 | ||
| 338 | bgcolor = green | ||
| 339 | fgcolor = black | ||
| 340 | label density = 25 | ||
| 341 | bump density = 100 | ||
| 342 | show summary = $summary_boundary | ||
| 343 | |||
| 344 | ### TRACK CONFIGURATION #### | ||
| 345 | # the remainder of the sections configure individual tracks | ||
| 346 | |||
| 347 | [Genes] | ||
| 348 | database = genes | ||
| 349 | feature = gene | ||
| 350 | glyph = gene | ||
| 351 | bgcolor = sub {shift->strand > 0 ? 'red' : 'blue'} | ||
| 352 | label_transcripts = 1 | ||
| 353 | label = sub { my \$f = shift; | ||
| 354 | my \$name = \$f->display_name; | ||
| 355 | my \@aliases = sort \$f->attributes('Alias'); | ||
| 356 | \$name .= " (\@aliases)" if \@aliases; | ||
| 357 | \$name; | ||
| 358 | } | ||
| 359 | height = 10 | ||
| 360 | description = 0 | ||
| 361 | key = Known Genes | ||
| 362 | |||
| 363 | [Genes:200000] | ||
| 364 | glyph = box | ||
| 365 | stranded = 1 | ||
| 366 | |||
| 367 | [Genes:500000] | ||
| 368 | glyph = box | ||
| 369 | bump = 0 | ||
| 370 | |||
| 371 | [ncRNA] | ||
| 372 | database = genes | ||
| 373 | feature = noncoding_transcript | ||
| 374 | fgcolor = orange | ||
| 375 | glyph = generic | ||
| 376 | description = 1 | ||
| 377 | key = Noncoding RNAs | ||
| 378 | |||
| 379 | [CDS] | ||
| 380 | database = genes | ||
| 381 | feature = mRNA | ||
| 382 | glyph = cds | ||
| 383 | description = 0 | ||
| 384 | height = 26 | ||
| 385 | sixframe = 1 | ||
| 386 | label = sub {shift->name . " reading frame"} | ||
| 387 | key = CDS | ||
| 388 | citation = This track shows CDS reading frames. | ||
| 389 | |||
| 390 | [CDS:200000] | ||
| 391 | glyph = box | ||
| 392 | stranded = 1 | ||
| 393 | |||
| 394 | [Translation] | ||
| 395 | glyph = translation | ||
| 396 | global feature = 1 | ||
| 397 | database = scaffolds | ||
| 398 | height = 40 | ||
| 399 | fgcolor = purple | ||
| 400 | strand = +1 | ||
| 401 | translation = 6frame | ||
| 402 | key = 6-frame translation | ||
| 403 | |||
| 404 | [Translation:1000000] | ||
| 405 | hide = 1 | ||
| 406 | |||
| 407 | [TranslationF] | ||
| 408 | glyph = translation | ||
| 409 | global feature = 1 | ||
| 410 | database = scaffolds | ||
| 411 | height = 20 | ||
| 412 | fgcolor = purple | ||
| 413 | strand = +1 | ||
| 414 | translation = 3frame | ||
| 415 | key = 3-frame translation (forward) | ||
| 416 | |||
| 417 | [TranslationF:1000000] | ||
| 418 | hide = 1 | ||
| 419 | |||
| 420 | [DNA/GC Content] | ||
| 421 | glyph = dna | ||
| 422 | global feature = 1 | ||
| 423 | database = scaffolds | ||
| 424 | height = 40 | ||
| 425 | do_gc = 1 | ||
| 426 | gc_window = auto | ||
| 427 | strand = both | ||
| 428 | fgcolor = red | ||
| 429 | axis_color = blue | ||
| 430 | |||
| 431 | [DNA/GC Content:1000000] | ||
| 432 | hide = 1 | ||
| 433 | |||
| 434 | [TranslationR] | ||
| 435 | glyph = translation | ||
| 436 | global feature = 1 | ||
| 437 | database = scaffolds | ||
| 438 | height = 20 | ||
| 439 | fgcolor = blue | ||
| 440 | strand = -1 | ||
| 441 | translation = 3frame | ||
| 442 | key = 3-frame translation (reverse) | ||
| 443 | |||
| 444 | [TranslationR:1000000] | ||
| 445 | hide = 1 | ||
| 446 | |||
| 447 | END | ||
| 448 | close $fh3; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub create_source { | ||
| 452 | my ($conf_dir,$dsn,$description) = @_; | ||
| 453 | my $path = "$conf_dir/GBrowse.conf"; | ||
| 454 | open my $fh,$path or die "$path: $!"; | ||
| 455 | my $foundit; | ||
| 456 | while (<$fh>) { | ||
| 457 | $foundit++ if /\[$dsn\]/; | ||
| 458 | } | ||
| 459 | close $fh; | ||
| 460 | return if $foundit; | ||
| 461 | |||
| 462 | create_writable_file($path); | ||
| 463 | open my $fh2,'>>',$path or die "$path: $!"; | ||
| 464 | print $fh2 "\n"; | ||
| 465 | print $fh2 <<END; | ||
| 466 | [$dsn] | ||
| 467 | description = $description | ||
| 468 | path = $conf_dir/${dsn}.conf | ||
| 469 | END | ||
| 470 | ; | ||
| 471 | close $fh2; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub create_writable_file { | ||
| 475 | my $file = shift; | ||
| 476 | return if -e $file && -w $file; | ||
| 477 | |||
| 478 | my $uid = $<; | ||
| 479 | my ($gid) = $( =~ /^(\d+)/; | ||
| 480 | system "sudo touch $file"; | ||
| 481 | system "sudo chown $uid $file"; | ||
| 482 | system "sudo chgrp $gid $file"; | ||
| 483 | system "chmod +w $file"; | ||
| 484 | } | ||
| 485 | |||
| 486 | package GFFWriter; | ||
| 487 | |||
| 488 | sub new { | ||
| 489 | my $class = shift; | ||
| 490 | my ($fh,$dsn) = @_; | ||
| 491 | return bless { | ||
| 492 | fh => $fh, | ||
| 493 | dsn => $dsn, | ||
| 494 | gene_id => 'g000000', | ||
| 495 | transcript_id => 't00000', | ||
| 496 | last_gene_name => '', | ||
| 497 | last_gene_id => '', | ||
| 498 | last_gene => {}, | ||
| 499 | },ref $class || $class; | ||
| 500 | } | ||
| 501 | |||
| 502 | # This subroutine is amazingly long and complicated looking, but probably correct | ||
| 503 | sub write_transcript { | ||
| 504 | my $self = shift; | ||
| 505 | my $fields = shift; | ||
| 506 | |||
| 507 | my ($gene_name,$accession,$chrom,$strand,$txStart,$txEnd,$cdsStart,$cdsEnd,$exons,$exonStarts,$exonEnds) = @$fields; | ||
| 508 | my ($utr5_start,$utr5_end,$utr3_start,$utr3_end,$gid,$tid); | ||
| 509 | $gene_name ||= $accession; | ||
| 510 | $chrom =~ s/^chr// if $REMOVE_CHR; | ||
| 511 | |||
| 512 | if ($self->{last_gene_name} ne $gene_name | ||
| 513 | || | ||
| 514 | $self->{last_gene}{chr} ne $chrom # avoid some gene name collisions | ||
| 515 | || | ||
| 516 | abs($self->{last_gene}{start} - $txStart) > 6_000_000 # avoid some gene name collisions | ||
| 517 | || | ||
| 518 | $self->{last_gene}{strand} ne $strand) | ||
| 519 | { | ||
| 520 | $self->write_last_gene; | ||
| 521 | $self->{last_gene} = {}; | ||
| 522 | $self->{last_gene_name} = ''; | ||
| 523 | $gid = $self->{last_gene_id} = $self->{gene_id}++; | ||
| 524 | } elsif ($self->{last_gene_id}) { | ||
| 525 | $gid = $self->{last_gene_id}; | ||
| 526 | } else { | ||
| 527 | $gid = $self->{last_gene_id} = $self->{gene_id}++; | ||
| 528 | } | ||
| 529 | |||
| 530 | $tid = $self->{transcript_id}++; | ||
| 531 | |||
| 532 | my $ORIGIN = 1; | ||
| 533 | my $SRC = $self->{dsn}; | ||
| 534 | my $fh = $self->{fh}; | ||
| 535 | |||
| 536 | # adjust for Jim's 0-based coordinates | ||
| 537 | $txStart++; | ||
| 538 | $cdsStart++; | ||
| 539 | |||
| 540 | $txStart -= $ORIGIN; | ||
| 541 | $txEnd -= $ORIGIN; | ||
| 542 | $cdsStart -= $ORIGIN; | ||
| 543 | $cdsEnd -= $ORIGIN; | ||
| 544 | |||
| 545 | # this is how noncoding genes are expressed (?!!!) | ||
| 546 | my $is_noncoding = $cdsStart >= $cdsEnd; | ||
| 547 | |||
| 548 | unless ($is_noncoding) { | ||
| 549 | $self->{last_gene_name} ||= $gene_name; | ||
| 550 | $self->{last_gene}{chr} ||= $chrom; | ||
| 551 | $self->{last_gene}{strand} ||= $strand; | ||
| 552 | $self->{last_gene}{start} = $txStart if !$self->{last_gene}{start} || $self->{last_gene}{start} > $txStart; | ||
| 553 | $self->{last_gene}{end} = $txEnd if !$self->{last_gene}{end} || $self->{last_gene}{end} < $txEnd; | ||
| 554 | } | ||
| 555 | |||
| 556 | # print the transcript | ||
| 557 | my $id = $is_noncoding ? "ID=$tid;Name=$gene_name;Alias=$accession" : "ID=$tid;Name=$accession;Parent=$gid"; | ||
| 558 | print $fh join | ||
| 559 | ("\t",$chrom,$SRC,($is_noncoding ? 'noncoding_transcript' : 'mRNA'),$txStart,$txEnd,'.',$strand,'.',$id),"\n"; | ||
| 560 | |||
| 561 | # now handle the CDS entries -- the tricky part is the need to keep | ||
| 562 | # track of phase | ||
| 563 | my $phase = 0; | ||
| 564 | my @exon_starts = map {$_-$ORIGIN} split ',',$exonStarts; | ||
| 565 | my @exon_ends = map {$_-$ORIGIN} split ',',$exonEnds; | ||
| 566 | |||
| 567 | if ($is_noncoding) { | ||
| 568 | for (my $i=0;$i<@exon_starts;$i++) { # for each exon start | ||
| 569 | print $fh join("\t", | ||
| 570 | $chrom,$SRC,'exon',$exon_starts[$i],$exon_ends[$i],'.',$strand,'.',"Parent=$tid" | ||
| 571 | ),"\n"; | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | elsif ($strand eq '+') { | ||
| 576 | for (my $i=0;$i<@exon_starts;$i++) { # for each exon start | ||
| 577 | my $exon_start = $exon_starts[$i] + 1; | ||
| 578 | my $exon_end = $exon_ends[$i]; | ||
| 579 | my (@utr_start,@utr_end,$cds_start,$cds_end); | ||
| 580 | |||
| 581 | if ($exon_start < $cdsStart) { # in a 5' UTR | ||
| 582 | push (@utr_start, $exon_start); | ||
| 583 | } elsif ($exon_start > $cdsEnd) { | ||
| 584 | push (@utr_start, $exon_start); | ||
| 585 | } else { | ||
| 586 | $cds_start = $exon_start; | ||
| 587 | } | ||
| 588 | |||
| 589 | if ($exon_end < $cdsStart) { | ||
| 590 | push (@utr_end, $exon_end); | ||
| 591 | } elsif ($exon_end > $cdsEnd) { | ||
| 592 | push (@utr_end, $exon_end); | ||
| 593 | } else { | ||
| 594 | $cds_end = $exon_end; | ||
| 595 | } | ||
| 596 | |||
| 597 | if ($utr_start[0] && !$utr_end[0]) { # half in half out on 5' end | ||
| 598 | $utr_end[0]= $cdsStart - 1; | ||
| 599 | $cds_start = $cdsStart; | ||
| 600 | $cds_end = $exon_end; | ||
| 601 | } | ||
| 602 | |||
| 603 | if ($utr_end[0] && !$utr_start[0]) { # half in half out on 3' end | ||
| 604 | $utr_start[0]= $cdsEnd + 1; | ||
| 605 | $cds_end = $cdsEnd; | ||
| 606 | $cds_start = $exon_start; | ||
| 607 | } | ||
| 608 | |||
| 609 | # If the CDS is within the exon | ||
| 610 | if ($utr_start[0] && $utr_end[0] && | ||
| 611 | $utr_start[0] < $cdsStart && | ||
| 612 | $utr_end[0] > $cdsEnd) | ||
| 613 | { | ||
| 614 | $utr_end[0]= $cdsStart - 1; | ||
| 615 | $cds_start = $cdsStart; | ||
| 616 | $cds_end = $cdsEnd; | ||
| 617 | |||
| 618 | push (@utr_start, $cdsEnd + 1); | ||
| 619 | push (@utr_end, $exon_end); | ||
| 620 | } | ||
| 621 | |||
| 622 | die "programmer error, not an even number of utr_starts and utr_ends" | ||
| 623 | unless $#utr_start == $#utr_end; | ||
| 624 | die "programmer error, cds_start and no cds_end" | ||
| 625 | unless defined $cds_start == defined $cds_end; | ||
| 626 | |||
| 627 | for (my $i=0;$i<@utr_start;$i++) { # for each utr start | ||
| 628 | if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] && | ||
| 629 | $utr_start[$i] < $cdsStart) { | ||
| 630 | print $fh join | ||
| 631 | ("\t",$chrom,$SRC,"five_prime_UTR",$utr_start[$i],$utr_end[$i],'.',$strand,'.',"Parent=$tid"),"\n" | ||
| 632 | } # end of if | ||
| 633 | } # end of foreach | ||
| 634 | |||
| 635 | if (defined $cds_start && $cds_start <= $cds_end) { | ||
| 636 | print $fh join | ||
| 637 | ("\t",$chrom,$SRC,'CDS',$cds_start,$cds_end,'.',$strand,$phase,"Parent=$tid"),"\n"; | ||
| 638 | $phase = (($cds_end-$cds_start+1-$phase)) % 3; | ||
| 639 | } | ||
| 640 | |||
| 641 | for (my $i=0;$i<@utr_start;$i++) { # for each utr start | ||
| 642 | if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] && | ||
| 643 | $utr_start[$i] > $cdsEnd) { | ||
| 644 | print $fh join ("\t",$chrom,$SRC,"three_prime_UTR",,$utr_start[$i], | ||
| 645 | $utr_end[$i],'.',$strand,'.',"Parent=$tid"),"\n" | ||
| 646 | } | ||
| 647 | } | ||
| 648 | } # end of for each exon | ||
| 649 | } # matches if strand = + | ||
| 650 | |||
| 651 | elsif ($strand eq '-') { | ||
| 652 | my @lines; | ||
| 653 | for (my $i=@exon_starts-1; $i>=0; $i--) { # count backwards | ||
| 654 | my $exon_start = $exon_starts[$i] + 1; | ||
| 655 | my $exon_end = $exon_ends[$i]; | ||
| 656 | my (@utr_start,@utr_end,$cds_start,$cds_end); | ||
| 657 | |||
| 658 | if ($exon_end > $cdsEnd) { # in a 5' UTR | ||
| 659 | push (@utr_end, $exon_end); | ||
| 660 | } elsif ($exon_end < $cdsStart) { | ||
| 661 | push (@utr_end, $exon_end); | ||
| 662 | } else { | ||
| 663 | $cds_end = $exon_end; | ||
| 664 | } | ||
| 665 | |||
| 666 | if ($exon_start > $cdsEnd) { | ||
| 667 | push (@utr_start, $exon_start); | ||
| 668 | } elsif ($exon_start < $cdsStart) { | ||
| 669 | push (@utr_start, $exon_start); | ||
| 670 | } else { | ||
| 671 | $cds_start = $exon_start; | ||
| 672 | } | ||
| 673 | |||
| 674 | if ($utr_start[0] && !$utr_end[0]) { # half in half out on 3' end | ||
| 675 | $utr_end[0] = $cdsStart - 1; | ||
| 676 | $cds_start = $cdsStart; | ||
| 677 | $cds_end = $exon_end; | ||
| 678 | } | ||
| 679 | |||
| 680 | if ($utr_end[0] && !$utr_start[0]) { # half in half out on 5' end | ||
| 681 | $utr_start[0] = $cdsEnd + 1; | ||
| 682 | $cds_end = $cdsEnd; | ||
| 683 | $cds_start = $exon_start; | ||
| 684 | } | ||
| 685 | |||
| 686 | # If the CDS is within the exon | ||
| 687 | if ($utr_start[0] && | ||
| 688 | $utr_end[0] && | ||
| 689 | $utr_start[0] < $cdsStart && | ||
| 690 | $utr_end[0] > $cdsEnd) | ||
| 691 | { | ||
| 692 | $utr_end[0]= $cdsStart - 1; | ||
| 693 | $cds_start = $cdsStart; | ||
| 694 | $cds_end = $cdsEnd; | ||
| 695 | |||
| 696 | push (@utr_start, $cdsEnd + 1); | ||
| 697 | push (@utr_end, $exon_end); | ||
| 698 | } | ||
| 699 | |||
| 700 | die "programmer error, not an even number of utr_starts and utr_ends" | ||
| 701 | unless $#utr_start == $#utr_end; | ||
| 702 | |||
| 703 | die "programmer error, cds_start and no cds_end" unless defined | ||
| 704 | $cds_start == defined $cds_end; | ||
| 705 | |||
| 706 | for (my $i=0;$i<@utr_start;$i++) { # for each utr start | ||
| 707 | if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] && | ||
| 708 | $utr_start[$i] > $cdsEnd) { | ||
| 709 | unshift @lines,join | ||
| 710 | ("\t",$chrom,$SRC,"five_prime_UTR",,$utr_start[$i],$utr_end[$i],'.',$strand,'.',"Parent=$tid"),"\n" | ||
| 711 | } | ||
| 712 | } # end of for | ||
| 713 | |||
| 714 | if (defined $cds_start && $cds_start <= $cds_end) { | ||
| 715 | unshift @lines,join | ||
| 716 | ("\t",$chrom,$SRC,'CDS',$cds_start,$cds_end,'.',$strand,$phase,"Parent=$tid"),"\n"; | ||
| 717 | $phase = (($cds_end-$cds_start+1-$phase)) % 3; | ||
| 718 | } | ||
| 719 | |||
| 720 | for (my $i=0;$i<@utr_start;$i++) { # for each utr start | ||
| 721 | if (defined $utr_start[$i] && $utr_start[$i] <= $utr_end[$i] && | ||
| 722 | $utr_end[$i] < $cdsStart) { | ||
| 723 | unshift @lines,join | ||
| 724 | ("\t",$chrom,$SRC,"three_prime_UTR",$utr_start[$i],$utr_end[$i],'.',$strand,'.',"Parent=$tid"),"\n" | ||
| 725 | } | ||
| 726 | } # end for | ||
| 727 | } | ||
| 728 | print $fh @lines; | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | |||
| 733 | sub write_last_gene { | ||
| 734 | my $self = shift; | ||
| 735 | my $name = $self->{last_gene_name} or return; | ||
| 736 | my $chr = $self->{last_gene}{chr}; | ||
| 737 | my $start = $self->{last_gene}{start}; | ||
| 738 | my $end = $self->{last_gene}{end}; | ||
| 739 | my $strand = $self->{last_gene}{strand}; | ||
| 740 | my $id = $self->{last_gene_id}; | ||
| 741 | my $fh = $self->{fh}; | ||
| 742 | print $fh join("\t", | ||
| 743 | $chr, | ||
| 744 | $self->{dsn}, | ||
| 745 | 'gene', | ||
| 746 | $start, | ||
| 747 | $end, | ||
| 748 | '.', | ||
| 749 | $strand, | ||
| 750 | '.', | ||
| 751 | "ID=$id;Name=$name"),"\n"; | ||
| 752 | $self->{last_gene_name} = ''; | ||
| 753 | } | ||
| 754 | |||
| 755 | sub finish { | ||
| 756 | my $self = shift; | ||
| 757 | $self->write_last_gene if defined fileno($self->{fh}); | ||
| 758 | } | ||
| 759 | sub DESTROY { shift->finish } |