« blib/conf/plugins/SequenceDumper.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/UserTracks/Database.pm »
| 1 | package Bio::Graphics::Browser2::DataLoader; | ||
| 2 | # $Id$ | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use IO::File; | ||
| 6 | use File::Basename 'basename','dirname'; | ||
| 7 | use Fcntl ':flock'; | ||
| 8 | use Carp 'croak', 'cluck'; | ||
| 9 | use Digest::MD5 'md5_hex'; | ||
| 10 | |||
| 11 | # for mysql to work, you must do something like this: | ||
| 12 | # grant create on `userdata\_%`.* to 'www-data'@localhost | ||
| 13 | # NOTICE the backticks around `userdata\_%` !! | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my ($track_name, $data_path, $conf_path, $settings, $userid) = @_; | ||
| 18 | my $loadid = substr($userid,0,6).'_'.$track_name; | ||
| 19 | my $self = bless | ||
| 20 | { name => $track_name, | ||
| 21 | data => $data_path, | ||
| 22 | conf => $conf_path, | ||
| 23 | settings => $settings, | ||
| 24 | loadid => $loadid, | ||
| 25 | }, ref $class || $class; | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub track_name { shift->{name} } | ||
| 30 | sub data_path { shift->{data} } | ||
| 31 | sub conf_path { shift->{conf} } | ||
| 32 | sub conf_fh { shift->{conf_fh} } | ||
| 33 | sub settings { shift->{settings} } | ||
| 34 | sub loadid { shift->{loadid} } | ||
| 35 | sub force_category { | ||
| 36 | my $self = shift; | ||
| 37 | my $d = $self->{category}; | ||
| 38 | $self->{category} = shift if @_; | ||
| 39 | return $d; | ||
| 40 | } | ||
| 41 | sub eol_char { | ||
| 42 | my $self = shift; | ||
| 43 | my $d = $self->{eol_char}; | ||
| 44 | $self->{eol_char} = shift if @_; | ||
| 45 | return $d; | ||
| 46 | } | ||
| 47 | sub setting { | ||
| 48 | my $self = shift; | ||
| 49 | my $option = shift; | ||
| 50 | $self->settings->global_setting($option); | ||
| 51 | } | ||
| 52 | sub globals { shift->settings->globals } | ||
| 53 | sub busy_path { | ||
| 54 | my $self = shift; | ||
| 55 | return File::Spec->catfile($self->data_path, | ||
| 56 | Bio::Graphics::Browser2::UserTracks->busy_file_name); | ||
| 57 | } | ||
| 58 | sub status_path { | ||
| 59 | my $self = shift; | ||
| 60 | return File::Spec->catfile($self->data_path, | ||
| 61 | Bio::Graphics::Browser2::UserTracks->status_file_name); | ||
| 62 | } | ||
| 63 | sub sources_path { | ||
| 64 | my $self = shift; | ||
| 65 | return File::Spec->catfile($self->data_path, | ||
| 66 | Bio::Graphics::Browser2::UserTracks->sources_dir_name); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub strip_prefix { | ||
| 70 | my $self = shift; | ||
| 71 | my $d = $self->{strip_prefix}; | ||
| 72 | $self->{strip_prefix} = shift if @_; | ||
| 73 | return $d; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub set_status { | ||
| 77 | my $self = shift; | ||
| 78 | my $msg = shift; | ||
| 79 | my $status = $self->status_path; | ||
| 80 | open my $fh,">",$status; | ||
| 81 | flock($fh,LOCK_EX); | ||
| 82 | seek($fh,0,0); | ||
| 83 | print $fh $msg,"\n"; | ||
| 84 | close $fh; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub get_status { | ||
| 88 | my $self = shift; | ||
| 89 | undef $!; | ||
| 90 | my $status = $self->status_path; | ||
| 91 | open my $fh,"<",$status or return; | ||
| 92 | flock($fh,LOCK_SH); | ||
| 93 | seek($fh,0,0); | ||
| 94 | my $msg = <$fh>; | ||
| 95 | close $fh; | ||
| 96 | chomp($msg); | ||
| 97 | return $msg; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub get_fasta_files { | ||
| 101 | my $self = shift; | ||
| 102 | my $source = $self->settings; | ||
| 103 | my (@fastai,@fasta); | ||
| 104 | |||
| 105 | my @dbs = $source->databases; | ||
| 106 | my %seenit; | ||
| 107 | |||
| 108 | for my $db (@dbs) { | ||
| 109 | my ($dbid,$adaptor,%args) = $source->db2args($db); | ||
| 110 | my $fasta = $args{-fasta} || $args{-dsn} || $args{-dir}; | ||
| 111 | next if $seenit{$fasta}++; | ||
| 112 | next unless -e $fasta; | ||
| 113 | if (-d _) { | ||
| 114 | push @fastai, glob("$fasta/*.fai"); | ||
| 115 | push @fasta, glob("$fasta/*.{fa,FA,fasta,FASTA}"); | ||
| 116 | } else { | ||
| 117 | push @fastai,$fasta if -e "$fasta.fai"; # points at an indexed fasta file | ||
| 118 | push @fasta, $fasta if $fasta =~ /\.(fa|fasta)$/i; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return (@fastai,@fasta); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub get_fasta_file { | ||
| 125 | my @fa = shift->get_fasta_files or return; | ||
| 126 | return $fa[0]; | ||
| 127 | } | ||
| 128 | |||
| 129 | # try to generate a chrom sizes file | ||
| 130 | sub chrom_sizes { | ||
| 131 | my $self = shift; | ||
| 132 | my $source = $self->settings; | ||
| 133 | my $globals = $source->globals; | ||
| 134 | |||
| 135 | my $mtime = $source->mtime; | ||
| 136 | my $name = $source->name; | ||
| 137 | |||
| 138 | my $sizes = File::Spec->catfile($globals->tmpdir('chrom_sizes'),"$name.sizes"); | ||
| 139 | if (-e $sizes && (stat(_))[9] >= $mtime) { | ||
| 140 | return $sizes; | ||
| 141 | } | ||
| 142 | $self->generate_chrom_sizes($sizes) or return; | ||
| 143 | return $sizes; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub chromosome_list { | ||
| 147 | my $self = shift; | ||
| 148 | my $sizes = $self->chrom_sizes or return; | ||
| 149 | open my $fh,$sizes or return; | ||
| 150 | my %result; | ||
| 151 | while (<$fh>) { | ||
| 152 | chomp; | ||
| 153 | my ($c,$s) = split /\s+/; | ||
| 154 | $result{$c} = $s; | ||
| 155 | } | ||
| 156 | close $fh; | ||
| 157 | return \%result; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub generate_chrom_sizes { | ||
| 161 | my $self = shift; | ||
| 162 | my $sizes = shift; | ||
| 163 | |||
| 164 | my $source = $self->settings; | ||
| 165 | my $build = $source->build_id; | ||
| 166 | my $species = $source->taxon_id; | ||
| 167 | |||
| 168 | open my $s,'>',$sizes or die "Can't open $sizes for writing: $!"; | ||
| 169 | |||
| 170 | print $s "##species http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=$species\n" | ||
| 171 | if $species; | ||
| 172 | print $s "##genome-build $build\n" if $build; | ||
| 173 | |||
| 174 | my $db = $source->open_database; | ||
| 175 | |||
| 176 | # Bio::DB::SeqFeature has a seq_ids method. | ||
| 177 | # First, try to query the default database for the | ||
| 178 | # seq_ids it knows about. | ||
| 179 | |||
| 180 | TRY: { | ||
| 181 | my @seqids = eval {$db->seq_ids} or last TRY; | ||
| 182 | my $result = eval { | ||
| 183 | for (@seqids) { | ||
| 184 | my ($segment) = $db->segment($_) or do { | ||
| 185 | warn "Can't find chromosome $_ in default database"; next}; | ||
| 186 | |||
| 187 | print $s "$_\t",$segment->length,"\n"; | ||
| 188 | } | ||
| 189 | close $s; | ||
| 190 | return 1; | ||
| 191 | }; | ||
| 192 | return 1 if $result; | ||
| 193 | warn $@; | ||
| 194 | unlink $sizes; | ||
| 195 | last TRY; | ||
| 196 | } | ||
| 197 | |||
| 198 | # Bio::DasI objects have an entry_points method | ||
| 199 | if (my @segs = eval {$db->entry_points}) { | ||
| 200 | for (@segs) { | ||
| 201 | print $s $_,"\t",$_->length,"\n"; | ||
| 202 | } | ||
| 203 | close $s; | ||
| 204 | return 1; | ||
| 205 | } | ||
| 206 | |||
| 207 | # Otherwise we search for databases with associated fasta files | ||
| 208 | # or indexed fasta (fai) files. | ||
| 209 | my $fasta = $self->get_fasta_file or return; | ||
| 210 | my $fai = "$fasta.fai"; | ||
| 211 | |||
| 212 | if (!-e $fai && -f $fasta && eval "require Bio::DB::Sam;1") { | ||
| 213 | Bio::DB::Sam::Fai->load($fasta) or return; | ||
| 214 | } | ||
| 215 | |||
| 216 | if (-e $fai) { # fai file -- copy to sizes | ||
| 217 | open my $f,$fai or die "Can't open $fasta: $!"; | ||
| 218 | while (<$f>) { | ||
| 219 | my ($seqid,$length) = split /\s+/; | ||
| 220 | print $s "$seqid\t$length\n"; | ||
| 221 | } | ||
| 222 | close $f; | ||
| 223 | return 1; | ||
| 224 | } elsif (eval "require Bio::DB::Fasta; 1") { | ||
| 225 | my $fa = Bio::DB::Fasta->new($fasta); | ||
| 226 | my @ids = $fa->ids; | ||
| 227 | open my $s,'>',$sizes or die "Can't open $sizes for writing: $!"; | ||
| 228 | for my $i (@ids) { | ||
| 229 | print $s $i,"\t",$fa->length($i),"\n"; | ||
| 230 | } | ||
| 231 | undef $fa; | ||
| 232 | return 1; | ||
| 233 | } | ||
| 234 | |||
| 235 | unlink $sizes; | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | # the client depends on this status literally | ||
| 242 | # BUG: this will interfere with i18n | ||
| 243 | sub set_processing_complete { | ||
| 244 | shift->set_status('processing complete'); | ||
| 245 | } | ||
| 246 | |||
| 247 | sub open_conf { | ||
| 248 | my $self = shift; | ||
| 249 | $self->{conf_fh} ||= IO::File->new($self->conf_path,">"); | ||
| 250 | $self->{conf_fh} or die $self->conf_path,": $!"; | ||
| 251 | $self->{conf_fh}; | ||
| 252 | } | ||
| 253 | sub close_conf { | ||
| 254 | undef shift->{conf_fh}; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub source_file { | ||
| 258 | my $self = shift; | ||
| 259 | return File::Spec->catfile($self->sources_path, $self->track_name); | ||
| 260 | } | ||
| 261 | |||
| 262 | sub load { | ||
| 263 | my $self = shift; | ||
| 264 | my ($initial_lines, $fh) = @_; | ||
| 265 | |||
| 266 | $self->flag_busy(1); | ||
| 267 | eval { | ||
| 268 | $self->set_status('starting load'); | ||
| 269 | |||
| 270 | mkdir $self->sources_path or die "Couldn't make ",$self->sources_path," directory: $!"; | ||
| 271 | my $source_file = IO::File->new($self->source_file, '>'); | ||
| 272 | |||
| 273 | $self->open_conf; | ||
| 274 | $self->start_load; | ||
| 275 | |||
| 276 | $self->set_status('load data'); | ||
| 277 | |||
| 278 | my $count = 0; | ||
| 279 | my $eol = $self->eol_char; | ||
| 280 | { | ||
| 281 | local $/ = $eol if $eol; | ||
| 282 | foreach (@$initial_lines) { | ||
| 283 | $source_file->print($_) if $source_file; | ||
| 284 | $self->load_line($_); | ||
| 285 | } | ||
| 286 | |||
| 287 | $count = @$initial_lines; | ||
| 288 | while (<$fh>) { | ||
| 289 | $source_file->print($_) if $source_file; | ||
| 290 | $self->load_line($_); | ||
| 291 | $self->set_status("loaded $count lines") if $count++ % 1000 == 0; | ||
| 292 | } | ||
| 293 | $source_file->close; | ||
| 294 | } | ||
| 295 | |||
| 296 | $self->finish_load($count); | ||
| 297 | $self->close_conf; | ||
| 298 | }; | ||
| 299 | $self->flag_busy(0); | ||
| 300 | |||
| 301 | die $@ if $@; | ||
| 302 | $self->set_processing_complete; | ||
| 303 | my @tracks = $self->tracks; | ||
| 304 | return @tracks; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub start_load { } | ||
| 308 | sub finish_load { } | ||
| 309 | sub flag_busy { | ||
| 310 | my $self = shift; | ||
| 311 | my $busy = shift; | ||
| 312 | my $busy_file = $self->busy_path; | ||
| 313 | |||
| 314 | if ($busy) { | ||
| 315 | my $fh = IO::File->new($busy_file,'>'); | ||
| 316 | } else { | ||
| 317 | unlink $busy_file; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | sub busy { | ||
| 322 | my $self = shift; | ||
| 323 | my $busy_file = $self->busy_path; | ||
| 324 | return -e $busy_file; | ||
| 325 | } | ||
| 326 | |||
| 327 | sub add_track { | ||
| 328 | my $self = shift; | ||
| 329 | my $label = shift; | ||
| 330 | |||
| 331 | $self->{_tracks} ||= {}; | ||
| 332 | $self->{_tracks}{$label}++; | ||
| 333 | } | ||
| 334 | sub tracks { | ||
| 335 | my $self = shift; | ||
| 336 | return unless $self->{_tracks}; | ||
| 337 | return keys %{$self->{_tracks}}; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub new_track_label { | ||
| 341 | my $self = shift; | ||
| 342 | my $type = shift; | ||
| 343 | $type ||= 'track'; | ||
| 344 | $type =~ tr/a-zA-Z0-9_/_/c; | ||
| 345 | |||
| 346 | my $loadid = $self->loadid; | ||
| 347 | $self->{_trackno}{$type} ||= 1; | ||
| 348 | |||
| 349 | my $label = $type."_${loadid}_$self->{_trackno}{$type}"; | ||
| 350 | $self->{_trackno}{$type}++; | ||
| 351 | $self->add_track($label); | ||
| 352 | return $label; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub load_line { | ||
| 356 | croak "virtual base class"; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub category { | ||
| 360 | my $self = shift; | ||
| 361 | return $self->force_category || (Bio::Graphics::Browser2::Util->translate('UPLOADED_TRACKS')||'').':'.$self->track_name; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub backend { | ||
| 365 | my $self = shift; | ||
| 366 | my $backend = $self->globals->upload_db_adaptor; | ||
| 367 | $backend = $self->guess_backend if $backend && $backend eq 'auto'; | ||
| 368 | unless ($backend) { | ||
| 369 | $backend = $self->guess_backend; | ||
| 370 | warn "No upload_db_adaptor option set in GBrowse.conf. Will try to use $backend."; | ||
| 371 | } | ||
| 372 | return $backend; | ||
| 373 | } | ||
| 374 | |||
| 375 | sub guess_backend { | ||
| 376 | my $self = shift; | ||
| 377 | my %db_drivers = map {$_=>1} DBI->available_drivers(1); | ||
| 378 | return 'DBI::SQLite' if $db_drivers{SQLite} && eval "require Bio::DB::SeqFeature::Store::DBI::SQLite; 1"; | ||
| 379 | return 'berkeleydb' if eval "require Bio::DB::SeqFeature::Store::berkeleydb; 1"; | ||
| 380 | return 'DBI::mysql' if $db_drivers{mysql}; | ||
| 381 | return 'memory'; | ||
| 382 | } | ||
| 383 | |||
| 384 | sub dsn { | ||
| 385 | my $self = shift; | ||
| 386 | my $d = $self->{dsn}; | ||
| 387 | $self->{dsn} = shift if @_; | ||
| 388 | $d; | ||
| 389 | } | ||
| 390 | |||
| 391 | # Create a new database to hold a user's uploaded file. | ||
| 392 | sub create_database { | ||
| 393 | my $self = shift; | ||
| 394 | my $data_path = shift; | ||
| 395 | |||
| 396 | my $backend = $self->backend; | ||
| 397 | |||
| 398 | if ($backend =~ /DBI:+mysql/) { | ||
| 399 | my $globals = $self->settings->globals; | ||
| 400 | my $db_name = 'userdata_'.md5_hex($data_path); | ||
| 401 | $data_path = $db_name; | ||
| 402 | $db_name .= ";host=".$globals->upload_db_host; | ||
| 403 | $db_name .= ";user=".$globals->upload_db_user; | ||
| 404 | $db_name .= ";password=".$globals->upload_db_pass; | ||
| 405 | $self->dsn($db_name); | ||
| 406 | my $mysql_admin = $self->mysql_admin; | ||
| 407 | |||
| 408 | my $mysql_usage = <<END; | ||
| 409 | For mysql to work as a backend to stored user data, you must set up the server | ||
| 410 | so that the web server user (e.g. "www-data") has the privileges to create databases | ||
| 411 | named "userdata_*". The usual way to do this is with the mysql shell: | ||
| 412 | |||
| 413 | mysql> grant create on `userdata\_%`.* to www-data\@localhost | ||
| 414 | END | ||
| 415 | my $dbh = DBI->connect($mysql_admin) | ||
| 416 | or die DBI->errstr,".\n",$mysql_usage; | ||
| 417 | $dbh->do("drop database if exists `$data_path`"); | ||
| 418 | $dbh->do("create database `$data_path`") | ||
| 419 | or die "Could not create $data_path:",DBI->errstr,".\n",$mysql_usage; | ||
| 420 | } elsif ($backend eq 'DBI::SQLite') { | ||
| 421 | $self->dsn(File::Spec->catfile($data_path,'index.SQLite')); | ||
| 422 | } else { | ||
| 423 | $self->dsn($data_path); | ||
| 424 | } | ||
| 425 | |||
| 426 | return Bio::DB::SeqFeature::Store->new( | ||
| 427 | -adaptor => $backend, | ||
| 428 | -dsn => $self->dsn, | ||
| 429 | -create => 1); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub drop_databases { | ||
| 433 | my $self = shift; | ||
| 434 | my $conf_path = shift; | ||
| 435 | # hacky job here - just drop anything that looks like a mysql database | ||
| 436 | my (@dsns,$using_mysql); | ||
| 437 | open my $f,$conf_path or (warn "Couldn't open $conf_path: $!" && return); | ||
| 438 | while (<$f>) { | ||
| 439 | if (/-adaptor/) { | ||
| 440 | $using_mysql = /DBI::mysql/; | ||
| 441 | } | ||
| 442 | push @dsns,$1 if /-dsn\s+(.+)/i && $using_mysql; | ||
| 443 | } | ||
| 444 | close $f; | ||
| 445 | |||
| 446 | for my $dsn (@dsns) { | ||
| 447 | eval "require DBI" unless DBI->can('connect'); | ||
| 448 | my $mysql_admin = $self->mysql_admin; | ||
| 449 | my $dbh = DBI->connect($mysql_admin) | ||
| 450 | or die DBI->errstr; | ||
| 451 | $dsn =~ s/;.+$//; | ||
| 452 | $dbh->do("drop database `$dsn`") | ||
| 453 | or die "Could not drop $dsn:",DBI->errstr; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | |||
| 457 | # MySQL Admin - Returns the string which defines the custom uploads DB MySQL connection details. | ||
| 458 | sub mysql_admin { | ||
| 459 | my $self = shift; | ||
| 460 | my $globals = $self->globals; | ||
| 461 | my $db_host = $globals->upload_db_host; | ||
| 462 | my $db_user = $globals->upload_db_user; | ||
| 463 | my $db_pass = $globals->upload_db_pass; | ||
| 464 | eval "require DBI" unless DBI->can('connect'); | ||
| 465 | my $dsn = 'DBI:mysql:gbrowse_login;'; | ||
| 466 | my @options; | ||
| 467 | push @options,"host=$db_host" if $db_host; | ||
| 468 | push @options,"user=$db_user" if $db_user; | ||
| 469 | push @options,"password=$db_pass" if $db_pass; | ||
| 470 | return $dsn . join ';',@options; | ||
| 471 | } | ||
| 472 | |||
| 473 | 1; |