[Bio] / FigKernelPackages / FIG.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/FIG.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.358, Thu Sep 1 20:52:51 2005 UTC revision 1.359, Fri Sep 2 22:21:57 2005 UTC
# Line 8  Line 8 
8  use IPC::Open2;  use IPC::Open2;
9  use MIME::Base64;  use MIME::Base64;
10  use File::Basename;  use File::Basename;
11    use FileHandle;
12    
13  use DBrtns;  use DBrtns;
14  use Sim;  use Sim;
# Line 6798  Line 6799 
6799      return 0;      return 0;
6800  }  }
6801    
6802    =head3 add_annotation_batch
6803    
6804    C<< my ($n_added, $badList) = $fig->add_annotation_batch($file); >>
6805    
6806    Install a batch of annotations.
6807    
6808    =over 4
6809    
6810    =item file
6811    
6812    File containing annotations.
6813    
6814    =item RETURN
6815    
6816    Returns the number of annotations successfully added in $n_added. If annotations failed,
6817    they are returned in $badList as a tuple [$peg, $error_msg, $entry].
6818    
6819    =back
6820    
6821    =cut
6822    
6823    #
6824    # This method exists because it is hugely slow to add a large number
6825    # of annotations with add_annotation (it opens and closes the annotation
6826    # file for each individual annotation, and uses individual INSERT statements
6827    # to update the database). This method batches updates to the files and creates
6828    # a load file for the database update.
6829    #
6830    # if the annotations are sorted by genome, so much the better: it will
6831    # do a single file open for the annotation file for that genome.
6832    #
6833    
6834    sub add_annotation_batch
6835    {
6836        my($self, $file) = @_;
6837    
6838        my $anno_fh = new FileHandle("<$file");
6839    
6840        if (not $anno_fh)
6841        {
6842            confess "Cannot open $file for reading: $!\n";
6843        }
6844    
6845        my $dbtmp = "$FIG_Config::temp/add_anno_db.$$";
6846    
6847        my $dbfh = new FileHandle(">$dbtmp");
6848        if (not $dbfh)
6849        {
6850            confess "Cannot write database tmpfile $dbtmp for writing: $!\n";
6851        }
6852    
6853        local $/ = "///\n";
6854        my $count = 0;
6855    
6856        my $last_file;
6857        my $anno_out_fh;
6858        my $errors = [];
6859    
6860        while (my $anno = <$anno_fh>)
6861        {
6862            chomp $anno;
6863    
6864            my ($feature_id, $time_made, $user, $annotation) = split(/\n/, $anno, 4);
6865    
6866            if ($feature_id eq '' or $time_made eq '' or $user eq '' or $annotation eq '')
6867            {
6868                push(@$errors, [$feature_id, "Empty fields in annotation", $anno]);
6869                next;
6870            }
6871    
6872            next if $self->is_deleted_fid($feature_id);
6873    
6874            my $genome = $self->genome_of($feature_id);
6875            if (not $genome)
6876            {
6877                push(@$errors, [$feature_id, "no genome found for fid '$feature_id'", $anno]);
6878                next;
6879            }
6880    
6881            my $real_file = "$FIG_Config::organisms/$genome/annotations";
6882            my $file = "$FIG_Config::temp/testdir/$genome";
6883            &verify_dir($file);
6884            $file .= "/annotations";
6885            my $fileno = $self->file2N($real_file);
6886            my $ma   = ($annotation =~ /^Set master function to/) ? 1 : 0;
6887    
6888            #
6889            # if this is the first time through or if we have a new file, close and reopen.
6890            #
6891            if (not $last_file or $file ne $last_file)
6892            {
6893                close($anno_out_fh) if $anno_out_fh;
6894                chmod 0777, $last_file;
6895                print "Close $last_file, open $file\n";
6896                $anno_out_fh = new FileHandle(">>$file");
6897                if (not $anno_out_fh)
6898                {
6899                    push(@$errors, [$feature_id, "cannot open annotation file $file: $!", $anno]);
6900                    next;
6901                }
6902                $last_file = $file;
6903                flock($anno_out_fh, LOCK_EX)  or confess "cannot lock assigned_functions $file: $!";
6904                seek($anno_out_fh, 0, 2)      or confess "failed to seek to the end of the file $file: $!";
6905            }
6906    
6907            # Tweaked this section for Windows compatability. The size on disk of
6908            # "\n" is not constant.
6909            my $seek1 = tell $anno_out_fh;
6910    
6911            my $dataLine = "$feature_id\n$time_made\n$user\n$annotation" . ((substr($annotation,-1) eq "\n") ? "" : "\n");
6912            print $anno_out_fh $dataLine . "//\n";
6913            my $ln = length($dataLine);
6914    
6915            print $dbfh join("\t", $feature_id, $time_made, $user, $ma, $fileno, $seek1, $ln), "\n";
6916            $count++;
6917        }
6918        close($anno_out_fh);
6919        chmod 0777, $last_file;
6920        print "Loading $count annotations into database from $dbtmp\n";
6921    
6922        close($dbfh);
6923    
6924        my $rows = $self->db_handle()->load_table(file => $dbtmp,
6925                                                  tbl => 'annotation_seeks');
6926        print "Loaded $rows rows\n";
6927        return $count, $errors;
6928    }
6929    
6930  =head3 merged_related_annotations  =head3 merged_related_annotations
6931    
6932  usage: @annotations = $fig->merged_related_annotations($fids)  usage: @annotations = $fig->merged_related_annotations($fids)

Legend:
Removed from v.1.358  
changed lines
  Added in v.1.359

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3