[Bio] / IGSBPortal / IGSBDB.pm Repository:
ViewVC logotype

View of /IGSBPortal/IGSBDB.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Tue Jul 6 17:05:45 2010 UTC (9 years, 3 months ago) by wilke
Branch: MAIN
CVS Tags: HEAD
*** empty log message ***

package IGSBportal::IGSBDB;

use strict;
use warnings;

use DBMaster;
use FIG_Config;

#
# Add following lines to FIGConfig.pm
# $mgrast_metadata_db  = "MGRASTMetadata";
# $mgrast_metadata_host = "mg-rast.mcs.anl.gov";
# $mgrast_metadata_user  = "mgrast";
#


sub new {
  my ($class) = @_;
  my $self = {};

  eval {
      $self->{_handle} = DBMaster->new( -database => $FIG_Config::IGSB_db || 'IGSB',
					-host     => $FIG_Config::IGSB_dbhost,
					-user     => $FIG_Config::IGSB_dbuser,
					-password => $FIG_Config::IGSB_password || "");
    };
  if ($@) {
    warn "Unable to connect to IGSB db: $@\n";
    $self->{_handle} = undef;
  }
  
  bless ($self, $class);
  return $self;
  
}


sub handle {
    my ($self) = @_;
    return $self->{_handle};
}

sub get_request {
    my ($self , $id ) = @_ ;

    my $request = '';
    
    ($id) = $id =~/R?(\d+-\d+)/ ; 
    my $tmp = $self->handle->Request->get_objects( { ID => $id } );

    unless (@$tmp) {
	return ( -1 , "No request for ID $id") ;
    }
    elsif (scalar @$tmp == 1){
	$request = $tmp->[0];
    }
    else{
	return ( -2 , "Multiple requests for ID $id") ;
    }

    my $rows =  $self->handle->RequestData->get_objects( { request => $request });

    my $data = {} ;
    foreach my $entry (@$rows){
	$data->{ $entry->tag } = $entry->value ;
    }
   
    return ( 1 , "Retrieved data for request ID $id " , $data , $request , $rows ) ;
}

sub update_status{
  my ($self , $id , $status ) = @_;

  my ($success , $msg , $data , $request , $rows ) = $self->get_request($id) ;
  # push @{$request->status_history} , $status->current_status ;

  $request->current_status($status);
  return ( 1 , "Status updated for $id to ".  $request->current_status  , $request ) ;
}

sub get_requests_by_status{
  my ($self , $status) = @_;
  my $list = $self->handle->Request->get_objects( { current_status => $status } );
  return $list;
}


sub create_request_id {
    my ($self) = @_;
    my $dbh = $self->handle->db_handle ;
    
    my $time = $self->get_time;
    print STDERR $time , "\n";
#    my $sth = $dbh->prepare( "select ID from Request where ID regex $time order by ID desc" );
#    $sth->execute();
    
    my $statement = "select ID from Request where ID regexp '$time' order by ID desc" ;

    print STDERR $statement , "\n";
    my $last_ids  = $dbh->selectcol_arrayref($statement);

    print STDERR $last_ids , "\n";

    # sort IDs 
    if (ref $last_ids and scalar @$last_ids){
	print STDERR  "Returning " , join " " , @$last_ids , "\n";
	my ($date , $counter) = $last_ids->[0] =~ /(\d+)-(\d+)/ ;
	$counter++;
	print STDERR "Incrementing Counter: $date-$counter\n";
	return "$date-$counter";
    }
    else{
	return "$time-1" ;
    }
}

sub get_time{
    my ( $self ,  $format ) = @_;

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
    my $version = 0;

    $mon++;
    $year += 1900;

    $mday = "0".$mday if ( $mday < 10 );
    $min  = "0".$min if ($min < 10);
    $sec  = "0".$sec if ($sec < 10);
    $mon  = "0".$mon if ($mon < 10);

    my ($year_short) = $year =~ /(\d\d)$/;

    my $time = $year_short.$mon.$mday;
    
    return $time
}

sub create_request {
    my ($self, $id , $data) = @_;
    
    #print STDERR "1\n";
    my $request = '';
    # print STDERR $self->handle , "\n";
    
    $id = $self->create_request_id unless ($id) ;
    print STDERR "Request ID = $id\n";

    eval{
	$self->handle->Request->get_objects( { ID => $id } );
    };

    print STDERR $@ , "\n" if ($@);
    
    my $tmp = $self->handle->Request->get_objects( { ID => $id } );
  
    unless( scalar @$tmp ){
	print STDERR "Creating new request\n";
	eval{ $request = $self->handle->Request->create( { ID => $id ,
							   current_status => 'new' ,
							 } ); } ;
	print STDERR $@ if $@ ;
    } elsif ( scalar @$tmp == 1) {
	$request = $tmp->[0];
    } else {
	print STDERR "Multiple request with ID $id exists , aborting\n" ;
	return (-1 , "Multiple request with ID $id exists") ;
    }
    
    print STDERR "Adding to DB\n";
    my ($error , $msg) = $self->add2request( $request , $data) ;
    #print STDERR "4\n";
    return ($error , $msg , $id);
}

sub add2request{
    my ($self, $request , $data) = @_ ;

    print STDERR "Adding " . scalar (keys %$data) . " fields to DB\n";
    
    foreach my $tag (keys %$data){
        my $obj = $self->handle->RequestData->get_objects( { request => $request ,
                                                             tag =>$tag,
							 });
	
        if (scalar @$obj){
            print STDERR "Existing entry , need to update\n";
            $obj->[0]->value( $data->{ $tag } );
        }
        else{
            print STDERR "Create new entry\n";
	    eval{
		my $row = $self->handle->RequestData->create( 
							      {
								  request => $request ,
								  tag     => $tag ,
								  value   => ( $data->{$tag} || '') ,  
							      }
							      );
	    };
	    print STDERR $@ if $@ ; 
	}
    }
    return ( 1 , '');
}

sub search {
    my ($self, $values, $where) = @_;
    
    my $mddb = $self->{_handle};
    my $search_attributes = $mddb->RequestData->attributes();
    
    my ($search_tmp, $all_tmp, $metadata_objects); 
    foreach(keys %$where){
	if(defined $search_attributes->{$_}){
	    $search_tmp->{$_} = $where->{$_};
	} else {
	    $all_tmp->{$_} = $where->{$_};      
	}
    }
    
    if(scalar keys %$search_tmp){
	$metadata_objects = $mddb->RequestData->get_objects($search_tmp);
	unless (scalar @$metadata_objects){
	    return undef;
	}
    } else {
	return undef;
    }
}



sub get_all {
    my ($self, $job) = @_;
    
    my $mddb = $self->{_handle};
    
    my $attributes =  $mddb->JobMD->get_objects({job => $job});	
    
    return $attributes;
}



sub get_samples_cistrack{
  my ($self , $request_id) = @_ ;

  my $msg    =  '' ;
  my $status = 1 ;

  my @samples ;

  # get request object
  my $request = $self->handle->Request->init( { ID => $request_id } );
  unless ($request and ref $request){
    return [] , "no request for $request_id" , -1 ;
  }

  # get samples from cistrack

  my $response =`curl https://www.cistrack.org/browsedb2/meta-move/get_samples.html?id=R$request_id` ;

  return ([] , "no response $response for request $request_id" , -1) unless ($response);

  print STDERR "Response $response\n";


  my $cistrack = {} ;
  foreach my $line ( split ";" , $response ){
    my ($id , $name) = split "," , $line ;
    $cistrack->{$id} = $name  if ($id and $id =~/[\w+]/);
  }
    
  # check samples in local db
  for my $id (keys %$cistrack){
     my $sample = $self->handle->Sample->init( { ID => $id } );
     
     unless ($sample and ref $sample) {
       
       # not in local DB , create entry
       $sample = $self->handle->Sample->create( { ID      => $id ,
						  name    => $cistrack->{$id} ,
						  request => $request ,
						} );
     }
     
     push @samples , $sample

  }

  return  (\@samples , $msg , $status ) ;
}

sub get_samples{
   my ($self , $hash) = @_;
   return $self->handle->Sample->get_objects( $hash ) , '' , 1 ;
}

sub sample{
  my ($self , $id) = @_;
  return $self->handle->Sample->init( { ID => $id } );
}


1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3