/usr/share/perl5/Bio/FeatureIO/chadobulk.pm is in libchado-perl 1.23-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | package Bio::FeatureIO::chadobulk;
use strict;
use base qw(Bio::FeatureIO);
use Bio::SeqIO;
use Bio::GMOD::Config;
use DBI;
use Data::Dumper;
my @TABLES = qw(db dbxref cvterm synonym feature featureloc
featureprop feature_cvterm feature_dbxref
feature_relationship feature_synonym
);
#don't need contact, obj already handed to us
#don't need organism, obj already handed to us
#don't need pub, obj already handed to us
sub _initialize {
my($self,%arg) = @_;
$self->SUPER::_initialize(%arg);
$self->organism($arg{-organism});
$self->contact($arg{-contact});
$self->pub($arg{-pub});
#my $config = Bio::GMOD::Config->new();
#warn Dumper($config);
$self->dbh(DBI->connect('dbi:Pg:host=soleus;dbname=chado_gec','allenday','allenday'));
$self->setup();
}
sub next_feature {
shift->throw('this class only writes to database');
}
sub write_feature {
my($self,$feature) = @_;
my $dbxref_id = "\\N";
my $organism_id = 1; #FIXME
my $feature_id = $self->write_row_feature($feature);
next unless $feature_id;
$self->write_row_featureloc($feature , $feature_id);
$self->write_row_feature_relationship($feature,$feature_id);
}
sub DESTROY {
my $self = shift;
$self->bulkload();
$self->cleanup();
}
=head2 write_row_feature
Title : write_row_feature
Usage :
Function:
Example :
Returns :
Args :
=cut
sub write_row_feature{
my ($self,$feature) = @_;
# Column | Type | Modifiers
#------------------+-----------------------------+----------
# feature_id | integer | not null
my $feature_id = $self->seq('feature');
# dbxref_id | integer |
my $dbxref_id = '\\N';
# organism_id | integer | not null
my $organism_id = 1; #FIXME
#my $organism_id = $self->organism->id;
# name | character varying(255) |
my @names = map {$_->value} $feature->annotation->get_Annotations('Name');
my $name = $names[0];
if(scalar(@names) > 1){
$self->throw('feature can only have one Name, (this is a limitation in the loader and/or gff spec, not the file) but had: '.join(' ',@names));
}
# uniquename | text | not null
my @unames = map {$_->value} $feature->annotation->get_Annotations('ID');
my $uname = $unames[0];
if(defined($uname) and $self->feature_id($uname)){
my $saw = $self->feature_id($uname);
$self->warn("already saw $uname, returning previous record $saw. FIXME we should still try to extract new attributes");
return $saw;
} elsif(defined($uname)){
$self->feature_id($uname,$feature_id);
} else {
$uname = $feature->seq_id.':'.$feature->start.','.$feature->end;
}
if(scalar(@unames) > 1){
$self->throw('feature can only have one ID (this is a limitation in the loader and/or gff spec, not the file), but had: '.join(' ',@unames));
}
$name ||= $uname;
# residues | text |
my $residues = '\\N';
# seqlen | integer |
my $seqlen = '\\N';
# md5checksum | character(32) |
my $md5 = '\\N';
# type_id | integer | not null
my @types = map { $self->cvterm($_->name) } $feature->annotation->get_Annotations('feature_type');
my $type = $types[0];
if(scalar(@types) > 1){
$self->throw('feature can only have one type, but had: '.join(' ',@types));
}
$self->throw("$feature has undefined feature_type, bailing out") unless $type;
# is_analysis | boolean | not null
my $is_analysis = 'F';
# timeaccessioned | timestamp without time zone | not null
my $timeaccessioned = $self->now();
# timelastmodified | timestamp without time zone | not null
my $timelastmodified = $self->now();
my $feature_fh = $self->file('feature');
print $feature_fh join("\t",
$feature_id,$dbxref_id,$organism_id,$name,$uname,
$residues,$seqlen,$md5,$type,$is_analysis,
$timeaccessioned,$timelastmodified),"\n";
return $feature_id;
}
=head2 write_row_featureloc
Title : write_row_featureloc
Usage :
Function:
Example :
Returns :
Args :
=cut
sub write_row_featureloc{
my ($self,$feature,$feature_id) = @_;
# Column | Type | Modifiers
#-----------------+----------+-----------------------------------------------------------------------
# featureloc_id | integer | not null
my $featureloc_id = $self->seq('featureloc');
# feature_id | integer | not null
#passed as arg
# srcfeature_id | integer |
my $srcfeature_id = $self->feature_id($feature->seq_id);
return undef unless defined($srcfeature_id);
# fmin | integer |
my $fmin = $feature->start() || 0;
# is_fmin_partial | boolean | not null
my $is_fmin_partial = 'F';
# fmax | integer |
my $fmax = $feature->end() + 1;
# is_fmax_partial | boolean | not null
my $is_fmax_partial = 'F';
# strand | smallint |
my $strand = $feature->strand();
# phase | integer |
my $phase = $feature->frame();
$phase = '\\N' if $phase eq '.';
# residue_info | text |
my $residue_info = '\\N';
# locgroup | integer | not null
my $locgroup = 0;
# rank | integer | not null
my $rank = $self->featureloc_rank($feature_id);
my $featureloc_fh = $self->file('featureloc');
print $featureloc_fh join("\t",
$featureloc_id,$feature_id,$srcfeature_id,$fmin,$is_fmin_partial,
$fmax,$is_fmax_partial,$strand,$phase,$residue_info,$locgroup,$rank),"\n";
return $featureloc_id;
}
=head2 write_row_feature_relationship
Title : write_row_feature_relationship
Usage :
Function:
Example :
Returns :
Args :
=cut
sub write_row_feature_relationship{
my ($self,$feature,$feature_id) = @_;
my $parent = ($feature->annotation->get_Annotations('Parent'))[0];
#warn $parent;
return undef unless $parent;
my $parent_id = $self->feature_id($parent->value());
my $part_of = $self->cvterm('part_of');
my $feature_relationship_id = $self->seq('feature_relationship');
my $fh = $self->file('feature_relationship');
print $fh join("\t",
$feature_relationship_id,$feature_id,$parent_id,$part_of,'\\N',0
),"\n";
}
=head2 setup
Title : setup
Usage : $obj->setup()
Function:
Example :
Returns :
Args :
=cut
sub setup {
my $self = shift;
my $dbh = $self->dbh();
$dbh->begin_work();
$self->setup_seq();
$self->setup_files();
$self->now();
}
=head2 cleanup
Title : cleanup
Usage :
Function:
Example :
Returns :
Args :
=cut
sub cleanup{
my $self = shift;
$self->cleanup_seq();
$self->cleanup_files();
$self->dbh->commit();
}
=head2 bulkload
Title : bulkload
Usage :
Function:
Example :
Returns :
Args :
=cut
sub bulkload{
my $self = shift;
foreach my $t(@TABLES){
$self->file($t,"/tmp/$t.dat",2); #close
}
foreach my $t(@TABLES){
$self->file($t,"/tmp/$t.dat",3); #open for read
}
}
=head2 now
Title : now
Usage :
Function:
Example :
Returns :
Args :
=cut
sub now{
my $self = shift;
if(!$self->{'now'}){
my $dbh = $self->dbh;
my $sth = $dbh->prepare('SELECT NOW()');
$sth->execute();
my($now) = $sth->fetchrow_array();
$self->{'now'} = $now;
}
return $self->{'now'};
}
=head2 setup_files
Title : setup_files
Usage :
Function:
Example :
Returns :
Args :
=cut
sub setup_files{
my ($self,@args) = @_;
foreach my $t(@TABLES){
$self->file($t,"/tmp/$t.dat",1); #open for write
}
}
=head2 cleanup_files
Title : cleanup_files
Usage :
Function:
Example :
Returns :
Args :
=cut
sub cleanup_files{
my ($self,@args) = @_;
foreach my $t(@TABLES){
$self->file($t,"/tmp/$t.dat",2); #close
}
}
=head2 setup_seq
Title : setup_seq
Usage :
Function:
Example :
Returns :
Args :
=cut
sub setup_seq{
my $self = shift;
my $dbh = $self->dbh();
foreach my $t (@TABLES){
$dbh->do("LOCK TABLE $t IN SHARE MODE");
$self->seq($t);
}
}
=head2 cleanup_seq
Title : cleanup_seq
Usage :
Function:
Example :
Returns :
Args :
=cut
sub cleanup_seq{
my $self = shift;
foreach my $t (@TABLES){
$self->seq($t,$self->seq($t));
}
}
=head2 seq
Title : seq
Usage :
Function:
Example :
Returns :
Args :
=cut
sub seq{
my($self,$seq,$val) = @_;
my $seqname = sprintf("%s_%s_id_seq",$seq,$seq);
my $dbh = $self->dbh();
if(defined($seq) && defined($val)){
$dbh->do("SELECT setval('public.$seqname',$val);") or die $dbh->errstr;
} elsif(defined($seq) && !defined($self->{'seq'}{$seq})){
my $sth = $dbh->prepare("SELECT nextval(?);") or die $dbh->errstr;
$sth->execute("public.$seqname");
my($nextval) = $sth->fetchrow_array();
$self->{'seq'}{$seq} = $nextval + 1;
return $nextval;
} else {
my $return = $self->{'seq'}{$seq};
$self->{'seq'}{$seq}++;
return $return;
}
}
=head2 featureloc_rank
Title : featureloc_rank
Usage :
Function:
Example :
Returns :
Args :
=cut
sub featureloc_rank{
my ($self,$feature_id) = @_;
my $r = $self->{'featureloc_rank'}{$feature_id} || 0;
$self->{'featureloc_rank'}{$feature_id}++;
return $r;
}
=head2 feature_id
Title : feature_id
Usage :
Function:
Example :
Returns :
Args :
=cut
sub feature_id{
my ($self,$id,$feature_id) = @_;
if(defined($feature_id)){
$self->{'feature_id'}{$id} = $feature_id;
} elsif(!defined($self->{'feature_id'}{$id})){
#do dbi lookup and cache here
if(!$self->{'feature_id_sth'}){
my $dbh = $self->dbh();
$self->{'feature_id_sth'} = $dbh->prepare('select feature_id from feature where name = ?');
}
$self->{'feature_id_sth'}->execute($id);
$self->throw("too many records for $id") if $self->{'feature_id_sth'}->rows() > 1;
my($x) = $self->{'feature_id_sth'}->fetchrow_array();
$self->{'feature_id'}{$id} = $x;
}
return $self->{'feature_id'}{$id};
}
=head2 file
Title : file
Usage :
Function:
Example :
Returns :
Args :
=cut
sub file{
my ($self,$table,$file,$action) = @_;
if(defined($action)){
if($action == 1){ #open for write
open(my $h, ">$file") or $self->throw($!);
$self->{'fh'}{$table} = $h;
} elsif($action == 2){ #close
close($self->{'fh'}{$table}) or $self->throw($!);
} elsif($action == 3){ #open for read
open(my $h, $file) or $self->throw($!);
$self->{'fh'}{$table} = $h;
} else {
$self->throw('no such action');
}
} elsif(defined($table)){
return $self->{'fh'}{$table};
} else {
return undef;
}
}
=head2 cvterm
Title : cvterm
Usage : $obj->cvterm()
Function:
Example :
Returns :
Args :
=cut
sub cvterm {
my $self = shift;
my $name = shift;
if(!$self->{'cvterm_sth'}){
my $dbh = $self->dbh;
#FIXME this does not handle SO:xxxxxxx identifiers from dbxref, which requires an OUTER JOIN query
my $sth = $dbh->prepare("select cvterm_id from cvterm c,dbxref d where c.name = ?;");
$self->{'cvterm_sth'} = $sth;
}
if(!$self->{'cvterm'}{$name}){
$self->{'cvterm_sth'}->execute($name);
my $id = $self->{'cvterm_sth'}->fetchrow_array();
$self->throw("couldn't find cvterm for $name") unless defined($id);
$self->{'cvterm'}{$name} = $id;
}
return $self->{'cvterm'}{$name};
}
=head2 dbh
Title : dbh
Usage : $obj->dbh($newval)
Function:
Example :
Returns : value of dbh (a scalar)
Args : on set, new value (a scalar or undef, optional)
=cut
sub dbh{
my $self = shift;
return $self->{'dbh'} = shift if @_;
return $self->{'dbh'};
}
=head2 contact
Title : contact
Usage : $obj->contact($newval)
Function:
Example :
Returns : value of contact (a scalar)
Args : on set, new value (a scalar or undef, optional)
=cut
sub contact{
my $self = shift;
return $self->{'contact'} = shift if @_;
return $self->{'contact'};
}
=head2 organism
Title : organism
Usage : $obj->organism($newval)
Function:
Example :
Returns : a Bio::Chado::CDBI::Organism object
Args : a Bio::Chado::CDBI::Organism object
=cut
sub organism{
my $self = shift;
return $self->{'organism'} = shift if @_;
return $self->{'organism'};
}
=head2 pub
Title : pub
Usage : $obj->pub($newval)
Function:
Example :
Returns : value of pub (a scalar)
Args : on set, new value (a scalar or undef, optional)
=cut
sub pub{
my $self = shift;
return $self->{'pub'} = shift if @_;
return $self->{'pub'};
}
1;
|