9 load_solcap_images.pl -D [ sandbox | cxgn | trial ] -H hostname -i dirname
13 Loads images into the SGN database, using the SGN::Image framework.
14 Then link the loaded image with the relevant stock (see phenome.stock_image table)
16 Requires the following parameters:
22 a database parameter, which can either be "cxgn", "sandbox", or "trial". "cxgn" and "sandbox" will cause the script to connect to the respective databases; "trial" will connect to sandbox, but not perform any of the database modifications.
30 a dirname that contains image filenames or subdirectories named after database accessions, containing one or more images (see option -d) .
34 use name - from sgn_people.sp_person.
38 location, as stored in stockprop (when plots are loaded with load_solcap_plots.pl)
42 files are stored in sub directories named after database accessions
46 image file extension . Defaults to 'jpg'
51 trial mode . Nothing will be stored.
56 The script will generate an error file, named like the filename supplied, with the extension .err.
60 Naama Menda (nm249@cornell.edu) September 2011.
66 use CXGN
::Metadata
::Schema
;
67 use CXGN
::Metadata
::Metadbdata
;
68 use CXGN
::DB
::InsertDBH
;
70 use Bio
::Chado
::Schema
;
71 use CXGN
::People
::Person
;
79 our ($opt_H, $opt_D, $opt_t, $opt_i, $opt_u, $opt_d, $opt_e, $opt_f, $opt_l);
80 getopts
('H:D:u:i:e:f:tdl:');
85 my $sp_person = $opt_u;
86 my $location = $opt_l;
88 my $ext = $opt_e || 'jpg';
90 if (!$dbhost && !$dbname) {
91 print "dbhost = $dbhost , dbname = $dbname\n";
92 print "opt_t = $opt_t, opt_u = $opt_u, opt_d= $opt_d opt_i = $dirname\n";
96 if (!$dirname) { print "dirname = $dirname\n" ; usage
(); }
98 my $dbh = CXGN
::DB
::InsertDBH
->new( { dbhost
=>$dbhost,
102 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } , { on_connect_do
=> ['SET search_path TO public;'] }
104 my $metadata_schema = CXGN
::Metadata
::Schema
->connect(
105 sub { $dbh->get_actual_dbh },
106 { on_connect_do
=> ['SET search_path TO metadata;'] },
108 my $sp_person_id= CXGN
::People
::Person
->get_person_by_username($dbh, $sp_person);
112 my $ch = SGN
::Context
->new();
113 my $image_dir = $opt_f || $ch->get_conf("image_dir");
115 print "PLEASE VERIFY:\n";
116 print "Using dbhost: $dbhost. DB name: $dbname. \n";
117 print "Path to image is: $image_dir\n";
120 if ($a !~ /[yY]/) { exit(); }
122 if (($dbname eq "sandbox") && ($image_dir !~ /sandbox/)) {
123 die "The image directory needs to be set to image_files_sandbox if running on rubisco/sandbox. Please change the image_dir parameter in SGN.conf\n\n";
125 if (($dbname eq "cxgn") && ($image_dir =~ /sandbox/)) {
126 warn "The image directory needs to be set to image_files when the script is running on the production database. Please change the image_dir parameter in SGN.conf\n\n";
129 my %image_hash = (); # used to retrieve images that are already loaded
130 my %connections = (); # keep track of object -- image connections that have already been made.
132 my $object_rs = $schema->resultset("Stock::Stock")->search( { } ) ;
133 while (my $object = $object_rs->next ) {
134 my $id = $object->stock_id;
135 my $name = $object->name;
136 $name2id{lc($name)} = $id;
139 # cache image chado object - image links to prevent reloading of the
142 print "Caching image-stock links...\n";
144 my $q = "SELECT * FROM phenome.stock_image";
145 my $sth = $dbh->prepare($q);
147 while ( my $hashref = $sth->fetchrow_hashref() ) {
148 my $image_id = $hashref->{image_id
};
149 my $chado_table_id = $hashref->{stock_id
}; ##### table specific
150 my $i = SGN
::Image
->new($dbh, $image_id);
151 my $original_filename = $i->get_original_filename();
152 $image_hash{$original_filename} = $i; # this doesn't have the file extension
153 $connections{$image_id."-".$chado_table_id}++;
156 open (ERR
, ">load_solcap_images.err") || die "Can't open error file\n";
158 my @files = glob "$dirname/*.$ext";
159 @files = glob "$dirname/*" if $opt_d ;
162 my $new_image_count = 0;
165 foreach my $file (@files) {
168 @sub_files = ($file);
169 @sub_files = glob "$file/*.$ext" if $opt_d;
171 my $object_name = basename
($file, ".$ext" );
172 print "object_name = '".$object_name."' \n";
173 #$individual_name =~s/(W\d{3,4}).*\.JPG/$1/i if $individual_name =~m/^W\d{3}/;
174 #2009_oh_8902_fruit-t
176 #my ($year, $place, $plot, undef) = split /_/ , $object_name;
177 #2009_ca_2138_fruit_c.jpg
178 my ($year, $place, $name, undef,undef) = split /_/ , $object_name;
180 #my $stock = $schema->resultset("Stock::Stock")->find( {
181 # stock_id => $name2id{ lc($name) } } );
182 my ($stock) = $schema->resultset("Stock::Stockprop")->search(
183 { value
=> $location, })->search_related('stock', { name
=> $name } );
186 warn "no stock found for plot # $name ! Skipping !!\n\n";
190 foreach my $filename (@sub_files) {
192 print STDOUT
"Processing file $file...\n";
193 print STDOUT
"Loading $name, image $filename\n";
194 print ERR
"Loading $name, image $filename\n";
195 my $image_id; # this will be set later, depending if the image is new or not
196 if (! -e
$filename) {
197 warn "The specified file $filename does not exist! Skipping...\n";
201 if (!exists($name2id{lc($name)})) {
202 message
("$name does not exist in the database...\n");
206 print ERR
"Adding $filename...\n";
207 if (exists($image_hash{$filename})) {
208 print ERR
"$filename is already loaded into the database...\n";
209 $image_id = $image_hash{$filename}->get_image_id();
210 $connections{$image_id."-".$name2id{lc($name)}}++;
211 if ($connections{$image_id."-".$name2id{lc($name)}} > 1) {
212 print ERR
"The connection between $name and image $filename has already been made. Skipping...\n";
214 elsif ($image_hash{$filename}) {
215 print ERR
qq { Associating stock
$name2id{lc($name)} with already loaded image
$filename...\n };
216 ################################
220 print ERR
qq { Generating new image object
for image
$filename and associating it with stock
$name, id
$name2id{lc($name) } ...\n };
224 print STDOUT
qq { Would associate file
$filename to stock
$name, id
$name2id{lc($name)}\n };
228 my $image = SGN
::Image
->new($dbh);
229 $image_hash{$filename}=$image;
231 $image->process_image("$filename", undef, undef);
232 $image->set_description("$caption");
233 $image->set_name(basename
($filename , ".$ext"));
234 $image->set_sp_person_id($sp_person_id);
235 $image->set_obsolete("f");
236 $image_id = $image->store();
241 my $metadata = CXGN
::Metadata
::Metadbdata
->new($metadata_schema, $sp_person);
242 my $metadata_id = $metadata->store()->get_metadata_id();
243 die "NO METADATA ID FOUND\n" if !$metadata_id;
244 #store the image_id - stock_id link
245 my $q = "INSERT INTO phenome.stock_image (stock_id, image_id, metadata_id) VALUES (?,?,?)";
246 my $sth = $dbh->prepare($q);
247 $sth->execute($stock->stock_id, $image_id, $metadata_id);
251 print STDOUT
"ERROR OCCURRED WHILE SAVING NEW INFORMATION. $@\n";
264 print STDOUT
"Inserted $new_image_count images.\n";
265 print STDOUT
"Done. \n";
268 print "Usage: load_images.pl -D dbname [ cxgn | sandbox ] -H dbhost -t [trial mode ] -i input dir -l location of the plots (as loaded into stockprop). \n Images will be stored in phenome.stock_image \n";
274 print STDOUT
$message;