[Bio] / MGRASTBackend / Metagenome.pm Repository:
ViewVC logotype

View of /MGRASTBackend/Metagenome.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Thu Apr 30 16:38:02 2009 UTC (10 years, 5 months ago) by jared
Branch: jared, MAIN
CVS Tags: initial-version, HEAD
Changes since 1.1: +0 -0 lines
new dir

package Metagenome;

use FlowMaster;
use SOAP::Lite;
use DBMaster;
use FIG_Config;
use FileHandle;
use File::Basename;
use FileLocking qw(lock_file unlock_file lock_file_shared);

use strict;
use warnings;


# create new metagenome
sub create {
  my ($class, $data) = @_;
  
  my $jobs_dir = $FIG_Config::mgrast_jobs;
  

  # register Tax id
  
  #   if (exists $data->{'taxonomy_id'}) {    
  #     my $tid = $data->{'taxonomy_id'} || "666666";
  #     $tid =~ s/\s+//;    
  #     # query clearing house about taxonomy id extension
  #     my $proxy = SOAP::Lite->uri('http://www.soaplite.com/Scripts')->proxy($FIG_Config::clearinghouse_url);
  #     my $r = $proxy->register_genome($tid);
  #     if ($r->fault) {
  #       return (undef, "Failed to deposit: " . $r->faultcode . " " . $r->faultstring);
  #     }
  
  #     $data->{'taxonomy_id_ext'} = $tid . "." . $r->result;
  #   }
  #   else {
  #     $data->{'taxonomy_id_ext'} = '';
  #   }
  



  # init job counter if necessary
  umask 0000;
  unless (-f "$jobs_dir/JOBCOUNTER") {
    open(FH, ">$jobs_dir/JOBCOUNTER") or die "could not create jobcounter file: $!\n";
    print FH "1";
    close FH;
  }

  #
  # get new job id from job counter
  # Carefully lock and fsync().
  #
  open(FH, "+<$jobs_dir/JOBCOUNTER") or die "could not open jobcounter file: $!\n";
  FH->autoflush(1);
  lock_file(\*FH);
  seek(FH, 0, SEEK_SET);
  my $jobnumber = <FH>;
  
  $jobnumber++;
  while (-f $jobs_dir.'/'.$jobnumber) {
    $jobnumber++;
  }
  
  seek(FH, 0, SEEK_SET);
  FH->truncate(0);
  print FH "$jobnumber\n";

  eval { File::Sync::fsync(\*FH) if $have_fsync; };
  
  close FH;
  


 
  # create job directory
  my $job_dir = $jobs_dir.'/'.$jobnumber;
  mkdir $job_dir;
  
  unless (-f $job_dir) {
    return (undef, 'The job directory could not be created.');
  }
  
  mkdir "$job_dir/raw";
  if ($data->{'taxonomy_id_ext'}) {
     mkdir "$job_dir/raw/" . $data->{'taxonomy_id_ext'};
     $data->{'tax_dir'} = "$job_dir/raw/".$data->{'taxonomy_id_ext'};
   }
  

  # create meta.xml file  
  my $meta_id = 'metagenome_'.$jobnumber;
  open(FH, ">". "$job_dir/MGRAST2");
  close(FH);
  
  my $meta;
  if ($FIG_Config::meta_use_db and $have_genome_meta_db){
    $meta = new GenomeMetaDB($meta_id, "$job_dir/meta.xml");
  } else {
    $meta = new GenomeMeta($meta_id, "$job_dir/meta.xml");
  }
  
  $meta->add_log_entry("genome", "Created $job_dir for uploaded file by " . $data->{'user'});
  


  #create job dir files 
  #need to add db writes for these values
  open(FH, ">" . $job_dir . "/GENOME") or die "could not open GENOME file in $job_dir: $!\n";
  print FH $data->{'genome'}."\n";
  close FH;
  
  open(FH, ">" . $job_dir . "/PROJECT") or die "could not open PROJECT file in $job_dir: $!\n";
  print FH $data->{'project'}."\n";
  close FH;
  
  open(FH, ">" . $job_dir . "/TAXONOMY") or die "could not open TAXONOMY file in $job_dir: $!\n";
  print FH $data->{'taxonomy'}."\n";
  close FH;
  
  open(FH, ">" . $job_dir . "/GENOME_ID") or die "cannot open GENOME_ID file in $job_dir: $!\n";
  print FH $data->{'taxonomy_id_ext'}."\n";
  close(FH);
  
  open(FH, ">" . $job_dir . "/USER") or die "cannot open USER file in $job_dir: $!\n";
  print FH $data->{'user'}."\n";
  close(FH);

  if ($data->{'metagenome'}) {
    open(FH, ">" . $job_dir . "/METAGENOME") or die "cannot open METAGENOME file in $job_dir: $!\n";
    close(FH);
  }

  open(FH, ">" . $job_dir . "/ACTIVE") or die "cannot open ACTIVE file in $job_dir: $!\n";
  close(FH);
  $meta->add_log_entry("genome", "Job set to active.");


  #crude meta data 
  if (defined $data->{'meta'} and ref $data->{'meta'} eq 'HASH') {
    foreach my $key (keys(%{$data->{'meta'}})) {
      $meta->set_metadata($key, $data->{'meta'}->{$key});
    }
  }  

  $meta->add_log_entry("genome", "Created metadata files.");  
  $meta->set_metadata("upload.timestamp", time);
  $meta->set_metadata("status.uploaded", "complete");
  
  return ($jobnumber,'');
}

# load existing metagenome 
sub init {
  my ($class, $job_id) = @_;
  
  unless($job_id){
    die "metagenome init called without job id";
  }

  my $handle = _dbh();  
  unless(ref $handle and $handle->isa('DBMaster')){
    die "metagenome init failed to get a DBMaster for jobcache";
  }
  
  my $job = $handle->Job->init({id=>$job_id}) or die "no metagenome id : $job_id found in jobcache db";
  
  my $self = {job => $job,
	      job_dir => $FIGConfig:mgrast_jobs."/".$job->id(),
	      id => $job->id(),
	      genome_id => $job->genome_id(),
	      genome_name => $job->genome_name(),
	      project_name => $job->project_name(),
	      created_on => $job->created_on(),
	      last_modified => $job->last_modified(),
	      type => "metagenome",
	      metagenome_type => $job->metagenome_type(),
	      active => $job->active(),
	      done => $job->done(),
	      public => $job->public(),
	      viewable => $job->viewable(),
	      size => $job->size(),
	      server_version => $job->server_version(),
	      genome_bp_count => $job->genome_bp_count(),
	      genome_contig_count => $job->genome_contig_count(),
	     };
  
  return $self;
 
}


# internal functions

sub _dbh {
  my ($class) = @_;
  my $dbh;

  eval {
    $dbh = DBMaster->new( -database => $FIG_Config::mgrast_jobcache_db,
				      -host     => $FIG_Config::mgrast_jobcache_host,
				      -user     => $FIG_Config::mgrast_jobcache_user,
				      -password => $FIG_Config::mgrast_jobcache_password );
  };
  if ($@) {
    warn "Unable to connect to MGRAST database: $@\n";
    return undef;
  }

  return $dbh;
}

sub file_write_or_replace {
  my ($self, $file, $content) = @_;

  if(-f $file){
    unlink($file) or die "could not delete $file: $!\n";
  }

  open(FH, ">$file") or die "could not open $file: $!\n";
  print FH "$content\n";
  close FH;  
}

# accessor / setter methods 

# check if all hell will break out if this gets changed later
# maybe do not allow this to change once it is set
sub genome_id {
  my ($self, $gid) = @_; 

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($gid){
    my $job_dir = $self->{job_dir};

    $self->{genome_id} = $gid;
    $self->{job}->genome_id($gid);
    $self->file_write_or_replace($job_dir."/GENOME_ID", $gid)
  } 

  return $self->{genome_id};
}

sub genome_name {
  my ($self, $name) = @_; 

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($name){
    my $job_dir = $self->{job_dir};

    $self->{genome_name} = $name;
    $self->{job}->genome_name($name);
    $self->file_write_or_replace($job_dir."/GENOME", $name)
  } 

  return $self->{genome_name};
}

sub project {
  my ($self, $prj) = @_; 

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($prj){
    my $job_dir = $self->{job_dir};

    $self->{project} = $prj;
    $self->{job}->project($prj);
    $self->file_write_or_replace($job_dir."/PROJECT", $prj)
  } 

  return $self->{project};
}

sub user {
  my ($self, $prj) = @_; 

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($prj){
    my $job_dir = $self->{job_dir};

    $self->{project} = $prj;
    $self->{job}->project($prj);
    $self->file_write_or_replace($job_dir."/PROJECT", $prj)
  } 

  return $self->{project};
}

# public will need to be handled slightly differently
sub public {
  return $self->{public};
}

# boolean files
sub active {
  my ($self, $val) = @_;
  my $file = $self->{job_dir}."/ACTIVE";

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($val eq 0){
    if(-f $file){
      unlink($file) or die "unable to delete $file: $!\n";
    }
    $self->{active} = $val;
    $self->{job}->active($val);
    return $self->{active};
  } elsif ($val eq 1) {
    if(-f $file){
      open(FH, ">$file") or die "could not open $file: $!\n";
      close FH;
    }
    $self->{active} = $val;
    $self->{job}->active($val);
    return $self->{active};
  } else {
    return $self->{active};
  }
}

sub done {
  my ($self, $val) = @_;
  my $file = $self->{job_dir}."/DONE";

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($val eq 0){
    if(-f $file){
      unlink($file) or die "unable to delete $file: $!\n";
    }
    $self->{done} = $val;
    $self->{job}->done($val);
    return $self->{done};    
  } elsif ($val eq 1) {
    unless(-f $file){
      open(FH, ">$file") or die "could not open $file: $!\n";
      close FH;
    }
    $self->{done} = $val;
    $self->{job}->done($val);
    return $self->{done};
  } else {
    return $self->{done};
  }
}

sub type {
  my ($self, $val) = @_;
  my $file = $self->{job_dir}."/METAGENOME";

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }

  if($val eq "genome"){
    if(-f $file){
      unlink($file) or die "unable to delete $file: $!\n";
    }
    $self->{type} = $val;
    return $self->{type};    
  } elsif ($val eq "metagenome") {
    if(-f $file){
      open(FH, ">$file") or die "could not open $file: $!\n";
      close FH;
    }
    $self->{type} = $val;
    return $self->{type};
  } else {
    return $self->{type};
  }
}

# other accessors / setters 
sub metagenome_type {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->metagenome_type($val || "");
}

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

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->id($val || "");
}

sub created_on {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->created_on($val || "");
}

sub last_modified {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->last_modified($val || "");
}

sub metagenome_type {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->metagenome_type($val || "");
}

sub viewable {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->viewable($val || "");
}	   

sub size {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->size($val || "");
}

sub server_version {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->server_version($val || "");
}

sub genome_bp_count {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->genome_bp_count($val || "");
}

sub genome_contig_count {
  my ($self, $val) = @_;

  # check if we are called properly
  unless (ref $self) {
    die "Not called as an object method.";
  }
  
  return $self->{job}->genome_contig_count($val || "");
}

1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3